altos/stm: Add better byte-level SPI api
[fw/altos] / src / stm / ao_arch_funcs.h
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 #ifndef _AO_ARCH_FUNCS_H_
19 #define _AO_ARCH_FUNCS_H_
20
21 /* ao_spi_stm.c
22  */
23
24 /* PCLK is set to 16MHz (HCLK 32MHz, APB prescaler 2) */
25
26 #define AO_SPI_SPEED_8MHz       STM_SPI_CR1_BR_PCLK_2
27 #define AO_SPI_SPEED_4MHz       STM_SPI_CR1_BR_PCLK_4
28 #define AO_SPI_SPEED_2MHz       STM_SPI_CR1_BR_PCLK_8
29 #define AO_SPI_SPEED_1MHz       STM_SPI_CR1_BR_PCLK_16
30 #define AO_SPI_SPEED_500kHz     STM_SPI_CR1_BR_PCLK_32
31 #define AO_SPI_SPEED_250kHz     STM_SPI_CR1_BR_PCLK_64
32 #define AO_SPI_SPEED_125kHz     STM_SPI_CR1_BR_PCLK_128
33 #define AO_SPI_SPEED_62500Hz    STM_SPI_CR1_BR_PCLK_256
34
35 #define AO_SPI_SPEED_FAST       AO_SPI_SPEED_8MHz
36
37 /* Companion bus wants something no faster than 200kHz */
38
39 #define AO_SPI_SPEED_200kHz     AO_SPI_SPEED_125kHz
40
41 #define AO_SPI_CONFIG_1         0x00
42 #define AO_SPI_1_CONFIG_PA5_PA6_PA7     AO_SPI_CONFIG_1
43 #define AO_SPI_2_CONFIG_PB13_PB14_PB15  AO_SPI_CONFIG_1
44
45 #define AO_SPI_CONFIG_2         0x04
46 #define AO_SPI_1_CONFIG_PB3_PB4_PB5     AO_SPI_CONFIG_2
47 #define AO_SPI_2_CONFIG_PD1_PD3_PD4     AO_SPI_CONFIG_2
48
49 #define AO_SPI_CONFIG_3         0x08
50 #define AO_SPI_1_CONFIG_PE13_PE14_PE15  AO_SPI_CONFIG_3
51
52 #define AO_SPI_CONFIG_NONE      0x0c
53
54 #define AO_SPI_INDEX_MASK       0x01
55 #define AO_SPI_CONFIG_MASK      0x0c
56
57 #define AO_SPI_1_PA5_PA6_PA7    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PA5_PA6_PA7)
58 #define AO_SPI_1_PB3_PB4_PB5    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PB3_PB4_PB5)
59 #define AO_SPI_1_PE13_PE14_PE15 (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PE13_PE14_PE15)
60
61 #define AO_SPI_2_PB13_PB14_PB15 (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PB13_PB14_PB15)
62 #define AO_SPI_2_PD1_PD3_PD4    (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PD1_PD3_PD4)
63
64 #define AO_SPI_INDEX(id)        ((id) & AO_SPI_INDEX_MASK)
65 #define AO_SPI_CONFIG(id)       ((id) & AO_SPI_CONFIG_MASK)
66
67 uint8_t
68 ao_spi_try_get(uint8_t spi_index, uint32_t speed, uint8_t task_id);
69
70 void
71 ao_spi_get(uint8_t spi_index, uint32_t speed);
72
73 void
74 ao_spi_put(uint8_t spi_index);
75
76 void
77 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
78
79 void
80 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
81
82 void
83 ao_spi_send_sync(const void *block, uint16_t len, uint8_t spi_index);
84
85 void
86 ao_spi_start_bytes(uint8_t spi_index);
87
88 void
89 ao_spi_stop_bytes(uint8_t spi_index);
90
91 static inline void
92 ao_spi_send_byte(uint8_t byte, uint8_t spi_index)
93 {
94         struct stm_spi  *stm_spi;
95
96         switch (AO_SPI_INDEX(spi_index)) {
97         case 0:
98                 stm_spi = &stm_spi1;
99                 break;
100         case 1:
101                 stm_spi = &stm_spi2;
102                 break;
103         }
104
105         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
106                 ;
107         stm_spi->dr = byte;
108         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
109                 ;
110         (void) stm_spi->dr;
111 }
112
113 static inline uint8_t
114 ao_spi_recv_byte(uint8_t spi_index)
115 {
116         struct stm_spi  *stm_spi;
117
118         switch (AO_SPI_INDEX(spi_index)) {
119         case 0:
120                 stm_spi = &stm_spi1;
121                 break;
122         case 1:
123                 stm_spi = &stm_spi2;
124                 break;
125         }
126
127         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
128                 ;
129         stm_spi->dr = 0xff;
130         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
131                 ;
132         return stm_spi->dr;
133 }
134
135 void
136 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
137
138 void
139 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
140
141 extern uint16_t ao_spi_speed[STM_NUM_SPI];
142
143 void
144 ao_spi_init(void);
145
146 #define ao_spi_set_cs(reg,mask) ((reg)->bsrr = ((uint32_t) (mask)) << 16)
147 #define ao_spi_clr_cs(reg,mask) ((reg)->bsrr = (mask))
148
149 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
150                 ao_spi_get(bus, speed);                         \
151                 ao_spi_set_cs(reg,mask);                        \
152         } while (0)
153
154 static inline uint8_t
155 ao_spi_try_get_mask(struct stm_gpio *reg, uint16_t mask, uint8_t bus, uint32_t speed, uint8_t task_id)
156 {
157         if (!ao_spi_try_get(bus, speed, task_id))
158                 return 0;
159         ao_spi_set_cs(reg, mask);
160         return 1;
161 }
162
163 #define ao_spi_put_mask(reg,mask,bus) do {      \
164                 ao_spi_clr_cs(reg,mask);        \
165                 ao_spi_put(bus);                \
166         } while (0)
167
168 #define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
169 #define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
170
171 #define ao_enable_port(port) do {                                       \
172                 if ((port) == &stm_gpioa)                               \
173                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN); \
174                 else if ((port) == &stm_gpiob)                          \
175                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN); \
176                 else if ((port) == &stm_gpioc)                          \
177                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN); \
178                 else if ((port) == &stm_gpiod)                          \
179                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN); \
180                 else if ((port) == &stm_gpioe)                          \
181                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOEEN); \
182         } while (0)
183
184 #define ao_disable_port(port) do {                                      \
185                 if ((port) == &stm_gpioa)                               \
186                         stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOAEN); \
187                 else if ((port) == &stm_gpiob)                          \
188                         stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOBEN); \
189                 else if ((port) == &stm_gpioc)                          \
190                         stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOCEN); \
191                 else if ((port) == &stm_gpiod)                          \
192                         stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIODEN); \
193                 else if ((port) == &stm_gpioe)                          \
194                         stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOEEN); \
195         } while (0)
196
197
198 #define ao_gpio_set(port, bit, pin, v) stm_gpio_set(port, bit, v)
199
200 #define ao_gpio_get(port, bit, pin) stm_gpio_get(port, bit)
201
202 #define ao_gpio_set_bits(port, bits) stm_gpio_set_bits(port, bits)
203
204 #define ao_gpio_clr_bits(port, bits) stm_gpio_clr_bits(port, bits);
205
206
207 #define ao_enable_output(port,bit,pin,v) do {                   \
208                 ao_enable_port(port);                           \
209                 ao_gpio_set(port, bit, pin, v);                 \
210                 stm_moder_set(port, bit, STM_MODER_OUTPUT);\
211         } while (0)
212
213 #define ao_gpio_set_mode(port,bit,mode) do {                            \
214                 if (mode == AO_EXTI_MODE_PULL_UP)                       \
215                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP);    \
216                 else if (mode == AO_EXTI_MODE_PULL_DOWN)                \
217                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN);  \
218                 else                                                    \
219                         stm_pupdr_set(port, bit, STM_PUPDR_NONE);       \
220         } while (0)
221         
222 #define ao_enable_input(port,bit,mode) do {                             \
223                 ao_enable_port(port);                                   \
224                 stm_moder_set(port, bit, STM_MODER_INPUT);              \
225                 ao_gpio_set_mode(port, bit, mode);                      \
226         } while (0)
227
228 #define ao_enable_cs(port,bit) do {                             \
229                 stm_gpio_set((port), bit, 1);                   \
230                 stm_moder_set((port), bit, STM_MODER_OUTPUT);   \
231         } while (0)
232
233 #define ao_spi_init_cs(port, mask) do {                         \
234                 ao_enable_port(port);                           \
235                 if ((mask) & 0x0001) ao_enable_cs(port, 0);     \
236                 if ((mask) & 0x0002) ao_enable_cs(port, 1);     \
237                 if ((mask) & 0x0004) ao_enable_cs(port, 2);     \
238                 if ((mask) & 0x0008) ao_enable_cs(port, 3);     \
239                 if ((mask) & 0x0010) ao_enable_cs(port, 4);     \
240                 if ((mask) & 0x0020) ao_enable_cs(port, 5);     \
241                 if ((mask) & 0x0040) ao_enable_cs(port, 6);     \
242                 if ((mask) & 0x0080) ao_enable_cs(port, 7);     \
243                 if ((mask) & 0x0100) ao_enable_cs(port, 8);     \
244                 if ((mask) & 0x0200) ao_enable_cs(port, 9);     \
245                 if ((mask) & 0x0400) ao_enable_cs(port, 10);\
246                 if ((mask) & 0x0800) ao_enable_cs(port, 11);\
247                 if ((mask) & 0x1000) ao_enable_cs(port, 12);\
248                 if ((mask) & 0x2000) ao_enable_cs(port, 13);\
249                 if ((mask) & 0x4000) ao_enable_cs(port, 14);\
250                 if ((mask) & 0x8000) ao_enable_cs(port, 15);\
251         } while (0)
252
253 /* ao_dma_stm.c
254  */
255
256 extern uint8_t ao_dma_done[STM_NUM_DMA];
257
258 void
259 ao_dma_set_transfer(uint8_t             index,
260                     volatile void       *peripheral,
261                     void                *memory,
262                     uint16_t            count,
263                     uint32_t            ccr);
264
265 void
266 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
267
268 void
269 ao_dma_start(uint8_t index);
270
271 void
272 ao_dma_done_transfer(uint8_t index);
273
274 void
275 ao_dma_alloc(uint8_t index);
276
277 void
278 ao_dma_init(void);
279
280 /* ao_i2c_stm.c */
281
282 void
283 ao_i2c_get(uint8_t i2c_index);
284
285 uint8_t
286 ao_i2c_start(uint8_t i2c_index, uint16_t address);
287
288 void
289 ao_i2c_put(uint8_t i2c_index);
290
291 uint8_t
292 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
293
294 uint8_t
295 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
296
297 void
298 ao_i2c_init(void);
299
300 #if USE_SERIAL_1_SW_FLOW || USE_SERIAL_2_SW_FLOW || USE_SERIAL_3_SW_FLOW
301 #define HAS_SERIAL_SW_FLOW 1
302 #else
303 #define HAS_SERIAL_SW_FLOW 0
304 #endif
305
306 #if USE_SERIAL_1_FLOW && !USE_SERIAL_1_SW_FLOW || USE_SERIAL_2_FLOW && !USE_SERIAL_2_SW_FLOW || USE_SERIAL_3_FLOW && !USE_SERIAL_3_SW_FLOW
307 #define HAS_SERIAL_HW_FLOW 1
308 #else
309 #define HAS_SERIAL_HW_FLOW 0
310 #endif
311
312 /* ao_serial_stm.c */
313 struct ao_stm_usart {
314         struct ao_fifo          rx_fifo;
315         struct ao_fifo          tx_fifo;
316         struct stm_usart        *reg;
317         uint8_t                 tx_running;
318         uint8_t                 draining;
319 #if HAS_SERIAL_SW_FLOW
320         /* RTS - 0 if we have FIFO space, 1 if not
321          * CTS - 0 if we can send, 0 if not
322          */
323         struct stm_gpio         *gpio_rts;
324         struct stm_gpio         *gpio_cts;
325         uint8_t                 pin_rts;
326         uint8_t                 pin_cts;
327         uint8_t                 rts;
328 #endif
329 };
330
331 #if HAS_SERIAL_1
332 extern struct ao_stm_usart      ao_stm_usart1;
333 #endif
334
335 #if HAS_SERIAL_2
336 extern struct ao_stm_usart      ao_stm_usart2;
337 #endif
338
339 #if HAS_SERIAL_3
340 extern struct ao_stm_usart      ao_stm_usart3;
341 #endif
342
343 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
344
345 typedef uint32_t        ao_arch_irq_t;
346
347 static inline uint32_t
348 ao_arch_irqsave(void) {
349         uint32_t        primask;
350         asm("mrs %0,primask" : "=&r" (primask));
351         ao_arch_block_interrupts();
352         return primask;
353 }
354
355 static inline void
356 ao_arch_irqrestore(uint32_t primask) {
357         asm("msr primask,%0" : : "r" (primask));
358 }
359
360 static inline void
361 ao_arch_memory_barrier() {
362         asm volatile("" ::: "memory");
363 }
364
365 static inline void
366 ao_arch_irq_check(void) {
367         uint32_t        primask;
368         asm("mrs %0,primask" : "=&r" (primask));
369         if ((primask & 1) == 0)
370                 ao_panic(AO_PANIC_IRQ);
371 }
372
373 #if HAS_TASK
374 static inline void
375 ao_arch_init_stack(struct ao_task *task, void *start)
376 {
377         uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
378         uint32_t        a = (uint32_t) start;
379         int             i;
380
381         /* Return address (goes into LR) */
382         ARM_PUSH32(sp, a);
383
384         /* Clear register values r0-r12 */
385         i = 13;
386         while (i--)
387                 ARM_PUSH32(sp, 0);
388
389         /* APSR */
390         ARM_PUSH32(sp, 0);
391
392         /* PRIMASK with interrupts enabled */
393         ARM_PUSH32(sp, 0);
394
395         task->sp = sp;
396 }
397
398 static inline void ao_arch_save_regs(void) {
399         /* Save general registers */
400         asm("push {r0-r12,lr}\n");
401
402         /* Save APSR */
403         asm("mrs r0,apsr");
404         asm("push {r0}");
405
406         /* Save PRIMASK */
407         asm("mrs r0,primask");
408         asm("push {r0}");
409 }
410
411 static inline void ao_arch_save_stack(void) {
412         uint32_t        *sp;
413         asm("mov %0,sp" : "=&r" (sp) );
414         ao_cur_task->sp = (sp);
415         if ((uint8_t *) sp < &ao_cur_task->stack[0])
416                 ao_panic (AO_PANIC_STACK);
417 }
418
419 static inline void ao_arch_restore_stack(void) {
420         uint32_t        sp;
421         sp = (uint32_t) ao_cur_task->sp;
422
423         /* Switch stacks */
424         asm("mov sp, %0" : : "r" (sp) );
425
426         /* Restore PRIMASK */
427         asm("pop {r0}");
428         asm("msr primask,r0");
429
430         /* Restore APSR */
431         asm("pop {r0}");
432         asm("msr apsr_nczvq,r0");
433
434         /* Restore general registers */
435         asm("pop {r0-r12,lr}\n");
436
437         /* Return to calling function */
438         asm("bx lr");
439 }
440
441 #ifndef HAS_SAMPLE_PROFILE
442 #define HAS_SAMPLE_PROFILE 0
443 #endif
444
445 #if DEBUG
446 #define HAS_ARCH_VALIDATE_CUR_STACK     1
447
448 static inline void
449 ao_validate_cur_stack(void)
450 {
451         uint8_t         *psp;
452
453         asm("mrs %0,psp" : "=&r" (psp));
454         if (ao_cur_task &&
455             psp <= ao_cur_task->stack &&
456             psp >= ao_cur_task->stack - 256)
457                 ao_panic(AO_PANIC_STACK);
458 }
459 #endif
460
461 #if !HAS_SAMPLE_PROFILE
462 #define HAS_ARCH_START_SCHEDULER        1
463
464 static inline void ao_arch_start_scheduler(void) {
465         uint32_t        sp;
466         uint32_t        control;
467
468         asm("mrs %0,msp" : "=&r" (sp));
469         asm("msr psp,%0" : : "r" (sp));
470         asm("mrs %0,control" : "=&r" (control));
471         control |= (1 << 1);
472         asm("msr control,%0" : : "r" (control));
473         asm("isb");
474 }
475 #endif
476
477 #define ao_arch_isr_stack()
478
479 #endif
480
481 #define ao_arch_wait_interrupt() do {                           \
482                 asm("\twfi\n");                                 \
483                 ao_arch_release_interrupts();                   \
484                 asm(".global ao_idle_loc\nao_idle_loc:");       \
485                 ao_arch_block_interrupts();                     \
486         } while (0)
487
488 #define ao_arch_critical(b) do {                        \
489                 uint32_t __mask = ao_arch_irqsave();    \
490                 do { b } while (0);                     \
491                 ao_arch_irqrestore(__mask);             \
492         } while (0)
493
494 #endif /* _AO_ARCH_FUNCS_H_ */