101
101
*/
102
102
103
103
/* Family specific description for I2C */
104
- #define I2C_NUM (5)
104
+ #if defined(STM32F7xx ) || defined(STM32L4xx )
105
+ #define I2C_NUM (4)
106
+ #elif defined(STM32F2xx ) || defined(STM32F3xx ) || defined(STM32F4xx ) || defined(STM32L0xx )
107
+ #define I2C_NUM (3)
108
+ #elif defined(STM32F0xx ) || defined(STM32F1xx ) || defined(STM32L1xx )
109
+ #define I2C_NUM (2)
110
+ #else
111
+ #error "Unknown Family - unknown I2C_NUM"
112
+ #endif
113
+
105
114
static I2C_HandleTypeDef * i2c_handles [I2C_NUM ];
106
115
107
116
/**
@@ -142,7 +151,6 @@ void i2c_init(i2c_t *obj)
142
151
*/
143
152
void i2c_custom_init (i2c_t * obj , i2c_timing_e timing , uint32_t addressingMode , uint32_t ownAddress , uint8_t master )
144
153
{
145
- UNUSED (master );
146
154
if (obj == NULL )
147
155
return ;
148
156
@@ -170,53 +178,56 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
170
178
#if defined I2C1_BASE
171
179
// Enable I2C1 clock if not done
172
180
if (obj -> i2c == I2C1 ) {
173
- __HAL_RCC_I2C1_CLK_ENABLE ();
174
- __HAL_RCC_I2C1_FORCE_RESET ();
175
- __HAL_RCC_I2C1_RELEASE_RESET ();
176
- obj -> irq = I2C1_EV_IRQn ;
177
- #ifdef STM32F1xx
178
- obj -> irqER = I2C1_ER_IRQn ;
179
- #endif
180
- i2c_handles [0 ] = handle ;
181
+ __HAL_RCC_I2C1_CLK_ENABLE ();
182
+ __HAL_RCC_I2C1_FORCE_RESET ();
183
+ __HAL_RCC_I2C1_RELEASE_RESET ();
184
+
185
+ obj -> irq = I2C1_EV_IRQn ;
186
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
187
+ obj -> irqER = I2C1_ER_IRQn ;
188
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
189
+ i2c_handles [0 ] = handle ;
181
190
}
182
- #endif
191
+ #endif // I2C1_BASE
183
192
#if defined I2C2_BASE
184
193
// Enable I2C2 clock if not done
185
194
if (obj -> i2c == I2C2 ) {
186
- __HAL_RCC_I2C2_CLK_ENABLE ();
187
- __HAL_RCC_I2C2_FORCE_RESET ();
188
- __HAL_RCC_I2C2_RELEASE_RESET ();
189
- #ifdef STM32F0xx
190
- obj -> irq = I2C2_IRQn ;
191
- #else
192
- obj -> irq = I2C2_EV_IRQn ;
193
- #ifdef STM32F1xx
194
- obj -> irqER = I2C2_ER_IRQn ;
195
- #endif
196
- #endif
197
- i2c_handles [1 ] = handle ;
195
+ __HAL_RCC_I2C2_CLK_ENABLE ();
196
+ __HAL_RCC_I2C2_FORCE_RESET ();
197
+ __HAL_RCC_I2C2_RELEASE_RESET ();
198
+ obj -> irq = I2C2_EV_IRQn ;
199
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
200
+ obj -> irqER = I2C2_ER_IRQn ;
201
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
202
+ i2c_handles [1 ] = handle ;
198
203
}
199
- #endif
204
+ #endif // I2C2_BASE
200
205
#if defined I2C3_BASE
201
206
// Enable I2C3 clock if not done
202
207
if (obj -> i2c == I2C3 ) {
203
- __HAL_RCC_I2C3_CLK_ENABLE ();
204
- __HAL_RCC_I2C3_FORCE_RESET ();
205
- __HAL_RCC_I2C3_RELEASE_RESET ();
206
- obj -> irq = I2C3_EV_IRQn ;
207
- i2c_handles [2 ] = handle ;
208
+ __HAL_RCC_I2C3_CLK_ENABLE ();
209
+ __HAL_RCC_I2C3_FORCE_RESET ();
210
+ __HAL_RCC_I2C3_RELEASE_RESET ();
211
+ obj -> irq = I2C3_EV_IRQn ;
212
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
213
+ obj -> irqER = I2C3_ER_IRQn ;
214
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
215
+ i2c_handles [2 ] = handle ;
208
216
}
209
- #endif
217
+ #endif // I2C3_BASE
210
218
#if defined I2C4_BASE
211
- // Enable I2C3 clock if not done
219
+ // Enable I2C4 clock if not done
212
220
if (obj -> i2c == I2C4 ) {
213
- __HAL_RCC_I2C4_CLK_ENABLE ();
214
- __HAL_RCC_I2C4_FORCE_RESET ();
215
- __HAL_RCC_I2C4_RELEASE_RESET ();
216
- obj -> irq = I2C4_EV_IRQn ;
217
- i2c_handles [3 ] = handle ;
221
+ __HAL_RCC_I2C4_CLK_ENABLE ();
222
+ __HAL_RCC_I2C4_FORCE_RESET ();
223
+ __HAL_RCC_I2C4_RELEASE_RESET ();
224
+ obj -> irq = I2C4_EV_IRQn ;
225
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
226
+ obj -> irqER = I2C4_ER_IRQn ;
227
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
228
+ i2c_handles [3 ] = handle ;
218
229
}
219
- #endif
230
+ #endif // I2C4_BASE
220
231
221
232
//SCL
222
233
port = set_GPIO_Port_Clock (STM_PORT (obj -> scl ));
@@ -250,7 +261,7 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
250
261
handle -> Init .Timing = timing ;
251
262
#else
252
263
handle -> Init .ClockSpeed = timing ;
253
- handle -> Init .DutyCycle = I2C_DUTYCYCLE_2 ;//16_9;
264
+ handle -> Init .DutyCycle = I2C_DUTYCYCLE_2 ;
254
265
#endif
255
266
handle -> Init .OwnAddress1 = ownAddress ;
256
267
handle -> Init .OwnAddress2 = 0xFF ;
@@ -263,13 +274,15 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
263
274
264
275
HAL_NVIC_SetPriority (obj -> irq , 0 , 1 );
265
276
HAL_NVIC_EnableIRQ (obj -> irq );
266
- #ifdef STM32F1xx
277
+ #if !defined( STM32F0xx ) && !defined( STM32L0xx )
267
278
HAL_NVIC_SetPriority (obj -> irqER , 0 , 1 );
268
279
HAL_NVIC_EnableIRQ (obj -> irqER );
269
- #endif
280
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
270
281
271
282
// Init the I2C
272
283
HAL_I2C_Init (handle );
284
+
285
+ obj -> isMaster = master ;
273
286
}
274
287
275
288
/**
@@ -280,9 +293,9 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
280
293
void i2c_deinit (i2c_t * obj )
281
294
{
282
295
HAL_NVIC_DisableIRQ (obj -> irq );
283
- #ifdef STM32F1xx
296
+ #if !defined( STM32F0xx ) && !defined( STM32L0xx )
284
297
HAL_NVIC_DisableIRQ (obj -> irqER );
285
- #endif
298
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
286
299
HAL_I2C_DeInit (& (obj -> handle ));
287
300
}
288
301
@@ -345,9 +358,11 @@ i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address,
345
358
ret = I2C_OK ;
346
359
// wait for transfer completion
347
360
while ((HAL_I2C_GetState (& (obj -> handle )) != HAL_I2C_STATE_READY )
348
- && (ret != I2C_TIMEOUT )){
361
+ && (ret == I2C_OK )){
349
362
if ((HAL_GetTick () - tickstart ) > I2C_TIMEOUT_TICK ) {
350
363
ret = I2C_TIMEOUT ;
364
+ } else if (HAL_I2C_GetError (& (obj -> handle )) != HAL_I2C_ERROR_NONE ) {
365
+ ret = I2C_ERROR ;
351
366
}
352
367
}
353
368
}
@@ -390,9 +405,11 @@ i2c_status_e i2c_master_read(i2c_t *obj, uint8_t dev_address, uint8_t *data, uin
390
405
ret = I2C_OK ;
391
406
// wait for transfer completion
392
407
while ((HAL_I2C_GetState (& (obj -> handle )) != HAL_I2C_STATE_READY )
393
- && (ret != I2C_TIMEOUT )){
408
+ && (ret == I2C_OK )){
394
409
if ((HAL_GetTick () - tickstart ) > I2C_TIMEOUT_TICK ) {
395
410
ret = I2C_TIMEOUT ;
411
+ } else if (HAL_I2C_GetError (& (obj -> handle )) != HAL_I2C_ERROR_NONE ) {
412
+ ret = I2C_ERROR ;
396
413
}
397
414
}
398
415
}
@@ -513,15 +530,24 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
513
530
514
531
/**
515
532
* @brief I2C error callback.
533
+ * @note In master mode, the callback is not used because the error is reported
534
+ * to the Arduino API from i2c_master_write() and i2c_master_read().
535
+ * In slave mode, there is no mechanism in Arduino API to report an error
536
+ * so the error callback forces the slave to listen again.
516
537
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
517
538
* the configuration information for the specified I2C.
518
539
* @retval None
519
540
*/
520
541
void HAL_I2C_ErrorCallback (I2C_HandleTypeDef * hi2c )
521
542
{
522
- HAL_I2C_EnableListen_IT (hi2c );
543
+ i2c_t * obj = get_i2c_obj (hi2c );
544
+
545
+ if (obj -> isMaster == 0 ) {
546
+ HAL_I2C_EnableListen_IT (hi2c );
547
+ }
523
548
}
524
549
550
+ #if defined(I2C1_BASE )
525
551
/**
526
552
* @brief This function handles I2C1 interrupt.
527
553
* @param None
@@ -531,8 +557,12 @@ void I2C1_EV_IRQHandler(void)
531
557
{
532
558
I2C_HandleTypeDef * handle = i2c_handles [0 ];
533
559
HAL_I2C_EV_IRQHandler (handle );
560
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
561
+ HAL_I2C_ER_IRQHandler (handle );
562
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
534
563
}
535
564
565
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
536
566
/**
537
567
* @brief This function handles I2C1 interrupt.
538
568
* @param None
@@ -543,7 +573,10 @@ void I2C1_ER_IRQHandler(void)
543
573
I2C_HandleTypeDef * handle = i2c_handles [0 ];
544
574
HAL_I2C_ER_IRQHandler (handle );
545
575
}
576
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
577
+ #endif // I2C1_BASE
546
578
579
+ #if defined(I2C2_BASE )
547
580
/**
548
581
* @brief This function handles I2C2 interrupt.
549
582
* @param None
@@ -553,8 +586,12 @@ void I2C2_EV_IRQHandler(void)
553
586
{
554
587
I2C_HandleTypeDef * handle = i2c_handles [1 ];
555
588
HAL_I2C_EV_IRQHandler (handle );
589
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
590
+ HAL_I2C_ER_IRQHandler (handle );
591
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
556
592
}
557
593
594
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
558
595
/**
559
596
* @brief This function handles I2C2 interrupt.
560
597
* @param None
@@ -565,7 +602,10 @@ void I2C2_ER_IRQHandler(void)
565
602
I2C_HandleTypeDef * handle = i2c_handles [1 ];
566
603
HAL_I2C_ER_IRQHandler (handle );
567
604
}
605
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
606
+ #endif // I2C2_BASE
568
607
608
+ #if defined(I2C3_BASE )
569
609
/**
570
610
* @brief This function handles I2C3 interrupt.
571
611
* @param None
@@ -575,8 +615,12 @@ void I2C3_EV_IRQHandler(void)
575
615
{
576
616
I2C_HandleTypeDef * handle = i2c_handles [2 ];
577
617
HAL_I2C_EV_IRQHandler (handle );
618
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
619
+ HAL_I2C_ER_IRQHandler (handle );
620
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
578
621
}
579
622
623
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
580
624
/**
581
625
* @brief This function handles I2C3 interrupt.
582
626
* @param None
@@ -587,7 +631,10 @@ void I2C3_ER_IRQHandler(void)
587
631
I2C_HandleTypeDef * handle = i2c_handles [2 ];
588
632
HAL_I2C_ER_IRQHandler (handle );
589
633
}
634
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
635
+ #endif // I2C3_BASE
590
636
637
+ #if defined(I2C4_BASE )
591
638
/**
592
639
* @brief This function handles I2C4 interrupt.
593
640
* @param None
@@ -597,8 +644,12 @@ void I2C4_EV_IRQHandler(void)
597
644
{
598
645
I2C_HandleTypeDef * handle = i2c_handles [3 ];
599
646
HAL_I2C_EV_IRQHandler (handle );
647
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
648
+ HAL_I2C_ER_IRQHandler (handle );
649
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
600
650
}
601
651
652
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
602
653
/**
603
654
* @brief This function handles I2C4 interrupt.
604
655
* @param None
@@ -609,6 +660,8 @@ void I2C4_ER_IRQHandler(void)
609
660
I2C_HandleTypeDef * handle = i2c_handles [3 ];
610
661
HAL_I2C_ER_IRQHandler (handle );
611
662
}
663
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
664
+ #endif // I2C4_BASE
612
665
613
666
/**
614
667
* @}
0 commit comments