altos: Provide ISR-based code paths for SPI
[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 extern uint8_t  ao_spi_mutex[STM_NUM_SPI];
24
25 void
26 ao_spi_get(uint8_t spi_index);
27
28 void
29 ao_spi_put(uint8_t spi_index);
30
31 void
32 ao_spi_send(void *block, uint16_t len, uint8_t spi_index);
33
34 void
35 ao_spi_queue_send(void *block, uint16_t len, uint8_t spi_index,
36                   void (*callback)(int spi_index));
37
38 void
39 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t spi_index);
40
41 void
42 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
43
44 void
45 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t spi_index);
46
47 void
48 ao_spi_init(void);
49
50 #define ao_spi_get_mask(reg,mask,bus) do {              \
51                 ao_spi_get(bus);                        \
52                 (reg).bsrr = ((uint32_t) mask) << 16;   \
53         } while (0)
54
55 #define ao_spi_put_mask(reg,mask,bus) do {      \
56                 (reg).bsrr = mask;              \
57                 ao_spi_put(bus);                \
58         } while (0)
59
60 #define ao_enable_port(port) do {                                       \
61                 if (&(port) == &stm_gpioa)                              \
62                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN); \
63                 else if (&(port) == &stm_gpiob)                         \
64                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN); \
65                 else if (&(port) == &stm_gpioc)                         \
66                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN); \
67                 else if (&(port) == &stm_gpiod)                         \
68                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN); \
69                 else if (&(port) == &stm_gpioe)                         \
70                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOEEN); \
71         } while (0)
72
73
74 #define ao_enable_cs(port,bit) do {                             \
75                 stm_gpio_set(&(port), bit, 1);                  \
76                 stm_moder_set(&(port), bit, STM_MODER_OUTPUT);  \
77         } while (0)
78
79 #define ao_spi_init_cs(port, mask) do {                         \
80                 ao_enable_port(port);                           \
81                 if ((mask) & 0x0001) ao_enable_cs(port, 0);     \
82                 if ((mask) & 0x0002) ao_enable_cs(port, 1);     \
83                 if ((mask) & 0x0004) ao_enable_cs(port, 2);     \
84                 if ((mask) & 0x0008) ao_enable_cs(port, 3);     \
85                 if ((mask) & 0x0010) ao_enable_cs(port, 4);     \
86                 if ((mask) & 0x0020) ao_enable_cs(port, 5);     \
87                 if ((mask) & 0x0040) ao_enable_cs(port, 6);     \
88                 if ((mask) & 0x0080) ao_enable_cs(port, 7);     \
89                 if ((mask) & 0x0100) ao_enable_cs(port, 8);     \
90                 if ((mask) & 0x0200) ao_enable_cs(port, 9);     \
91                 if ((mask) & 0x0400) ao_enable_cs(port, 10);\
92                 if ((mask) & 0x0800) ao_enable_cs(port, 11);\
93                 if ((mask) & 0x1000) ao_enable_cs(port, 12);\
94                 if ((mask) & 0x2000) ao_enable_cs(port, 13);\
95                 if ((mask) & 0x4000) ao_enable_cs(port, 14);\
96                 if ((mask) & 0x8000) ao_enable_cs(port, 15);\
97         } while (0)
98
99 /* ao_dma_stm.c
100  */
101
102 extern uint8_t ao_dma_done[STM_NUM_DMA];
103
104 void
105 ao_dma_set_transfer(uint8_t             index,
106                     volatile void       *peripheral,
107                     void                *memory,
108                     uint16_t            count,
109                     uint32_t            ccr);
110
111 void
112 ao_dma_set_isr(uint8_t index, void (*isr)(int arg), int arg);
113
114 void
115 ao_dma_start(uint8_t index);
116
117 void
118 ao_dma_done_transfer(uint8_t index);
119
120 void
121 ao_dma_abort(uint8_t index);
122
123 void
124 ao_dma_alloc(uint8_t index);
125
126 void
127 ao_dma_init(void);
128
129 /* ao_i2c_stm.c */
130
131 void
132 ao_i2c_get(uint8_t i2c_index);
133
134 uint8_t
135 ao_i2c_start(uint8_t i2c_index, uint16_t address);
136
137 void
138 ao_i2c_put(uint8_t i2c_index);
139
140 uint8_t
141 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
142
143 uint8_t
144 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
145
146 void
147 ao_i2c_init(void);
148
149 #endif /* _AO_ARCH_FUNCS_H_ */