1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* Copyright © 2013 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#ifndef _AO_ARCH_FUNCS_H_
#define _AO_ARCH_FUNCS_H_
#define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
#define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
#define ao_enable_port(port) (lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_GPIO))
#define lpc_all_bit(port,bit) (((port) << 5) | (bit))
#define ao_gpio_set(port, bit, pin, v) (lpc_gpio.byte[lpc_all_bit(port,bit)] = v)
#define ao_gpio_get(port, bit, pin) (lpc_gpio_byte[lpc_all_bit(port,bit)])
#define ao_enable_output(port,bit,pin,v) do { \
ao_enable_port(port); \
ao_gpio_set(port, bit, pin, v); \
lpc_gpio.dir[port] |= (1 << bit); \
} while (0)
#define ao_enable_input(port,bit,mode) do { \
ao_enable_port(port); \
lpc_gpio.dir[port] &= ~(1 << bit); \
if (mode == AO_EXTI_MODE_PULL_UP) \
stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP); \
else if (mode == AO_EXTI_MODE_PULL_DOWN) \
stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN); \
else \
stm_pupdr_set(port, bit, STM_PUPDR_NONE); \
} while (0)
#define ARM_PUSH32(stack, val) (*(--(stack)) = (val))
static inline uint32_t
ao_arch_irqsave(void) {
uint32_t primask;
asm("mrs %0,primask" : "=&r" (primask));
ao_arch_block_interrupts();
return primask;
}
static inline void
ao_arch_irqrestore(uint32_t primask) {
asm("msr primask,%0" : : "r" (primask));
}
static inline void
ao_arch_memory_barrier() {
asm volatile("" ::: "memory");
}
static inline void
ao_arch_init_stack(struct ao_task *task, void *start)
{
uint32_t *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
uint32_t a = (uint32_t) start;
int i;
/* Return address (goes into LR) */
ARM_PUSH32(sp, a);
/* Clear register values r0-r7 */
i = 8;
while (i--)
ARM_PUSH32(sp, 0);
/* APSR */
ARM_PUSH32(sp, 0);
/* PRIMASK with interrupts enabled */
ARM_PUSH32(sp, 0);
task->sp = sp;
}
static inline void ao_arch_save_regs(void) {
/* Save general registers */
asm("push {r0-r7,lr}\n");
/* Save APSR */
asm("mrs r0,apsr");
asm("push {r0}");
/* Save PRIMASK */
asm("mrs r0,primask");
asm("push {r0}");
}
static inline void ao_arch_save_stack(void) {
uint32_t *sp;
asm("mov %0,sp" : "=&r" (sp) );
ao_cur_task->sp = (sp);
if ((uint8_t *) sp < &ao_cur_task->stack[0])
ao_panic (AO_PANIC_STACK);
}
static inline void ao_arch_restore_stack(void) {
uint32_t sp;
sp = (uint32_t) ao_cur_task->sp;
/* Switch stacks */
asm("mov sp, %0" : : "r" (sp) );
/* Restore PRIMASK */
asm("pop {r0}");
asm("msr primask,r0");
/* Restore APSR */
asm("pop {r0}");
asm("msr apsr,r0");
/* Restore general registers and return */
asm("pop {r0-r7,pc}\n");
}
#define ao_arch_isr_stack()
#define ao_arch_wait_interrupt() do { \
asm(".global ao_idle_loc\n\twfi\nao_idle_loc:"); \
ao_arch_release_interrupts(); \
ao_arch_block_interrupts(); \
} while (0)
#define ao_arch_critical(b) do { \
ao_arch_block_interrupts(); \
do { b } while (0); \
ao_arch_release_interrupts(); \
} while (0)
#endif /* _AO_ARCH_FUNCS_H_ */
|