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