2 * Copyright © 2012 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.
25 * STM32L definitions and code fragments for AltOS
28 #define AO_STACK_SIZE 512
30 #define AO_LED_TYPE uint16_t
33 #define AO_TICK_TYPE uint16_t
34 #define AO_TICK_SIGNED int16_t
37 /* Various definitions to make GCC look more like SDCC */
39 #define ao_arch_naked_declare __attribute__((naked))
40 #define ao_arch_naked_define
46 #define __interrupt(n)
49 #define CORTEX_M3_AIRCR ((uint32_t *) 0xe000ed0c)
51 #define ao_arch_reboot() (*((uint32_t *) 0xe000ed0c) = 0x05fa0004)
53 #define ao_arch_nop() asm("nop")
55 #define ao_arch_interrupt(n) /* nothing */
59 #define putchar(c) ao_putchar(c)
60 #define getchar ao_getchar
62 extern void putchar(char c);
63 extern char getchar(void);
64 extern void ao_avr_stdio_init(void);
71 #define AO_ROMCONFIG_VERSION 2
73 #define AO_ROMCONFIG_SYMBOL(a) __attribute__((section(".romconfig"))) const
75 extern const uint16_t ao_romconfig_version;
76 extern const uint16_t ao_romconfig_check;
77 extern const uint16_t ao_serial_number;
78 extern const uint32_t ao_radio_cal;
80 #define ARM_PUSH32(stack, val) (*(--(stack)) = (val))
82 #define ao_arch_task_members\
83 uint32_t *sp; /* saved stack pointer */
85 #define ao_arch_block_interrupts() asm("cpsid i")
86 #define ao_arch_release_interrupts() asm("cpsie i")
88 #define cli() ao_arch_block_interrupts()
89 #define sei() ao_arch_release_interrupts()
92 ao_arch_irqsave(void) {
94 asm("mrs %0,primask" : "=&r" (primask));
95 ao_arch_block_interrupts();
100 ao_arch_irqrestore(uint32_t primask) {
101 asm("msr primask,%0" : : "r" (primask));
105 ao_arch_memory_barrier() {
106 asm volatile("" ::: "memory");
109 #define ao_arch_init_stack(task, start) do { \
110 uint32_t *sp = (uint32_t *) (task->stack + AO_STACK_SIZE); \
111 uint32_t a = (uint32_t) start; \
114 /* Return address (goes into LR) */ \
117 /* Clear register values r0-r12 */ \
125 /* PRIMASK with interrupts enabled */ \
131 #define ao_arch_save_regs() do { \
132 /* Save general registers */ \
133 asm("push {r0-r12,lr}\n"); \
136 asm("mrs r0,apsr"); \
140 asm("mrs r0,primask"); \
143 /* Enable interrupts */ \
147 #define ao_arch_save_stack() do { \
149 asm("mov %0,sp" : "=&r" (sp) ); \
150 ao_cur_task->sp = (sp); \
151 if ((uint8_t *) sp < &ao_cur_task->stack[0]) \
152 ao_panic (AO_PANIC_STACK); \
156 #define ao_arch_isr_stack() do { \
157 uint32_t *sp = (uint32_t *) 0x20004000; \
158 asm("mov %0,sp" : "=&r" (sp) ); \
161 #define ao_arch_isr_stack()
165 #define ao_arch_cpu_idle() do { \
166 asm(".global ao_idle_loc\n\twfi\nao_idle_loc:"); \
169 #define ao_arch_restore_stack() do { \
171 sp = (uint32_t) ao_cur_task->sp; \
173 /* Switch stacks */ \
174 asm("mov sp, %0" : : "r" (sp) ); \
176 /* Restore PRIMASK */ \
178 asm("msr primask,r0"); \
182 asm("msr apsr,r0"); \
184 /* Restore general registers */ \
185 asm("pop {r0-r12,lr}\n"); \
187 /* Return to calling function */ \
191 #define ao_arch_critical(b) do { cli(); do { b } while (0); sei(); } while (0)
194 * For now, we're running at a weird frequency
198 #define AO_PLLSRC AO_HSE
200 #define AO_PLLSRC STM_HSI_FREQ
203 #define AO_PLLVCO (AO_PLLSRC * AO_PLLMUL)
204 #define AO_SYSCLK (AO_PLLVCO / AO_PLLDIV)
205 #define AO_HCLK (AO_SYSCLK / AO_AHB_PRESCALER)
206 #define AO_PCLK1 (AO_HCLK / AO_APB1_PRESCALER)
207 #define AO_PCLK2 (AO_HCLK / AO_APB2_PRESCALER)
209 #if AO_APB1_PRESCALER == 1
210 #define AO_TIM23467_CLK AO_PCLK1
212 #define AO_TIM23467_CLK (2 * AO_PCLK1)
215 #if AO_APB2_PRESCALER == 1
216 #define AO_TIM91011_CLK AO_PCLK2
218 #define AO_TIM91011_CLK (2 * AO_PCLK2)
221 #define AO_STM_NVIC_HIGH_PRIORITY 4
222 #define AO_STM_NVIC_CLOCK_PRIORITY 6
223 #define AO_STM_NVIC_MED_PRIORITY 8
224 #define AO_STM_NVIC_LOW_PRIORITY 10
226 void ao_lcd_stm_init(void);
228 void ao_lcd_font_init(void);
230 void ao_lcd_font_string(char *s);
233 ao_serial1_getchar(void);
236 ao_serial1_putchar(char c);
239 ao_serial1_pollchar(void);
242 ao_serial1_set_speed(uint8_t speed);
245 ao_serial2_getchar(void);
248 ao_serial2_putchar(char c);
251 ao_serial2_pollchar(void);
254 ao_serial2_set_speed(uint8_t speed);
257 ao_serial3_getchar(void);
260 ao_serial3_putchar(char c);
263 ao_serial3_pollchar(void);
266 ao_serial3_set_speed(uint8_t speed);
268 extern const uint32_t ao_radio_cal;
273 #endif /* _AO_ARCH_H_ */