Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Core/Inc/stm32f4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void SPI2_IRQHandler(void);
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void DMA2_Stream0_IRQHandler(void);
Expand Down
5 changes: 5 additions & 0 deletions Core/Src/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* SPI2 interrupt Init */
HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SPI2_IRQn);
/* USER CODE BEGIN SPI2_MspInit 1 */

/* USER CODE END SPI2_MspInit 1 */
Expand All @@ -110,6 +113,8 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15);

/* SPI2 interrupt Deinit */
HAL_NVIC_DisableIRQ(SPI2_IRQn);
/* USER CODE BEGIN SPI2_MspDeInit 1 */

/* USER CODE END SPI2_MspDeInit 1 */
Expand Down
15 changes: 15 additions & 0 deletions Core/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_adc1;
extern SPI_HandleTypeDef hspi2;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
/* USER CODE BEGIN EV */
Expand Down Expand Up @@ -200,6 +201,20 @@ void SysTick_Handler(void)
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/

/**
* @brief This function handles SPI2 global interrupt.
*/
void SPI2_IRQHandler(void)
{
/* USER CODE BEGIN SPI2_IRQn 0 */

/* USER CODE END SPI2_IRQn 0 */
HAL_SPI_IRQHandler(&hspi2);
/* USER CODE BEGIN SPI2_IRQn 1 */

/* USER CODE END SPI2_IRQn 1 */
}

/**
* @brief This function handles USART1 global interrupt.
*/
Expand Down
148 changes: 148 additions & 0 deletions Lib/Inc/CBME280.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
*@file CBME280.h
*
*/

/*
* CBME280.h
*
* Created on: 24 Jul 2022
* Author: salavat.magazov
*/

#ifndef CBME280_H_
#define CBME280_H_

#include "CGpioWrapper.h"
#include "gpio.h"
#include "spi.h"

#define T_CALIBRATION_SIZE 3
#define P_CALIBRATION_SIZE 9
#define H_CALIBRATION_SIZE 6
#define RAW_ADC_DATA_SIZE 8
#define MAX_SENSORS 2

class CBME280
{
enum REGISTER_MAP
{
/* Calibration registers. */
DIG_T1 = 0x88,
DIG_T2 = 0x8A,
DIG_T3 = 0x8C,
DIG_P1 = 0x8E,
DIG_P2 = 0x90,
DIG_P3 = 0x92,
DIG_P4 = 0x94,
DIG_P5 = 0x96,
DIG_P6 = 0x98,
DIG_P7 = 0x9A,
DIG_P8 = 0x9C,
DIG_P9 = 0x9E,
DIG_H1 = 0xA1,
DIG_H2 = 0xE1,
DIG_H3 = 0xE3,
DIG_H4 = 0xE4,
DIG_H5 = 0xE5,
/* Sensor data registers. */
ID = 0xD0,
RESET = 0xE0,
CTRL_HUM = 0xF2,
STATUS = 0xF3,
CTRL_MEAS = 0xF4,
CONFIG = 0xF5,
PRESS_MSB = 0xF7,
PRESS_LSB = 0xF8,
PRESS_XLSB = 0xF9,
TEMP_MSB = 0xFA,
TEMP_LSB = 0xFB,
TEMP_XLSB = 0xFC,
HUM_MSB = 0xFD,
HUM_LSB = 0xFE
};

public:
CBME280(SPI_HandleTypeDef *p_spi,
GPIO_TypeDef *p_slave_select_port,
uint16_t slave_select_pin);
virtual ~CBME280();

bool init();
bool run();

/**
* @brief Access processed temperature data.
*
* @return Temperature in degrees Celsius.
*/
float getTemperature() const
{
return m_temperature;
};

/**
* @brief Access processed pressure data.
*
* @return Pressure in Pascals.
*/
float getPressure() const
{
return m_pressure;
};

/**
* @brief Access processed humidity data.
*
* @return Humidity in %RH.
*/
float getHumidity() const
{
return m_humidity;
};

bool isMeasuring() const
{
return m_state == MEASURING;
}

static void processIrq();

private:
void calibrateSensor(uint8_t const *const p_calibration_data);
void convertRawData();
bool startMeasurement();
void calculateT();
void calculateP();
void calculateH();

SPI_HandleTypeDef *mp_spi;
CGpioWrapper m_slave_select;

enum STATES
{
INIT_PENDING = 0,
READY,
MEASURING,
NEW_DATA
} m_state;

static CBME280 *sp_sensors[MAX_SENSORS];
static uint8_t s_sensor_count;

float m_temperature_calibration[T_CALIBRATION_SIZE];
float m_pressure_calibration[P_CALIBRATION_SIZE];
float m_humidity_calibration[H_CALIBRATION_SIZE];

uint8_t m_raw_adc_data[RAW_ADC_DATA_SIZE];
uint32_t m_raw_temperature_data;
uint32_t m_raw_pressure_data;
uint32_t m_raw_humidity_data;

float m_t_fine;
float m_temperature;
float m_pressure;
float m_humidity;
};

#endif /* CBME280_H_ */
Loading