refactoring, unit test stub

This commit is contained in:
myles-parfeniuk 2024-11-13 18:21:16 -08:00
parent d1dabc4d57
commit a33a53dea5
7 changed files with 88 additions and 6 deletions

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS BNO08x.cpp
INCLUDE_DIRS .
idf_component_register(SRC_DIRS "source" "tests"
INCLUDE_DIRS "include" "tests"
REQUIRES driver esp_timer)

View File

@ -10,6 +10,7 @@
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <freertos/queue.h>
#include <freertos/semphr.h>
#include <rom/ets_sys.h>
// standard library includes
@ -88,7 +89,8 @@ typedef struct bno08x_config_t
class BNO08x
{
public:
BNO08x(bno08x_config_t imu_config = default_imu_config);
BNO08x(bno08x_config_t imu_config = bno08x_config_t());
~BNO08x();
bool initialize();
bool hard_reset();
@ -327,8 +329,6 @@ class BNO08x
void print_header(bno08x_rx_packet_t* packet);
void print_packet(bno08x_rx_packet_t* packet);
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.
@ -388,6 +388,9 @@ class BNO08x
static void data_proc_task_trampoline(void* arg);
void data_proc_task();
bool kill_tasks; ///<indicates to spi_task and data_proc task that deconstructor wants them to self-delete
SemaphoreHandle_t sem_kill_tasks;
static void IRAM_ATTR hint_handler(void* arg);
static bool
isr_service_installed; ///<true of the isr service has been installed, only has to be done once regardless of how many devices are used
@ -398,6 +401,8 @@ class BNO08x
static const constexpr uint64_t HOST_INT_TIMEOUT_MS =
300ULL; ///<Max wait between HINT being asserted by BNO08x before transaction is considered failed (in miliseconds)
static const constexpr uint8_t TASK_CNT = 2;
// evt_grp_spi bits
static const constexpr EventBits_t EVT_GRP_SPI_RX_DONE_BIT =
(1 << 0); ///<When this bit is set it indicates a receive procedure has completed.

View File

@ -1,7 +1,6 @@
#include "BNO08x.hpp"
bool BNO08x::isr_service_installed = {false};
bno08x_config_t BNO08x::default_imu_config;
/**
* @brief BNO08x imu constructor.
@ -21,6 +20,7 @@ BNO08x::BNO08x(bno08x_config_t imu_config)
, queue_reset_reason(xQueueCreate(1, sizeof(uint32_t)))
, imu_config(imu_config)
, calibration_status(1)
, kill_tasks(false)
{
uint8_t tx_buffer[50] = {0};
@ -103,6 +103,46 @@ BNO08x::BNO08x(bno08x_config_t imu_config)
spi_device_polling_transmit(spi_hdl, &spi_transaction); // send data packet
}
BNO08x::~BNO08x()
{
static const constexpr uint8_t TASK_DELETE_TIMEOUT_MS = 10;
bno08x_rx_packet_t dummy_packet;
uint8_t kill_count = 0;
//disable interrupts before beginning so we can ensure SPI task doesn't attempt to run
gpio_intr_disable(imu_config.io_int);
//delete tasks
kill_tasks = true;
xTaskNotifyGive(spi_task_hdl); //notify spi task for self deletion
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 < TASK_CNT; i++)
if(xSemaphoreTake(sem_kill_tasks, TASK_DELETE_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE)
kill_count++;
if(kill_count != TASK_CNT)
{
ESP_LOGE(TAG, "Task deletion timedout in deconstructor call.");
ESP_ERROR_CHECK(ESP_ERR_TIMEOUT);
}
//delete queues
vQueueDelete(queue_rx_data);
vQueueDelete(queue_tx_data);
vQueueDelete(queue_frs_read_data);
vQueueDelete(queue_reset_reason);
//delete event groups
vEventGroupDelete(evt_grp_spi);
vEventGroupDelete(evt_grp_report_en);
//clear callback list
cb_list.clear();
}
/**
* @brief Initializes BNO08x sensor
*
@ -2785,6 +2825,9 @@ void BNO08x::spi_task()
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // block until notified by ISR (hint_handler)
if(kill_tasks)
break;
#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));
@ -2796,6 +2839,9 @@ void BNO08x::spi_task()
else
receive_packet(); // receive packet
}
xSemaphoreGive(sem_kill_tasks); //signal to deconstructor deletion is completed
vTaskDelete(NULL);
}
/**
@ -2825,6 +2871,9 @@ void BNO08x::data_proc_task()
{
if (xQueueReceive(queue_rx_data, &packet, portMAX_DELAY)) // receive packet from spi_task()
{
if(kill_tasks)
break;
if (parse_packet(&packet) != 0) // check if packet is valid
{
// execute any registered callbacks
@ -2837,6 +2886,9 @@ void BNO08x::data_proc_task()
xEventGroupSetBits(evt_grp_spi, EVT_GRP_SPI_RX_INVALID_PACKET); // indicated invalid packet to wait_for_data()
}
}
xSemaphoreGive(sem_kill_tasks); //signal to deconstructor deletion is completed
vTaskDelete(NULL);
}
/**

View File

@ -0,0 +1 @@
#include "BNO08xTestHelper.hpp"

View File

@ -0,0 +1,9 @@
#pragma once
#include "stdio.h"
#include "BNO08x.hpp"
class BNO08xTestHelper
{
};

View File

@ -0,0 +1,6 @@
#include "BNO08xTestSuite.hpp"
void BNO08xTestSuite::run_tests()
{
}

View File

@ -0,0 +1,9 @@
#pragma once
#include "BNO08xTestHelper.hpp"
class BNO08xTestSuite
{
public:
static void run_tests();
};