altos/teletiny-v2.0: Support multiple SPI busses on CC1111
[fw/altos] / src / cc1111 / ao_spi.c
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 /* Default pin usage for existing Altus Metrum devices */
21
22 #ifndef SPI_CONST
23 #define SPI_CONST       0xff
24 #endif
25
26 /*
27  * USART0 SPI config alt 1
28  * 
29  *      MO      P0_3
30  *      MI      P0_2
31  *      CLK     P0_5
32  *      SS      P0_4
33  *
34  * USART0 SPI config alt 2
35  *
36  *      MO      P1_5
37  *      MI      P1_4
38  *      CLK     P1_3
39  *      CSS     P1_2
40  *
41  * USART1 SPI config alt 1
42  *
43  *      MO      P0_4
44  *      MI      P0_5
45  *      CLK     P0_3
46  *      SS      P0_2
47  *
48  * USART1 SPI config alt 2
49  *
50  *      MO      P1_6
51  *      MI      P1_7
52  *      CLK     P1_5
53  *      SS      P1_4
54  *
55  *
56  * Chip select is the responsibility of the caller in master mode
57  */
58
59 #if HAS_SPI_0
60 #define SPI_BUF_0       &U0DBUFXADDR
61 #define SPI_CSR_0       U0CSR
62 #define SPI_BAUD_0      U0BAUD
63 #define SPI_GCR_0       U0GCR
64 #define SPI_CFG_MASK_0  PERCFG_U0CFG_ALT_MASK
65 #define SPI_DMA_TX_0    DMA_CFG0_TRIGGER_UTX0
66 #define SPI_DMA_RX_0    DMA_CFG0_TRIGGER_URX0
67
68 #if SPI_0_ALT_1
69 #define SPI_CFG_0       PERCFG_U0CFG_ALT_1
70 #define SPI_SEL_0       P0SEL
71 #define SPI_BITS_0      (1 << 3) | (1 << 2) | (1 << 5)
72 #define SPI_CSS_BIT_0   (1 << 4)
73 #endif
74
75 #if SPI_0_ALT_2
76 #define SPI_CFG_0       PERCFG_U0CFG_ALT_2
77 #define SPI_SEL_0       P1SEL
78 #define SPI_PRI_0       P2SEL_PRI3P1_USART0
79 #define SPI_BITS_0      (1 << 5) | (1 << 4) | (1 << 3)
80 #define SPI_CSS_BIT_0   (1 << 2)
81 #endif
82
83 #endif
84
85 #if HAS_SPI_1
86 #define SPI_BUF_1       &U1DBUFXADDR
87 #define SPI_CSR_1       U1CSR
88 #define SPI_BAUD_1      U1BAUD
89 #define SPI_GCR_1       U1GCR
90 #define SPI_CFG_MASK_1  PERCFG_U1CFG_ALT_MASK
91 #define SPI_DMA_TX_1    DMA_CFG0_TRIGGER_UTX1
92 #define SPI_DMA_RX_1    DMA_CFG0_TRIGGER_URX1
93
94 #if SPI_1_ALT_1
95 #define SPI_CFG_1       PERCFG_U1CFG_ALT_1
96 #define SPI_SEL_1       P0SEL
97 #define SPI_BITS_1      (1 << 4) | (1 << 5) | (1 << 3)
98 #define SPI_CSS_BIT_1   (1 << 2)
99 #endif
100
101 #if SPI_1_ALT_2
102 #define SPI_CFG_1       PERCFG_U1CFG_ALT_2
103 #define SPI_SEL_1       P1SEL
104 #define SPI_PRI_1       P2SEL_PRI3P1_USART1
105 #define SPI_BITS_1      (1 << 6) | (1 << 7) | (1 << 5)
106 #define SPI_CSS_BIT_1   (1 << 4)
107 #endif
108
109 #endif
110
111 #if MULTI_SPI
112
113 #define SPI_BUF(bus)            ((bus) ? SPI_BUF_1 : SPI_BUF_0)
114 #define SPI_CSR(bus)            ((bus) ? SPI_CSR_1 : SPI_CSR_0)
115 #define SPI_BAUD(bus)           ((bus) ? SPI_BAUD_1 : SPI_BAUD_0)
116 #define SPI_GCR(bus)            ((bus) ? SPI_GCR_1 : SPI_GCR_0)
117 #define SPI_CFG_MASK(bus)       ((bus) ? SPI_CFG_MASK_1 : SPI_CFG_MASK_0)
118 #define SPI_DMA_TX(bus)         ((bus) ? SPI_DMA_TX_1 : SPI_DMA_TX_0)
119 #define SPI_DMA_RX(bus)         ((bus) ? SPI_DMA_RX_1 : SPI_DMA_RX_0)
120 #define SPI_CFG(bus)            ((bus) ? SPI_CFG_1 : SPI_CFG_0)
121 #define SPI_SEL(bus)            ((bus) ? SPI_SEL_1 : SPI_SEL_0)
122 #define SPI_BITS(bus)           ((bus) ? SPI_BITS_1 : SPI_BITS_0)
123 #define SPI_CSS_BIT(bus)        ((bus) ? SPI_CSS_BIT_1 : SPI_CSS_BIT_0)
124
125 #else
126
127 #if HAS_SPI_0
128 #define SPI_BUF(bus)            SPI_BUF_0
129 #define SPI_CSR(bus)            SPI_CSR_0
130 #define SPI_BAUD(bus)           SPI_BAUD_0
131 #define SPI_GCR(bus)            SPI_GCR_0
132 #define SPI_CFG_MASK(bus)       SPI_CFG_MASK_0
133 #define SPI_DMA_TX(bus)         SPI_DMA_TX_0
134 #define SPI_DMA_RX(bus)         SPI_DMA_RX_0
135 #define SPI_CFG(bus)            SPI_CFG_0
136 #define SPI_SEL(bus)            SPI_SEL_0
137 #define SPI_BITS(bus)           SPI_BITS_0
138 #define SPI_CSS_BIT(bus)        SPI_CSS_BIT_0
139 #endif
140 #if HAS_SPI_1
141 #define SPI_BUF(bus)            SPI_BUF_1
142 #define SPI_CSR(bus)            SPI_CSR_1
143 #define SPI_BAUD(bus)           SPI_BAUD_1
144 #define SPI_GCR(bus)            SPI_GCR_1
145 #define SPI_CFG_MASK(bus)       SPI_CFG_MASK_1
146 #define SPI_DMA_TX(bus)         SPI_DMA_TX_1
147 #define SPI_DMA_RX(bus)         SPI_DMA_RX_1
148 #define SPI_CFG(bus)            SPI_CFG_1
149 #define SPI_SEL(bus)            SPI_SEL_1
150 #define SPI_BITS(bus)           SPI_BITS_1
151 #define SPI_CSS_BIT(bus)        SPI_CSS_BIT_1
152 #endif
153
154 #endif /* MULTI_SPI */
155
156 #if AO_SPI_SLAVE
157 #define CSS(bus)                SPI_CSS_BIT(bus)
158 #define UxCSR_DIRECTION UxCSR_SLAVE
159 #else
160 #define CSS(bus)                0
161 #define UxCSR_DIRECTION UxCSR_MASTER
162 #endif
163
164 /* Shared mutex to protect SPI bus, must cover the entire
165  * operation, from CS low to CS high. This means that any SPI
166  * user must protect the SPI bus with this mutex
167  */
168 __xdata uint8_t ao_spi_mutex[N_SPI];
169 __xdata uint8_t ao_spi_dma_in_done[N_SPI];
170 __xdata uint8_t ao_spi_dma_out_done[N_SPI];
171
172 uint8_t ao_spi_dma_out_id[N_SPI];
173 uint8_t ao_spi_dma_in_id[N_SPI];
174
175 static __xdata uint8_t ao_spi_const;
176
177
178 /* Send bytes over SPI.
179  *
180  * This sets up two DMA engines, one writing the data and another reading
181  * bytes coming back.  We use the bytes coming back to tell when the transfer
182  * is complete, as the transmit register is double buffered and hence signals
183  * completion one byte before the transfer is actually complete
184  */
185 #if MULTI_SPI
186 void
187 ao_spi_send(void __xdata *block, uint16_t len, uint8_t bus) __reentrant
188 #else
189 void
190 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant
191 #define bus     0
192 #endif
193 {
194         ao_dma_set_transfer(ao_spi_dma_in_id[bus],
195                             SPI_BUF(bus),
196                             &ao_spi_const,
197                             len,
198                             DMA_CFG0_WORDSIZE_8 |
199                             DMA_CFG0_TMODE_SINGLE |
200                             SPI_DMA_RX(bus),
201                             DMA_CFG1_SRCINC_0 |
202                             DMA_CFG1_DESTINC_0 |
203                             DMA_CFG1_PRIORITY_NORMAL);
204         ao_dma_set_transfer(ao_spi_dma_out_id[bus],
205                             block,
206                             SPI_BUF(bus),
207                             len,
208                             DMA_CFG0_WORDSIZE_8 |
209                             DMA_CFG0_TMODE_SINGLE |
210                             SPI_DMA_TX(bus),
211                             DMA_CFG1_SRCINC_1 |
212                             DMA_CFG1_DESTINC_0 |
213                             DMA_CFG1_PRIORITY_NORMAL);
214
215         ao_dma_start(ao_spi_dma_in_id[bus]);
216         ao_dma_start(ao_spi_dma_out_id[bus]);
217         ao_dma_trigger(ao_spi_dma_out_id[bus]);
218 #if !AO_SPI_SLAVE
219         __critical while (!ao_spi_dma_in_done[bus])
220                 ao_sleep(&ao_spi_dma_in_done[bus]);
221 #endif
222 #undef bus
223 }
224
225 #if AO_SPI_SLAVE
226 void
227 ao_spi_send_wait(void)
228 {
229         __critical while (!ao_spi_dma_in_done[0])
230                 ao_sleep(&ao_spi_dma_in_done[0]);
231 }
232 #endif
233
234 /* Receive bytes over SPI.
235  *
236  * This sets up tow DMA engines, one reading the data and another
237  * writing constant values to the SPI transmitter as that is what
238  * clocks the data coming in.
239  */
240 #if MULTI_SPI
241 void
242 ao_spi_recv(void __xdata *block, uint16_t len, uint8_t bus) __reentrant
243 #else
244 void
245 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant
246 #define bus 0
247 #endif
248 {
249         ao_dma_set_transfer(ao_spi_dma_in_id[bus],
250                             SPI_BUF(bus),
251                             block,
252                             len,
253                             DMA_CFG0_WORDSIZE_8 |
254                             DMA_CFG0_TMODE_SINGLE |
255                             SPI_DMA_RX(bus),
256                             DMA_CFG1_SRCINC_0 |
257                             DMA_CFG1_DESTINC_1 |
258                             DMA_CFG1_PRIORITY_NORMAL);
259
260         ao_spi_const = SPI_CONST;
261
262 #if !AO_SPI_SLAVE
263         ao_dma_set_transfer(ao_spi_dma_out_id[bus],
264                             &ao_spi_const,
265                             SPI_BUF(bus),
266                             len,
267                             DMA_CFG0_WORDSIZE_8 |
268                             DMA_CFG0_TMODE_SINGLE |
269                             SPI_DMA_TX(bus),
270                             DMA_CFG1_SRCINC_0 |
271                             DMA_CFG1_DESTINC_0 |
272                             DMA_CFG1_PRIORITY_NORMAL);
273 #endif
274
275         ao_dma_start(ao_spi_dma_in_id[bus]);
276 #if !AO_SPI_SLAVE
277         ao_dma_start(ao_spi_dma_out_id[bus]);
278         ao_dma_trigger(ao_spi_dma_out_id[bus]);
279         __critical while (!ao_spi_dma_in_done[bus])
280                 ao_sleep(&ao_spi_dma_in_done[bus]);
281 #endif
282 }
283
284 #if AO_SPI_SLAVE
285 void
286 ao_spi_recv_wait(void)
287 {
288         __critical while (!ao_spi_dma_in_done[0])
289                 ao_sleep(&ao_spi_dma_in_done[0]);
290 }
291 #endif
292
293 /* Set up the USART.
294  *
295  * SPI master/slave mode
296  */
297 /* Set the baud rate and signal parameters
298  *
299  * The cc1111 is limited to a 24/8 MHz SPI clock.
300  * Every peripheral I've ever seen goes faster than that,
301  * so set the clock to 3MHz (BAUD_E 17, BAUD_M 0)
302  */
303 #define SPI_INIT(bus,o) do {                                            \
304                 /* Set up the USART pin assignment */                   \
305                 PERCFG = (PERCFG & ~SPI_CFG_MASK(bus)) | SPI_CFG(bus);  \
306                                                                         \
307                 /* Make the SPI pins be controlled by the USART peripheral */ \
308                 SPI_SEL(bus) |= SPI_BITS(bus) | CSS(bus);               \
309                 SPI_CSR(bus) = (UxCSR_MODE_SPI | UxCSR_RE | UxCSR_DIRECTION); \
310                 SPI_BAUD(bus) = 0;                                      \
311                 SPI_GCR(bus) = (UxGCR_CPOL_NEGATIVE |                   \
312                                 UxGCR_CPHA_FIRST_EDGE |                 \
313                                 UxGCR_ORDER_MSB |                       \
314                                 (17 << UxGCR_BAUD_E_SHIFT));            \
315                 /* Set up OUT DMA */                                    \
316                 ao_spi_dma_out_id[o] = ao_dma_alloc(&ao_spi_dma_out_done[o]); \
317                                                                         \
318                 /* Set up IN DMA */                                     \
319                 ao_spi_dma_in_id[o] = ao_dma_alloc(&ao_spi_dma_in_done[o]);     \
320         } while (0)
321
322 void
323 ao_spi_init(void)
324 {
325         /* Ensure that SPI USART takes precidence over the other USART
326          * for pins that they share
327          */
328 #ifdef SPI_PRI
329         P2SEL = (P2SEL & ~P2SEL_PRI3P1_MASK) | SPI_PRI;
330 #endif
331
332 #if HAS_SPI_0
333         SPI_INIT(0, 0);
334 #endif
335 #if HAS_SPI_1
336         SPI_INIT(1, MULTI_SPI);
337 #endif
338 }