first draft FirmwareHandler
FirmwareHandler has no own Task. Only uses callbacks from messges. so it run in the UsbDataHandler Task.
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
#include "UsbDataHandler.h"
|
||||
#include "cmsis_os2.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "firmware.pb.h"
|
||||
#include "usb_device.h"
|
||||
#include <pb_decode.h>
|
||||
|
||||
|
||||
/* Declare the thread function */
|
||||
#define NUM_BUFFERS 4 // Define the number of buffers you want to use
|
||||
|
||||
|
||||
/* Define the task attributes */
|
||||
#define TASK_STACK_SIZE 1024
|
||||
#define TASK_STACK_SIZE 2048
|
||||
#define TASK_PRIORITY osPriorityNormal
|
||||
/* Declare static memory for the task */
|
||||
static uint32_t UsbDataHandler_TaskStack[TASK_STACK_SIZE];
|
||||
static uint8_t UsbDataHandler_TaskCB[sizeof(StaticTask_t)];
|
||||
static const osThreadAttr_t UsbDataHandler_TaskAttr = {
|
||||
.attr_bits =0,
|
||||
.cb_mem= UsbDataHandler_TaskCB,
|
||||
.cb_size= sizeof(UsbDataHandler_TaskCB),
|
||||
.name = "UsbDataHandler",
|
||||
.stack_mem = UsbDataHandler_TaskStack,
|
||||
.stack_size = sizeof(UsbDataHandler_TaskStack),
|
||||
@@ -23,15 +27,13 @@ static const osThreadAttr_t UsbDataHandler_TaskAttr = {
|
||||
|
||||
// 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)};
|
||||
.cb_mem = 0,
|
||||
.cb_size = 0,
|
||||
.mq_mem = 0,
|
||||
.mq_size = 0};
|
||||
|
||||
|
||||
|
||||
@@ -50,10 +52,10 @@ union {
|
||||
} mem_msg_decode;
|
||||
|
||||
|
||||
static Packet_u buffers[NUM_BUFFERS];
|
||||
static uint32_t bufferIndex = 0;
|
||||
static uint32_t dataIndex = 0;
|
||||
static uint16_t packetLength = 0;
|
||||
Packet_u buffers[NUM_BUFFERS];
|
||||
uint32_t USB_bufferIndex = 0;
|
||||
uint32_t USB_dataIndex = 0;
|
||||
uint16_t USB_packetLength = 0;
|
||||
|
||||
|
||||
void UsbDataHandler_Task(void *argument);
|
||||
@@ -72,7 +74,6 @@ void UsbDataHandler_Start() {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
MX_USB_DEVICE_Init();
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +90,7 @@ typedef struct {
|
||||
|
||||
|
||||
#define MESSAGE_HANDLER(type, message) \
|
||||
{ type, DataClbk_##message, &mem_msg_decode.msg_##message, message##_size, message##_fields }
|
||||
{ type, DataClbk_##message, &mem_msg_decode.msg_##message, sizeof( message ), message##_fields }
|
||||
|
||||
|
||||
message_handler_t message_handlers[] = {
|
||||
@@ -101,6 +102,10 @@ message_handler_t message_handlers[] = {
|
||||
|
||||
|
||||
void UsbDataHandler_Task(void *argument) {
|
||||
MX_USB_DEVICE_Init();
|
||||
USB_bufferIndex = 0;
|
||||
USB_dataIndex = 0;
|
||||
USB_packetLength = 0;
|
||||
while (1) {
|
||||
UsbDataHandler_Runner();
|
||||
}
|
||||
@@ -115,7 +120,7 @@ void UsbDataHandler_Runner() {
|
||||
osStatus_t msg_stat = osMessageQueueGet(usbDataHandlerQueue, &msg_buffer_index, NULL, osWaitForever);
|
||||
if (msg_stat != osOK) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
pb_istream_t stream =
|
||||
pb_istream_from_buffer(buffers[msg_buffer_index].pack.data, buffers[msg_buffer_index].pack.head.length);
|
||||
@@ -136,37 +141,37 @@ void UsbDataHandler_Runner() {
|
||||
int 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++;
|
||||
buffers[USB_bufferIndex].bytes[USB_dataIndex] = Buf[i];
|
||||
USB_dataIndex++;
|
||||
|
||||
/* Check if we have received the packet type and length */
|
||||
if (dataIndex == sizeof(UsbDataPacketHead)) {
|
||||
if (USB_dataIndex == sizeof(UsbDataPacketHead)) {
|
||||
/* Extract the packet length */
|
||||
packetLength = buffers[bufferIndex].pack.head.length;
|
||||
USB_packetLength = buffers[USB_bufferIndex].pack.head.length;
|
||||
|
||||
// the header checksum is invalid
|
||||
if (!UsbDataPacket_head_check(&buffers[bufferIndex].pack)) {
|
||||
dataIndex = 0;
|
||||
packetLength = 0;
|
||||
if (!UsbDataPacket_head_check(&buffers[USB_bufferIndex].pack)) {
|
||||
USB_dataIndex = 0;
|
||||
USB_packetLength = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (packetLength >= MAX_PACKET_SIZE) {
|
||||
dataIndex = 0;
|
||||
packetLength = 0;
|
||||
if (USB_packetLength >= MAX_PACKET_SIZE) {
|
||||
USB_dataIndex = 0;
|
||||
USB_packetLength = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check if we have received a full packet */
|
||||
if (dataIndex - sizeof(UsbDataPacketHead) == packetLength) {
|
||||
if (USB_dataIndex - sizeof(UsbDataPacketHead) == USB_packetLength) {
|
||||
/* Notify the task that a full packet is received */
|
||||
osMessageQueuePut(usbDataHandlerQueue, &bufferIndex, 0, 0);
|
||||
osMessageQueuePut(usbDataHandlerQueue, &USB_bufferIndex, 0, 0);
|
||||
|
||||
/* Prepare for the next packet */
|
||||
bufferIndex = (bufferIndex + 1) % NUM_BUFFERS;
|
||||
dataIndex = 0;
|
||||
packetLength = 0;
|
||||
USB_bufferIndex = (USB_bufferIndex + 1) % NUM_BUFFERS;
|
||||
USB_dataIndex = 0;
|
||||
USB_packetLength = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -185,7 +190,7 @@ bool UsbDataPacket_head_check(const UsbDataPacket *p) {
|
||||
}
|
||||
|
||||
|
||||
__attribute__((weak)) void DataClbk_FirmwareStart(void *msg, uint32_t length) {}
|
||||
__attribute__((weak)) void DataClbk_FirmwarePackage(void *msg, uint32_t length) {}
|
||||
__attribute__((weak)) void DataClbk_FirmwarePackageAck(void *msg, uint32_t length) {}
|
||||
__attribute__((weak)) void DataClbk_FirmwareDone(void *msg, uint32_t length) {}
|
||||
//__attribute__((weak)) void DataClbk_FirmwareStart(void *msg, uint32_t length) {}
|
||||
//__attribute__((weak)) void DataClbk_FirmwarePackage(void *msg, uint32_t length) {}
|
||||
//__attribute__((weak)) void DataClbk_FirmwarePackageAck(void *msg, uint32_t length) {}
|
||||
//__attribute__((weak)) void DataClbk_FirmwareDone(void *msg, uint32_t length) {}
|
||||
Reference in New Issue
Block a user