esp32_BNO08x/source/BNO08x.cpp

1026 lines
30 KiB
C++
Raw Normal View History

2023-11-07 05:10:02 +00:00
#include "BNO08x.hpp"
2024-11-15 07:48:33 +00:00
#include "BNO08x_macros.hpp"
2023-11-07 05:10:02 +00:00
/**
2023-11-16 08:43:45 +00:00
* @brief BNO08x imu constructor.
*
2023-11-07 05:10:02 +00:00
* Construct a BNO08x object for managing a BNO08x sensor.
2023-11-16 08:43:45 +00:00
*
2023-11-07 05:10:02 +00:00
* @param imu_config Configuration settings (optional), default settings can be seen in bno08x_config_t
* @return void, nothing to return
*/
2023-11-16 08:43:45 +00:00
BNO08x::BNO08x(bno08x_config_t imu_config)
2024-11-14 06:42:21 +00:00
: spi_task_hdl(NULL)
, data_proc_task_hdl(NULL)
, evt_grp_spi(xEventGroupCreate())
, evt_grp_report_en(xEventGroupCreate())
2024-11-14 06:42:21 +00:00
, evt_grp_task_flow(xEventGroupCreate())
, queue_rx_data(xQueueCreate(1, sizeof(sh2_packet_t)))
, queue_tx_data(xQueueCreate(1, sizeof(sh2_packet_t)))
, imu_config(imu_config)
2024-11-14 06:42:21 +00:00
{
}
2024-11-17 05:36:11 +00:00
/**
* @brief BNO08x imu deconstructor.
*
* Deconstructs a BNO08x object and releases any utilized resources.
*
* @return void, nothing to return.
2024-11-17 05:36:11 +00:00
*/
2024-11-14 06:42:21 +00:00
BNO08x::~BNO08x()
{
// disable interrupts before beginning so we can ensure SPI task doesn't attempt to run
gpio_intr_disable(imu_config.io_int);
2024-11-14 09:36:28 +00:00
// delete any tasks if they have been created
ESP_ERROR_CHECK(kill_all_tasks());
// deinitialize spi if has been initialized
ESP_ERROR_CHECK(deinit_spi());
// deinitialize hint ISR if it has been initialized
ESP_ERROR_CHECK(deinit_hint_isr());
// deinitialize GPIO if they have been initialized
ESP_ERROR_CHECK(deinit_gpio());
2024-11-14 06:42:21 +00:00
// delete queues
vQueueDelete(queue_rx_data);
vQueueDelete(queue_tx_data);
// delete event groups
vEventGroupDelete(evt_grp_spi);
vEventGroupDelete(evt_grp_report_en);
vEventGroupDelete(evt_grp_task_flow);
// clear callback list
cb_list.clear();
}
/**
* @brief Initializes BNO08x sensor
*
2024-11-17 05:36:11 +00:00
* Resets sensor and goes through initialization process.
* Configures GPIO, required ISRs, and launches two tasks, one to manage SPI transactions, another to process any received data.
2024-11-14 06:42:21 +00:00
*
* @return True if initialization was success, false if otherwise.
2024-11-14 06:42:21 +00:00
*/
bool BNO08x::initialize()
{
2024-11-14 09:36:28 +00:00
// initialize configuration arguments
if (init_config_args() != ESP_OK)
return false;
2024-11-14 06:42:21 +00:00
2024-11-14 09:36:28 +00:00
// initialize GPIO
if (init_gpio() != ESP_OK)
return false;
2024-11-14 06:42:21 +00:00
2024-11-14 09:36:28 +00:00
// initialize HINT ISR
if (init_hint_isr() != ESP_OK)
2024-11-14 06:42:21 +00:00
return false;
// initialize SPI
2024-11-14 09:36:28 +00:00
if (init_spi() != ESP_OK)
2024-11-14 06:42:21 +00:00
return false;
// launch tasks
if (launch_tasks() != ESP_OK)
return false;
// reset BNO08x
if (!hard_reset())
return false;
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGI(TAG, "Successfully initialized....");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return true;
2024-11-14 06:42:21 +00:00
}
2024-11-17 05:36:11 +00:00
/**
* @brief Initializes required esp-idf SPI data structures with values from user passed bno08x_config_t struct.
*
* @return ESP_OK if initialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::init_config_args()
{
2024-11-14 09:36:28 +00:00
if ((imu_config.io_cs == GPIO_NUM_NC))
2024-11-14 06:42:21 +00:00
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, CS GPIO cannot be unassigned.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ESP_ERR_INVALID_ARG;
2024-11-14 06:42:21 +00:00
}
2024-11-14 09:36:28 +00:00
if ((imu_config.io_miso == GPIO_NUM_NC))
2024-11-14 06:42:21 +00:00
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, MISO GPIO cannot be unassigned.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ESP_ERR_INVALID_ARG;
2024-11-14 06:42:21 +00:00
}
2024-11-14 09:36:28 +00:00
if ((imu_config.io_mosi == GPIO_NUM_NC))
2024-11-14 06:42:21 +00:00
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, MOSI GPIO cannot be unassigned.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ESP_ERR_INVALID_ARG;
2024-11-14 06:42:21 +00:00
}
2024-11-14 09:36:28 +00:00
if ((imu_config.io_sclk == GPIO_NUM_NC))
2024-11-14 06:42:21 +00:00
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, SCLK GPIO cannot be unassigned.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ESP_ERR_INVALID_ARG;
2024-11-14 06:42:21 +00:00
}
2024-11-14 09:36:28 +00:00
if ((imu_config.io_rst == GPIO_NUM_NC))
2024-11-14 06:42:21 +00:00
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 06:42:21 +00:00
ESP_LOGE(TAG, "RST GPIO cannot be unassigned.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ESP_ERR_INVALID_ARG;
2024-11-14 06:42:21 +00:00
}
2023-11-07 05:10:02 +00:00
2023-11-16 08:43:45 +00:00
// SPI bus config
bus_config.mosi_io_num = imu_config.io_mosi; // assign mosi gpio pin
bus_config.miso_io_num = imu_config.io_miso; // assign miso gpio pin
bus_config.sclk_io_num = imu_config.io_sclk; // assign sclk gpio pin
bus_config.quadhd_io_num = -1; // hold signal gpio (not used)
bus_config.quadwp_io_num = -1; // write protect signal gpio (not used)
2023-11-07 05:10:02 +00:00
2023-11-16 08:43:45 +00:00
// 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)
2023-11-07 05:10:02 +00:00
2024-11-14 06:42:21 +00:00
if (imu_config.sclk_speed > SCLK_MAX_SPEED) // max sclk speed of 3MHz for BNO08x
2023-11-07 05:10:02 +00:00
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 06:42:21 +00:00
ESP_LOGE(TAG, "Max SPI clock speed exceeded, %ld overwritten with 3MHz", imu_config.sclk_speed);
#endif
// clang-format on
2024-11-14 06:42:21 +00:00
imu_config.sclk_speed = SCLK_MAX_SPEED;
2023-11-16 08:43:45 +00:00
}
2023-11-07 05:10:02 +00:00
imu_spi_config.clock_source = SPI_CLK_SRC_DEFAULT;
imu_spi_config.clock_speed_hz = imu_config.sclk_speed; // assign SCLK speed
imu_spi_config.address_bits = 0; // 0 address bits, not using this system
imu_spi_config.command_bits = 0; // 0 command bits, not using this system
imu_spi_config.spics_io_num = -1; // due to esp32 silicon issue, chip select cannot be used with full-duplex mode
// driver, it must be handled via calls to gpio pins
2024-11-14 09:36:28 +00:00
imu_spi_config.queue_size = static_cast<int>(CONFIG_ESP32_BNO08X_SPI_QUEUE_SZ); // set max allowable queued SPI transactions
2024-11-14 06:42:21 +00:00
2024-11-14 09:36:28 +00:00
return ESP_OK;
2024-11-14 06:42:21 +00:00
}
2024-11-17 05:36:11 +00:00
/**
* @brief Initializes required gpio inputs.
*
* @return ESP_OK if initialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::init_gpio_inputs()
2024-11-14 06:42:21 +00:00
{
esp_err_t ret = ESP_OK;
2024-11-14 09:36:28 +00:00
// configure input(s) (HINT)
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_DISABLE;
inputs_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
inputs_config.intr_type = GPIO_INTR_NEGEDGE;
ret = gpio_config(&inputs_config);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, failed to configure HINT gpio.");
#endif
// clang-format on
}
2024-11-14 09:36:28 +00:00
else
{
2024-11-14 09:36:28 +00:00
init_status.gpio_inputs = true; // set gpio_inputs to initialized such that deconstructor knows to clean them up
}
2024-11-14 09:36:28 +00:00
return ret;
}
2024-11-17 05:36:11 +00:00
/**
* @brief Initializes required gpio outputs.
*
* @return ESP_OK if initialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::init_gpio_outputs()
{
esp_err_t ret = ESP_OK;
2023-11-16 08:43:45 +00:00
2024-11-14 06:42:21 +00:00
// configure output(s) (CS, RST, and WAKE)
2023-11-07 05:10:02 +00:00
gpio_config_t outputs_config;
2024-11-14 06:42:21 +00:00
outputs_config.pin_bit_mask = (imu_config.io_wake != GPIO_NUM_NC)
? ((1ULL << imu_config.io_cs) | (1ULL << imu_config.io_rst) | (1ULL << imu_config.io_wake))
: ((1ULL << imu_config.io_cs) | (1ULL << imu_config.io_rst));
2023-11-16 08:43:45 +00:00
outputs_config.mode = GPIO_MODE_OUTPUT;
2023-11-07 05:10:02 +00:00
outputs_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
outputs_config.pull_up_en = GPIO_PULLUP_DISABLE;
outputs_config.intr_type = GPIO_INTR_DISABLE;
2024-11-14 06:42:21 +00:00
ret = gpio_config(&outputs_config);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, failed to configure CS, RST, and WAKE (if used) gpio.");
#endif
// clang-format on
}
2024-11-14 09:36:28 +00:00
else
{
2024-11-14 09:36:28 +00:00
init_status.gpio_outputs = true; // set gpio_inputs to initialized such that deconstructor knows to clean them up
}
2024-11-14 09:36:28 +00:00
return ret;
}
2023-11-07 05:10:02 +00:00
2024-11-17 05:36:11 +00:00
/**
* @brief Initializes required gpio.
*
* @return ESP_OK if initialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::init_gpio()
{
esp_err_t ret = ESP_OK;
2024-11-14 06:42:21 +00:00
2024-11-14 09:36:28 +00:00
/*GPIO config for pins not controlled by SPI peripheral*/
ret = init_gpio_outputs();
if (ret != ESP_OK)
return ret;
ret = init_gpio_inputs();
2024-11-14 06:42:21 +00:00
if (ret != ESP_OK)
return ret;
2023-11-07 05:10:02 +00:00
2024-11-14 06:42:21 +00:00
gpio_set_level(imu_config.io_cs, 1);
gpio_set_level(imu_config.io_rst, 1);
2023-11-07 05:10:02 +00:00
2024-11-14 06:42:21 +00:00
if (imu_config.io_wake != GPIO_NUM_NC)
gpio_set_level(imu_config.io_wake, 1);
2023-11-07 05:10:02 +00:00
2024-11-14 06:42:21 +00:00
return ret;
2023-11-07 05:10:02 +00:00
}
2024-11-17 05:36:11 +00:00
/**
* @brief Initializes host interrupt ISR.
*
* @return ESP_OK if initialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::init_hint_isr()
2024-11-14 06:42:21 +00:00
{
esp_err_t ret = ESP_OK;
2024-11-14 02:21:16 +00:00
2024-11-14 06:42:21 +00:00
// check if installation of ISR service has been requested by user (default is true)
if (imu_config.install_isr_service)
ret = gpio_install_isr_service(0); // install isr service
2024-11-14 02:21:16 +00:00
2024-11-14 06:42:21 +00:00
if (ret != ESP_OK)
2024-11-14 02:21:16 +00:00
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, failed to install global ISR service.");
#endif
// clang-format on
2024-11-14 06:42:21 +00:00
return ret;
2024-11-14 02:21:16 +00:00
}
2024-11-14 09:36:28 +00:00
else
{
init_status.isr_service = true; // set isr service to initialized such that deconstructor knows to clean it up (this will be ignored if
// imu_config.install_isr_service == false)
}
2024-11-14 02:21:16 +00:00
2024-11-14 06:42:21 +00:00
ret = gpio_isr_handler_add(imu_config.io_int, hint_handler, (void*) this);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, failed to add hint_handler ISR.");
#endif
// clang-format on
2024-11-14 06:42:21 +00:00
return ret;
}
2024-11-14 09:36:28 +00:00
else
{
init_status.isr_handler = true; // set isr handler to initialized such that deconstructor knows to clean it up
}
2024-11-14 02:21:16 +00:00
2024-11-14 06:42:21 +00:00
gpio_intr_disable(imu_config.io_int); // disable interrupts initially before reset
2024-11-14 02:21:16 +00:00
2024-11-14 06:42:21 +00:00
return ret;
}
2024-11-14 02:21:16 +00:00
2024-11-17 05:36:11 +00:00
/**
* @brief Initializes SPI.
*
* @return ESP_OK if initialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::init_spi()
{
2024-11-14 06:42:21 +00:00
esp_err_t ret = ESP_OK;
uint8_t tx_buffer[50] = {0}; // for dummy transaction to stabilize SPI peripheral
2024-11-14 06:42:21 +00:00
// initialize the spi peripheral
ret = spi_bus_initialize(imu_config.spi_peripheral, &bus_config, SPI_DMA_CH_AUTO);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, SPI bus failed to initialize.");
#endif
// clang-format on
2024-11-14 06:42:21 +00:00
return ret;
}
2024-11-14 09:36:28 +00:00
else
{
init_status.spi_bus = true;
}
2023-11-16 08:43:45 +00:00
2024-11-14 06:42:21 +00:00
// add the imu device to the bus
ret = spi_bus_add_device(imu_config.spi_peripheral, &imu_spi_config, &spi_hdl);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, failed to add device to SPI bus.");
#endif
// clang-format on
2024-11-14 06:42:21 +00:00
return ret;
2023-11-07 05:10:02 +00:00
}
2024-11-14 09:36:28 +00:00
else
{
init_status.spi_device = true;
}
2023-11-07 05:10:02 +00:00
2024-11-14 06:42:21 +00:00
// do first SPI operation into nowhere before BNO085 reset to let periphiral stabilize (Anton B.)
spi_transaction.length = 8;
spi_transaction.rxlength = 0;
spi_transaction.tx_buffer = tx_buffer;
spi_transaction.rx_buffer = NULL;
spi_transaction.flags = 0;
spi_device_polling_transmit(spi_hdl, &spi_transaction); // send data packet
return ret;
}
2024-11-17 05:36:11 +00:00
/**
* @brief Deinitializes GPIO, called from deconstructor.
*
* @return ESP_OK if deinitialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::deinit_gpio()
{
esp_err_t ret = ESP_OK;
if (init_status.gpio_inputs)
{
ret = deinit_gpio_inputs();
if (ret != ESP_OK)
return ret;
}
if (init_status.gpio_outputs)
{
ret = deinit_gpio_outputs();
if (ret != ESP_OK)
return ret;
}
return ret;
}
2024-11-17 05:36:11 +00:00
/**
* @brief Deinitializes GPIO inputs, called from deconstructor.
*
* @return ESP_OK if deinitialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::deinit_gpio_inputs()
{
esp_err_t ret = ESP_OK;
ret = gpio_reset_pin(imu_config.io_int);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
ESP_LOGE(TAG, "Initialization failed, failed to add device to SPI bus.");
#endif
// clang-format on
}
2024-11-14 09:36:28 +00:00
return ret;
}
2024-11-17 05:36:11 +00:00
/**
* @brief Deinitializes GPIO outputs, called from deconstructor.
*
* @return ESP_OK if deinitialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::deinit_gpio_outputs()
{
esp_err_t ret = ESP_OK;
if (imu_config.io_wake != GPIO_NUM_NC)
{
ret = gpio_reset_pin(imu_config.io_wake);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Deconstruction failed, could reset gpio WAKE pin to default state.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ret;
}
}
ret = gpio_reset_pin(imu_config.io_cs);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Deconstruction failed, could reset gpio CS pin to default state.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ret;
}
ret = gpio_reset_pin(imu_config.io_rst);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Deconstruction failed, could reset gpio RST pin to default state.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ret;
}
return ret;
}
2024-11-17 05:36:11 +00:00
/**
* @brief Deinitializes host interrupt ISR, called from deconstructor.
*
* @return ESP_OK if deinitialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::deinit_hint_isr()
{
esp_err_t ret = ESP_OK;
if (init_status.isr_handler)
{
ret = gpio_isr_handler_remove(imu_config.io_int);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Deconstruction failed, could not remove hint ISR handler.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ret;
}
}
if (init_status.isr_service)
{
// only remove the ISR service if it was requested to be installed by user
if (imu_config.install_isr_service)
{
gpio_uninstall_isr_service();
}
}
return ret;
}
2024-11-17 05:36:11 +00:00
/**
* @brief Deinitializes SPI.
*
* @return ESP_OK if deinitialization was success.
*/
2024-11-14 09:36:28 +00:00
esp_err_t BNO08x::deinit_spi()
{
esp_err_t ret = ESP_OK;
if (init_status.spi_device)
{
ret = spi_bus_remove_device(spi_hdl);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Deconstruction failed, could not remove spi device.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ret;
}
}
if (init_status.spi_bus)
{
ret = spi_bus_free(imu_config.spi_peripheral);
if (ret != ESP_OK)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Deconstruction failed, could free SPI peripheral.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ret;
}
}
return ret;
}
2023-11-07 05:10:02 +00:00
/**
2023-11-16 08:43:45 +00:00
* @brief Hard resets BNO08x sensor.
*
2024-11-19 21:24:24 +00:00
* @return True if reset succeeded.
2023-11-07 05:10:02 +00:00
*/
bool BNO08x::hard_reset()
{
2024-11-19 21:24:24 +00:00
bool reset_success = false;
2024-11-17 03:37:35 +00:00
// resetting disables all reports
xEventGroupClearBits(evt_grp_report_en, EVT_GRP_RPT_ALL_BITS);
2023-11-07 05:10:02 +00:00
gpio_set_level(imu_config.io_cs, 1);
if (imu_config.io_wake != GPIO_NUM_NC)
gpio_set_level(imu_config.io_wake, 1);
gpio_set_level(imu_config.io_rst, 0); // set reset pin low
2024-11-14 21:29:46 +00:00
vTaskDelay(HARD_RESET_DELAY_MS); // 10ns min, set to larger delay to let things stabilize(Anton)
gpio_set_level(imu_config.io_rst, 1); // bring out of reset
2023-11-07 05:10:02 +00:00
// Receive advertisement message on boot (see SH2 Ref. Manual 5.2 & 5.3)
2024-11-19 21:24:24 +00:00
return reset_success;
2023-11-07 05:10:02 +00:00
}
/**
* @brief Receives/sends a SHTP packet via SPI. Sends any received packets to data_proc_task().
2023-11-16 08:43:45 +00:00
*
2023-11-07 05:10:02 +00:00
* @return void, nothing to return
*/
esp_err_t BNO08x::transmit_packet()
{
static sh2_packet_t rx_packet, tx_packet;
2024-11-16 01:50:42 +00:00
esp_err_t ret = ESP_OK;
2023-11-07 05:10:02 +00:00
if (gpio_get_level(imu_config.io_int)) // ensure INT pin is low
2024-11-16 01:50:42 +00:00
return ESP_ERR_INVALID_STATE;
gpio_set_level(imu_config.io_cs, 0); // assert chip select
if (xQueueReceive(queue_tx_data, &tx_packet, 0) == pdFALSE) // check for queued packet to be sent, non blocking
{
memset(&tx_packet, 0U, sizeof(sh2_packet_t)); // no queued packet to send, set everything to 0
}
// receive/send packet header
ret = transmit_packet_header(&rx_packet, &tx_packet);
2024-11-16 01:50:42 +00:00
if (ret != ESP_OK)
{
gpio_set_level(imu_config.io_cs, 1); // de-assert chip select
return ret;
}
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
ESP_LOGW(TAG, "packet rx length: %d", rx_packet.length);
2024-11-16 01:50:42 +00:00
#endif
// clang-format on
if (rx_packet.length == 0)
2024-11-16 01:50:42 +00:00
{
gpio_set_level(imu_config.io_cs, 1); // de-assert chip select
return ESP_ERR_INVALID_RESPONSE;
}
ret = transmit_packet_body(&rx_packet, &tx_packet);
2024-11-16 01:50:42 +00:00
if (ret == ESP_OK)
{
// tx_packet non-zero length implies one was rx'd through queue
if (tx_packet.length != 0)
xEventGroupSetBits(evt_grp_spi, EVT_GRP_SPI_TX_DONE_BIT);
xQueueSend(queue_rx_data, &rx_packet, 0); // send received data to data_proc_task
2024-11-16 01:50:42 +00:00
xEventGroupSetBits(evt_grp_spi, EVT_GRP_SPI_RX_DONE_BIT);
}
gpio_set_level(imu_config.io_cs, 1); // de-assert chip select
return ret;
}
2024-11-17 05:36:11 +00:00
/**
* @brief Receives/sends a SHTP packet header via SPI.
2024-11-17 05:36:11 +00:00
*
* @param rx_packet Pointer to packet to receive header into.
* @param tx_packet Pointer to packet with header to send.
2024-11-17 05:36:11 +00:00
*
* @return ESP_OK if receive was success.
*/
esp_err_t BNO08x::transmit_packet_header(sh2_packet_t* rx_packet, sh2_packet_t* tx_packet)
2024-11-16 01:50:42 +00:00
{
esp_err_t ret = ESP_OK;
2023-11-07 05:10:02 +00:00
// setup transaction to send/receive first 4 bytes (packet header)
spi_transaction.rx_buffer = rx_packet->header;
spi_transaction.tx_buffer = tx_packet->header;
2023-11-07 05:10:02 +00:00
spi_transaction.length = 4 * 8;
spi_transaction.rxlength = 4 * 8;
spi_transaction.flags = 0;
2024-11-16 01:50:42 +00:00
ret = spi_device_polling_transmit(spi_hdl, &spi_transaction); // receive first 4 bytes (packet header)
2023-11-16 08:43:45 +00:00
2024-11-16 01:50:42 +00:00
if (ret == ESP_OK)
{
// calculate length of packet from received header
rx_packet->length = PARSE_PACKET_LENGTH(rx_packet);
rx_packet->length &= ~(1U << 15U); // clear the MSbit
rx_packet->length -= 4; // remove 4 header bytes from rx packet length (we already read those)
if (tx_packet->length != 0)
tx_packet->length -= 4; // remove 4 header bytes from tx packet length (we already sent those)
2024-11-16 01:50:42 +00:00
}
2023-11-07 05:10:02 +00:00
2024-11-16 01:50:42 +00:00
return ret;
}
2023-11-07 05:10:02 +00:00
2024-11-17 05:36:11 +00:00
/**
* @brief Receives/sends a SHTP packet body via SPI.
2024-11-17 05:36:11 +00:00
*
* @param rx_packet Pointer to packet to save body to.
* @param packet_tx Pointer to packet with body to send.
2024-11-17 05:36:11 +00:00
*
* @return ESP_OK if receive was success.
*/
esp_err_t BNO08x::transmit_packet_body(sh2_packet_t* rx_packet, sh2_packet_t* tx_packet)
2024-11-16 01:50:42 +00:00
{
esp_err_t ret = ESP_OK;
const uint16_t transaction_length = (rx_packet->length > tx_packet->length) ? rx_packet->length : tx_packet->length;
2023-11-16 08:43:45 +00:00
// setup transacton to read the data packet
spi_transaction.rx_buffer = rx_packet->body;
spi_transaction.tx_buffer = tx_packet->body;
spi_transaction.length = transaction_length * 8;
spi_transaction.rxlength = rx_packet->length * 8;
2023-11-07 05:10:02 +00:00
spi_transaction.flags = 0;
2024-11-16 01:50:42 +00:00
ret = spi_device_polling_transmit(spi_hdl, &spi_transaction); // receive rest of packet
2024-11-16 01:50:42 +00:00
return ret;
2023-11-07 05:10:02 +00:00
}
/**
2024-11-19 21:24:24 +00:00
* @brief Registers a callback to execute when new data from a report is received.
*
2024-11-19 21:24:24 +00:00
* @param cb_fxn Pointer to the call-back function should be of void return type and void input parameters.
*
* @return void, nothing to return
*/
2024-11-19 21:24:24 +00:00
void BNO08x::register_cb(std::function<void()> cb_fxn)
{
2024-11-19 21:24:24 +00:00
cb_list.push_back(cb_fxn);
}
/**
2024-11-19 21:24:24 +00:00
* @brief Prints the header of the passed SHTP packet to serial console with ESP_LOG statement.
*
2024-11-19 21:24:24 +00:00
* @param packet The packet containing the header to be printed.
* @return void, nothing to return
*/
2024-11-19 21:24:24 +00:00
void BNO08x::print_header(sh2_packet_t* packet)
{
2024-11-19 21:24:24 +00:00
// print most recent header
ESP_LOGI(TAG,
"SHTP Header:\n\r"
" Raw 32 bit word: 0x%02X%02X%02X%02X\n\r"
" Packet Length: %d\n\r"
" Channel Number: %d\n\r"
" Sequence Number: %d\n\r"
" Channel Type: %s\n\r",
(int) packet->header[0], (int) packet->header[1], (int) packet->header[2], (int) packet->header[3], (int) (packet->length + 4),
(int) packet->header[2], (int) packet->header[3],
(packet->header[2] == 0) ? "Command"
: (packet->header[2] == 1) ? "Executable"
: (packet->header[2] == 2) ? "Control"
: (packet->header[2] == 3) ? "Sensor-report"
: (packet->header[2] == 4) ? "Wake-report"
: (packet->header[2] == 5) ? "Gyro-vector"
: "Unknown");
}
2023-11-07 05:10:02 +00:00
/**
2024-11-19 21:24:24 +00:00
* @brief Prints the passed SHTP packet to serial console with ESP_LOG statement.
2023-11-16 08:43:45 +00:00
*
2024-11-19 21:24:24 +00:00
* @param packet The packet to be printed.
2023-11-07 05:10:02 +00:00
* @return void, nothing to return
*/
2024-11-19 21:24:24 +00:00
void BNO08x::print_packet(sh2_packet_t* packet)
{
2024-11-19 21:24:24 +00:00
uint8_t i = 0;
uint16_t print_length = 0;
char packet_string[600];
char byte_string[8];
2023-11-07 05:10:02 +00:00
2024-11-19 21:24:24 +00:00
if (packet->length > 40)
print_length = 40;
else
print_length = packet->length;
2023-11-07 05:10:02 +00:00
2024-11-19 21:24:24 +00:00
sprintf(packet_string, " Body: \n\r ");
for (i = 0; i < print_length; i++)
{
2024-11-19 21:24:24 +00:00
sprintf(byte_string, " 0x%02X ", packet->body[i]);
strcat(packet_string, byte_string);
2023-11-07 05:10:02 +00:00
2024-11-19 21:24:24 +00:00
if ((i + 1) % 6 == 0) // add a newline every 6 bytes
strcat(packet_string, "\n\r ");
}
2023-11-07 05:10:02 +00:00
2024-11-19 21:24:24 +00:00
ESP_LOGI(TAG,
"SHTP Header:\n\r"
" Raw 32 bit word: 0x%02X%02X%02X%02X\n\r"
" Packet Length: %d\n\r"
" Channel Number: %d\n\r"
" Sequence Number: %d\n\r"
" Channel Type: %s\n\r"
"%s",
(int) packet->header[0], (int) packet->header[1], (int) packet->header[2], (int) packet->header[3], (int) (packet->length + 4),
(int) packet->header[2], (int) packet->header[3],
(packet->header[2] == 0) ? "Command"
: (packet->header[2] == 1) ? "Executable"
: (packet->header[2] == 2) ? "Control"
: (packet->header[2] == 3) ? "Sensor-report"
: (packet->header[2] == 4) ? "Wake-report"
: (packet->header[2] == 5) ? "Gyro-vector"
: "Unknown",
packet_string);
2024-11-14 21:29:46 +00:00
}
2023-11-07 05:10:02 +00:00
/**
2024-11-19 21:24:24 +00:00
* @brief Static function used to launch spi task.
2023-11-16 08:43:45 +00:00
*
2024-11-19 21:24:24 +00:00
* Used such that spi_task() can be non-static class member.
*
2024-11-19 21:24:24 +00:00
* @param arg void pointer to BNO08x imu object
2023-11-07 05:10:02 +00:00
* @return void, nothing to return
*/
2024-11-19 21:24:24 +00:00
void BNO08x::spi_task_trampoline(void* arg)
{
BNO08x* imu = (BNO08x*) arg; // cast argument received by xTaskCreate ("this" pointer to imu object created by constructor call)
imu->spi_task(); // launch spi task from object
2023-11-07 05:10:02 +00:00
}
/**
* @brief Task responsible for SPI transactions. Executed when HINT in is asserted by BNO08x
2023-11-16 08:43:45 +00:00
*
2023-11-07 05:10:02 +00:00
* @return void, nothing to return
*/
void BNO08x::spi_task()
{
2024-11-16 01:50:42 +00:00
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
static uint64_t prev_time = esp_timer_get_time();
static uint64_t current_time = 0;
2024-11-16 01:50:42 +00:00
#endif
// clang-format on
2024-07-24 00:01:11 +01:00
while (1)
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // block until notified by ISR (hint_handler)
2024-11-14 06:42:21 +00:00
if (CHECK_TASKS_RUNNING(evt_grp_task_flow, EVT_GRP_TSK_FLW_RUNNING_BIT)) // ensure deconstructor has not requested that task be deleted
{
2024-11-16 01:50:42 +00:00
// clang-format off
#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;
2024-11-16 01:50:42 +00:00
#endif
// clang-format on
2024-11-14 06:42:21 +00:00
transmit_packet();
2024-11-14 06:42:21 +00:00
}
2023-11-07 05:10:02 +00:00
else
2024-11-14 06:42:21 +00:00
{
// exit loop, deconstructor has requested task be deleted
break;
}
}
2024-11-14 02:21:16 +00:00
2024-11-14 06:42:21 +00:00
xSemaphoreGive(sem_kill_tasks); // signal to deconstructor deletion is completed
2024-11-14 02:21:16 +00:00
vTaskDelete(NULL);
}
/**
* @brief Static function used to launch data processing task.
*
* Used such that data_proc_task() can be non-static class member.
*
* @param arg void pointer to BNO08x imu object
* @return void, nothing to return
*/
void BNO08x::data_proc_task_trampoline(void* arg)
{
BNO08x* imu = (BNO08x*) arg; // cast argument received by xTaskCreate ("this" pointer to imu object created by constructor call)
imu->data_proc_task(); // launch data processing task task from object
}
/**
* @brief Task responsible parsing packets. Executed when SPI task sends a packet to be parsed, notifies wait_for_data() call.
*
* @return void, nothing to return
*/
void BNO08x::data_proc_task()
{
sh2_packet_t packet;
2024-11-14 06:42:21 +00:00
while (1) // receive packet from spi_task()
{
2024-11-14 06:42:21 +00:00
if (xQueueReceive(queue_rx_data, &packet, portMAX_DELAY) == pdTRUE)
{
2024-11-14 06:42:21 +00:00
if (CHECK_TASKS_RUNNING(evt_grp_task_flow, EVT_GRP_TSK_FLW_RUNNING_BIT)) // ensure deconstructor has not requested that task be deleted
2024-07-23 07:16:14 +01:00
{
2024-11-19 21:24:24 +00:00
// PROCESS RX HERE
2024-07-23 07:16:14 +01:00
}
else
2024-11-14 06:42:21 +00:00
{
// exit loop, deconstructor has requested task be deleted
break;
}
}
2023-11-07 05:10:02 +00:00
}
2024-11-14 02:21:16 +00:00
2024-11-14 06:42:21 +00:00
// self delete task
xSemaphoreGive(sem_kill_tasks); // signal to deconstructor task deletion is completed
2024-11-14 02:21:16 +00:00
vTaskDelete(NULL);
2023-11-07 05:10:02 +00:00
}
2024-11-17 05:36:11 +00:00
/**
* @brief Launches spi_task and data_proc_task on constructor call.
2024-11-17 05:36:11 +00:00
*
* @return ESP_OK if tasks successfully created.
2024-11-17 05:36:11 +00:00
*/
2024-11-14 06:42:21 +00:00
esp_err_t BNO08x::launch_tasks()
{
BaseType_t task_created = pdFALSE;
xEventGroupSetBits(evt_grp_task_flow, EVT_GRP_TSK_FLW_RUNNING_BIT); // set task flow to running
// launch data processing task
task_created = xTaskCreate(
&data_proc_task_trampoline, "bno08x_data_processing_task", CONFIG_ESP32_BNO08X_DATA_PROC_TASK_SZ, this, 7, &data_proc_task_hdl);
2024-11-14 09:36:28 +00:00
if (task_created != pdTRUE)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, data_proc_task failed to launch.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ESP_ERR_INVALID_STATE;
}
else
{
init_status.data_proc_task = true;
init_status.task_count++;
}
task_created = xTaskCreate(&spi_task_trampoline, "bno08x_spi_task", 4096, this, 8, &spi_task_hdl); // launch SPI task
2024-11-14 06:42:21 +00:00
if (task_created != pdTRUE)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Initialization failed, spi_task failed to launch.");
#endif
// clang-format on
2024-11-14 06:42:21 +00:00
return ESP_ERR_INVALID_STATE;
}
else
{
2024-11-14 09:36:28 +00:00
init_status.spi_task = true;
init_status.task_count++;
2024-11-14 06:42:21 +00:00
}
2024-11-14 09:36:28 +00:00
return ESP_OK;
2024-11-14 06:42:21 +00:00
}
2024-11-17 05:36:11 +00:00
/**
* @brief Deletes spi_task and data_proc_task safely on deconstructor call.
2024-11-17 05:36:11 +00:00
*
* @return ESP_OK if tasks successfully deleted.
2024-11-17 05:36:11 +00:00
*/
2024-11-14 06:42:21 +00:00
esp_err_t BNO08x::kill_all_tasks()
{
static const constexpr uint8_t TASK_DELETE_TIMEOUT_MS = 10;
uint8_t kill_count = 0;
sh2_packet_t dummy_packet;
2024-11-14 21:29:46 +00:00
sem_kill_tasks = xSemaphoreCreateCounting(init_status.task_count, 0);
memset(&dummy_packet, 0, sizeof(sh2_packet_t));
2024-11-14 06:42:21 +00:00
xEventGroupClearBits(
evt_grp_task_flow, EVT_GRP_TSK_FLW_RUNNING_BIT); // clear task running bit in task flow event group to request deletion of tasks
2024-11-14 09:36:28 +00:00
if (init_status.task_count != 0)
2024-11-14 06:42:21 +00:00
{
2024-11-14 09:36:28 +00:00
if (init_status.spi_task)
xTaskNotifyGive(spi_task_hdl); // notify spi task for self deletion
if (init_status.data_proc_task)
xQueueSend(queue_rx_data, &dummy_packet, 0); // send a dummy packet to wake up data_proc task for self-deletion
for (uint8_t i = 0; i < init_status.task_count; i++)
if (xSemaphoreTake(sem_kill_tasks, TASK_DELETE_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE)
kill_count++;
if (kill_count != init_status.task_count)
{
// clang-format off
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
2024-11-14 09:36:28 +00:00
ESP_LOGE(TAG, "Task deletion timed out in deconstructor call.");
#endif
// clang-format on
2024-11-14 09:36:28 +00:00
return ESP_ERR_TIMEOUT;
}
2024-11-14 06:42:21 +00:00
}
return ESP_OK;
}
2023-11-07 05:10:02 +00:00
/**
* @brief HINT interrupt service routine, handles falling edge of BNO08x HINT pin.
2023-11-16 08:43:45 +00:00
*
* ISR that launches SPI task to perform transaction upon assertion of BNO08x interrupt pin.
*
2023-11-07 05:10:02 +00:00
* @return void, nothing to return
*/
void IRAM_ATTR BNO08x::hint_handler(void* arg)
{
2023-11-07 05:10:02 +00:00
BaseType_t xHighPriorityTaskWoken = pdFALSE;
BNO08x* imu = (BNO08x*) arg; // cast argument received by gpio_isr_handler_add ("this" pointer to imu object
// created by constructor call)
2023-11-07 05:10:02 +00:00
gpio_intr_disable(imu->imu_config.io_int); // disable interrupts
vTaskNotifyGiveFromISR(imu->spi_task_hdl, &xHighPriorityTaskWoken); // notify SPI task BNO08x is ready for
// servicing
portYIELD_FROM_ISR(xHighPriorityTaskWoken); // perform context switch if necessary
2023-11-07 05:10:02 +00:00
}