101101 */
102102
103103/* 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+
105114static I2C_HandleTypeDef * i2c_handles [I2C_NUM ];
106115
107116/**
@@ -142,7 +151,6 @@ void i2c_init(i2c_t *obj)
142151 */
143152void i2c_custom_init (i2c_t * obj , i2c_timing_e timing , uint32_t addressingMode , uint32_t ownAddress , uint8_t master )
144153{
145- UNUSED (master );
146154 if (obj == NULL )
147155 return ;
148156
@@ -170,53 +178,56 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
170178#if defined I2C1_BASE
171179 // Enable I2C1 clock if not done
172180 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 ;
181190 }
182- #endif
191+ #endif // I2C1_BASE
183192#if defined I2C2_BASE
184193 // Enable I2C2 clock if not done
185194 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 ;
198203 }
199- #endif
204+ #endif // I2C2_BASE
200205#if defined I2C3_BASE
201206 // Enable I2C3 clock if not done
202207 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 ;
208216 }
209- #endif
217+ #endif // I2C3_BASE
210218#if defined I2C4_BASE
211- // Enable I2C3 clock if not done
219+ // Enable I2C4 clock if not done
212220 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 ;
218229 }
219- #endif
230+ #endif // I2C4_BASE
220231
221232 //SCL
222233 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
250261 handle -> Init .Timing = timing ;
251262#else
252263 handle -> Init .ClockSpeed = timing ;
253- handle -> Init .DutyCycle = I2C_DUTYCYCLE_2 ;//16_9;
264+ handle -> Init .DutyCycle = I2C_DUTYCYCLE_2 ;
254265#endif
255266 handle -> Init .OwnAddress1 = ownAddress ;
256267 handle -> Init .OwnAddress2 = 0xFF ;
@@ -263,13 +274,15 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
263274
264275 HAL_NVIC_SetPriority (obj -> irq , 0 , 1 );
265276 HAL_NVIC_EnableIRQ (obj -> irq );
266- #ifdef STM32F1xx
277+ #if !defined( STM32F0xx ) && !defined( STM32L0xx )
267278 HAL_NVIC_SetPriority (obj -> irqER , 0 , 1 );
268279 HAL_NVIC_EnableIRQ (obj -> irqER );
269- #endif
280+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
270281
271282 // Init the I2C
272283 HAL_I2C_Init (handle );
284+
285+ obj -> isMaster = master ;
273286}
274287
275288/**
@@ -280,9 +293,9 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
280293void i2c_deinit (i2c_t * obj )
281294{
282295 HAL_NVIC_DisableIRQ (obj -> irq );
283- #ifdef STM32F1xx
296+ #if !defined( STM32F0xx ) && !defined( STM32L0xx )
284297 HAL_NVIC_DisableIRQ (obj -> irqER );
285- #endif
298+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
286299 HAL_I2C_DeInit (& (obj -> handle ));
287300}
288301
@@ -345,9 +358,11 @@ i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address,
345358 ret = I2C_OK ;
346359 // wait for transfer completion
347360 while ((HAL_I2C_GetState (& (obj -> handle )) != HAL_I2C_STATE_READY )
348- && (ret != I2C_TIMEOUT )){
361+ && (ret == I2C_OK )){
349362 if ((HAL_GetTick () - tickstart ) > I2C_TIMEOUT_TICK ) {
350363 ret = I2C_TIMEOUT ;
364+ } else if (HAL_I2C_GetError (& (obj -> handle )) != HAL_I2C_ERROR_NONE ) {
365+ ret = I2C_ERROR ;
351366 }
352367 }
353368 }
@@ -390,9 +405,11 @@ i2c_status_e i2c_master_read(i2c_t *obj, uint8_t dev_address, uint8_t *data, uin
390405 ret = I2C_OK ;
391406 // wait for transfer completion
392407 while ((HAL_I2C_GetState (& (obj -> handle )) != HAL_I2C_STATE_READY )
393- && (ret != I2C_TIMEOUT )){
408+ && (ret == I2C_OK )){
394409 if ((HAL_GetTick () - tickstart ) > I2C_TIMEOUT_TICK ) {
395410 ret = I2C_TIMEOUT ;
411+ } else if (HAL_I2C_GetError (& (obj -> handle )) != HAL_I2C_ERROR_NONE ) {
412+ ret = I2C_ERROR ;
396413 }
397414 }
398415 }
@@ -513,15 +530,24 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
513530
514531/**
515532 * @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.
516537 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
517538 * the configuration information for the specified I2C.
518539 * @retval None
519540 */
520541void HAL_I2C_ErrorCallback (I2C_HandleTypeDef * hi2c )
521542{
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+ }
523548}
524549
550+ #if defined(I2C1_BASE )
525551/**
526552* @brief This function handles I2C1 interrupt.
527553* @param None
@@ -531,8 +557,12 @@ void I2C1_EV_IRQHandler(void)
531557{
532558 I2C_HandleTypeDef * handle = i2c_handles [0 ];
533559 HAL_I2C_EV_IRQHandler (handle );
560+ #if defined(STM32F0xx ) || defined(STM32L0xx )
561+ HAL_I2C_ER_IRQHandler (handle );
562+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
534563}
535564
565+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
536566/**
537567* @brief This function handles I2C1 interrupt.
538568* @param None
@@ -543,7 +573,10 @@ void I2C1_ER_IRQHandler(void)
543573 I2C_HandleTypeDef * handle = i2c_handles [0 ];
544574 HAL_I2C_ER_IRQHandler (handle );
545575}
576+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
577+ #endif // I2C1_BASE
546578
579+ #if defined(I2C2_BASE )
547580/**
548581* @brief This function handles I2C2 interrupt.
549582* @param None
@@ -553,8 +586,12 @@ void I2C2_EV_IRQHandler(void)
553586{
554587 I2C_HandleTypeDef * handle = i2c_handles [1 ];
555588 HAL_I2C_EV_IRQHandler (handle );
589+ #if defined(STM32F0xx ) || defined(STM32L0xx )
590+ HAL_I2C_ER_IRQHandler (handle );
591+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
556592}
557593
594+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
558595/**
559596* @brief This function handles I2C2 interrupt.
560597* @param None
@@ -565,7 +602,10 @@ void I2C2_ER_IRQHandler(void)
565602 I2C_HandleTypeDef * handle = i2c_handles [1 ];
566603 HAL_I2C_ER_IRQHandler (handle );
567604}
605+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
606+ #endif // I2C2_BASE
568607
608+ #if defined(I2C3_BASE )
569609/**
570610* @brief This function handles I2C3 interrupt.
571611* @param None
@@ -575,8 +615,12 @@ void I2C3_EV_IRQHandler(void)
575615{
576616 I2C_HandleTypeDef * handle = i2c_handles [2 ];
577617 HAL_I2C_EV_IRQHandler (handle );
618+ #if defined(STM32F0xx ) || defined(STM32L0xx )
619+ HAL_I2C_ER_IRQHandler (handle );
620+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
578621}
579622
623+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
580624/**
581625* @brief This function handles I2C3 interrupt.
582626* @param None
@@ -587,7 +631,10 @@ void I2C3_ER_IRQHandler(void)
587631 I2C_HandleTypeDef * handle = i2c_handles [2 ];
588632 HAL_I2C_ER_IRQHandler (handle );
589633}
634+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
635+ #endif // I2C3_BASE
590636
637+ #if defined(I2C4_BASE )
591638/**
592639* @brief This function handles I2C4 interrupt.
593640* @param None
@@ -597,8 +644,12 @@ void I2C4_EV_IRQHandler(void)
597644{
598645 I2C_HandleTypeDef * handle = i2c_handles [3 ];
599646 HAL_I2C_EV_IRQHandler (handle );
647+ #if defined(STM32F0xx ) || defined(STM32L0xx )
648+ HAL_I2C_ER_IRQHandler (handle );
649+ #endif // defined(STM32F0xx) || defined(STM32L0xx)
600650}
601651
652+ #if !defined(STM32F0xx ) && !defined(STM32L0xx )
602653/**
603654* @brief This function handles I2C4 interrupt.
604655* @param None
@@ -609,6 +660,8 @@ void I2C4_ER_IRQHandler(void)
609660 I2C_HandleTypeDef * handle = i2c_handles [3 ];
610661 HAL_I2C_ER_IRQHandler (handle );
611662}
663+ #endif // !defined(STM32F0xx) && !defined(STM32L0xx)
664+ #endif // I2C4_BASE
612665
613666/**
614667 * @}
0 commit comments