+/** Handler for the XPROG ENTER_PROGMODE command to establish a PDI connection with the attached device. */\r
+static void PDIProtocol_EnterXPROGMode(void)\r
+{\r
+ uint8_t ReturnStatus = XPRG_ERR_OK;\r
+\r
+ Endpoint_ClearOUT();\r
+ Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+ \r
+ PDIDATA_LINE_DDR |= PDIDATA_LINE_MASK;\r
+ PDICLOCK_LINE_DDR |= PDICLOCK_LINE_MASK;\r
+ \r
+ /* Must hold DATA line high for at least 90nS to enable PDI interface */\r
+ PDIDATA_LINE_PORT |= PDIDATA_LINE_MASK;\r
+ asm volatile ("NOP"::);\r
+ asm volatile ("NOP"::);\r
+ \r
+ /* Toggle CLOCK line 16 times within 100uS of the original 90nS timeout to keep PDI interface enabled */\r
+ for (uint8_t i = 0; i < 16; i++)\r
+ TOGGLE_PDI_CLOCK;\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
+ for (uint8_t i = 0; i < sizeof(PDI_NVMENABLE_KEY); i++)\r
+ PDITarget_SendByte(PDI_NVMENABLE_KEY[i]);\r
+\r
+ /* Read out the STATUS register to check that NVM access was successfully enabled */\r
+ PDITarget_SendByte(PDI_CMD_LDCS | PD_STATUS_REG); \r
+ if (!(PDITarget_ReceiveByte() & PDI_STATUS_NVM))\r
+ ReturnStatus = XPRG_ERR_FAILED;\r
+ \r
+ Endpoint_Write_Byte(CMD_XPROG);\r
+ Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE);\r
+ Endpoint_Write_Byte(ReturnStatus);\r
+ Endpoint_ClearIN();\r
+}\r
+\r
+/** Handler for the XPROG LEAVE_PROGMODE command to terminate the PDI programming connection with\r
+ * the attached device.\r
+ */\r
+static void PDIProtocol_LeaveXPROGMode(void)\r
+{\r
+ Endpoint_ClearOUT();\r
+ Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+ \r
+ /* Set DATA and CLOCK lines to inputs */\r
+ PDIDATA_LINE_DDR &= ~PDIDATA_LINE_MASK;\r
+ PDICLOCK_LINE_DDR &= ~PDICLOCK_LINE_MASK;\r
+ \r
+ /* Tristate DATA and CLOCK lines */\r
+ PDIDATA_LINE_PORT &= ~PDIDATA_LINE_MASK;\r
+ PDICLOCK_LINE_PORT &= ~PDICLOCK_LINE_MASK;\r
+ \r
+ Endpoint_Write_Byte(CMD_XPROG);\r
+ Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE);\r
+ Endpoint_Write_Byte(XPRG_ERR_OK);\r
+ Endpoint_ClearIN();\r
+}\r
+\r
+/** Handler for the XPRG ERASE command to erase a specific memory address space in the attached device. */\r
+static void PDIProtocol_Erase(void)\r