Browse Source

ble: Interprete command sent to M4

Andreas Berthoud 4 years ago
parent
commit
239ff97b38
  1. 10
      nucleo-wb55-dongle-ble/Core/Src/main.c
  2. 2
      nucleo-wb55-dongle-ble/USB_Device/App/usbd_cdc_if.c
  3. 15
      nucleo-wb55-dongle-ble/app/Command.cpp
  4. 4
      nucleo-wb55-dongle-ble/app/Command.hpp
  5. 4
      nucleo-wb55-dongle-ble/app/LogCommand.cpp
  6. 30
      nucleo-wb55-dongle-ble/app/command_interpreter.c
  7. 2
      nucleo-wb55-dongle-ble/app/commands.h

10
nucleo-wb55-dongle-ble/Core/Src/main.c

@ -25,7 +25,6 @@
/* USER CODE BEGIN Includes */
#include <stdio.h>
#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);

2
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 */
/**

15
nucleo-wb55-dongle-ble/app/Command.cpp

@ -4,6 +4,7 @@
* Created on: Jul 8, 2021
* Author: Andreas Berthoud
*/
#include <queue>
#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> 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();
}
}

4
nucleo-wb55-dongle-ble/app/Command.hpp

@ -10,6 +10,8 @@
#include <stdint.h>
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_ */

4
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, ...) {

30
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);
}

2
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, ...);

Loading…
Cancel
Save