Complete TPI protocol code to enter and exit TPI programming mode for the ATTINY...
[pub/USBasp.git] / Projects / AVRISP / Lib / XPROG / XPROGProtocol.c
index d919413..edcc874 100644 (file)
@@ -44,7 +44,7 @@ uint32_t XPROG_Param_NVMBase = 0x010001C0;
 uint32_t XPROG_Param_EEPageSize;\r
 \r
 /** Currently selected XPROG programming protocol */\r
-uint8_t  XPROG_SelectedProtocol;\r
+uint8_t  XPROG_SelectedProtocol = XPRG_PROTOCOL_PDI;\r
 \r
 \r
 /** Handler for the CMD_XPROG_SETMODE command, which sets the programmer-to-target protocol used for PDI/TPI\r
@@ -113,23 +113,32 @@ static void XPROGProtocol_EnterXPROGMode(void)
        if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)\r
        {\r
                /* Enable PDI programming mode with the attached target */\r
-               PDITarget_EnableTargetPDI();\r
+               XPROGTarget_EnableTargetPDI();\r
                \r
                /* Store the RESET key into the RESET PDI register to keep the XMEGA in reset */\r
-               PDITarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);       \r
-               PDITarget_SendByte(PDI_RESET_KEY);\r
+               XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);     \r
+               XPROGTarget_SendByte(PDI_RESET_KEY);\r
 \r
                /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
-               PDITarget_SendByte(PDI_CMD_KEY);        \r
+               XPROGTarget_SendByte(PDI_CMD_KEY);      \r
                for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--)\r
-                 PDITarget_SendByte(PDI_NVMENABLE_KEY[i - 1]);\r
+                 XPROGTarget_SendByte(PDI_NVMENABLE_KEY[i - 1]);\r
 \r
                /* Wait until the NVM bus becomes active */\r
-               NVMBusEnabled = PDITarget_WaitWhileNVMBusBusy();\r
+               NVMBusEnabled = XMEGANVM_WaitWhileNVMBusBusy();\r
        }\r
        else\r
        {\r
-               // TODO\r
+               /* Enable TPI programming mode with the attached target */\r
+               XPROGTarget_EnableTargetTPI();\r
+               \r
+               /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
+               XPROGTarget_SendByte(TPI_CMD_SKEY);     \r
+               for (uint8_t i = sizeof(TPI_NVMENABLE_KEY); i > 0; i--)\r
+                 XPROGTarget_SendByte(TPI_NVMENABLE_KEY[i - 1]);\r
+\r
+               /* Wait until the NVM bus becomes active */\r
+               NVMBusEnabled = TINYNVM_WaitWhileNVMBusBusy();\r
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
@@ -149,14 +158,18 @@ static void XPROGProtocol_LeaveXPROGMode(void)
        if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)\r
        {\r
                /* Clear the RESET key in the RESET PDI register to allow the XMEGA to run */\r
-               PDITarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);       \r
-               PDITarget_SendByte(0x00);\r
+               XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);     \r
+               XPROGTarget_SendByte(0x00);\r
 \r
-               PDITarget_DisableTargetPDI();\r
+               XPROGTarget_DisableTargetPDI();\r
        }\r
        else\r
        {\r
-               // TODO\r
+               /* Clear the NVMEN bit in the TPI CONTROL register to disable TPI mode */\r
+               XPROGTarget_SendByte(TPI_CMD_SSTCS | TPI_CTRL_REG);     \r
+               XPROGTarget_SendByte(0x00);\r
+       \r
+               XPROGTarget_DisableTargetTPI();\r
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
@@ -381,7 +394,8 @@ static void XPROGProtocol_ReadCRC(void)
        }\r
        else\r
        {\r
-               // TODO\r
+               /* TPI does not support memory CRC */\r
+               ReturnStatus = XPRG_ERR_FAILED;\r
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r