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