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