Browse Source

somewhat made mpu + display work together

try-fix-comms-errors
Alex Mikhalev 6 years ago
parent
commit
e8ee6cd107
  1. 14
      main/i2c_mutex.h
  2. 3
      main/u8g2_esp32_hal.c
  3. 5
      main/ugv_io_mpu.cc
  4. 8
      main/ugv_main.cc

14
main/i2c_mutex.h

@ -0,0 +1,14 @@
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#ifdef __cplusplus
extern "C" {
#endif
extern SemaphoreHandle_t i2c_mutex;
#ifdef __cplusplus
}
#endif

3
main/u8g2_esp32_hal.c

@ -3,6 +3,7 @@
#include "esp_log.h" #include "esp_log.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "i2c_mutex.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@ -173,8 +174,10 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int,
ESP_ERROR_CHECK( ESP_ERROR_CHECK(
i2c_master_write(handle_i2c, txbuf, txbuf_ptr - txbuf, ACK_CHECK_EN)); i2c_master_write(handle_i2c, txbuf, txbuf_ptr - txbuf, ACK_CHECK_EN));
ESP_ERROR_CHECK(i2c_master_stop(handle_i2c)); ESP_ERROR_CHECK(i2c_master_stop(handle_i2c));
xSemaphoreTake(i2c_mutex, portMAX_DELAY);
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c,
I2C_TIMEOUT_MS / portTICK_RATE_MS)); I2C_TIMEOUT_MS / portTICK_RATE_MS));
xSemaphoreGive(i2c_mutex);
i2c_cmd_link_delete(handle_i2c); i2c_cmd_link_delete(handle_i2c);
break; break;
} }

5
main/ugv_io_mpu.cc

@ -3,6 +3,7 @@
#include <driver/uart.h> #include <driver/uart.h>
#include <esp_log.h> #include <esp_log.h>
#include "i2c_mutex.h"
#include "MPU.hpp" #include "MPU.hpp"
#include "mpu/math.hpp" #include "mpu/math.hpp"
@ -32,6 +33,7 @@ void MPU::Init() {
mpu_bus_ = &i2c0; mpu_bus_ = &i2c0;
// This is shared with the oled, so just use those pins // This is shared with the oled, so just use those pins
xSemaphoreTake(i2c_mutex, portMAX_DELAY);
mpu_bus_->begin(MPU_SDA, MPU_SCL); mpu_bus_->begin(MPU_SDA, MPU_SCL);
mpu_ = new mpud::MPU(*mpu_bus_); mpu_ = new mpud::MPU(*mpu_bus_);
@ -54,6 +56,7 @@ void MPU::Init() {
} }
mpu_->setAccelFullScale(MPU_ACCEL_FS); mpu_->setAccelFullScale(MPU_ACCEL_FS);
mpu_->setGyroFullScale(MPU_GYRO_FS); mpu_->setGyroFullScale(MPU_GYRO_FS);
xSemaphoreGive(i2c_mutex);
BaseType_t xRet = BaseType_t xRet =
xTaskCreate(MPU::MPU_Task, "ugv_io_mpu", 2 * 1024, this, 3, &this->task_); xTaskCreate(MPU::MPU_Task, "ugv_io_mpu", 2 * 1024, this, 3, &this->task_);
@ -74,7 +77,9 @@ void MPU::DoTask() {
esp_err_t ret; esp_err_t ret;
while (true) { while (true) {
vTaskDelay(pdMS_TO_TICKS(50)); vTaskDelay(pdMS_TO_TICKS(50));
xSemaphoreTake(i2c_mutex, portMAX_DELAY);
ret = mpu_->motion(&accel_, &gyro_, &mag_); ret = mpu_->motion(&accel_, &gyro_, &mag_);
xSemaphoreGive(i2c_mutex);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGE(TAG, "error reading MPU"); ESP_LOGE(TAG, "error reading MPU");
continue; continue;

8
main/ugv_main.cc

@ -3,6 +3,7 @@
#include "ugv_comms.hh" #include "ugv_comms.hh"
#include "ugv_display.hh" #include "ugv_display.hh"
#include "ugv_io.hh" #include "ugv_io.hh"
#include "i2c_mutex.h"
#include <math.h> #include <math.h>
@ -13,6 +14,10 @@ using ugv::io::IOClass;
static const char *TAG = "ugv_main"; static const char *TAG = "ugv_main";
extern "C" {
SemaphoreHandle_t i2c_mutex;
}
constexpr uint64_t LOOP_PERIOD_US = 1e6 / 100; constexpr uint64_t LOOP_PERIOD_US = 1e6 / 100;
static const float PI = atanf(1.0) * 4.0; static const float PI = atanf(1.0) * 4.0;
@ -36,10 +41,11 @@ struct State {
void Init() { void Init() {
esp_timer_init(); esp_timer_init();
i2c_mutex = xSemaphoreCreateMutex();
comms->Init(); comms->Init();
io->Init(); io->Init();
// display->Init(); display->Init();
esp_timer_create_args_t timer_args; esp_timer_create_args_t timer_args;
timer_args.callback = OnTimeout; timer_args.callback = OnTimeout;

Loading…
Cancel
Save