X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/48e50b6b578fa7a74b4f33f067aa684e3469850e..4f74075fad7f1e7a35d04ff534d9d6a57d2b97fc:/Projects/AVRISP/Lib/PDIProtocol.c diff --git a/Projects/AVRISP/Lib/PDIProtocol.c b/Projects/AVRISP/Lib/PDIProtocol.c index 39f33cf49..1b3fc53c4 100644 --- a/Projects/AVRISP/Lib/PDIProtocol.c +++ b/Projects/AVRISP/Lib/PDIProtocol.c @@ -37,9 +37,10 @@ #include "PDIProtocol.h" #if defined(ENABLE_PDI_PROTOCOL) || defined(__DOXYGEN__) -#warning PDI Programming Protocol support is incomplete and not currently suitable for use. - +/** Base absolute address for the target's NVM controller */ uint32_t XPROG_Param_NVMBase; + +/** Size in bytes of the target's EEPROM page */ uint32_t XPROG_Param_EEPageSize; /** Handler for the CMD_XPROG_SETMODE command, which sets the programmer-to-target protocol used for PDI @@ -114,7 +115,7 @@ static void PDIProtocol_EnterXPROGMode(void) PDITarget_SendByte(PDI_NVMENABLE_KEY[i - 1]); /* Wait until the NVM bus becomes active */ - bool NVMBusEnabled = NVMTarget_WaitWhileNVMBusBusy(); + bool NVMBusEnabled = PDITarget_WaitWhileNVMBusBusy(); Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE); @@ -159,8 +160,9 @@ static void PDIProtocol_Erase(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - uint8_t EraseCommand; + uint8_t EraseCommand = NVM_CMD_NOOP; + /* Determine which NVM command to send to the device depending on the memory to erase */ if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_CHIP) EraseCommand = NVM_CMD_CHIPERASE; else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_APP) @@ -178,7 +180,9 @@ static void PDIProtocol_Erase(void) else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_USERSIG) EraseCommand = NVM_CMD_ERASEUSERSIG; - NVMTarget_EraseMemory(EraseCommand, Erase_XPROG_Params.Address); + /* Erase the target memory, indicate timeout if ocurred */ + if (!(NVMTarget_EraseMemory(EraseCommand, Erase_XPROG_Params.Address))) + ReturnStatus = XPRG_ERR_TIMEOUT; Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_ERASE); @@ -194,6 +198,7 @@ static void PDIProtocol_WriteMemory(void) struct { uint8_t MemoryType; + uint8_t PageMode; uint32_t Address; uint16_t Length; uint8_t ProgData[256]; @@ -207,11 +212,56 @@ static void PDIProtocol_WriteMemory(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + /* Assume FLASH page programming by default, as it is the common case */ + uint8_t WriteCommand = NVM_CMD_WRITEFLASHPAGE; + uint8_t WriteBuffCommand = NVM_CMD_LOADFLASHPAGEBUFF; + uint8_t EraseBuffCommand = NVM_CMD_ERASEFLASHPAGEBUFF; + bool PagedMemory = true; + + if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_APPL) + { + WriteCommand = NVM_CMD_WRITEAPPSECPAGE; + } + else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_BOOT) + { + WriteCommand = NVM_CMD_WRITEBOOTSECPAGE; + } + else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_EEPROM) + { + WriteCommand = NVM_CMD_WRITEEEPROMPAGE; + WriteBuffCommand = NVM_CMD_LOADEEPROMPAGEBUFF; + EraseBuffCommand = NVM_CMD_ERASEEEPROMPAGEBUFF; + } + else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_USERSIG) + { + /* User signature is paged, but needs us to manually indicate the mode bits since the host doesn't set them */ + WriteMemory_XPROG_Params.PageMode = (XPRG_PAGEMODE_ERASE | XPRG_PAGEMODE_WRITE); + WriteCommand = NVM_CMD_WRITEUSERSIG; + } + else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_FUSE) + { + WriteCommand = NVM_CMD_WRITEFUSE; + PagedMemory = false; + } + else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_LOCKBITS) + { + WriteCommand = NVM_CMD_WRITELOCK; + PagedMemory = false; + } - // TODO: Send program command here via PDI protocol + /* Send the appropriate memory write commands to the device, indicate timeout if occurred */ + if ((PagedMemory && !NVMTarget_WritePageMemory(WriteBuffCommand, EraseBuffCommand, WriteCommand, + WriteMemory_XPROG_Params.PageMode, WriteMemory_XPROG_Params.Address, + WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length)) || + (!PagedMemory && !NVMTarget_WriteByteMemory(WriteCommand, WriteMemory_XPROG_Params.Address, + WriteMemory_XPROG_Params.ProgData))) + { + ReturnStatus = XPRG_ERR_TIMEOUT; + } Endpoint_Write_Byte(CMD_XPROG); - Endpoint_Write_Byte(XPRG_CMD_READ_MEM); + Endpoint_Write_Byte(XPRG_CMD_WRITE_MEM); Endpoint_Write_Byte(ReturnStatus); Endpoint_ClearIN(); } @@ -237,8 +287,11 @@ static void PDIProtocol_ReadMemory(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - uint8_t ReadBuffer[ReadMemory_XPROG_Params.Length]; - NVMTarget_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length); + uint8_t ReadBuffer[256]; + + /* Read the target's memory, indicate timeout if occurred */ + if (!(NVMTarget_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length))) + ReturnStatus = XPRG_ERR_TIMEOUT; Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_READ_MEM); @@ -266,9 +319,10 @@ static void PDIProtocol_ReadCRC(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + uint8_t CRCCommand = NVM_CMD_NOOP; uint32_t MemoryCRC; - uint8_t CRCCommand; + /* Determine which NVM command to send to the device depending on the memory to CRC */ if (ReadCRC_XPROG_Params.CRCType == XPRG_CRC_APP) CRCCommand = NVM_CMD_APPCRC; else if (ReadCRC_XPROG_Params.CRCType == XPRG_CRC_BOOT) @@ -276,7 +330,9 @@ static void PDIProtocol_ReadCRC(void) else CRCCommand = NVM_CMD_FLASHCRC; - MemoryCRC = NVMTarget_GetMemoryCRC(CRCCommand); + /* Perform and retrieve the memory CRC, indicate timeout if occurred */ + if (!(NVMTarget_GetMemoryCRC(CRCCommand, &MemoryCRC))) + ReturnStatus = XPRG_ERR_TIMEOUT; Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_CRC); @@ -300,6 +356,7 @@ static void PDIProtocol_SetParam(void) uint8_t XPROGParam = Endpoint_Read_Byte(); + /* Determine which parameter is being set, store the new parameter value */ if (XPROGParam == XPRG_PARAM_NVMBASE) XPROG_Param_NVMBase = Endpoint_Read_DWord_BE(); else if (XPROGParam == XPRG_PARAM_EEPPAGESIZE)