|
@@ -25,122 +25,11 @@
|
|
|
Included File
|
|
|
*************************************************************************/
|
|
|
#include "i2c_master.h"
|
|
|
-#include "api_rt.h"
|
|
|
-#include "gd32f30x_dma.h"
|
|
|
#include "api.h"
|
|
|
/*************************************************************************
|
|
|
Exported Functions (N/A)
|
|
|
*************************************************************************/
|
|
|
|
|
|
-/*************************************************************************
|
|
|
- Function:
|
|
|
- Description:
|
|
|
- Call by:
|
|
|
- Input Variables:
|
|
|
- Output/Return Variables:
|
|
|
- Subroutine Call:
|
|
|
- Reference:
|
|
|
-*************************************************************************/
|
|
|
-static UWORD i2c_pvt_uwWaitCnt = 0, i2c_pvt_uwWaitCnt2 = 0;
|
|
|
-void i2c_voWaitEEReady(UBYTE SlaveAddr)
|
|
|
-{
|
|
|
- /* Wait at least 5ms */
|
|
|
- while (i2c_pvt_uwWaitCnt2 < 2)
|
|
|
- {
|
|
|
- i2c_pvt_uwWaitCnt++;
|
|
|
- if (i2c_pvt_uwWaitCnt == 10000)
|
|
|
- {
|
|
|
- i2c_pvt_uwWaitCnt2++;
|
|
|
- i2c_pvt_uwWaitCnt = 0;
|
|
|
- }
|
|
|
- }
|
|
|
- i2c_pvt_uwWaitCnt2 = 0;
|
|
|
-
|
|
|
-// /* GD MCU has ACK detect function */
|
|
|
-// ULONG ulTimeCnt = 0;
|
|
|
-// UWORD uwTimeoutNum = 0, uwTimeoutNum2;
|
|
|
-//
|
|
|
-// while(!I2C_EE_ComuFltFlg)
|
|
|
-// {
|
|
|
-// /* write to EEPROM enable*/
|
|
|
-// iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
|
|
|
-// /* wait until I2C bus is idle */
|
|
|
-// while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* send a start condition to I2C bus */
|
|
|
-// i2c_start_on_bus(I2C0);
|
|
|
-// /* wait until SBSEND bit is set */
|
|
|
-// while((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* send slave address to I2C bus */
|
|
|
-// i2c_master_addressing(I2C0, SlaveAddr, I2C_TRANSMITTER);
|
|
|
-// /* wait until ADDSEND bit is set */
|
|
|
-// while((!((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) || (i2c_flag_get(I2C0, I2C_FLAG_AERR))))&& (++ulTimeCnt < I2C_SHORT_TIMEOUT));
|
|
|
-// if(ulTimeCnt < I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// if(i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
|
|
|
-// {
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* clear the bit of I2C_FLAG_ADDSEND */
|
|
|
-// i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
|
-// /* send a stop condition to I2C bus */
|
|
|
-// i2c_stop_on_bus(I2C0);
|
|
|
-// /* wait until stop condition generate */
|
|
|
-// while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* exit the function */
|
|
|
-// I2C_EE_ComuFltFlg = FALSE;
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// /* clear the bit of I2C_FLAG_AERR */
|
|
|
-// i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
|
|
-// /* send a stop condition to I2C bus */
|
|
|
-// i2c_stop_on_bus(I2C0);
|
|
|
-// /* wait until stop condition generate */
|
|
|
-// while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// uwTimeoutNum2 ++;
|
|
|
-// if(uwTimeoutNum2 > 4)
|
|
|
-// {
|
|
|
-// I2C_EE_ComuFltFlg = TRUE;
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// /* I2C communication timeout fault */
|
|
|
-// if(uwTimeoutNum > 3 )
|
|
|
-// {
|
|
|
-// I2C_EE_ComuFltFlg = TRUE;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// /* write to EEPROM disable */
|
|
|
-// iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
/*************************************************************************
|
|
|
Function:
|
|
|
Description:
|
|
@@ -317,332 +206,6 @@ void i2c_voHistoryWriteBuffer(void)
|
|
|
I2C_ubHistoyWriteBuffer[I2C_HISTORY_PARA_CRC_ARDDR] = (UBYTE)(i2c_uwCRCCcitt(I2C_pHistoryWriteBuffer, I2C_HISTORY_PARA_N_BYTES) >> 8);
|
|
|
I2C_ubHistoyWriteBuffer[I2C_HISTORY_PARA_CRC_ARDDR + 1] = (UBYTE)(i2c_uwCRCCcitt(I2C_pHistoryWriteBuffer, I2C_HISTORY_PARA_N_BYTES));
|
|
|
}
|
|
|
-/*************************************************************************
|
|
|
- Function:
|
|
|
- Description:
|
|
|
- Call by:
|
|
|
- Input Variables:
|
|
|
- Output/Return Variables:
|
|
|
- Subroutine Call:
|
|
|
- Reference:
|
|
|
-*************************************************************************/
|
|
|
-//static void i2c_voByteWrite2EE(UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE Data) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
|
|
|
-//{
|
|
|
-// ULONG ulTimeCnt = 0;
|
|
|
-// UWORD uwTimeoutNum = 0;
|
|
|
-//
|
|
|
-// if(!I2C_EE_ComuFltFlg)
|
|
|
-// {
|
|
|
-// /* write to EEPROM enable*/
|
|
|
-// iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
|
|
|
-// /* wait until I2C bus is idle */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (ulTimeCnt< I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// ulTimeCnt++;
|
|
|
-// }
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* send a start condition to I2C bus */
|
|
|
-// i2c_start_on_bus(I2C0);
|
|
|
-// /* wait until SBSEND bit is set */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// ulTimeCnt++;
|
|
|
-// }
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* send slave address to I2C bus */
|
|
|
-// i2c_master_addressing(I2C0, SlaveAddr, I2C_TRANSMITTER);
|
|
|
-// /* wait until ADDSEND bit is set */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// ulTimeCnt++;
|
|
|
-// }
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
|
-// /* wait until the transmit data buffer is empty */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// ulTimeCnt++;
|
|
|
-// }
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* writeAddr transmission */
|
|
|
-// i2c_data_transmit(I2C0, WriteAddr);
|
|
|
-// /* wait until the BTC bit is set */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// ulTimeCnt++;
|
|
|
-// }
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* data transmission */
|
|
|
-// i2c_data_transmit(I2C0, Data);
|
|
|
-// /* wait until the BTC bit is set */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// ulTimeCnt++;
|
|
|
-// }
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* send a stop condition to I2C bus */
|
|
|
-// i2c_stop_on_bus(I2C0);
|
|
|
-// /* wait until stop condition generate */
|
|
|
-// while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// ulTimeCnt++;
|
|
|
-// }
|
|
|
-// if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// uwTimeoutNum ++;
|
|
|
-// }
|
|
|
-// ulTimeCnt = 0;
|
|
|
-// /* write to EEPROM disable */
|
|
|
-// iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
|
|
|
-//
|
|
|
-// /* I2C EEPROM communication timeout fault */
|
|
|
-// if(uwTimeoutNum > 0)
|
|
|
-// {
|
|
|
-// I2C_EE_ComuFltFlg = TRUE;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {}
|
|
|
-//}
|
|
|
-/*************************************************************************
|
|
|
- Function:
|
|
|
- Description:
|
|
|
- Call by:
|
|
|
- Input Variables:
|
|
|
- Output/Return Variables:
|
|
|
- Subroutine Call:
|
|
|
- Reference:
|
|
|
-*************************************************************************/
|
|
|
-void i2c_voPageWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE NBytesToWrite) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
|
|
|
-{
|
|
|
- ULONG ulTimeCnt = 0;
|
|
|
- UWORD uwTimeoutNum = 0;
|
|
|
-
|
|
|
- if(!I2C_EE_ComuFltFlg)
|
|
|
- {
|
|
|
- /* write to EEPROM enable*/
|
|
|
- iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_HighLevel);
|
|
|
- /* wait until I2C bus is idle */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* send a start condition to I2C bus */
|
|
|
- i2c_start_on_bus(I2C0);
|
|
|
- /* wait until SBSEND bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* send slave address to I2C bus */
|
|
|
- i2c_master_addressing(I2C0, SlaveAddr, I2C_TRANSMITTER);
|
|
|
- /* wait until ADDSEND bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
|
- /* wait until the transmit data buffer is empty */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* writeAddr transmission */
|
|
|
- i2c_data_transmit(I2C0, WriteAddr);
|
|
|
- /* wait until the BTC bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* data transmission */
|
|
|
- while (NBytesToWrite != 0)
|
|
|
- {
|
|
|
- NBytesToWrite --;
|
|
|
- i2c_data_transmit(I2C0, *pBuffer);
|
|
|
- pBuffer++; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
|
|
|
- /* wait until the BTC bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* I2C communication timeout fault */
|
|
|
- if(uwTimeoutNum > 3 )
|
|
|
- {
|
|
|
- I2C_EE_ComuFltFlg = TRUE;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- /* send a stop condition to I2C bus */
|
|
|
- i2c_stop_on_bus(I2C0);
|
|
|
- /* wait until stop condition generate */
|
|
|
- while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* I2C EEPROM communication timeout fault */
|
|
|
- if(uwTimeoutNum > 0)
|
|
|
- {
|
|
|
- I2C_EE_ComuFltFlg = TRUE;
|
|
|
- }
|
|
|
- /* write to EEPROM disable */
|
|
|
-
|
|
|
- iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
|
|
|
- }
|
|
|
- else
|
|
|
- {}
|
|
|
-}
|
|
|
-/*************************************************************************
|
|
|
- Function:
|
|
|
- Description:
|
|
|
- Call by:
|
|
|
- Input Variables:
|
|
|
- Output/Return Variables:
|
|
|
- Subroutine Call:
|
|
|
- Reference:
|
|
|
-*************************************************************************/
|
|
|
-static void i2c_voBufferWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE NBytesToWrite)
|
|
|
-{
|
|
|
- UBYTE ubNPages = 0, ubNSingleBytes = 0, ubAddr = 0, ubCnt = 0, ubTemp = 0;
|
|
|
-
|
|
|
- ubAddr = WriteAddr % I2C_EE_PAGESIZE_NBYTES;
|
|
|
- ubCnt = I2C_EE_PAGESIZE_NBYTES - ubAddr; // Cnt datas away from page alignment
|
|
|
- ubNPages = NBytesToWrite / I2C_EE_PAGESIZE_NBYTES;
|
|
|
- ubNSingleBytes = NBytesToWrite % I2C_EE_PAGESIZE_NBYTES;
|
|
|
-
|
|
|
- if (ubAddr == 0)
|
|
|
- {
|
|
|
- if (ubNPages == 0)
|
|
|
- {
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubNSingleBytes);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- while (ubNPages != 0)
|
|
|
- {
|
|
|
- ubNPages--;
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, I2C_EE_PAGESIZE_NBYTES);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- WriteAddr += I2C_EE_PAGESIZE_NBYTES;
|
|
|
- pBuffer += I2C_EE_PAGESIZE_NBYTES; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
|
|
|
- }
|
|
|
- if (ubNSingleBytes != 0)
|
|
|
- {
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubNSingleBytes);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (ubNPages == 0)
|
|
|
- {
|
|
|
- if (ubNSingleBytes > ubCnt)
|
|
|
- {
|
|
|
- ubTemp = ubNSingleBytes - ubCnt;
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubCnt);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- WriteAddr += ubCnt;
|
|
|
- pBuffer += ubCnt; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
|
|
|
-
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubTemp);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, NBytesToWrite);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- NBytesToWrite -= ubCnt;
|
|
|
- ubNPages = NBytesToWrite / I2C_EE_PAGESIZE_NBYTES;
|
|
|
- ubNSingleBytes = NBytesToWrite % I2C_EE_PAGESIZE_NBYTES;
|
|
|
- if (ubCnt != 0)
|
|
|
- {
|
|
|
- /* Write the remaining bytes of the page where WriteAddr is located */
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubCnt);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- WriteAddr += ubCnt;
|
|
|
- pBuffer += ubCnt; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
|
|
|
- }
|
|
|
- while (ubNPages != 0)
|
|
|
- {
|
|
|
- ubNPages--;
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, I2C_EE_PAGESIZE_NBYTES);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- WriteAddr += I2C_EE_PAGESIZE_NBYTES;
|
|
|
- pBuffer += I2C_EE_PAGESIZE_NBYTES; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
|
|
|
- }
|
|
|
- if (ubNSingleBytes != 0)
|
|
|
- {
|
|
|
- i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, I2C_EE_PAGESIZE_NBYTES);
|
|
|
- i2c_voWaitEEReady(SlaveAddr);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
|
|
|
/*************************************************************************
|
|
|
Function:
|
|
@@ -680,7 +243,7 @@ void i2c_voInfoWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
|
|
|
{
|
|
|
ubSlaveAddr = I2C_SLAVEADDR_BLOCK1;
|
|
|
}
|
|
|
- //i2c_voBufferWrite2EE(I2C_pWriteBuffer, ubSlaveAddr, ubWriteAddr, ubNBytes);
|
|
|
+
|
|
|
iI2C_Write(0, ubSlaveAddr, ubWriteAddr, 8, I2C_pWriteBuffer, ubNBytes);
|
|
|
}
|
|
|
|
|
@@ -731,7 +294,6 @@ void i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
|
|
|
ubSlaveAddr = I2C_SLAVEADDR_BLOCK3;
|
|
|
}
|
|
|
|
|
|
- //i2c_voBufferWrite2EE(I2C_pHistoryWriteBuffer, ubSlaveAddr, ubWriteAddr, ubNBytes);
|
|
|
iI2C_Write(0, ubSlaveAddr, ubWriteAddr, 8, I2C_pHistoryWriteBuffer, ubNBytes);
|
|
|
}
|
|
|
if ((ubReTX2 == 0) && (I2C_EE_ComuFltFlg != TRUE))
|
|
@@ -743,6 +305,7 @@ void i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
|
|
|
out->blTX2FinishFlg = FALSE;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/*************************************************************************
|
|
|
Function:
|
|
|
Description:
|
|
@@ -752,314 +315,6 @@ void i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
|
|
|
Subroutine Call:
|
|
|
Reference:
|
|
|
*************************************************************************/
|
|
|
-void i2c_bus_reset(void)
|
|
|
-{
|
|
|
- i2c_deinit(I2C0);
|
|
|
- /* configure SDA/SCL for GPIO */
|
|
|
- GPIO_BC(GPIOB) |= GPIO_PIN_6;
|
|
|
- GPIO_BC(GPIOB) |= GPIO_PIN_7;
|
|
|
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
|
|
|
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- GPIO_BOP(GPIOB) |= GPIO_PIN_6;
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- __NOP();
|
|
|
- GPIO_BOP(GPIOB) |= GPIO_PIN_7;
|
|
|
- /* connect I2C_SCL_PIN to I2C_SCL */
|
|
|
- /* connect I2C_SDA_PIN to I2C_SDA */
|
|
|
- gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
|
|
|
- gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
|
|
|
- /* configure the I2CX interface */
|
|
|
- /* configure I2C0 clock */
|
|
|
- i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
|
|
|
-
|
|
|
- /* configure I2C0 address */
|
|
|
- i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_SLAVEADDR_BLOCK1);
|
|
|
-
|
|
|
- /* enable acknowledge */
|
|
|
- i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
|
-
|
|
|
-// /* enable I2C0 DMA */
|
|
|
-// i2c_dma_config(I2C0, I2C_DMA_ON);
|
|
|
-
|
|
|
- /* enable I2C0 */
|
|
|
- i2c_enable(I2C0);
|
|
|
-}
|
|
|
-/*************************************************************************
|
|
|
- Function:
|
|
|
- Description:
|
|
|
- Call by:
|
|
|
- Input Variables:
|
|
|
- Output/Return Variables:
|
|
|
- Subroutine Call:
|
|
|
- Reference:
|
|
|
-*************************************************************************/
|
|
|
-// void i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
|
|
|
-// {
|
|
|
-
|
|
|
-// UWORD timeout = 0;
|
|
|
-// UBYTE ubRdNBytes = 0, ubRdSlaveAddr = 0, ubRdAddr = 0;
|
|
|
-// UBYTE state = (UBYTE)I2C_START;
|
|
|
-// UBYTE read_cycle = 0;
|
|
|
-// UBYTE ubRdCnt = 2;
|
|
|
-
|
|
|
-// UBYTE i2c_timeout_flag = 0;
|
|
|
-// UBYTE *p_buffer;
|
|
|
-
|
|
|
-// while(ubRdCnt != 0)
|
|
|
-// {
|
|
|
-// ubRdCnt--;
|
|
|
-
|
|
|
-// if (ubRdCnt == 1)
|
|
|
-// {
|
|
|
-// ubRdSlaveAddr = I2C_SLAVEADDR_BLOCK1;
|
|
|
-// ubRdAddr = 0x00;
|
|
|
-// ubRdNBytes = I2C_RX1_NBYTES;
|
|
|
-// p_buffer = I2C_ubReadBuffer[0];
|
|
|
-// i2c_timeout_flag = 0;
|
|
|
-// }
|
|
|
-// else if (ubRdCnt == 0)
|
|
|
-// {
|
|
|
-// ubRdSlaveAddr = I2C_SLAVEADDR_BLOCK3;
|
|
|
-// ubRdAddr = 0x00;
|
|
|
-// ubRdNBytes = I2C_RX2_NBYTES;
|
|
|
-// p_buffer = I2C_ubReadBuffer[1];
|
|
|
-// i2c_timeout_flag = 0;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// //do nothing
|
|
|
-// }
|
|
|
-
|
|
|
-// /* enable acknowledge */
|
|
|
-// i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
|
-
|
|
|
-// while(i2c_timeout_flag ==0)
|
|
|
-// {
|
|
|
-// switch(state)
|
|
|
-// {
|
|
|
-// case I2C_START:
|
|
|
-// if(RESET == read_cycle)
|
|
|
-// {
|
|
|
-// /* disable I2C0 */
|
|
|
-// i2c_disable(I2C0);
|
|
|
-// /* enable I2C0 */
|
|
|
-// i2c_enable(I2C0);
|
|
|
-// /* enable acknowledge */
|
|
|
-// i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
|
-// /* i2c master sends start signal only when the bus is idle */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (timeout < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// timeout++;
|
|
|
-// }
|
|
|
-
|
|
|
-// if(timeout < I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// /* send the start signal */
|
|
|
-// i2c_start_on_bus(I2C0);
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_SEND_ADDRESS;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// i2c_bus_reset();
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// i2c_start_on_bus(I2C0);
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_SEND_ADDRESS;
|
|
|
-// }
|
|
|
-// break;
|
|
|
-// case I2C_SEND_ADDRESS:
|
|
|
-// /* i2c master sends START signal successfully */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (timeout < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// timeout++;
|
|
|
-// }
|
|
|
-// if(timeout < I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// if(RESET == read_cycle)
|
|
|
-// {
|
|
|
-// i2c_master_addressing(I2C0, ubRdSlaveAddr, I2C_TRANSMITTER);
|
|
|
-// state = (UBYTE)I2C_CLEAR_ADDRESS_FLAG;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// i2c_master_addressing(I2C0, ubRdSlaveAddr, I2C_RECEIVER);
|
|
|
-// state = (UBYTE)I2C_CLEAR_ADDRESS_FLAG;
|
|
|
-// }
|
|
|
-// timeout = 0;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// read_cycle = 0;
|
|
|
-// }
|
|
|
-// break;
|
|
|
-// case I2C_CLEAR_ADDRESS_FLAG:
|
|
|
-// /* address flag set means i2c slave sends ACK */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (timeout < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// timeout++;
|
|
|
-// }
|
|
|
-// if(timeout < I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_TRANSMIT_DATA;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// read_cycle = 0;
|
|
|
-// }
|
|
|
-// break;
|
|
|
-// case I2C_TRANSMIT_DATA:
|
|
|
-// if(RESET == read_cycle)
|
|
|
-// {
|
|
|
-// /* wait until the transmit data buffer is empty */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (timeout < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// timeout++;
|
|
|
-// }
|
|
|
-// if(timeout < I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// /* send the EEPROM's internal address to write to : only one byte address */
|
|
|
-// i2c_data_transmit(I2C0, ubRdAddr);
|
|
|
-// timeout = 0;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// read_cycle = 0;
|
|
|
-// }
|
|
|
-// /* wait until BTC bit is set */
|
|
|
-// while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (timeout < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// timeout++;
|
|
|
-// }
|
|
|
-// if(timeout < I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// read_cycle++;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// read_cycle = 0;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// /* one byte master reception procedure (polling) */
|
|
|
-// if(ubRdNBytes < 2)
|
|
|
-// {
|
|
|
-// /* disable acknowledge */
|
|
|
-// i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
|
|
-// /* clear ADDSEND register by reading I2C_STAT0 then I2C_STAT1 register (I2C_STAT0 has already been read) */
|
|
|
-// i2c_flag_get(I2C0, I2C_FLAG_ADDSEND);
|
|
|
-// /* send a stop condition to I2C bus*/
|
|
|
-// i2c_stop_on_bus(I2C0);
|
|
|
-// /* wait for the byte to be received */
|
|
|
-// while(i2c_flag_get(I2C0, I2C_FLAG_RBNE) == 0)
|
|
|
-// {
|
|
|
-// // do nothing
|
|
|
-// }
|
|
|
-// /* read the byte received from the EEPROM */
|
|
|
-// *p_buffer = i2c_data_receive(I2C0);
|
|
|
-// /* decrement the read bytes counter */
|
|
|
-// ubRdNBytes--;
|
|
|
-// timeout = 0;
|
|
|
-// state = (UBYTE)I2C_STOP;
|
|
|
-// }
|
|
|
-// else
|
|
|
-// { /* more than one byte master reception procedure (DMA) */
|
|
|
-
|
|
|
-// dma_transfer_number_config(DMA0, DMA_CH6, ubRdNBytes);
|
|
|
-// DMA_CH6MADDR(DMA0) = (ULONG)p_buffer;
|
|
|
-
|
|
|
-// i2c_dma_last_transfer_config(I2C0, I2C_DMALST_ON);
|
|
|
-// /* enable I2C0 DMA */
|
|
|
-// i2c_dma_config(I2C0, I2C_DMA_ON);
|
|
|
-// /* enable DMA0 channel5 */
|
|
|
-// dma_channel_enable(DMA0, DMA_CH6);
|
|
|
-// /* wait until BTC bit is set */
|
|
|
-// while(dma_flag_get(DMA0, DMA_CH6, DMA_FLAG_FTF) == 0)
|
|
|
-// {
|
|
|
-// // do nothing
|
|
|
-// }
|
|
|
-
|
|
|
-// state = (UBYTE)I2C_STOP;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// break;
|
|
|
-// case I2C_STOP:
|
|
|
-// /* send a stop condition to I2C bus */
|
|
|
-// i2c_stop_on_bus(I2C0);
|
|
|
-// /* i2c master sends STOP signal successfully */
|
|
|
-// while((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_SHORT_TIMEOUT))
|
|
|
-// {
|
|
|
-// timeout++;
|
|
|
-// }
|
|
|
-
|
|
|
-// if(timeout < I2C_SHORT_TIMEOUT)
|
|
|
-// {
|
|
|
-// timeout = 0;
|
|
|
-// i2c_timeout_flag = 1;
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// read_cycle = 0;
|
|
|
-
|
|
|
-// /* disable DMA0 CH6 */
|
|
|
-// dma_channel_disable(DMA0, DMA_CH6);
|
|
|
-// /* disable I2C0 DMA */
|
|
|
-// i2c_dma_config(I2C0, I2C_DMA_OFF);
|
|
|
-// i2c_dma_last_transfer_config(I2C0, I2C_DMALST_OFF);
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// timeout = 0;
|
|
|
-// //state = I2C_START;
|
|
|
-// state = (UBYTE)I2C_STOP;
|
|
|
-// read_cycle = 0;
|
|
|
-// }
|
|
|
-
|
|
|
-// break;
|
|
|
-// default:
|
|
|
-// state = (UBYTE)I2C_START;
|
|
|
-// read_cycle = 0;
|
|
|
-// i2c_timeout_flag = 1;
|
|
|
-// timeout = 0;
|
|
|
-
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
-// }
|
|
|
-
|
|
|
-// i2c_voReadBufferCRC(out);
|
|
|
-
|
|
|
-// i2c_voGetValueFrmBuffer(out);
|
|
|
-
|
|
|
-// out->ReadFinishFlg = TRUE;
|
|
|
-
|
|
|
-// }
|
|
|
-
|
|
|
void i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
|
|
|
{
|
|
|
UBYTE ubRdNBytes = 0, ubRdSlaveAddr = 0, ubRdAddr = 0;
|
|
@@ -1098,6 +353,7 @@ void i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out) /* parasoft-suppress METRICS-28
|
|
|
|
|
|
out->ReadFinishFlg = TRUE;
|
|
|
}
|
|
|
+
|
|
|
/*************************************************************************
|
|
|
Function:
|
|
|
Description:
|
|
@@ -1368,194 +624,8 @@ void i2c_voGetValueFrmBuffer(const I2C_RXCRC_OUT *out)
|
|
|
Syspara2.stHistoryPara.uwPosSensorAlamTimes.uwReal = I2C_uwHistoryParaRead[30];
|
|
|
|
|
|
}
|
|
|
-/*************************************************************************
|
|
|
- Function:
|
|
|
- Description:
|
|
|
- Call by:
|
|
|
- Input Variables:
|
|
|
- Output/Return Variables:
|
|
|
- Subroutine Call:
|
|
|
- Reference:
|
|
|
-*************************************************************************/
|
|
|
-void i2c_voWriteTest(void)
|
|
|
-{
|
|
|
- UBYTE WriteBuffer[4];
|
|
|
- UBYTE *pBuffer;
|
|
|
-
|
|
|
- WriteBuffer[0] = 0x06;
|
|
|
- WriteBuffer[1] = 0x08;
|
|
|
- WriteBuffer[2] = 0x0A;
|
|
|
- WriteBuffer[3] = 0x01;
|
|
|
- pBuffer = WriteBuffer;
|
|
|
- i2c_voPageWrite2EE(pBuffer, I2C_SLAVEADDR_BLOCK1, 0x00, 4);
|
|
|
-}
|
|
|
|
|
|
-/*************************************************************************
|
|
|
- Function:
|
|
|
- Description:
|
|
|
- Call by:
|
|
|
- Input Variables:
|
|
|
- Output/Return Variables:
|
|
|
- Subroutine Call:
|
|
|
- Reference:
|
|
|
-*************************************************************************/
|
|
|
-void i2c_voReadTest(void) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
|
|
|
-{
|
|
|
- UBYTE ReadBuffer[6];
|
|
|
-
|
|
|
- ULONG ulTimeCnt = 0;
|
|
|
- UWORD uwTimeoutNum = 0;
|
|
|
- UWORD uwReadNBytes = 6;
|
|
|
-
|
|
|
- /* wait until I2C bus is idle */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* send a start condition to I2C bus */
|
|
|
- i2c_start_on_bus(I2C0);
|
|
|
- /* wait until SBSEND bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* send slave address to I2C bus */
|
|
|
- i2c_master_addressing(I2C0, I2C_SLAVEADDR_BLOCK1, I2C_TRANSMITTER);
|
|
|
- /* wait until ADDSEND bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* clear ADDSEND bit */
|
|
|
- i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
|
- /* wait until the TBE bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* readAddr transmission */
|
|
|
- i2c_data_transmit(I2C0, 0x00);
|
|
|
- /* wait until the BTC bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
-
|
|
|
- /* send a start condition to I2C bus */
|
|
|
- i2c_start_on_bus(I2C0);
|
|
|
- /* wait until SBSEND bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* send slave address to I2C bus */
|
|
|
- i2c_master_addressing(I2C0, I2C_SLAVEADDR_BLOCK1, I2C_RECEIVER);
|
|
|
- /* wait until ADDSEND bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0)&& (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* clear ADDSEND bit */
|
|
|
- i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
|
-
|
|
|
- while(uwReadNBytes != 0)
|
|
|
- {
|
|
|
- uwReadNBytes--;
|
|
|
- if(2 == uwReadNBytes)
|
|
|
- {
|
|
|
- /* wait until BTC bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* disable acknowledge */
|
|
|
- i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
|
|
- }
|
|
|
- if(1 == uwReadNBytes)
|
|
|
- {
|
|
|
- /* wait until BTC bit is set */
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- /* send a stop condition to I2C bus */
|
|
|
- i2c_stop_on_bus(I2C0);
|
|
|
- }
|
|
|
-
|
|
|
- while((i2c_flag_get(I2C0, I2C_FLAG_RBNE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
- ReadBuffer[5 - uwReadNBytes] = i2c_data_receive(I2C0);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /* wait until stop condition generate */
|
|
|
- while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
|
|
|
- {
|
|
|
- ulTimeCnt++;
|
|
|
- }
|
|
|
- if(ulTimeCnt == I2C_SHORT_TIMEOUT)
|
|
|
- {
|
|
|
- uwTimeoutNum ++;
|
|
|
- }
|
|
|
- ulTimeCnt = 0;
|
|
|
-
|
|
|
- if(uwTimeoutNum > 3)
|
|
|
- {
|
|
|
- I2C_EE_ComuFltFlg = TRUE;
|
|
|
- }
|
|
|
-}
|
|
|
+
|
|
|
/*************************************************************************
|
|
|
Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
|
|
|
All rights reserved.
|