X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/5d5e3590762745af400e8ba736d1c5692a5de521..c58c53dba90fdc19d38f5e5d6957f2ede2a740f3:/Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.c diff --git a/Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.c b/Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.c index c2714f2ac..07dce5011 100644 --- a/Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.c +++ b/Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.c @@ -119,14 +119,14 @@ static void XPROGProtocol_EnterXPROGMode(void) { /* Enable PDI programming mode with the attached target */ XPROGTarget_EnableTargetPDI(); - + /* Store the RESET key into the RESET PDI register to keep the XMEGA in reset */ XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG); XPROGTarget_SendByte(PDI_RESET_KEY); - /* Lower direction change guard time to 8 USART bits */ + /* Lower direction change guard time to 0 USART bits */ XPROGTarget_SendByte(PDI_CMD_STCS | PDI_CTRL_REG); - XPROGTarget_SendByte(0x04); + XPROGTarget_SendByte(0x07); /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */ XPROGTarget_SendByte(PDI_CMD_KEY); @@ -141,9 +141,9 @@ static void XPROGProtocol_EnterXPROGMode(void) /* Enable TPI programming mode with the attached target */ XPROGTarget_EnableTargetTPI(); - /* Lower direction change guard time to 8 USART bits */ + /* Lower direction change guard time to 0 USART bits */ XPROGTarget_SendByte(TPI_CMD_SSTCS | TPI_CTRL_REG); - XPROGTarget_SendByte(0x04); + XPROGTarget_SendByte(0x07); /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */ XPROGTarget_SendByte(TPI_CMD_SKEY); @@ -170,6 +170,8 @@ static void XPROGProtocol_LeaveXPROGMode(void) if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI) { + XMEGANVM_WaitWhileNVMBusBusy(); + /* Clear the RESET key in the RESET PDI register to allow the XMEGA to run */ XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG); XPROGTarget_SendByte(0x00); @@ -178,6 +180,8 @@ static void XPROGProtocol_LeaveXPROGMode(void) } else { + TINYNVM_WaitWhileNVMBusBusy(); + /* Clear the NVMEN bit in the TPI CONTROL register to disable TPI mode */ XPROGTarget_SendByte(TPI_CMD_SSTCS | TPI_CTRL_REG); XPROGTarget_SendByte(0x00); @@ -208,27 +212,41 @@ static void XPROGProtocol_Erase(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - uint8_t EraseCommand = XMEGA_NVM_CMD_NOOP; - + uint8_t EraseCommand; + if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI) - { + { /* Determine which NVM command to send to the device depending on the memory to erase */ - if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_CHIP) - EraseCommand = XMEGA_NVM_CMD_CHIPERASE; - else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_APP) - EraseCommand = XMEGA_NVM_CMD_ERASEAPPSEC; - else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_BOOT) - EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSEC; - else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_EEPROM) - EraseCommand = XMEGA_NVM_CMD_ERASEEEPROM; - else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_APP_PAGE) - EraseCommand = XMEGA_NVM_CMD_ERASEAPPSECPAGE; - else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_BOOT_PAGE) - EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSECPAGE; - else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_EEPROM_PAGE) - EraseCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGE; - else if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_USERSIG) - EraseCommand = XMEGA_NVM_CMD_ERASEUSERSIG; + switch (Erase_XPROG_Params.MemoryType) + { + case XPRG_ERASE_CHIP: + EraseCommand = XMEGA_NVM_CMD_CHIPERASE; + break; + case XPRG_ERASE_APP: + EraseCommand = XMEGA_NVM_CMD_ERASEAPPSEC; + break; + case XPRG_ERASE_BOOT: + EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSEC; + break; + case XPRG_ERASE_EEPROM: + EraseCommand = XMEGA_NVM_CMD_ERASEEEPROM; + break; + case XPRG_ERASE_APP_PAGE: + EraseCommand = XMEGA_NVM_CMD_ERASEAPPSECPAGE; + break; + case XPRG_ERASE_BOOT_PAGE: + EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSECPAGE; + break; + case XPRG_ERASE_EEPROM_PAGE: + EraseCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGE; + break; + case XPRG_ERASE_USERSIG: + EraseCommand = XMEGA_NVM_CMD_ERASEUSERSIG; + break; + default: + EraseCommand = XMEGA_NVM_CMD_NOOP; + break; + } /* Erase the target memory, indicate timeout if ocurred */ if (!(XMEGANVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address))) @@ -236,8 +254,13 @@ static void XPROGProtocol_Erase(void) } else { + if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_CHIP) + EraseCommand = TINY_NVM_CMD_CHIPERASE; + else + EraseCommand = TINY_NVM_CMD_SECTIONERASE; + /* Erase the target memory, indicate timeout if ocurred */ - if (!(TINYNVM_EraseMemory())) + if (!(TINYNVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address))) ReturnStatus = XPRG_ERR_TIMEOUT; } @@ -270,43 +293,40 @@ static void XPROGProtocol_WriteMemory(void) Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - /* Assume FLASH page programming by default, as it is the common case */ - uint8_t WriteCommand = XMEGA_NVM_CMD_WRITEFLASHPAGE; - uint8_t WriteBuffCommand = XMEGA_NVM_CMD_LOADFLASHPAGEBUFF; - uint8_t EraseBuffCommand = XMEGA_NVM_CMD_ERASEFLASHPAGEBUFF; - bool PagedMemory = true; - if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI) { - if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_APPL) - { - WriteCommand = XMEGA_NVM_CMD_WRITEAPPSECPAGE; - } - else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_BOOT) - { - WriteCommand = XMEGA_NVM_CMD_WRITEBOOTSECPAGE; - } - else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_EEPROM) - { - WriteCommand = XMEGA_NVM_CMD_WRITEEEPROMPAGE; - WriteBuffCommand = XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF; - EraseBuffCommand = XMEGA_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 = XMEGA_NVM_CMD_WRITEUSERSIG; - } - else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_FUSE) - { - WriteCommand = XMEGA_NVM_CMD_WRITEFUSE; - PagedMemory = false; - } - else if (WriteMemory_XPROG_Params.MemoryType == XPRG_MEM_TYPE_LOCKBITS) + /* Assume FLASH page programming by default, as it is the common case */ + uint8_t WriteCommand = XMEGA_NVM_CMD_WRITEFLASHPAGE; + uint8_t WriteBuffCommand = XMEGA_NVM_CMD_LOADFLASHPAGEBUFF; + uint8_t EraseBuffCommand = XMEGA_NVM_CMD_ERASEFLASHPAGEBUFF; + bool PagedMemory = true; + + switch (WriteMemory_XPROG_Params.MemoryType) { - WriteCommand = XMEGA_NVM_CMD_WRITELOCK; - PagedMemory = false; + case XPRG_MEM_TYPE_APPL: + WriteCommand = XMEGA_NVM_CMD_WRITEAPPSECPAGE; + break; + case XPRG_MEM_TYPE_BOOT: + WriteCommand = XMEGA_NVM_CMD_WRITEBOOTSECPAGE; + break; + case XPRG_MEM_TYPE_EEPROM: + WriteCommand = XMEGA_NVM_CMD_WRITEEEPROMPAGE; + WriteBuffCommand = XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF; + EraseBuffCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF; + break; + case 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 = XMEGA_NVM_CMD_WRITEUSERSIG; + break; + case XPRG_MEM_TYPE_FUSE: + WriteCommand = XMEGA_NVM_CMD_WRITEFUSE; + PagedMemory = false; + break; + case XPRG_MEM_TYPE_LOCKBITS: + WriteCommand = XMEGA_NVM_CMD_WRITELOCK; + PagedMemory = false; + break; } /* Send the appropriate memory write commands to the device, indicate timeout if occurred */ @@ -321,8 +341,6 @@ static void XPROGProtocol_WriteMemory(void) } else { - Serial_TxByte((uint8_t)WriteMemory_XPROG_Params.Length); - /* Send write command to the TPI device, indicate timeout if occurred */ if (!(TINYNVM_WriteMemory(WriteMemory_XPROG_Params.Address, WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length))) @@ -368,8 +386,6 @@ static void XPROGProtocol_ReadMemory(void) } else { - Serial_TxByte((uint8_t)ReadMemory_XPROG_Params.Length); - /* Read the TPI target's memory, indicate timeout if occurred */ if (!(TINYNVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length))) ReturnStatus = XPRG_ERR_TIMEOUT; @@ -398,21 +414,29 @@ static void XPROGProtocol_ReadCRC(void) } ReadCRC_XPROG_Params; Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params, sizeof(ReadCRC_XPROG_Params), NO_STREAM_CALLBACK); + Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - uint8_t CRCCommand = XMEGA_NVM_CMD_NOOP; uint32_t MemoryCRC; if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI) { + 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 = XMEGA_NVM_CMD_APPCRC; - else if (ReadCRC_XPROG_Params.CRCType == XPRG_CRC_BOOT) - CRCCommand = XMEGA_NVM_CMD_BOOTCRC; - else - CRCCommand = XMEGA_NVM_CMD_FLASHCRC; + switch (ReadCRC_XPROG_Params.CRCType) + { + case XPRG_CRC_APP: + CRCCommand = XMEGA_NVM_CMD_APPCRC; + break; + case XPRG_CRC_BOOT: + CRCCommand = XMEGA_NVM_CMD_BOOTCRC; + break; + default: + CRCCommand = XMEGA_NVM_CMD_FLASHCRC; + break; + } /* Perform and retrieve the memory CRC, indicate timeout if occurred */ if (!(XMEGANVM_GetMemoryCRC(CRCCommand, &MemoryCRC)))