Merge remote-tracking branch 'mjb/altosdroid'
[fw/altos] / src / stm / ao_arch_funcs.h
1 /*
2  * Copyright © 2012 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 /* ao_spi_stm.c
22  */
23
24 /* PCLK is set to 16MHz (HCLK 32MHz, APB prescaler 2) */
25
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
34
35 #define AO_SPI_SPEED_FAST       AO_SPI_SPEED_8MHz
36
37 /* Companion bus wants something no faster than 200kHz */
38
39 #define AO_SPI_SPEED_200kHz     AO_SPI_SPEED_125kHz
40
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
44
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
48
49 #define AO_SPI_CONFIG_3         0x08
50 #define AO_SPI_1_CONFIG_PE13_PE14_PE15  AO_SPI_CONFIG_3
51
52 #define AO_SPI_CONFIG_NONE      0x0c
53
54 #define AO_SPI_INDEX_MASK       0x01
55 #define AO_SPI_CONFIG_MASK      0x0c
56
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)
60
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)
63
64 #define AO_SPI_INDEX(id)        ((id) & AO_SPI_INDEX_MASK)
65 #define AO_SPI_CONFIG(id)       ((id) & AO_SPI_CONFIG_MASK)
66
67 void
68 ao_spi_get(uint8_t spi_index, uint32_t speed);
69
70 void
71 ao_spi_put(uint8_t spi_index);
72
73 void
74 ao_spi_send(void *block, uint16_t len, uint8_t spi_index);
75
76 void
77 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
78
79 void
80 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
81
82 void
83 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
84
85 extern uint16_t ao_spi_speed[STM_NUM_SPI];
86
87 void
88 ao_spi_init(void);
89
90 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
91                 ao_spi_get(bus, speed);                         \
92                 (reg)->bsrr = ((uint32_t) mask) << 16;  \
93         } while (0)
94
95 #define ao_spi_put_mask(reg,mask,bus) do {      \
96                 (reg)->bsrr = mask;             \
97                 ao_spi_put(bus);                \
98         } while (0)
99
100 #define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
101 #define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
102
103 #define ao_enable_port(port) do {                                       \
104                 if ((port) == &stm_gpioa)                               \
105                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN); \
106                 else if ((port) == &stm_gpiob)                          \
107                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN); \
108                 else if ((port) == &stm_gpioc)                          \
109                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN); \
110                 else if ((port) == &stm_gpiod)                          \
111                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN); \
112                 else if ((port) == &stm_gpioe)                          \
113                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOEEN); \
114         } while (0)
115
116
117 #define ao_gpio_set(port, bit, pin, v) stm_gpio_set(port, bit, v)
118
119 #define ao_gpio_get(port, bit, pin) stm_gpio_get(port, bit)
120
121 #define ao_enable_output(port,bit,pin,v) do {                   \
122                 ao_enable_port(port);                           \
123                 ao_gpio_set(port, bit, pin, v);                 \
124                 stm_moder_set(port, bit, STM_MODER_OUTPUT);\
125         } while (0)
126
127 #define ao_enable_input(port,bit,mode) do {                             \
128                 ao_enable_port(port);                                   \
129                 stm_moder_set(port, bit, STM_MODER_INPUT);              \
130                 if (mode == AO_EXTI_MODE_PULL_UP)                       \
131                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP);    \
132                 else if (mode == AO_EXTI_MODE_PULL_DOWN)                \
133                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN);  \
134                 else                                                    \
135                         stm_pupdr_set(port, bit, STM_PUPDR_NONE);       \
136         } while (0)
137
138 #define ao_enable_cs(port,bit) do {                             \
139                 stm_gpio_set((port), bit, 1);                   \
140                 stm_moder_set((port), bit, STM_MODER_OUTPUT);   \
141         } while (0)
142
143 #define ao_spi_init_cs(port, mask) do {                         \
144                 ao_enable_port(port);                           \
145                 if ((mask) & 0x0001) ao_enable_cs(port, 0);     \
146                 if ((mask) & 0x0002) ao_enable_cs(port, 1);     \
147                 if ((mask) & 0x0004) ao_enable_cs(port, 2);     \
148                 if ((mask) & 0x0008) ao_enable_cs(port, 3);     \
149                 if ((mask) & 0x0010) ao_enable_cs(port, 4);     \
150                 if ((mask) & 0x0020) ao_enable_cs(port, 5);     \
151                 if ((mask) & 0x0040) ao_enable_cs(port, 6);     \
152                 if ((mask) & 0x0080) ao_enable_cs(port, 7);     \
153                 if ((mask) & 0x0100) ao_enable_cs(port, 8);     \
154                 if ((mask) & 0x0200) ao_enable_cs(port, 9);     \
155                 if ((mask) & 0x0400) ao_enable_cs(port, 10);\
156                 if ((mask) & 0x0800) ao_enable_cs(port, 11);\
157                 if ((mask) & 0x1000) ao_enable_cs(port, 12);\
158                 if ((mask) & 0x2000) ao_enable_cs(port, 13);\
159                 if ((mask) & 0x4000) ao_enable_cs(port, 14);\
160                 if ((mask) & 0x8000) ao_enable_cs(port, 15);\
161         } while (0)
162
163 /* ao_dma_stm.c
164  */
165
166 extern uint8_t ao_dma_done[STM_NUM_DMA];
167
168 void
169 ao_dma_set_transfer(uint8_t             index,
170                     volatile void       *peripheral,
171                     void                *memory,
172                     uint16_t            count,
173                     uint32_t            ccr);
174
175 void
176 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
177
178 void
179 ao_dma_start(uint8_t index);
180
181 void
182 ao_dma_done_transfer(uint8_t index);
183
184 void
185 ao_dma_abort(uint8_t index);
186
187 void
188 ao_dma_alloc(uint8_t index);
189
190 void
191 ao_dma_init(void);
192
193 /* ao_i2c_stm.c */
194
195 void
196 ao_i2c_get(uint8_t i2c_index);
197
198 uint8_t
199 ao_i2c_start(uint8_t i2c_index, uint16_t address);
200
201 void
202 ao_i2c_put(uint8_t i2c_index);
203
204 uint8_t
205 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
206
207 uint8_t
208 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
209
210 void
211 ao_i2c_init(void);
212
213 /* ao_serial_stm.c */
214 struct ao_stm_usart {
215         struct ao_fifo          rx_fifo;
216         struct ao_fifo          tx_fifo;
217         struct stm_usart        *reg;
218         uint8_t                 tx_started;
219 };
220
221 #if HAS_SERIAL_1
222 extern struct ao_stm_usart      ao_stm_usart1;
223 #endif
224
225 #if HAS_SERIAL_2
226 extern struct ao_stm_usart      ao_stm_usart2;
227 #endif
228
229 #if HAS_SERIAL_3
230 extern struct ao_stm_usart      ao_stm_usart3;
231 #endif
232
233 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
234
235 static inline uint32_t
236 ao_arch_irqsave(void) {
237         uint32_t        primask;
238         asm("mrs %0,primask" : "=&r" (primask));
239         ao_arch_block_interrupts();
240         return primask;
241 }
242
243 static inline void
244 ao_arch_irqrestore(uint32_t primask) {
245         asm("msr primask,%0" : : "r" (primask));
246 }
247
248 static inline void
249 ao_arch_memory_barrier() {
250         asm volatile("" ::: "memory");
251 }
252
253 static inline void
254 ao_arch_init_stack(struct ao_task *task, void *start)
255 {
256         uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
257         uint32_t        a = (uint32_t) start;
258         int             i;
259
260         /* Return address (goes into LR) */
261         ARM_PUSH32(sp, a);
262
263         /* Clear register values r0-r12 */
264         i = 13;
265         while (i--)
266                 ARM_PUSH32(sp, 0);
267
268         /* APSR */
269         ARM_PUSH32(sp, 0);
270
271         /* PRIMASK with interrupts enabled */
272         ARM_PUSH32(sp, 0);
273
274         task->sp = sp;
275 }
276
277 static inline void ao_arch_save_regs(void) {
278         /* Save general registers */
279         asm("push {r0-r12,lr}\n");
280
281         /* Save APSR */
282         asm("mrs r0,apsr");
283         asm("push {r0}");
284
285         /* Save PRIMASK */
286         asm("mrs r0,primask");
287         asm("push {r0}");
288 }
289
290 static inline void ao_arch_save_stack(void) {
291         uint32_t        *sp;
292         asm("mov %0,sp" : "=&r" (sp) );
293         ao_cur_task->sp = (sp);
294         if ((uint8_t *) sp < &ao_cur_task->stack[0])
295                 ao_panic (AO_PANIC_STACK);
296 }
297
298 static inline void ao_arch_restore_stack(void) {
299         uint32_t        sp;
300         sp = (uint32_t) ao_cur_task->sp;
301
302         /* Switch stacks */
303         asm("mov sp, %0" : : "r" (sp) );
304
305         /* Restore PRIMASK */
306         asm("pop {r0}");
307         asm("msr primask,r0");
308
309         /* Restore APSR */
310         asm("pop {r0}");
311         asm("msr apsr,r0");
312
313         /* Restore general registers */
314         asm("pop {r0-r12,lr}\n");
315
316         /* Return to calling function */
317         asm("bx lr");
318 }
319
320 #define ao_arch_isr_stack()
321
322 #define ao_arch_wait_interrupt() do {                   \
323                 asm(".global ao_idle_loc\n\twfi\nao_idle_loc:");        \
324                 ao_arch_release_interrupts();                           \
325                 ao_arch_block_interrupts();                             \
326         } while (0)
327
328 #define ao_arch_critical(b) do {                                \
329                 ao_arch_block_interrupts();                     \
330                 do { b } while (0);                             \
331                 ao_arch_release_interrupts();                   \
332         } while (0)
333
334 #endif /* _AO_ARCH_FUNCS_H_ */