GlobLib
HAL and API libraries for MCUs and hardware.
stm32f103cb_i2c.c
Go to the documentation of this file.
1 /*!**************************************************************************
2  @file stm32f103cb_i2c.c
3  @brief Source file for stm32f103cb I2C
4  @author Stuart Ianna
5  @version 0.1
6  @date June 2018
7  @copyright GNU GPLv3
8  @warning None
9  @bug
10 
11  @details
12 
13  @par Compilers
14  - arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
15 ******************************************************************************/
16 
17 #include "stm32f103cb_i2c.h"
18 
19 /****************************************************************************/
20 // I2C1
21 /****************************************************************************/
22 
23 //I2C1 routines
24 static void i2c1_setup(void);
25 static mcu_error i2c1_write(uint8_t byte);
26 static uint8_t i2c1_read(void);
27 static uint8_t i2c1_repeatRead(void);
28 static mcu_error i2c1_start(void);
29 static mcu_error i2c1_stop(void);
30 static mcu_error i2c1_address(uint8_t address, uint8_t operation);
31 
32 /****************************************************************************/
33 // I2C2
34 /****************************************************************************/
35 
36 static void i2c2_setup(void);
37 static mcu_error i2c2_write(uint8_t byte);
38 static uint8_t i2c2_read(void);
39 static uint8_t i2c2_repeatRead(void);
40 static mcu_error i2c2_start(void);
41 static mcu_error i2c2_stop(void);
42 static mcu_error i2c2_address(uint8_t address, uint8_t operation);
43 
44 
45 /****************************************************************************/
46 // SETUP FUNCTIONS
47 /****************************************************************************/
48 
50 
51  switch(peripheral){
52 
53  case I2C_1:
54 
55  //Setup the port
56  i2c1_setup();
57  break;
58 
59  case I2C_2:
60 
61  //Setup the port
62  i2c2_setup();
63  break;
64 
65  default:
66 
67  if(MCU_debugIsEnabled()){
69  }
70  return E_I2C_PORT;
71  break;
72  }
73 
74  return E_I2C_NOERROR;
75 }
76 
77 void i2c1_setup(void){
78 
79  //Setup the GPIO pins
82 
83  //Setup clock
84  rcc_periph_clock_enable(RCC_AFIO);
85  rcc_periph_clock_enable(RCC_I2C1);
86 
87  //Disable port
88  i2c_peripheral_disable(I2C1);
89 
90 
91  //The frequency of the APB bus needs to be entered
92  I2C_CR2(I2C1) |= (rcc_apb2_frequency/1000000);
93 
94  //Set fast mode
95  i2c_set_fast_mode(I2C1);
96 
97  //Clock speed should be 400kHz -> 2.5us divided by 25 = 100ns
98  //CCR register should count until 900ns
99  i2c_set_ccr(I2C1,(uint8_t)(900/(10000000000.0f/(float)rcc_apb2_frequency)));
100 
101  //Set duty cycle to one
102  I2C_CCR(I2C1) |= I2C_CCR_DUTY;
103 
104  //Set rise time
105  //For 400kHz rise time is 300ns so
106  i2c_set_trise(I2C1, (uint8_t)(300/(1000000000.0f/(float)rcc_apb2_frequency)));
107 
108  //Set own address
109  i2c_set_own_7bit_slave_address(I2C1, I2C_MY_ADDRESS);
110 
111  //Enable
112  i2c_peripheral_enable(I2C1);
113 }
114 
115 void i2c2_setup(void){
116 
117  //Setup the GPIO pins
120 
121  //Setup clock
122  rcc_periph_clock_enable(RCC_AFIO);
123  rcc_periph_clock_enable(RCC_I2C2);
124 
125  //Disable port
126  i2c_peripheral_disable(I2C2);
127 
128  //The frequency of the APB bus needs to be entered
129  I2C_CR2(I2C2) |= (rcc_apb2_frequency/1000000);
130 
131  //Set fast mode
132  i2c_set_fast_mode(I2C2);
133 
134  //Clock speed should be 400kHz -> 2.5us divided by 25 = 100ns
135  //CCR register should count until 900ns
136  i2c_set_ccr(I2C2,(uint8_t)(900/(10000000000.0f/(float)rcc_apb2_frequency)));
137 
138  //Set duty cycle to one
139  I2C_CCR(I2C2) |= I2C_CCR_DUTY;
140 
141  //Set rise time
142  //For 400kHz rise time is 300ns so
143  i2c_set_trise(I2C2, (uint8_t)(300/(1000000000.0f/(float)rcc_apb2_frequency)));
144 
145  //Set own address
146  i2c_set_own_7bit_slave_address(I2C2, I2C_MY_ADDRESS);
147 
148  //Enable
149  i2c_peripheral_enable(I2C2);
150 }
151 
152 /****************************************************************************/
153 // WRITE FUNCTIONS
154 /****************************************************************************/
155 
156 mcu_error I2C_write(i2c_periph peripheral, uint8_t byte){
157 
158  switch(peripheral){
159 
160  case I2C_1:
161 
162  //Write a byte to the bus
163  i2c1_write(byte);
164  break;
165 
166  case I2C_2:
167 
168  //Write a byte to the bus
169  i2c2_write(byte);
170  break;
171 
172  default:
173 
174  if(MCU_debugIsEnabled()){
176  }
177  return E_I2C_PORT;
178  break;
179  }
180 
181  return E_I2C_NOERROR;
182 }
183 
184 mcu_error i2c1_write(uint8_t byte){
185 
186  uint32_t timeoutCounter = 0;
187 
188  i2c_send_data(I2C1, byte);
189  while (!(I2C_SR1(I2C1) & (I2C_SR1_BTF | I2C_SR1_TxE))){
190 
191  timeoutCounter++;
192  if(timeoutCounter >= I2C_TIMEOUT){
193 
194  if(MCU_debugIsEnabled()){
196  }
197  return E_I2C_WRITE;
198  }
199  }
200 
201  return E_I2C_NOERROR;
202 }
203 
204 mcu_error i2c2_write(uint8_t byte){
205 
206  uint32_t timeoutCounter = 0;
207 
208  i2c_send_data(I2C2, byte);
209  while (!(I2C_SR1(I2C2) & (I2C_SR1_BTF | I2C_SR1_TxE))){
210 
211  timeoutCounter++;
212  if(timeoutCounter >= I2C_TIMEOUT){
213 
214  if(MCU_debugIsEnabled()){
216  }
217  return E_I2C_WRITE;
218  }
219  }
220 
221  return E_I2C_NOERROR;
222 }
223 
224 /****************************************************************************/
225 // READ FUNCTIONS
226 /****************************************************************************/
228 
229  uint8_t byte;
230 
231  switch(peripheral){
232 
233  case I2C_1:
234 
235  //read a byte
236  byte = i2c1_read();
237  break;
238 
239  case I2C_2:
240 
241  //Read a byte
242  byte = i2c2_read();
243  break;
244 
245  default:
246  byte = E_I2C_PORT;
247 
248  if(MCU_debugIsEnabled()){
250  }
251  break;
252  }
253 
254  return byte;
255 }
256 uint8_t i2c1_read(void){
257 
258  uint32_t timeoutCounter = 0;
259 
260  I2C_CR1(I2C1) &= ~I2C_CR1_ACK;
261 
262  //Wait for byte to arrive
263  while (!(I2C_SR1(I2C1) & I2C_SR1_RxNE)){
264 
265  timeoutCounter++;
266  if(timeoutCounter >= I2C_TIMEOUT){
267 
268  if(MCU_debugIsEnabled()){
270  }
271  return E_I2C_READ;
272  }
273 
274  }
275 
276  return I2C_DR(I2C1);
277 }
278 
279 uint8_t i2c2_read(void){
280 
281  uint32_t timeoutCounter = 0;
282 
283  I2C_CR1(I2C2) &= ~I2C_CR1_ACK;
284 
285  //Wait for byte to arrive
286  while (!(I2C_SR1(I2C2) & I2C_SR1_RxNE)){
287 
288  timeoutCounter++;
289  if(timeoutCounter >= I2C_TIMEOUT){
290 
291  if(MCU_debugIsEnabled()){
293  }
294  return E_I2C_READ;
295  }
296 
297  }
298 
299  return I2C_DR(I2C2);
300 }
301 
302 uint8_t I2C_repeatRead(i2c_periph peripheral){
303 
304  uint8_t byte;
305 
306  switch(peripheral){
307 
308  case I2C_1:
309 
310  //read a byte
311  byte = i2c1_repeatRead();
312  break;
313 
314  case I2C_2:
315 
316  //Read a byte
317  byte = i2c2_repeatRead();
318  break;
319 
320  default:
321  byte = E_I2C_PORT;
322 
323  if(MCU_debugIsEnabled()){
325  }
326  break;
327  }
328 
329  return byte;
330 }
331 
332 uint8_t i2c1_repeatRead(void){
333 
334  uint32_t timeoutCounter = 0;
335 
336  I2C_CR1(I2C1) |= I2C_CR1_ACK;
337 
338  //Wait for byte to arrive
339  while (!(I2C_SR1(I2C1) & I2C_SR1_RxNE)){
340 
341  timeoutCounter++;
342  if(timeoutCounter >= I2C_TIMEOUT){
343 
344  if(MCU_debugIsEnabled()){
346  }
347  return E_I2C_READ;
348  }
349 
350  }
351 
352  return I2C_DR(I2C1);
353 }
354 
355 uint8_t i2c2_repeatRead(void){
356 
357  uint32_t timeoutCounter = 0;
358 
359  I2C_CR1(I2C2) |= I2C_CR1_ACK;
360 
361  //Wait for byte to arrive
362  while (!(I2C_SR1(I2C2) & I2C_SR1_RxNE)){
363 
364  timeoutCounter++;
365  if(timeoutCounter >= I2C_TIMEOUT){
366 
367  if(MCU_debugIsEnabled()){
369  }
370  return E_I2C_READ;
371  }
372 
373  }
374 
375  return I2C_DR(I2C2);
376 }
377 
378 /****************************************************************************/
379 // START FUNCTIONS
380 /****************************************************************************/
382 
383  switch(peripheral){
384 
385  case I2C_1:
386 
387  //Generate start condition
388  i2c1_start();
389  break;
390 
391  case I2C_2:
392 
393  //Generate start condition
394  i2c2_start();
395  break;
396 
397  default:
398 
399  if(MCU_debugIsEnabled()){
401  }
402  return E_I2C_PORT;
403  break;
404  }
405 
406  return E_I2C_NOERROR;
407 }
408 
409 mcu_error i2c1_start(void){
410 
411  uint32_t timeoutCounter = 0;
412 
413  //Set the start bit
414  i2c_send_start(I2C1);
415 
416  while (!((I2C_SR1(I2C1) & I2C_SR1_SB)& (I2C_SR2(I2C1) & (I2C_SR2_MSL | I2C_SR2_BUSY)))){
417 
418  timeoutCounter++;
419  if(timeoutCounter >= I2C_TIMEOUT){
420 
421  if(MCU_debugIsEnabled()){
423  }
424  return E_I2C_START;
425  }
426 
427  }
428 
429  return E_I2C_NOERROR;
430 }
431 
432 mcu_error i2c2_start(void){
433 
434  uint32_t timeoutCounter = 0;
435 
436  //Set the start bit
437  i2c_send_start(I2C2);
438 
439  while (!((I2C_SR1(I2C2) & I2C_SR1_SB)& (I2C_SR2(I2C2) & (I2C_SR2_MSL | I2C_SR2_BUSY)))){
440 
441  timeoutCounter++;
442  if(timeoutCounter >= I2C_TIMEOUT){
443 
444  if(MCU_debugIsEnabled()){
446  }
447  return E_I2C_START;
448  }
449 
450  }
451 
452  return E_I2C_NOERROR;
453 }
454 
455 /****************************************************************************/
456 // STOP FUNCTIONS
457 /****************************************************************************/
458 
460 
461  switch(peripheral){
462 
463  case I2C_1:
464 
465  //Generate start condition
466  i2c1_stop();
467  break;
468 
469  case I2C_2:
470 
471  //Generate start condition
472  i2c2_stop();
473  break;
474 
475  default:
476 
477  if(MCU_debugIsEnabled()){
479  }
480  return E_I2C_PORT;
481  break;
482  }
483 
484  return E_I2C_NOERROR;
485 }
486 
487 mcu_error i2c1_stop(void){
488 
489  i2c_send_stop(I2C1);
490  return E_I2C_NOERROR;
491 }
492 
493 mcu_error i2c2_stop(void){
494 
495  i2c_send_stop(I2C2);
496  return E_I2C_NOERROR;
497 }
498 
499 /****************************************************************************/
500 // ADDRESS FUNCTIONS
501 /****************************************************************************/
502 
503 mcu_error I2C_address(i2c_periph peripheral, uint8_t address, uint8_t operation){
504 
505  switch(peripheral){
506 
507  case I2C_1:
508 
509  //Send address
510  i2c1_address(address, operation);
511  break;
512 
513  case I2C_2:
514 
515  //Send address
516  i2c2_address(address, operation);
517  break;
518 
519  default:
520 
521  if(MCU_debugIsEnabled()){
523  }
524  return E_I2C_PORT;
525  break;
526  }
527 
528  return E_I2C_NOERROR;
529 }
530 
531 mcu_error i2c1_address(uint8_t address, uint8_t operation){
532 
533  uint32_t timeoutCounter = 0;
534 
535  i2c_send_7bit_address(I2C1, address, operation);
536 
537  while (!(I2C_SR1(I2C1) & I2C_SR1_ADDR)){
538 
539  timeoutCounter++;
540  if(timeoutCounter >= I2C_TIMEOUT){
541 
542  if(MCU_debugIsEnabled()){
544  }
545  return E_I2C_STOP;
546  }
547 
548  }
549 
550  //Read status register to clear flag
551  address = I2C_SR2(I2C1);
552 
553  return E_I2C_NOERROR;
554 
555 }
556 
557 mcu_error i2c2_address(uint8_t address, uint8_t operation){
558 
559  uint32_t timeoutCounter = 0;
560 
561  i2c_send_7bit_address(I2C2, address, operation);
562 
563  while (!(I2C_SR1(I2C2) & I2C_SR1_ADDR)){
564 
565  timeoutCounter++;
566  if(timeoutCounter >= I2C_TIMEOUT){
567 
568  if(MCU_debugIsEnabled()){
570  }
571  return E_I2C_STOP;
572  }
573 
574  }
575 
576  //Read status register to clear flag
577  address = I2C_SR2(I2C2);
578 
579  return E_I2C_NOERROR;
580 
581 }
#define I2C_MY_ADDRESS
The address of this device.
mcu_error I2C_address(i2c_periph peripheral, uint8_t address, uint8_t operation)
Send a seven bit address on the bus.
Header file for stm32f103cb I2C.
ERROR I2C: Port doesn't exist.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
ERROR I2C: No Error.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
mcu_error I2C_setup(i2c_periph peripheral)
Initialize I2C data structure and setup port.
#define I2C_TIMEOUT
The number of loop cycles to wait when polling peripheral registers.
mcu_error I2C_stop(i2c_periph peripheral)
Generate a stop condition on the bus.
Second I2C port.
ERROR I2C: Timeout during get()
mcu_error I2C_read(i2c_periph peripheral)
Read one byte from the bus without acknowledge generation.
ERROR I2C: Timeout during stop()
ERROR I2C: Timeout during start()
ERROR I2C: Timeout during put()
Pin 7 of the port.
i2c_periph
I2C ports available on the MCU.
Pin 6 of the port.
First I2C port.
Port B of the MCU.
mcu_error pinSetup(gpio_mode mode, gpio_port port, gpio_pin pin)
Setup a GPIO pin for a given function.
Pin 11 of the port.
mcu_error I2C_start(i2c_periph peripheral)
Generate a start condition on the bus.
Pin 10 of the port.
uint8_t I2C_repeatRead(i2c_periph peripheral)
Read one byte from the bus with acknowledge generation.
mcu_error I2C_write(i2c_periph peripheral, uint8_t byte)
Send one byte on the bus.
Setup the port for I2C.
mcu_error
Error enumerators for the Debug peripheral.