+/** Handler for the XPROG ENTER_PROGMODE command to establish a PDI connection with the attached device. */\r
+static void PDIProtocol_EnterXPROGMode(void)\r
+{\r
+ Endpoint_ClearOUT();\r
+ Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+ \r
+ /* Enable PDI programming mode with the attached target */\r
+ 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 | 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
+ PDITarget_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
+\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(NVMBusEnabled ? XPRG_ERR_OK : XPRG_ERR_FAILED);\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
+ /* 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
+\r
+ PDITarget_DisableTargetPDI();\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