Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IIC 驱动无法使用 #20

Open
songzhishuo opened this issue Jul 18, 2023 · 1 comment
Open

IIC 驱动无法使用 #20

songzhishuo opened this issue Jul 18, 2023 · 1 comment

Comments

@songzhishuo
Copy link

测试了下IIC 的驱动,并无法使用,只能发送Addr 后面的buf数据无法发送出来

@deividAlfa
Copy link
Contributor

deividAlfa commented Jul 27, 2023

This is expected. You cannot send data to i2c devices without sending the address first.
This works for me:

ErrorStatus I2C1_MasterTransmit(uint8_t DevAddress, uint8_t *pData, uint16_t Size)
{
  ErrorStatus status = SUCCESS;
  LL_I2C_DisableBitPOS(I2C1);                                                           // Master mode                      

  LL_I2C_GenerateStartCondition(I2C1);                                                  // Send start
  while(LL_I2C_IsActiveFlag_SB(I2C1) != 1);                                             // Wait for Start

  LL_I2C_TransmitData8(I2C1, (DevAddress & (uint8_t)(~0x01)));                          // Send device address, RW=0
  while(LL_I2C_IsActiveFlag_ADDR(I2C1) != 1 || LL_I2C_IsActiveFlag_TXE(I2C1) != 1){     // Wait for ADDR and TXE bits to be 1
    if(LL_I2C_IsActiveFlag_AF(I2C1))                                                    // Check if got ACK failure
      break;
  }
  LL_I2C_ClearFlag_ADDR(I2C1);                                                          // Clear ADDR bit
  if(LL_I2C_IsActiveFlag_AF(I2C1)){                                                     // If NACK received
    status = ERROR;                                                                     // Error status
    LL_I2C_ClearFlag_AF(I2C1);                                                          // Clear AF bit
  }
  else{                                                                                 // Got ACK
    while(Size--) {
      LL_I2C_TransmitData8(I2C1, *pData++);                                             // Send data
      while(LL_I2C_IsActiveFlag_TXE(I2C1) != 1);                                        // Wait for TX buffer ready
      if(LL_I2C_IsActiveFlag_AF(I2C1)){                                                 // Check for NACK
        status = ERROR;                                                                 // Got NACK, stop transfer
        break;
      }
    }
  }
  LL_I2C_GenerateStopCondition(I2C1);                                                   // Send Stop
  while(LL_I2C_IsActiveFlag_MSL(I2C1) != 0);                                            // Wait until MSL bit is cleared
  return status;                                                                        // Return status
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants