altos: stm i2c debug code was calling flush() even when disabled
[fw/altos] / src / stm / 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; 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 #include <ao.h>
19
20 struct ao_i2c_stm_info {
21         uint8_t tx_dma_index;
22         uint8_t rx_dma_index;
23         struct stm_i2c  *stm_i2c;
24 };
25
26 #define I2C_TIMEOUT     100
27
28 #define I2C_IDLE        0
29 #define I2C_RUNNING     1
30 #define I2C_ERROR       2
31
32 static uint8_t  ao_i2c_state[STM_NUM_I2C];
33 static uint16_t ao_i2c_addr[STM_NUM_I2C];
34 uint8_t         ao_i2c_mutex[STM_NUM_I2C];
35
36 #define AO_STM_I2C_CR1 ((0 << STM_I2C_CR1_SWRST) |      \
37                         (0 << STM_I2C_CR1_ALERT) |      \
38                         (0 << STM_I2C_CR1_PEC) |        \
39                         (0 << STM_I2C_CR1_POS) |        \
40                         (0 << STM_I2C_CR1_ACK) |        \
41                         (0 << STM_I2C_CR1_STOP) |       \
42                         (0 << STM_I2C_CR1_START) |      \
43                         (0 << STM_I2C_CR1_NOSTRETCH) |  \
44                         (0 << STM_I2C_CR1_ENGC) |       \
45                         (0 << STM_I2C_CR1_ENPEC) |      \
46                         (0 << STM_I2C_CR1_ENARP) |      \
47                         (0 << STM_I2C_CR1_SMBTYPE) |    \
48                         (0 << STM_I2C_CR1_SMBUS) |      \
49                         (1 << STM_I2C_CR1_PE))
50
51 #define AO_STM_I2C_CR2  ((0 << STM_I2C_CR2_LAST) |                      \
52                          (0 << STM_I2C_CR2_DMAEN) |                     \
53                          (0 << STM_I2C_CR2_ITBUFEN) |                   \
54                          (0 << STM_I2C_CR2_ITEVTEN) |                   \
55                          (0 << STM_I2C_CR2_ITERREN) |                   \
56                          (STM_I2C_CR2_FREQ_16_MHZ << STM_I2C_CR2_FREQ))
57
58 static const struct ao_i2c_stm_info     ao_i2c_stm_info[STM_NUM_I2C] = {
59         {
60                 .tx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C1_TX),
61                 .rx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C1_RX),
62                 .stm_i2c = &stm_i2c1
63         },
64         {
65                 .tx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C2_TX),
66                 .rx_dma_index = STM_DMA_INDEX(STM_DMA_CHANNEL_I2C2_RX),
67                 .stm_i2c = &stm_i2c2
68         },
69 };
70
71 static uint8_t  *ao_i2c_recv_data[STM_NUM_I2C];
72 static uint16_t ao_i2c_recv_len[STM_NUM_I2C];
73 static uint16_t ev_count;
74
75 static void
76 ao_i2c_ev_isr(uint8_t index)
77 {
78         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
79         uint32_t        sr1;
80
81         ++ev_count;
82         sr1 = stm_i2c->sr1;
83         if (sr1 & (1 << STM_I2C_SR1_SB))
84                 stm_i2c->dr = ao_i2c_addr[index];
85         if (sr1 & (1 << STM_I2C_SR1_ADDR)) {
86                 stm_i2c->cr2 &= ~(1 << STM_I2C_CR2_ITEVTEN);
87                 ao_i2c_state[index] = I2C_RUNNING;
88                 ao_wakeup(&ao_i2c_state[index]);
89         }
90         if (sr1 & (1 << STM_I2C_SR1_BTF)) {
91                 stm_i2c->cr2 &= ~(1 << STM_I2C_CR2_ITEVTEN);
92                 ao_wakeup(&ao_i2c_state[index]);
93         }
94         if (sr1 & (1 << STM_I2C_SR1_RXNE)) {
95                 if (ao_i2c_recv_len[index]) {                   
96                         *(ao_i2c_recv_data[index]++) = stm_i2c->dr;
97                         if (!--ao_i2c_recv_len[index])
98                                 ao_wakeup(&ao_i2c_recv_len[index]);
99                 }
100         }
101 }
102
103 void stm_i2c1_ev_isr(void) { ao_i2c_ev_isr(0); }
104 void stm_i2c2_ev_isr(void) { ao_i2c_ev_isr(1); }
105
106 static void
107 ao_i2c_er_isr(uint8_t index)
108 {
109         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
110         uint32_t        sr1;
111
112         sr1 = stm_i2c->sr1;
113         if (sr1 & (1 << STM_I2C_SR1_AF)) {
114                 ao_i2c_state[index] = I2C_ERROR;
115                 stm_i2c->sr1 = sr1 & ~(1 << STM_I2C_SR1_AF);
116                 ao_wakeup(&ao_i2c_state[index]);
117         }
118 }
119
120 void stm_i2c1_er_isr(void) { ao_i2c_er_isr(0); }
121 void stm_i2c2_er_isr(void) { ao_i2c_er_isr(1); }
122
123 void
124 ao_i2c_get(uint8_t index)
125 {
126         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
127         ao_mutex_get(&ao_i2c_mutex[index]);
128
129         stm_i2c->sr1 = 0;
130         stm_i2c->sr2 = 0;
131 }
132
133 void
134 ao_i2c_put(uint8_t index)
135 {
136         ao_mutex_put(&ao_i2c_mutex[index]);
137 }
138
139 static inline void
140 ao_i2c_delay(void)
141 {
142         uint8_t i;
143
144         for (i = 0; i < 10; i++)
145                 ao_arch_nop();
146 }
147
148 #define I2C_DEBUG       0
149 #if I2C_DEBUG
150 #define DBG(x...)       do { printf(x); flush(); } while (0)
151 #else
152 #define DBG(x...)
153 #endif
154
155 static inline uint32_t in_sr1(char *where, struct stm_i2c *stm_i2c) {
156         uint32_t        sr1 = stm_i2c->sr1;
157         DBG("%s: sr1: %x\n", where, sr1);
158         return sr1;
159 }
160
161 static inline uint32_t in_sr2(char *where, struct stm_i2c *stm_i2c) {
162         uint32_t        sr2 = stm_i2c->sr2;
163         DBG("%s: sr2: %x\n", where, sr2);
164         return sr2;
165 }
166
167 static inline void out_cr1(char *where, struct stm_i2c *stm_i2c, uint32_t cr1) {
168         DBG("%s: cr1: %x\n", where, cr1);
169         stm_i2c->cr1 = cr1;
170 }
171
172 static inline uint32_t in_cr1(char *where, struct stm_i2c *stm_i2c) {
173         uint32_t        cr1 = stm_i2c->cr1;
174         DBG("%s: cr1: %x\n", where, cr1);
175         return cr1;
176 }
177
178 static inline void out_cr2(char *where, struct stm_i2c *stm_i2c, uint32_t cr2) {
179         DBG("%s: cr2: %x\n", where, cr2);
180         stm_i2c->cr2 = cr2;
181 }
182
183 static inline uint32_t in_dr(char *where, struct stm_i2c *stm_i2c) {
184         uint32_t        dr = stm_i2c->dr;
185         DBG("%s: dr: %x\n", where, dr);
186         return dr;
187 }
188
189 static inline void out_dr(char *where, struct stm_i2c *stm_i2c, uint32_t dr) {
190         DBG("%s: dr: %x\n", where, dr);
191         stm_i2c->dr = dr;
192 }
193
194 uint8_t
195 ao_i2c_start(uint8_t index, uint16_t addr)
196 {
197         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
198         uint32_t        sr1, sr2;
199         int             t;
200
201         ao_i2c_state[index] = I2C_IDLE;
202         ao_i2c_addr[index] = addr;
203         out_cr2("start", stm_i2c, AO_STM_I2C_CR2);
204         out_cr1("start", stm_i2c,
205                 AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_START));
206         ao_i2c_delay();
207         out_cr2("start", stm_i2c,
208                 AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN));
209         ao_alarm(1);
210         cli();
211         while (ao_i2c_state[index] == I2C_IDLE)
212                 if (ao_sleep(&ao_i2c_state[index]))
213                         break;
214         sei();
215         ao_clear_alarm();
216         return ao_i2c_state[index] == I2C_RUNNING;
217 }
218
219 static void
220 ao_i2c_wait_stop(uint8_t index)
221 {
222         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
223         int     t;
224
225         for (t = 0; t < I2C_TIMEOUT; t++) {
226                 if (!(in_cr1("wait stop", stm_i2c) & (1 << STM_I2C_CR1_STOP)))
227                         break;
228                 ao_yield();
229         }
230         ao_i2c_state[index] = I2C_IDLE;
231 }
232
233 uint8_t
234 ao_i2c_send(void *block, uint16_t len, uint8_t index, uint8_t stop)
235 {
236         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
237         uint8_t         *b = block;
238         uint32_t        sr1;
239         uint8_t         tx_dma_index = ao_i2c_stm_info[index].tx_dma_index;
240
241         /* Clear any pending ADDR bit */
242         in_sr2("send clear addr", stm_i2c);
243         out_cr2("send", stm_i2c, AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_DMAEN));
244         ao_dma_set_transfer(tx_dma_index,
245                             &stm_i2c->dr,
246                             block,
247                             len,
248                             (0 << STM_DMA_CCR_MEM2MEM) |
249                             (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
250                             (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
251                             (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
252                             (1 << STM_DMA_CCR_MINC) |
253                             (0 << STM_DMA_CCR_PINC) |
254                             (0 << STM_DMA_CCR_CIRC) |
255                             (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR));
256                            
257         ao_dma_start(tx_dma_index);
258         ao_alarm(1 + len);
259         cli();
260         while (!ao_dma_done[tx_dma_index])
261                 if (ao_sleep(&ao_dma_done[tx_dma_index]))
262                         break;
263         ao_clear_alarm();
264         ao_dma_done_transfer(tx_dma_index);
265         out_cr2("send enable isr", stm_i2c,
266                 AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN));
267         while ((in_sr1("send_btf", stm_i2c) & (1 << STM_I2C_SR1_BTF)) == 0)
268                 if (ao_sleep(&ao_i2c_state[index]))
269                         break;
270         out_cr2("send disable isr", stm_i2c, AO_STM_I2C_CR2);
271         sei();
272         if (stop) {
273                 out_cr1("stop", stm_i2c, AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP));
274                 ao_i2c_wait_stop(index);
275         }
276         return TRUE;
277 }
278
279 void
280 ao_i2c_recv_dma_isr(int index)
281 {
282         int             i;
283         struct stm_i2c  *stm_i2c = NULL;
284
285         for (i = 0; i < STM_NUM_I2C; i++)
286                 if (index == ao_i2c_stm_info[i].rx_dma_index) {
287                         stm_i2c = ao_i2c_stm_info[i].stm_i2c;
288                         break;
289                 }
290         if (!stm_i2c)
291                 return;
292         stm_i2c->cr2 = AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_LAST);
293         ao_dma_done[index] = 1;
294         ao_wakeup(&ao_dma_done[index]);
295 }
296
297 uint8_t
298 ao_i2c_recv(void *block, uint16_t len, uint8_t index, uint8_t stop)
299 {
300         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
301         uint8_t         *b = block;
302         int             t;
303         uint8_t         ret = TRUE;
304
305         if (len == 0)
306                 return TRUE;
307         if (len == 1) {
308                 ao_i2c_recv_data[index] = block;
309                 ao_i2c_recv_len[index] = 1;
310                 out_cr1("setup recv 1", stm_i2c, AO_STM_I2C_CR1);
311
312                 /* Clear any pending ADDR bit */
313                 in_sr2("clear addr", stm_i2c);
314
315                 /* Enable interrupts to transfer the byte */
316                 out_cr2("setup recv 1", stm_i2c,
317                         AO_STM_I2C_CR2 |
318                         (1 << STM_I2C_CR2_ITEVTEN) |
319                         (1 << STM_I2C_CR2_ITERREN) |
320                         (1 << STM_I2C_CR2_ITBUFEN));
321                 if (stop)
322                         out_cr1("setup recv 1", stm_i2c, AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP));
323
324                 ao_alarm(1);
325                 cli();
326                 while (ao_i2c_recv_len[index])
327                         if (ao_sleep(&ao_i2c_recv_len[index]))
328                                 break;
329                 sei();
330                 ret = ao_i2c_recv_len[index] == 0;
331                 ao_clear_alarm();
332         } else {
333                 uint8_t         rx_dma_index = ao_i2c_stm_info[index].rx_dma_index;
334                 ao_dma_set_transfer(rx_dma_index,
335                                     &stm_i2c->dr,
336                                     block,
337                                     len,
338                                     (0 << STM_DMA_CCR_MEM2MEM) |
339                                     (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
340                                     (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) |
341                                     (STM_DMA_CCR_PSIZE_8 << STM_DMA_CCR_PSIZE) |
342                                     (1 << STM_DMA_CCR_MINC) |
343                                     (0 << STM_DMA_CCR_PINC) |
344                                     (0 << STM_DMA_CCR_CIRC) |
345                                     (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
346                 out_cr1("recv > 1", stm_i2c, AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_ACK));
347                 out_cr2("recv > 1", stm_i2c, AO_STM_I2C_CR2 |
348                         (1 << STM_I2C_CR2_DMAEN) | (1 << STM_I2C_CR2_LAST));
349                 /* Clear any pending ADDR bit */
350                 in_sr2("clear addr", stm_i2c);
351
352                 ao_dma_start(rx_dma_index);
353                 ao_alarm(len);
354                 cli();
355                 while (!ao_dma_done[rx_dma_index])
356                         if (ao_sleep(&ao_dma_done[rx_dma_index]))
357                                 break;
358                 sei();
359                 ao_clear_alarm();
360                 ret = ao_dma_done[rx_dma_index];
361                 ao_dma_done_transfer(rx_dma_index);
362                 out_cr1("stop recv > 1", stm_i2c, AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP));
363         }
364         if (stop)
365                 ao_i2c_wait_stop(index);
366         return ret;
367 }
368
369 void
370 ao_i2c_channel_init(uint8_t index)
371 {
372         struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
373         int i;
374
375         /* Turn I2C off while configuring */
376         stm_i2c->cr1 = (1 << STM_I2C_CR1_SWRST);
377         for (i = 0; i < 100; i++)
378                 asm("nop");
379         stm_i2c->cr1 = 0;
380         stm_i2c->cr2 = AO_STM_I2C_CR2;
381
382         (void) stm_i2c->sr1;
383         (void) stm_i2c->sr2;
384         (void) stm_i2c->dr;
385
386         stm_i2c->sr1 = 0;
387         stm_i2c->sr2 = 0;
388
389         stm_i2c->ccr = ((1 << STM_I2C_CCR_FS) |
390                         (0 << STM_I2C_CCR_DUTY) |
391                         (20 << STM_I2C_CCR_CCR));
392         
393
394         stm_i2c->cr1 = AO_STM_I2C_CR1;
395 }
396
397 void
398 ao_i2c_init(void)
399 {
400         /* All of the I2C configurations are on port B */
401         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
402 #if HAS_I2C_1
403 # if I2C_1_PB6_PB7
404         stm_afr_set(&stm_gpiob, 6, STM_AFR_AF4);
405         stm_afr_set(&stm_gpiob, 7, STM_AFR_AF4);
406 # else
407 #  if I2C_1_PB8_PB9
408         stm_afr_set(&stm_gpiob, 8, STM_AFR_AF4);
409         stm_afr_set(&stm_gpiob, 9, STM_AFR_AF4);
410 #  else
411 #   error "No I2C_1 port configuration specified"
412 #  endif
413 # endif
414
415         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_I2C1EN);
416         ao_i2c_channel_init(0);
417
418         stm_nvic_set_enable(STM_ISR_I2C1_EV_POS);
419         stm_nvic_set_priority(STM_ISR_I2C1_EV_POS, 3);
420         stm_nvic_set_enable(STM_ISR_I2C1_ER_POS);
421         stm_nvic_set_priority(STM_ISR_I2C1_ER_POS, 3);
422 #endif
423
424 #if HAS_I2C_2
425 # if I2C_2_PB10_PB11
426         stm_afr_set(&stm_gpiob, 10, STM_AFR_AF4);
427         stm_afr_set(&stm_gpiob, 11, STM_AFR_AF4);
428 # else
429 #  error "No I2C_2 port configuration specified"
430 # endif
431         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_I2C2EN);
432         ao_i2c_channel_init(1);
433
434         stm_nvic_set_enable(STM_ISR_I2C2_EV_POS);
435         stm_nvic_set_priority(STM_ISR_I2C2_EV_POS, 3);
436         stm_nvic_set_enable(STM_ISR_I2C2_ER_POS);
437         stm_nvic_set_priority(STM_ISR_I2C2_ER_POS, 3);
438 #endif
439 }