altos/stm: Clean up spi_enable/disable_index functions
[fw/altos] / src / stm / ao_spi_stm.c
1 /*
2  * Copyright © 2012 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 struct ao_spi_stm_info {
21         uint8_t miso_dma_index;
22         uint8_t mosi_dma_index;
23         struct stm_spi *stm_spi;
24 };
25
26 static uint8_t          ao_spi_mutex[STM_NUM_SPI];
27 static uint8_t          ao_spi_index[STM_NUM_SPI];
28
29 static const struct ao_spi_stm_info ao_spi_stm_info[STM_NUM_SPI] = {
30         {
31                 .miso_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_SPI1_RX),
32                 .mosi_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_SPI1_TX),
33                 &stm_spi1
34         },
35         {
36                 .miso_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_SPI2_RX),
37                 .mosi_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_SPI2_TX),
38                 &stm_spi2
39         }
40 };
41
42 static uint8_t  spi_dev_null;
43
44 #if DEBUG
45 static struct {
46         uint8_t task;
47         uint8_t which;
48         AO_TICK_TYPE tick;
49         uint16_t len;
50 } spi_tasks[64];
51 static uint8_t  spi_task_index;
52
53 static void
54 validate_spi(struct stm_spi *stm_spi, int which, uint16_t len)
55 {
56         uint32_t        sr = stm_spi->sr;
57
58         if (stm_spi != &stm_spi2)
59                 return;
60         spi_tasks[spi_task_index].task = ao_cur_task ? ao_cur_task->task_id : 0;
61         spi_tasks[spi_task_index].which = which;
62         spi_tasks[spi_task_index].tick = ao_time();
63         spi_tasks[spi_task_index].len = len;
64         spi_task_index = (spi_task_index + 1) & (63);
65         if (sr & (1 << STM_SPI_SR_FRE))
66                 ao_panic(0x40 | 1);
67         if (sr & (1 << STM_SPI_SR_BSY))
68                 ao_panic(0x40 | 2);
69         if (sr & (1 << STM_SPI_SR_OVR))
70                 ao_panic(0x40 | 3);
71         if (sr & (1 << STM_SPI_SR_MODF))
72                 ao_panic(0x40 | 4);
73         if (sr & (1 << STM_SPI_SR_UDR))
74                 ao_panic(0x40 | 5);
75         if ((sr & (1 << STM_SPI_SR_TXE)) == 0)
76                 ao_panic(0x40 | 6);
77         if (sr & (1 << STM_SPI_SR_RXNE))
78                 ao_panic(0x40 | 7);
79         if (which != 5 && which != 6 && which != 13)
80                 if (ao_cur_task->task_id != ao_spi_mutex[1])
81                         ao_panic(0x40 | 8);
82 }
83 #else
84 #define validate_spi(stm_spi, which, len) do { (void) (which); (void) (len); } while (0)
85 #endif
86
87 static void
88 ao_spi_run(uint8_t id, uint8_t which, uint16_t len)
89 {
90         struct stm_spi *stm_spi = ao_spi_stm_info[id].stm_spi;
91         uint8_t mosi_dma_index = ao_spi_stm_info[id].mosi_dma_index;
92         uint8_t miso_dma_index = ao_spi_stm_info[id].miso_dma_index;
93
94         validate_spi(stm_spi, which, len);
95
96         stm_spi->cr2 = ((0 << STM_SPI_CR2_TXEIE) |
97                         (0 << STM_SPI_CR2_RXNEIE) |
98                         (0 << STM_SPI_CR2_ERRIE) |
99                         (0 << STM_SPI_CR2_SSOE) |
100                         (1 << STM_SPI_CR2_TXDMAEN) |
101                         (1 << STM_SPI_CR2_RXDMAEN));
102
103         ao_dma_start(miso_dma_index);
104         ao_dma_start(mosi_dma_index);
105
106         ao_arch_critical(
107                 while (!ao_dma_done[miso_dma_index])
108                         ao_sleep(&ao_dma_done[miso_dma_index]);
109                 );
110
111         while ((stm_spi->sr & (1 << STM_SPI_SR_TXE)) == 0);
112         while (stm_spi->sr & (1 << STM_SPI_SR_BSY));
113
114         validate_spi(stm_spi, which+1, len);
115
116         stm_spi->cr2 = 0;
117
118         ao_dma_done_transfer(mosi_dma_index);
119         ao_dma_done_transfer(miso_dma_index);
120 }
121
122 void
123 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index)
124 {
125         uint8_t id = AO_SPI_INDEX(spi_index);
126         struct stm_spi *stm_spi = ao_spi_stm_info[id].stm_spi;
127         uint8_t mosi_dma_index = ao_spi_stm_info[id].mosi_dma_index;
128         uint8_t miso_dma_index = ao_spi_stm_info[id].miso_dma_index;
129
130         /* Set up the transmit DMA to deliver data */
131         ao_dma_set_transfer(mosi_dma_index,
132                             &stm_spi->dr,
133                             (void *) block,
134                             len,
135                             (0 << STM_DMA_CCR_MEM2MEM) |
136                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
137                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
138                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
139                             (1 << STM_DMA_CCR_MINC) |
140                             (0 << STM_DMA_CCR_PINC) |
141                             (0 << STM_DMA_CCR_CIRC) |
142                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
143
144         /* Set up the receive DMA -- when this is done, we know the SPI unit
145          * is idle. Without this, we'd have to poll waiting for the BSY bit to
146          * be cleared
147          */
148         ao_dma_set_transfer(miso_dma_index,
149                             &stm_spi->dr,
150                             &spi_dev_null,
151                             len,
152                             (0 << STM_DMA_CCR_MEM2MEM) |
153                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
154                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
155                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
156                             (0 << STM_DMA_CCR_MINC) |
157                             (0 << STM_DMA_CCR_PINC) |
158                             (0 << STM_DMA_CCR_CIRC) |
159                             (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
160
161         ao_spi_run(id, 1, len);
162 }
163
164 void
165 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index)
166 {
167         uint8_t id = AO_SPI_INDEX(spi_index);
168         struct stm_spi *stm_spi = ao_spi_stm_info[id].stm_spi;
169         uint8_t mosi_dma_index = ao_spi_stm_info[id].mosi_dma_index;
170         uint8_t miso_dma_index = ao_spi_stm_info[id].miso_dma_index;
171
172         /* Set up the transmit DMA to deliver data */
173         ao_dma_set_transfer(mosi_dma_index,
174                             &stm_spi->dr,
175                             &value,
176                             len,
177                             (0 << STM_DMA_CCR_MEM2MEM) |
178                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
179                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
180                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
181                             (0 << STM_DMA_CCR_MINC) |
182                             (0 << STM_DMA_CCR_PINC) |
183                             (0 << STM_DMA_CCR_CIRC) |
184                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
185
186         /* Set up the receive DMA -- when this is done, we know the SPI unit
187          * is idle. Without this, we'd have to poll waiting for the BSY bit to
188          * be cleared
189          */
190         ao_dma_set_transfer(miso_dma_index,
191                             &stm_spi->dr,
192                             &spi_dev_null,
193                             len,
194                             (0 << STM_DMA_CCR_MEM2MEM) |
195                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
196                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
197                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
198                             (0 << STM_DMA_CCR_MINC) |
199                             (0 << STM_DMA_CCR_PINC) |
200                             (0 << STM_DMA_CCR_CIRC) |
201                             (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
202
203         ao_spi_run(id, 3, len);
204 }
205
206 void
207 ao_spi_send_sync(const void *block, uint16_t len, uint8_t spi_index)
208 {
209         uint8_t         id = AO_SPI_INDEX(spi_index);
210         const uint8_t   *b = block;
211         struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
212
213         stm_spi->cr2 = ((0 << STM_SPI_CR2_TXEIE) |
214                         (0 << STM_SPI_CR2_RXNEIE) |
215                         (0 << STM_SPI_CR2_ERRIE) |
216                         (0 << STM_SPI_CR2_SSOE) |
217                         (0 << STM_SPI_CR2_TXDMAEN) |
218                         (0 << STM_SPI_CR2_RXDMAEN));
219         validate_spi(stm_spi, 7, len);
220         while (len--) {
221                 while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)));
222                 stm_spi->dr = *b++;
223         }
224         while ((stm_spi->sr & (1 << STM_SPI_SR_TXE)) == 0)
225                 ;
226         while (stm_spi->sr & (1 << STM_SPI_SR_BSY))
227                 ;
228         /* Clear the OVR flag */
229         (void) stm_spi->dr;
230         (void) stm_spi->sr;
231         validate_spi(stm_spi, 8, len);
232 }
233
234 void
235 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index)
236 {
237         uint8_t         id = AO_SPI_INDEX(spi_index);
238         struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
239         uint8_t         mosi_dma_index = ao_spi_stm_info[id].mosi_dma_index;
240         uint8_t         miso_dma_index = ao_spi_stm_info[id].miso_dma_index;
241
242         spi_dev_null = 0xff;
243
244         /* Set up transmit DMA to make the SPI hardware actually run */
245         ao_dma_set_transfer(mosi_dma_index,
246                             &stm_spi->dr,
247                             &spi_dev_null,
248                             len,
249                             (0 << STM_DMA_CCR_MEM2MEM) |
250                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
251                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
252                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
253                             (0 << STM_DMA_CCR_MINC) |
254                             (0 << STM_DMA_CCR_PINC) |
255                             (0 << STM_DMA_CCR_CIRC) |
256                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
257
258         /* Set up the receive DMA to capture data */
259         ao_dma_set_transfer(miso_dma_index,
260                             &stm_spi->dr,
261                             block,
262                             len,
263                             (0 << STM_DMA_CCR_MEM2MEM) |
264                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
265                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
266                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
267                             (1 << STM_DMA_CCR_MINC) |
268                             (0 << STM_DMA_CCR_PINC) |
269                             (0 << STM_DMA_CCR_CIRC) |
270                             (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
271
272         ao_spi_run(id, 9, len);
273 }
274
275 void
276 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index)
277 {
278         uint8_t         id = AO_SPI_INDEX(spi_index);
279         struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
280         uint8_t         mosi_dma_index = ao_spi_stm_info[id].mosi_dma_index;
281         uint8_t         miso_dma_index = ao_spi_stm_info[id].miso_dma_index;
282
283         /* Set up transmit DMA to send data */
284         ao_dma_set_transfer(mosi_dma_index,
285                             &stm_spi->dr,
286                             out,
287                             len,
288                             (0 << STM_DMA_CCR_MEM2MEM) |
289                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
290                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
291                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
292                             (1 << STM_DMA_CCR_MINC) |
293                             (0 << STM_DMA_CCR_PINC) |
294                             (0 << STM_DMA_CCR_CIRC) |
295                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
296
297         /* Set up the receive DMA to capture data */
298         ao_dma_set_transfer(miso_dma_index,
299                             &stm_spi->dr,
300                             in,
301                             len,
302                             (0 << STM_DMA_CCR_MEM2MEM) |
303                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
304                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
305                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
306                             (1 << STM_DMA_CCR_MINC) |
307                             (0 << STM_DMA_CCR_PINC) |
308                             (0 << STM_DMA_CCR_CIRC) |
309                             (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
310         ao_spi_run(id, 11, len);
311 }
312
313 static void
314 ao_spi_disable_index(uint8_t spi_index)
315 {
316         /* Disable current config
317          */
318         switch (spi_index) {
319         case AO_SPI_1_PA5_PA6_PA7:
320                 stm_gpio_set(&stm_gpioa, 5, 1);
321                 stm_moder_set(&stm_gpioa, 5, STM_MODER_OUTPUT);
322                 stm_moder_set(&stm_gpioa, 6, STM_MODER_INPUT);
323                 stm_moder_set(&stm_gpioa, 7, STM_MODER_OUTPUT);
324                 break;
325         case AO_SPI_1_PB3_PB4_PB5:
326                 stm_gpio_set(&stm_gpiob, 3, 1);
327                 stm_moder_set(&stm_gpiob, 3, STM_MODER_OUTPUT);
328                 stm_moder_set(&stm_gpiob, 4, STM_MODER_INPUT);
329                 stm_moder_set(&stm_gpiob, 5, STM_MODER_OUTPUT);
330                 break;
331         case AO_SPI_1_PE13_PE14_PE15:
332                 stm_gpio_set(&stm_gpioe, 13, 1);
333                 stm_moder_set(&stm_gpioe, 13, STM_MODER_OUTPUT);
334                 stm_moder_set(&stm_gpioe, 14, STM_MODER_INPUT);
335                 stm_moder_set(&stm_gpioe, 15, STM_MODER_OUTPUT);
336                 break;
337         case AO_SPI_2_PB13_PB14_PB15:
338                 stm_gpio_set(&stm_gpiob, 13, 1);
339                 stm_moder_set(&stm_gpiob, 13, STM_MODER_OUTPUT);
340                 stm_moder_set(&stm_gpiob, 14, STM_MODER_INPUT);
341                 stm_moder_set(&stm_gpiob, 15, STM_MODER_OUTPUT);
342                 break;
343         case AO_SPI_2_PD1_PD3_PD4:
344                 stm_gpio_set(&stm_gpiod, 1, 1);
345                 stm_moder_set(&stm_gpiod, 1, STM_MODER_OUTPUT);
346                 stm_moder_set(&stm_gpiod, 3, STM_MODER_INPUT);
347                 stm_moder_set(&stm_gpiod, 4, STM_MODER_OUTPUT);
348                 break;
349         }
350 }
351
352 static void
353 ao_spi_enable_index(uint8_t spi_index)
354 {
355         /* Enable new config
356          */
357         switch (spi_index) {
358         case AO_SPI_1_PA5_PA6_PA7:
359                 stm_afr_set(&stm_gpioa, 5, STM_AFR_AF5);
360                 stm_afr_set(&stm_gpioa, 6, STM_AFR_AF5);
361                 stm_afr_set(&stm_gpioa, 7, STM_AFR_AF5);
362                 break;
363         case AO_SPI_1_PB3_PB4_PB5:
364                 stm_afr_set(&stm_gpiob, 3, STM_AFR_AF5);
365                 stm_afr_set(&stm_gpiob, 4, STM_AFR_AF5);
366                 stm_afr_set(&stm_gpiob, 5, STM_AFR_AF5);
367                 break;
368         case AO_SPI_1_PE13_PE14_PE15:
369                 stm_afr_set(&stm_gpioe, 13, STM_AFR_AF5);
370                 stm_afr_set(&stm_gpioe, 14, STM_AFR_AF5);
371                 stm_afr_set(&stm_gpioe, 15, STM_AFR_AF5);
372                 break;
373         case AO_SPI_2_PB13_PB14_PB15:
374                 stm_afr_set(&stm_gpiob, 13, STM_AFR_AF5);
375                 stm_afr_set(&stm_gpiob, 14, STM_AFR_AF5);
376                 stm_afr_set(&stm_gpiob, 15, STM_AFR_AF5);
377                 break;
378         case AO_SPI_2_PD1_PD3_PD4:
379                 stm_afr_set(&stm_gpiod, 1, STM_AFR_AF5);
380                 stm_afr_set(&stm_gpiod, 3, STM_AFR_AF5);
381                 stm_afr_set(&stm_gpiod, 4, STM_AFR_AF5);
382                 break;
383         }
384 }
385
386 static void
387 ao_spi_config(uint8_t spi_index, uint32_t speed)
388 {
389         uint8_t         id = AO_SPI_INDEX(spi_index);
390         struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
391
392         if (spi_index != ao_spi_index[id]) {
393
394                 /* Disable old config
395                  */
396                 ao_spi_disable_index(ao_spi_index[id]);
397
398                 /* Enable new config
399                  */
400                 ao_spi_enable_index(spi_index);
401
402                 /* Remember current config
403                  */
404                 ao_spi_index[id] = spi_index;
405         }
406         stm_spi->cr1 = ((0 << STM_SPI_CR1_BIDIMODE) |           /* Three wire mode */
407                         (0 << STM_SPI_CR1_BIDIOE) |
408                         (0 << STM_SPI_CR1_CRCEN) |              /* CRC disabled */
409                         (0 << STM_SPI_CR1_CRCNEXT) |
410                         (0 << STM_SPI_CR1_DFF) |
411                         (0 << STM_SPI_CR1_RXONLY) |
412                         (1 << STM_SPI_CR1_SSM) |                /* Software SS handling */
413                         (1 << STM_SPI_CR1_SSI) |                /*  ... */
414                         (0 << STM_SPI_CR1_LSBFIRST) |           /* Big endian */
415                         (1 << STM_SPI_CR1_SPE) |                /* Enable SPI unit */
416                         (speed << STM_SPI_CR1_BR) |             /* baud rate to pclk/4 */
417                         (1 << STM_SPI_CR1_MSTR) |
418                         (0 << STM_SPI_CR1_CPOL) |               /* Format 0 */
419                         (0 << STM_SPI_CR1_CPHA));
420         validate_spi(stm_spi, 13, 0);
421 }
422
423 uint8_t
424 ao_spi_try_get(uint8_t spi_index, uint32_t speed, uint8_t task_id)
425 {
426         uint8_t         id = AO_SPI_INDEX(spi_index);
427
428         if (!ao_mutex_try(&ao_spi_mutex[id], task_id))
429                 return 0;
430         ao_spi_config(spi_index, speed);
431         return 1;
432 }
433
434 void
435 ao_spi_get(uint8_t spi_index, uint32_t speed)
436 {
437         uint8_t         id = AO_SPI_INDEX(spi_index);
438
439         ao_mutex_get(&ao_spi_mutex[id]);
440         ao_spi_config(spi_index, speed);
441 }
442
443 void
444 ao_spi_put(uint8_t spi_index)
445 {
446         uint8_t         id = AO_SPI_INDEX(spi_index);
447         struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
448
449         stm_spi->cr1 = 0;
450         ao_mutex_put(&ao_spi_mutex[id]);
451 }
452
453 static void
454 ao_spi_channel_init(uint8_t spi_index)
455 {
456         uint8_t         id = AO_SPI_INDEX(spi_index);
457         struct stm_spi  *stm_spi = ao_spi_stm_info[id].stm_spi;
458
459         ao_spi_disable_index(spi_index);
460
461         stm_spi->cr1 = 0;
462         stm_spi->cr2 = ((0 << STM_SPI_CR2_TXEIE) |
463                         (0 << STM_SPI_CR2_RXNEIE) |
464                         (0 << STM_SPI_CR2_ERRIE) |
465                         (0 << STM_SPI_CR2_SSOE) |
466                         (0 << STM_SPI_CR2_TXDMAEN) |
467                         (0 << STM_SPI_CR2_RXDMAEN));
468
469         /* Clear any pending data and error flags */
470         (void) stm_spi->dr;
471         (void) stm_spi->sr;
472 }
473
474 #if DEBUG
475 void
476 ao_spi_dump_cmd(void)
477 {
478         int s;
479
480         for (s = 0; s < 64; s++) {
481                 int i = (spi_task_index + s) & 63;
482                 if (spi_tasks[i].which) {
483                         int t;
484                         const char *name = "(none)";
485                         for (t = 0; t < ao_num_tasks; t++)
486                                 if (ao_tasks[t]->task_id == spi_tasks[i].task) {
487                                         name = ao_tasks[t]->name;
488                                         break;
489                                 }
490                         printf("%2d: %5d task %2d which %2d len %5d %s\n",
491                                s,
492                                spi_tasks[i].tick,
493                                spi_tasks[i].task,
494                                spi_tasks[i].which,
495                                spi_tasks[i].len,
496                                name);
497                 }
498         }
499         for (s = 0; s < STM_NUM_SPI; s++) {
500                 struct stm_spi *spi = ao_spi_stm_info[s].stm_spi;
501
502                 printf("%1d: mutex %2d index %3d miso dma %3d mosi dma %3d",
503                        s, ao_spi_mutex[s], ao_spi_index[s],
504                        ao_spi_stm_info[s].miso_dma_index,
505                        ao_spi_stm_info[s].mosi_dma_index);
506                 printf(" cr1 %04x cr2 %02x sr %03x\n",
507                        spi->cr1, spi->cr2, spi->sr);
508         }
509
510 }
511
512 static const struct ao_cmds ao_spi_cmds[] = {
513         { ao_spi_dump_cmd,      "S\0Dump SPI status" },
514         { 0, NULL }
515 };
516 #endif
517
518 void
519 ao_spi_init(void)
520 {
521 #if HAS_SPI_1
522 # if SPI_1_PA5_PA6_PA7
523         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
524         stm_ospeedr_set(&stm_gpioa, 5, SPI_1_OSPEEDR);
525         stm_ospeedr_set(&stm_gpioa, 6, SPI_1_OSPEEDR);
526         stm_ospeedr_set(&stm_gpioa, 7, SPI_1_OSPEEDR);
527 # endif
528 # if SPI_1_PB3_PB4_PB5
529         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
530         stm_ospeedr_set(&stm_gpiob, 3, SPI_1_OSPEEDR);
531         stm_ospeedr_set(&stm_gpiob, 4, SPI_1_OSPEEDR);
532         stm_ospeedr_set(&stm_gpiob, 5, SPI_1_OSPEEDR);
533 # endif
534 # if SPI_1_PE13_PE14_PE15
535         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOEEN);
536         stm_ospeedr_set(&stm_gpioe, 13, SPI_1_OSPEEDR);
537         stm_ospeedr_set(&stm_gpioe, 14, SPI_1_OSPEEDR);
538         stm_ospeedr_set(&stm_gpioe, 15, SPI_1_OSPEEDR);
539 # endif
540         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SPI1EN);
541         ao_spi_index[0] = AO_SPI_CONFIG_NONE;
542         ao_spi_channel_init(0);
543 #endif
544
545 #if HAS_SPI_2
546 # if SPI_2_PB13_PB14_PB15
547         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
548         stm_ospeedr_set(&stm_gpiob, 13, SPI_2_OSPEEDR);
549         stm_ospeedr_set(&stm_gpiob, 14, SPI_2_OSPEEDR);
550         stm_ospeedr_set(&stm_gpiob, 15, SPI_2_OSPEEDR);
551 # endif
552 # if SPI_2_PD1_PD3_PD4
553         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN);
554         stm_ospeedr_set(&stm_gpiod, 1, SPI_2_OSPEEDR);
555         stm_ospeedr_set(&stm_gpiod, 3, SPI_2_OSPEEDR);
556         stm_ospeedr_set(&stm_gpiod, 4, SPI_2_OSPEEDR);
557 # endif
558         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_SPI2EN);
559         ao_spi_index[1] = AO_SPI_CONFIG_NONE;
560         ao_spi_channel_init(1);
561 #endif
562 #if DEBUG
563         ao_cmd_register(&ao_spi_cmds[0]);
564 #endif
565 }