altos/stm32f1: Grab both TX/RX DMA mutexes while doing I2C
[fw/altos] / src / stm32f1 / ao_i2c_stm.c
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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20
21 struct ao_i2c_stm_info {
22         uint8_t tx_dma_index;
23         uint8_t rx_dma_index;
24         struct stm_i2c  *stm_i2c;
25 };
26
27 #define I2C_FAST        1
28
29 #define I2C_TIMEOUT     100
30
31 #define I2C_IDLE        0
32 #define I2C_RUNNING     1
33 #define I2C_ERROR       2
34
35 static uint8_t  ao_i2c_state[STM_NUM_I2C];
36 static uint16_t ao_i2c_addr[STM_NUM_I2C];
37 uint8_t         ao_i2c_mutex[STM_NUM_I2C];
38
39 # define I2C_HIGH_SLOW  5000    /* ns, 100kHz clock */
40 #ifdef TELEMEGA
41 # define I2C_HIGH_FAST  2000    /* ns, 167kHz clock */
42 #else
43 # define I2C_HIGH_FAST  1000    /* ns, 333kHz clock */
44 #endif
45
46 # define I2C_RISE_SLOW  500     /* ns */
47 # define I2C_RISE_FAST  100     /* ns */
48
49 /* Clock period in ns */
50 #define CYCLES(period)  (((period) * (AO_PCLK1 / 1000)) / 1000000)
51
52 #define max(a,b)        ((a) > (b) ? (a) : (b))
53 #define I2C_CCR_HIGH_SLOW       max(4,CYCLES(I2C_HIGH_SLOW))
54 #define I2C_CCR_HIGH_FAST       max(4,CYCLES(I2C_HIGH_FAST))
55 #define I2C_TRISE_SLOW          (CYCLES(I2C_RISE_SLOW) + 1)
56 #define I2C_TRISE_FAST          (CYCLES(I2C_RISE_FAST) + 1)
57
58 #if I2C_FAST
59 #define I2C_TRISE       I2C_TRISE_FAST
60 #define I2C_CCR_HIGH    I2C_CCR_HIGH_FAST
61 #else
62 #define I2C_TRISE       I2C_TRISE_SLOW
63 #define I2C_CCR_HIGH    I2C_CCR_HIGH_SLOW
64 #endif
65
66 #define AO_STM_I2C_CR2_FREQ     (AO_APB1CLK / 1000000)
67
68 #define AO_STM_I2C_CR1 ((0 << STM_I2C_CR1_SWRST) |      \
69                         (0 << STM_I2C_CR1_ALERT) |      \
70                         (0 << STM_I2C_CR1_PEC) |        \
71                         (0 << STM_I2C_CR1_POS) |        \
72                         (0 << STM_I2C_CR1_ACK) |        \
73                         (0 << STM_I2C_CR1_STOP) |       \
74                         (0 << STM_I2C_CR1_START) |      \
75                         (0 << STM_I2C_CR1_NOSTRETCH) |  \
76                         (0 << STM_I2C_CR1_ENGC) |       \
77                         (0 << STM_I2C_CR1_ENPEC) |      \
78                         (0 << STM_I2C_CR1_ENARP) |      \
79                         (0 << STM_I2C_CR1_SMBTYPE) |    \
80                         (0 << STM_I2C_CR1_SMBUS) |      \
81                         (1 << STM_I2C_CR1_PE))
82
83 #define AO_STM_I2C_CR2  ((0 << STM_I2C_CR2_LAST) |                      \
84                          (0 << STM_I2C_CR2_DMAEN) |                     \
85                          (0 << STM_I2C_CR2_ITBUFEN) |                   \
86                          (0 << STM_I2C_CR2_ITEVTEN) |                   \
87                          (0 << STM_I2C_CR2_ITERREN) |                   \
88                          (AO_STM_I2C_CR2_FREQ << STM_I2C_CR2_FREQ))
89
90 static const struct ao_i2c_stm_info     ao_i2c_stm_info[STM_NUM_I2C] = {
91         {
92                 .tx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C1_TX),
93                 .rx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C1_RX),
94                 .stm_i2c = &stm_i2c1
95         },
96         {
97                 .tx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C2_TX),
98                 .rx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C2_RX),
99                 .stm_i2c = &stm_i2c2
100         },
101 };
102
103 static uint8_t  *ao_i2c_recv_data[STM_NUM_I2C];
104 static uint16_t ao_i2c_recv_len[STM_NUM_I2C];
105 static uint16_t ev_count;
106
107 static void
108 ao_i2c_ev_isr(uint8_t index)
109 {
110         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
111         uint32_t        sr1;
112
113         ++ev_count;
114         sr1 = stm_i2c->sr1;
115         if (sr1 & (1 << STM_I2C_SR1_SB))
116                 stm_i2c->dr = ao_i2c_addr[index];
117         if (sr1 & (1 << STM_I2C_SR1_ADDR)) {
118                 stm_i2c->cr2 &= ~(1UL << STM_I2C_CR2_ITEVTEN);
119                 ao_i2c_state[index] = I2C_RUNNING;
120                 ao_wakeup(&ao_i2c_state[index]);
121         }
122         if (sr1 & (1 << STM_I2C_SR1_BTF)) {
123                 stm_i2c->cr2 &= ~(1UL << STM_I2C_CR2_ITEVTEN);
124                 ao_wakeup(&ao_i2c_state[index]);
125         }
126         if (sr1 & (1 << STM_I2C_SR1_RXNE)) {
127                 if (ao_i2c_recv_len[index]) {
128                         *(ao_i2c_recv_data[index]++) = (uint8_t) stm_i2c->dr;
129                         if (!--ao_i2c_recv_len[index])
130                                 ao_wakeup(&ao_i2c_recv_len[index]);
131                 }
132         }
133 }
134
135 void stm_i2c1_ev_isr(void) { ao_i2c_ev_isr(0); }
136 void stm_i2c2_ev_isr(void) { ao_i2c_ev_isr(1); }
137
138 static void
139 ao_i2c_er_isr(uint8_t index)
140 {
141         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
142         uint32_t        sr1;
143
144         sr1 = stm_i2c->sr1;
145         if (sr1 & (1 << STM_I2C_SR1_AF)) {
146                 ao_i2c_state[index] = I2C_ERROR;
147                 stm_i2c->sr1 = sr1 & ~(1UL << STM_I2C_SR1_AF);
148                 ao_wakeup(&ao_i2c_state[index]);
149         }
150 }
151
152 void stm_i2c1_er_isr(void) { ao_i2c_er_isr(0); }
153 void stm_i2c2_er_isr(void) { ao_i2c_er_isr(1); }
154
155 void
156 ao_i2c_get(uint8_t index)
157 {
158         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
159         ao_mutex_get(&ao_i2c_mutex[index]);
160
161         stm_i2c->sr1 = 0;
162         stm_i2c->sr2 = 0;
163 }
164
165 void
166 ao_i2c_put(uint8_t index)
167 {
168         ao_mutex_put(&ao_i2c_mutex[index]);
169 }
170
171 uint8_t
172 ao_i2c_start(uint8_t index, uint16_t addr)
173 {
174         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
175         int             t;
176
177         ao_i2c_state[index] = I2C_IDLE;
178         ao_i2c_addr[index] = addr;
179         stm_i2c->cr2 = AO_STM_I2C_CR2;
180         stm_i2c->cr1 = AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_START);
181         for (t = 0; t < I2C_TIMEOUT; t++) {
182                 if (!(stm_i2c->cr1 & (1 << STM_I2C_CR1_START)))
183                         break;
184         }
185         ao_arch_block_interrupts();
186         stm_i2c->cr2 = AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN);
187         ao_i2c_ev_isr(index);
188         while (ao_i2c_state[index] == I2C_IDLE)
189                 if (ao_sleep_for(&ao_i2c_state[index], AO_MS_TO_TICKS(250)))
190                         break;
191         ao_arch_release_interrupts();
192         return ao_i2c_state[index] == I2C_RUNNING;
193 }
194
195 static void
196 ao_i2c_wait_stop(uint8_t index)
197 {
198         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
199         int     t;
200
201         for (t = 0; t < I2C_TIMEOUT; t++) {
202                 if (!(stm_i2c->cr1 & (1 << STM_I2C_CR1_STOP)))
203                         break;
204                 ao_yield();
205         }
206         ao_i2c_state[index] = I2C_IDLE;
207 }
208
209 static void
210 ao_i2c_wait_addr(uint8_t index)
211 {
212         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
213         int     t;
214
215         for (t = 0; t < I2C_TIMEOUT; t++)
216                 if (!(stm_i2c->sr1 & (1 << STM_I2C_SR1_ADDR)))
217                         break;
218         if (t)
219                 printf ("wait_addr %d\n", t);
220 }
221
222 uint8_t
223 ao_i2c_send(void *block, uint16_t len, uint8_t index, uint8_t stop)
224 {
225         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
226         uint8_t         tx_dma_index = ao_i2c_stm_info[index].tx_dma_index;
227         uint8_t         rx_dma_index = ao_i2c_stm_info[index].rx_dma_index;
228
229         /* Clear any pending ADDR bit */
230         (void) stm_i2c->sr2;
231         ao_i2c_wait_addr(index);
232         ao_dma_set_transfer(tx_dma_index,
233                             &stm_i2c->dr,
234                             block,
235                             len,
236                             (0 << STM_DMA_CCR_MEM2MEM) |
237                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
238                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
239                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
240                             (1 << STM_DMA_CCR_MINC) |
241                             (0 << STM_DMA_CCR_PINC) |
242                             (0 << STM_DMA_CCR_CIRC) |
243                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
244         ao_dma_mutex_get(rx_dma_index);
245         stm_i2c->cr2 = AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_DMAEN);
246
247         ao_dma_start(tx_dma_index);
248         ao_arch_block_interrupts();
249         while (!ao_dma_done[tx_dma_index])
250                 if (ao_sleep_for(&ao_dma_done[tx_dma_index], 1 + len))
251                         break;
252         stm_i2c->cr2 = AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN);
253         while ((stm_i2c->sr1 & (1 << STM_I2C_SR1_BTF)) == 0)
254                 if (ao_sleep_for(&ao_i2c_state[index], 1 + len))
255                         break;
256         stm_i2c->cr2 = AO_STM_I2C_CR2;
257         ao_arch_release_interrupts();
258         if (stop) {
259                 stm_i2c->cr1 = AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP);
260                 ao_i2c_wait_stop(index);
261         }
262         ao_dma_mutex_put(rx_dma_index);
263         ao_dma_done_transfer(tx_dma_index);
264         return true;
265 }
266
267 static void
268 ao_i2c_recv_dma_isr(int index)
269 {
270         int             i;
271         struct stm_i2c  *stm_i2c = NULL;
272
273         for (i = 0; i < STM_NUM_I2C; i++)
274                 if (index == ao_i2c_stm_info[i].rx_dma_index) {
275                         stm_i2c = ao_i2c_stm_info[i].stm_i2c;
276                         break;
277                 }
278         if (!stm_i2c)
279                 return;
280         stm_i2c->cr2 = AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_LAST);
281         ao_dma_done[index] = 1;
282         ao_wakeup(&ao_dma_done[index]);
283 }
284
285 uint8_t
286 ao_i2c_recv(void *block, uint16_t len, uint8_t index, uint8_t stop)
287 {
288         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
289         uint8_t         ret = true;
290
291         if (len == 0)
292                 return true;
293         if (len == 1) {
294                 ao_i2c_recv_data[index] = block;
295                 ao_i2c_recv_len[index] = 1;
296                 stm_i2c->cr1 = AO_STM_I2C_CR1;
297
298                 /* Clear any pending ADDR bit */
299                 stm_i2c->sr2;
300                 ao_i2c_wait_addr(index);
301
302                 /* Enable interrupts to transfer the byte */
303                 stm_i2c->cr2 = (AO_STM_I2C_CR2 |
304                                 (1 << STM_I2C_CR2_ITEVTEN) |
305                                 (1 << STM_I2C_CR2_ITERREN) |
306                                 (1 << STM_I2C_CR2_ITBUFEN));
307                 if (stop)
308                         stm_i2c->cr1 = AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP);
309
310                 ao_arch_block_interrupts();
311                 while (ao_i2c_recv_len[index])
312                         if (ao_sleep_for(&ao_i2c_recv_len[index], 1))
313                                 break;
314                 ao_arch_release_interrupts();
315                 ret = ao_i2c_recv_len[index] == 0;
316         } else {
317                 uint8_t         tx_dma_index = ao_i2c_stm_info[index].tx_dma_index;
318                 uint8_t         rx_dma_index = ao_i2c_stm_info[index].rx_dma_index;
319                 ao_dma_mutex_get(tx_dma_index);
320                 ao_dma_set_transfer(rx_dma_index,
321                                     &stm_i2c->dr,
322                                     block,
323                                     len,
324                                     (0 << STM_DMA_CCR_MEM2MEM) |
325                                     (STM_DMA_CCR_PL_HIGH << STM_DMA_CCR_PL) |
326                                     (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
327                                     (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
328                                     (1 << STM_DMA_CCR_MINC) |
329                                     (0 << STM_DMA_CCR_PINC) |
330                                     (0 << STM_DMA_CCR_CIRC) |
331                                     (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
332
333                 /* XXX ao_i2c_recv_dma_isr hasn't ever been used, so it
334                  * doesn't appear to be necessary. Testing with a device
335                  * that uses i2c would really be useful here to discover
336                  * whether this function is necessary or not.
337                  */
338 #if 0
339                 ao_dma_set_isr(rx_dma_index, ao_i2c_recv_dma_isr);
340 #else
341                 (void) ao_i2c_recv_dma_isr;
342 #endif
343                 stm_i2c->cr1 = AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_ACK);
344                 stm_i2c->cr2 = AO_STM_I2C_CR2 |
345                         (1 << STM_I2C_CR2_DMAEN) | (1 << STM_I2C_CR2_LAST);
346                 /* Clear any pending ADDR bit */
347                 (void) stm_i2c->sr2;
348                 ao_i2c_wait_addr(index);
349
350                 ao_dma_start(rx_dma_index);
351                 ao_arch_block_interrupts();
352                 while (!ao_dma_done[rx_dma_index])
353                         if (ao_sleep_for(&ao_dma_done[rx_dma_index], len))
354                                 break;
355                 ao_arch_release_interrupts();
356                 ret = ao_dma_done[rx_dma_index];
357                 stm_i2c->cr1 = AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP);
358                 ao_dma_done_transfer(rx_dma_index);
359                 ao_dma_mutex_put(tx_dma_index);
360         }
361         if (stop)
362                 ao_i2c_wait_stop(index);
363         return ret;
364 }
365
366 static void
367 ao_i2c_channel_init(uint8_t index)
368 {
369         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
370         int i;
371
372         /* Turn I2C off while configuring */
373         stm_i2c->cr1 = (1 << STM_I2C_CR1_SWRST);
374         for (i = 0; i < 100; i++)
375                 asm("nop");
376         stm_i2c->cr1 = 0;
377         stm_i2c->cr2 = AO_STM_I2C_CR2;
378
379         (void) stm_i2c->sr1;
380         (void) stm_i2c->sr2;
381         (void) stm_i2c->dr;
382
383         stm_i2c->sr1 = 0;
384         stm_i2c->sr2 = 0;
385
386         stm_i2c->ccr = ((I2C_FAST << STM_I2C_CCR_FS) |
387                         (0 << STM_I2C_CCR_DUTY) |
388                         (I2C_CCR_HIGH << STM_I2C_CCR_CCR));
389
390         stm_i2c->trise = I2C_TRISE;
391
392         stm_i2c->cr1 = AO_STM_I2C_CR1;
393 }
394
395 static inline void
396 i2c_pin_set(struct stm_gpio *gpio, int pin)
397 {
398         ao_enable_port(gpio);
399         stm_gpio_conf(gpio, pin,
400                       STM_GPIO_CR_MODE_OUTPUT_2MHZ,
401                       STM_GPIO_CR_CNF_OUTPUT_AF_OPEN_DRAIN);
402 }
403
404 void
405 ao_i2c_init(void)
406 {
407 #if HAS_I2C_1
408 # if I2C_1_PB6_PB7
409         stm_set_afio_mapr(STM_AFIO_MAPR_I2C1_REMAP,
410                           STM_AFIO_MAPR_I2C1_REMAP_PB6_PB7,
411                           STM_AFIO_MAPR_I2C1_REMAP_MASK);
412         i2c_pin_set(&stm_gpiob, 6);
413         i2c_pin_set(&stm_gpiob, 7);
414 # else
415 #  if I2C_1_PB8_PB9
416         stm_set_afio_mapr(STM_AFIO_MAPR_I2C1_REMAP,
417                           STM_AFIO_MAPR_I2C1_REMAP_PB8_PB9,
418                           STM_AFIO_MAPR_I2C1_REMAP_MASK);
419         i2c_pin_set(&stm_gpiob, 8);
420         i2c_pin_set(&stm_gpiob, 9);
421 #  else
422 #   error "No I2C_1 port configuration specified"
423 #  endif
424 # endif
425
426         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_I2C1EN);
427         ao_i2c_channel_init(0);
428
429         stm_nvic_set_enable(STM_ISR_I2C1_EV_POS);
430         stm_nvic_set_priority(STM_ISR_I2C1_EV_POS, AO_STM_NVIC_MED_PRIORITY);
431         stm_nvic_set_enable(STM_ISR_I2C1_ER_POS);
432         stm_nvic_set_priority(STM_ISR_I2C1_ER_POS, AO_STM_NVIC_MED_PRIORITY);
433 #endif
434
435 #if HAS_I2C_2
436 # if I2C_2_PB10_PB11
437         i2c_pin_set(&stm_gpiob, 10);
438         i2c_pin_set(&stm_gpiob, 11);
439 # else
440 #  error "No I2C_2 port configuration specified"
441 # endif
442         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_I2C2EN);
443         ao_i2c_channel_init(1);
444
445         stm_nvic_set_enable(STM_ISR_I2C2_EV_POS);
446         stm_nvic_set_priority(STM_ISR_I2C2_EV_POS, AO_STM_NVIC_MED_PRIORITY);
447         stm_nvic_set_enable(STM_ISR_I2C2_ER_POS);
448         stm_nvic_set_priority(STM_ISR_I2C2_ER_POS, AO_STM_NVIC_MED_PRIORITY);
449 #endif
450 }