lpc: Initial lpcxpresso bits
[fw/altos] / src / lpc / ao_arch_funcs.h
1 /*
2  * Copyright © 2013 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #ifndef _AO_ARCH_FUNCS_H_
19 #define _AO_ARCH_FUNCS_H_
20
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)
23
24 #define ao_enable_port(port) (lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_GPIO))
25
26 #define lpc_all_bit(port,bit)   (((port) << 5) | (bit))
27
28 #define ao_gpio_set(port, bit, pin, v)  (lpc_gpio.byte[lpc_all_bit(port,bit)] = v)
29
30 #define ao_gpio_get(port, bit, pin)     (lpc_gpio_byte[lpc_all_bit(port,bit)])
31
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);               \
36         } while (0)
37
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);  \
45                 else                                                    \
46                         stm_pupdr_set(port, bit, STM_PUPDR_NONE);       \
47         } while (0)
48
49 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
50
51 static inline uint32_t
52 ao_arch_irqsave(void) {
53         uint32_t        primask;
54         asm("mrs %0,primask" : "=&r" (primask));
55         ao_arch_block_interrupts();
56         return primask;
57 }
58
59 static inline void
60 ao_arch_irqrestore(uint32_t primask) {
61         asm("msr primask,%0" : : "r" (primask));
62 }
63
64 static inline void
65 ao_arch_memory_barrier() {
66         asm volatile("" ::: "memory");
67 }
68
69 static inline void
70 ao_arch_init_stack(struct ao_task *task, void *start)
71 {
72         uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
73         uint32_t        a = (uint32_t) start;
74         int             i;
75
76         /* Return address (goes into LR) */
77         ARM_PUSH32(sp, a);
78
79         /* Clear register values r0-r7 */
80         i = 8;
81         while (i--)
82                 ARM_PUSH32(sp, 0);
83
84         /* APSR */
85         ARM_PUSH32(sp, 0);
86
87         /* PRIMASK with interrupts enabled */
88         ARM_PUSH32(sp, 0);
89
90         task->sp = sp;
91 }
92
93 static inline void ao_arch_save_regs(void) {
94         /* Save general registers */
95         asm("push {r0-r7,lr}\n");
96
97         /* Save APSR */
98         asm("mrs r0,apsr");
99         asm("push {r0}");
100
101         /* Save PRIMASK */
102         asm("mrs r0,primask");
103         asm("push {r0}");
104 }
105
106 static inline void ao_arch_save_stack(void) {
107         uint32_t        *sp;
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);
112 }
113
114 static inline void ao_arch_restore_stack(void) {
115         uint32_t        sp;
116         sp = (uint32_t) ao_cur_task->sp;
117
118         /* Switch stacks */
119         asm("mov sp, %0" : : "r" (sp) );
120
121         /* Restore PRIMASK */
122         asm("pop {r0}");
123         asm("msr primask,r0");
124
125         /* Restore APSR */
126         asm("pop {r0}");
127         asm("msr apsr,r0");
128
129         /* Restore general registers and return */
130         asm("pop {r0-r7,pc}\n");
131 }
132
133 #define ao_arch_isr_stack()
134
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();                             \
139         } while (0)
140
141 #define ao_arch_critical(b) do {                                \
142                 ao_arch_block_interrupts();                     \
143                 do { b } while (0);                             \
144                 ao_arch_release_interrupts();                   \
145         } while (0)
146
147 #endif /* _AO_ARCH_FUNCS_H_ */