From c74f36da0c8c8b88024039bd66a44decd673d0df Mon Sep 17 00:00:00 2001 From: Paul Nykiel Date: Sat, 28 Jan 2023 19:29:42 +0100 Subject: [PATCH 1/3] Added bno05 uart requirements --- Src/Components/system.h | 2 +- Src/Drivers/bno055_uart.c | 34 ++++------- Src/Drivers/bno055_uart.h | 81 +++++++++++++++++++++++--- Tests/LowLevel/Drivers/bno055_uart.cpp | 23 -------- 4 files changed, 83 insertions(+), 57 deletions(-) diff --git a/Src/Components/system.h b/Src/Components/system.h index ec52f0a4..c4aa51a6 100644 --- a/Src/Components/system.h +++ b/Src/Components/system.h @@ -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) diff --git a/Src/Drivers/bno055_uart.c b/Src/Drivers/bno055_uart.c index d018dae1..a9a8b0ed 100644 --- a/Src/Drivers/bno055_uart.c +++ b/Src/Drivers/bno055_uart.c @@ -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; @@ -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; } diff --git a/Src/Drivers/bno055_uart.h b/Src/Drivers/bno055_uart.h index 8b3a5f2f..853e45af 100644 --- a/Src/Drivers/bno055_uart.h +++ b/Src/Drivers/bno055_uart.h @@ -29,7 +29,6 @@ typedef enum { receive_char_timeout = 0x0A, // From Library - callback_buffer_invalid, invalid_sync } bno055_response_t; @@ -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); diff --git a/Tests/LowLevel/Drivers/bno055_uart.cpp b/Tests/LowLevel/Drivers/bno055_uart.cpp index af83955a..ca458ca6 100644 --- a/Tests/LowLevel/Drivers/bno055_uart.cpp +++ b/Tests/LowLevel/Drivers/bno055_uart.cpp @@ -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([&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()); - - 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 From 5d632fb1c9f94d655aea5ea7afc3d8950f3f380b Mon Sep 17 00:00:00 2001 From: Paul Nykiel Date: Sun, 29 Jan 2023 19:13:43 +0100 Subject: [PATCH 2/3] Reordered tests --- Tests/LowLevel/Drivers/bno055_uart.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Tests/LowLevel/Drivers/bno055_uart.cpp b/Tests/LowLevel/Drivers/bno055_uart.cpp index ca458ca6..b03d2e0a 100644 --- a/Tests/LowLevel/Drivers/bno055_uart.cpp +++ b/Tests/LowLevel/Drivers/bno055_uart.cpp @@ -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; @@ -97,14 +97,13 @@ TEST(TEST_NAME, write_register__write_success) { bno055_uart_write_register(0, data, 2, bnoCallback); EXPECT_TRUE(handle.functionGotCalled()); - 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; @@ -121,10 +120,11 @@ TEST(TEST_NAME, write_register__invalid_sync) { bno055_uart_write_register(0, data, 2, bnoCallback); EXPECT_TRUE(handle.functionGotCalled()); - 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) { From 1999df706bbf5c87033af3e2feba8054425569af Mon Sep 17 00:00:00 2001 From: Paul Nykiel Date: Sun, 29 Jan 2023 19:29:21 +0100 Subject: [PATCH 3/3] Maybe fixed coverage reports --- .github/workflows/low_level_tests.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/low_level_tests.yml b/.github/workflows/low_level_tests.yml index 7bca4a39..368caccd 100644 --- a/.github/workflows/low_level_tests.yml +++ b/.github/workflows/low_level_tests.yml @@ -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 @@ -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