existing FW file check to avoid redundant transmit

+ refactro dupliacte code
This commit is contained in:
2024-02-08 02:44:26 +01:00
parent 0ab07d6572
commit 19bcaaf18c
5 changed files with 121 additions and 51 deletions

View File

@@ -4,6 +4,7 @@
#include "CLSAddress.h"
#include "fatfs.h"
#include "FirmwareUpdate.h"
#include "UsbDataHandler.h"
// Memory for the task
StaticTask_t CLS_FW_Task_cb;
@@ -22,7 +23,6 @@ const osThreadAttr_t CLS_FW_Task_attr = {
};
osMutexId_t CLS_FW_Task_FileLock = NULL;
static FirmwareUpdateArgs configuration = {0};
static FIL fw_file;
static FwFrame frame = {0};
@@ -33,15 +33,15 @@ void wait_for_start_callback(uint16_t canid, uint8_t * data, uint8_t size) {
osThreadFlagsSet(CLS_FW_Task_id, TASK_FLAG_WAIT);
}
void wait_for_start() {
void wait_for_start(FirmwareUpdateArgs * configuration) {
// setup wait_for_start_callback to get notified when the slave is ready
uint16_t ready_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration.device, CLS_CH_FW_MISO);
uint16_t ready_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration->device, CLS_CH_FW_MISO);
// wait_for_start_callback // MessageCode::Firmware(FirmwareChannel::SlaveOutMasterIn);
CanData_regEventMsg(ready_canid, wait_for_start_callback);
// setup the command to send the slave into booloader mode
uint16_t target_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration.device, CLS_CH_FW_BOOTCALL);
uint16_t target_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration->device, CLS_CH_FW_BOOTCALL);
uint8_t bootloader_call[8] = {0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab};
@@ -87,10 +87,10 @@ void running_callback_ack(uint16_t canid, uint8_t * data, uint8_t size) {
}
void running() {
uint16_t error_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration.device, CLS_CH_FW_MISO);
uint16_t ack_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration.device, CLS_CH_FW_SLAVE_FEEDBACK);
uint16_t target_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration.device, CLS_CH_FW_MOSI);
void running(FirmwareUpdateArgs * configuration) {
uint16_t error_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration->device, CLS_CH_FW_MISO);
uint16_t ack_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration->device, CLS_CH_FW_SLAVE_FEEDBACK);
uint16_t target_canid = GENERATE_CLS_ADDRESS(CLS_CODE_FIMWARE, configuration->device, CLS_CH_FW_MOSI);
CLS_BSP_TxHeaderType header = CREATE_BSP_CAN_HEADER(target_canid,CLS_BSP_DLC_BYTES_8);
CanData_regEventMsg(error_canid,running_callback_error);
@@ -129,7 +129,8 @@ void running() {
}
#define MAX_FW_UPDATES 16
osMessageQueueId_t fwUpdateQueue;
void CLSFirmwareUpdateTask_func(void *argument);
@@ -139,31 +140,54 @@ void FirmwareUpdateTask_start(FirmwareUpdateArgs args) {
CLS_FW_Task_FileLock = osMutexNew(NULL);
}
// Initialize the queue if it hasn't been initialized yet
if(fwUpdateQueue == NULL) {
fwUpdateQueue = osMessageQueueNew(MAX_FW_UPDATES, sizeof(FirmwareUpdateArgs), NULL);
}
if(CLS_FW_Task_FileLock == NULL) {
// error cant create mutex
return;
}
}
osMessageQueuePut(fwUpdateQueue, &args, 0, 0);
// check CLS_FW_Task_id is null or stopped
// only one Task can runn at once
if(CLS_FW_Task_id == NULL || osThreadGetState(CLS_FW_Task_id) == osThreadTerminated) {
configuration = args;
CLS_FW_Task_id = osThreadNew(CLSFirmwareUpdateTask_func, NULL, &CLS_FW_Task_attr);
}
}
UsbDataPacket buffer;
void CLSFirmwareUpdateTask_func(void *argument) {
char * filename = configuration.name;
if( f_open(&fw_file, filename, FA_READ) != FR_OK) {
// TODO: Logging
osThreadExit();
}
FirmwareUpdateArgs args;
osStatus_t status;
while(1) {
// Wait for a firmware update request
status = osMessageQueueGet(fwUpdateQueue, &args, NULL, 100);
// If the queue is empty, terminate the task
if (status != osOK) {
osThreadExit();
}
char * filename = args.name;
if(f_open(&fw_file, filename, FA_READ) != FR_OK) {
// TODO: Logging
continue;
}
wait_for_start();
wait_for_start(&args);
running();
running(&args);
FirmwareUpdateDone done = {
.device_id = args.device,
};
f_close(&fw_file);
osThreadExit();
//UsbDataPacketSendMessage(UsbPackageType_FIRMWAREUPDATEDONE, &buffer,FirmwareUpdateDone_fields,&done);
f_close(&fw_file);
}
}