From d3b5d5b047ab97e3c9ce868e2669ac18f3f44210 Mon Sep 17 00:00:00 2001 From: Oliver Walter Date: Fri, 2 Feb 2024 02:44:38 +0100 Subject: [PATCH] added UsbDataHandler --- Application/CMakeLists.txt | 1 + Application/Tasks/CMakeLists.txt | 2 + Application/Tasks/UsbDataHandler.c | 135 +++++++++++++++++++++++++++++ Application/Tasks/UsbDataHandler.h | 21 +++++ proto/firmware.proto | 32 +++++++ 5 files changed, 191 insertions(+) create mode 100644 Application/CMakeLists.txt create mode 100644 Application/Tasks/CMakeLists.txt create mode 100644 Application/Tasks/UsbDataHandler.c create mode 100644 Application/Tasks/UsbDataHandler.h create mode 100644 proto/firmware.proto diff --git a/Application/CMakeLists.txt b/Application/CMakeLists.txt new file mode 100644 index 0000000..7126bbb --- /dev/null +++ b/Application/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(Tasks) \ No newline at end of file diff --git a/Application/Tasks/CMakeLists.txt b/Application/Tasks/CMakeLists.txt new file mode 100644 index 0000000..9b58a2d --- /dev/null +++ b/Application/Tasks/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(Tasks UsbDataHandler.c UsbDataHandler.h) +target_include_directories(Tasks PUBLIC ./) \ No newline at end of file diff --git a/Application/Tasks/UsbDataHandler.c b/Application/Tasks/UsbDataHandler.c new file mode 100644 index 0000000..c245cb7 --- /dev/null +++ b/Application/Tasks/UsbDataHandler.c @@ -0,0 +1,135 @@ + + +#include "cmsis_os2.h" +#include "usbd_cdc.h" +#include "usbd_cdc_if.h" + +#include "cmsis_os2.h" + +/* Define the task attributes */ +#define TASK_STACK_SIZE 1024 +#define TASK_PRIORITY osPriorityNormal + +/* Declare the thread function */ + + +#define MAX_PACKET_SIZE 512-4 // Define your maximum packet size +#define NUM_BUFFERS 4 // Define the number of buffers you want to use + +/* Declare static memory for the task */ +static uint32_t UsbDataHandler_TaskStack[TASK_STACK_SIZE]; +static const osThreadAttr_t UsbDataHandler_TaskAttr = { + .name = "UsbDataHandler", + .stack_mem = UsbDataHandler_TaskStack, + .stack_size = sizeof(UsbDataHandler_TaskStack), + .priority = TASK_PRIORITY +}; + + +// Static queue data structures +static osMessageQueueId_t usbDataHandlerQueue; +static uint8_t usbDataHandlerQueueControlBlock[32]; +static uint8_t usbDataHandlerQueueStorageArea[NUM_BUFFERS * sizeof(uint32_t)]; +static const osMessageQueueAttr_t usbDataHandlerQueueAttr = { + .name = "UsbDataHandlerQueue", + .attr_bits = 0, + .cb_mem = &usbDataHandlerQueueControlBlock, + .cb_size = sizeof(usbDataHandlerQueueControlBlock), + .mq_mem = usbDataHandlerQueueStorageArea, + .mq_size = sizeof(usbDataHandlerQueueStorageArea) +}; + +typedef struct { + uint16_t type; + uint16_t length; + uint8_t data[MAX_PACKET_SIZE]; +} Packet; + +typedef union { + Packet pack; + uint8_t bytes[sizeof(Packet)]; +}Packet_u; + + +Packet_u buffers[NUM_BUFFERS]; +uint32_t bufferIndex = 0; +uint32_t dataIndex = 0; +uint16_t packetLength = 0; + +void UsbDataHandler_Task(void *argument); + + +void UsbDataHander_Start(void) { + /* Create the task */ + osThreadId_t UsbDataHandler_TaskHandle = osThreadNew(UsbDataHandler_Task, NULL, &UsbDataHandler_TaskAttr); + + /* Create the message queue */ + usbDataHandlerQueue = osMessageQueueNew(NUM_BUFFERS, sizeof(uint32_t), &usbDataHandlerQueueAttr); + + + /* Check if the task was created successfully */ + if (UsbDataHandler_TaskHandle == NULL) { + /* Handle error */ + } +} + +void UsbDataHandler_Task(void *argument) { + uint32_t msg_buffer_index; + + while(1) { + /* Wait for a full packet */ + osMessageQueueGet(usbDataHandlerQueue, &msg_buffer_index, NULL, osWaitForever); + + switch (buffers[msg_buffer_index].pack.type) + { + + case 0xF01: + + break; + + case 0xF02: + break; + + case 0xF03: + break; + + case 0xF04: + break; + + default: + break; + } + + + + + } +} + + +USBD_StatusTypeDef UsbDataHandler_RxCallback(uint8_t* Buf, uint32_t Len) { + /* Copy the data into the current buffer */ + for (uint32_t i = 0; i < Len; i++) { + buffers[bufferIndex].bytes[dataIndex] = Buf[i]; + dataIndex++; + + /* Check if we have received the packet type and length */ + if (dataIndex == 4) { + /* Extract the packet length */ + packetLength = buffers[bufferIndex].pack.length; + } + + /* Check if we have received a full packet */ + if (dataIndex - 4 == packetLength) { + /* Notify the task that a full packet is received */ + osMessageQueuePut(usbDataHandlerQueue, &bufferIndex, 0, 0); + + /* Prepare for the next packet */ + bufferIndex = (bufferIndex + 1) % NUM_BUFFERS; + dataIndex = 0; + packetLength = 0; + } + } + + return USBD_OK; +} \ No newline at end of file diff --git a/Application/Tasks/UsbDataHandler.h b/Application/Tasks/UsbDataHandler.h new file mode 100644 index 0000000..6e64963 --- /dev/null +++ b/Application/Tasks/UsbDataHandler.h @@ -0,0 +1,21 @@ +#ifndef USBDATAHANDLER_H +#define USBDATAHANDLER_H + +#include "usbd_cdc_if.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +void UsbDataHandler_Start(void); + +USBD_StatusTypeDef UsbDataHandler_RxCallback(uint8_t* Buf, uint32_t Len); + + + +#ifdef __cplusplus +} +#endif + +#endif /* USBDATAHANDLER_H */ \ No newline at end of file diff --git a/proto/firmware.proto b/proto/firmware.proto new file mode 100644 index 0000000..37a4141 --- /dev/null +++ b/proto/firmware.proto @@ -0,0 +1,32 @@ +syntax = "proto2"; + +import "nanopb.proto"; + +message FirmwareStart { + required string name = 1 [(nanopb).max_size = 32]; + required uint32 size = 2; + required uint32 packages = 3; + required uint32 device_id = 4; + required uint32 crc_fw = 5; +} + +message FirmwarePackage { + required uint32 counter = 1; + required uint32 crc_pac = 2; + required uint32 device_id = 3; + required bytes data = 4 [(nanopb).max_size = 256]; +} + +message FirmwarePackageAck { + required uint32 counter = 1; + required uint32 crc_pac = 2; + required uint32 device_id = 3; + required bool ack =4; +} + +message FirmwareDone { + required uint32 size = 1; + required uint32 crc_fw = 2; + required uint32 device_id = 3; +} +