cancel
Showing results for 
Search instead for 
Did you mean: 

BMP280 I2C is not updating

Bal�zs Gyenge
Associate II

Hi,

I am using stm32l432kc nucleo and BMP280 sensor with I2c communication.

During the debugging I realized that the data from the sensor is not updating, always 2481 (temp32 variable).

Could you help me in this topic?

Thank you in advance.

Info:

Used driver:

https://github.com/BoschSensortec/BMP280_driver

BMP280 application file:

#include "bmp280_defs.h"
#include "bmp280_user.h"
 
extern I2C_HandleTypeDef hi2c1;
 
HAL_StatusTypeDef status = HAL_OK;
 
/*!
 *  @brief Function that creates a mandatory delay required in some of the APIs such as "bmg250_soft_reset",
 *      "bmg250_set_foc", "bmg250_perform_self_test"  and so on.
 *
 *  @param[in] period_ms  : the required wait time in milliseconds.
 *  @return void.
 *
 */
void delay_ms(uint32_t period_ms)
{
	HAL_Delay(period_ms);
}
 
/*!
 *  @brief Function for writing the sensor's registers through I2C bus.
 *
 *  @param[in] i2c_addr : sensor I2C address.
 *  @param[in] reg_addr : Register address.
 *  @param[in] reg_data : Pointer to the data buffer whose value is to be written.
 *  @param[in] length   : No of bytes to write.
 *
 *  @return Status of execution
 *  @retval 0 -> Success
 *  @retval >0 -> Failure Info
 *
 */
int8_t i2c_reg_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
{
	int8_t rslt = BMP280_OK;
	
	status = HAL_I2C_Mem_Write(&hi2c1, (uint8_t)(i2c_addr<<1), (uint8_t)reg_addr, I2C_MEMADD_SIZE_8BIT, (uint8_t*)(&reg_data), length, 10000);
 
	if (status != HAL_OK)
  {
		rslt = BMP280_E_COMM_FAIL; 
  }
	
  return rslt;
}
 
/*!
 *  @brief Function for reading the sensor's registers through I2C bus.
 *
 *  @param[in] i2c_addr : Sensor I2C address.
 *  @param[in] reg_addr : Register address.
 *  @param[out] reg_data    : Pointer to the data buffer to store the read data.
 *  @param[in] length   : No of bytes to read.
 *
 *  @return Status of execution
 *  @retval 0 -> Success
 *  @retval >0 -> Failure Info
 *
 */
int8_t i2c_reg_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
{
	int8_t rslt = BMP280_OK;
	uint8_t array[28] = {0};
	uint8_t stringpos = 0;
	array[0] = reg_addr;
	
	status = HAL_I2C_Mem_Read(&hi2c1, (uint8_t)(i2c_addr<<1), (uint8_t)reg_addr, I2C_MEMADD_SIZE_8BIT, (uint8_t*)(&array), length, 10000);
	
	for (stringpos = 0; stringpos < length; stringpos++)
	{
		*(reg_data + stringpos)  = array[stringpos];
	}
	
	if (status != HAL_OK)
  {
		rslt = BMP280_E_COMM_FAIL; 
  }
	
  return rslt;
}

main.c:

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "init.h"
#include "cmsis_os.h"
#include "Adafruit_INA219.h"
#include "bmp280.h"
#include "bmp280_user.h"
#include <stdio.h>
 
/* Private includes ----------------------------------------------------------*/
 
/* Private typedef -----------------------------------------------------------*/
 
/* Private define ------------------------------------------------------------*/
 
/* Private macro -------------------------------------------------------------*/
 
/* Private variables ---------------------------------------------------------*/
extern I2C_HandleTypeDef hi2c1;
extern UART_HandleTypeDef huart2;
 
 
// Task handles
//TaskHandle_t Task1HandleIna219 = NULL;
TaskHandle_t TanskHandleBmp280 = NULL;
 
/* Private function prototypes -----------------------------------------------*/
//static void Ina219(void * argument);
static void Bmp280(void * argument);
 
/* Private user code ---------------------------------------------------------*/
 
// data from INA219
//float current, busVoltage, shuntVoltage, power;
 
// data from BMP280
int8_t rslt;
struct bmp280_dev bmp;
struct bmp280_config conf;
struct bmp280_uncomp_data ucomp_data;
int32_t temp32;
double temp;	
	
	
 
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
	
	bmp.delay_ms = delay_ms;
	bmp.dev_id = BMP280_I2C_ADDR_PRIM;
	bmp.intf = BMP280_I2C_INTF;
	bmp.read = i2c_reg_read;
	bmp.write = i2c_reg_write;
	
	rslt = bmp280_init(&bmp);
	rslt = bmp280_get_config(&conf, &bmp);
	
	conf.filter = BMP280_FILTER_COEFF_2;
	conf.os_temp = BMP280_OS_4X;
	conf.os_pres = BMP280_OS_NONE;
	conf.odr = BMP280_ODR_1000_MS;
	
	rslt = bmp280_set_config(&conf, &bmp);
	
	rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, &bmp);
	
	/* IN219 calibration */
	//setCalibration_32V_1A();
 
  /* Create the thread(s) */
  //xTaskCreate(Ina219, "Ina219", configMINIMAL_STACK_SIZE, NULL, 0, &Task1HandleIna219);
	xTaskCreate(Bmp280, "Bmp280", configMINIMAL_STACK_SIZE, NULL, 0, &TanskHandleBmp280);
	
  /* Start scheduler */
  vTaskStartScheduler();
  
  /* We should never get here as control is now taken by the scheduler */
 
  /* Infinite loop */
  while (1)
  {
  }
}
 
 
/**
  * @brief  Ina219 thread.
  * @param  argument: Not used 
  * @retval 
  */
/*static void Ina219(void * argument)
{
  while(1)
  {
		// Show instantaneous measurements values
		current = getCurrent_mA();
		//busVoltage = getBusVoltage_V();
		//shuntVoltage = getShuntVoltage_mV();
		//power = getPower_mW();
		
		printf("current = %.2f \n\r", current);
		
    osDelay(1000);
  } 
}
*/
/**
  * @brief  Bmp280 thread.
  * @param  argument: Not used 
  * @retval 
  */
static void Bmp280(void * argument)
{
	while(1)
	{		
		rslt = bmp280_get_uncomp_data(&ucomp_data, &bmp);
		rslt = bmp280_get_comp_temp_32bit(&temp32, ucomp_data.uncomp_temp, &bmp);
		rslt = bmp280_get_comp_temp_double(&temp, ucomp_data.uncomp_temp, &bmp);
		
		printf("temp = %.f \n\r", temp);
		
		vTaskDelay(1000);
		
		taskYIELD();	
	}
}
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Instance == TIM1) {
    HAL_IncTick();
  }
}
 
 
/**
 * @brief  Retargets the C library printf function to the USART.
 * @param  None
 * @retval None
 */
int fputc(int ch, FILE *f)
{
	 HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
 
	 return ch;
}
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* User can add his own implementation to report the HAL error return state */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(char *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 REPLIES 0