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