Browse Source

Test for BME280.

Vladimir N. Shilov 4 years ago
parent
commit
fb65f0de50
3 changed files with 121 additions and 11 deletions
  1. 3 4
      Inc/bme280_defs.h
  2. 3 6
      Inc/main.h
  3. 115 1
      Src/main.c

+ 3 - 4
Inc/bme280_defs.h

@@ -104,13 +104,12 @@
 /********************************************************/
 
 #ifndef BME280_FLOAT_ENABLE
-
 /* #define BME280_FLOAT_ENABLE */
 #endif
 
 #ifndef BME280_FLOAT_ENABLE
 #ifndef BME280_64BIT_ENABLE
-#define BME280_64BIT_ENABLE
+/* #define BME280_64BIT_ENABLE */
 #endif
 #endif
 
@@ -122,8 +121,8 @@
 #endif
 
 /**\name I2C addresses */
-#define BME280_I2C_ADDR_PRIM              UINT8_C(0x76)
-#define BME280_I2C_ADDR_SEC               UINT8_C(0x77)
+#define BME280_I2C_ADDR_PRIM              UINT8_C(0x76 << 1)
+#define BME280_I2C_ADDR_SEC               UINT8_C(0x77 << 1)
 
 /**\name BME280 chip identifier */
 #define BME280_CHIP_ID                    UINT8_C(0x60)

+ 3 - 6
Inc/main.h

@@ -50,6 +50,7 @@ extern "C" {
 /* USER CODE BEGIN Includes */
 #include "gpio.h"
 #include "ds3231.h"
+#include "bme280.h"
 
 /* USER CODE END Includes */
 
@@ -70,7 +71,8 @@ volatile struct {
   uint32_t SPI_TX_End:  1;
   uint32_t I2C_TX_End:  1;
   uint32_t I2C_RX_End:  1;
-  uint32_t _reserv:    28;
+  uint32_t BME280:      1;
+  uint32_t _reserv:     27;
 } Flag;
 
 /* USER CODE END ET */
@@ -143,11 +145,6 @@ void Error_Handler(void);
 /* USER CODE BEGIN Private defines */
 #define USE_FULL_LL_DRIVER 1
 
-/* 0xd0 */
-#define I2C_ADDR_RTC      (0x68 << 1)
-/* 0x76 */
-#define I2C_ADDR_BME      (0x3b << 1)
-
 /* USER CODE END Private defines */
 
 #ifdef __cplusplus

+ 115 - 1
Src/main.c

@@ -72,6 +72,9 @@ static const uint16_t nixieCathodeMap[4][10] = {
 static const uint8_t nixieCathodeMask[4][2] = {{0x00, 0x3f}, {0xc0, 0x0f}, {0xf0, 0x03}, {0xc0, 0x00}};
 static uint8_t tubesBuffer[SPI_BUFFER_SIZE] = {0};
 static rtc_t Clock;
+static struct bme280_dev SensorDev;
+static struct bme280_data SensorData;
+static int8_t rsltSensor = BME280_OK;
 
 /* USER CODE END PV */
 
@@ -88,6 +91,8 @@ static void MX_TIM17_Init(void);
 /* USER CODE BEGIN PFP */
 static void showDigit(tube_pos_t pos, uint8_t dig);
 static void SPI_StartTX(void);
+static int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
+static int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
 /* USER CODE END PFP */
 
 /* Private user code ---------------------------------------------------------*/
@@ -177,12 +182,41 @@ int main(void)
 
   RTC_Init();
   while (Flag.I2C_TX_End == 0) { __NOP(); };
+
+  SensorDev.dev_id = BME280_I2C_ADDR_PRIM;
+  SensorDev.intf = BME280_I2C_INTF;
+  SensorDev.read = user_i2c_read;
+  SensorDev.write = user_i2c_write;
+  SensorDev.delay_ms = LL_mDelay;
+  rsltSensor = bme280_init(&SensorDev);
+  if (rsltSensor == BME280_OK) {
+    Flag.BME280 = 1;
+  }
   /* USER CODE END 2 */
 
   /* USER CODE BEGIN WHILE */
   RTC_ReadAll(&Clock);
   while (Flag.I2C_RX_End == 0) { __NOP(); };
 
+  /* BME280 Recommended mode of operation: Indoor navigation */
+  SensorDev.settings.osr_h = BME280_OVERSAMPLING_1X;
+  SensorDev.settings.osr_p = BME280_OVERSAMPLING_16X;
+  SensorDev.settings.osr_t = BME280_OVERSAMPLING_2X;
+  SensorDev.settings.filter = BME280_FILTER_COEFF_16;
+
+  rsltSensor = bme280_set_sensor_settings((BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL), &SensorDev);
+  rsltSensor = bme280_set_sensor_mode(BME280_NORMAL_MODE, &SensorDev);
+  //SensorDev.delay_ms(50);
+  //rsltSensor = bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
+  /* bme280_get_sensor_data(...) returns:
+   *  - temperature in DegC, resolution is 0.01 DegC. Output value of "5123" equals 51.23 DegC.
+   *  - pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
+   *    Output value "24674867" represents 24674867/256 = 96386.2 Pa = 963.862 hPa.
+   *  - humidity in %RH as unsigned 32 bit integer in Q22.10 format.
+   *    Output value of "47445" represents 47445/1024 = 46.333 %RH
+   */
+  uint8_t temp_l, temp_h, hum_h, hum_l, pres_h, pres_l;
+  uint32_t tmp;
   /* Infinite loop */
   while (1)
   {
@@ -190,7 +224,8 @@ int main(void)
     COLOR_RGB(0, 0, 0);
     LL_mDelay(500);
 
-    RTC_ReadAll(&Clock);
+    //RTC_ReadAll(&Clock);
+    rsltSensor = bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
 
     if (Flag.RTC_IRQ != 0) {
       Flag.RTC_IRQ = 0;
@@ -201,10 +236,36 @@ int main(void)
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
+/*
     showDigit(Tube_A, Clock.Min >> 4);
     showDigit(Tube_B, Clock.Min & 0xf);
     showDigit(Tube_D, Clock.Sec >> 4);
     showDigit(Tube_E, Clock.Sec & 0xf);
+*/
+    if (rsltSensor == BME280_OK) {
+      temp_h = (uint8_t)SensorData.temperature / 100;
+      temp_l = (uint8_t)SensorData.temperature % 100;
+
+      hum_h = (uint8_t)SensorData.humidity / 1024;
+      hum_l = (uint8_t)(((SensorData.humidity % 1024) + 5) / 10);
+
+      tmp = (SensorData.pressure + 128) / 256; // pressure in Pa
+      tmp *= 1000;
+      tmp += 66661;
+      tmp /= 133322; // pressure in mmHg
+      pres_h = (uint8_t)(tmp / 100);
+      pres_l = (uint8_t)(tmp % 100);
+
+      showDigit(Tube_A, temp_h >> 4);
+      showDigit(Tube_B, temp_h & 0xf);
+      showDigit(Tube_D, temp_l >> 4);
+      showDigit(Tube_E, temp_l & 0xf);
+    } else {
+      showDigit(Tube_A, 7);
+      showDigit(Tube_B, 7);
+      showDigit(Tube_D, 7);
+      showDigit(Tube_E, 7);
+    }
     SPI_StartTX();
 
     //__WFI();
@@ -217,6 +278,59 @@ static void SPI_StartTX(void) {
   LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
 }
 
+static int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) {
+  Flag.I2C_TX_End = 0;
+
+  DMA1_Channel2->CMAR = (uint32_t)data;
+  DMA1_Channel2->CPAR = (uint32_t)&(I2C1->RXDR);
+  DMA1_Channel2->CNDTR = len;
+  DMA1_Channel2->CCR |= DMA_CCR_EN;
+
+  while ( I2C1->ISR & I2C_ISR_BUSY ) {};
+
+  I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
+  I2C1->CR2 |= ( id | 1 << I2C_CR2_NBYTES_Pos );
+
+  I2C1->CR2 |= ( I2C_CR2_START );
+
+  while ( !( I2C1->CR2 & I2C_CR2_START ) ) {};
+  I2C1->TXDR = reg_addr;
+
+  while ( I2C1->ISR & I2C_ISR_BUSY ) {};
+  Flag.I2C_RX_End = 0;
+
+  I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
+  I2C1->CR2 |= ( id | len << I2C_CR2_NBYTES_Pos | I2C_CR2_RD_WRN);
+
+  I2C1->CR1 |= ( I2C_CR1_RXDMAEN );
+  I2C1->CR2 |= ( I2C_CR2_START );
+
+  return 0;
+}
+
+static int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) {
+  Flag.I2C_TX_End = 0;
+
+  //DMA1_Channel3->CCR &= ~DMA_CCR_EN;
+  DMA1_Channel3->CMAR = (uint32_t)data;
+  DMA1_Channel3->CPAR = (uint32_t)&(I2C1->TXDR);
+  DMA1_Channel3->CNDTR = len;
+
+  while ( I2C1->ISR & I2C_ISR_BUSY ) {};
+
+  I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
+  I2C1->CR2 |= ( id | (len + 1) << I2C_CR2_NBYTES_Pos );
+  I2C1->CR1 |= ( I2C_CR1_TXDMAEN );
+  I2C1->CR2 |= ( I2C_CR2_START );
+
+  while ( !( I2C1->CR2 & I2C_CR2_START ) ) {};
+  I2C1->TXDR = reg_addr;
+
+  DMA1_Channel3->CCR |= DMA_CCR_EN;
+
+  return 0;
+}
+
 /**
   * @brief System Clock Configuration
   * @retval None