From 13a590b4f3a88f90bb5ed8f56a34baeb2c409bc3 Mon Sep 17 00:00:00 2001 From: Isaak Devos Date: Sun, 25 Feb 2024 18:52:25 +0100 Subject: [PATCH] Clean + attempt at getting gyro to work (still getting 0x03 as chip id) --- software/Core/Inc/gyro.h | 2 +- software/Core/Inc/log.h | 2 +- software/Core/Src/cli.c | 14 ++++++- software/Core/Src/gyro.c | 81 ++++++++++++++++++++++---------------- software/Core/Src/log.c | 2 +- software/Core/Src/mymain.c | 6 +-- 6 files changed, 66 insertions(+), 41 deletions(-) diff --git a/software/Core/Inc/gyro.h b/software/Core/Inc/gyro.h index 73d35de..8588ee8 100644 --- a/software/Core/Inc/gyro.h +++ b/software/Core/Inc/gyro.h @@ -25,6 +25,6 @@ typedef struct gyro_err_t gyro_init(gyro_t *gyro); gyro_err_t gyro_read(gyro_t *gyro); -void print_gyro_err_t(gyro_err_t err); +void log_gyro_err_t(gyro_err_t err); #endif //BETTERFLIGHT_GYRO_H diff --git a/software/Core/Inc/log.h b/software/Core/Inc/log.h index 800e59b..2e61514 100644 --- a/software/Core/Inc/log.h +++ b/software/Core/Inc/log.h @@ -44,7 +44,7 @@ void LOGI(const uint8_t *format, ...); void LOGW(const uint8_t *format, ...); void LOGE(const uint8_t *format, ...); void LOGH(const uint8_t *format, ...); -void LOG(uint8_t *format, ...); +void LOG(const uint8_t *format, ...); void LOG_ascii_hex_dump(uint8_t *data); #endif //BETTERFLIGHT_LOG_H diff --git a/software/Core/Src/cli.c b/software/Core/Src/cli.c index 8f05834..cf6b189 100644 --- a/software/Core/Src/cli.c +++ b/software/Core/Src/cli.c @@ -99,12 +99,24 @@ void cli_handle_cmd(uint8_t * cmd_str) { NVIC_SystemReset(); break; case CLI_CMD_DEV1: - dev1_callback(); + if (get_log_level() != LOG_LEVEL_DEBUG) { + LOGE("Can't run dev command in non-debug mode. (Use `set loglevel debug` to go into debugging mode)."); + return; + } + dev1_callback(); break; case CLI_CMD_DEV2: + if (get_log_level() != LOG_LEVEL_DEBUG) { + LOGE("Can't run dev command in non-debug mode. (Use `set loglevel debug` to go into debugging mode)."); + return; + } dev2_callback(); break; case CLI_CMD_DEV3: + if (get_log_level() != LOG_LEVEL_DEBUG) { + LOGE("Can't run dev command in non-debug mode. (Use `set loglevel debug` to go into debugging mode)."); + return; + } dev3_callback(); break; case CLI_CMD_NONE: diff --git a/software/Core/Src/gyro.c b/software/Core/Src/gyro.c index ef47121..beddf75 100644 --- a/software/Core/Src/gyro.c +++ b/software/Core/Src/gyro.c @@ -1,24 +1,31 @@ #include "gyro.h" -#include "BMI270_CONFIG.h" -#include "main.h" + +#include + #include "stm32f7xx_hal.h" #include "stm32f7xx_hal_spi.h" -#include +#include "main.h" +#include "BMI270_CONFIG.h" +#include "log.h" + +typedef enum { + CHIP_ID = 0x24, + PWR_CONF = 0x7C, + INIT_CTRL = 0x59, + INTERNAL_STATUS = 0x21, + PWR_CTRL = 0x7D, + ACC_CONF = 0x40, + GYR_CONF = 0x42, + DATA_8_TO_19 = 0x41, + DATA_START = 0x0C, +} BMI270_REG_t; -#define BMI270_CHIP_ID_REG 0x00 -#define BMI270_PWR_CONF_REG 0x7C -#define BMI270_INIT_CTRL_REG 0x59 -#define BMI270_INTERNAL_STATUS_REG 0x21 -#define BMI270_PWR_CTRL_REG 0x7D -#define BMI270_ACC_CONF_REG 0x40 -#define BMI270_GYR_CONF_REG 0x42 -#define BMI270_DATA_8_TO_19 0x41 -#define BMI270_DATA_START_REG 0x0C #define NUM_BYTES_TO_READ 12 void gyro_cs_enable(void); void gyro_cs_disable(void); +void gyro_send_spi(BMI270_REG_t reg, uint8_t *rx_data_ptr, uint8_t size); gyro_err_t gyro_init(gyro_t *gyro) { @@ -26,49 +33,51 @@ gyro_err_t gyro_init(gyro_t *gyro) HAL_Delay(1000); // wait for gyro to power up gyro_cs_enable(); // enable chip select - //dummy read - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_CHIP_ID_REG, NULL, 1, HAL_MAX_DELAY); + //dummy read - configures the spi (its i2c by default) + gyro_send_spi(CHIP_ID, NULL, 1); + + // read chip id to verify communication + gyro_send_spi(CHIP_ID, &gyro->chip_id, 1); - // read chip id to init spi on the BMI270 - HAL_SPI_TransmitReceive(&hspi1, BMI270_CHIP_ID_REG, &gyro->chip_id, 1, HAL_MAX_DELAY); - printf("[debug] chip id: %x\n", gyro->chip_id); + LOGD("chip id: %x\n", gyro->chip_id); if (gyro->chip_id != 0x24) { gyro_cs_disable(); return GYRO_ERR_CHIP_ID; } + gyro_cs_disable(); + return GYRO_OK; //debugging // disable power save mode - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_PWR_CONF_REG, 0x00, 1, HAL_MAX_DELAY); + gyro_send_spi(PWR_CONF, 0x00, 1); HAL_Delay(1); // prepare config load - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_INIT_CTRL_REG, 0x00, 1, HAL_MAX_DELAY); + gyro_send_spi(INIT_CTRL, 0x00, 1); //burst write to reg INIT_DATA Start with byte 0 HAL_SPI_Transmit(&hspi1, (uint8_t *) bmi270_config_file, sizeof(bmi270_config_file), HAL_MAX_DELAY); - // enable power save mode - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_PWR_CONF_REG, (uint8_t*) 0x01, 1, HAL_MAX_DELAY); + gyro_send_spi(PWR_CONF, (uint8_t*) 0x01, 1); // check internal status uint8_t status = 0; - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_INTERNAL_STATUS_REG, &status, 1, HAL_MAX_DELAY); + gyro_send_spi(INTERNAL_STATUS, &status, 1); if (status != 0x01) { gyro_cs_disable(); return GYRO_ERR_INTERNAL_STATUS; } //enable acquisiton of acceleration; gyroscope and temperature sensor data; disable the auxiliary interface. - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_PWR_CTRL_REG, (uint8_t*) 0x0E, 1, HAL_MAX_DELAY); + gyro_send_spi(PWR_CTRL, (uint8_t*) 0x0E, 1); //enable the acc_filter_perf bit; set acc_bwp to normal mode; set acc_odr to 100 Hz - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_ACC_CONF_REG, (uint8_t*) 0xA8, 1, HAL_MAX_DELAY); + gyro_send_spi(ACC_CONF, (uint8_t*) 0xA8, 1); //enable the gyr_filter_perf bit; enable; gyr_noise_perf bit; set gyr_bwp to normal mode; set gyr_odr to 200 Hz - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_GYR_CONF_REG, (uint8_t*) 0xE9, 1, HAL_MAX_DELAY); + gyro_send_spi(GYR_CONF, (uint8_t*) 0xE9, 1); //disable the adv_power_save bit; leave the fifo_self_wakeup enabled - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) BMI270_PWR_CONF_REG, (uint8_t*) 0x02, 1, HAL_MAX_DELAY); + gyro_send_spi(PWR_CONF, (uint8_t*) 0x02, 1); @@ -79,7 +88,7 @@ gyro_err_t gyro_init(gyro_t *gyro) gyro_err_t gyro_read(gyro_t *gyro) { gyro_cs_enable(); - HAL_SPI_TransmitReceive(&hspi1, (uint8_t *) (BMI270_DATA_START_REG | 0x80), &gyro->raw_data, NUM_BYTES_TO_READ, HAL_MAX_DELAY); + gyro_send_spi(DATA_START | 0x80, &gyro->raw_data, NUM_BYTES_TO_READ); gyro_cs_disable(); return GYRO_OK; } @@ -87,29 +96,33 @@ gyro_err_t gyro_read(gyro_t *gyro) void gyro_cs_enable(void) { HAL_GPIO_WritePin(GYRO_CS_GPIO_Port, GYRO_CS_Pin, GPIO_PIN_SET); - printf("[debug] gyro cs enabled\n"); + LOGD("gyro cs enabled\n"); } void gyro_cs_disable(void) { HAL_GPIO_WritePin(GYRO_CS_GPIO_Port, GYRO_CS_Pin, GPIO_PIN_RESET); - printf("[debug] gyro cs disabled\n"); + LOGD("gyro cs disabled\n"); } -void print_gyro_err_t (gyro_err_t err) +void log_gyro_err_t(gyro_err_t err) { switch (err) { case GYRO_OK: - printf("[ERR] GYRO_OK\n"); + LOGE("[ERR] GYRO_OK\n"); break; case GYRO_ERR_CHIP_ID: - printf("[ERR] GYRO_ERR_CHIP_ID\n"); + LOGE("[ERR] GYRO_ERR_CHIP_ID\n"); break; case GYRO_ERR_INTERNAL_STATUS: - printf("[ERR] GYRO_ERR_INTERNAL_STATUS\n"); + LOGE("[ERR] GYRO_ERR_INTERNAL_STATUS\n"); break; default: - printf("[ERR] UNKNOWN GYRO_ERR\n"); + LOGE("[ERR] UNKNOWN GYRO_ERR\n"); break; } +} + +void gyro_send_spi(BMI270_REG_t reg, uint8_t *rx_data_ptr, uint8_t size) { + HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)reg, rx_data_ptr, size, HAL_MAX_DELAY); } \ No newline at end of file diff --git a/software/Core/Src/log.c b/software/Core/Src/log.c index 4dc0c92..6e488a3 100644 --- a/software/Core/Src/log.c +++ b/software/Core/Src/log.c @@ -105,7 +105,7 @@ void LOGH(const uint8_t *format, ...) { va_end(args); } -void LOG(uint8_t *format, ...) { +void LOG( const uint8_t *format, ...) { va_list args; va_start(args, format); diff --git a/software/Core/Src/mymain.c b/software/Core/Src/mymain.c index 1c48036..5a88fbe 100644 --- a/software/Core/Src/mymain.c +++ b/software/Core/Src/mymain.c @@ -13,12 +13,11 @@ // check version.h for version settings /* EOF SETTINGS */ - -extern uint8_t cli_rx_buffer[64]; - uint64_t led_toggle_last_ms = 0; uint64_t cli_process_last_ms = 0; +gyro_t gyro; + void myinit(void) { log_init(LOG_LEVEL, true); if (get_log_level() == LOG_LEVEL_DEBUG) { // give dev time to open serial monitor when debugging @@ -48,6 +47,7 @@ void mymain(void) { /* dev callbacks for testing purpose, type devx in the cli and the corresponding function will be called */ void dev1_callback(void) { + gyro_init(&gyro); } void dev2_callback(void) {