diff --git a/BNO08x.cpp b/BNO08x.cpp index 564629a..68482c6 100644 --- a/BNO08x.cpp +++ b/BNO08x.cpp @@ -35,10 +35,10 @@ BNO08x::BNO08x(bno08x_config_t imu_config) // SPI slave device specific config imu_spi_config.mode = 0x3; // set mode to 3 as per BNO08x datasheet (CPHA second edge, CPOL bus high when idle) - if (imu_config.sclk_speed > 3000000) // max sclk speed of 3MHz for BNO08x + if (imu_config.sclk_speed > 3000000UL) // max sclk speed of 3MHz for BNO08x { - ESP_LOGE(TAG, "Max clock speed exceeded, %lld overwritten with 3000000Hz", imu_config.sclk_speed); - imu_config.sclk_speed = 3000000; + ESP_LOGE(TAG, "Max clock speed exceeded, %ld overwritten with 3MHz", imu_config.sclk_speed); + imu_config.sclk_speed = 3000000UL; } imu_spi_config.clock_source = SPI_CLK_SRC_DEFAULT; @@ -74,7 +74,7 @@ BNO08x::BNO08x(bno08x_config_t imu_config) gpio_config_t inputs_config; inputs_config.pin_bit_mask = (1ULL << imu_config.io_int); inputs_config.mode = GPIO_MODE_INPUT; - inputs_config.pull_up_en = GPIO_PULLUP_ENABLE; + inputs_config.pull_up_en = GPIO_PULLUP_DISABLE; inputs_config.pull_down_en = GPIO_PULLDOWN_DISABLE; inputs_config.intr_type = GPIO_INTR_NEGEDGE; gpio_config(&inputs_config); @@ -116,8 +116,9 @@ bool BNO08x::initialize() // launch tasks data_proc_task_hdl = NULL; spi_task_hdl = NULL; - xTaskCreate(&data_proc_task_trampoline, "bno08x_data_processing_task", 4096, this, 7, &data_proc_task_hdl); // launch data processing task - xTaskCreate(&spi_task_trampoline, "bno08x_spi_task", 4096, this, 8, &spi_task_hdl); // launch SPI task + xTaskCreate(&data_proc_task_trampoline, "bno08x_data_processing_task", CONFIG_ESP32_BNO08X_DATA_PROC_TASK_SZ, this, 7, + &data_proc_task_hdl); // launch data processing task + xTaskCreate(&spi_task_trampoline, "bno08x_spi_task", 4096, this, 8, &spi_task_hdl); // launch SPI task if (!hard_reset()) return false; @@ -151,8 +152,9 @@ bool BNO08x::wait_for_rx_done() // wait until an interrupt has been asserted and data received or timeout has occured if (xEventGroupWaitBits(evt_grp_spi, EVT_GRP_SPI_RX_DONE_BIT, pdTRUE, pdTRUE, HOST_INT_TIMEOUT_MS / portTICK_PERIOD_MS)) { - if (imu_config.debug_en) - ESP_LOGI(TAG, "int asserted"); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGI(TAG, "int asserted"); + #endif success = true; } @@ -190,8 +192,10 @@ bool BNO08x::wait_for_data() // only return true if packet is valid if (xEventGroupGetBits(evt_grp_spi) & EVT_GRP_SPI_RX_VALID_PACKET) { - if (imu_config.debug_en) - ESP_LOGI(TAG, "Valid packet received."); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGI(TAG, "Valid packet received."); + #endif + success = true; } else @@ -224,9 +228,9 @@ bool BNO08x::wait_for_tx_done() if (xEventGroupWaitBits(evt_grp_spi, EVT_GRP_SPI_TX_DONE, pdTRUE, pdTRUE, HOST_INT_TIMEOUT_MS / portTICK_PERIOD_MS)) { - - if (imu_config.debug_en) - ESP_LOGI(TAG, "Packet sent successfully."); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGI(TAG, "Packet sent successfully."); + #endif return true; } @@ -411,8 +415,9 @@ bool BNO08x::receive_packet() packet.length = (((uint16_t) packet.header[1]) << 8) | ((uint16_t) packet.header[0]); packet.length &= ~(1 << 15); // Clear the MSbit - if (imu_config.debug_en) - ESP_LOGW(TAG, "packet rx length: %d", packet.length); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGW(TAG, "packet rx length: %d", packet.length); + #endif if (packet.length == 0) return false; @@ -853,7 +858,7 @@ bool BNO08x::data_available() /** * @brief Registers a callback to execute when new data from a report is received. * - * @param cb_fxn Pointer to the call-back function should be of void return type and void input parameters. + * @param cb_fxn Pointer to the call-back function should be of void return type and void input parameters. * @return void, nothing to return */ void BNO08x::register_cb(std::function cb_fxn) @@ -869,8 +874,9 @@ void BNO08x::register_cb(std::function cb_fxn) */ uint16_t BNO08x::parse_packet(bno08x_rx_packet_t* packet) { - if (imu_config.debug_en) - ESP_LOGE(TAG, "SHTP Header RX'd: 0x%X 0x%X 0x%X 0x%X", packet->header[0], packet->header[1], packet->header[2], packet->header[3]); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGW(TAG, "SHTP Header RX'd: 0x%X 0x%X 0x%X 0x%X", packet->header[0], packet->header[1], packet->header[2], packet->header[3]); + #endif if (packet->body[0] == SHTP_REPORT_PRODUCT_ID_RESPONSE) // check to see that product ID matches what it should { @@ -885,23 +891,26 @@ uint16_t BNO08x::parse_packet(bno08x_rx_packet_t* packet) // Check to see if this packet is a sensor reporting its data to us if (packet->header[2] == CHANNEL_REPORTS && packet->body[0] == SHTP_REPORT_BASE_TIMESTAMP) { - if (imu_config.debug_en) - ESP_LOGI(TAG, "RX'd packet, channel report"); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGI(TAG, "RX'd packet, channel report"); + #endif return parse_input_report(packet); // This will update the rawAccelX, etc variables depending on which feature // report is found } else if (packet->header[2] == CHANNEL_CONTROL) { - if (imu_config.debug_en) - ESP_LOGI(TAG, "RX'd packet, channel control"); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGI(TAG, "RX'd packet, channel control"); + #endif return parse_command_report(packet); // This will update responses to commands, calibrationStatus, etc. } else if (packet->header[2] == CHANNEL_GYRO) { - if (imu_config.debug_en) - ESP_LOGI(TAG, "Rx packet, channel gyro"); + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + ESP_LOGI(TAG, "Rx packet, channel gyro"); + #endif return parse_input_report(packet); // This will update the rawAccelX, etc variables depending on which feature // report is found @@ -2770,7 +2779,11 @@ void BNO08x::spi_task_trampoline(void* arg) */ void BNO08x::spi_task() { - static uint64_t prev_time = 0; + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + static uint64_t prev_time = esp_timer_get_time(); + static uint64_t current_time = 0; + #endif + bno08x_tx_packet_t tx_packet; while (1) @@ -2781,11 +2794,12 @@ void BNO08x::spi_task() gpio_intr_enable(imu_config.io_int); ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // block until notified by ISR (hint_handler) - if (imu_config.debug_en) - { - ESP_LOGI(TAG, "HINT asserted, time since last assertion: %llu", (esp_timer_get_time() - prev_time)); - prev_time = esp_timer_get_time(); - } + + #ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS + current_time = esp_timer_get_time(); + ESP_LOGI(TAG, "HINT asserted, time since last assertion: %llu", (current_time - prev_time)); + prev_time = current_time; + #endif if (xQueueReceive(queue_tx_data, &tx_packet, 0)) // check for queued packet to be sent, non blocking send_packet(&tx_packet); // send packet @@ -2823,9 +2837,9 @@ void BNO08x::data_proc_task() { if (parse_packet(&packet) != 0) // check if packet is valid { - //execute any registered callbacks - for(auto& cb_fxn : cb_list) - cb_fxn(); + // execute any registered callbacks + for (auto& cb_fxn : cb_list) + cb_fxn(); xEventGroupSetBits(evt_grp_spi, EVT_GRP_SPI_RX_VALID_PACKET); // indicate valid packet to wait_for_data() } diff --git a/BNO08x.hpp b/BNO08x.hpp index b5ecfe0..72b0cf9 100644 --- a/BNO08x.hpp +++ b/BNO08x.hpp @@ -1,5 +1,5 @@ #pragma once -//esp-idf includes +// esp-idf includes #include #include #include @@ -12,7 +12,7 @@ #include #include -//standard library includes +// standard library includes #include #include #include @@ -50,62 +50,28 @@ typedef struct bno08x_config_t gpio_num_t io_int; /// Host interrupt pin (connects to BNO08x INT pin) gpio_num_t io_rst; /// Reset pin (connects to BNO08x RST pin) gpio_num_t io_wake; ///GPIO Configuration. + /// Alternatively, edit the default values in "Kconfig.projbuild" bno08x_config_t() - : spi_peripheral(SPI2_HOST) - , io_mosi(GPIO_NUM_4) - , io_miso(GPIO_NUM_19) - , io_sclk(GPIO_NUM_18) - , io_cs(GPIO_NUM_5) - , io_int(GPIO_NUM_6) - , io_rst(GPIO_NUM_7) - , io_wake(GPIO_NUM_NC) - , sclk_speed(2000000UL) // 2MHz SCLK speed - , debug_en(false) - { - } -#elif defined(ESP32C6_IMU_CONFIG) - /// @brief Default IMU configuration settings constructor for ESP32-C6, add - /// add_compile_definitions("ESP32C6_IMU_CONFIG") to CMakeList to use - bno08x_config_t() - : spi_peripheral(SPI2_HOST) - , io_mosi(GPIO_NUM_22) - , io_miso(GPIO_NUM_21) - , io_sclk(GPIO_NUM_23) - , io_cs(GPIO_NUM_6) - , io_int(GPIO_NUM_4) - , io_rst(GPIO_NUM_5) - , io_wake(GPIO_NUM_NC) - , sclk_speed(2000000UL) // 2MHz SCLK speed - , debug_en(false) - { - } -#else - /// @brief Default IMU configuration settings constructor for ESP32 - bno08x_config_t() - : spi_peripheral(SPI3_HOST) - , io_mosi(GPIO_NUM_23) - , io_miso(GPIO_NUM_19) - , io_sclk(GPIO_NUM_18) - , io_cs(GPIO_NUM_33) - , io_int(GPIO_NUM_26) - , io_rst(GPIO_NUM_32) - , io_wake(GPIO_NUM_NC) - , sclk_speed(2000000UL) // 2MHz SCLK speed - // , sclk_speed(10000U), //clock slowed to see on AD2 - , debug_en(false) + : spi_peripheral((spi_host_device_t)CONFIG_ESP32_BNO08x_SPI_HOST) + , io_mosi((gpio_num_t)CONFIG_ESP32_BNO08X_GPIO_DI) // default: + , io_miso((gpio_num_t)CONFIG_ESP32_BNO08X_GPIO_SDA) // default: + , io_sclk((gpio_num_t)CONFIG_ESP32_BNO08X_GPIO_SCL) // default: + , io_cs((gpio_num_t)CONFIG_ESP32_BNO08X_GPIO_CS) // default: + , io_int((gpio_num_t)CONFIG_ESP32_BNO08X_GPIO_HINT) // default: + , io_rst((gpio_num_t)CONFIG_ESP32_BNO08X_GPIO_RST) // default: + , io_wake((gpio_num_t)CONFIG_ESP32_BNO08X_GPIO_WAKE) // default: -1 (unused) + , sclk_speed((uint32_t)CONFIG_ESP32_BNO08X_SCL_SPEED_HZ) // default: 2MH { } -#endif + /// @brief Overloaded IMU configuration settings constructor for custom pin settings bno08x_config_t(spi_host_device_t spi_peripheral, gpio_num_t io_mosi, gpio_num_t io_miso, gpio_num_t io_sclk, gpio_num_t io_cs, - gpio_num_t io_int, gpio_num_t io_rst, gpio_num_t io_wake, uint64_t sclk_speed, bool debug) + gpio_num_t io_int, gpio_num_t io_rst, gpio_num_t io_wake, uint32_t sclk_speed) : spi_peripheral(spi_peripheral) , io_mosi(io_mosi) , io_miso(io_miso) @@ -115,8 +81,6 @@ typedef struct bno08x_config_t , io_rst(io_rst) , io_wake(io_wake) , sclk_speed(sclk_speed) - , debug_en(false) - { } } bno08x_config_t; @@ -354,8 +318,6 @@ class BNO08x static bno08x_config_t default_imu_config; ///< default imu config settings - - EventGroupHandle_t evt_grp_spi; ///> cb_list; //Vector for storing any call-back functions added with register_cb() + std::vector> cb_list; // Vector for storing any call-back functions added with register_cb() uint32_t meta_data[9]; ///
  • Wiring
  • Adding to Project
  • -
  • Examples
  • +
  • Menuconfig
  • +
  • Examples
  • Documentation
  • @@ -32,7 +33,10 @@ Currently, only SPI is supported. There are no plans to support I2C due to unpre

    (back to top)

    ### Wiring -The default wiring is depicted below, it can be changed at driver initialization (see example section). +The default wiring is depicted below, it can be changed at driver initialization (see example section). + +If your ESP does not have the GPIO pin numbers depicted below, you **must change the default GPIO settings in menuconfig**. See the Menuconfig section. + ![image](esp32_BNO08x_wiring.png)

    (back to top)

    @@ -59,6 +63,22 @@ The default wiring is depicted below, it can be changed at driver initialization ```

    (back to top)

    +### Menuconfig +This library provides a menuconfig menu configured in Kconfig.projbuild. It contains settings to control the default GPIO and a few other things. + +To access the menu: + +1. Within esp-idf enabled terminal, execute the menuconfig command: + ```sh + idf.py menuconfig + +2. Scroll down to the esp_BNO08x menu and enter it, if you're using vsCode you may have to use the "j" and "k" keys instead of the arrow keys. + ![image](esp32_BNO08x_menuconfig_1.png) + +3. Modify whatever settings you'd like from the sub menus. The GPIO Configuration menu allows for the default GPIO pins to be modified, the SPI Configuration menu allows for the default SCLK frequency and host peripheral to be modified. + ![image](esp32_BNO08x_menuconfig_2.png) +

    (back to top)

    + ### Examples There are two ways data returned from the BNO08x can be accessed with this library: diff --git a/esp32_BNO08x_menuconfig_1.png b/esp32_BNO08x_menuconfig_1.png new file mode 100644 index 0000000..bb8ee15 Binary files /dev/null and b/esp32_BNO08x_menuconfig_1.png differ diff --git a/esp32_BNO08x_menuconfig_2.png b/esp32_BNO08x_menuconfig_2.png new file mode 100644 index 0000000..6cc7f43 Binary files /dev/null and b/esp32_BNO08x_menuconfig_2.png differ