Commit a95a9be9 authored by Daniel Kampert's avatar Daniel Kampert 👷

- Update comments

- Add some more error handling
parent 769733d7
......@@ -47,9 +47,9 @@ int main(void)
{
}
while(1)
{
}
while(1)
{
}
return 0;
}
\ No newline at end of file
......@@ -36,7 +36,7 @@
*/
static unsigned char _LineBuffer[PARSER_MAX_DATA_BYTES];
/** @brief Intel hex parser line object.
/** @brief Data block object for the parsing engine.
*/
static Parser_Block_t _Line = {
.pBuffer = _LineBuffer,
......@@ -114,41 +114,31 @@ Bool_t Bootloader_Enter(void)
do
{
if(Parser_GetByte(Bootloader_GetChar()) == PARSER_STATE_SUCCESSFULL)
if(Parser_GetByte(Bootloader_GetChar()) == PARSER_STATE_SUCCESSFUL)
{
// Disable the transmitter
Bootloader_PutChar(XOFF);
// Parsing successfully?
if(Parser_Parse(&_Line) == PARSER_STATE_SUCCESSFULL)
if((Parser_Parse(&_Line) == PARSER_STATE_SUCCESSFUL) && _Line.Valid)
{
switch(_Line.Type)
if(_Line.Type == PARSER_TYPE_DATA)
{
case(PARSER_TYPE_DATA):
uint32_t Address = (_Line.Offset + _Line.Address) >> 0x01;
Offset = Address & 0xFF;
Page = (Address & 0x3FF00) >> 0x08;
for(uint8_t i = 0x00; i < _Line.Length; i += 0x02)
{
uint32_t Address = (_Line.Offset + _Line.Address) >> 0x01;
Offset = Address & 0xFF;
Page = (Address & 0x3FF00) >> 0x08;
uint16_t CodeWord = (_LineBuffer[i + 1] << 0x08) | _LineBuffer[i];
NVM_LoadFlashBuffer(Offset + (i >> 0x01), CodeWord);
for(uint8_t i = 0x00; i < _Line.Bytes; i += 0x02)
// Write the page when the buffer is full
if(Words++ == (APP_SECTION_PAGE_SIZE / 2))
{
uint16_t CodeWord = (_LineBuffer[i + 1] << 0x08) | _LineBuffer[i];
NVM_LoadFlashBuffer(Offset + (i >> 0x01), CodeWord);
// Write the page when the buffer is full
Words++;
if(Words == (APP_SECTION_PAGE_SIZE / 2))
{
NVM_FlushFlash(Page);
Words = 0x00;
}
NVM_FlushFlash(Page);
Words = 0x00;
}
break;
}
default:
{
break;
}
}
}
......@@ -174,7 +164,7 @@ void Bootloader_Exit(void)
Bootloader_PutString("Leave bootloader...");
// Wait until last transmission finish
while(!(((USART_t*)(&USART_NAME(BOOTLOADER_INTERFACE)))->STATUS & USART_DREIF_bm));
//while(!(((USART_t*)(&USART_NAME(BOOTLOADER_INTERFACE)))->STATUS & USART_DREIF_bm));
// Disable the SPM command
NVM_LockSPM();
......
......@@ -48,17 +48,19 @@ typedef enum
*/
static uint8_t _ParserBuffer[PARSER_LENGTH_BYTES + PARSER_ADDRESS_BYTES + PARSER_TYPE_BYTES + (PARSER_MAX_DATA_BYTES * PARSER_DATA_BYTES) + PARSER_CHECK_BYTES];
/** @brief Current state for the parser state machine.
/** @brief Boolean value to flag the parser as active.
*/
static StateMachine_t _ParserState;
static Bool_t _Active;
static Bool_t _IsActive;
/** @brief Current line buffer index.
*/
static uint8_t _Index;
/** @brief
/** @brief Current state for the parser state machine.
*/
static StateMachine_t _ParserState;
/** @brief Current state of the parsing engine.
*/
static Parser_State_t _ParserEngineState;
......@@ -70,11 +72,11 @@ static Parser_State_t _ParserEngineState;
static uint16_t Hex2Num(const uint8_t Length)
{
uint16_t Temp = 0x00;
for(uint8_t i = 0x00; i < Length; i++)
{
uint8_t c = _ParserBuffer[_Index++];
if(c >= '0' && c <= '9')
{
c -= '0';
......@@ -87,7 +89,7 @@ static uint16_t Hex2Num(const uint8_t Length)
{
c -= 'a' - 10;
}
Temp = (Temp << 0x04) + c;
}
......@@ -97,20 +99,20 @@ static uint16_t Hex2Num(const uint8_t Length)
void Parser_Init(void)
{
_ParserState = PARSER_INIT;
_Active = FALSE;
_IsActive = FALSE;
}
Parser_State_t Parser_GetByte(const uint8_t Received)
{
if(_Active)
if(_IsActive)
{
if(Received == PARSER_LINE_END)
{
_Index = 0x00;
_Active = FALSE;
_IsActive = FALSE;
_ParserState = PARSER_INIT;
return PARSER_STATE_SUCCESSFULL;
return PARSER_STATE_SUCCESSFUL;
}
if(_Index < sizeof(_ParserBuffer))
......@@ -129,7 +131,7 @@ Parser_State_t Parser_GetByte(const uint8_t Received)
if(Received == ':')
{
_Index = 0x00;
_Active = TRUE;
_IsActive = TRUE;
}
return PARSER_STATE_BUSY;
......@@ -146,7 +148,7 @@ Parser_State_t Parser_Parse(Parser_Block_t* Line)
case PARSER_INIT:
{
Line->Checksum = 0x00;
Line->Bytes = 0x00;
Line->Length = 0x00;
_ParserState = PARSER_GET_SIZE;
......@@ -155,7 +157,7 @@ Parser_State_t Parser_Parse(Parser_Block_t* Line)
case PARSER_GET_SIZE:
{
uint8_t Length = Hex2Num(PARSER_LENGTH_BYTES);
Line->Bytes = Length;
Line->Length = Length;
Line->Checksum += Length;
_ParserState = PARSER_GET_ADDRESS;
......@@ -182,7 +184,7 @@ Parser_State_t Parser_Parse(Parser_Block_t* Line)
{
case PARSER_TYPE_DATA:
{
for(uint8_t i = 0x00; i < Line->Bytes; i++)
for(uint8_t i = 0x00; i < Line->Length; i++)
{
uint8_t Data = Hex2Num(PARSER_DATA_BYTES);
*(Line->pBuffer + i) = Data;
......@@ -215,7 +217,7 @@ Parser_State_t Parser_Parse(Parser_Block_t* Line)
Line->Offset <<= 0x04;
_ParserState = PARSER_GET_CHECK;
break;
}
case PARSER_TYPE_SSA:
......@@ -260,7 +262,7 @@ Parser_State_t Parser_Parse(Parser_Block_t* Line)
if(Line->Checksum == Checksum_Temp)
{
Line->Valid = TRUE;
_ParserEngineState = PARSER_STATE_SUCCESSFULL;
_ParserEngineState = PARSER_STATE_SUCCESSFUL;
}
else
{
......@@ -278,7 +280,7 @@ Parser_State_t Parser_Parse(Parser_Block_t* Line)
}
}
}
while (_ParserEngineState == PARSER_STATE_BUSY);
while(_ParserEngineState == PARSER_STATE_BUSY);
return _ParserEngineState;
}
\ No newline at end of file
......@@ -24,7 +24,7 @@
/** @file main.c
* @brief Example application for the bootloader application.
* NOTE: This application gets mapped at start address 0x1FFFE.
* NOTE: This application gets mapped at start address 0x10000.
*
* Software for the XMega bootloader example from
* https://www.kampis-elektroecke.de/2020/03/bootloader-fuer-avr-xmega/
......@@ -43,9 +43,9 @@ int main(void)
*/
GPIO_SetDirection(&PORTR, 0, GPIO_DIRECTION_OUT);
while(1)
{
while(1)
{
GPIO_Toggle(&PORTR, 0);
for(uint16_t i = 0x00; i < 0xFFFF; i++);
}
}
}
\ No newline at end of file
......@@ -52,7 +52,7 @@
typedef enum
{
PARSER_STATE_BUSY = 0x00, /**< Busy */
PARSER_STATE_SUCCESSFULL = 0x01, /**< Valid record line */
PARSER_STATE_SUCCESSFUL = 0x01, /**< Parsing successful */
PARSER_STATE_ERROR = 0x02, /**< Error while parsing the line */
PARSER_STATE_OVERFLOW = 0x03, /**< Buffer overflow during line receive */
} Parser_State_t;
......@@ -73,8 +73,8 @@
*/
typedef struct
{
uint16_t Bytes; /**< Byte count */
uint16_t Address; /**< Address */
uint16_t Length; /**< Data byte count */
uint16_t Address; /**< Memory address */
uint32_t Offset; /**< Offset address */
uint32_t StartAddress; /**< Start address */
Parser_Type_t Type; /**< Record type */
......@@ -92,7 +92,7 @@
* @return #PARSER_STATE_SUCCESSFULL when line end
*/
Parser_State_t Parser_GetByte(const uint8_t Received);
/** @brief Parse the line buffer into a #IntelHexParser_Line_t object. Must be called after #IntelHexParser_GetLine.
* @param Line Pointer to line object
* @return Parser state. Must be polled until #PARSER_STATE_SUCCESSFULL or #PARSER_STATE_ERROR is returned
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment