Skip to content

Commit

Permalink
Merge pull request #60 from ToolboxPlane/Fix#58
Browse files Browse the repository at this point in the history
#58: Requirements for all functions
  • Loading branch information
aul12 authored Jan 29, 2023
2 parents 3403429 + 1999df7 commit 2d10428
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 65 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/low_level_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jobs:
with:
name: RunResults
path: build/Tests/LowLevel
- uses: actions/upload-artifact@v3
with:
name: ${{ matrix.compiler.cc }}
path: build/Tests/LowLevel

Report:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -79,7 +83,7 @@ jobs:
submodules: recursive
- uses: actions/download-artifact@v3
with:
name: RunResults
name: gcc-11
- name: Install dependencies
run: |
sudo apt-get update
Expand Down
2 changes: 1 addition & 1 deletion Src/Components/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void system_pre_init(system_timer_16_384ms_callback low_prio_callback,
* * Call the low-priority callback
* * Check the runtime, if it exceeds 12ms call
* error_handler_handle_warning(ERROR_HANDLER_GROUP_SYSTEM, SYSTEM_ERROR_RUNTIME_LOW_PRIO)
* * For the low priority timer:
* * For the high priority timer:
* * Call the high-priority callback
* * Check the runtime, if it exceeds 1ms call
* error_handler_handle_warning(ERROR_HANDLER_GROUP_SYSTEM, SYSTEM_ERROR_RUNTIME_HIGH_PRIO)
Expand Down
34 changes: 10 additions & 24 deletions Src/Drivers/bno055_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,9 @@ enum { BNO_SEND_START = 0xAA };
enum { BNO_RECEIVE_START = 0xBB };
enum { BNO_RECEIVE_ERROR = 0xEE };

enum { BNO_RECEIVE_BUF_SIZE = 2 + 8 };


static volatile uint8_t receive_buf[BNO_RECEIVE_BUF_SIZE];
static volatile bno055_callback_t bno_callback;
static volatile void *bno_result_data;

static void bno_uart_handle_response(volatile const uint8_t *data, uint8_t len, bno055_response_t response) {
if (response == read_success && len > 0) {
if (bno_result_data == 0 || data == 0) {
bno_callback(callback_buffer_invalid);
return;
}

for (uint8_t index = 0; index < len; ++index) {
((uint8_t *) bno_result_data)[index] = data[index];
}
}
bno_callback(response);
}

static void bno_uart_callback(uint8_t data) {
static uint8_t byte_in_message = 0;
static uint8_t payload_len = 0;
Expand All @@ -53,24 +35,28 @@ static void bno_uart_callback(uint8_t data) {
byte_in_message = 1;
} else {
byte_in_message = 0;
bno_uart_handle_response(0, 0, invalid_sync);
bno_callback(invalid_sync);
}
break;
case 1:
if (acknowledge_or_failure) {
byte_in_message = 0;
bno_uart_handle_response(0, 0, (bno055_response_t) data);
bno_callback(data);
} else {
payload_len = data;
byte_in_message = 2;
}
break;
default:
receive_buf[byte_in_message - 2] = data;
++byte_in_message;
if (byte_in_message >= payload_len + 2) {
*((uint8_t *) bno_result_data) = data;
bno_result_data += 1;
payload_len -= 1;

if (payload_len == 0) {
byte_in_message = 0;
bno_uart_handle_response(receive_buf, payload_len, read_success);
bno_callback(read_success);
} else {
byte_in_message += 1;
}
break;
}
Expand Down
81 changes: 72 additions & 9 deletions Src/Drivers/bno055_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ typedef enum {
receive_char_timeout = 0x0A,

// From Library
callback_buffer_invalid,
invalid_sync
} bno055_response_t;

Expand All @@ -39,36 +38,100 @@ typedef enum {
typedef void (*bno055_callback_t)(bno055_response_t);

/**
* Initialize the uart interface of the sensor.
* @brief Initialize the uart interface of the sensor.
*
* The initialization of the sensor itself (operation mode, units...) is not
* This function initializes the uart with the following values:
* * ID: 1
* * Baud rate: 115200
* * No Parity bits
* * 1 Stop Bit
* * An internal function as callback
*
* The internal callback function decodes data in accordance with the following state machine:
* \dot
*
* digraph {
* rankdir = "LR";
* INIT -> READ_SUCCESS [
* label = "data=0xBB"
* ]
*
* INIT -> READ_ERROR_OR_WRITE_RESPONSE [
* label = "data=0xEE"
* ]
*
* INIT -> INIT [
* label = "otherwise/\ncallback(invalid_sync)"
* ]
*
* READ_ERROR_OR_WRITE_RESPONSE -> INIT [
* label = "/callback(data)";
* ]
*
* READ_SUCCESS -> IN_DATA [
* label = "/length=data\nindex=0";
* ]
*
* IN_DATA -> IN_DATA [
* label = "index < length/\nbuffer[index] = data\nindex += 1";
* ]
*
* IN_DATA -> INIT [
* label = "index >= length/\ncallback(read_success)"
* ]
* }
*
* \enddot
*
* @see https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bno055-ds000.pdf Chapter 4.7
* @note The initialization of the sensor itself (operation mode, units...) is not
* done here as it is application dependent.
*/
void bno055_uart_init(void);

/**
* Write to a register of the sensor via UART.
* @brief Write to a register of the sensor via UART.
*
* This will build the uart message, send it and report the result
* with the provided callback.
* This function performs the following tasks:
* * Build the message in accordance with the BNO055-UART protocol:
* | Byte | Description | Value |
* | --- | --- | --- |
* | 1 | Start byte | 0xAA |
* | 2 | Write command | 0x00 |
* | 3 | Register address | The value of the "reg" argument |
* | 4 | Length | The value of the "len" argument |
* | 5..5+len | Data | The "len" bytes pointed to by the argument "data" |
* * Send the message via UART to ID 2.
* * Store the argument "callback" such that the internal uart callback can call this function
*
* @param reg the address of the register
* @param data pointer to the data to write
* @param len the number of bytes to write
* @param callback a function to call once the sensor sends a reply
* @see https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bno055-ds000.pdf Chapter 4.7
*/
void bno055_uart_write_register(uint8_t reg, const uint8_t *data, uint8_t len, bno055_callback_t callback);

/**
* Read data from a register via UART.
* @brief Read data from a register via UART.
*
* This will build the uart message, send it, decode the incoming
* response, write the result to the provided address and then call the callback.
* This function performs the following tasks:
* * Build the message in accordance with the BNO055-UART protocol:
* | Byte | Description | Value |
* | --- | --- | --- |
* | 1 | Start byte | 0xAA |
* | 2 | Read command | 0x01 |
* | 3 | Register address | The value of the "reg" argument |
* | 4 | Length | The value of the "len" argument |
* * Send the message via UART to ID 2.
* * Store the arguments "callback" and "out" such that the internal uart callback can call this function and write
* result data
*
* @param reg the address of the register to read from
* @param len the number of bytes to read
* @param callback a function to call once the transaction is finished
* @param result the location at which to write the result
* @see https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bno055-ds000.pdf Chapter 4.7
*/
void bno055_uart_read_register(uint8_t reg, uint8_t len, bno055_callback_t callback, void *result);

Expand Down
37 changes: 7 additions & 30 deletions Tests/LowLevel/Drivers/bno055_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void bnoCallback(bno055_response_t response) {
bnoResponse = response;
}

TEST(TEST_NAME, write_register__write_success) {
TEST(TEST_NAME, write_register__invalid_sync) {
auto handle = mock::uart.getHandle();
// Setup to capture uart-Callback
uart_callback_t uartCallback;
Expand All @@ -97,14 +97,13 @@ TEST(TEST_NAME, write_register__write_success) {
bno055_uart_write_register(0, data, 2, bnoCallback);
EXPECT_TRUE(handle.functionGotCalled<uart_send_buf>());

uartCallback(0xEE);
uartCallback(0x01);
uartCallback(0x00);

ASSERT_TRUE(bnoResponse.has_value());
EXPECT_EQ(bnoResponse.value(), bno055_response_t::write_success);
EXPECT_EQ(bnoResponse.value(), bno055_response_t::invalid_sync);
}

TEST(TEST_NAME, write_register__invalid_sync) {
TEST(TEST_NAME, write_register__write_success) {
auto handle = mock::uart.getHandle();
// Setup to capture uart-Callback
uart_callback_t uartCallback;
Expand All @@ -121,10 +120,11 @@ TEST(TEST_NAME, write_register__invalid_sync) {
bno055_uart_write_register(0, data, 2, bnoCallback);
EXPECT_TRUE(handle.functionGotCalled<uart_send_buf>());

uartCallback(0x00);
uartCallback(0xEE);
uartCallback(0x01);

ASSERT_TRUE(bnoResponse.has_value());
EXPECT_EQ(bnoResponse.value(), bno055_response_t::invalid_sync);
EXPECT_EQ(bnoResponse.value(), bno055_response_t::write_success);
}

TEST(TEST_NAME, write_register__write_response_write_fail) {
Expand Down Expand Up @@ -202,29 +202,6 @@ TEST(TEST_NAME, read_register__read_success_16bit) {
EXPECT_EQ(result, 37 + 9 * 256);
}

TEST(TEST_NAME, read_register__buffer_invalid) {
auto handle = mock::uart.getHandle();
// Setup to capture uart-Callback
uart_callback_t uartCallback;
handle.overrideFunc<uart_init>([&uartCallback](uint8_t /*id*/, uint16_t /*baud*/, uart_parity_t /*parity*/,
uint8_t /*stop*/,
uart_callback_t callback) -> void { uartCallback = callback; });

bno055_uart_init();
bnoResponse.reset();

// Actual test
bno055_uart_read_register(0, 1, bnoCallback, nullptr);
EXPECT_TRUE(handle.functionGotCalled<uart_send_buf>());

uartCallback(0xBB);
uartCallback(0x01);
uartCallback(17);

ASSERT_TRUE(bnoResponse.has_value());
EXPECT_EQ(bnoResponse.value(), bno055_response_t::callback_buffer_invalid);
}

TEST(TEST_NAME, read_register__read_fail) {
auto handle = mock::uart.getHandle();
// Setup to capture uart-Callback
Expand Down

0 comments on commit 2d10428

Please sign in to comment.