cancel
Showing results for 
Search instead for 
Did you mean: 

How to setup I2C

TVoge
Associate II

I am trying to connect the VL53L1X distance sensor to a B-L072z-lrwan dicoboard so it can send data using the lorawan stack .

This is my first project using a hal and i cant get the I2C connection working.

The output to my serial monitor is: 536872016,status

My code right now:

GPIO init

static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
	GPIO_InitStruct.Pin = GPIO_PIN_9;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
	GPIO_InitStruct.Pin = GPIO_PIN_8;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
}

I2C init:

static void MX_I2C1_Init(void)
{
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x00300F38;
  hi2c1.Init.OwnAddress1 = 164;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
}

ranging function

void AutonomousLowPowerRangingTest(void)
{
	static VL53L1_RangingMeasurementData_t RangingData;
	//PRINTF("Autonomous Ranging Test\n");
		  status = VL53L1_WaitMeasurementDataReady(Dev);
			if(!status)
			{
				status = VL53L1_GetRangingMeasurementData(Dev, &RangingData);
				//status =0;
				PRINTF("%d,status\n");
				if(status==0){
					
					PRINTF("%d,%d,%.2f,%.2f\n", RangingData.RangeStatus,RangingData.RangeMilliMeter,
									(RangingData.SignalRateRtnMegaCps/65536.0),RangingData.AmbientRateRtnMegaCps/65336.0);
				
					}
				status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
	
				}
//  return status;
}

Main:

int main( void )
{
  /* STM32 HAL library initialization*/
  HAL_Init();
  
  /* Configure the system clock*/
  SystemClock_Config();
  
  /* Configure the debug mode*/
  DBG_Init();
  
  /* Configure the hardware*/
  HW_Init();
	
	
  /* USER CODE BEGIN 1 */
 	
	// initialize vl53l1x communication parameters
 
	MX_GPIO_Init();
	MX_I2C1_Init();
	HAL_I2C_Init(&hi2c1);
	HAL_I2C_MspInit(&hi2c1);
	
	
	dev.I2cHandle = &hi2c1;
  dev.I2cDevAddr = 0x52;
	
	
	/*** VL53L1X Initialization ***/
  VL53L1_WaitDeviceBooted(Dev);
  VL53L1_DataInit(Dev);
  VL53L1_StaticInit(Dev);
  VL53L1_SetDistanceMode(Dev, VL53L1_DISTANCEMODE_MEDIUM);
  VL53L1_SetMeasurementTimingBudgetMicroSeconds(Dev, 50000);
  VL53L1_SetInterMeasurementPeriodMilliSeconds(Dev, 500);
  VL53L1_StartMeasurement(Dev);
	
 
  /* USER CODE END 1 */
  
  /*Disbale Stand-by mode*/
  LPM_SetOffMode(LPM_APPLI_Id , LPM_Disable );
  
  PRINTF("VERSION: %X\n\r", VERSION);
 
  
  /* Configure the Lora Stack*/
  LORA_Init( &LoRaMainCallbacks, &LoRaParamInit);
  
  LORA_Join();
 
  LoraStartTx( TX_ON_TIMER) ;
 
	SetPWR(Sensor, PWR_ON);
	
  while( 1 )
  {
  if (AppProcessRequest==LORA_SET)
   {
      /*reset notification flag*/
      AppProcessRequest=LORA_RESET;
	  /*Send*/
      Send( NULL );
    }
	if (LoraMacProcessRequest==LORA_SET)
    {
     /*reset notification flag*/
    LoraMacProcessRequest=LORA_RESET;
      LoRaMacProcess( );
    }
    /*If a flag is set at this point, mcu must not enter low power and must loop*/
    DISABLE_IRQ( );
    
    /* if an interrupt has occurred after DISABLE_IRQ, it is kept pending 
     * and cortex will not enter low power anyway  */
    if ((LoraMacProcessRequest!=LORA_SET) && (AppProcessRequest!=LORA_SET))
    {
#ifndef LOW_POWER_DISABLE
      LPM_EnterLowPower( );
#endif
    }
		
    ENABLE_IRQ();
    
    /* USER CODE BEGIN 2 */
	
		AutonomousLowPowerRangingTest();
 
		/* USER CODE END 2 */
  }
}

1 REPLY 1
TVoge
Associate II

After measuring the SCL and SDA on an osiloscope i found out that they are both set to 1.