42 #ifndef I2C_VALID_TIMING_NBR
43 #define I2C_VALID_TIMING_NBR 128U
45 #define I2C_SPEED_FREQ_STANDARD 0U
46 #define I2C_SPEED_FREQ_FAST 1U
47 #define I2C_SPEED_FREQ_FAST_PLUS 2U
48 #define I2C_ANALOG_FILTER_DELAY_MIN 50U
49 #define I2C_ANALOG_FILTER_DELAY_MAX 260U
50 #define I2C_USE_ANALOG_FILTER 1U
51 #define I2C_DIGITAL_FILTER_COEF 0U
52 #define I2C_PRESC_MAX 16U
53 #define I2C_SCLDEL_MAX 16U
54 #define I2C_SDADEL_MAX 16U
55 #define I2C_SCLH_MAX 256U
56 #define I2C_SCLL_MAX 256U
57 #define SEC2NSEC 1000000000UL
147 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
148 static uint32_t IsI2c4MspCbValid = 0;
153 #if defined(BSP_USE_CMSIS_OS)
154 static osSemaphoreId BspI2cSemaphore = 0;
173 static int32_t
I2C4_WriteReg(uint16_t DevAddr, uint16_t MemAddSize, uint16_t Reg, uint8_t *pData, uint16_t Length);
174 static int32_t
I2C4_ReadReg(uint16_t DevAddr, uint16_t MemAddSize, uint16_t Reg, uint8_t *pData, uint16_t Length);
175 static uint32_t
I2C_GetTiming(uint32_t clock_src_freq, uint32_t i2c_freq);
202 #if defined(BSP_USE_CMSIS_OS)
203 if(BspI2cSemaphore ==
NULL)
206 osSemaphoreDef(BSP_I2C_SEM);
207 BspI2cSemaphore = osSemaphoreCreate(osSemaphore(BSP_I2C_SEM), 1);
210 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 0)
214 if (IsI2c4MspCbValid == 0U)
228 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
248 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 0)
287 uint32_t analog_filter;
317 #if defined(BSP_USE_CMSIS_OS)
319 osSemaphoreWait(BspI2cSemaphore, osWaitForever);
336 #if defined(BSP_USE_CMSIS_OS)
338 osSemaphoreRelease(BspI2cSemaphore);
354 #if defined(BSP_USE_CMSIS_OS)
356 osSemaphoreWait(BspI2cSemaphore, osWaitForever);
373 #if defined(BSP_USE_CMSIS_OS)
375 osSemaphoreRelease(BspI2cSemaphore);
391 #if defined(BSP_USE_CMSIS_OS)
393 osSemaphoreWait(BspI2cSemaphore, osWaitForever);
410 #if defined(BSP_USE_CMSIS_OS)
412 osSemaphoreRelease(BspI2cSemaphore);
428 #if defined(BSP_USE_CMSIS_OS)
430 osSemaphoreWait(BspI2cSemaphore, osWaitForever);
447 #if defined(BSP_USE_CMSIS_OS)
449 osSemaphoreRelease(BspI2cSemaphore);
464 #if defined(BSP_USE_CMSIS_OS)
466 osSemaphoreWait(BspI2cSemaphore, osWaitForever);
472 #if defined(BSP_USE_CMSIS_OS)
474 osSemaphoreRelease(BspI2cSemaphore);
488 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
493 int32_t BSP_I2C4_RegisterDefaultMspCallbacks (
void)
496 #if defined(BSP_USE_CMSIS_OS)
498 osSemaphoreWait(BspI2cSemaphore, osWaitForever);
513 IsI2c4MspCbValid = 1U;
515 #if defined(BSP_USE_CMSIS_OS)
517 osSemaphoreRelease(BspI2cSemaphore);
528 int32_t BSP_I2C4_RegisterMspCallbacks (BSP_I2C_Cb_t *Callback)
531 #if defined(BSP_USE_CMSIS_OS)
533 osSemaphoreWait(BspI2cSemaphore, osWaitForever);
538 if(HAL_I2C_RegisterCallback(&
hbus_i2c4, HAL_I2C_MSPINIT_CB_ID, Callback->pMspI2cInitCb) !=
HAL_OK)
542 else if(HAL_I2C_RegisterCallback(&
hbus_i2c4, HAL_I2C_MSPDEINIT_CB_ID, Callback->pMspI2cDeInitCb) !=
HAL_OK)
548 IsI2c4MspCbValid = 1U;
550 #if defined(BSP_USE_CMSIS_OS)
552 osSemaphoreRelease(BspI2cSemaphore);
578 if((clock_src_freq != 0U) && (i2c_freq != 0U))
582 if ((i2c_freq >=
I2C_Charac[speed].freq_min) &&
614 int32_t tsdadel_min, tsdadel_max;
616 uint32_t presc, scldel, sdadel;
617 uint32_t tafdel_min, tafdel_max;
619 ti2cclk = (
SEC2NSEC + (clock_src_freq / 2U))/ clock_src_freq;
630 (int32_t)tafdel_min - (int32_t)(((int32_t)
I2C_Charac[I2C_speed].dnf + 3) * (int32_t)ti2cclk);
633 (int32_t)tafdel_max - (int32_t)(((int32_t)
I2C_Charac[I2C_speed].dnf + 4) * (int32_t)ti2cclk);
639 if (tsdadel_min <= 0)
644 if (tsdadel_max <= 0)
654 uint32_t tscldel = (scldel + 1U) * (presc + 1U) * ti2cclk;
656 if (tscldel >= (uint32_t)tscldel_min)
661 uint32_t tsdadel = (sdadel * (presc + 1U)) * ti2cclk;
663 if ((tsdadel >= (uint32_t)tsdadel_min) && (tsdadel <= (uint32_t)tsdadel_max))
665 if(presc != prev_presc)
693 uint32_t ret = 0xFFFFFFFFU;
698 uint32_t clk_min, clk_max;
702 ti2cclk = (
SEC2NSEC + (clock_src_freq / 2U))/ clock_src_freq;
713 prev_error = ti2cspeed;
723 uint32_t tscl_l = tafdel_min + dnf_delay + (2U * ti2cclk) + ((scll + 1U) * tpresc);
728 if ((tscl_l >
I2C_Charac[I2C_speed].lscl_min) && (ti2cclk < ((tscl_l - tafdel_min - dnf_delay) / 4U)))
733 uint32_t tscl_h = tafdel_min + dnf_delay + (2U * ti2cclk) + ((sclh + 1U) * tpresc);
738 if ((tscl >= clk_min) && (tscl <= clk_max) && (tscl_h >=
I2C_Charac[I2C_speed].hscl_min) && (ti2cclk < tscl_h))
740 int32_t
error = (int32_t)tscl - (int32_t)ti2cspeed;
748 if ((uint32_t)
error < prev_error)
750 prev_error = (uint32_t)
error;
840 static int32_t
I2C4_WriteReg(uint16_t DevAddr, uint16_t Reg, uint16_t MemAddSize, uint8_t *pData, uint16_t Length)
859 static int32_t
I2C4_ReadReg(uint16_t DevAddr, uint16_t Reg, uint16_t MemAddSize, uint8_t *pData, uint16_t Length)