diff --git a/nucleo-wb55-dongle-ble/Core/Src/main.c b/nucleo-wb55-dongle-ble/Core/Src/main.c index a9a8dea..2afafb0 100644 --- a/nucleo-wb55-dongle-ble/Core/Src/main.c +++ b/nucleo-wb55-dongle-ble/Core/Src/main.c @@ -25,7 +25,6 @@ /* USER CODE BEGIN Includes */ #include -#include "usbd_cdc_if.h" #include "commands.h" /* USER CODE END Includes */ @@ -58,9 +57,7 @@ static void MX_GPIO_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void usb_receive(uint8_t *buf, uint32_t *len) { - CDC_Transmit_FS(buf, *len); // echo -} + /* USER CODE END 0 */ /** @@ -104,7 +101,10 @@ int main(void) HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); HAL_Delay(50); HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); - HAL_Delay(100); + + pop_and_execute_commands(); + + HAL_Delay(450); counter_value++; log_debug("Counter value is %d", 1, counter_value); diff --git a/nucleo-wb55-dongle-ble/USB_Device/App/usbd_cdc_if.c b/nucleo-wb55-dongle-ble/USB_Device/App/usbd_cdc_if.c index 1a107b5..8ce6621 100644 --- a/nucleo-wb55-dongle-ble/USB_Device/App/usbd_cdc_if.c +++ b/nucleo-wb55-dongle-ble/USB_Device/App/usbd_cdc_if.c @@ -129,7 +129,7 @@ static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len); static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum); /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */ -__weak void usb_receive(uint8_t *buf, uint32_t *len) {} +//__weak void usb_receive(uint8_t *buf, uint32_t *len) {} /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */ /** diff --git a/nucleo-wb55-dongle-ble/app/Command.cpp b/nucleo-wb55-dongle-ble/app/Command.cpp index 2257545..354495b 100644 --- a/nucleo-wb55-dongle-ble/app/Command.cpp +++ b/nucleo-wb55-dongle-ble/app/Command.cpp @@ -4,6 +4,7 @@ * Created on: Jul 8, 2021 * Author: Andreas Berthoud */ +#include #include "Command.hpp" #include "string.h" @@ -26,3 +27,17 @@ void Command::set_payload_length(uint16_t payload_length) { data[1] = (payload_length & 0xFF00) >> 8; data[2] = payload_length & 0x00FF; } + +std::queue command_queue; + +void push_command(const Command &command) { + command_queue.push(command); +} + +void pop_and_execute_commands() { + while (!command_queue.empty()) { + Command command = command_queue.front(); + command.send(); + command_queue.pop(); + } +} diff --git a/nucleo-wb55-dongle-ble/app/Command.hpp b/nucleo-wb55-dongle-ble/app/Command.hpp index df2da4e..fad6c30 100644 --- a/nucleo-wb55-dongle-ble/app/Command.hpp +++ b/nucleo-wb55-dongle-ble/app/Command.hpp @@ -10,6 +10,8 @@ #include +extern "C" void pop_and_execute_commands(); + typedef enum : uint8_t { COMMAND_NONE = 0, COMMAND_LOG = 0xff, @@ -36,4 +38,6 @@ private: }; +void push_command(const Command &command); + #endif /* SRC_COMMAND_H_ */ diff --git a/nucleo-wb55-dongle-ble/app/LogCommand.cpp b/nucleo-wb55-dongle-ble/app/LogCommand.cpp index a4d2c22..e3bff28 100644 --- a/nucleo-wb55-dongle-ble/app/LogCommand.cpp +++ b/nucleo-wb55-dongle-ble/app/LogCommand.cpp @@ -15,10 +15,8 @@ void log_debug(const char * format, int nargs, ...) { va_list args; va_start(args, nargs); - LogCommand command = LogCommand(format, args, LOG_LEVEL_DEBUG); + push_command(LogCommand(format, args, LOG_LEVEL_DEBUG)); va_end(args); - - command.send(); } void log_info(const char * format, int nargs, ...) { diff --git a/nucleo-wb55-dongle-ble/app/command_interpreter.c b/nucleo-wb55-dongle-ble/app/command_interpreter.c new file mode 100644 index 0000000..851c753 --- /dev/null +++ b/nucleo-wb55-dongle-ble/app/command_interpreter.c @@ -0,0 +1,30 @@ +/* + * command_interpreter.c + * + * Created on: Jul 10, 2021 + * Author: Andreas Berthoud + */ + +#include "usbd_cdc_if.h" +#include "commands.h" + +void usb_receive(uint8_t *buf, uint32_t *len) { + + CDC_Transmit_FS(buf, *len); // echo + + if (*len < 5) { + return; + } + uint8_t command_id = buf[0]; + uint16_t length = buf[1] << 8 | buf[2]; + uint8_t * payload_ptr = buf + 4; + uint8_t stop_byte = 0x1; + + if (*len >= 4 + length) { + stop_byte = buf[4+ length]; + } + + log_debug("received command, id: 0x%x", 1, command_id); + log_debug("length: %n", 1, length); + log_debug("stop_byte: 0x%x", 1, stop_byte); +} diff --git a/nucleo-wb55-dongle-ble/app/commands.h b/nucleo-wb55-dongle-ble/app/commands.h index 45f83a5..6527f97 100644 --- a/nucleo-wb55-dongle-ble/app/commands.h +++ b/nucleo-wb55-dongle-ble/app/commands.h @@ -8,6 +8,8 @@ #ifndef COMMANDS_H_ #define COMMANDS_H_ +void pop_and_execute_commands(); + void log_debug(const char * format, int nargs, ...); void log_info(const char * format, int nargs, ...); void log_warning(const char * format, int nargs, ...);