The AVRISP project can now enter PDI mode under both bit-bang and hardware USART...
[pub/USBasp.git] / Projects / AVRISP / Lib / PDIProtocol.c
index 2107e2b..7769343 100644 (file)
@@ -106,7 +106,7 @@ static void PDIProtocol_EnterXPROGMode(void)
        PDITarget_EnableTargetPDI();\r
        \r
        /* Store the RESET key into the RESET PDI register to keep the XMEGA in reset */\r
-       PDITarget_SendByte(PDI_CMD_STCS | PD_RESET_REG);        \r
+       PDITarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);       \r
        PDITarget_SendByte(PDI_RESET_KEY);\r
 \r
        /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
@@ -114,20 +114,12 @@ static void PDIProtocol_EnterXPROGMode(void)
        for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--)\r
          PDITarget_SendByte(PDI_NVMENABLE_KEY[i - 1]);\r
 \r
-       /* Poll the STATUS register to check to see if NVM access has been enabled */\r
-       uint8_t NVMAttemptsRemaining = 255;\r
-       while (NVMAttemptsRemaining)\r
-       {\r
-               PDITarget_SendByte(PDI_CMD_LDCS | PD_STATUS_REG);\r
-               if (PDITarget_ReceiveByte() & PDI_STATUS_NVM)\r
-                 break;\r
-\r
-               NVMAttemptsRemaining--;\r
-       }\r
+       /* Wait until the NVM bus becomes active */\r
+       bool NVMBusEnabled = PDITarget_WaitWhileNVMBusBusy();\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
        Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE);\r
-       Endpoint_Write_Byte(NVMAttemptsRemaining ? XPRG_ERR_OK : XPRG_ERR_FAILED);\r
+       Endpoint_Write_Byte(NVMBusEnabled ? XPRG_ERR_OK : XPRG_ERR_FAILED);\r
        Endpoint_ClearIN();\r
 }\r
 \r
@@ -140,7 +132,7 @@ static void PDIProtocol_LeaveXPROGMode(void)
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        /* Clear the RESET key into the RESET PDI register to allow the XMEGA to run */\r
-       PDITarget_SendByte(PDI_CMD_STCS | PD_RESET_REG);        \r
+       PDITarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);       \r
        PDITarget_SendByte(0x00);\r
 \r
        PDITarget_DisableTargetPDI();\r