Skip to content

Commit fd8943c

Browse files
WIP bootloader cleanup
1 parent 5cdfb8a commit fd8943c

File tree

3 files changed

+111
-114
lines changed

3 files changed

+111
-114
lines changed

engine/IO/src/luos_io.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,7 @@ error_return_t LuosIO_ConsumeMsg(const msg_t *input)
515515
//**************************************** bootloader section ****************************************
516516
case BOOTLOADER_CMD:
517517
// send data to the bootloader
518-
LuosBootloader_MsgHandler(input);
519-
return SUCCEED;
518+
return FAILED;
520519
break;
521520

522521
default:

engine/bootloader/bootloader_core.c

+110-111
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
/*******************************************************************************
2525
* Variables
2626
******************************************************************************/
27-
static bootloader_cmd_t bootloader_cmd;
2827

2928
#ifdef BOOTLOADER
3029
// variables use to save binary data in flash
@@ -38,7 +37,6 @@ uint16_t residual_space = (uint16_t)BUFFER_SIZE;
3837
uint32_t nb_bytes = 0;
3938
uint8_t crc = 0;
4039
bool load_flag = false;
41-
uint16_t source_id = 0; // used to save source_id, ie gate_id
4240
uint32_t tickstart = 0;
4341

4442
#ifndef BOOTLOADER_UPDATER
@@ -57,8 +55,9 @@ static inline uint8_t LuosBootloader_IsEnoughSpace(uint32_t);
5755
static inline void LuosBootloader_EraseMemory(void);
5856
static inline void LuosBootloader_ProcessData(void);
5957
static inline void LuosBootloader_SaveLastData(void);
60-
static void LuosBootloader_SendResponse(bootloader_cmd_t);
61-
static void LuosBootloader_SendCrc(bootloader_cmd_t, uint8_t);
58+
static void LuosBootloader_SendResponse(service_t *service, uint16_t source_id, bootloader_cmd_t response);
59+
static void LuosBootloader_SendCrc(service_t *service, uint16_t source_id, bootloader_cmd_t, uint8_t);
60+
static void LuosBootloader_MsgHandler(service_t *service, const msg_t *input);
6261
#endif
6362

6463
/******************************************************************************
@@ -91,9 +90,9 @@ void LuosBootloader_Init(void)
9190
{
9291
revision_t version = {.major = 2, .minor = 0, .build = 0};
9392
#ifdef BOOTLOADER_UPDATER
94-
Luos_CreateService(0, VOID_TYPE, "boot_updater", version);
93+
Luos_CreateService(LuosBootloader_MsgHandler, VOID_TYPE, "boot_updater", version);
9594
#else
96-
Luos_CreateService(0, VOID_TYPE, "boot_service", version);
95+
Luos_CreateService(LuosBootloader_MsgHandler, VOID_TYPE, "boot_service", version);
9796
#endif
9897

9998
// set ID node saved in flash
@@ -315,7 +314,7 @@ uint8_t compute_crc(void)
315314
* @param data : The crc value
316315
* @return None
317316
******************************************************************************/
318-
void LuosBootloader_SendCrc(bootloader_cmd_t response, uint8_t data)
317+
void LuosBootloader_SendCrc(service_t *service, uint16_t source_id, bootloader_cmd_t response, uint8_t data)
319318
{
320319
msg_t ready_msg;
321320
ready_msg.header.cmd = BOOTLOADER_RESP;
@@ -328,15 +327,15 @@ void LuosBootloader_SendCrc(bootloader_cmd_t response, uint8_t data)
328327
uint32_t tick = LuosHAL_GetSystick();
329328
while (LuosHAL_GetSystick() - tick < node->node_id)
330329
;
331-
Luos_SendMsg(0, &ready_msg);
330+
Luos_SendMsg(service, &ready_msg);
332331
}
333332

334333
/******************************************************************************
335334
* @brief Send response to the gate
336335
* @param response : The type of crc message
337336
* @return None
338337
******************************************************************************/
339-
void LuosBootloader_SendResponse(bootloader_cmd_t response)
338+
void LuosBootloader_SendResponse(service_t *service, uint16_t source_id, bootloader_cmd_t response)
340339
{
341340
msg_t ready_msg;
342341
ready_msg.header.cmd = BOOTLOADER_RESP;
@@ -348,7 +347,7 @@ void LuosBootloader_SendResponse(bootloader_cmd_t response)
348347
uint32_t tick = LuosHAL_GetSystick();
349348
while (LuosHAL_GetSystick() - tick < node->node_id)
350349
;
351-
Luos_SendMsg(0, &ready_msg);
350+
Luos_SendMsg(service, &ready_msg);
352351
}
353352

354353
/******************************************************************************
@@ -363,115 +362,115 @@ void LuosBootloader_Loop(void)
363362

364363
/******************************************************************************
365364
* @brief Message handler called from luos library
365+
* @param service : Pointer to the service which received the message
366366
* @param input : Pointer to message received from luos network
367367
* @return None
368368
******************************************************************************/
369-
void LuosBootloader_MsgHandler(const msg_t *input)
369+
void LuosBootloader_MsgHandler(service_t *service, const msg_t *input)
370370
{
371-
bootloader_cmd = input->data[0];
372-
373-
switch (bootloader_cmd)
371+
if (input->header.cmd == BOOTLOADER_CMD)
374372
{
373+
switch (input->data[0])
374+
{
375375
#ifdef WITH_BOOTLOADER
376-
case BOOTLOADER_START:
377-
// We're in the app,
378-
// set bootloader mode, save node ID and reboot
379-
LuosBootloader_JumpToBootloader();
380-
break;
376+
case BOOTLOADER_START:
377+
// We're in the app,
378+
// set bootloader mode, save node ID and reboot
379+
LuosBootloader_JumpToBootloader();
380+
break;
381381
#endif
382382
#ifdef BOOTLOADER
383-
// we're in the bootloader,
384-
// process cmd and data
385-
case BOOTLOADER_READY:
386-
source_id = input->header.source;
387-
bootloader_data_size = input->header.size - 2 * sizeof(char);
388-
Luos_Subscribe(0, (uint16_t)input->data[1]);
389-
memcpy(bootloader_data, &(input->data[2]), bootloader_data_size);
390-
391-
LuosHAL_SetMode((uint8_t)BOOT_MODE);
392-
393-
// save binary length
394-
memcpy(&nb_bytes, &bootloader_data[0], sizeof(uint32_t));
395-
// check free space in flash
396-
if (LuosBootloader_IsEnoughSpace(nb_bytes) == SUCCEED)
397-
{
398-
// send READY response
399-
LuosBootloader_SendResponse(BOOTLOADER_READY_RESP);
400-
}
401-
else
402-
{
403-
// send ERROR response
404-
LuosBootloader_SendResponse(BOOTLOADER_ERROR_SIZE);
405-
}
406-
break;
407-
408-
case BOOTLOADER_ERASE:
409-
// erase flash memory
410-
LuosBootloader_EraseMemory();
411-
// reset load flag
412-
load_flag = false;
413-
// send ERASE response
414-
LuosBootloader_SendResponse(BOOTLOADER_ERASE_RESP);
415-
break;
416-
417-
case BOOTLOADER_BIN_CHUNK:
418-
source_id = input->header.source;
419-
bootloader_data_size = input->header.size - sizeof(char);
420-
memcpy(bootloader_data, &(input->data[1]), bootloader_data_size);
421-
422-
// handle binary data
423-
LuosBootloader_ProcessData();
424-
// send ack to the Host
425-
LuosBootloader_SendResponse(BOOTLOADER_BIN_CHUNK_RESP);
426-
break;
427-
428-
case BOOTLOADER_BIN_END:
429-
source_id = input->header.source;
430-
bootloader_data_size = input->header.size - sizeof(char);
431-
memcpy(bootloader_data, &(input->data[1]), bootloader_data_size);
432-
433-
// save the current page in flash memory
434-
LuosBootloader_SaveLastData();
435-
// send ack to the Host
436-
LuosBootloader_SendResponse(BOOTLOADER_BIN_END_RESP);
437-
break;
438-
439-
case BOOTLOADER_CRC_TEST:
440-
crc = compute_crc();
441-
// send ack to the Host
442-
LuosBootloader_SendCrc(BOOTLOADER_CRC_RESP, crc);
443-
break;
444-
445-
case BOOTLOADER_APP_SAVED:
446-
// set load flag
447-
load_flag = true;
448-
Luos_Unsubscribe(0, input->header.target);
449-
break;
450-
451-
case BOOTLOADER_STOP:
452-
// wait for the command to be send to all nodes
453-
tickstart = LuosHAL_GetSystick();
454-
while ((LuosHAL_GetSystick() - tickstart) < 1000)
455-
;
456-
// save bootloader mode in flash
457-
if (load_flag || (LuosBootloader_GetMode() == APP_RELOAD_MODE))
458-
{
459-
// boot the application programmed in dedicated flash partition
460-
LuosBootloader_DeInit();
461-
LuosBootloader_JumpToApp();
462-
}
463-
else
464-
{
465-
// reboot the node
466-
LuosHAL_Reboot();
467-
}
468-
break;
383+
// we're in the bootloader,
384+
// process cmd and data
385+
case BOOTLOADER_READY:
386+
bootloader_data_size = input->header.size - 2 * sizeof(char);
387+
Luos_Subscribe(service, (uint16_t)input->data[1]);
388+
memcpy(bootloader_data, &(input->data[2]), bootloader_data_size); // why?
389+
390+
LuosHAL_SetMode((uint8_t)BOOT_MODE);
391+
392+
// save binary length
393+
memcpy(&nb_bytes, &bootloader_data[0], sizeof(uint32_t));
394+
// check free space in flash
395+
if (LuosBootloader_IsEnoughSpace(nb_bytes) == SUCCEED)
396+
{
397+
// send READY response
398+
LuosBootloader_SendResponse(service, input->header.source, BOOTLOADER_READY_RESP);
399+
}
400+
else
401+
{
402+
// send ERROR response
403+
LuosBootloader_SendResponse(service, input->header.source, BOOTLOADER_ERROR_SIZE);
404+
}
405+
break;
406+
407+
case BOOTLOADER_ERASE:
408+
// erase flash memory
409+
LuosBootloader_EraseMemory();
410+
// reset load flag
411+
load_flag = false;
412+
// send ERASE response
413+
LuosBootloader_SendResponse(service, input->header.source, BOOTLOADER_ERASE_RESP);
414+
break;
415+
416+
case BOOTLOADER_BIN_CHUNK:
417+
bootloader_data_size = input->header.size - sizeof(char);
418+
memcpy(bootloader_data, &(input->data[1]), bootloader_data_size);
419+
420+
// handle binary data
421+
LuosBootloader_ProcessData();
422+
423+
// send ack to the Host
424+
LuosBootloader_SendResponse(service, input->header.source, BOOTLOADER_BIN_CHUNK_RESP);
425+
break;
426+
427+
case BOOTLOADER_BIN_END:
428+
bootloader_data_size = input->header.size - sizeof(char);
429+
memcpy(bootloader_data, &(input->data[1]), bootloader_data_size);
430+
431+
// save the current page in flash memory
432+
LuosBootloader_SaveLastData();
433+
// send ack to the Host
434+
LuosBootloader_SendResponse(service, input->header.source, BOOTLOADER_BIN_END_RESP);
435+
break;
436+
437+
case BOOTLOADER_CRC_TEST:
438+
crc = compute_crc();
439+
// send ack to the Host
440+
LuosBootloader_SendCrc(service, input->header.source, BOOTLOADER_CRC_RESP, crc);
441+
break;
442+
443+
case BOOTLOADER_APP_SAVED:
444+
// set load flag
445+
load_flag = true;
446+
Luos_Unsubscribe(service, input->header.target);
447+
break;
448+
449+
case BOOTLOADER_STOP:
450+
// wait for the command to be send to all nodes
451+
tickstart = LuosHAL_GetSystick();
452+
while ((LuosHAL_GetSystick() - tickstart) < 1000)
453+
;
454+
// save bootloader mode in flash
455+
if (load_flag || (LuosBootloader_GetMode() == APP_RELOAD_MODE))
456+
{
457+
// boot the application programmed in dedicated flash partition
458+
LuosBootloader_DeInit();
459+
LuosBootloader_JumpToApp();
460+
}
461+
else
462+
{
463+
// reboot the node
464+
LuosHAL_Reboot();
465+
}
466+
break;
469467
#endif
470-
case BOOTLOADER_RESET:
471-
LuosHAL_SetMode((uint8_t)BOOT_MODE);
472-
LuosHAL_Reboot();
473-
break;
474-
default:
475-
break;
468+
case BOOTLOADER_RESET:
469+
LuosHAL_SetMode((uint8_t)BOOT_MODE);
470+
LuosHAL_Reboot();
471+
break;
472+
default:
473+
break;
474+
}
476475
}
477476
}

engine/bootloader/bootloader_core.h

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ void LuosBootloader_Loop(void);
6464
/******************************************************************************
6565
* @brief function used by Luos to send message to the bootloader
6666
******************************************************************************/
67-
void LuosBootloader_MsgHandler(const msg_t *input);
6867
#ifndef BOOTLOADER
6968
void LuosBootloader_JumpToBootloader(void);
7069
#endif

0 commit comments

Comments
 (0)