Added I2C and BMP280

i2c already running and functional, bmp280 code is still in the main and needs re-organisation
Temperature readout works, pressure is not required at this stage
This commit is contained in:
Matthias Mitscherlich
2023-02-06 16:21:56 +01:00
parent bc0da9ec9e
commit 2e8aa31cbf
11 changed files with 409 additions and 76 deletions
+2
View File
@@ -4,7 +4,9 @@
idf_component_register(
SRCS # list the source files of this component
"main.cpp"
"src/bmp280.cpp"
"src/gpio.cpp"
"src/i2c.cpp"
"src/wifi.cpp"
"src/logger.cpp"
"src/led_strip_encoder.c"
+70
View File
@@ -0,0 +1,70 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file bme280.h
/// \brief File description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
#ifndef MAIN_INC_BMP280_H_
#define MAIN_INC_BMP280_H_
/**
* bme280 implementation
* \defgroup bme280
* \brief {group_description}
* \addtogroup {Layer}
*
* Detailed description
* @{
*/
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
// CompilerIncludes
// All include files that are provided by the compiler directly
// ProjectIncludes
// All include files that are provided by the project
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions.
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
class BMP280
{
public:
private:
};
/** @} */
#endif /* MAIN_INC_BMP280_H_ */
+86
View File
@@ -0,0 +1,86 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file i2c.h
/// \brief File description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
#ifndef MAIN_INC_I2C_H_
#define MAIN_INC_I2C_H_
/**
* i2c implementation
* \defgroup i2c
* \brief {group_description}
* \addtogroup {Layer}
*
* Detailed description
* @{
*/
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
// CompilerIncludes
// All include files that are provided by the compiler directly
#include "stdint.h"
// ProjectIncludes
// All include files that are provided by the project
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions.
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
class I2C
{
public:
I2C(unsigned int SCL, unsigned int SDA);
bool write_register(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* const data, uint8_t length);
bool read_register(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length);
private:
static unsigned int number;
unsigned int thisNumber;
unsigned int SCL;
unsigned int SDA;
unsigned int frequency;
unsigned int timeout_ms;
void write(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* const data, uint8_t length);
void read(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length);
};
/** @} */
#endif /* MAIN_INC_I2C_H_ */
+1
View File
@@ -80,6 +80,7 @@ class Wordmap
struct word
{
string identifier;
// list<LEDMatrix::coordinate> pixels;
LEDMatrix::coordinate position;
int length;
};
+67 -62
View File
@@ -33,7 +33,9 @@
#include "driver/uart_select.h"
#include "driver/gptimer.h"
#include "inc/bmp280.h"
#include "inc/gpio.h"
#include "inc/i2c.h"
#include "inc/led_strip_encoder.h"
#include "inc/ledmatrix.h"
#include "inc/logger.h"
@@ -49,8 +51,6 @@
#define RMT_LED_STRIP_RESOLUTION_HZ 10000000 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
#define RMT_LED_STRIP_GPIO_NUM 0
#define RMT_LED_STRIP_GPIO_AUX 1
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
@@ -81,32 +81,16 @@ static rmt_encoder_handle_t led_encoder = NULL;
static LEDMatrix_Parameters_t ledmatrix_parameters =
{
LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT,
LEDMATRIX_ORIENTATION_COLUM_UP_DOWN,
LEDMATRIX_ORIENTATION_COLUM_DOWN_UP,
LEDMATRIX_ORIENTATION_ROW,
6,
4,
11,
10,
&led_chan,
&led_encoder,
&tx_config
};
static rmt_channel_handle_t led_aux_chan = NULL;
static rmt_transmit_config_t tx_aux_config;
static rmt_encoder_handle_t led_aux_encoder = NULL;
static LEDMatrix_Parameters_t ledmatrix_aux_parameters =
{
LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT,
LEDMATRIX_ORIENTATION_COLUM_DOWN_UP,
LEDMATRIX_ORIENTATION_COLUM,
11,
10,
&led_aux_chan,
&led_aux_encoder,
&tx_aux_config
};
static LEDMatrix matrix(&ledmatrix_parameters);
static LEDMatrix aux(&ledmatrix_aux_parameters);
static Wordmap map(&matrix);
@@ -125,11 +109,28 @@ static bool timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data
static void devTask(void* parameters);
static void colourMapTask(void* parameters);
static int bmp280_compensate_T_int32(int adc_T);
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
int t_fine;
uint16_t dig_T1 = 0x6AA3;
int16_t dig_T2 = 0x6555;
int16_t dig_T3 = 0x0032;
int bmp280_compensate_T_int32(int adc_T)
{
int var1, var2, T;
var1 = ((((adc_T>>3) - ((int)dig_T1<<1))) * ((int)dig_T2)) >> 11;
var2 = (((((adc_T>>4) - ((int)dig_T1)) * ((adc_T>>4) - ((int)dig_T1))) >> 12) * ((int)dig_T3)) >> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
return T;
}
extern "C" void app_main(void)
{
esp_log_level_set("*", ESP_LOG_WARN);
@@ -164,12 +165,12 @@ extern "C" void app_main(void)
//
Logger logger(10, uartPort);
LOGGER_PRINT("-----------------------------------------------------------------------");
LOGGER_PRINT("System Start");
LOGGER_PRINT("");
LOGGER_PRINT("WordClock");
LOGGER_PRINT("Release: %f", RELEASE);
LOGGER_PRINT("Compiled on %d %d", __TIME__, __DATE__);
LOGGER_PRINT("\n\r-----------------------------------------------------------------------\n\r");
LOGGER_PRINT("System Start\n\r");
LOGGER_PRINT("\n\r");
LOGGER_PRINT("WordClock\n\r");
LOGGER_PRINT("Release: %f\n\r", RELEASE);
LOGGER_PRINT("Compiled on %d %d\n\r\n\r\n\r", __TIME__, __DATE__);
//--------------------------------------------
@@ -202,38 +203,30 @@ extern "C" void app_main(void)
//--------------------------------------------
// RMT Channel
// I2C
//
LOGGER_INFO("Create RMT TX channel");
// memset(&tx_chan_config, 0, sizeof(tx_chan_config));
// SourceClock: GPIO 8
// SourceData: GPIO 9
I2C i2c0(8, 9);
// tx_chan_config.clk_src = RMT_CLK_SRC_DEFAULT; // select source clock
// tx_chan_config.gpio_num = RMT_LED_STRIP_GPIO_AUX;
// tx_chan_config.mem_block_symbols = 64; // increase the block size can make the LED less flickering
// tx_chan_config.resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ;
// tx_chan_config.trans_queue_depth = 4;
// ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_aux_chan));
// LOGGER_INFO("Install led strip encoder");
// led_strip_encoder_config_t encoder_aux_config;
// memset(&encoder_aux_config, 0, sizeof(encoder_aux_config));
// encoder_config.resolution = RMT_LED_STRIP_RESOLUTION_HZ;
// ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_aux_config, &led_aux_encoder));
// LOGGER_INFO("Enable RMT TX channel");
// ESP_ERROR_CHECK(rmt_enable(led_chan));
// memset(&tx_config, 0, sizeof(tx_config));
// tx_config.loop_count = 0;
uint8_t data[6];
// Read BME280 ID register
i2c0.read_register(0x76, 0xD0, data, 1);
LOGGER_DEBUG("BMP280 ID: %02X", data[0]);
// Read compensation values
i2c0.read_register(0x76, 0x88, data, 6);
// Set the oversampling to x1
uint8_t writeData = 0x27;
i2c0.write_register(0x76, 0xF4, &writeData, 1);
//--------------------------------------------
// LED Matrix
//
matrix.setGlobalColour(0x10, 0, 0x04);
matrix.setGlobalColour(0x80, 0, 0x40);
// matrix.setGlobalColour(0x80, 0, 0x40);
//--------------------------------------------
// GP Timer for automatic matrix re-draw trigger
@@ -263,11 +256,11 @@ extern "C" void app_main(void)
LOGGER_ERROR("Task not created");
}
// // Create the colour Map task
// if(xTaskCreate(colourMapTask, "ColourTask", 2048, NULL, 3, &colourMapTaskHandle) != pdPASS)
// {
// LOGGER_ERROR("Task not created");
// }
// Create the colour Map task
if(xTaskCreate(colourMapTask, "ColourTask", 2048, NULL, 3, &colourMapTaskHandle) != pdPASS)
{
LOGGER_ERROR("Task not created");
}
Wifi wifi;
wifi.start_client();
@@ -292,6 +285,18 @@ extern "C" void app_main(void)
// Add a seconds indicator
matrix.setPixelValue(10, 9, clock.getTime() % 2);
i2c0.read_register(0x76, 0xF7, data, 3);
LOGGER_DEBUG("BMP280 pressure: %02X %02X %02X", data[0], data[1], data[2]);
i2c0.read_register(0x76, 0xFA, data, 3);
int32_t value = 0;
value |= data[0] << 12;
value |= data[1] << 4;
value |= data[2] >> 4;
int valueComp = bmp280_compensate_T_int32(value);
LOGGER_DEBUG("BMP280 temperature: %02X %02X %02X -> %d", data[0], data[1], data[2], valueComp);
// Update the clock every second (1000 ms)
vTaskDelay(1000);
@@ -314,9 +319,9 @@ static void devTask(void* parameters)
static void colourMapTask(void* parameters)
{
uint8_t red = 0;
uint8_t red = 0x10;
uint8_t green = 0;
uint8_t blue = 0;
uint8_t blue = 0x04;
uint32_t counter = 0;
@@ -325,10 +330,10 @@ static void colourMapTask(void* parameters)
{
matrix.setGlobalColour(red, green, blue);
red = counter & 0xFF;
green = (counter >> 8) & 0xFF;
blue = (counter >> 16) & 0xFF;
counter++;
// red = counter & 0xFF;
// green = (counter >> 8) & 0xFF;
// blue = (counter >> 16) & 0xFF;
// counter++;
vTaskDelay(30);
}
+51
View File
@@ -0,0 +1,51 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file bme280.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include <bmp280.h>
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
-10
View File
@@ -17,9 +17,6 @@
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "esp_system.h"
#include "esp_log.h"
#include <gpio.h>
#include "driver/gpio.h"
@@ -52,11 +49,7 @@
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
GPIO::GPIO(int number, GPIO_Direction_t direction)
{
this->number = number;
this->direction = direction;
@@ -111,6 +104,3 @@ GPIO_Value_t GPIO::GetInput(void)
return this->value;
}
+122
View File
@@ -0,0 +1,122 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file i2c.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "i2c.h"
#include "driver/i2c.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_TIMEOUT_MS 1000
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
// Reset the class variable
unsigned int I2C::number = 0;
I2C::I2C(unsigned int SCL, unsigned int SDA)
{
I2C::thisNumber = number++;
I2C::SCL = SCL;
I2C::SDA = SDA;
I2C::frequency = I2C_MASTER_FREQ_HZ;
I2C::timeout_ms = I2C_MASTER_TIMEOUT_MS;
//
i2c_port_t i2c_master_port = (i2c_port_t)I2C::thisNumber;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = (int)I2C::SDA,
.scl_io_num = (int)I2C::SCL,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {I2C::frequency},
.clk_flags = 0
};
ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
}
bool I2C::write_register(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* const data, uint8_t length)
{
bool returnValue = true;
write(slaveAddress, registerAddress, data, length);
return returnValue;
}
bool I2C::read_register(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length)
{
bool returnValue = true;
read(slaveAddress, registerAddress, data, length);
return returnValue;
}
void I2C::write(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* const data, uint8_t length)
{
uint8_t buffer[length + 1];
buffer[0] = registerAddress;
for (int i = 0; i < length; i++)
{
buffer[i+1] = data[i];
}
ESP_ERROR_CHECK(i2c_master_write_to_device((i2c_port_t)thisNumber, slaveAddress, buffer, sizeof(buffer), timeout_ms / portTICK_PERIOD_MS));
}
void I2C::read(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length)
{
ESP_ERROR_CHECK(i2c_master_write_read_device((i2c_port_t)thisNumber, slaveAddress, &registerAddress, 1, data, length, timeout_ms / portTICK_PERIOD_MS));
}
+4 -4
View File
@@ -48,10 +48,10 @@
// --------------------------------------------------------------------------------------------------------------------
//static const char* ssid = "Kowalski";
//static const char* pass = "madagascar";
static const char* ssid = "vbchaos";
static const char* pass = "mijninternet";
static const char* ssid = "Kowalski";
static const char* pass = "madagascar";
//static const char* ssid = "vbchaos";
//static const char* pass = "mijninternet";
// --------------------------------------------------------------------------------------------------------------------
// Function declarations