First Draft of the UsbDataHandler

This commit is contained in:
2024-02-02 03:58:09 +01:00
parent d3b5d5b047
commit 43b177c468
6 changed files with 84 additions and 58 deletions

View File

@@ -1,5 +0,0 @@
{
"files.associations": {
"stm32h7xx_hal_conf.h": "c"
}
}

View File

@@ -1,2 +1,3 @@
add_library(Tasks UsbDataHandler.c UsbDataHandler.h) add_library(Tasks UsbDataHandler.c UsbDataHandler.h)
target_include_directories(Tasks PUBLIC ./) target_include_directories(Tasks PUBLIC ./)
target_link_libraries(Tasks PUBLIC PROTOS)

View File

@@ -1,18 +1,16 @@
#include "cmsis_os2.h" #include "cmsis_os2.h"
#include "usbd_cdc.h" #include "usb_device.h"
#include "usbd_cdc_if.h"
#include "cmsis_os2.h" #include <pb_decode.h>
#include "firmware.pb.h"
#include "UsbDataHandler.h"
/* Define the task attributes */ /* Define the task attributes */
#define TASK_STACK_SIZE 1024 #define TASK_STACK_SIZE 1024
#define TASK_PRIORITY osPriorityNormal #define TASK_PRIORITY osPriorityNormal
/* Declare the thread function */ /* Declare the thread function */
#define MAX_PACKET_SIZE 512-4 // Define your maximum packet size #define MAX_PACKET_SIZE 512-4 // Define your maximum packet size
#define NUM_BUFFERS 4 // Define the number of buffers you want to use #define NUM_BUFFERS 4 // Define the number of buffers you want to use
@@ -39,6 +37,7 @@ static const osMessageQueueAttr_t usbDataHandlerQueueAttr = {
.mq_size = sizeof(usbDataHandlerQueueStorageArea) .mq_size = sizeof(usbDataHandlerQueueStorageArea)
}; };
typedef struct { typedef struct {
uint16_t type; uint16_t type;
uint16_t length; uint16_t length;
@@ -51,6 +50,14 @@ typedef union {
}Packet_u; }Packet_u;
// static memory only for decoding messages
union {
FirmwareStart msg_FirmwareStart;
FirmwarePackage msg_FirmwarePackage;
FirmwarePackageAck msg_FirmwarePackageAck;
FirmwareDone msg_FirmwareDone;
} mem_msg_decode;
Packet_u buffers[NUM_BUFFERS]; Packet_u buffers[NUM_BUFFERS];
uint32_t bufferIndex = 0; uint32_t bufferIndex = 0;
uint32_t dataIndex = 0; uint32_t dataIndex = 0;
@@ -66,43 +73,53 @@ void UsbDataHander_Start(void) {
/* Create the message queue */ /* Create the message queue */
usbDataHandlerQueue = osMessageQueueNew(NUM_BUFFERS, sizeof(uint32_t), &usbDataHandlerQueueAttr); usbDataHandlerQueue = osMessageQueueNew(NUM_BUFFERS, sizeof(uint32_t), &usbDataHandlerQueueAttr);
/* Check if the task was created successfully */ /* Check if the task was created successfully */
if (UsbDataHandler_TaskHandle == NULL) { if (UsbDataHandler_TaskHandle == NULL || usbDataHandlerQueue == NULL) {
/* Handle error */ /* Handle error */
Error_Handler();
} }
MX_USB_DEVICE_Init();
} }
typedef void (*callback_func_t)(void*, uint32_t length);
typedef struct {
uint16_t type;
callback_func_t callback_func;
void *msg_ptr;
uint32_t msg_size;
const pb_msgdesc_t *fields;
} message_handler_t;
#define MESSAGE_HANDLER(type, message) {type, DataClbk_##message, &mem_msg_decode.msg_##message,message##_size , message##_fields}
message_handler_t message_handlers[] = {
MESSAGE_HANDLER(0xF01, FirmwareStart),
MESSAGE_HANDLER(0xF02, FirmwarePackage),
MESSAGE_HANDLER(0xF03, FirmwareDone),
MESSAGE_HANDLER(0xF04, FirmwarePackageAck),
};
void UsbDataHandler_Task(void *argument) { void UsbDataHandler_Task(void *argument) {
uint32_t msg_buffer_index; uint32_t msg_buffer_index;
bool status;
while(1) { while(1) {
/* Wait for a full packet */ /* Wait for a full packet */
osMessageQueueGet(usbDataHandlerQueue, &msg_buffer_index, NULL, osWaitForever); osMessageQueueGet(usbDataHandlerQueue, &msg_buffer_index, NULL, osWaitForever);
pb_istream_t stream = pb_istream_from_buffer(buffers[msg_buffer_index].pack.data, buffers[msg_buffer_index].pack.length);
switch (buffers[msg_buffer_index].pack.type) for (uint32_t i = 0; i < sizeof(message_handlers) / sizeof(message_handler_t); i++) {
{ if (buffers[msg_buffer_index].pack.type == message_handlers[i].type) {
status = pb_decode(&stream, message_handlers[i].fields, message_handlers[i].msg_ptr);
case 0xF01: if (!status) {
// Handle decode error
break; }
message_handlers[i].callback_func(message_handlers[i].msg_ptr, message_handlers[i].msg_size);
case 0xF02: break;
break; }
case 0xF03:
break;
case 0xF04:
break;
default:
break;
} }
} }
} }
@@ -132,4 +149,9 @@ USBD_StatusTypeDef UsbDataHandler_RxCallback(uint8_t* Buf, uint32_t Len) {
} }
return USBD_OK; return USBD_OK;
} }
__weak void DataClbk_FirmwareStart(void * msg, uint32_t length) {}
__weak void DataClbk_FirmwarePackage(void * msg, uint32_t length) {}
__weak void DataClbk_FirmwarePackageAck(void * msg, uint32_t length) {}
__weak void DataClbk_FirmwareDone(void * msg, uint32_t length) {}

View File

@@ -2,6 +2,7 @@
#define USBDATAHANDLER_H #define USBDATAHANDLER_H
#include "usbd_cdc_if.h" #include "usbd_cdc_if.h"
#include "firmware.pb.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -14,6 +15,11 @@ USBD_StatusTypeDef UsbDataHandler_RxCallback(uint8_t* Buf, uint32_t Len);
void DataClbk_FirmwareStart(void * msg, uint32_t length);
void DataClbk_FirmwarePackage(void * msg, uint32_t length);
void DataClbk_FirmwarePackageAck(void * msg, uint32_t length);
void DataClbk_FirmwareDone(void * msg, uint32_t length);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -61,14 +61,6 @@ set(linker_OPTS)
include("cmake_generated/cmake_generated.cmake") include("cmake_generated/cmake_generated.cmake")
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib/nanopb/extra)
find_package(Nanopb REQUIRED)
include_directories(${NANOPB_INCLUDE_DIRS})
nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS RELPATH proto proto/firmware.proto)
add_library(PROTOS ${PROTO_SRCS} ${PROTO_HDRS})
target_include_directories(PROTOS PUBLIC ${NANOPB_INCLUDE_DIRS})
# these options need to be applied for the hole project # these options need to be applied for the hole project
# so that all subprojects and libaries are compiled and compatable # so that all subprojects and libaries are compiled and compatable
add_compile_options( add_compile_options(
@@ -99,6 +91,15 @@ add_link_options(
-Wl,--print-memory-usage -Wl,--print-memory-usage
) )
# Add project symbols (macros)
add_compile_definitions(
${symbols_SYMB}
${symbols_c_SYMB}
${symbols_cxx_SYMB}
${symbols_asm_SYMB}
)
# Link directories setup # Link directories setup
# Must be before executable is added # Must be before executable is added
link_directories(${CMAKE_PROJECT_NAME} ${link_DIRS}) link_directories(${CMAKE_PROJECT_NAME} ${link_DIRS})
@@ -118,17 +119,18 @@ include_directories(
$<$<COMPILE_LANGUAGE:ASM>: ${include_asm_DIRS}> $<$<COMPILE_LANGUAGE:ASM>: ${include_asm_DIRS}>
) )
# Add project symbols (macros)
add_compile_definitions(
${symbols_SYMB}
${symbols_c_SYMB}
${symbols_cxx_SYMB}
${symbols_asm_SYMB}
)
add_subdirectory("lib") add_subdirectory("lib")
add_subdirectory("Application") add_subdirectory("Application")
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib/nanopb/extra)
find_package(Nanopb REQUIRED)
include_directories(${NANOPB_INCLUDE_DIRS})
nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS RELPATH proto proto/firmware.proto)
add_library(PROTOS ${PROTO_SRCS} ${PROTO_HDRS})
target_include_directories(PROTOS PUBLIC ${NANOPB_INCLUDE_DIRS} ${PROJECT_BINARY_DIR})
include_directories(${PROJECT_BINARY_DIR})
# Add linked libraries # Add linked libraries
# target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC protobuf-nanopb-static) # target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC protobuf-nanopb-static)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC uart_driver) target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC uart_driver)
@@ -141,10 +143,10 @@ target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
$<$<COMPILE_LANGUAGE:C>: > $<$<COMPILE_LANGUAGE:C>: >
$<$<COMPILE_LANGUAGE:CXX>: $<$<COMPILE_LANGUAGE:CXX>:
# -Wno-volatile -Wno-volatile
# -Wold-style-cast -Wold-style-cast
# -Wuseless-cast -Wuseless-cast
# -Wsuggest-override -Wsuggest-override
> >
$<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP> $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP>
$<$<CONFIG:Debug>:-Og -g3 -ggdb> $<$<CONFIG:Debug>:-Og -g3 -ggdb>

View File

@@ -86,7 +86,7 @@ void MX_USB_DEVICE_Init(void)
} }
/* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */ /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
HAL_PWREx_EnableUSBVoltageDetector(); //HAL_PWREx_EnableUSBVoltageDetector();
/* USER CODE END USB_DEVICE_Init_PostTreatment */ /* USER CODE END USB_DEVICE_Init_PostTreatment */
} }