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))
25 #define ao_disable_port(port) (lpc_scb.sysahbclkctrl &= ~(1 << LPC_SCB_SYSAHBCLKCTRL_GPIO))
27 #define lpc_all_bit(port,bit) (((port) << 5) | (bit))
29 #define ao_gpio_set(port, bit, pin, v) (lpc_gpio.byte[lpc_all_bit(port,bit)] = (v))
31 #define ao_gpio_get(port, bit, pin) (lpc_gpio.byte[lpc_all_bit(port,bit)])
33 #define ao_enable_output(port,bit,pin,v) do { \
34 ao_enable_port(port); \
35 ao_gpio_set(port, bit, pin, v); \
36 lpc_gpio.dir[port] |= (1 << bit); \
39 #define ao_gpio_set_mode(port,bit,mode) do { \
40 vuint32_t *_ioconf = &lpc_ioconf.pio0_0 + ((port)*24+(bit)); \
42 if (mode == AO_EXTI_MODE_PULL_UP) \
43 _mode = LPC_IOCONF_MODE_PULL_UP << LPC_IOCONF_MODE; \
44 else if (mode == AO_EXTI_MODE_PULL_DOWN) \
45 _mode = LPC_IOCONF_MODE_PULL_UP << LPC_IOCONF_MODE; \
47 _mode = LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE; \
48 *_ioconf = ((*_ioconf & ~(LPC_IOCONF_MODE_MASK << LPC_IOCONF_MODE)) | \
50 (1 << LPC_IOCONF_ADMODE)); \
53 #define ao_enable_input(port,bit,mode) do { \
54 ao_enable_port(port); \
55 lpc_gpio.dir[port] &= ~(1 << bit); \
56 ao_gpio_set_mode(port,bit,mode); \
59 #define lpc_token_paster_2(x,y) x ## y
60 #define lpc_token_evaluator_2(x,y) lpc_token_paster_2(x,y)
61 #define lpc_token_paster_3(x,y,z) x ## y ## z
62 #define lpc_token_evaluator_3(x,y,z) lpc_token_paster_3(x,y,z)
63 #define lpc_token_paster_4(w,x,y,z) w ## x ## y ## z
64 #define lpc_token_evaluator_4(w,x,y,z) lpc_token_paster_4(w,x,y,z)
65 #define analog_reg(port,bit) lpc_token_evaluator_4(pio,port,_,bit)
66 #define analog_func(id) lpc_token_evaluator_2(LPC_IOCONF_FUNC_AD,id)
68 #define ao_enable_analog(port,bit,id) do { \
69 ao_enable_port(port); \
70 lpc_gpio.dir[port] &= ~(1 << bit); \
71 lpc_ioconf.analog_reg(port,bit) = ((analog_func(id) << LPC_IOCONF_FUNC) | \
72 (0 << LPC_IOCONF_ADMODE)); \
75 #define ARM_PUSH32(stack, val) (*(--(stack)) = (val))
77 static inline uint32_t
78 ao_arch_irqsave(void) {
80 asm("mrs %0,primask" : "=&r" (primask));
81 ao_arch_block_interrupts();
86 ao_arch_irqrestore(uint32_t primask) {
87 asm("msr primask,%0" : : "r" (primask));
91 ao_arch_memory_barrier() {
92 asm volatile("" ::: "memory");
97 ao_arch_init_stack(struct ao_task *task, void *start)
99 uint32_t *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
100 uint32_t a = (uint32_t) start;
103 /* Return address (goes into LR) */
106 /* Clear register values r0-r7 */
114 /* PRIMASK with interrupts enabled */
120 static inline void ao_arch_save_regs(void) {
121 /* Save general registers */
122 asm("push {r0-r7,lr}\n");
129 asm("mrs r0,primask");
133 static inline void ao_arch_save_stack(void) {
135 asm("mov %0,sp" : "=&r" (sp) );
136 ao_cur_task->sp = (sp);
137 if ((uint8_t *) sp < &ao_cur_task->stack[0])
138 ao_panic (AO_PANIC_STACK);
141 static inline void ao_arch_restore_stack(void) {
143 sp = (uint32_t) ao_cur_task->sp;
146 asm("mov sp, %0" : : "r" (sp) );
148 /* Restore PRIMASK */
150 asm("msr primask,r0");
154 asm("msr apsr_nczvq,r0");
156 /* Restore general registers and return */
157 asm("pop {r0-r7,pc}\n");
160 #define ao_arch_isr_stack()
162 #endif /* HAS_TASK */
164 #define ao_arch_wait_interrupt() do { \
165 asm(".global ao_idle_loc\n\twfi\nao_idle_loc:"); \
166 ao_arch_release_interrupts(); \
167 ao_arch_block_interrupts(); \
170 #define ao_arch_critical(b) do { \
171 ao_arch_block_interrupts(); \
172 do { b } while (0); \
173 ao_arch_release_interrupts(); \
180 #define ao_spi_set_cs(port,mask) (lpc_gpio.clr[port] = (mask))
181 #define ao_spi_clr_cs(port,mask) (lpc_gpio.set[port] = (mask))
183 #define ao_spi_get_mask(port,mask,bus,speed) do { \
184 ao_spi_get(bus, speed); \
185 ao_spi_set_cs(port, mask); \
188 #define ao_spi_put_mask(reg,mask,bus) do { \
189 ao_spi_clr_cs(reg,mask); \
193 #define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
194 #define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
197 ao_spi_get(uint8_t spi_index, uint32_t speed);
200 ao_spi_put(uint8_t spi_index);
203 ao_spi_send(void *block, uint16_t len, uint8_t spi_index);
206 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
209 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
212 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
214 extern uint16_t ao_spi_speed[LPC_NUM_SPI];
219 #define ao_spi_init_cs(port, mask) do { \
221 for (__bit__ = 0; __bit__ < 32; __bit__++) { \
222 if (mask & (1 << __bit__)) \
223 ao_enable_output(port, __bit__, PIN, 1); \
227 #define HAS_ARCH_START_SCHEDULER 1
229 static inline void ao_arch_start_scheduler(void) {
233 asm("mrs %0,msp" : "=&r" (sp));
234 asm("msr psp,%0" : : "r" (sp));
235 asm("mrs %0,control" : "=&r" (control));
237 asm("msr control,%0" : : "r" (control));
241 #endif /* _AO_ARCH_FUNCS_H_ */