1a9f28e30ef0b68d4f7ac69fe846e16dea915bb5
[fw/altos] / src / stmf0 / 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; 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 #ifndef _AO_ARCH_FUNCS_H_
20 #define _AO_ARCH_FUNCS_H_
21
22 #include <ao_power.h>
23
24 /* ao_spi_stm.c
25  */
26
27 /* PCLK is set to 48MHz (HCLK 48MHz, HPRE 1, PPRE 1) */
28
29 #define _AO_SPI_SPEED_24MHz     STM_SPI_CR1_BR_PCLK_2
30 #define _AO_SPI_SPEED_12MHz     STM_SPI_CR1_BR_PCLK_4
31 #define _AO_SPI_SPEED_6MHz      STM_SPI_CR1_BR_PCLK_8
32 #define _AO_SPI_SPEED_3MHz      STM_SPI_CR1_BR_PCLK_16
33 #define _AO_SPI_SPEED_1500kHz   STM_SPI_CR1_BR_PCLK_32
34 #define _AO_SPI_SPEED_750kHz    STM_SPI_CR1_BR_PCLK_64
35 #define _AO_SPI_SPEED_375kHz    STM_SPI_CR1_BR_PCLK_128
36 #define _AO_SPI_SPEED_187500Hz  STM_SPI_CR1_BR_PCLK_256
37
38 static inline uint32_t
39 ao_spi_speed(uint32_t hz)
40 {
41         if (hz >=24000000) return _AO_SPI_SPEED_24MHz;
42         if (hz >=12000000) return _AO_SPI_SPEED_12MHz;
43         if (hz >= 6000000) return _AO_SPI_SPEED_6MHz;
44         if (hz >= 3000000) return _AO_SPI_SPEED_3MHz;
45         if (hz >= 1500000) return _AO_SPI_SPEED_1500kHz;
46         if (hz >=  750000) return _AO_SPI_SPEED_750kHz;
47         if (hz >=  375000) return _AO_SPI_SPEED_375kHz;
48         return _AO_SPI_SPEED_187500Hz;
49 }
50
51 #define AO_SPI_CONFIG_1         0x00
52 #define AO_SPI_1_CONFIG_PA5_PA6_PA7     AO_SPI_CONFIG_1
53 #define AO_SPI_2_CONFIG_PB13_PB14_PB15  AO_SPI_CONFIG_1
54
55 #define AO_SPI_CONFIG_2         0x04
56 #define AO_SPI_1_CONFIG_PB3_PB4_PB5     AO_SPI_CONFIG_2
57 #define AO_SPI_2_CONFIG_PD1_PD3_PD4     AO_SPI_CONFIG_2
58
59 #define AO_SPI_CONFIG_3         0x08
60 #define AO_SPI_1_CONFIG_PE13_PE14_PE15  AO_SPI_CONFIG_3
61
62 #define AO_SPI_CONFIG_NONE      0x0c
63
64 #define AO_SPI_INDEX_MASK       0x01
65 #define AO_SPI_CONFIG_MASK      0x0c
66
67 #define AO_SPI_1_PA5_PA6_PA7    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PA5_PA6_PA7)
68 #define AO_SPI_1_PB3_PB4_PB5    (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PB3_PB4_PB5)
69 #define AO_SPI_1_PE13_PE14_PE15 (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PE13_PE14_PE15)
70
71 #define AO_SPI_2_PB13_PB14_PB15 (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PB13_PB14_PB15)
72 #define AO_SPI_2_PD1_PD3_PD4    (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PD1_PD3_PD4)
73
74 #define AO_SPI_INDEX(id)        ((id) & AO_SPI_INDEX_MASK)
75 #define AO_SPI_CONFIG(id)       ((id) & AO_SPI_CONFIG_MASK)
76 #define AO_SPI_PIN_CONFIG(id)   ((id) & (AO_SPI_INDEX_MASK | AO_SPI_CONFIG_MASK))
77
78 #define AO_SPI_CPOL_BIT         4
79 #define AO_SPI_CPHA_BIT         5
80 #define AO_SPI_CPOL(id)         ((uint32_t) (((id) >> AO_SPI_CPOL_BIT) & 1))
81 #define AO_SPI_CPHA(id)         ((uint32_t) (((id) >> AO_SPI_CPHA_BIT) & 1))
82
83 #define AO_SPI_MAKE_MODE(pol,pha)       (((pol) << AO_SPI_CPOL_BIT) | ((pha) << AO_SPI_CPHA_BIT))
84 #define AO_SPI_MODE_0           AO_SPI_MAKE_MODE(0,0)
85 #define AO_SPI_MODE_1           AO_SPI_MAKE_MODE(0,1)
86 #define AO_SPI_MODE_2           AO_SPI_MAKE_MODE(1,0)
87 #define AO_SPI_MODE_3           AO_SPI_MAKE_MODE(1,1)
88
89 uint8_t
90 ao_spi_try_get(uint8_t spi_index, uint32_t speed, uint8_t task_id);
91
92 void
93 ao_spi_get(uint8_t spi_index, uint32_t speed);
94
95 void
96 ao_spi_put(uint8_t spi_index);
97
98 void
99 ao_spi_put_pins(uint8_t spi_index);
100
101 void
102 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
103
104 void
105 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
106
107 void
108 ao_spi_send_sync(const void *block, uint16_t len, uint8_t spi_index);
109
110 void
111 ao_spi_start_bytes(uint8_t spi_index);
112
113 void
114 ao_spi_stop_bytes(uint8_t spi_index);
115
116 static inline void
117 ao_spi_send_byte(uint8_t byte, uint8_t spi_index)
118 {
119         struct stm_spi  *stm_spi;
120
121         switch (AO_SPI_INDEX(spi_index)) {
122         case 0:
123                 stm_spi = &stm_spi1;
124                 break;
125         case 1:
126                 stm_spi = &stm_spi2;
127                 break;
128         }
129
130         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
131                 ;
132         stm_spi->dr = byte;
133         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
134                 ;
135         (void) stm_spi->dr;
136 }
137
138 static inline uint8_t
139 ao_spi_recv_byte(uint8_t spi_index)
140 {
141         struct stm_spi  *stm_spi;
142
143         switch (AO_SPI_INDEX(spi_index)) {
144         case 0:
145                 stm_spi = &stm_spi1;
146                 break;
147         case 1:
148                 stm_spi = &stm_spi2;
149                 break;
150         }
151
152         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)))
153                 ;
154         stm_spi->dr = 0xff;
155         while (!(stm_spi->sr & (1 << STM_SPI_SR_RXNE)))
156                 ;
157         return (uint8_t) stm_spi->dr;
158 }
159
160 void
161 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
162
163 void
164 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
165
166 void
167 ao_spi_init(void);
168
169 #define ao_spi_set_cs(reg,mask) ((reg)->bsrr = ((uint32_t) (mask)) << 16)
170 #define ao_spi_clr_cs(reg,mask) ((reg)->bsrr = (mask))
171
172 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
173                 ao_spi_get(bus, speed);                         \
174                 ao_spi_set_cs(reg,mask);                        \
175         } while (0)
176
177 static inline uint8_t
178 ao_spi_try_get_mask(struct stm_gpio *reg, uint16_t mask, uint8_t bus, uint32_t speed, uint8_t task_id)
179 {
180         if (!ao_spi_try_get(bus, speed, task_id))
181                 return 0;
182         ao_spi_set_cs(reg, mask);
183         return 1;
184 }
185
186 #define ao_spi_put_mask(reg,mask,bus) do {      \
187                 ao_spi_clr_cs(reg,mask);        \
188                 ao_spi_put(bus);                \
189         } while (0)
190
191 #define ao_spi_get_bit(reg,bit,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
192 #define ao_spi_put_bit(reg,bit,bus) ao_spi_put_mask(reg,(1<<bit),bus)
193
194 #if AO_POWER_MANAGEMENT
195 extern struct ao_power  ao_power_gpioa;
196 extern struct ao_power  ao_power_gpiob;
197 extern struct ao_power  ao_power_gpioc;
198 extern struct ao_power  ao_power_gpiof;
199 #endif
200
201 static inline void ao_enable_port(struct stm_gpio *port)
202 {
203         if ((port) == &stm_gpioa) {
204                 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_IOPAEN);
205                 ao_power_register(&ao_power_gpioa);
206         } else if ((port) == &stm_gpiob) {
207                 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_IOPBEN);
208                 ao_power_register(&ao_power_gpiob);
209         } else if ((port) == &stm_gpioc) {
210                 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_IOPCEN);
211                 ao_power_register(&ao_power_gpioc);
212         } else if ((port) == &stm_gpiof) {
213                 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_IOPFEN);
214                 ao_power_register(&ao_power_gpiof);
215         }
216 }
217
218 static inline void ao_disable_port(struct stm_gpio *port)
219 {
220         if ((port) == &stm_gpioa) {
221                 stm_rcc.ahbenr &= ~(1UL << STM_RCC_AHBENR_IOPAEN);
222                 ao_power_unregister(&ao_power_gpioa);
223         } else if ((port) == &stm_gpiob) {
224                 stm_rcc.ahbenr &= ~(1UL << STM_RCC_AHBENR_IOPBEN);
225                 ao_power_unregister(&ao_power_gpiob);
226         } else if ((port) == &stm_gpioc) {
227                 stm_rcc.ahbenr &= ~(1UL << STM_RCC_AHBENR_IOPCEN);
228                 ao_power_unregister(&ao_power_gpioc);
229         } else if ((port) == &stm_gpiof) {
230                 stm_rcc.ahbenr &= ~(1UL << STM_RCC_AHBENR_IOPFEN);
231                 ao_power_unregister(&ao_power_gpiof);
232         }
233 }
234
235 #define ao_gpio_set(port, bit, v) stm_gpio_set(port, bit, v)
236
237 #define ao_gpio_get(port, bit) stm_gpio_get(port, bit)
238
239 #define ao_enable_output(port,bit,v) do {                       \
240                 ao_enable_port(port);                           \
241                 ao_gpio_set(port, bit, v);                      \
242                 stm_moder_set(port, bit, STM_MODER_OUTPUT);\
243         } while (0)
244
245 #define ao_gpio_set_mode(port,bit,mode) do {                            \
246                 if (mode == AO_EXTI_MODE_PULL_UP)                       \
247                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP);    \
248                 else if (mode == AO_EXTI_MODE_PULL_DOWN)                \
249                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN);  \
250                 else                                                    \
251                         stm_pupdr_set(port, bit, STM_PUPDR_NONE);       \
252         } while (0)
253
254 #define ao_enable_input(port,bit,mode) do {                             \
255                 ao_enable_port(port);                                   \
256                 stm_moder_set(port, bit, STM_MODER_INPUT);              \
257                 ao_gpio_set_mode(port, bit, mode);                      \
258         } while (0)
259
260 #define ao_enable_cs(port,bit) do {                             \
261                 ao_enable_output(port, bit, 1);         \
262         } while (0)
263
264 #define ao_spi_init_cs(port, mask) do {                         \
265                 ao_enable_port(port);                           \
266                 if ((mask) & 0x0001) ao_enable_cs(port, 0);     \
267                 if ((mask) & 0x0002) ao_enable_cs(port, 1);     \
268                 if ((mask) & 0x0004) ao_enable_cs(port, 2);     \
269                 if ((mask) & 0x0008) ao_enable_cs(port, 3);     \
270                 if ((mask) & 0x0010) ao_enable_cs(port, 4);     \
271                 if ((mask) & 0x0020) ao_enable_cs(port, 5);     \
272                 if ((mask) & 0x0040) ao_enable_cs(port, 6);     \
273                 if ((mask) & 0x0080) ao_enable_cs(port, 7);     \
274                 if ((mask) & 0x0100) ao_enable_cs(port, 8);     \
275                 if ((mask) & 0x0200) ao_enable_cs(port, 9);     \
276                 if ((mask) & 0x0400) ao_enable_cs(port, 10);\
277                 if ((mask) & 0x0800) ao_enable_cs(port, 11);\
278                 if ((mask) & 0x1000) ao_enable_cs(port, 12);\
279                 if ((mask) & 0x2000) ao_enable_cs(port, 13);\
280                 if ((mask) & 0x4000) ao_enable_cs(port, 14);\
281                 if ((mask) & 0x8000) ao_enable_cs(port, 15);\
282         } while (0)
283
284 /* ao_dma_stm.c
285  */
286
287 extern uint8_t ao_dma_done[STM_NUM_DMA];
288
289 void
290 ao_dma_set_transfer(uint8_t             index,
291                     volatile void       *peripheral,
292                     void                *memory,
293                     uint16_t            count,
294                     uint32_t            ccr);
295
296 void
297 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
298
299 void
300 ao_dma_start(uint8_t index);
301
302 void
303 ao_dma_done_transfer(uint8_t index);
304
305 void
306 ao_dma_abort(uint8_t index);
307
308 void
309 ao_dma_alloc(uint8_t index);
310
311 void
312 ao_dma_init(void);
313
314 /* ao_i2c_stm.c */
315
316 void
317 ao_i2c_get(uint8_t i2c_index);
318
319 uint8_t
320 ao_i2c_start(uint8_t i2c_index, uint16_t address);
321
322 void
323 ao_i2c_put(uint8_t i2c_index);
324
325 uint8_t
326 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
327
328 uint8_t
329 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
330
331 void
332 ao_i2c_init(void);
333
334 /* ao_serial_stm.c */
335
336 #if USE_SERIAL_1_FLOW && USE_SERIAL_1_SW_FLOW || USE_SERIAL_2_FLOW && USE_SERIAL_2_SW_FLOW
337 #define HAS_SERIAL_SW_FLOW      1
338 #else
339 #define HAS_SERIAL_SW_FLOW      0
340 #endif
341
342 #if USE_SERIAL_2_FLOW && !USE_SERIAL_2_SW_FLOW
343 #define USE_SERIAL_2_HW_FLOW    1
344 #endif
345
346 #if USE_SERIAL_1_FLOW && !USE_SERIAL_1_SW_FLOW
347 #define USE_SERIAL_1_HW_FLOW    1
348 #endif
349
350 #if USE_SERIAL_1_HW_FLOW || USE_SERIAL_2_HW_FLOW
351 #define HAS_SERIAL_HW_FLOW      1
352 #else
353 #define HAS_SERIAL_HW_FLOW      0
354 #endif
355
356 struct ao_stm_usart {
357         struct ao_fifo          rx_fifo;
358         struct ao_fifo          tx_fifo;
359         struct stm_usart        *reg;
360         uint8_t                 tx_running;
361         uint8_t                 draining;
362 #if HAS_SERIAL_SW_FLOW
363         /* RTS - 0 if we have FIFO space, 1 if not
364          * CTS - 0 if we can send, 0 if not
365          */
366         struct stm_gpio         *gpio_rts;
367         struct stm_gpio         *gpio_cts;
368         uint8_t                 pin_rts;
369         uint8_t                 pin_cts;
370         uint8_t                 rts;
371 #endif
372 };
373
374 #if HAS_SERIAL_1
375 extern struct ao_stm_usart      ao_stm_usart1;
376 #endif
377
378 #if HAS_SERIAL_2
379 extern struct ao_stm_usart      ao_stm_usart2;
380 #endif
381
382 #if HAS_SERIAL_3
383 extern struct ao_stm_usart      ao_stm_usart3;
384 #endif
385
386 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
387
388 typedef uint32_t        ao_arch_irq_t;
389
390 static inline uint32_t
391 ao_arch_irqsave(void) {
392         uint32_t        primask;
393         asm("mrs %0,primask" : "=&r" (primask));
394         ao_arch_block_interrupts();
395         return primask;
396 }
397
398 static inline void
399 ao_arch_irqrestore(uint32_t primask) {
400         asm("msr primask,%0" : : "r" (primask));
401 }
402
403 static inline void
404 ao_arch_memory_barrier(void) {
405         asm volatile("" ::: "memory");
406 }
407
408 #if HAS_TASK
409 static inline void
410 ao_arch_init_stack(struct ao_task *task, uint32_t *sp, void *start)
411 {
412         uint32_t        a = (uint32_t) start;
413         int             i;
414
415         /* Return address (goes into LR) */
416         ARM_PUSH32(sp, a);
417
418         /* Clear register values r0-r7 */
419         i = 8;
420         while (i--)
421                 ARM_PUSH32(sp, 0);
422
423         /* APSR */
424         ARM_PUSH32(sp, 0);
425
426         /* PRIMASK with interrupts enabled */
427         ARM_PUSH32(sp, 0);
428
429         task->sp32 = sp;
430 }
431
432 static inline void ao_arch_save_regs(void) {
433         /* Save general registers */
434         asm("push {r0-r7,lr}\n");
435
436         /* Save APSR */
437         asm("mrs r0,apsr");
438         asm("push {r0}");
439
440         /* Save PRIMASK */
441         asm("mrs r0,primask");
442         asm("push {r0}");
443 }
444
445 static inline void ao_arch_save_stack(void) {
446         uint32_t        *sp;
447         asm("mov %0,sp" : "=&r" (sp) );
448         ao_cur_task->sp32 = (sp);
449         if (sp < &ao_cur_task->stack32[0])
450                 ao_panic (AO_PANIC_STACK);
451 }
452
453 static inline void ao_arch_restore_stack(void) {
454         /* Switch stacks */
455         asm("mov sp, %0" : : "r" (ao_cur_task->sp32) );
456
457         /* Restore PRIMASK */
458         asm("pop {r0}");
459         asm("msr primask,r0");
460
461         /* Restore APSR */
462         asm("pop {r0}");
463         asm("msr apsr_nczvq,r0");
464
465         /* Restore general registers */
466         asm("pop {r0-r7,pc}\n");
467 }
468
469 static inline void ao_sleep_mode(void) {
470
471         /*
472           WFI (Wait for Interrupt) or WFE (Wait for Event) while:
473            – Set SLEEPDEEP in Cortex ® -M0 System Control register
474            – Set PDDS bit in Power Control register (PWR_CR)
475            – Clear WUF bit in Power Control/Status register (PWR_CSR)
476         */
477
478         ao_arch_block_interrupts();
479
480         /* Enable power interface clock */
481         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
482         ao_arch_nop();
483         stm_scb.scr |= (1 << STM_SCB_SCR_SLEEPDEEP);
484         ao_arch_nop();
485         stm_pwr.cr |= (1 << STM_PWR_CR_PDDS) | (1 << STM_PWR_CR_LPDS);
486         ao_arch_nop();
487         stm_pwr.cr |= (1 << STM_PWR_CR_CWUF);
488         ao_arch_nop();
489         ao_arch_nop();
490         ao_arch_nop();
491         ao_arch_nop();
492         ao_arch_nop();
493         asm("wfi");
494         ao_arch_nop();
495 }
496
497 #ifndef HAS_SAMPLE_PROFILE
498 #define HAS_SAMPLE_PROFILE 0
499 #endif
500
501 #if !HAS_SAMPLE_PROFILE
502 #define HAS_ARCH_START_SCHEDULER        1
503
504 static inline void ao_arch_start_scheduler(void) {
505         uint32_t        sp;
506         uint32_t        control;
507
508         asm("mrs %0,msp" : "=&r" (sp));
509         asm("msr psp,%0" : : "r" (sp));
510         asm("mrs %0,control" : "=&r" (control));
511         control |= (1 << 1);
512         asm("msr control,%0" : : "r" (control));
513         asm("isb");
514 }
515 #endif
516
517 #define ao_arch_isr_stack()
518
519 #endif
520
521 #define ao_arch_wait_interrupt() do {                           \
522                 asm("\twfi\n");                                 \
523                 ao_arch_release_interrupts();                   \
524                 asm(".global ao_idle_loc\nao_idle_loc:");       \
525                 ao_arch_block_interrupts();                     \
526         } while (0)
527
528 #define ao_arch_critical(b) do {                        \
529                 uint32_t __mask = ao_arch_irqsave();    \
530                 do { b } while (0);                     \
531                 ao_arch_irqrestore(__mask);             \
532         } while (0)
533
534 /* ao_usb_stm.c */
535
536 #if AO_USB_DIRECTIO
537 uint8_t
538 ao_usb_alloc(uint16_t *buffers[2]);
539
540 uint8_t
541 ao_usb_alloc2(uint16_t *buffers[2]);
542
543 uint8_t
544 ao_usb_write(uint16_t len);
545
546 uint8_t
547 ao_usb_write2(uint16_t len);
548 #endif /* AO_USB_DIRECTIO */
549
550 void start(void);
551
552 void
553 ao_debug_out(char c);
554
555 #endif /* _AO_ARCH_FUNCS_H_ */