altos: Add one-byte SPI output routine for LPC and STM cores
[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 uint8_t
68 ao_spi_try_get(uint8_t spi_index, uint32_t speed, uint8_t task_id);
69
70 void
71 ao_spi_get(uint8_t spi_index, uint32_t speed);
72
73 void
74 ao_spi_put(uint8_t spi_index);
75
76 void
77 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
78
79 void
80 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
81
82 void
83 ao_spi_send_sync(void *block, uint16_t len, uint8_t spi_index);
84
85 static inline void
86 ao_spi_send_byte(uint8_t byte, uint8_t spi_index)
87 {
88         struct stm_spi  *stm_spi;
89
90         switch (AO_SPI_INDEX(spi_index)) {
91         case 0:
92                 stm_spi = &stm_spi1;
93                 break;
94         case 1:
95                 stm_spi = &stm_spi2;
96                 break;
97         }
98
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));
105
106         /* Clear RXNE */
107         (void) stm_spi->dr;
108
109         while (!(stm_spi->sr & (1 << STM_SPI_SR_TXE)));
110         stm_spi->dr = byte;
111 }
112
113 void
114 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
115
116 void
117 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
118
119 extern uint16_t ao_spi_speed[STM_NUM_SPI];
120
121 void
122 ao_spi_init(void);
123
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))
126
127 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
128                 ao_spi_get(bus, speed);                         \
129                 ao_spi_set_cs(reg,mask);                        \
130         } while (0)
131
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)
134 {
135         if (!ao_spi_try_get(bus, speed, task_id))
136                 return 0;
137         ao_spi_set_cs(reg, mask);
138         return 1;
139 }
140
141 #define ao_spi_put_mask(reg,mask,bus) do {      \
142                 ao_spi_clr_cs(reg,mask);        \
143                 ao_spi_put(bus);                \
144         } while (0)
145
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)
148
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); \
160         } while (0)
161
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); \
173         } while (0)
174
175
176 #define ao_gpio_set(port, bit, pin, v) stm_gpio_set(port, bit, v)
177
178 #define ao_gpio_get(port, bit, pin) stm_gpio_get(port, bit)
179
180 #define ao_enable_output(port,bit,pin,v) do {                   \
181                 ao_enable_port(port);                           \
182                 ao_gpio_set(port, bit, pin, v);                 \
183                 stm_moder_set(port, bit, STM_MODER_OUTPUT);\
184         } while (0)
185
186 #define ao_gpio_set_mode(port,bit,mode) do {                            \
187                 if (mode == AO_EXTI_MODE_PULL_UP)                       \
188                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP);    \
189                 else if (mode == AO_EXTI_MODE_PULL_DOWN)                \
190                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN);  \
191                 else                                                    \
192                         stm_pupdr_set(port, bit, STM_PUPDR_NONE);       \
193         } while (0)
194         
195 #define ao_enable_input(port,bit,mode) do {                             \
196                 ao_enable_port(port);                                   \
197                 stm_moder_set(port, bit, STM_MODER_INPUT);              \
198                 ao_gpio_set_mode(port, bit, mode);                      \
199         } while (0)
200
201 #define ao_enable_cs(port,bit) do {                             \
202                 stm_gpio_set((port), bit, 1);                   \
203                 stm_moder_set((port), bit, STM_MODER_OUTPUT);   \
204         } while (0)
205
206 #define ao_spi_init_cs(port, mask) do {                         \
207                 ao_enable_port(port);                           \
208                 if ((mask) & 0x0001) ao_enable_cs(port, 0);     \
209                 if ((mask) & 0x0002) ao_enable_cs(port, 1);     \
210                 if ((mask) & 0x0004) ao_enable_cs(port, 2);     \
211                 if ((mask) & 0x0008) ao_enable_cs(port, 3);     \
212                 if ((mask) & 0x0010) ao_enable_cs(port, 4);     \
213                 if ((mask) & 0x0020) ao_enable_cs(port, 5);     \
214                 if ((mask) & 0x0040) ao_enable_cs(port, 6);     \
215                 if ((mask) & 0x0080) ao_enable_cs(port, 7);     \
216                 if ((mask) & 0x0100) ao_enable_cs(port, 8);     \
217                 if ((mask) & 0x0200) ao_enable_cs(port, 9);     \
218                 if ((mask) & 0x0400) ao_enable_cs(port, 10);\
219                 if ((mask) & 0x0800) ao_enable_cs(port, 11);\
220                 if ((mask) & 0x1000) ao_enable_cs(port, 12);\
221                 if ((mask) & 0x2000) ao_enable_cs(port, 13);\
222                 if ((mask) & 0x4000) ao_enable_cs(port, 14);\
223                 if ((mask) & 0x8000) ao_enable_cs(port, 15);\
224         } while (0)
225
226 /* ao_dma_stm.c
227  */
228
229 extern uint8_t ao_dma_done[STM_NUM_DMA];
230
231 void
232 ao_dma_set_transfer(uint8_t             index,
233                     volatile void       *peripheral,
234                     void                *memory,
235                     uint16_t            count,
236                     uint32_t            ccr);
237
238 void
239 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
240
241 void
242 ao_dma_start(uint8_t index);
243
244 void
245 ao_dma_done_transfer(uint8_t index);
246
247 void
248 ao_dma_abort(uint8_t index);
249
250 void
251 ao_dma_alloc(uint8_t index);
252
253 void
254 ao_dma_init(void);
255
256 /* ao_i2c_stm.c */
257
258 void
259 ao_i2c_get(uint8_t i2c_index);
260
261 uint8_t
262 ao_i2c_start(uint8_t i2c_index, uint16_t address);
263
264 void
265 ao_i2c_put(uint8_t i2c_index);
266
267 uint8_t
268 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
269
270 uint8_t
271 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
272
273 void
274 ao_i2c_init(void);
275
276 /* ao_serial_stm.c */
277 struct ao_stm_usart {
278         struct ao_fifo          rx_fifo;
279         struct ao_fifo          tx_fifo;
280         struct stm_usart        *reg;
281         uint8_t                 tx_started;
282 };
283
284 #if HAS_SERIAL_1
285 extern struct ao_stm_usart      ao_stm_usart1;
286 #endif
287
288 #if HAS_SERIAL_2
289 extern struct ao_stm_usart      ao_stm_usart2;
290 #endif
291
292 #if HAS_SERIAL_3
293 extern struct ao_stm_usart      ao_stm_usart3;
294 #endif
295
296 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
297
298 typedef uint32_t        ao_arch_irq_t;
299
300 static inline uint32_t
301 ao_arch_irqsave(void) {
302         uint32_t        primask;
303         asm("mrs %0,primask" : "=&r" (primask));
304         ao_arch_block_interrupts();
305         return primask;
306 }
307
308 static inline void
309 ao_arch_irqrestore(uint32_t primask) {
310         asm("msr primask,%0" : : "r" (primask));
311 }
312
313 static inline void
314 ao_arch_memory_barrier() {
315         asm volatile("" ::: "memory");
316 }
317
318 #if HAS_TASK
319 static inline void
320 ao_arch_init_stack(struct ao_task *task, void *start)
321 {
322         uint32_t        *sp = (uint32_t *) (task->stack + AO_STACK_SIZE);
323         uint32_t        a = (uint32_t) start;
324         int             i;
325
326         /* Return address (goes into LR) */
327         ARM_PUSH32(sp, a);
328
329         /* Clear register values r0-r12 */
330         i = 13;
331         while (i--)
332                 ARM_PUSH32(sp, 0);
333
334         /* APSR */
335         ARM_PUSH32(sp, 0);
336
337         /* PRIMASK with interrupts enabled */
338         ARM_PUSH32(sp, 0);
339
340         task->sp = sp;
341 }
342
343 static inline void ao_arch_save_regs(void) {
344         /* Save general registers */
345         asm("push {r0-r12,lr}\n");
346
347         /* Save APSR */
348         asm("mrs r0,apsr");
349         asm("push {r0}");
350
351         /* Save PRIMASK */
352         asm("mrs r0,primask");
353         asm("push {r0}");
354 }
355
356 static inline void ao_arch_save_stack(void) {
357         uint32_t        *sp;
358         asm("mov %0,sp" : "=&r" (sp) );
359         ao_cur_task->sp = (sp);
360         if ((uint8_t *) sp < &ao_cur_task->stack[0])
361                 ao_panic (AO_PANIC_STACK);
362 }
363
364 static inline void ao_arch_restore_stack(void) {
365         uint32_t        sp;
366         uint32_t        control;
367
368         asm("mrs %0,control" : "=&r" (control));
369         control |= (1 << 1);
370         asm("msr control,%0" : : "r" (control));
371         asm("isb");
372
373         sp = (uint32_t) ao_cur_task->sp;
374
375         /* Switch stacks */
376         asm("mov sp, %0" : : "r" (sp) );
377
378         /* Restore PRIMASK */
379         asm("pop {r0}");
380         asm("msr primask,r0");
381
382         /* Restore APSR */
383         asm("pop {r0}");
384         asm("msr apsr_nczvq,r0");
385
386         /* Restore general registers */
387         asm("pop {r0-r12,lr}\n");
388
389         /* Return to calling function */
390         asm("bx lr");
391 }
392
393 #ifndef HAS_SAMPLE_PROFILE
394 #define HAS_SAMPLE_PROFILE 0
395 #endif
396
397 #if DEBUG
398 #define HAS_ARCH_VALIDATE_CUR_STACK     1
399
400 static inline void
401 ao_validate_cur_stack(void)
402 {
403         uint8_t         *psp;
404
405         asm("mrs %0,psp" : "=&r" (psp));
406         if (ao_cur_task &&
407             psp <= ao_cur_task->stack &&
408             psp >= ao_cur_task->stack - 256)
409                 ao_panic(AO_PANIC_STACK);
410 }
411 #endif
412
413 #if !HAS_SAMPLE_PROFILE
414 #define HAS_ARCH_START_SCHEDULER        1
415
416 static inline void ao_arch_start_scheduler(void) {
417         uint32_t        sp;
418         uint32_t        control;
419
420         asm("mrs %0,msp" : "=&r" (sp));
421         asm("msr psp,%0" : : "r" (sp));
422         asm("mrs %0,control" : "=&r" (control));
423         control |= (1 << 1);
424         asm("msr control,%0" : : "r" (control));
425         asm("isb");
426 }
427 #endif
428
429 static inline void ao_arch_isr_stack(void) {
430         uint32_t        control;
431
432         asm("mrs %0,control" : "=&r" (control));
433         control &= ~(1 << 1);
434         asm("msr control,%0" : : "r" (control));
435         asm("isb");
436 }
437
438 #endif
439
440 #define ao_arch_wait_interrupt() do {                           \
441                 asm("\twfi\n");                                 \
442                 ao_arch_release_interrupts();                   \
443                 asm(".global ao_idle_loc\nao_idle_loc:");       \
444                 ao_arch_block_interrupts();                     \
445         } while (0)
446
447 #define ao_arch_critical(b) do {                        \
448                 uint32_t __mask = ao_arch_irqsave();    \
449                 do { b } while (0);                     \
450                 ao_arch_irqrestore(__mask);             \
451         } while (0)
452
453 #endif /* _AO_ARCH_FUNCS_H_ */