Bootloader dev

This commit is contained in:
jfmartel 2021-07-05 21:17:33 -04:00
parent e28b725a8b
commit dd3e8a4a29
14 changed files with 8366 additions and 7688 deletions

View File

@ -6,15 +6,37 @@
#include "BoardCfg.h" #include "BoardCfg.h"
#include "timer.h" #include "timer.h"
#include "WiFiCtrl.h" #include "WiFiCtrl.h"
#include "SPI_Flash.h"
#include "FlashMapping.h"
#include "NetworkProtocol.h"
char BootloaderBuffer[200];
#define BOOTLOADER_FLASH_ERASE_POLL_TIMEOUT 25//100 //ms
#define BOOTLOADER_FLASH_ERASE_MAX_POLL_COUNT 40//10 //One sector should not take more than 1s to erase...
#define BOOTLOADER_FLASH_WRITE_POLL_TIMEOUT 25//100 //ms
#define BOOTLOADER_FLASH_WRITE_MAX_POLL_COUNT 40//10 //One sector should not take more than 1s to erase...
char BootloaderBuffer[300];
int BootloaderInterfaceState; int BootloaderInterfaceState;
int BootloaderFlashErased;
int ReadyToReceiveData;
int DataChunkWritten; int DataChunkWritten;
int CurDataChunkIndex; int CurDataChunkIndex;
int FirmwareUploaded; int FirmwareUploaded;
int CurDataChunkSize;
int BooloaderFlashEraseState;
int BootloaderFlashErased;
unsigned int BootloaderCurFlashEraseAddress;
int BooloaderFlashErasePollCount;
int BootloaderFlashWriteState;
unsigned int BootloaderCurFlashWriteAddress;
int BootloaderFlashWritePollCount;
int BootloaderFirmwareChunkWriteCount;
char* BootloaderFlashWriteDataPtr;
int BootloaderInterfaceInit() int BootloaderInterfaceInit()
{ {
@ -140,18 +162,25 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
{ {
case BOOTLOADER_SM_ERASE_FLASH_CMD: case BOOTLOADER_SM_ERASE_FLASH_CMD:
{ {
//TODO: Start flash erase process ResetBootloaderFlashEraseStateMachine(); //Setup the state machine
BootloaderProtocolSendACK(BOOTLOADER_ERASE_BOOTLOADER_FLASH_REQUEST); BootloaderProtocolSendACK(BOOTLOADER_ERASE_BOOTLOADER_FLASH_RESPONSE);
BootloaderInterfaceState = BOOTLOADER_ERASE_FLASH_STATE; BootloaderInterfaceState = BOOTLOADER_ERASE_FLASH_STATE;
printf("Bootloader Interface going into Erase Flash state\n"); printf("Bootloader Interface going into Erase Flash state\n");
break; break;
} }
case BOOTLOADER_SM_INIT_UPLOAD_CMD: case BOOTLOADER_SM_INIT_UPLOAD_CMD:
{ {
BootloaderProtocolSendInitUploadResponse();
if(BootloaderFlashErased == 0)
{
BootloaderProtocolSendInitUploadResponse(BOOTLOADEDR_INIT_TRANSFER_ERROR_FLASH_NOT_ERASED);
}
else
{
BootloaderProtocolSendInitUploadResponse(BOOTLOADEDR_INIT_TRANSFER_OK);
BootloaderInterfaceState = BOOTLOADER_RECEIVING_FIRMWARE_STATE; BootloaderInterfaceState = BOOTLOADER_RECEIVING_FIRMWARE_STATE;
printf("Bootloader Interface going into Firmware RX state\n"); printf("Bootloader Interface going into Firmware RX state\n");
ReadyToReceiveData = 1; //TODO: Manage this flag in flash SM }
break; break;
} }
case BOOTLOADER_SM_ABORT_CMD: case BOOTLOADER_SM_ABORT_CMD:
@ -161,6 +190,20 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
BootloaderResetStateMachine(); BootloaderResetStateMachine();
break; break;
} }
case BOOTLOADER_SM_EXECUTE_UPGRAGE_CMD:
{
if(FirmwareUploaded == 1)
{
//TODO: Do that!
BootloaderProtocolSendACK(BOOTLOADER_EXECUTE_UPGRADE_RESPONSE);
printf("Bootloader will now upgrade and reboot!!\n");
}
else
{
BootloaderProtocolSendNACK(BOOTLOADER_EXECUTE_UPGRADE_RESPONSE);
printf("Bootloader upgrade request denied: Firmware not uploaded\n");
}
}
} }
default: default:
{ {
@ -182,11 +225,38 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
{ {
case BOOTLOADER_TICK_EVENT: case BOOTLOADER_TICK_EVENT:
{ {
//TODO: Check flash erase operation. int res = BootloaderFlashEraseStateMachine(BOOTLOADER_FLASH_ERASE_SM_TICK_EVENT);
BootloaderFlashErased = 1; switch(res)
{
case BOOTLOADER_FLASH_ERASE_RUNNING_RES:
{
break;
}
case BOOTLOADER_FLASH_ERASE_FINISHED_RES:
{
printf("Flash erase finished. Bootloader Interface going into Active state\n"); printf("Flash erase finished. Bootloader Interface going into Active state\n");
BootloaderProtocolSendACK(BOOTLOADER_ERASE_BOOTLOADER_FLASH_RESULT_RESPONSE); //TODO: send result instead BootloaderProtocolSendACK(BOOTLOADER_ERASE_BOOTLOADER_FLASH_RESULT_RESPONSE); //TODO: send result instead
BootloaderInterfaceState = BOOTLOADER_ACTIVE_STATE; BootloaderInterfaceState = BOOTLOADER_ACTIVE_STATE;
BootloaderFlashErased = 1;
break;
}
case BOOTLOADER_FLASH_ERASE_ERROR_RES:
{
printf("Flash erase error. Bootloader Interface going into Active state\n");
BootloaderProtocolSendNACK(BOOTLOADER_ERASE_BOOTLOADER_FLASH_RESULT_RESPONSE); //TODO: send result instead
BootloaderInterfaceState = BOOTLOADER_ACTIVE_STATE;
break;
}
case BOOTLOADER_FLASH_ERASE_ABORT_RES:
{
printf("Flash erase abort. Bootloader Interface going into Active state\n");
BootloaderProtocolSendNACK(BOOTLOADER_ERASE_BOOTLOADER_FLASH_RESULT_RESPONSE); //TODO: send result instead
BootloaderInterfaceState = BOOTLOADER_ACTIVE_STATE;
break;
}
}
break; break;
} }
case BOOTLOADER_NEW_CMD_EVENT: case BOOTLOADER_NEW_CMD_EVENT:
@ -197,7 +267,8 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
{ {
//TODO: stop erasing and reset SM. //TODO: stop erasing and reset SM.
//TODO invalidate data in Flash //TODO invalidate data in Flash
printf("Aborting upload, going into STANDBY mode\n"); BootloaderFlashEraseStateMachine(BOOTLOADER_FLASH_ERASE_SM_ABORT_EVENT);
printf("Aborting Flash erase, going into STANDBY mode\n");
BootloaderResetStateMachine(); BootloaderResetStateMachine();
break; break;
} }
@ -218,18 +289,31 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
{ {
case BOOTLOADER_TICK_EVENT: case BOOTLOADER_TICK_EVENT:
{ {
//TODO: Manage flash writing process int res = BootloaderFlashWriteStateMachine(BOOTLOADER_FLASH_WRITE_SM_NEW_BUFFER_EVENT);
if(ReadyToReceiveData == 1) //TODO: replace this with flash SM result switch(res)
{ {
ReadyToReceiveData = 0; case BOOTLOADER_FLASH_WRITING_RES:
printf("Bootloader Interface ready to receive data\n"); {
BootloaderProtocolSendACK(BOOTLOADER_READY_FOR_DATA_RESPONSE); break;
} }
if(DataChunkWritten == 1) case BOOTLOADER_FLASH_WRITE_FINISHED_RES:
{ {
DataChunkWritten = 0;
BootloaderProtocolSendDataChunkResult(BOOTLOADER_CHUNK_TRANSFER_SUCCESS,CurDataChunkIndex); BootloaderProtocolSendDataChunkResult(BOOTLOADER_CHUNK_TRANSFER_SUCCESS,CurDataChunkIndex);
printf("Bootloader Chunk %d successfuly written to flash\n",CurDataChunkIndex);
CurDataChunkIndex++; CurDataChunkIndex++;
break;
}
case BOOTLOADER_FLASH_WRITE_ERROR_RES:
{
BootloaderProtocolSendDataChunkResult(BOOTLOADER_CHUNK_TRANSFER_ERROR_FLASH_ERROR,CurDataChunkIndex);
BootloaderResetStateMachine();
printf("Bootloader Flash write error. Aborting and going into STANDBY state\n");
break;
}
case BOOTLOADER_FLASH_WRITE_ABORT_RES:
{
break;
}
} }
break; break;
@ -247,6 +331,8 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
//Extract index from buffer //Extract index from buffer
int DataChunkIndex = 0; int DataChunkIndex = 0;
int DataChunkSize = 0; int DataChunkSize = 0;
DataChunkSize = 0;
DataChunkIndex = BootloaderBuffer[0]; DataChunkIndex = BootloaderBuffer[0];
DataChunkIndex <<= 8; DataChunkIndex <<= 8;
DataChunkIndex += BootloaderBuffer[1]; DataChunkIndex += BootloaderBuffer[1];
@ -263,21 +349,25 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
DataChunkSize <<= 8; DataChunkSize <<= 8;
DataChunkSize += BootloaderBuffer[7]; DataChunkSize += BootloaderBuffer[7];
BootloaderFlashWriteDataPtr = &BootloaderBuffer[8];
//Check CRC //Check CRC
//if(CurDataChunkIndex != DataChunkIndex) if(CurDataChunkIndex != DataChunkIndex)
if(0)
{ {
//Error... abort. //Error... abort.
BootloaderProtocolSendDataChunkResult(BOOTLOADER_CHUNK_TRANSFER_ERROR_INVALID_CHUNK_INDEX,CurDataChunkIndex); BootloaderProtocolSendDataChunkResult(BOOTLOADER_CHUNK_TRANSFER_ERROR_INVALID_CHUNK_INDEX,CurDataChunkIndex);
printf("Bootloader Interface ABORTING UPLOAD. Received invalid chunk index. Rx: [%d] - Expected: [%d]\n", DataChunkIndex,CurDataChunkIndex); printf("Bootloader Interface ABORTING UPLOAD. Received invalid chunk index. Rx: [%d] - Expected: [%d]\n", DataChunkIndex,CurDataChunkIndex);
BootloaderResetStateMachine(); ResetBootloaderFlashWriteStateMachine();
}
else
{
CurDataChunkSize = DataChunkSize;
BootloaderFlashWriteStateMachine(BOOTLOADER_FLASH_WRITE_SM_NEW_BUFFER_EVENT);
} }
DataChunkWritten = 1; //For debug only.
break; break;
} }
@ -296,7 +386,8 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
case BOOTLOADER_SM_ABORT_CMD: case BOOTLOADER_SM_ABORT_CMD:
{ {
//TODO invalidate data in Flash //TODO invalidate data in Flash
printf("Aborting upload, going into STANDBY mode\n"); printf("Bootloader aborting firmware download. Going back to STANDBY state\n");
BootloaderFlashWriteStateMachine(BOOTLOADER_FLASH_WRITE_SM_ABORT_EVENT);
BootloaderResetStateMachine(); BootloaderResetStateMachine();
break; break;
} }
@ -354,11 +445,16 @@ void BootloaderIterfaceStateMachine(int Event, int Param)
void BootloaderResetStateMachine() void BootloaderResetStateMachine()
{ {
BootloaderInterfaceState = BOOTLOADER_STANDBY_STATE; BootloaderInterfaceState = BOOTLOADER_STANDBY_STATE;
BootloaderFlashErased = 0;
ReadyToReceiveData = 0;
DataChunkWritten = 0; DataChunkWritten = 0;
CurDataChunkIndex = 0; CurDataChunkIndex = 0;
FirmwareUploaded = 0; FirmwareUploaded = 0;
CurDataChunkIndex = 0;
FirmwareUploaded = 0;
CurDataChunkSize = 0;
ResetBootloaderFlashEraseStateMachine();
ResetBootloaderFlashWriteStateMachine();
} }
void BootloaderActivateBootloader() void BootloaderActivateBootloader()
@ -371,3 +467,177 @@ void BootloaderDeactivateBootloader()
CloseBootloaderServer(); CloseBootloaderServer();
BootloaderIterfaceStateMachine(BOOTLOADER_SM_ABORT_CMD,0); BootloaderIterfaceStateMachine(BOOTLOADER_SM_ABORT_CMD,0);
} }
int BootloaderFlashEraseStateMachine(int event)
{
if(event == BOOTLOADER_FLASH_ERASE_SM_ABORT_EVENT)
{
ResetBootloaderFlashEraseStateMachine();
return BOOTLOADER_FLASH_ERASE_ABORT_RES;
}
switch(BooloaderFlashEraseState)
{
case BOOTLOADER_FLASH_ERASE_SECTOR_STATE:
{
if(SPIFlashErase64KSector(BootloaderCurFlashEraseAddress,0) == RET_ERROR)
{
printf("Bootloader Interface Erasing sector %0x%x\n", BootloaderCurFlashEraseAddress);
BooloaderFlashEraseState = BOOTLOADER_FLASH_ERASE_ERROR_STATE;
return BOOTLOADER_FLASH_ERASE_ERROR_RES;
}
BooloaderFlashErasePollCount = 0;
TimerStart(BOOTLOADER_FLASH_POLL_TIMER,BOOTLOADER_FLASH_ERASE_POLL_TIMEOUT);
BooloaderFlashEraseState = BOOTLOADER_FLASH_ERASE_WAIT_FOR_SECTOR_DONE;
break;
}
case BOOTLOADER_FLASH_ERASE_WAIT_FOR_SECTOR_DONE:
{
if(IsTimerExpired(BOOTLOADER_FLASH_POLL_TIMER) == 1)
{
if(SPIFlashCheckBusy() == 0) //sector erased
{
if(BootloaderCurFlashEraseAddress == FLASH_BTLDR_FIRMWARE_LAST_64K_SECTOR_ADD)
{
//Whole bootloader partition is erased.
printf("Bootloader Interface: Last sector 0x%x erased after %d polls\n",BootloaderCurFlashEraseAddress,BooloaderFlashErasePollCount);
BooloaderFlashEraseState = BOOTLOADER_FLASH_ERASE_FINISHED_STATE;
return BOOTLOADER_FLASH_ERASE_FINISHED_RES;
break;
}
else
{
printf("Bootloader Interface sector 0x%x erased after %d polls\n",BootloaderCurFlashEraseAddress,BooloaderFlashErasePollCount);
BootloaderCurFlashEraseAddress += SPI_FLASH_64K_SECTOR_SIZE;
BooloaderFlashEraseState = BOOTLOADER_FLASH_ERASE_SECTOR_STATE;
}
}
else
{
if(BooloaderFlashErasePollCount >= BOOTLOADER_FLASH_ERASE_MAX_POLL_COUNT)
{
printf("Bootloader Interface Flash erase error. Max poll count reached : %d!!!\n",BooloaderFlashErasePollCount);
BooloaderFlashEraseState = BOOTLOADER_FLASH_ERASE_ERROR_STATE;
return BOOTLOADER_FLASH_ERASE_ERROR_RES;
}
else
{
TimerStart(BOOTLOADER_FLASH_POLL_TIMER,BOOTLOADER_FLASH_ERASE_POLL_TIMEOUT);
BooloaderFlashErasePollCount++;
}
}
}
break;
}
case BOOTLOADER_FLASH_ERASE_CHECKBACK_STATE:
{
break;
}
case BOOTLOADER_FLASH_ERASE_FINISHED_STATE:
{
return BOOTLOADER_FLASH_ERASE_FINISHED_RES;
break;
}
case BOOTLOADER_FLASH_ERASE_ERROR_STATE:
{
return BOOTLOADER_FLASH_ERASE_ERROR_RES;
break;
}
}
return BOOTLOADER_FLASH_ERASE_RUNNING_RES;
}
int ResetBootloaderFlashEraseStateMachine()
{
BooloaderFlashEraseState = BOOTLOADER_FLASH_ERASE_SECTOR_STATE;
BootloaderCurFlashEraseAddress = FLASH_BTLDR_FIRMWARE_START_ADDRESS;
BooloaderFlashErasePollCount = 0;
BootloaderFlashErased = 0;
}
int BootloaderFlashWriteStateMachine(int event)
{
switch(BootloaderFlashWriteState)
{
case BOOTLOADER_FLASH_WRITE_STANDBY_STATE:
{
if(event == BOOTLOADER_FLASH_WRITE_SM_NEW_BUFFER_EVENT)
{
BootloaderFlashWriteState = BOOTLOADER_FLASH_WRITE_BUFFER_STATE;
BootloaderFirmwareChunkWriteCount = 0;
}
}
case BOOTLOADER_FLASH_WRITE_BUFFER_STATE:
{
if(BootloaderFlashWriteDataPtr == 0)
{
ResetBootloaderFlashWriteStateMachine();
return BOOTLOADER_FLASH_WRITE_ERROR_RES;
}
while(BootloaderFirmwareChunkWriteCount <= CurDataChunkSize)
{
if(SPIFlashWriteByte(BootloaderCurFlashWriteAddress,*BootloaderFlashWriteDataPtr++,0) == RET_ERROR)
{
printf("Bootloader flash error. Aborting and going back to STANDBY\n");
BootloaderFlashWriteState = BOOTLOADER_FLASH_WRITE_ERROR_STATE;
return BOOTLOADER_FLASH_WRITE_ERROR_RES;
}
int cnt = 0;
while(1)
{
if(SPIFlashCheckBusy() == 0)
{
break;
}
if(cnt++ > 200)
{
printf("Bootloader flash write timeout error. Aborting and going back to STANDBY\n");
BootloaderFlashWriteState = BOOTLOADER_FLASH_WRITE_ERROR_STATE;
return BOOTLOADER_FLASH_WRITE_ERROR_RES;
}
}
BootloaderFirmwareChunkWriteCount++;
}
BootloaderFlashWriteState = BOOTLOADER_FLASH_WRITE_STANDBY_STATE;
return BOOTLOADER_FLASH_WRITE_FINISHED_RES;
break;
}
case BOOTLOADER_FLASH_WRITE_WAIT_FOR_BYTE_DONE:
{
break;
}
case BOOTLOADER_FLASH_WRITE_CHECKBACK_STATE:
{
break;
}
case BOOTLOADER_FLASH_WRITE_FINISHED_STATE:
{
return BOOTLOADER_FLASH_WRITE_FINISHED_RES;
break;
}
case BOOTLOADER_FLASH_WRITE_ERROR_STATE:
{
return BOOTLOADER_FLASH_WRITE_ERROR_RES;
break;
}
}
}
int ResetBootloaderFlashWriteStateMachine()
{
BootloaderFlashWriteState = BOOTLOADER_FLASH_WRITE_BUFFER_STATE;
BootloaderCurFlashWriteAddress = FLASH_BTLDR_FIRMWARE_START_ADDRESS;
BootloaderFlashWritePollCount = 0;
BootloaderFirmwareChunkWriteCount = 0;
BootloaderFlashWriteDataPtr = 0;
BootloaderFirmwareChunkWriteCount = 0;
}

View File

@ -29,6 +29,67 @@ enum eBootloaderStateMachineEvents
BOOTLOADER_MAX_EVENT BOOTLOADER_MAX_EVENT
}; };
enum eBootloaderFlashEraseStates
{
BOOTLOADER_FLASH_ERASE_SECTOR_STATE,
BOOTLOADER_FLASH_ERASE_WAIT_FOR_SECTOR_DONE,
BOOTLOADER_FLASH_ERASE_CHECKBACK_STATE,
BOOTLOADER_FLASH_ERASE_FINISHED_STATE,
BOOTLOADER_FLASH_ERASE_ERROR_STATE,
BOOTLOADER_FLASH_ERASE_MAX_STATE
};
enum eBootloaderFlashEraseResults
{
BOOTLOADER_FLASH_ERASE_RUNNING_RES,
BOOTLOADER_FLASH_ERASE_FINISHED_RES,
BOOTLOADER_FLASH_ERASE_ERROR_RES,
BOOTLOADER_FLASH_ERASE_ABORT_RES,
BOOTLOADER_FLASH_ERASE_MAX_RES
};
enum eBootloaderFlahsEraseSMEvents
{
BOOTLOADER_FLASH_ERASE_SM_TICK_EVENT,
BOOTLOADER_FLASH_ERASE_SM_ABORT_EVENT,
BOOTLOADER_FLASH_ERASE_SM_MAX_EVENT
};
enum eBootloaderFlashWriteStates
{
BOOTLOADER_FLASH_WRITE_STANDBY_STATE,
BOOTLOADER_FLASH_WRITE_BUFFER_STATE,
BOOTLOADER_FLASH_WRITE_WAIT_FOR_BYTE_DONE,
BOOTLOADER_FLASH_WRITE_CHECKBACK_STATE,
BOOTLOADER_FLASH_WRITE_FINISHED_STATE,
BOOTLOADER_FLASH_WRITE_ERROR_STATE,
BOOTLOADER_FLASH_WRITE_MAX_STATE
};
enum eBootloaderFlashWriteResults
{
BOOTLOADER_FLASH_WRITING_RES,
BOOTLOADER_FLASH_WRITE_FINISHED_RES,
BOOTLOADER_FLASH_WRITE_ERROR_RES,
BOOTLOADER_FLASH_WRITE_ABORT_RES,
BOOTLOADER_FLASH_WRITE_MAX_RES
};
enum eBootloaderFlahsWriteSMEvents
{
BOOTLOADER_FLASH_WRITE_SM_TICK_EVENT,
BOOTLOADER_FLASH_WRITE_SM_NEW_BUFFER_EVENT,
BOOTLOADER_FLASH_WRITE_SM_ABORT_EVENT,
BOOTLOADER_FLASH_WRITE_SM_MAX_EVENT
};
enum eBootloaderStateMachineCmds enum eBootloaderStateMachineCmds
{ {
BOOTLOADER_SM_ACTIVATE_CMD, BOOTLOADER_SM_ACTIVATE_CMD,
@ -42,7 +103,7 @@ enum eBootloaderStateMachineCmds
}; };
extern char BootloaderBuffer[200]; extern char BootloaderBuffer[300];
int BootloaderInterfaceInit(); int BootloaderInterfaceInit();
void BootloaderExecuteCmd(char Cmd); void BootloaderExecuteCmd(char Cmd);
@ -56,6 +117,11 @@ void BootloaderResetStateMachine();
void BootloaderActivateBootloader(); void BootloaderActivateBootloader();
void BootloaderDeactivateBootloader(); void BootloaderDeactivateBootloader();
int BootloaderFlashEraseStateMachine(int event);
int ResetBootloaderFlashEraseStateMachine();
int BootloaderFlashWriteStateMachine(int event);
int ResetBootloaderFlashWriteStateMachine();

View File

@ -300,13 +300,14 @@ void BootloaderProtocolSendNACK(unsigned char Cmd)
BootloaderProtocolSendFrame(Cmd,1); BootloaderProtocolSendFrame(Cmd,1);
} }
void BootloaderProtocolSendInitUploadResponse() void BootloaderProtocolSendInitUploadResponse(char result)
{ {
int MaxSize = MAX_BOOTLOADER_PAYLOAD_SIZE; int MaxSize = MAX_BOOTLOADER_PAYLOAD_SIZE;
char* DataPtr = BootloaderProtocolGetDataBufferPtr(); char* DataPtr = BootloaderProtocolGetDataBufferPtr();
*DataPtr++ = (char)BOOTLOADER_ACK; *DataPtr++ = result;
if(result == 1)
{
char nibble = (char)((MaxSize >> 24) &0x000000FF); char nibble = (char)((MaxSize >> 24) &0x000000FF);
*DataPtr++ = nibble; *DataPtr++ = nibble;
@ -318,6 +319,14 @@ void BootloaderProtocolSendInitUploadResponse()
nibble = (char)(MaxSize &0x000000FF); nibble = (char)(MaxSize &0x000000FF);
*DataPtr++ = nibble; *DataPtr++ = nibble;
}
else
{
*DataPtr++ = 0;
*DataPtr++ = 0;
*DataPtr++ = 0;
*DataPtr++ = 0;
}
BootloaderProtocolSendFrame(BOOTLOADER_INIT_UPLOAD_RESPONSE,5); BootloaderProtocolSendFrame(BOOTLOADER_INIT_UPLOAD_RESPONSE,5);
} }

View File

@ -51,10 +51,20 @@ enum eBootloaderProtocolDataTransferError
BOOTLOADER_CHUNK_TRANSFER_ERROR_RESEND = 2, BOOTLOADER_CHUNK_TRANSFER_ERROR_RESEND = 2,
BOOTLOADER_CHUNK_TRANSFER_ERROR_FLASH_FAILURE = 3, BOOTLOADER_CHUNK_TRANSFER_ERROR_FLASH_FAILURE = 3,
BOOTLOADER_CHUNK_TRANSFER_ERROR_INVALID_CHUNK_INDEX = 4, BOOTLOADER_CHUNK_TRANSFER_ERROR_INVALID_CHUNK_INDEX = 4,
BOOTLOADER_CHUNK_TRANSFER_ERROR_FLASH_ERROR = 5,
BOOTLOADER_CHUNK_TRANSFER_MAX_ERROR BOOTLOADER_CHUNK_TRANSFER_MAX_ERROR
}; };
enum eBootloaderProtocolInitTransferError
{
BOOTLOADEDR_INIT_TRANSFER_ERROR = 0,
BOOTLOADEDR_INIT_TRANSFER_OK = 1,
BOOTLOADEDR_INIT_TRANSFER_ERROR_FLASH_NOT_ERASED,
BOOTLOADEDR_INIT_TRANSFER_MAX_ERROR
};
//enum DEVICES_IDS //enum DEVICES_IDS
//{ //{
// ID_MASTER, //Master Controller // ID_MASTER, //Master Controller
@ -86,7 +96,7 @@ unsigned char *BootloaderProtocolGetDataBufferPtr();
void BootloaderProtocolSendHeartbeat(); void BootloaderProtocolSendHeartbeat();
void BootloaderProtocolSendACK(unsigned char Cmd); void BootloaderProtocolSendACK(unsigned char Cmd);
void BootloaderProtocolSendNACK(unsigned char Cmd); void BootloaderProtocolSendNACK(unsigned char Cmd);
void BootloaderProtocolSendInitUploadResponse(); void BootloaderProtocolSendInitUploadResponse(char result);
void BootloaderProtocolSendDataChunkResult(char ErrorCode, int ChunkValue); void BootloaderProtocolSendDataChunkResult(char ErrorCode, int ChunkValue);

View File

@ -19,7 +19,7 @@
#define FLASH_END_ADDRESS 180000 #define FLASH_END_ADDRESS 180000
#define FLASH_BTLDR_FIRMWARE_START_ADDRESS 0x180000 #define FLASH_BTLDR_FIRMWARE_START_ADDRESS 0x180000
#define FLASH_BTLDR_FIRMWARE_LAST_SECTOR_ADD 0x180000 #define FLASH_BTLDR_FIRMWARE_LAST_64K_SECTOR_ADD 0x1F0000

View File

@ -104,22 +104,22 @@ int SPIFlashCheckChipID()
int SPIFlashReadBuffer(unsigned char *Buf, int Size, int StartAddress) int SPIFlashReadBuffer(unsigned char *Buf, int Size, int StartAddress)
{ {
if(StartAddress + Size > SPI_FLASH_MAX_ADDRESS) if(StartAddress + Size - 1 > SPI_FLASH_MAX_ADDRESS)
{ {
return RET_ERROR; return RET_ERROR;
} }
FLASH_SS_PIN = 0; FLASH_SS_PIN = 0;
SPITransaction(SPI_FLASH_HI_SPEED_READ,mSPIFlashBaudrate); SPITransaction(SPI_FLASH_HI_SPEED_READ,mSPIFlashBaudrate);
SPITransaction(((StartAddress & 0xFF0000) >> 16),mSPIFlashBaudrate); SPITransaction(((StartAddress & 0xFF0000) >> 16),mSPIFlashHighSpeedBaudrate);
SPITransaction(((StartAddress & 0x00FF00) >> 8),mSPIFlashBaudrate); SPITransaction(((StartAddress & 0x00FF00) >> 8),mSPIFlashHighSpeedBaudrate);
SPITransaction((StartAddress & 0x0000FF),mSPIFlashBaudrate); SPITransaction((StartAddress & 0x0000FF),mSPIFlashHighSpeedBaudrate);
SPITransaction((0x00),mSPIFlashBaudrate); //Chip requires a dummy read in high speed SPITransaction((0x00),mSPIFlashHighSpeedBaudrate); //Chip requires a dummy read in high speed
int i; int i;
for(i = 0; i < Size; i++) for(i = 0; i < Size; i++)
{ {
*Buf++ = SPITransaction(0xDE,mSPIFlashBaudrate); *Buf++ = SPITransaction(0xDE,mSPIFlashHighSpeedBaudrate);
} }
FLASH_SS_PIN = 1; FLASH_SS_PIN = 1;
@ -138,18 +138,50 @@ int SPIFlashEraseSector(int SectorAddress)
FLASH_SS_PIN = 0; FLASH_SS_PIN = 0;
SPITransaction(SPI_FLASH_4KB_SECOTR_ERASE,mSPIFlashHighSpeedBaudrate); SPITransaction(SPI_FLASH_4KB_SECOTR_ERASE,mSPIFlashHighSpeedBaudrate);
SPITransaction(((SectorAddress & 0xFF0000) >> 16),mSPIFlashBaudrate); SPITransaction(((SectorAddress & 0xFF0000) >> 16),mSPIFlashHighSpeedBaudrate);
SPITransaction(((SectorAddress & 0x00FF00) >> 8),mSPIFlashBaudrate); SPITransaction(((SectorAddress & 0x00FF00) >> 8),mSPIFlashHighSpeedBaudrate);
SPITransaction((SectorAddress & 0x0000FF),mSPIFlashBaudrate); SPITransaction((SectorAddress & 0x0000FF),mSPIFlashHighSpeedBaudrate);
FLASH_SS_PIN = 1; FLASH_SS_PIN = 1;
SectorAddress++; SectorAddress++;
while( SPIFlashCheckBusy() == true); SPIFlashWriteEnable(); while( SPIFlashCheckBusy() == true);
//SPIFlashWriteEnable();
return RET_OK; return RET_OK;
} }
int SPIFlashErase64KSector(int SectorAddress, int Blocking)
{
if(SectorAddress % SPI_FLASH_64K_SECTOR_SIZE != 0) //Sectors are aligned on 0x1000
{
return RET_ERROR;
}
if((SectorAddress + SPI_FLASH_64K_SECTOR_SIZE - 1) > SPI_FLASH_MAX_ADDRESS)
{
return RET_ERROR;
}
SPIFlashWriteEnable();
FLASH_SS_PIN = 0;
SPITransaction(SPI_FLASH_64KB_BLOCK_ERASE,mSPIFlashHighSpeedBaudrate);
SPITransaction(((SectorAddress & 0xFF0000) >> 16),mSPIFlashHighSpeedBaudrate);
SPITransaction(((SectorAddress & 0x00FF00) >> 8),mSPIFlashHighSpeedBaudrate);
SPITransaction((SectorAddress & 0x0000FF),mSPIFlashHighSpeedBaudrate);
FLASH_SS_PIN = 1;
if(Blocking != 0)
{
while( SPIFlashCheckBusy() == true);
// SPIFlashWriteEnable();
}
return RET_OK;
}
int SPIFlashWriteSectorWorkingBuffer(int SectorAddress, int Erase) int SPIFlashWriteSectorWorkingBuffer(int SectorAddress, int Erase)
{ {
if(SectorAddress % SPI_FLASH_SECTOR_SIZE != 0) //Sectors are aligned on 0x1000 if(SectorAddress % SPI_FLASH_SECTOR_SIZE != 0) //Sectors are aligned on 0x1000
@ -170,10 +202,10 @@ int SPIFlashWriteSectorWorkingBuffer(int SectorAddress, int Erase)
FLASH_SS_PIN = 0; FLASH_SS_PIN = 0;
SPITransaction(SPI_FLASH_BYTE_PROGRAM,mSPIFlashHighSpeedBaudrate); SPITransaction(SPI_FLASH_BYTE_PROGRAM,mSPIFlashHighSpeedBaudrate);
SPITransaction(((SectorAddress & 0xFF0000) >> 16),mSPIFlashBaudrate); SPITransaction(((SectorAddress & 0xFF0000) >> 16),mSPIFlashHighSpeedBaudrate);
SPITransaction(((SectorAddress & 0x00FF00) >> 8),mSPIFlashBaudrate); SPITransaction(((SectorAddress & 0x00FF00) >> 8),mSPIFlashHighSpeedBaudrate);
SPITransaction((SectorAddress & 0x0000FF),mSPIFlashBaudrate); SPITransaction((SectorAddress & 0x0000FF),mSPIFlashHighSpeedBaudrate);
SPITransaction(*DataPtr++,mSPIFlashBaudrate); SPITransaction(*DataPtr++,mSPIFlashHighSpeedBaudrate);
FLASH_SS_PIN = 1; FLASH_SS_PIN = 1;
SectorAddress++; SectorAddress++;
@ -181,8 +213,34 @@ int SPIFlashWriteSectorWorkingBuffer(int SectorAddress, int Erase)
while( SPIFlashCheckBusy() == true); while( SPIFlashCheckBusy() == true);
} }
return RET_OK;
}
int SPIFlashWriteByte(unsigned int ByteAddress, char byte, int blocking)
{
if(ByteAddress > SPI_FLASH_MAX_ADDRESS)
{
return RET_ERROR;
}
SPIFlashWriteEnable();
FLASH_SS_PIN = 0;
SPITransaction(SPI_FLASH_BYTE_PROGRAM,mSPIFlashHighSpeedBaudrate);
SPITransaction(((ByteAddress & 0xFF0000) >> 16),mSPIFlashHighSpeedBaudrate);
SPITransaction(((ByteAddress & 0x00FF00) >> 8),mSPIFlashHighSpeedBaudrate);
SPITransaction((ByteAddress & 0x0000FF),mSPIFlashHighSpeedBaudrate);
SPITransaction(byte,mSPIFlashHighSpeedBaudrate);
FLASH_SS_PIN = 1;
if(blocking)
{
while( SPIFlashCheckBusy() == true);
}
return RET_OK; return RET_OK;
} }

View File

@ -26,6 +26,7 @@
#define SPI_FLASH_CHIP_ID 0x41 #define SPI_FLASH_CHIP_ID 0x41
#define SPI_FLASH_MAX_ADDRESS 0x1FFFFF #define SPI_FLASH_MAX_ADDRESS 0x1FFFFF
#define SPI_FLASH_SECTOR_SIZE 0x1000 #define SPI_FLASH_SECTOR_SIZE 0x1000
#define SPI_FLASH_64K_SECTOR_SIZE 0x10000
#define SPI_NB_SECTORS 0x1FF //511 sectors = SPI_FLASH_MAX_ADDRESS / SPI_FLASH_SECTOR_SIZE #define SPI_NB_SECTORS 0x1FF //511 sectors = SPI_FLASH_MAX_ADDRESS / SPI_FLASH_SECTOR_SIZE
@ -37,7 +38,10 @@ int SPIFlashReadBuffer(unsigned char *Buf, int Size, int StartAddress);
int SPIFlashCheckBusy(); int SPIFlashCheckBusy();
int SPIFlashWriteEnable(); int SPIFlashWriteEnable();
int SPIFlashEraseSector(int SectorAddress); int SPIFlashEraseSector(int SectorAddress);
int SPIFlashErase64KSector(int SectorAddress, int Blocking);
int SPIFlashWriteSectorWorkingBuffer(int SectorAddress, int Erase); int SPIFlashWriteSectorWorkingBuffer(int SectorAddress, int Erase);
int SPIFlashWriteBuffer(unsigned char *Buf, int Size, int StartAddress);
int SPIFlashWriteByte(unsigned int ByteAddress, char byte, int blocking);
#endif /* SPI_FLASH_H */ #endif /* SPI_FLASH_H */

View File

@ -48,6 +48,7 @@ typedef enum
WIFI_TICK_TIMER, WIFI_TICK_TIMER,
SYSLOG_TX_TIMER, SYSLOG_TX_TIMER,
TEMP_SENSOR_REFRESH_TIMER, TEMP_SENSOR_REFRESH_TIMER,
BOOTLOADER_FLASH_POLL_TIMER,
TIMER_MAX_ID TIMER_MAX_ID
}eTimerID; }eTimerID;

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,8 @@
<project-private xmlns="http://www.netbeans.org/ns/project-private/1"> <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/> <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/> <group>
<file>file:/D:/Main/PicDev/Projets/ChaletLora/ChaletLora.X/Source/BootloaderProtocol.c</file>
</group>
</open-files> </open-files>
</project-private> </project-private>