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