New feature, menuconfig menu

This commit is contained in:
myles-parfeniuk 2024-07-23 16:01:11 -07:00
parent 46bbdbf3f5
commit d820430865
6 changed files with 178 additions and 92 deletions

View File

@ -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,7 +116,8 @@ 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(&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())
@ -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)
#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)
#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)
#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)
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
ESP_LOGW(TAG, "packet rx length: %d", packet.length);
#endif
if (packet.length == 0)
return false;
@ -869,8 +874,9 @@ void BNO08x::register_cb(std::function<void()> 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)
#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)
#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)
#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

View File

@ -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; ///<Wake pin (optional, connects to BNO08x P0)
uint64_t sclk_speed; ///<Desired SPI SCLK speed in Hz (max 3MHz)
bool debug_en; ///<Whether or not debugging print statements are enabled
uint32_t sclk_speed; ///<Desired SPI SCLK speed in Hz (max 3MHz)
#ifdef ESP32C3_IMU_CONFIG
/// @brief Default IMU configuration settings constructor for ESP32-C3, add
/// add_compile_definitions("ESP32C3_IMU_CONFIG") to CMakeList to use
/// @brief Default IMU configuration settings constructor.
/// To modify default GPIO pins, run "idf.py menuconfig" esp32_BNO08x->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; ///<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.
EventGroupHandle_t evt_grp_report_en; ///<Event group for indicating which reports are currently enabled.

90
Kconfig.projbuild Normal file
View File

@ -0,0 +1,90 @@
menu "esp32_BNO08x"
menu "GPIO Configuration"
config ESP32_BNO08X_GPIO_HINT
int "INT GPIO NUM"
range 0 50
default 26
help
Host interrupt GPIO pin connected to BNO08x INT pin.
config ESP32_BNO08X_GPIO_RST
int "RST GPIO NUM"
range 0 50
default 32
help
Reset GPIO pin connected to BNO08x RST pin.
config ESP32_BNO08X_GPIO_CS
int "CS GPIO NUM"
range 0 50
default 33
help
Chip select GPIO pin connected to BNO08x CS pin.
config ESP32_BNO08X_GPIO_SCL
int "SCL GPIO NUM"
range 0 50
default 18
help
Clock GPIO pin connected to BNO08x SCL pin.
config ESP32_BNO08X_GPIO_DI
int "DI GPIO NUM"
range 0 50
default 23
help
MOSI GPIO pin connected to BNO08x DI pin.
config ESP32_BNO08X_GPIO_SDA
int "SDA GPIO NUM"
range 0 50
default 19
help
MISO GPIO pin connected to BNO08x SDA pin.
config ESP32_BNO08X_GPIO_WAKE
int "WAKE GPIO NUM (optional, -1 == unused)"
range -1 50
default -1
help
Wake GPIO pin, connected to BNO08x P0 pin (optional)
endmenu # GPIO Config
menu "SPI Configuration"
config ESP32_BNO08x_SPI_HOST
int "SPI Host Peripheral"
range 0 2
default 2
help
SPI controller peripheral inside ESP32.
config ESP32_BNO08X_SCL_SPEED_HZ
int "SCL SPEED (HZ)"
range 10000 3000000
default 2000000
help
SPI clock speed in Hz, default 2MHz.
endmenu #SPI Configuration
config ESP32_BNO08X_DATA_PROC_TASK_SZ
int "Callback task size, (data_proc_task())"
range 1024 20480
default 4096
help
Stack size of task responsible for parsing packets and executing callbacks.
Note that callbacks should remain as short as possible, pass the data out of
the callback with a queue or save it to different variables for longer operations.
config ESP32_BNO08x_DEBUG_STATEMENTS
bool "Print debug statements."
default "n"
help
Print the various debug statements scattered throughout the code when running.
endmenu

View File

@ -10,7 +10,8 @@
<ul>
<li><a href="#wiring">Wiring</a></li>
<li><a href="#adding-to-project">Adding to Project</a></li>
<li><a href="#Examples">Examples</a></li>
<li><a href="#menuconfig">Menuconfig</a></li>
<li><a href="#examples">Examples</a></li>
</ul>
</li>
<li><a href="#documentation">Documentation</a></li>
@ -33,6 +34,9 @@ Currently, only SPI is supported. There are no plans to support I2C due to unpre
### Wiring
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)
<p align="right">(<a href="#readme-top">back to top</a>)</p>
@ -59,6 +63,22 @@ The default wiring is depicted below, it can be changed at driver initialization
```
<p align="right">(<a href="#readme-top">back to top</a>)</p>
### 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)
<p align="right">(<a href="#readme-top">back to top</a>)</p>
### Examples
There are two ways data returned from the BNO08x can be accessed with this library:

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB