2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 #ifndef _AO_ARCH_FUNCS_H_
19 #define _AO_ARCH_FUNCS_H_
21 #define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
22 #define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
24 #define ao_enable_port(port) (lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_GPIO))
26 #define lpc_all_bit(port,bit) (((port) << 5) | (bit))
28 #define ao_gpio_set(port, bit, pin, v) (lpc_gpio.byte[lpc_all_bit(port,bit)] = v)
30 #define ao_gpio_get(port, bit, pin) (lpc_gpio_byte[lpc_all_bit(port,bit)])
32 #define ao_enable_output(port,bit,pin,v) do { \
33 ao_enable_port(port); \
34 ao_gpio_set(port, bit, pin, v); \
35 lpc_gpio.dir[port] |= (1 << bit); \
38 #define ao_enable_input(port,bit,mode) do { \
39 ao_enable_port(port); \
40 lpc_gpio.dir[port] &= ~(1 << bit); \
41 if (mode == AO_EXTI_MODE_PULL_UP) \
42 stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP); \
43 else if (mode == AO_EXTI_MODE_PULL_DOWN) \
44 stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN); \
46 stm_pupdr_set(port, bit, STM_PUPDR_NONE); \
49 #define ARM_PUSH32(stack, val) (*(--(stack)) = (val))
51 static inline uint32_t
52 ao_arch_irqsave(void) {
54 asm("mrs %0,primask" : "=&r" (primask));
55 ao_arch_block_interrupts();
60 ao_arch_irqrestore(uint32_t primask) {
61 asm("msr primask,%0" : : "r" (primask));
65 ao_arch_memory_barrier() {
66 asm volatile("" ::: "memory");
70 ao_arch_init_stack(struct ao_task *task, void *start)
72 uint32_t *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
73 uint32_t a = (uint32_t) start;
76 /* Return address (goes into LR) */
79 /* Clear register values r0-r7 */
87 /* PRIMASK with interrupts enabled */
93 static inline void ao_arch_save_regs(void) {
94 /* Save general registers */
95 asm("push {r0-r7,lr}\n");
102 asm("mrs r0,primask");
106 static inline void ao_arch_save_stack(void) {
108 asm("mov %0,sp" : "=&r" (sp) );
109 ao_cur_task->sp = (sp);
110 if ((uint8_t *) sp < &ao_cur_task->stack[0])
111 ao_panic (AO_PANIC_STACK);
114 static inline void ao_arch_restore_stack(void) {
116 sp = (uint32_t) ao_cur_task->sp;
119 asm("mov sp, %0" : : "r" (sp) );
121 /* Restore PRIMASK */
123 asm("msr primask,r0");
127 asm("msr apsr_nczvq,r0");
129 /* Restore general registers and return */
130 asm("pop {r0-r7,pc}\n");
133 #define ao_arch_isr_stack()
135 #define ao_arch_wait_interrupt() do { \
136 asm(".global ao_idle_loc\n\twfi\nao_idle_loc:"); \
137 ao_arch_release_interrupts(); \
138 ao_arch_block_interrupts(); \
141 #define ao_arch_critical(b) do { \
142 ao_arch_block_interrupts(); \
143 do { b } while (0); \
144 ao_arch_release_interrupts(); \
147 #endif /* _AO_ARCH_FUNCS_H_ */