esp32_BNO08x/include/BNO08x.hpp

134 lines
5.6 KiB
C++
Raw Normal View History

2024-11-17 05:36:11 +00:00
/**
* @file BNO08x.hpp
* @author Myles Parfeniuk
*/
2023-11-07 05:10:02 +00:00
#pragma once
2024-11-15 07:48:33 +00:00
// standard library includes
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <functional>
#include <vector>
2024-07-24 00:01:11 +01:00
// esp-idf includes
2023-11-16 08:43:45 +00:00
#include <esp_rom_gpio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <freertos/queue.h>
2024-11-14 02:21:16 +00:00
#include <freertos/semphr.h>
2023-11-16 08:43:45 +00:00
#include <rom/ets_sys.h>
// in-house includes
#include "BNO08x_global_types.hpp"
2024-11-20 01:02:16 +00:00
#include "BNO08xSH2HAL.hpp"
2023-11-07 05:10:02 +00:00
2024-11-17 05:36:11 +00:00
/**
* @class BNO08x
*
2024-11-17 05:36:11 +00:00
* @brief BNO08x IMU driver class.
* */
class BNO08x
{
public:
2024-11-14 02:21:16 +00:00
BNO08x(bno08x_config_t imu_config = bno08x_config_t());
2024-11-14 09:36:28 +00:00
~BNO08x();
bool initialize();
2024-11-20 01:02:16 +00:00
void hard_reset();
2024-07-23 07:16:14 +01:00
void register_cb(std::function<void()> cb_fxn);
2024-11-20 01:02:16 +00:00
void print_product_ids();
private:
2024-11-14 09:36:28 +00:00
/// @brief Holds info about which functionality has been successfully initialized (used by deconstructor during cleanup).
typedef struct bno08x_init_status_t
{
2024-11-14 21:29:46 +00:00
bool gpio_outputs; ///< True if GPIO outputs have been initialized.
bool gpio_inputs; ///< True if GPIO inputs have been initialized.
bool isr_service; ///< True if global ISR service has been initialized.
bool isr_handler; ///< True if HINT ISR handler has been initialized.
uint8_t task_count; ///< How many successfully initialized tasks (max of TSK_CNT)
bool data_proc_task; ///< True if xTaskCreate has been called successfully for data_proc_task.
bool spi_task; ///< True if xTaskCreate has been called successfully for spi_task.
bool spi_bus; ///< True if spi_bus_initialize() has been called successfully.
bool spi_device; ///< True if spi_bus_add_device() has been called successfully.
2024-11-20 01:02:16 +00:00
bool sh2_HAL; ///< True if sh2_open() has been called successfully.
2024-11-14 09:36:28 +00:00
bno08x_init_status_t()
: gpio_outputs(false)
, gpio_inputs(false)
, isr_service(false)
, isr_handler(false)
, task_count(0)
, data_proc_task(false)
, spi_task(false)
, spi_bus(false)
, spi_device(false)
{
}
} bno08x_init_status_t;
esp_err_t init_config_args();
esp_err_t init_gpio();
esp_err_t init_gpio_inputs();
esp_err_t init_gpio_outputs();
esp_err_t init_hint_isr();
esp_err_t init_spi();
2024-11-20 01:02:16 +00:00
esp_err_t init_sh2_HAL();
2024-11-14 09:36:28 +00:00
esp_err_t deinit_gpio();
esp_err_t deinit_gpio_inputs();
esp_err_t deinit_gpio_outputs();
esp_err_t deinit_hint_isr();
esp_err_t deinit_spi();
2024-11-20 01:02:16 +00:00
esp_err_t deinit_sh2_HAL();
2024-11-14 06:42:21 +00:00
2024-11-20 01:02:16 +00:00
esp_err_t wait_for_hint();
2024-11-14 09:36:28 +00:00
2024-11-20 01:02:16 +00:00
sh2_Hal_t sh2_HAL; ///< SH2 hardware abstraction layer struct for use with SH-2 lib.
2024-11-14 06:42:21 +00:00
EventGroupHandle_t
evt_grp_spi; ///<Event group for indicating when bno08x hint pin has triggered and when new data has been processed. Used by calls to sending or receiving functions.
2024-07-24 00:01:11 +01:00
std::vector<std::function<void()>> cb_list; // Vector for storing any call-back functions added with register_cb()
2024-07-23 07:16:14 +01:00
bno08x_config_t imu_config{}; ///<IMU configuration settings
spi_bus_config_t bus_config{}; ///<SPI bus GPIO configuration settings
spi_device_interface_config_t imu_spi_config{}; ///<SPI slave device settings
spi_device_handle_t spi_hdl{}; ///<SPI device handle
spi_transaction_t spi_transaction{}; ///<SPI transaction handle
2024-11-14 21:29:46 +00:00
bno08x_init_status_t
init_status; ///<Initialization status of various functionality, used by deconstructor during cleanup, set during initialization.
2024-11-20 01:02:16 +00:00
sh2_ProductIds_t product_ID; ///< TODO
sh2_SensorValue_t* sensor_report_val = NULL; ///< TODO
2024-11-20 01:02:16 +00:00
bool reset_occurred = false;
static void IRAM_ATTR hint_handler(void* arg);
2024-11-14 09:36:28 +00:00
2024-11-19 21:24:24 +00:00
static const constexpr uint16_t RX_DATA_LENGTH = 300U; ///<length buffer containing data received over spi
2024-11-17 03:37:35 +00:00
static const constexpr TickType_t HOST_INT_TIMEOUT_DEFAULT_MS =
3000UL /
portTICK_PERIOD_MS; ///<Max wait between HINT being asserted by BNO08x before transaction is considered failed (in miliseconds), when no reports are enabled (ie during reset)
2024-11-14 21:29:46 +00:00
static const constexpr TickType_t HARD_RESET_DELAY_MS =
2024-11-16 06:24:11 +00:00
100UL /
2024-11-14 21:29:46 +00:00
portTICK_PERIOD_MS; ///<How long RST pin is held low during hard reset (min 10ns according to datasheet, but should be longer for stable operation)
2024-11-14 09:36:28 +00:00
static const constexpr uint32_t SCLK_MAX_SPEED = 3000000UL; ///<Max SPI SCLK speed BNO08x is capable of
2024-11-14 02:21:16 +00:00
// evt_grp_spi bits
2024-11-20 01:02:16 +00:00
static const constexpr EventBits_t EVT_GRP_SPI_HINT_ASSERTED_BIT =
(1U << 0U); ///<When this bit is set it indicates the BNO08x has asserted its host interrupt pin, thus an SPI transaction should commence.
static const constexpr EventBits_t EVT_GRP_SPI_RX_DONE_BIT =
2024-11-20 01:02:16 +00:00
(1U << 1U); ///<When this bit is set it indicates a receive procedure has completed.
static const constexpr EventBits_t EVT_GRP_SPI_TX_DONE_BIT = (1U << 2U); ///<When this bit is set, it indicates a queued packet has been sent.
2024-11-14 06:42:21 +00:00
static const constexpr char* TAG = "BNO08x"; ///< Class tag used for serial print statements
2024-11-20 01:02:16 +00:00
friend class BNO08xSH2HAL;
2023-11-07 05:10:02 +00:00
};