59 #include <adi_processor.h> 64 #include <drivers/general/adi_drivers_general.h> 65 #include <drivers/i2c/adi_i2c.h> 69 #include <drivers/pwr/adi_pwr.h> 93 #pragma diag_suppress=Pm011,Pm123,Pm073,Pm143,Pm088,Pm140 97 #include "adi_i2c_data.c" 101 uint16_t uZero16 = 0u;
104 #define I2C_BUSY (uZero16 != ((hDevice->pDev->MSTAT) & (uint16_t)(BITM_I2C_MSTAT_MBUSY | BITM_I2C_MSTAT_LINEBUSY))) 109 #define READ_NOT_WRITE (1u) 113 extern void I2C0_Master_Int_Handler(
void);
116 #define I2C_GPIO_PORT0_DS4 ((uint16_t) ((uint16_t) 1<<4)) 117 #define I2C_GPIO_PORT0_DS5 ((uint16_t) ((uint16_t) 1<<5)) 119 #define ADI_ADUCM302X_CHIPID_SI_1_2 0x284u 122 #if defined(ADI_DEBUG) 130 if ((i2c_device_info[0].hDevice != (hDevice)) && ((hDevice)->pDevInfo->hDevice != NULL)) {
138 #if defined(__ADUCM302x__) 139 static ADI_SYS_REGISTERS adi_sys_base = { pADI_SYS };
185 #if defined(__ADUCM302x__) 192 ADI_SYS_REGISTERS *sys = &adi_sys_base;
194 if( sys->pReg->CHIPID == ADI_ADUCM302X_CHIPID_SI_1_2 )
196 *((
volatile uint32_t *)REG_GPIO0_DS) |= ( I2C_GPIO_PORT0_DS4 | I2C_GPIO_PORT0_DS5 );
200 #if defined(ADI_DEBUG) 202 if (DeviceNum >= (uint32_t)ADI_I2C_NUM_INSTANCES) {
207 if (i2c_device_info[DeviceNum].hDevice != NULL) {
232 i2c_device_info[DeviceNum].hDevice = (ADI_I2C_DEV_DATA_TYPE *)pMemory;
233 memset(pMemory, 0, MemorySize);
236 hDevice->pDevInfo = &i2c_device_info[DeviceNum];
243 hDevice->pDev = i2c_device_info[DeviceNum].pDev;
246 hDevice->pDevInfo->pConfig = (ADI_I2C_CONFIG*)&gConfigInfo[DeviceNum];
281 if (IsDeviceHandle(hDevice)) {
294 hDevice->pDevInfo->hDevice = NULL;
358 if (IsDeviceHandle(hDevice)) {
365 if ((NULL == pTransaction->
pData) || (0u == pTransaction->
nDataSize)) {
371 hDevice->bSubmitCalled =
false;
374 submitTransaction(hDevice, pTransaction);
390 *pHwErrors = hDevice->hwErrors;
391 if (0u != hDevice->hwErrors) {
397 return hDevice->result;
453 if (IsDeviceHandle(hDevice)) {
460 if ((NULL == pTransaction->
pData) || (0u == pTransaction->
nDataSize)) {
466 hDevice->bSubmitCalled =
true;
469 submitTransaction(hDevice, pTransaction);
472 return hDevice->result;
514 if (IsDeviceHandle(hDevice)) {
520 if (
false == hDevice->bSubmitCalled) {
526 *pbCompletionState =
false;
528 *pbCompletionState =
true;
573 if (IsDeviceHandle(hDevice)) {
579 if (
false == hDevice->bSubmitCalled) {
592 *pHwErrors = hDevice->hwErrors;
593 if (0u != hDevice->hwErrors) {
599 return hDevice->result;
616 if (IsDeviceHandle(hDevice)) {
628 return i2cReset(hDevice);
658 uint32_t clockFrequency32, halfClock32;
659 uint16_t halfClock16;
660 uint16_t highTime16, lowTime16;
663 if (IsDeviceHandle(hDevice)) {
682 halfClock32 = (clockFrequency32 / requestedBitRate32) >> 1;
685 halfClock16 = (uint16_t)(halfClock32 & 0x0000ffffu);
688 if (halfClock32 != halfClock16) {
693 highTime16 = halfClock16 - 7u;
694 lowTime16 = halfClock16 - 1u;
697 highTime16 <<= BITP_I2C_DIV_HIGH;
698 lowTime16 <<= BITP_I2C_DIV_LOW;
701 if ( (uZero16 != ((uint16_t)highTime16 & (uint16_t)(~(BITM_I2C_DIV_HIGH))))
703 (uZero16 != ((uint16_t)lowTime16 & (uint16_t)(~(BITM_I2C_DIV_LOW))))
709 hDevice->pDev->DIV = highTime16 | lowTime16;
738 if (IsDeviceHandle(hDevice)) {
747 if (uZero16 != (SlaveAddress & (uint16_t)(~(BITM_I2C_ADDR1_VALUE >> 1)))) {
752 hDevice->i2cDeviceAddress = SlaveAddress;
795 if (IsDeviceHandle(hDevice)) {
804 uint16_t savedSlaveAddress = hDevice->i2cDeviceAddress;
805 hDevice->i2cDeviceAddress = 0u;
819 hDevice->i2cDeviceAddress = savedSlaveAddress;
824 return hDevice->result;
850 hDevice->pNextPrologueByte = pTransaction->
pPrologue;
851 hDevice->remainingPrologueCount = pTransaction->
nPrologueSize;
855 hDevice->i2cEncodedDeviceAddress = (hDevice->i2cDeviceAddress & (BITM_I2C_ADDR1_VALUE >> 1)) << 1;
861 hDevice->pNextReadByte = pTransaction->
pData;
862 hDevice->remainingReadCount = pTransaction->
nDataSize;
863 hDevice->pNextWriteByte = NULL;
864 hDevice->remainingWriteCount = 0u;
867 hDevice->i2cEncodedDeviceAddress |= READ_NOT_WRITE;
870 commenceReceive(hDevice);
875 hDevice->pNextReadByte = NULL;
876 hDevice->remainingReadCount = 0u;
877 hDevice->pNextWriteByte = pTransaction->
pData;
878 hDevice->remainingWriteCount = pTransaction->
nDataSize;
881 hDevice->i2cEncodedDeviceAddress &= (~READ_NOT_WRITE);
884 commenceTransmit(hDevice);
894 NVIC_EnableIRQ(hDevice->pDevInfo->pioIRQn);
897 hDevice->pDev->MCTL |= (BITM_I2C_MCTL_IENMTX | BITM_I2C_MCTL_MASEN);
900 uint16_t writableBytes = 2u - (hDevice->pDev->MSTAT & (uint16_t)BITM_I2C_MSTAT_MTXF);
903 while ((hDevice->remainingPrologueCount) && (0u < writableBytes)) {
904 hDevice->pDev->MTX = *hDevice->pNextPrologueByte;
905 hDevice->pNextPrologueByte++;
906 hDevice->remainingPrologueCount--;
911 while ((hDevice->remainingWriteCount) && (0u < writableBytes) ) {
912 hDevice->pDev->MTX = *hDevice->pNextWriteByte;
913 hDevice->pNextWriteByte++;
914 hDevice->remainingWriteCount--;
919 hDevice->pDev->ADDR1 = hDevice->i2cEncodedDeviceAddress;
930 NVIC_EnableIRQ(hDevice->pDevInfo->pioIRQn);
933 hDevice->pDev->MCTL |= (uint16_t)(BITM_I2C_MCTL_IENMRX | BITM_I2C_MCTL_MASEN);
936 if (hDevice->remainingReadCount > BITM_I2C_MRXCNT_EXTEND) {
937 hDevice->pDev->MRXCNT = BITM_I2C_MRXCNT_EXTEND;
938 hDevice->remainingReadCount -= BITM_I2C_MRXCNT_EXTEND;
940 hDevice->pDev->MRXCNT = hDevice->remainingReadCount - 1u;
941 hDevice->remainingReadCount = 0u;
945 if (hDevice->remainingPrologueCount) {
948 hDevice->pDev->MCTL |= BITM_I2C_MCTL_IENMTX;
951 uint16_t writableBytes = 2u - (hDevice->pDev->MSTAT & (uint16_t)BITM_I2C_MSTAT_MTXF);
954 while ((hDevice->remainingPrologueCount) && (0u < writableBytes)) {
955 hDevice->pDev->MTX = *hDevice->pNextPrologueByte;
956 hDevice->pNextPrologueByte++;
957 hDevice->remainingPrologueCount--;
963 hDevice->pDev->ADDR1 = hDevice->i2cEncodedDeviceAddress & (uint16_t)(~READ_NOT_WRITE);
968 hDevice->pDev->ADDR1 = hDevice->i2cEncodedDeviceAddress;
976 volatile uint16_t temp;
978 NVIC_DisableIRQ(hDevice->pDevInfo->pioIRQn);
981 temp = hDevice->pDev->MSTAT;
982 hDevice->pDev->MSTAT = temp;
985 while (uZero16 != (hDevice->pDev->STAT & (uint16_t)BITM_I2C_STAT_MRXF)) {
986 volatile uint16_t delme ADI_UNUSED_ATTRIBUTE = hDevice->pDev->MTX;
990 hDevice->pDev->MCTL = 0u;
993 hDevice->pDev->SHCTL = 1u;
996 hDevice->pDev->MCTL = hDevice->pDevInfo->pConfig->MasterControlRegister;
997 hDevice->pDev->DIV = hDevice->pDevInfo->pConfig->ClockDividerRegister;
998 hDevice->pDev->SHCTL = hDevice->pDevInfo->pConfig->SharedControlRegister;
999 hDevice->pDev->TCTL = hDevice->pDevInfo->pConfig->TimingControlRegister;
1000 hDevice->pDev->ASTRETCH_SCL = hDevice->pDevInfo->pConfig->ClockStretchRegister;
1001 hDevice->i2cDeviceAddress = hDevice->pDevInfo->pConfig->TargetSlaveAddress;
1017 uint16_t writableBytes = 2u - ((hDevice->pDev->STAT & (uint16_t)BITM_I2C_STAT_MTXF) >> BITP_I2C_STAT_MTXF);
1020 while ((hDevice->remainingPrologueCount) && (0u < writableBytes)) {
1021 hDevice->pDev->MTX = *hDevice->pNextPrologueByte;
1022 hDevice->pNextPrologueByte++;
1023 hDevice->remainingPrologueCount--;
1028 if (0u == hDevice->remainingPrologueCount) {
1031 if (0u < hDevice->remainingReadCount) {
1034 hDevice->pDev->ADDR1 = hDevice->i2cEncodedDeviceAddress;
1039 while ((hDevice->remainingWriteCount) && (0u < writableBytes)) {
1040 hDevice->pDev->MTX = *hDevice->pNextWriteByte;
1041 hDevice->pNextWriteByte++;
1042 hDevice->remainingWriteCount--;
1049 if (0u == hDevice->remainingWriteCount) {
1050 hDevice->pDev->MSTAT = BITM_I2C_MSTAT_MTXREQ;
1061 uint16_t readableBytes = (hDevice->pDev->STAT & (uint16_t)BITM_I2C_STAT_MRXF) >> BITP_I2C_STAT_MRXF;
1064 while (0u < readableBytes) {
1069 *hDevice->pNextReadByte = (uint8_t)hDevice->pDev->MRX;
1070 hDevice->pNextReadByte++;
1072 if (0u == hDevice->pDev->MCRXCNT) {
1075 if (hDevice->remainingReadCount > BITM_I2C_MRXCNT_EXTEND) {
1077 hDevice->pDev->MRXCNT = BITM_I2C_MRXCNT_EXTEND;
1078 hDevice->remainingReadCount -= BITM_I2C_MRXCNT_EXTEND;
1080 if (hDevice->remainingReadCount) {
1082 hDevice->pDev->MRXCNT = hDevice->remainingReadCount - 1u;
1083 hDevice->remainingReadCount = 0u;
1100 NVIC_DisableIRQ(hDevice->pDevInfo->pioIRQn);
1103 hDevice->pDev->MCTL = (uint16_t)gConfigInfo->MasterControlRegister;
1112 if (uZero16 != (hDevice->hwStatus & (uint16_t)BITM_I2C_MSTAT_NACKADDR)) {
1116 if (uZero16 != (hDevice->hwStatus & (uint16_t)BITM_I2C_MSTAT_NACKDATA)) {
1120 if (uZero16 != (hDevice->hwStatus & (uint16_t)BITM_I2C_MSTAT_ALOST)) {
1137 void I2C0_Master_Int_Handler(
void) {
1149 hDevice->hwStatus = hDevice->pDev->MSTAT;
1152 if (hDevice->bRepeatStart) {
1153 hDevice->pDev->ADDR1 = hDevice->i2cEncodedDeviceAddress;
1154 hDevice->bRepeatStart =
false;
1158 if (uZero16 != (hDevice->hwStatus & (uint16_t)BITM_I2C_MSTAT_MTXREQ)) {
1159 transmitHandler(hDevice);
1163 if (uZero16 != (hDevice->hwStatus & (uint16_t)BITM_I2C_MSTAT_MRXREQ)) {
1164 receiveHandler(hDevice);
1168 if (uZero16 != (hDevice->hwStatus & ADI_I2C_STATUS_ERROR_MASK)) {
1169 errorHandler(hDevice);
1176 if (uZero16 != (hDevice->hwStatus & BITM_I2C_MSTAT_TCOMP)) {
1177 completeHandler(hDevice);
1184 if (
true == bPost) {
ADI_I2C_RESULT adi_i2c_IssueGeneralCall(ADI_I2C_HANDLE const hDevice, uint8_t *const pData, uint8_t const nDataSize, uint32_t *const pHwErrors)
Transmit a General Call command to all slave devices on the I2C bus.
ADI_I2C_RESULT adi_i2c_Open(uint32_t const DeviceNum, void *const pMemory, uint32_t const MemorySize, ADI_I2C_HANDLE *const phDevice)
Initialize and allocate an I2C device for use in Master Mode.
ADI_I2C_RESULT adi_i2c_ReadWrite(ADI_I2C_HANDLE const hDevice, ADI_I2C_TRANSACTION *const pTransaction, uint32_t *const pHwErrors)
Blocking I2C Master-Mode data read/write API.
ADI_I2C_RESULT adi_i2c_IsBufferAvailable(ADI_I2C_HANDLE const hDevice, bool *const pbCompletionState)
Query if a non-blocking I2C transfer is complete.
#define ADI_I2C_MEMORY_SIZE
struct __ADI_I2C_DEV_DATA_TYPE * ADI_I2C_HANDLE
ADI_I2C_RESULT adi_i2c_SetBitRate(ADI_I2C_HANDLE const hDevice, uint32_t const requestedBitRate32)
Set the I2C serial bus speed.
ADI_I2C_RESULT adi_i2c_Reset(ADI_I2C_HANDLE const hDevice)
Reset an I2C device and driver instance.
ADI_PWR_RESULT adi_pwr_GetClockFrequency(const ADI_CLOCK_ID eClockId, uint32_t *pClock)
Get the frequency of the given clock. Obtain individual peripheral clock frequencies.
ADI_I2C_RESULT adi_i2c_SetSlaveAddress(ADI_I2C_HANDLE const hDevice, uint16_t const SlaveAddress)
Set the I2C serial bus slave address.
ADI_I2C_RESULT adi_i2c_SubmitBuffer(ADI_I2C_HANDLE const hDevice, ADI_I2C_TRANSACTION *const pTransaction)
Non-Blocking I2C Master-Mode data read or data write API.
ADI_I2C_RESULT adi_i2c_Close(ADI_I2C_HANDLE const hDevice)
Uninitialize and deallocate an I2C device.
ADI_I2C_RESULT adi_i2c_GetBuffer(ADI_I2C_HANDLE const hDevice, uint32_t *const pHwErrors)
Request ownership of a submitted buffer.