Browse Source

BME280 work ok, except pressure.

Vladimir N. Shilov 3 years ago
parent
commit
6d6a590728
2 changed files with 59 additions and 49 deletions
  1. 55 48
      Src/main.c
  2. 4 1
      Src/stm32g0xx_it.c

+ 55 - 48
Src/main.c

@@ -74,7 +74,7 @@ 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 = 33; //BME280_OK;
+static int8_t rsltSensor = -1; //BME280_OK;
 static int16_t Humidity, Temperature;
 /* USER CODE END PV */
 
@@ -154,9 +154,6 @@ int main(void)
   MX_TIM17_Init();
   /* USER CODE BEGIN 2 */
   RTOS_Init();
-  // __enable_irq();
-  //LL_Init1msTick(rcc_clocks.HCLK_Frequency);
-  //tdelay_ms(1);
 
   /* Start RGB PWM */
   LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH1);
@@ -190,35 +187,35 @@ int main(void)
   RTC_Init();
   while (Flag.I2C_TX_End == 0) { __NOP(); };
 
-  SensorDev.dev_id = BME280_I2C_ADDR_PRIM;
+  SensorDev.dev_id = (BME280_I2C_ADDR_PRIM << 1);
   SensorDev.intf = BME280_I2C_INTF;
   SensorDev.read = user_i2c_read;
   SensorDev.write = user_i2c_write;
   SensorDev.delay_ms = tdelay_ms;
-//  rsltSensor = bme280_init(&SensorDev);
+  rsltSensor = bme280_init(&SensorDev);
   if (rsltSensor == BME280_OK) {
+COLOR_RGB(255, 0, 0);
     Flag.BME280 = 1;
   }
-COLOR_RGB(255, 0, 0);
 
-  SHT_Init();
+  //SHT_Init();
   /* USER CODE END 2 */
 
   /* USER CODE BEGIN WHILE */
   RTC_ReadAll(&Clock);
   while (Flag.I2C_RX_End == 0) { __NOP(); };
 
-  tdelay_ms(10);
-  /* 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);
+  if (Flag.BME280 != 0) {
+    /* 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_FORCED_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).
@@ -234,16 +231,21 @@ COLOR_RGB(255, 0, 0);
     IN15_OFF;
     COLOR_RGB(0, 0, 0);
     RTC_ReadAll(&Clock);
-    SHT_StartH();
-    //SHT_StartT();
+    if (Flag.BME280 != 0) {
+      rsltSensor = bme280_set_sensor_mode(BME280_FORCED_MODE, &SensorDev);
+    }
+//    SHT_StartH();
+//    SHT_StartT();
     tdelay_ms(500);
 
     if (Flag.RTC_IRQ != 0) {
       Flag.RTC_IRQ = 0;
       IN15_Minus;
+      COLOR_RGB(0xFF, 0x12, 0x0); // FF7E00 or FFBF00
+    }
+    if (Flag.BME280 != 0) {
+      rsltSensor = bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
     }
-    COLOR_RGB(0xFF, 0x12, 0x0); // FF7E00 or FFBF00
-//    rsltSensor = bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
     tdelay_ms(500);
     /* USER CODE END WHILE */
 
@@ -254,7 +256,7 @@ COLOR_RGB(255, 0, 0);
     showDigit(Tube_D, Clock.Sec >> 4);
     showDigit(Tube_E, Clock.Sec & 0xf);
 */
-
+/*
     Humidity = SHT_GetH();
     hum_h = Humidity / 100;
     hum_l = Humidity % 100;
@@ -263,7 +265,7 @@ COLOR_RGB(255, 0, 0);
     showDigit(Tube_B, hum_h % 10);
     showDigit(Tube_D, hum_l / 10);
     showDigit(Tube_E, hum_l % 10);
-
+*/
 /*
     Temperature = SHT_GetT();
     temp_h = Temperature / 100;
@@ -274,32 +276,34 @@ COLOR_RGB(255, 0, 0);
     showDigit(Tube_D, temp_l / 10);
     showDigit(Tube_E, temp_l % 10);
 */
-/*
+
     if (rsltSensor == BME280_OK) {
-      temp_h = (uint8_t)SensorData.temperature / 100;
-      temp_l = (uint8_t)SensorData.temperature % 100;
+      tmp = SensorData.temperature + 50;
+      temp_h = (uint8_t)(tmp / 100);
+      temp_l = (uint8_t)(tmp % 100);
 
-      hum_h = (uint8_t)SensorData.humidity / 1024;
-      hum_l = (uint8_t)(((SensorData.humidity % 1024) + 5) / 10);
+      tmp = SensorData.humidity + 512;
+      hum_h = (uint8_t)(tmp / 1024);
+      hum_l = (uint8_t)((tmp % 1024) / 10);
 
       tmp = (SensorData.pressure + 128) / 256; // pressure in Pa
-      tmp *= 1000;
-      tmp += 66661;
-      tmp /= 133322; // pressure in mmHg
+      //tmp *= 1000;
+      //tmp += 66661;
+      //tmp /= 133322; // pressure in mmHg
       pres_h = (uint8_t)(tmp / 100);
       pres_l = (uint8_t)(tmp % 100);
 
-      showDigit(Tube_A, hum_h / 10);
-      showDigit(Tube_B, hum_h % 10);
-      showDigit(Tube_D, hum_l / 10);
-      showDigit(Tube_E, hum_l % 10);
+      showDigit(Tube_A, pres_h / 10);
+      showDigit(Tube_B, pres_h % 10);
+      showDigit(Tube_D, pres_l / 10);
+      showDigit(Tube_E, pres_l % 10);
     } else {
       showDigit(Tube_A, Clock.Min >> 4);
       showDigit(Tube_B, Clock.Min & 0xf);
       showDigit(Tube_D, Clock.Sec >> 4);
       showDigit(Tube_E, Clock.Sec & 0xf);
     }
-*/
+
     SPI_StartTX();
 
     RTOS_DispatchTask();
@@ -341,7 +345,7 @@ int8_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *data, co
 //  while ( !( I2C1->CR2 & I2C_CR2_START ) ) {};
 
   /* wait for byte request or any error */
-  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_STOPF | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
+  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
 
   if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
   /* device ok, send reg addr */
@@ -355,6 +359,7 @@ int8_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *data, co
   }
   if (r != 0) {
     Flag.I2C_TX_Err = 1;
+    Flag.I2C_TX_End = 1;
     I2C1->CR1 &= ~I2C_CR1_PE;
     while ((I2C1->CR1 & I2C_CR1_PE) != 0) {};
     I2C1->CR1 |= I2C_CR1_PE;
@@ -362,19 +367,20 @@ int8_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *data, co
   }
 
   /* wait for i2c or any error */
-  while (((I2C1->ISR & I2C_ISR_BUSY) != 0) && ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_STOPF | I2C_ISR_NACKF)) == 0)) { __NOP(); };
+  while (((I2C1->ISR & I2C_ISR_BUSY) != 0) && ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF)) == 0)) { __NOP(); };
+
   /* check for errors */
   if ((I2C1->ISR & I2C_ISR_NACKF) != 0) {
   /* device not present */
     r = I2C_RET_NACK;
-  } else if ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_STOPF)) != 0) {
+  } else if ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR)) != 0) {
   /* other error */
     r = I2C_RET_ERR;
   }
   if (r != 0) {
     Flag.I2C_TX_Err = 1;
     I2C1->CR1 &= ~I2C_CR1_PE;
-    while ((I2C1->CR1 & I2C_CR1_PE) != 0) {};
+    while ((I2C1->CR1 & I2C_CR1_PE) != 0) { __NOP(); };
     I2C1->CR1 |= I2C_CR1_PE;
     return r;
   }
@@ -418,7 +424,7 @@ int8_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *data, c
   I2C1->CR2 |= ( I2C_CR2_START );
 
 //  while ( !( I2C1->CR2 & I2C_CR2_START ) ) {};
-  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_STOPF | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
+  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
   if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
     I2C1->TXDR = reg_addr;
   } else { /* if ((I2C1->ISR & I2C_ISR_NACKF) != 0) */
@@ -705,8 +711,8 @@ static void MX_I2C1_Init(void)
   LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MDATAALIGN_BYTE);
 
   /* I2C1 interrupt Init */
-  NVIC_SetPriority(I2C1_IRQn, 0);
-  NVIC_EnableIRQ(I2C1_IRQn);
+//  NVIC_SetPriority(I2C1_IRQn, 0);
+//  NVIC_EnableIRQ(I2C1_IRQn);
 
   /* USER CODE BEGIN I2C1_Init 1 */
   /* Enable DMA transfer complete/error interrupts */
@@ -725,14 +731,15 @@ static void MX_I2C1_Init(void)
   I2C_InitStruct.OwnAddress1 = 0;
   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
-  LL_I2C_Init(I2C1, &I2C_InitStruct);
   LL_I2C_EnableAutoEndMode(I2C1);
   LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
   LL_I2C_DisableOwnAddress2(I2C1);
   LL_I2C_DisableGeneralCall(I2C1);
-  LL_I2C_EnableClockStretching(I2C1);
+  LL_I2C_DisableClockStretching(I2C1);
+  LL_I2C_Init(I2C1, &I2C_InitStruct);
   /* USER CODE BEGIN I2C1_Init 2 */
-  LL_I2C_EnableIT_NACK(I2C1);
+//  LL_I2C_EnableIT_NACK(I2C1);
+//  LL_I2C_EnableIT_ERR(I2C1);
 
   /* USER CODE END I2C1_Init 2 */
 

+ 4 - 1
Src/stm32g0xx_it.c

@@ -287,10 +287,13 @@ void TIM17_IRQHandler(void)
 void I2C1_IRQHandler(void)
 {
   /* USER CODE BEGIN I2C1_IRQn 0 */
+  if ((I2C1->ISR & I2C_ISR_NACKF) != 0) {
+  }
   /* USER CODE END I2C1_IRQn 0 */
 
   /* USER CODE BEGIN I2C1_IRQn 1 */
-
+  if ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR)) != 0) {
+  }
   /* USER CODE END I2C1_IRQn 1 */
 }