302 #include "stm32f4xx_hal.h"
313 #ifdef HAL_I2C_MODULE_ENABLED
320 #define I2C_TIMEOUT_FLAG 35U
321 #define I2C_TIMEOUT_BUSY_FLAG 25U
322 #define I2C_TIMEOUT_STOP_FLAG 5U
323 #define I2C_NO_OPTION_FRAME 0xFFFF0000U
326 #define I2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | (uint32_t)HAL_I2C_STATE_BUSY_RX) & (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY))))
327 #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE))
328 #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER))
329 #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER))
330 #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE))
331 #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE))
353 static HAL_StatusTypeDef I2C_RequestMemoryWrite(
I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
354 static HAL_StatusTypeDef I2C_RequestMemoryRead(
I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
462 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
475 if (hi2c->MspInitCallback ==
NULL)
481 hi2c->MspInitCallback(hi2c);
566 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
567 if (hi2c->MspDeInitCallback ==
NULL)
573 hi2c->MspDeInitCallback(hi2c);
622 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
648 if (pCallback ==
NULL)
651 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
662 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
663 hi2c->MasterTxCpltCallback = pCallback;
666 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
667 hi2c->MasterRxCpltCallback = pCallback;
670 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
671 hi2c->SlaveTxCpltCallback = pCallback;
674 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
675 hi2c->SlaveRxCpltCallback = pCallback;
678 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
679 hi2c->ListenCpltCallback = pCallback;
682 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
683 hi2c->MemTxCpltCallback = pCallback;
686 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
687 hi2c->MemRxCpltCallback = pCallback;
690 case HAL_I2C_ERROR_CB_ID :
691 hi2c->ErrorCallback = pCallback;
694 case HAL_I2C_ABORT_CB_ID :
695 hi2c->AbortCpltCallback = pCallback;
698 case HAL_I2C_MSPINIT_CB_ID :
699 hi2c->MspInitCallback = pCallback;
702 case HAL_I2C_MSPDEINIT_CB_ID :
703 hi2c->MspDeInitCallback = pCallback;
708 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
719 case HAL_I2C_MSPINIT_CB_ID :
720 hi2c->MspInitCallback = pCallback;
723 case HAL_I2C_MSPDEINIT_CB_ID :
724 hi2c->MspDeInitCallback = pCallback;
729 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
739 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
782 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
786 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
790 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
794 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
798 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
802 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
806 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
810 case HAL_I2C_ERROR_CB_ID :
814 case HAL_I2C_ABORT_CB_ID :
818 case HAL_I2C_MSPINIT_CB_ID :
822 case HAL_I2C_MSPDEINIT_CB_ID :
828 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
839 case HAL_I2C_MSPINIT_CB_ID :
843 case HAL_I2C_MSPDEINIT_CB_ID :
849 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
859 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
882 if (pCallback ==
NULL)
885 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
894 hi2c->AddrCallback = pCallback;
899 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
931 hi2c->
ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
1069 if (I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) !=
HAL_OK)
1080 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
1114 if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
1190 if (I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) !=
HAL_OK)
1242 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
1344 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
1404 if ((pData ==
NULL) || (Size == 0U))
1460 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
1534 if ((pData ==
NULL) || (Size == (uint16_t)0))
1577 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
1610 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
1815 if ((pData ==
NULL) || (Size == 0U))
1877 if ((pData ==
NULL) || (Size == 0U))
2004 if (dmaxferstatus ==
HAL_OK)
2145 if (dmaxferstatus ==
HAL_OK)
2222 if ((pData ==
NULL) || (Size == 0U))
2265 if (dmaxferstatus ==
HAL_OK)
2319 if ((pData ==
NULL) || (Size == 0U))
2362 if (dmaxferstatus ==
HAL_OK)
2455 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) !=
HAL_OK)
2463 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
2498 if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
2578 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) !=
HAL_OK)
2627 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
2729 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) !=
HAL_OK)
3040 if (dmaxferstatus ==
HAL_OK)
3043 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) !=
HAL_OK)
3206 if (dmaxferstatus ==
HAL_OK)
3209 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) !=
HAL_OK)
3279 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) !=
HAL_OK)
3319 uint32_t I2C_Trials = 1U;
3374 if (((
HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
3424 while (I2C_Trials < Trials);
3453 __IO uint32_t Prev_State = 0x00U;
3551 __IO uint32_t Prev_State = 0x00U;
3625 if (dmaxferstatus ==
HAL_OK)
3717 __IO uint32_t Prev_State = 0x00U;
3778 if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
3841 __IO uint32_t Prev_State = 0x00U;
3908 if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
3950 if (dmaxferstatus ==
HAL_OK)
4049 if ((pData ==
NULL) || (Size == 0U))
4117 if ((pData ==
NULL) || (Size == 0U))
4212 if (dmaxferstatus ==
HAL_OK)
4272 if ((pData ==
NULL) || (Size == 0U))
4340 if ((pData ==
NULL) || (Size == 0U))
4435 if (dmaxferstatus ==
HAL_OK)
4525 tmp = (uint32_t)(hi2c->
State) & I2C_STATE_MSK;
4613 uint32_t sr1itflags;
4614 uint32_t sr2itflags = 0U;
4636 I2C_ConvertOtherXferOptions(hi2c);
4638 I2C_Master_SB(hi2c);
4643 I2C_Master_ADD10(hi2c);
4648 I2C_Master_ADDR(hi2c);
4659 I2C_MasterTransmit_TXE(hi2c);
4666 I2C_MasterTransmit_BTF(hi2c);
4670 I2C_MemoryTransmit_TXE_BTF(hi2c);
4688 I2C_MasterReceive_RXNE(hi2c);
4693 I2C_MasterReceive_BTF(hi2c);
4725 I2C_Slave_ADDR(hi2c, sr2itflags);
4730 I2C_Slave_STOPF(hi2c);
4738 I2C_SlaveTransmit_TXE(hi2c);
4743 I2C_SlaveTransmit_BTF(hi2c);
4756 I2C_SlaveReceive_RXNE(hi2c);
4761 I2C_SlaveReceive_BTF(hi2c);
4926 UNUSED(TransferDirection);
5103 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5104 hi2c->MasterTxCpltCallback(hi2c);
5123 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5124 hi2c->MemTxCpltCallback(hi2c);
5132 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5133 hi2c->MasterTxCpltCallback(hi2c);
5152 I2C_MemoryTransmit_TXE_BTF(hi2c);
5208 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5209 hi2c->MasterTxCpltCallback(hi2c);
5226 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5227 hi2c->MasterTxCpltCallback(hi2c);
5307 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5308 hi2c->MemTxCpltCallback(hi2c);
5358 if (I2C_WaitOnSTOPRequestThroughIT(hi2c) ==
HAL_OK)
5382 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5383 hi2c->MemRxCpltCallback(hi2c);
5393 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5394 hi2c->MasterRxCpltCallback(hi2c);
5419 hi2c->ErrorCallback(hi2c);
5528 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5529 hi2c->MemRxCpltCallback(hi2c);
5538 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5539 hi2c->MasterRxCpltCallback(hi2c);
5682 if (CurrentXferOptions == I2C_NO_OPTION_FRAME)
5706 && ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (CurrentXferOptions ==
I2C_FIRST_FRAME)))
5817 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5818 hi2c->SlaveTxCpltCallback(hi2c);
5879 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5880 hi2c->SlaveRxCpltCallback(hi2c);
5919 uint16_t SlaveAddrCode;
5945 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5946 hi2c->AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
6092 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6093 hi2c->SlaveRxCpltCallback(hi2c);
6107 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6108 hi2c->ListenCpltCallback(hi2c);
6121 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6122 hi2c->SlaveRxCpltCallback(hi2c);
6161 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6162 hi2c->ListenCpltCallback(hi2c);
6183 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6184 hi2c->SlaveTxCpltCallback(hi2c);
6207 uint32_t CurrentError;
6302 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6303 hi2c->AbortCpltCallback(hi2c);
6321 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6322 hi2c->ErrorCallback(hi2c);
6342 CurrentState = hi2c->
State;
6351 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6352 hi2c->ListenCpltCallback(hi2c);
6410 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADD10, Timeout, Tickstart) !=
HAL_OK)
6420 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADDR, Timeout, Tickstart) !=
HAL_OK)
6483 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADD10, Timeout, Tickstart) !=
HAL_OK)
6492 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADDR, Timeout, Tickstart) !=
HAL_OK)
6518 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADDR, Timeout, Tickstart) !=
HAL_OK)
6538 static HAL_StatusTypeDef I2C_RequestMemoryWrite(
I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
6557 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADDR, Timeout, Tickstart) !=
HAL_OK)
6566 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) !=
HAL_OK)
6589 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) !=
HAL_OK)
6618 static HAL_StatusTypeDef I2C_RequestMemoryRead(
I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
6640 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADDR, Timeout, Tickstart) !=
HAL_OK)
6649 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) !=
HAL_OK)
6672 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) !=
HAL_OK)
6687 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) !=
HAL_OK)
6714 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c,
I2C_FLAG_ADDR, Timeout, Tickstart) !=
HAL_OK)
6763 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6764 hi2c->SlaveTxCpltCallback(hi2c);
6776 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6777 hi2c->SlaveRxCpltCallback(hi2c);
6820 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6821 hi2c->ErrorCallback(hi2c);
6835 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6836 hi2c->MemRxCpltCallback(hi2c);
6846 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6847 hi2c->MasterRxCpltCallback(hi2c);
6892 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6893 hi2c->ErrorCallback(hi2c);
6964 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6965 hi2c->AbortCpltCallback(hi2c);
6991 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6992 hi2c->ErrorCallback(hi2c);
7017 if (((
HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7069 if (((
HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7099 if (I2C_IsAcknowledgeFailed(hi2c) !=
HAL_OK)
7107 if (((
HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7137 if (I2C_IsAcknowledgeFailed(hi2c) !=
HAL_OK)
7145 if (((
HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7175 if (I2C_IsAcknowledgeFailed(hi2c) !=
HAL_OK)
7181 if (((
HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7255 if (((
HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))