Merge remote-tracking branch 'mjb/master'
[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 #define AO_SPI_SPEED_FAST       STM_SPI_CR1_BR_PCLK_4
26 #define AO_SPI_SPEED_1MHz       STM_SPI_CR1_BR_PCLK_16
27 #define AO_SPI_SPEED_200kHz     STM_SPI_CR1_BR_PCLK_256
28
29 void
30 ao_spi_get(uint8_t spi_index, uint32_t speed);
31
32 void
33 ao_spi_put(uint8_t spi_index);
34
35 void
36 ao_spi_send(void *block, uint16_t len, uint8_t 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 extern uint16_t ao_spi_speed[STM_NUM_SPI];
48
49 void
50 ao_spi_init(void);
51
52 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
53                 ao_spi_get(bus, speed);                         \
54                 (reg)->bsrr = ((uint32_t) mask) << 16;  \
55         } while (0)
56
57 #define ao_spi_put_mask(reg,mask,bus) do {      \
58                 (reg)->bsrr = mask;             \
59                 ao_spi_put(bus);                \
60         } while (0)
61
62 #define ao_spi_get_bit(reg,bit,pin,bus,speed) ao_spi_get_mask(reg,(1<<bit),bus,speed)
63 #define ao_spi_put_bit(reg,bit,pin,bus) ao_spi_put_mask(reg,(1<<bit),bus)
64
65 #define ao_enable_port(port) do {                                       \
66                 if ((port) == &stm_gpioa)                               \
67                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN); \
68                 else if ((port) == &stm_gpiob)                          \
69                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN); \
70                 else if ((port) == &stm_gpioc)                          \
71                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN); \
72                 else if ((port) == &stm_gpiod)                          \
73                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN); \
74                 else if ((port) == &stm_gpioe)                          \
75                         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOEEN); \
76         } while (0)
77
78
79 #define ao_gpio_set(port, bit, pin, v) stm_gpio_set(port, bit, v)
80
81 #define ao_gpio_get(port, bit, pin) stm_gpio_get(port, bit)
82
83 #define ao_enable_output(port,bit,pin,v) do {                   \
84                 ao_enable_port(port);                           \
85                 ao_gpio_set(port, bit, pin, v);                 \
86                 stm_moder_set(port, bit, STM_MODER_OUTPUT);\
87         } while (0)
88
89 #define ao_enable_input(port,bit,mode) do {                             \
90                 ao_enable_port(port);                                   \
91                 stm_moder_set(port, bit, STM_MODER_INPUT);              \
92                 if (mode == AO_EXTI_MODE_PULL_UP)                       \
93                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_UP);    \
94                 else if (mode == AO_EXTI_MODE_PULL_DOWN)                \
95                         stm_pupdr_set(port, bit, STM_PUPDR_PULL_DOWN);  \
96                 else                                                    \
97                         stm_pupdr_set(port, bit, STM_PUPDR_NONE);       \
98         } while (0)
99
100 #define ao_enable_cs(port,bit) do {                             \
101                 stm_gpio_set((port), bit, 1);                   \
102                 stm_moder_set((port), bit, STM_MODER_OUTPUT);   \
103         } while (0)
104
105 #define ao_spi_init_cs(port, mask) do {                         \
106                 ao_enable_port(port);                           \
107                 if ((mask) & 0x0001) ao_enable_cs(port, 0);     \
108                 if ((mask) & 0x0002) ao_enable_cs(port, 1);     \
109                 if ((mask) & 0x0004) ao_enable_cs(port, 2);     \
110                 if ((mask) & 0x0008) ao_enable_cs(port, 3);     \
111                 if ((mask) & 0x0010) ao_enable_cs(port, 4);     \
112                 if ((mask) & 0x0020) ao_enable_cs(port, 5);     \
113                 if ((mask) & 0x0040) ao_enable_cs(port, 6);     \
114                 if ((mask) & 0x0080) ao_enable_cs(port, 7);     \
115                 if ((mask) & 0x0100) ao_enable_cs(port, 8);     \
116                 if ((mask) & 0x0200) ao_enable_cs(port, 9);     \
117                 if ((mask) & 0x0400) ao_enable_cs(port, 10);\
118                 if ((mask) & 0x0800) ao_enable_cs(port, 11);\
119                 if ((mask) & 0x1000) ao_enable_cs(port, 12);\
120                 if ((mask) & 0x2000) ao_enable_cs(port, 13);\
121                 if ((mask) & 0x4000) ao_enable_cs(port, 14);\
122                 if ((mask) & 0x8000) ao_enable_cs(port, 15);\
123         } while (0)
124
125 /* ao_dma_stm.c
126  */
127
128 extern uint8_t ao_dma_done[STM_NUM_DMA];
129
130 void
131 ao_dma_set_transfer(uint8_t             index,
132                     volatile void       *peripheral,
133                     void                *memory,
134                     uint16_t            count,
135                     uint32_t            ccr);
136
137 void
138 ao_dma_set_isr(uint8_t index, void (*isr)(int index));
139
140 void
141 ao_dma_start(uint8_t index);
142
143 void
144 ao_dma_done_transfer(uint8_t index);
145
146 void
147 ao_dma_abort(uint8_t index);
148
149 void
150 ao_dma_alloc(uint8_t index);
151
152 void
153 ao_dma_init(void);
154
155 /* ao_i2c_stm.c */
156
157 void
158 ao_i2c_get(uint8_t i2c_index);
159
160 uint8_t
161 ao_i2c_start(uint8_t i2c_index, uint16_t address);
162
163 void
164 ao_i2c_put(uint8_t i2c_index);
165
166 uint8_t
167 ao_i2c_send(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
168
169 uint8_t
170 ao_i2c_recv(void *block, uint16_t len, uint8_t i2c_index, uint8_t stop);
171
172 void
173 ao_i2c_init(void);
174
175 #endif /* _AO_ARCH_FUNCS_H_ */