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