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