设置 I2C 通信

Set up I2C communications

我正在尝试在 nucleo STM32F466RE 板上的两个 Adafruit TOF 传感器 VL6180X 之间设置 I²C 通信。我正在使用 cubeMX 和 VS 代码,还帮助 ST website 的 x-nucleo-6180xa1 上的 VL6180X API,我想设置它以便我可以分别测试两个传感器。到目前为止,我只成功测量了一个传感器,但是当我尝试将两个传感器连接到 SHDN 到 nucleo STM32F466RE 上的 GPIO 时,我遇到了问题。我正在尝试管理一些主从操作,但我是这方面的新手,有人知道怎么做吗?

我要找的是示例代码,这样我就可以看到它是如何实现的。这不是制作 运行,而是供我家庭使用的。目标是在这两个TOF传感器上进行管理,我一直没能找到代码研究。

这是我的主要代码:

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

void WaitMilliSec(int ms);

VL6180xDev_t theVL6180xDev;
struct MyVL6180Dev_t BoardDevs[2] = { 
                                        [0]= { .DevID = 0 }, 
                                        [1]= { .DevID = 1 } 
                                    };

VL6180xDev_t theVL6180xDev = &BoardDevs[0];

/**
 * VL6180x CubeMX F401 multiple device i2c implementation
 */

#define i2c_bus      (&hi2c1)
#define def_i2c_time_out 100

int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
    int status;
    status = HAL_I2C_Master_Transmit(i2c_bus, dev->I2cAddr, buff, len, def_i2c_time_out);
    if (status) {
        HAL_I2C_MspInit(&hi2c1);
    }
    return status? -1 : 0;
}

int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
    int status;
    status = HAL_I2C_Master_Receive(i2c_bus, dev->I2cAddr, buff, len, def_i2c_time_out);
    if (status) {
        HAL_I2C_MspInit(&hi2c1);
    }

    return status? -1 : 0;
}

void WaitMilliSec(int ms) {
    HAL_Delay(ms); /* it's milli sec  cos we do set systick to 1KHz */
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  #define MAX_DEV 1
  VL6180x_RangeData_t Range[MAX_DEV];

  int status;
  int i;
  int n_dev=1;
  int PresentDevMask;
  int nPresentDevs;
  int PresentDevIds[MAX_DEV];
  int nReady;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  
  /* detect presence and initialize devices i2c address  */
    /*set device i2c address for dev[i] = 0x52+(i+1)*2 */
    PresentDevMask = 0;
    nPresentDevs = 0;
    //strcpy(DisplayStr,"TLBR");
    for (i = 0; i <= n_dev; i++){
        int FinalI2cAddr;
        uint8_t id;
        /* unreset device that wake up at default i2c addres 0x52 */
        WaitMilliSec(2);    /* at least 400usec before to acces device */
        BoardDevs[i].I2cAddr = 0x52;
        /* to detect device presence try to read it's dev id */
        status = VL6180x_RdByte(&BoardDevs[i], IDENTIFICATION_MODEL_ID, &id);
        if (status) {
            /* these device is not present skip init and clear it's letter on string */
            BoardDevs[i].Present = 0;
            //DisplayStr[i]=' ';
            continue;
        }

        /* device present only */
        BoardDevs[i].Present = 1;
        PresentDevMask |= 1 << i;
        PresentDevIds[nPresentDevs]=i;
        nPresentDevs++;
        status = VL6180x_InitData(&BoardDevs[i]);

        FinalI2cAddr = 0x52 + ((i+1) * 2);
        if (FinalI2cAddr != 0x52) {
            status = VL6180x_SetI2CAddress(&BoardDevs[i], FinalI2cAddr);
            if( status ){
                //HandleError("VL6180x_SetI2CAddress fail");
            }
            BoardDevs[i].I2cAddr = FinalI2cAddr;
        }

        WaitMilliSec(1);
        status = VL6180x_RdByte(&BoardDevs[i], IDENTIFICATION_MODEL_ID, &id);
        WaitMilliSec(1);
        status= VL6180x_Prepare(&BoardDevs[i]);
        if( status<0 ){
            //HandleError("VL6180x_Prepare fail");
        }
        /* Disable Dmax computation */
        VL6180x_DMaxSetState(&BoardDevs[i], 0);
    }
    
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
    VL6180xDev_t dev;
    /*
    VL6180x_RangePollMeasurement(dev, &Range[0]);
    HAL_Delay(20);
    */
        // kick off measure on all device 
        for( i=0; i<nPresentDevs; i++){

            dev =  BoardDevs + PresentDevIds[i];
            //TODO: GPIO enamble of pa11
            status = VL6180x_RangeStartSingleShot(dev);
            if( status<0 ){
                //HandleError("VL6180x_RangeStartSingleShot fail");
            }
            dev->Ready=0;
        }
        // wait for all present device to have a measure  
        nReady=0;
        do{
            //DISP_ExecLoopBody();
            for( i=0; i<nPresentDevs; i++){
                dev =  BoardDevs + PresentDevIds[i];
                if( !dev->Ready ){
                    status = VL6180x_RangeGetMeasurementIfReady(dev, &Range[i]);
                    if( status == 0 ){
                        if(Range[i].errorStatus == DataNotReady)
                            continue;
                        // New measurement ready 
                        dev->Ready=1;
                        nReady++;
                    } else {
                        //HandleError("VL6180x_RangeStartSingleShot fail");
                    }
                }
            }
        }
        while( nReady<nPresentDevs);
        
      

    /* USER CODE END WHILE */
   
    
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

...

我进入这类问题的方式如下。

  • 从任何来源下载此传感器的 Arduino 库。 here
  • 仔细阅读源文件,找出使用i2c通信的核心函数。
  • 根据您的任务修改该核心函数。(实施适当的 i2c 调用)
  • 将修改后的头文件和源文件包含到您的项目中并使用它们。

我解决了我的问题。您可能知道,要让几个 Adafruit TOF VL6180X 传感器一起工作,它们必须从它们的 SHDN 连接到板上的 GPIO。实际上缺少的是所有传感器的重置和这些传感器及其地址的设置。我在寻找解决方案时遇到的问题之一是命令的顺序。重要的是要注意,我们首先重置所有传感器,并在分配地址后设置。如果您也有兴趣,请附上对代码的更正。您可以在依赖于循环中的变量 i 的代码中看到 if 查询,以便分隔两个传感器。我这样做只是为了找到特定的组件。有一个更聪明的方法来做,它只是为了理解命令的顺序。

在评论中查看我的解决方案:

<<<<-------------------------RESET-----

<<<<-------------------------SET-----

/* USER CODE BEGIN PV */
// global vars only for debugging with STMStudio
VL6180x_RangeData_t Range[MAX_DEV];
VL6180x_RangeData_t RangeDB1;
VL6180x_RangeData_t RangeDB2;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

void WaitMilliSec(int ms);

VL6180xDev_t theVL6180xDev;
struct MyVL6180Dev_t BoardDevs[MAX_DEV] = { 
                                        [0]= { .DevID = 0 }, 
                                        [1]= { .DevID = 1 }, 
                                    };

VL6180xDev_t theVL6180xDev = &BoardDevs[0];


/**
 * VL6180x CubeMX F401 multiple device i2c implementation
 */

#define i2c_bus      (&hi2c1)
#define def_i2c_time_out 100


int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
    int status;
    status = HAL_I2C_Master_Transmit(i2c_bus, dev->I2cAddr, buff, len, def_i2c_time_out);
    if (status) {
        HAL_I2C_MspInit(&hi2c1);
    }
    return status? -1 : 0;
}

int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
    int status;
    status = HAL_I2C_Master_Receive(i2c_bus, dev->I2cAddr, buff, len, def_i2c_time_out);
    if (status) {
        HAL_I2C_MspInit(&hi2c1);
    }

    return status? -1 : 0;
}

void WaitMilliSec(int ms) {
    HAL_Delay(ms); /* it's milli sec  cos we do set systick to 1KHz */
}



/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  

  int status;
  int i;
  int n_dev=2;
  int PresentDevMask;
  int nPresentDevs;
  int PresentDevIds[MAX_DEV];
  int nReady;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  // here I put all my devices under reset <<<<-------------------------RESET----- 
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);


  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */


  /* detect presence and initialize devices i2c address  */
  /*set device i2c address for dev[i] = 0x52+(i+1)*2 */
  PresentDevMask = 0;
  nPresentDevs = 0;
  //strcpy(DisplayStr,"TLBR");
  for (i = 0; i <= n_dev - 1; i++) {
      int FinalI2cAddr;
      uint8_t id;
      // here I put all my devices under set <<<<-------------------------SET-----
      if (i==0){
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
      }
      if (i==1){
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
      }
      /* unreset device that wake up at default i2c addres 0x52 */
      WaitMilliSec(2);    /* at least 400usec before to acces device */
      BoardDevs[i].I2cAddr = 0x52;
      /* to detect device presence try to read it's dev id */
      status = VL6180x_RdByte(&BoardDevs[i], IDENTIFICATION_MODEL_ID, &id);
      if (status) {
          /* these device is not present skip init and clear it's letter on string */
          /*we can add here some debug prints*/
          //BoardDevs[i].Present = 0;
          //DisplayStr[i]=' ';
          continue;
      }
      /* device present only */
      BoardDevs[i].Present = 1;
      PresentDevMask |= 1 << i;
      PresentDevIds[nPresentDevs]=i;
      nPresentDevs++;
      status = VL6180x_InitData(&BoardDevs[i]);

      FinalI2cAddr = 0x52 + ((i+1) * 2);
      if (FinalI2cAddr != 0x52) {
          status = VL6180x_SetI2CAddress(&BoardDevs[i], FinalI2cAddr);
          if( status ){
              //HandleError("VL6180x_SetI2CAddress fail");
          }
          BoardDevs[i].I2cAddr = FinalI2cAddr;
      }

      WaitMilliSec(1);
      status = VL6180x_RdByte(&BoardDevs[i], IDENTIFICATION_MODEL_ID, &id);
      WaitMilliSec(1);
      status= VL6180x_Prepare(&BoardDevs[i]);
      if( status<0 ){
          //HandleError("VL6180x_Prepare fail");
      }
      /* Disable Dmax computation */
      VL6180x_DMaxSetState(&BoardDevs[i], 0);

    }
    
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    VL6180xDev_t dev;
    
    // kick off measure on all device 
    for( i=0; i<nPresentDevs; i++){
        dev =  BoardDevs + PresentDevIds[i];
        status = VL6180x_RangeStartSingleShot(dev);
        if( status<0 ){
            //TODO: print error : debug
        }
        dev->Ready=0;
    }

    // wait for all present device to have a measure  
    nReady=0;
    
    do{
        //DISP_ExecLoopBody();
        for( i=0; i<nPresentDevs; i++){
            dev =  BoardDevs + PresentDevIds[i];
            if( !dev->Ready ){
                status = VL6180x_RangeGetMeasurementIfReady(dev, &Range[i]);
                if (i==0)
                  RangeDB1 = Range[i];
                if (i==1)
                  RangeDB2 = Range[i];
                if( status == 0 ){
                    if(Range[i].errorStatus == DataNotReady)
                        continue;
                    // New measurement ready 
                    dev->Ready=1;
                    nReady++;
                } else {
                    //HandleError("VL6180x_RangeStartSingleShot fail");
                }
            }
        }
    }
    while( nReady<nPresentDevs);
    
    HAL_Delay(10);
      
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}