X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/1f8dfd0205d431351425f468627984280b21cd5a..66482341573c035e4c90cee32b89fb7f59068e40:/Projects/AVRISP/Lib/PDIProtocol.c diff --git a/Projects/AVRISP/Lib/PDIProtocol.c b/Projects/AVRISP/Lib/PDIProtocol.c index b095a49c5..ecd2fe9c3 100644 --- a/Projects/AVRISP/Lib/PDIProtocol.c +++ b/Projects/AVRISP/Lib/PDIProtocol.c @@ -28,14 +28,16 @@ this software. */ -#if defined(ENABLE_XPROG_PROTOCOL) +#if defined(ENABLE_PDI_PROTOCOL) || defined(__DOXYGEN__) + +#warning PDI Programming Protocol support is incomplete and not currently suitable for use. /** \file * * PDI Protocol handler, to process V2 Protocol wrapped PDI commands used in Atmel programmer devices. */ -#define INCLUDE_FROM_XPROG_C +#define INCLUDE_FROM_PDIPROTOCOL_C #include "PDIProtocol.h" uint32_t XPROG_Param_NVMBase; @@ -61,6 +63,9 @@ void PDIProtocol_XPROG_SetMode(void) Endpoint_ClearIN(); } +/** Handler for the CMD_XPROG command, which wraps up XPROG commands in a V2 wrapper which need to be + * removed and processed so that the underlying XPROG command can be handled. + */ void PDIProtocol_XPROG_Command(void) { uint8_t XPROGCommand = Endpoint_Read_Byte(); @@ -74,7 +79,7 @@ void PDIProtocol_XPROG_Command(void) PDIProtocol_LeaveXPROGMode(); break; case XPRG_CMD_ERASE: - PDIProtocol_EraseChip(); + PDIProtocol_Erase(); break; case XPRG_CMD_WRITE_MEM: PDIProtocol_WriteMemory(); @@ -91,7 +96,55 @@ void PDIProtocol_XPROG_Command(void) } } -static void PDIProtocol_EraseChip(void) +/** Handler for the XPROG ENTER_PROGMODE command to establish a PDI connection with the attached device. */ +static void PDIProtocol_EnterXPROGMode(void) +{ + Endpoint_ClearOUT(); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + /* Enable PDI programming mode with the attached target */ + PDITarget_EnableTargetPDI(); + + /* Store the RESET key into the RESET PDI register to keep the XMEGA in reset */ + PDITarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG); + PDITarget_SendByte(PDI_RESET_KEY); + + /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */ + PDITarget_SendByte(PDI_CMD_KEY); + for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--) + PDITarget_SendByte(PDI_NVMENABLE_KEY[i - 1]); + + /* Wait until the NVM bus becomes active */ + bool NVMBusEnabled = PDITarget_WaitWhileNVMBusBusy(); + + Endpoint_Write_Byte(CMD_XPROG); + Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE); + Endpoint_Write_Byte(NVMBusEnabled ? XPRG_ERR_OK : XPRG_ERR_FAILED); + Endpoint_ClearIN(); +} + +/** Handler for the XPROG LEAVE_PROGMODE command to terminate the PDI programming connection with + * the attached device. + */ +static void PDIProtocol_LeaveXPROGMode(void) +{ + Endpoint_ClearOUT(); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + /* Clear the RESET key into the RESET PDI register to allow the XMEGA to run */ + PDITarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG); + PDITarget_SendByte(0x00); + + PDITarget_DisableTargetPDI(); + + Endpoint_Write_Byte(CMD_XPROG); + Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE); + Endpoint_Write_Byte(XPRG_ERR_OK); + Endpoint_ClearIN(); +} + +/** Handler for the XPRG ERASE command to erase a specific memory address space in the attached device. */ +static void PDIProtocol_Erase(void) { uint8_t ReturnStatus = XPRG_ERR_OK; @@ -114,6 +167,7 @@ static void PDIProtocol_EraseChip(void) Endpoint_ClearIN(); } +/** Handler for the XPROG WRITE_MEMORY command to write to a specific memory space within the attached device. */ static void PDIProtocol_WriteMemory(void) { uint8_t ReturnStatus = XPRG_ERR_OK; @@ -143,6 +197,9 @@ static void PDIProtocol_WriteMemory(void) Endpoint_ClearIN(); } +/** Handler for the XPROG READ_MEMORY command to read data from a specific address space within the + * attached device. + */ static void PDIProtocol_ReadMemory(void) { uint8_t ReturnStatus = XPRG_ERR_OK; @@ -161,40 +218,68 @@ static void PDIProtocol_ReadMemory(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - // TODO: Send read command here via PDI protocol + if (ReadMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_USERSIG) + { + PDITarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_1BYTE << 2)); + PDITarget_SendAddress(DATAMEM_BASE | DATAMEM_NVM_CMD); + PDITarget_SendByte(NVM_CMD_READUSERSIG); + + + } Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_READ_MEM); Endpoint_Write_Byte(ReturnStatus); - - // START TEMP - uint8_t ProgData[256]; - for (uint16_t i = 0; i < ReadMemory_XPROG_Params.Length; i++) - ProgData[i] = i; - Endpoint_Write_Stream_LE(ProgData, ReadMemory_XPROG_Params.Length); - - if (!Endpoint_IsReadWriteAllowed()) - { - Endpoint_ClearIN(); - while(!(Endpoint_IsReadWriteAllowed())); - } - // END TEMP Endpoint_ClearIN(); } +/** Handler for the XPROG CRC command to read a specific memory space's CRC value for comparison between the + * attached device's memory and a data set on the host. + */ static void PDIProtocol_ReadCRC(void) { uint8_t ReturnStatus = XPRG_ERR_OK; - - uint8_t CRCType = Endpoint_Read_Byte(); + struct + { + uint8_t CRCType; + } ReadCRC_XPROG_Params; + + Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params, sizeof(ReadCRC_XPROG_Params)); Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); uint32_t MemoryCRC = 0; + uint8_t CRCReadCommand; + + if (ReadCRC_XPROG_Params.CRCType == XPRG_CRC_APP) + CRCReadCommand = NVM_CMD_APPCRC; + else if (ReadCRC_XPROG_Params.CRCType == XPRG_CRC_BOOT) + CRCReadCommand = NVM_CMD_BOOTCRC; + else + CRCReadCommand = NVM_CMD_FLASHCRC; + + /* Set the NVM command to the correct CRC read command */ + PDITarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_1BYTE << 2)); + PDITarget_SendAddress(DATAMEM_BASE | DATAMEM_NVM_CMD); + PDITarget_SendByte(CRCReadCommand); + + /* Set CMDEX bit in NVM CTRLA register to start the CRC generation */ + PDITarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_1BYTE << 2)); + PDITarget_SendAddress(DATAMEM_BASE | DATAMEM_NVM_CTRLA); + PDITarget_SendByte(1 << 0); + + /* Wait until the NVM bus and controller is no longer busy */ + PDITarget_WaitWhileNVMBusBusy(); + PDITarget_WaitWhileNVMControllerBusy(); - // TODO: Read device CRC for desired memory via PDI protocol + /* Read the three byte generated CRC value */ + PDITarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_3BYTES << 2)); + PDITarget_SendAddress(DATAMEM_BASE | DATAMEM_NVM_DAT0); + MemoryCRC = PDITarget_ReceiveByte(); + MemoryCRC |= ((uint16_t)PDITarget_ReceiveByte() << 8); + MemoryCRC |= ((uint32_t)PDITarget_ReceiveByte() << 16); Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_CRC); @@ -209,34 +294,9 @@ static void PDIProtocol_ReadCRC(void) Endpoint_ClearIN(); } -static void PDIProtocol_EnterXPROGMode(void) -{ - uint8_t ReturnStatus = XPRG_ERR_OK; - - Endpoint_ClearOUT(); - Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - - // TODO: Enter device programming mode here via PDI protocol - - Endpoint_Write_Byte(CMD_XPROG); - Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE); - Endpoint_Write_Byte(ReturnStatus); - Endpoint_ClearIN(); -} - -static void PDIProtocol_LeaveXPROGMode(void) -{ - Endpoint_ClearOUT(); - Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - - // TODO: Exit device programming mode here via PDI protocol - - Endpoint_Write_Byte(CMD_XPROG); - Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE); - Endpoint_Write_Byte(XPRG_ERR_OK); - Endpoint_ClearIN(); -} - +/** Handler for the XPROG SET_PARAM command to set a PDI parameter for use when communicating with the + * attached device. + */ static void PDIProtocol_SetParam(void) { uint8_t ReturnStatus = XPRG_ERR_OK;