From 11a90da71603e1f092cb8f08d7b68a582335e4a2 Mon Sep 17 00:00:00 2001 From: Pawel Jalocha Date: Thu, 30 Apr 2020 23:23:17 +0100 Subject: [PATCH] Add I2C_Mutex --- main/disp.cpp | 10 +++++----- main/hal.cpp | 8 +++++++- main/hal.h | 2 +- main/main.cpp | 6 +++++- main/sens.cpp | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/main/disp.cpp b/main/disp.cpp index b59ef36..cc8a68f 100644 --- a/main/disp.cpp +++ b/main/disp.cpp @@ -171,15 +171,15 @@ void vTaskDISP(void* pvParameters) // #endif { switch(DISP_Page) { case 2: OLED_DrawGPS (&U8G2_OLED, GPS); break; - case 3: OLED_DrawRF (&U8G2_OLED); break; + case 3: OLED_DrawRF (&U8G2_OLED, GPS); break; case 4: OLED_DrawBaro (&U8G2_OLED, GPS); break; - case 1: OLED_DrawID (&U8G2_OLED); break; - case 5: OLED_DrawSystem(&U8G2_OLED); break; + case 1: OLED_DrawID (&U8G2_OLED, GPS); break; + case 5: OLED_DrawSystem(&U8G2_OLED, GPS); break; // case 6: OLED_DrawRelay (&U8G2_OLED, GPS); break; // case 7: OLED_DrawLookout(&U8G2_OLED, GPS); break; - case 0: OLED_DrawBattery(&U8G2_OLED); break; + case 0: OLED_DrawBattery(&U8G2_OLED, GPS); break; #ifdef WITH_U8G2_LISTS - case 6: OLED_DrawRelay (&U8G2_OLED, GPS); break; + case 6: OLED_DrawRelay (&U8G2_OLED, GPS); break; case 7: OLED_DrawLookout(&U8G2_OLED, GPS); break; case 9: OLED_DrawTrafWarn(&U8G2_OLED, GPS); break; #endif diff --git a/main/hal.cpp b/main/hal.cpp index cf56973..0727791 100644 --- a/main/hal.cpp +++ b/main/hal.cpp @@ -1281,7 +1281,9 @@ static uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int break; } case U8X8_MSG_BYTE_END_TRANSFER: { i2c_master_stop(U8G2_Cmd); + xSemaphoreTake(I2C_Mutex, portMAX_DELAY); i2c_master_cmd_begin(I2C_BUS, U8G2_Cmd, 10); + xSemaphoreGive(I2C_Mutex); i2c_cmd_link_delete(U8G2_Cmd); break; } } @@ -1907,7 +1909,7 @@ int SPIFFS_Info(size_t &Total, size_t &Used, const char *Label) // ====================================================================================================== -// SemaphoreHandle_t I2C_Mutex; +SemaphoreHandle_t I2C_Mutex; uint8_t I2C_Read(uint8_t Bus, uint8_t Addr, uint8_t Reg, uint8_t *Data, uint8_t Len, uint8_t Wait) { i2c_cmd_handle_t Cmd = i2c_cmd_link_create(); @@ -1918,7 +1920,9 @@ uint8_t I2C_Read(uint8_t Bus, uint8_t Addr, uint8_t Reg, uint8_t *Data, uint8_t i2c_master_write_byte(Cmd, (Addr<<1) | I2C_MASTER_READ, I2C_MASTER_ACK); i2c_master_read(Cmd, Data, Len, I2C_MASTER_LAST_NACK); i2c_master_stop(Cmd); + xSemaphoreTake(I2C_Mutex, portMAX_DELAY); esp_err_t Ret = i2c_master_cmd_begin((i2c_port_t)Bus, Cmd, Wait); + xSemaphoreGive(I2C_Mutex); i2c_cmd_link_delete(Cmd); return Ret; } @@ -1929,7 +1933,9 @@ uint8_t I2C_Write(uint8_t Bus, uint8_t Addr, uint8_t Reg, uint8_t *Data, uint8_t i2c_master_write_byte(Cmd, Reg , I2C_MASTER_ACK); i2c_master_write(Cmd, Data, Len, I2C_MASTER_NACK); i2c_master_stop(Cmd); + xSemaphoreTake(I2C_Mutex, portMAX_DELAY); esp_err_t Ret = i2c_master_cmd_begin((i2c_port_t)Bus, Cmd, Wait); + xSemaphoreGive(I2C_Mutex); i2c_cmd_link_delete(Cmd); return Ret; } diff --git a/main/hal.h b/main/hal.h index f96305d..d5e5b49 100644 --- a/main/hal.h +++ b/main/hal.h @@ -48,7 +48,7 @@ extern uint8_t MAV_Seq; // sequence number for MAVlink messag // ============================================================================================================ extern SemaphoreHandle_t CONS_Mutex; // console port Mutex -// extern SemaphoreHandle_t I2C_Mutex; // I2C port Mutex (OLED and Baro) +extern SemaphoreHandle_t I2C_Mutex; // I2C port Mutex (OLED and Baro) uint64_t getUniqueID(void); // get some unique ID of the CPU/chip uint32_t getUniqueAddress(void); // get unique 24-bit address for the transmitted IF diff --git a/main/main.cpp b/main/main.cpp index 068f26d..7f438bc 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -9,6 +9,7 @@ #include "proc.h" // GPS/RF process taskk #include "gps.h" // GPS control data acquisiton #include "sens.h" +#include "imu.h" #include "ctrl.h" // Control task #include "log.h" // Data logging task #include "knob.h" // potentiometer as rotary encoder @@ -29,7 +30,7 @@ void app_main(void) // printf("OGN Tracker on ESP32\n"); CONS_Mutex = xSemaphoreCreateMutex(); // semaphore for sharing the writing to the console - // I2C_Mutex = xSemaphoreCreateMutex(); // semaphore for sharing the I2C bus + I2C_Mutex = xSemaphoreCreateMutex(); // semaphore for sharing the I2C bus NVS_Init(); // initialize Non-Volatile-Storage in Flash and read the tracker parameters @@ -75,6 +76,9 @@ void app_main(void) #if defined(WITH_BMP180) || defined(WITH_BMP280) || defined(WITH_BME280) || defined(WITH_MS5607) || defined(WITH_MS5611) xTaskCreate(vTaskSENS, "SENS", 2048, 0, tskIDLE_PRIORITY+4, 0); #endif +#ifdef WITH_BMX055 + xTaskCreate(vTaskIMU, "IMU", 2048, 0, tskIDLE_PRIORITY+4, 0); +#endif #ifdef WITH_KNOB xTaskCreate(vTaskKNOB, "KNOB", 2048, 0, tskIDLE_PRIORITY+3, 0); #endif diff --git a/main/sens.cpp b/main/sens.cpp index 762fa3d..be5b75f 100644 --- a/main/sens.cpp +++ b/main/sens.cpp @@ -97,7 +97,7 @@ static Delay PressDelay; // 4-second delay for long-term clim static char Line[96]; // line to prepare the barometer NMEA sentence -static uint8_t InitBaro() +static uint8_t InitBaro(void) { // xSemaphoreTake(I2C_Mutex, portMAX_DELAY); Baro.Bus=BARO_I2C; uint8_t Err=Baro.CheckID();