summaryrefslogtreecommitdiffstats
path: root/app/i2c_hw.c
blob: a17c56488c53d7c865292e6c9c7c72a46c03b1d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "project.h"


#define SCL1  GPIO6
#define SDA1  GPIO7
#define SCL1_PORT GPIOB
#define SDA1_PORT GPIOB

#define SCL2  GPIO10
#define SDA2  GPIO11
#define SCL2_PORT GPIOB
#define SDA2_PORT GPIOB

void
i2c_clear_start (uint32_t i2c)
{
  I2C_CR1 (i2c) &= ~ (uint32_t) I2C_CR1_START;
}


int
i2cp_start (uint32_t i2c)
{
  uint32_t timeout = 1000;
  i2c_send_start (i2c);

  while (! ((I2C_SR1 (i2c) & I2C_SR1_SB)
            & (I2C_SR2 (i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))) && (timeout--));

  return timeout ? 0 : -1;
}

void
i2cp_abort_start (uint32_t i2c)
{
  i2c_clear_start (i2c);
  delay_us (10);
}

void
i2cp_stop (uint32_t i2c)
{
  i2c_send_stop (i2c);
}


void
i2cp_abort_stop (uint32_t i2c)
{
  i2c_clear_stop (i2c);
  delay_us (10);
}

int
i2cp_send (uint32_t i2c, uint8_t v)
{
  uint32_t reg;
  uint32_t timeout = 1000;

  i2c_send_data (i2c, v);

  while (!
         ((reg =
             I2C_SR1 (i2c)) & (I2C_SR1_BTF | I2C_SR1_BERR | I2C_SR1_ARLO |
                               I2C_SR1_AF | I2C_SR1_PECERR | I2C_SR1_TIMEOUT |
                               I2C_SR1_SMBALERT)) && (timeout--));

  I2C_SR1 (i2c) =
    reg & ~ (I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_PECERR |
             I2C_SR1_TIMEOUT | I2C_SR1_SMBALERT);

  if (!timeout)
    return 1;


  return (reg & I2C_SR1_BTF) ? 0 : -1;
}

/*0 on success 1 on failure*/
int
i2cp_start_transaction (uint32_t i2c, uint8_t a, int wnr)
{
  uint32_t reg;
  uint32_t __attribute__ ((unused)) dummy;
  uint32_t timeout = 1000;

  i2c_send_start (i2c);

  while (! ((I2C_SR1 (i2c) & I2C_SR1_SB)
            & (I2C_SR2 (i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))) && (timeout--));

  if (!timeout)
    return -1;

  i2c_send_7bit_address (i2c, a, wnr);

  while (!
         ((reg =
             I2C_SR1 (i2c)) & (I2C_SR1_ADDR | I2C_SR1_BERR | I2C_SR1_ARLO |
                               I2C_SR1_AF | I2C_SR1_PECERR | I2C_SR1_TIMEOUT |
                               I2C_SR1_SMBALERT)) && (timeout--));

  if (!timeout)
    return -1;

  dummy = I2C_SR2 (i2c);

  I2C_SR1 (i2c) =
    reg & ~ (I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_PECERR |
             I2C_SR1_TIMEOUT | I2C_SR1_SMBALERT);


  return (reg == 0x82) ? 0 : -1;
}


/*This is stupid, it bit bangs a dummy transaction to get the host i2c controller back in the game */
void
i2cp1_reset_sm (void)
{
  int i;

  MAP_OUTPUT_PP (SCL1);
  MAP_OUTPUT_PP (SDA1);

  SET (SDA1);
  SET (SCL1);
  delay_us (10);
  CLEAR (SDA1);
  delay_us (10);
  CLEAR (SCL1);

  for (i = 0; i < 9; ++i) {
    delay_us (10);
    SET (SCL1);
    delay_us (10);
    CLEAR (SCL1);
    delay_us (10);
  }

  SET (SCL1);
  delay_us (10);
  SET (SDA1);
  delay_us (10);

  MAP_AF_OD (SCL1);
  MAP_AF_OD (SDA1);

}

void
i2cp2_reset_sm (void)
{
  int i;

  MAP_OUTPUT_PP (SCL2);
  MAP_OUTPUT_PP (SDA2);

  SET (SDA2);
  SET (SCL2);
  delay_us (10);
  CLEAR (SDA2);
  delay_us (10);
  CLEAR (SCL2);

  for (i = 0; i < 9; ++i) {
    delay_us (10);
    SET (SCL2);
    delay_us (10);
    CLEAR (SCL2);
    delay_us (10);
  }

  SET (SCL2);
  delay_us (10);
  SET (SDA2);
  delay_us (10);

  MAP_AF_OD (SCL2);
  MAP_AF_OD (SDA2);

}



void
i2cp1_start_dma (uint8_t *buf, int len)
{
  dma_channel_reset (DMA1, DMA_CHANNEL6);
  dma_set_peripheral_address (DMA1, DMA_CHANNEL6, (uint32_t) & I2C1_DR);
  dma_set_memory_address (DMA1, DMA_CHANNEL6, (uint32_t) buf);
  dma_set_number_of_data (DMA1, DMA_CHANNEL6, len);
  dma_set_read_from_memory (DMA1, DMA_CHANNEL6);
  dma_enable_memory_increment_mode (DMA1, DMA_CHANNEL6);
  dma_set_peripheral_size (DMA1, DMA_CHANNEL6, DMA_CCR_PSIZE_8BIT);
  dma_set_memory_size (DMA1, DMA_CHANNEL6, DMA_CCR_MSIZE_8BIT);
  dma_set_priority (DMA1, DMA_CHANNEL6, DMA_CCR_PL_MEDIUM);
  dma_enable_transfer_complete_interrupt (DMA1, DMA_CHANNEL6);
  dma_enable_transfer_error_interrupt (DMA1, DMA_CHANNEL6);
  dma_enable_channel (DMA1, DMA_CHANNEL6);

  i2c_enable_dma (I2C1);
}


void
i2cp2_start_dma (uint8_t *buf, int len)
{
  dma_channel_reset (DMA1, DMA_CHANNEL4);
  dma_set_peripheral_address (DMA1, DMA_CHANNEL4, (uint32_t) & I2C2_DR);
  dma_set_memory_address (DMA1, DMA_CHANNEL4, (uint32_t) buf);
  dma_set_number_of_data (DMA1, DMA_CHANNEL4, len);
  dma_set_read_from_memory (DMA1, DMA_CHANNEL4);
  dma_enable_memory_increment_mode (DMA1, DMA_CHANNEL4);
  dma_set_peripheral_size (DMA1, DMA_CHANNEL4, DMA_CCR_PSIZE_8BIT);
  dma_set_memory_size (DMA1, DMA_CHANNEL4, DMA_CCR_MSIZE_8BIT);
  dma_set_priority (DMA1, DMA_CHANNEL4, DMA_CCR_PL_MEDIUM);
  dma_enable_transfer_complete_interrupt (DMA1, DMA_CHANNEL4);
  dma_enable_transfer_error_interrupt (DMA1, DMA_CHANNEL4);
  dma_enable_channel (DMA1, DMA_CHANNEL4);

  i2c_enable_dma (I2C2);
}


int
i2cp1_dma_in_progress (void)
{
  return ! (DMA1_ISR & (DMA_ISR_TCIF6 | DMA_ISR_TEIF6));
}

int
i2cp2_dma_in_progress (void)
{
  return ! (DMA1_ISR & (DMA_ISR_TCIF4 | DMA_ISR_TEIF4));
}

void
i2cp1_stop_dma (void)
{
  DMA1_IFCR |= DMA_IFCR_CTCIF6 | DMA_IFCR_CTEIF6 | DMA_IFCR_CGIF6;

  dma_disable_transfer_complete_interrupt (DMA1, DMA_CHANNEL6);
  dma_disable_transfer_error_interrupt (DMA1, DMA_CHANNEL6);

  i2c_disable_dma (I2C1);
  dma_disable_channel (DMA1, DMA_CHANNEL6);
}

void
i2cp2_stop_dma (void)
{
  DMA1_IFCR |= DMA_IFCR_CTCIF4 | DMA_IFCR_CTEIF4 | DMA_IFCR_CGIF4;

  dma_disable_transfer_complete_interrupt (DMA1, DMA_CHANNEL4);
  dma_disable_transfer_error_interrupt (DMA1, DMA_CHANNEL4);

  i2c_disable_dma (I2C2);
  dma_disable_channel (DMA1, DMA_CHANNEL4);
}


void
i2cp_reset (uint32_t i2c)
{
  while (i2c_lock());

  i2cp_start (i2c);
  i2cp_abort_start (i2c);
  i2cp_stop (i2c);
  i2cp_abort_stop (i2c);

  i2cp_start (i2c);
  i2cp_abort_start (i2c);
  i2cp_stop (i2c);
  i2cp_abort_stop (i2c);
  i2c_unlock();
}



void
i2cp_scan (uint32_t i2c)
{
  int i, r;

  i2cp_reset (i2c);

  printf ("Probing bus\n");

  for (i = 0; i < 128; ++i) {
    printf ("%d\n", i);

    while (i2c_lock());

    i2cp_start (i2c);
    i2cp_abort_start (i2c);
    i2cp_stop (i2c);
    i2cp_abort_stop (i2c);
    delay_ms (10);
    r = i2cp_start_transaction (i2c, i, I2C_WRITE);
    i2cp_stop (i2c);
    i2c_unlock();

    if (!r)
      printf ("Found device at address 0x%x\n", i);

    usart1_drain();
  }

  printf ("Done\n");
  i2cp_reset (i2c);

}

static void
i2cp_bringup (uint32_t i2c)
{
  i2c_peripheral_enable (i2c);
  i2c_clear_start (i2c);
  i2c_clear_stop (i2c);
  i2c_reset (i2c);
  i2c_peripheral_enable (i2c);

  I2C_CR1 (i2c) |= I2C_CR1_SWRST;
  delay_us (1000);
  I2C_CR1 (i2c) &= ~I2C_CR1_SWRST;


  if (i2c == I2C1)
    i2cp1_reset_sm();
  else
    i2cp2_reset_sm();


  i2c_set_clock_frequency (i2c, I2C_CR2_FREQ_36MHZ);
  i2c_set_standard_mode (i2c);
  i2c_set_ccr (i2c, 0x80);      /* t_high=t_high=CCR * Tpclk1 */
  i2c_set_trise (i2c, 0x0b);

  i2c_set_own_7bit_slave_address (i2c, 0x00);

  i2c_peripheral_enable (i2c);
}


void
i2cp_init (void)
{
  rcc_periph_clock_enable (RCC_I2C1);
  rcc_periph_clock_enable (RCC_I2C2);
  rcc_periph_clock_enable (RCC_DMA1);

  while (i2c_lock());

  i2cp_bringup (I2C1);
  i2cp_bringup (I2C2);

  i2c_unlock();
}