写寄存器
uint8_t BSP_IIC_WriteReg(uint8_t ucChannel, uint16_t DevAddress, uint16_t Reg, uint8_t *pData, uint16_t usLen, uint32_t ulTimeout)
{
uint8_t Ret = TRUE;
uint16_t XferCount = usLen;
uint8_t *TxBuffer = pData;
uint16_t XferSize = 0;
/*等待I2C总线空闲*/
while(((I2C1->ISR) & I2C_ISR_BUSY) == I2C_ISR_BUSY);
/* Send Slave Address and Memory Address */
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, I2C_MEMADD_SIZE_8BIT, I2C_CR2_RELOAD , LL_I2C_GENERATE_START_WRITE);
while(((I2C1->ISR) & I2C_ISR_TXIS) != I2C_ISR_TXIS);
/* Send Memory Address */
I2C1->TXDR = I2C_MEM_ADD_LSB(Reg);
while(((I2C1->ISR) & I2C_ISR_TCR) != I2C_ISR_TCR);
/* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
if (XferCount > MAX_NBYTE_SIZE)
{
XferSize = MAX_NBYTE_SIZE;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_RELOAD , I2C_NO_STARTSTOP);
}
else
{
XferSize = XferCount;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_AUTOEND , I2C_NO_STARTSTOP);
}
do
{
/* Wait until TXIS flag is set */
while(((I2C1->ISR) & I2C_ISR_TXIS) != I2C_ISR_TXIS);
/* Write data to TXDR */
I2C1->TXDR = *TxBuffer;
/* Increment Buffer pointer */
TxBuffer++;
XferCount--;
XferSize--;
if ((XferCount != 0U) && (XferSize == 0U))
{
/* Wait until TCR flag is set */
while(((I2C1->ISR) & I2C_ISR_TCR) != I2C_ISR_TCR);
if (XferCount > MAX_NBYTE_SIZE)
{
XferSize = MAX_NBYTE_SIZE;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_RELOAD , I2C_NO_STARTSTOP);
}
else
{
XferSize = XferCount;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_AUTOEND , I2C_NO_STARTSTOP);
}
}
}
while(XferCount > 0U);
/* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
/* Wait until STOPF flag is reset */
while(((I2C1->ISR) & I2C_ISR_STOPF) != I2C_ISR_STOPF);
/* Clear NACKF Flag */
__HAL_I2C_CLEAR_FLAG(I2C1, I2C_ISR_NACKF);
/* Clear STOP Flag */
LL_I2C_ClearFlag_STOP(I2C1);
/* Clear Configuration Register 2 */
I2C1->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN));
return Ret;
}
读寄存器:
uint8_t BSP_IIC_ReadReg(uint8_t ucChannel, uint16_t DevAddress, uint16_t Reg, uint8_t *pData, uint16_t usLen, uint32_t ulTimeout)
{
uint8_t Ret = TRUE;
uint16_t XferCount = usLen;
uint8_t *TxBuffer = pData;
uint16_t XferSize = 0;
/*等待I2C总线空闲*/
while(((I2C1->ISR) & I2C_ISR_BUSY) == I2C_ISR_BUSY);
/* Send Slave Address and Memory Address */
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, I2C_MEMADD_SIZE_8BIT, LL_I2C_MODE_SOFTEND, I2C_GENERATE_START_WRITE);
while(((I2C1->ISR) & I2C_ISR_TXIS) != I2C_ISR_TXIS);
/* Send Memory Address */
I2C1->TXDR = I2C_MEM_ADD_LSB(Reg);
while(((I2C1->ISR) & I2C_ISR_TC) != I2C_ISR_TC);
/* Send Slave Address */
/* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
if (XferCount > MAX_NBYTE_SIZE)
{
XferSize = MAX_NBYTE_SIZE;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_RELOAD , I2C_GENERATE_START_READ);
}
else
{
XferSize = XferCount;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_AUTOEND ,I2C_GENERATE_START_READ);
}
do
{
/* Wait until RXNE flag is set */
while(((I2C1->ISR) & I2C_ISR_RXNE) != I2C_ISR_RXNE);
/* Read data from RXDR */
*TxBuffer = (U8)((I2C1->RXDR >> 0) & 0xFF);
/* Increment Buffer pointer */
TxBuffer++;
XferCount--;
XferSize--;
if ((XferCount != 0U) && (XferSize == 0U))
{
/* Wait until TCR flag is set */
while(((I2C1->ISR) & I2C_ISR_TCR) != I2C_ISR_TCR);
if (XferCount > MAX_NBYTE_SIZE)
{
XferSize = MAX_NBYTE_SIZE;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_RELOAD , I2C_NO_STARTSTOP);
}
else
{
XferSize = XferCount;
LL_I2C_HandleTransfer(I2C1, DevAddress, LL_I2C_ADDRSLAVE_7BIT, (uint8_t)XferSize, I2C_CR2_AUTOEND , I2C_NO_STARTSTOP);
}
}
}
while(XferCount > 0U);
/* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
/* Wait until STOPF flag is reset */
while(((I2C1->ISR) & I2C_ISR_STOPF) != I2C_ISR_STOPF)
{
}
/* Clear NACKF Flag */
__HAL_I2C_CLEAR_FLAG(I2C1, I2C_ISR_NACKF);
/* Clear STOP Flag */
LL_I2C_ClearFlag_STOP(I2C1);
/* Clear Configuration Register 2 */
I2C1->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN));
return Ret;
}