X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/f69f03cb0d02937dee018264f0ac4e9be76fc1f4..32b7762325829c6e7a4168bb7db5084a34673f20:/Projects/AVRISP/Lib/PDIProtocol.c diff --git a/Projects/AVRISP/Lib/PDIProtocol.c b/Projects/AVRISP/Lib/PDIProtocol.c index c91956143..ecd2fe9c3 100644 --- a/Projects/AVRISP/Lib/PDIProtocol.c +++ b/Projects/AVRISP/Lib/PDIProtocol.c @@ -28,7 +28,9 @@ 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 * @@ -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,60 +96,55 @@ void PDIProtocol_XPROG_Command(void) } } +/** Handler for the XPROG ENTER_PROGMODE command to establish a PDI connection with the attached device. */ static void PDIProtocol_EnterXPROGMode(void) { - uint8_t ReturnStatus = XPRG_ERR_OK; - Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - PDIDATA_LINE_DDR |= PDIDATA_LINE_MASK; - PDICLOCK_LINE_DDR |= PDICLOCK_LINE_MASK; + /* Enable PDI programming mode with the attached target */ + PDITarget_EnableTargetPDI(); - PDIDATA_LINE_PORT |= PDIDATA_LINE_MASK; + /* 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); - _delay_us(1); - - for (uint8_t i = 0; i < 16; i++) - { - PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK; - PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK; - } - - static const uint8_t NVMKey[8] = {0x12, 0x89, 0xAB, 0x45, 0xCD, 0xD8, 0x88, 0xFF}; - + /* 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 = 0; i < 8; i++) - PDITarget_SendByte(NVMKey[i]); + for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--) + PDITarget_SendByte(PDI_NVMENABLE_KEY[i - 1]); - PDITarget_SendByte(PDI_CMD_LDCS | PD_STATUS_REG); - if (!(PDITarget_ReceiveByte() & PDI_STATUS_NVM)) - ReturnStatus = XPRG_ERR_FAILED; + /* 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(ReturnStatus); + 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); - PDIDATA_LINE_DDR &= ~PDIDATA_LINE_MASK; - PDICLOCK_LINE_DDR &= ~PDICLOCK_LINE_MASK; + /* 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(); - PDIDATA_LINE_PORT &= ~PDIDATA_LINE_MASK; - PDICLOCK_LINE_PORT &= ~PDICLOCK_LINE_MASK; - Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE); Endpoint_Write_Byte(XPRG_ERR_OK); Endpoint_ClearIN(); } -static void PDIProtocol_EraseChip(void) +/** 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; @@ -167,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; @@ -196,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; @@ -214,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; - // TODO: Read device CRC for desired memory via PDI protocol + /* 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(); + + /* 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); @@ -262,6 +294,9 @@ static void PDIProtocol_ReadCRC(void) 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;