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 ;
@@ -261,17 +272,17 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
261
272
262
273
handle -> State = HAL_I2C_STATE_RESET ;
263
274
264
- if (master == 0 ) {
265
- HAL_NVIC_SetPriority (obj -> irq , 0 , 1 );
266
- HAL_NVIC_EnableIRQ (obj -> irq );
267
- #ifdef STM32F1xx
268
- HAL_NVIC_SetPriority (obj -> irqER , 0 , 1 );
269
- HAL_NVIC_EnableIRQ (obj -> irqER );
270
- #endif
271
- }
275
+ HAL_NVIC_SetPriority (obj -> irq , 0 , 1 );
276
+ HAL_NVIC_EnableIRQ (obj -> irq );
277
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
278
+ HAL_NVIC_SetPriority (obj -> irqER , 0 , 1 );
279
+ HAL_NVIC_EnableIRQ (obj -> irqER );
280
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
272
281
273
282
// Init the I2C
274
283
HAL_I2C_Init (handle );
284
+
285
+ obj -> isMaster = master ;
275
286
}
276
287
277
288
/**
@@ -282,9 +293,9 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
282
293
void i2c_deinit (i2c_t * obj )
283
294
{
284
295
HAL_NVIC_DisableIRQ (obj -> irq );
285
- #ifdef STM32F1xx
296
+ #if !defined( STM32F0xx ) && !defined( STM32L0xx )
286
297
HAL_NVIC_DisableIRQ (obj -> irqER );
287
- #endif
298
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
288
299
HAL_I2C_DeInit (& (obj -> handle ));
289
300
}
290
301
@@ -341,17 +352,20 @@ i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address,
341
352
342
353
{
343
354
i2c_status_e ret = I2C_ERROR ;
344
- HAL_StatusTypeDef status = HAL_OK ;
355
+ uint32_t tickstart = HAL_GetTick () ;
345
356
346
- // Check the communication status
347
- status = HAL_I2C_Master_Transmit (& (obj -> handle ), dev_address , data , size , I2C_TIMEOUT_TICK );
348
-
349
- if (status == HAL_OK )
357
+ if (HAL_I2C_Master_Transmit_IT (& (obj -> handle ), dev_address , data , size ) == HAL_OK ){
350
358
ret = I2C_OK ;
351
- else if (status == HAL_TIMEOUT )
352
- ret = I2C_TIMEOUT ;
353
- else
354
- ret = I2C_ERROR ;
359
+ // wait for transfer completion
360
+ while ((HAL_I2C_GetState (& (obj -> handle )) != HAL_I2C_STATE_READY )
361
+ && (ret == I2C_OK )){
362
+ if ((HAL_GetTick () - tickstart ) > I2C_TIMEOUT_TICK ) {
363
+ ret = I2C_TIMEOUT ;
364
+ } else if (HAL_I2C_GetError (& (obj -> handle )) != HAL_I2C_ERROR_NONE ) {
365
+ ret = I2C_ERROR ;
366
+ }
367
+ }
368
+ }
355
369
356
370
return ret ;
357
371
}
@@ -385,9 +399,19 @@ void i2c_slave_write_IT(i2c_t *obj, uint8_t *data, uint8_t size)
385
399
i2c_status_e i2c_master_read (i2c_t * obj , uint8_t dev_address , uint8_t * data , uint8_t size )
386
400
{
387
401
i2c_status_e ret = I2C_ERROR ;
402
+ uint32_t tickstart = HAL_GetTick ();
388
403
389
- if (HAL_I2C_Master_Receive (& (obj -> handle ), dev_address , data , size , I2C_TIMEOUT_TICK ) == HAL_OK ) {
404
+ if (HAL_I2C_Master_Receive_IT (& (obj -> handle ), dev_address , data , size ) == HAL_OK ) {
390
405
ret = I2C_OK ;
406
+ // wait for transfer completion
407
+ while ((HAL_I2C_GetState (& (obj -> handle )) != HAL_I2C_STATE_READY )
408
+ && (ret == I2C_OK )){
409
+ if ((HAL_GetTick () - tickstart ) > I2C_TIMEOUT_TICK ) {
410
+ ret = I2C_TIMEOUT ;
411
+ } else if (HAL_I2C_GetError (& (obj -> handle )) != HAL_I2C_ERROR_NONE ) {
412
+ ret = I2C_ERROR ;
413
+ }
414
+ }
391
415
}
392
416
393
417
return ret ;
@@ -506,15 +530,24 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
506
530
507
531
/**
508
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.
509
537
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
510
538
* the configuration information for the specified I2C.
511
539
* @retval None
512
540
*/
513
541
void HAL_I2C_ErrorCallback (I2C_HandleTypeDef * hi2c )
514
542
{
515
- 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
+ }
516
548
}
517
549
550
+ #if defined(I2C1_BASE )
518
551
/**
519
552
* @brief This function handles I2C1 interrupt.
520
553
* @param None
@@ -524,8 +557,12 @@ void I2C1_EV_IRQHandler(void)
524
557
{
525
558
I2C_HandleTypeDef * handle = i2c_handles [0 ];
526
559
HAL_I2C_EV_IRQHandler (handle );
560
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
561
+ HAL_I2C_ER_IRQHandler (handle );
562
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
527
563
}
528
564
565
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
529
566
/**
530
567
* @brief This function handles I2C1 interrupt.
531
568
* @param None
@@ -536,7 +573,10 @@ void I2C1_ER_IRQHandler(void)
536
573
I2C_HandleTypeDef * handle = i2c_handles [0 ];
537
574
HAL_I2C_ER_IRQHandler (handle );
538
575
}
576
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
577
+ #endif // I2C1_BASE
539
578
579
+ #if defined(I2C2_BASE )
540
580
/**
541
581
* @brief This function handles I2C2 interrupt.
542
582
* @param None
@@ -546,8 +586,12 @@ void I2C2_EV_IRQHandler(void)
546
586
{
547
587
I2C_HandleTypeDef * handle = i2c_handles [1 ];
548
588
HAL_I2C_EV_IRQHandler (handle );
589
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
590
+ HAL_I2C_ER_IRQHandler (handle );
591
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
549
592
}
550
593
594
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
551
595
/**
552
596
* @brief This function handles I2C2 interrupt.
553
597
* @param None
@@ -558,7 +602,10 @@ void I2C2_ER_IRQHandler(void)
558
602
I2C_HandleTypeDef * handle = i2c_handles [1 ];
559
603
HAL_I2C_ER_IRQHandler (handle );
560
604
}
605
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
606
+ #endif // I2C2_BASE
561
607
608
+ #if defined(I2C3_BASE )
562
609
/**
563
610
* @brief This function handles I2C3 interrupt.
564
611
* @param None
@@ -568,8 +615,12 @@ void I2C3_EV_IRQHandler(void)
568
615
{
569
616
I2C_HandleTypeDef * handle = i2c_handles [2 ];
570
617
HAL_I2C_EV_IRQHandler (handle );
618
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
619
+ HAL_I2C_ER_IRQHandler (handle );
620
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
571
621
}
572
622
623
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
573
624
/**
574
625
* @brief This function handles I2C3 interrupt.
575
626
* @param None
@@ -580,7 +631,10 @@ void I2C3_ER_IRQHandler(void)
580
631
I2C_HandleTypeDef * handle = i2c_handles [2 ];
581
632
HAL_I2C_ER_IRQHandler (handle );
582
633
}
634
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
635
+ #endif // I2C3_BASE
583
636
637
+ #if defined(I2C4_BASE )
584
638
/**
585
639
* @brief This function handles I2C4 interrupt.
586
640
* @param None
@@ -590,8 +644,12 @@ void I2C4_EV_IRQHandler(void)
590
644
{
591
645
I2C_HandleTypeDef * handle = i2c_handles [3 ];
592
646
HAL_I2C_EV_IRQHandler (handle );
647
+ #if defined(STM32F0xx ) || defined(STM32L0xx )
648
+ HAL_I2C_ER_IRQHandler (handle );
649
+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
593
650
}
594
651
652
+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
595
653
/**
596
654
* @brief This function handles I2C4 interrupt.
597
655
* @param None
@@ -602,6 +660,8 @@ void I2C4_ER_IRQHandler(void)
602
660
I2C_HandleTypeDef * handle = i2c_handles [3 ];
603
661
HAL_I2C_ER_IRQHandler (handle );
604
662
}
663
+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
664
+ #endif // I2C4_BASE
605
665
606
666
/**
607
667
* @}
0 commit comments