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.
18 #ifndef _AO_ARCH_FUNCS_H_
19 #define _AO_ARCH_FUNCS_H_
24 /* PCLK is set to 16MHz (HCLK 32MHz, APB prescaler 2) */
26 #define AO_SPI_SPEED_8MHz STM_SPI_CR1_BR_PCLK_2
27 #define AO_SPI_SPEED_4MHz STM_SPI_CR1_BR_PCLK_4
28 #define AO_SPI_SPEED_2MHz STM_SPI_CR1_BR_PCLK_8
29 #define AO_SPI_SPEED_1MHz STM_SPI_CR1_BR_PCLK_16
30 #define AO_SPI_SPEED_500kHz STM_SPI_CR1_BR_PCLK_32
31 #define AO_SPI_SPEED_250kHz STM_SPI_CR1_BR_PCLK_64
32 #define AO_SPI_SPEED_125kHz STM_SPI_CR1_BR_PCLK_128
33 #define AO_SPI_SPEED_62500Hz STM_SPI_CR1_BR_PCLK_256
35 #define AO_SPI_SPEED_FAST AO_SPI_SPEED_8MHz
37 /* Companion bus wants something no faster than 200kHz */
39 #define AO_SPI_SPEED_200kHz AO_SPI_SPEED_125kHz
41 #define AO_SPI_CONFIG_1 0x00
42 #define AO_SPI_1_CONFIG_PA5_PA6_PA7 AO_SPI_CONFIG_1
43 #define AO_SPI_2_CONFIG_PB13_PB14_PB15 AO_SPI_CONFIG_1
45 #define AO_SPI_CONFIG_2 0x04
46 #define AO_SPI_1_CONFIG_PB3_PB4_PB5 AO_SPI_CONFIG_2
47 #define AO_SPI_2_CONFIG_PD1_PD3_PD4 AO_SPI_CONFIG_2
49 #define AO_SPI_CONFIG_3 0x08
50 #define AO_SPI_1_CONFIG_PE13_PE14_PE15 AO_SPI_CONFIG_3
52 #define AO_SPI_CONFIG_NONE 0x0c
54 #define AO_SPI_INDEX_MASK 0x01
55 #define AO_SPI_CONFIG_MASK 0x0c
57 #define AO_SPI_1_PA5_PA6_PA7 (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PA5_PA6_PA7)
58 #define AO_SPI_1_PB3_PB4_PB5 (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PB3_PB4_PB5)
59 #define AO_SPI_1_PE13_PE14_PE15 (STM_SPI_INDEX(1) | AO_SPI_1_CONFIG_PE13_PE14_PE15)
61 #define AO_SPI_2_PB13_PB14_PB15 (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PB13_PB14_PB15)
62 #define AO_SPI_2_PD1_PD3_PD4 (STM_SPI_INDEX(2) | AO_SPI_2_CONFIG_PD1_PD3_PD4)
64 #define AO_SPI_INDEX(id) ((id) & AO_SPI_INDEX_MASK)
65 #define AO_SPI_CONFIG(id) ((id) & AO_SPI_CONFIG_MASK)
68 ao_spi_try_get(uint8_t spi_index, uint32_t speed, uint8_t task_id);
71 ao_spi_get(uint8_t spi_index, uint32_t speed);
74 ao_spi_put(uint8_t spi_index);
77 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
80 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
83 ao_spi_send_sync(void *block, uint16_t len, uint8_t spi_index);
86 ao_spi_send_byte(uint8_t byte, uint8_t spi_index)
88 struct stm_spi *stm_spi;
90 switch (AO_SPI_INDEX(spi_index)) {
99 stm_spi->cr2 = ((0 << STM_SPI_CR2_TXEIE) |
100 (0 << STM_SPI_CR2_RXNEIE) |
101 (0 << STM_SPI_CR2_ERRIE) |
102 (0 << STM_SPI_CR2_SSOE) |
103 (0 << STM_SPI_CR2_TXDMAEN) |
104 (0 << STM_SPI_CR2_RXDMAEN));
109 while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)));
114 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
117 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
119 extern uint16_t ao_spi_speed[STM_NUM_SPI];
124 #define ao_spi_set_cs(reg,mask) ((reg)->bsrr = ((uint32_t) (mask)) << 16)
125 #define ao_spi_clr_cs(reg,mask) ((reg)->bsrr = (mask))
127 #define ao_spi_get_mask(reg,mask,bus, speed) do { \
128 ao_spi_get(bus, speed); \
129 ao_spi_set_cs(reg,mask); \
132 static inline uint8_t
133 ao_spi_try_get_mask(struct stm_gpio *reg, uint16_t mask, uint8_t bus, uint32_t speed, uint8_t task_id)
135 if (!ao_spi_try_get(bus, speed, task_id))
137 ao_spi_set_cs(reg, mask);
141 #define ao_spi_put_mask(reg,mask,bus) do { \
142 ao_spi_clr_cs(reg,mask); \
146 #define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
147 #define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
149 #define ao_enable_port(port) do { \
150 if ((port) == &stm_gpioa) \
151 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN); \
152 else if ((port) == &stm_gpiob) \
153 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN); \
154 else if ((port) == &stm_gpioc) \
155 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN); \
156 else if ((port) == &stm_gpiod) \
157 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN); \
158 else if ((port) == &stm_gpioe) \
159 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOEEN); \
162 #define ao_disable_port(port) do { \
163 if ((port) == &stm_gpioa) \
164 stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOAEN); \
165 else if ((port) == &stm_gpiob) \
166 stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOBEN); \
167 else if ((port) == &stm_gpioc) \
168 stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOCEN); \
169 else if ((port) == &stm_gpiod) \
170 stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIODEN); \
171 else if ((port) == &stm_gpioe) \
172 stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_GPIOEEN); \
176 #define ao_gpio_set(port, bit, pin, v) stm_gpio_set(port, bit, v)
178 #define ao_gpio_get(port, bit, pin) stm_gpio_get(port, bit)
180 #define ao_gpio_set_bits(port, bits) stm_gpio_set_bits(port, bits)
182 #define ao_gpio_clr_bits(port, bits) stm_gpio_clr_bits(port, bits);
185 #define ao_enable_output(port,bit,pin,v) do { \
186 ao_enable_port(port); \
187 ao_gpio_set(port, bit, pin, v); \
188 stm_moder_set(port, bit, STM_MODER_OUTPUT);\
191 #define ao_gpio_set_mode(port,bit,mode) do { \
192 if (mode == AO_EXTI_MODE_PULL_UP) \
193 stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP); \
194 else if (mode == AO_EXTI_MODE_PULL_DOWN) \
195 stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN); \
197 stm_pupdr_set(port, bit, STM_PUPDR_NONE); \
200 #define ao_enable_input(port,bit,mode) do { \
201 ao_enable_port(port); \
202 stm_moder_set(port, bit, STM_MODER_INPUT); \
203 ao_gpio_set_mode(port, bit, mode); \
206 #define ao_enable_cs(port,bit) do { \
207 stm_gpio_set((port), bit, 1); \
208 stm_moder_set((port), bit, STM_MODER_OUTPUT); \
211 #define ao_spi_init_cs(port, mask) do { \
212 ao_enable_port(port); \
213 if ((mask) & 0x0001) ao_enable_cs(port, 0); \
214 if ((mask) & 0x0002) ao_enable_cs(port, 1); \
215 if ((mask) & 0x0004) ao_enable_cs(port, 2); \
216 if ((mask) & 0x0008) ao_enable_cs(port, 3); \
217 if ((mask) & 0x0010) ao_enable_cs(port, 4); \
218 if ((mask) & 0x0020) ao_enable_cs(port, 5); \
219 if ((mask) & 0x0040) ao_enable_cs(port, 6); \
220 if ((mask) & 0x0080) ao_enable_cs(port, 7); \
221 if ((mask) & 0x0100) ao_enable_cs(port, 8); \
222 if ((mask) & 0x0200) ao_enable_cs(port, 9); \
223 if ((mask) & 0x0400) ao_enable_cs(port, 10);\
224 if ((mask) & 0x0800) ao_enable_cs(port, 11);\
225 if ((mask) & 0x1000) ao_enable_cs(port, 12);\
226 if ((mask) & 0x2000) ao_enable_cs(port, 13);\
227 if ((mask) & 0x4000) ao_enable_cs(port, 14);\
228 if ((mask) & 0x8000) ao_enable_cs(port, 15);\
234 extern uint8_t ao_dma_done[STM_NUM_DMA];
237 ao_dma_set_transfer(uint8_t index,
238 volatile void *peripheral,
244 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
247 ao_dma_start(uint8_t index);
250 ao_dma_done_transfer(uint8_t index);
253 ao_dma_abort(uint8_t index);
256 ao_dma_alloc(uint8_t index);
264 ao_i2c_get(uint8_t i2c_index);
267 ao_i2c_start(uint8_t i2c_index, uint16_t address);
270 ao_i2c_put(uint8_t i2c_index);
273 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
276 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
281 /* ao_serial_stm.c */
282 struct ao_stm_usart {
283 struct ao_fifo rx_fifo;
284 struct ao_fifo tx_fifo;
285 struct stm_usart *reg;
290 extern struct ao_stm_usart ao_stm_usart1;
294 extern struct ao_stm_usart ao_stm_usart2;
298 extern struct ao_stm_usart ao_stm_usart3;
301 #define ARM_PUSH32(stack, val) (*(--(stack)) = (val))
303 typedef uint32_t ao_arch_irq_t;
305 static inline uint32_t
306 ao_arch_irqsave(void) {
308 asm("mrs %0,primask" : "=&r" (primask));
309 ao_arch_block_interrupts();
314 ao_arch_irqrestore(uint32_t primask) {
315 asm("msr primask,%0" : : "r" (primask));
319 ao_arch_memory_barrier() {
320 asm volatile("" ::: "memory");
325 ao_arch_init_stack(struct ao_task *task, void *start)
327 uint32_t *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
328 uint32_t a = (uint32_t) start;
331 /* Return address (goes into LR) */
334 /* Clear register values r0-r12 */
342 /* PRIMASK with interrupts enabled */
348 static inline void ao_arch_save_regs(void) {
349 /* Save general registers */
350 asm("push {r0-r12,lr}\n");
357 asm("mrs r0,primask");
361 static inline void ao_arch_save_stack(void) {
363 asm("mov %0,sp" : "=&r" (sp) );
364 ao_cur_task->sp = (sp);
365 if ((uint8_t *) sp < &ao_cur_task->stack[0])
366 ao_panic (AO_PANIC_STACK);
369 static inline void ao_arch_restore_stack(void) {
373 asm("mrs %0,control" : "=&r" (control));
375 asm("msr control,%0" : : "r" (control));
378 sp = (uint32_t) ao_cur_task->sp;
381 asm("mov sp, %0" : : "r" (sp) );
383 /* Restore PRIMASK */
385 asm("msr primask,r0");
389 asm("msr apsr_nczvq,r0");
391 /* Restore general registers */
392 asm("pop {r0-r12,lr}\n");
394 /* Return to calling function */
398 #ifndef HAS_SAMPLE_PROFILE
399 #define HAS_SAMPLE_PROFILE 0
403 #define HAS_ARCH_VALIDATE_CUR_STACK 1
406 ao_validate_cur_stack(void)
410 asm("mrs %0,psp" : "=&r" (psp));
412 psp <= ao_cur_task->stack &&
413 psp >= ao_cur_task->stack - 256)
414 ao_panic(AO_PANIC_STACK);
418 #if !HAS_SAMPLE_PROFILE
419 #define HAS_ARCH_START_SCHEDULER 1
421 static inline void ao_arch_start_scheduler(void) {
425 asm("mrs %0,msp" : "=&r" (sp));
426 asm("msr psp,%0" : : "r" (sp));
427 asm("mrs %0,control" : "=&r" (control));
429 asm("msr control,%0" : : "r" (control));
434 static inline void ao_arch_isr_stack(void) {
437 asm("mrs %0,control" : "=&r" (control));
438 control &= ~(1 << 1);
439 asm("msr control,%0" : : "r" (control));
445 #define ao_arch_wait_interrupt() do { \
447 ao_arch_release_interrupts(); \
448 asm(".global ao_idle_loc\nao_idle_loc:"); \
449 ao_arch_block_interrupts(); \
452 #define ao_arch_critical(b) do { \
453 uint32_t __mask = ao_arch_irqsave(); \
454 do { b } while (0); \
455 ao_arch_irqrestore(__mask); \
458 #endif /* _AO_ARCH_FUNCS_H_ */