2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 struct ao_i2c_stm_info {
23 struct stm_i2c *stm_i2c;
30 static uint8_t ao_i2c_state[STM_NUM_I2C];
31 static uint16_t ao_i2c_addr[STM_NUM_I2C];
32 uint8_t ao_i2c_mutex[STM_NUM_I2C];
34 #define AO_STM_I2C_CR1 ((0 << STM_I2C_CR1_SWRST) | \
35 (0 << STM_I2C_CR1_ALERT) | \
36 (0 << STM_I2C_CR1_PEC) | \
37 (0 << STM_I2C_CR1_POS) | \
38 (0 << STM_I2C_CR1_ACK) | \
39 (0 << STM_I2C_CR1_STOP) | \
40 (0 << STM_I2C_CR1_START) | \
41 (0 << STM_I2C_CR1_NOSTRETCH) | \
42 (0 << STM_I2C_CR1_ENGC) | \
43 (0 << STM_I2C_CR1_ENPEC) | \
44 (0 << STM_I2C_CR1_ENARP) | \
45 (0 << STM_I2C_CR1_SMBTYPE) | \
46 (0 << STM_I2C_CR1_SMBUS) | \
47 (1 << STM_I2C_CR1_PE))
49 #define AO_STM_I2C_CR2 ((0 << STM_I2C_CR2_LAST) | \
50 (1 << STM_I2C_CR2_DMAEN) | \
51 (0 << STM_I2C_CR2_ITBUFEN) | \
52 (0 << STM_I2C_CR2_ITEVTEN) | \
53 (0 << STM_I2C_CR2_ITERREN) | \
54 (STM_I2C_CR2_FREQ_16_MHZ << STM_I2C_CR2_FREQ))
56 static const struct ao_i2c_stm_info ao_i2c_stm_info[STM_NUM_I2C] = {
58 .tx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C1_TX),
59 .rx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C1_RX),
63 .tx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C2_TX),
64 .rx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C2_RX),
70 ao_i2c_ev_isr(uint8_t index)
72 struct stm_i2c *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
76 if (sr1 & (1 << STM_I2C_SR1_SB))
77 stm_i2c->dr = ao_i2c_addr[index];
78 if (sr1 & (1 << STM_I2C_SR1_ADDR)) {
80 ao_i2c_state[index] = I2C_RUNNING;
81 ao_wakeup(&ao_i2c_state[index]);
85 void stm_i2c1_ev_isr(void) { ao_i2c_ev_isr(0); }
86 void stm_i2c2_ev_isr(void) { ao_i2c_ev_isr(1); }
89 ao_i2c_er_isr(uint8_t index)
91 struct stm_i2c *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
95 if (sr1 & (1 << STM_I2C_SR1_AF)) {
96 ao_i2c_state[index] = I2C_ERROR;
97 stm_i2c->sr1 = sr1 & ~(1 << STM_I2C_SR1_AF);
98 ao_wakeup(&ao_i2c_state[index]);
102 void stm_i2c1_er_isr(void) { ao_i2c_er_isr(0); }
103 void stm_i2c2_er_isr(void) { ao_i2c_er_isr(1); }
106 ao_i2c_get(uint8_t index)
108 struct stm_i2c *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
109 ao_mutex_get(&ao_i2c_mutex[index]);
116 ao_i2c_put(uint8_t index)
118 ao_mutex_put(&ao_i2c_mutex[index]);
122 ao_i2c_start(uint8_t index, uint16_t addr)
124 struct stm_i2c *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
126 ao_i2c_state[index] = I2C_IDLE;
127 ao_i2c_addr[index] = addr;
128 stm_i2c->cr2 = AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN);
129 stm_i2c->cr1 = AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_START);
131 while (ao_i2c_state[index] == I2C_IDLE)
132 ao_sleep(&ao_i2c_state[index]);
134 return ao_i2c_state[index] == I2C_RUNNING;
138 ao_i2c_send(void *block, uint16_t len, uint8_t index)
140 struct stm_i2c *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
141 uint8_t tx_dma_index = ao_i2c_stm_info[index].tx_dma_index;
143 ao_dma_set_transfer(tx_dma_index,
147 (0 << STM_DMA_CCR_MEM2MEM) |
148 (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
149 (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
150 (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
151 (1 << STM_DMA_CCR_MINC) |
152 (0 << STM_DMA_CCR_PINC) |
153 (0 << STM_DMA_CCR_CIRC) |
154 (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
156 ao_dma_start(tx_dma_index);
158 while (!ao_dma_done[tx_dma_index])
159 ao_sleep(&ao_dma_done[tx_dma_index]);
161 ao_dma_done_transfer(tx_dma_index);
165 ao_i2c_channel_init(uint8_t index)
167 struct stm_i2c *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
169 /* Turn I2C off while configuring */
171 stm_i2c->cr2 = AO_STM_I2C_CR2;
180 stm_i2c->ccr = ((1 << STM_I2C_CCR_FS) |
181 (0 << STM_I2C_CCR_DUTY) |
182 (20 << STM_I2C_CCR_CCR));
184 stm_i2c->cr1 = AO_STM_I2C_CR1;
190 /* All of the I2C configurations are on port B */
191 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
194 stm_afr_set(&stm_gpiob, 6, STM_AFR_AF4);
195 stm_afr_set(&stm_gpiob, 7, STM_AFR_AF4);
198 stm_afr_set(&stm_gpiob, 8, STM_AFR_AF4);
199 stm_afr_set(&stm_gpiob, 9, STM_AFR_AF4);
201 # error "No I2C_1 port configuration specified"
205 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_I2C1EN);
206 ao_i2c_channel_init(0);
208 stm_nvic_set_enable(STM_ISR_I2C1_EV_POS);
209 stm_nvic_set_priority(STM_ISR_I2C1_EV_POS, 3);
210 stm_nvic_set_enable(STM_ISR_I2C1_ER_POS);
211 stm_nvic_set_priority(STM_ISR_I2C1_ER_POS, 3);
216 stm_afr_set(&stm_gpiob, 10, STM_AFR_AF4);
217 stm_afr_set(&stm_gpiob, 11, STM_AFR_AF4);
219 # error "No I2C_2 port configuration specified"
221 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_I2C2EN);
222 ao_i2c_channel_init(1);
224 stm_nvic_set_enable(STM_ISR_I2C2_EV_POS);
225 stm_nvic_set_priority(STM_ISR_I2C2_EV_POS, 3);
226 stm_nvic_set_enable(STM_ISR_I2C2_ER_POS);
227 stm_nvic_set_priority(STM_ISR_I2C2_ER_POS, 3);