如何将Arduino Wire(I2C)移植到STM32 HAL?

时间:2018-12-06 16:51:41

标签: arduino stm32 i2c hal

我正在尝试为STM32F4 Nucleo板(用于记录的PN532 NFC控制器)翻译Arduino库。

我坚持以下代码:

void NFC_Module::read_dt(uint8_t *buf, uint8_t len)
{
  delay(2);

  // Start read (n+1 to take into account leading 0x01 with I2C)
  Wire.requestFrom((u8)PN532_I2C_ADDRESS, (uint8_t)(len+2));
  // Discard the leading 0x01
  receive();
  for (uint8_t i=0; i<len; i++) {
    delay(1);
    buf[i] = receive();
  }
}

这是我当前的“ HAL版本”:

static uint8_t i2c_read(uint8_t reg)
{
  uint8_t ret = 0;
  HAL_I2C_Mem_Read(&hi2c2, PN532_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, &ret, 1, 1000);
  return ret;
}

void read_dt(uint8_t *buf, uint8_t len)
{
    HAL_Delay(10);

    // Start read (n+1 to take into account leading 0x01 with I2C)
    //Wire.requestFrom((uint8_t)PN532_I2C_ADDRESS, (uint8_t)(len+2));
    // Discard the leading 0x01
        i2c_read((len+2));     //receive();

    for (uint8_t i=0; i<len; i++)
    {
        HAL_Delay(1);
        buf[i] = i2c_read((len+2));     //receive();
    }
}

这可能是错误的,因为我不了解Arduino Wire中没有'register'参数的事实。

我们将很乐意接受任何帮助和/或更正,非常感谢!

0 个答案:

没有答案