aboutsummaryrefslogtreecommitdiff
path: root/Projects/AVRISP/Lib/PDIProtocol.c
diff options
context:
space:
mode:
authorDean Camera <[email protected]>2009-12-11 00:19:25 +0000
committerDean Camera <[email protected]>2009-12-11 00:19:25 +0000
commit7e5966c1a88bf670c2f4962e8a52fcfc3528e82a (patch)
tree306b0ada4c354cfff19a9e502cd93e5a70a56399 /Projects/AVRISP/Lib/PDIProtocol.c
parent42cfd157936571c70b6ba0be48fbda1ab7b450a1 (diff)
downloadqmk_firmware-7e5966c1a88bf670c2f4962e8a52fcfc3528e82a.tar.gz
qmk_firmware-7e5966c1a88bf670c2f4962e8a52fcfc3528e82a.zip
Fix AVRISP PDI protocol - make sure inverted masks have the appropriate parenthesis around them, make sure the BREAK command for the hardware USART has 12 clock cycles exactly. Poll NVM Enable bit in the target's STATUS PDI register with a timeout, as it is not set immediately.
Diffstat (limited to 'Projects/AVRISP/Lib/PDIProtocol.c')
-rw-r--r--Projects/AVRISP/Lib/PDIProtocol.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/Projects/AVRISP/Lib/PDIProtocol.c b/Projects/AVRISP/Lib/PDIProtocol.c
index c1e7bf2ee6..28bd540716 100644
--- a/Projects/AVRISP/Lib/PDIProtocol.c
+++ b/Projects/AVRISP/Lib/PDIProtocol.c
@@ -99,15 +99,13 @@ void PDIProtocol_XPROG_Command(void)
/** Handler for the XPROG ENTER_PROGMODE command to establish a PDI connection with the attached device. */
static void PDIProtocol_EnterXPROGMode(void)
{
- uint8_t ReturnStatus = XPRG_ERR_OK;
-
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
/* Enable PDI programming mode with the attached target */
PDITarget_EnableTargetPDI();
- /* Store the RESET ket into the RESET PDI register to complete the handshake */
+ /* Store the RESET key into the RESET PDI register to keep the XMEGA in reset */
PDITarget_SendByte(PDI_CMD_STCS | PD_RESET_REG);
PDITarget_SendByte(PDI_RESET_KEY);
@@ -116,14 +114,20 @@ static void PDIProtocol_EnterXPROGMode(void)
for (uint8_t i = 0; i < sizeof(PDI_NVMENABLE_KEY); i++)
PDITarget_SendByte(PDI_NVMENABLE_KEY[i]);
- /* Read out the STATUS register to check that NVM access was successfully enabled */
- PDITarget_SendByte(PDI_CMD_LDCS | PD_STATUS_REG);
- if (!(PDITarget_ReceiveByte() & PDI_STATUS_NVM))
- ReturnStatus = XPRG_ERR_FAILED;
+ /* Poll the STATUS register to check to see if NVM access has been enabled */
+ uint8_t NVMAttemptsRemaining = 200;
+ while (NVMAttemptsRemaining--)
+ {
+ _delay_ms(1);
+ PDITarget_SendByte(PDI_CMD_LDCS | PD_STATUS_REG);
+
+ if (PDITarget_ReceiveByte() & PDI_STATUS_NVM)
+ break;
+ }
Endpoint_Write_Byte(CMD_XPROG);
Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE);
- Endpoint_Write_Byte(ReturnStatus);
+ Endpoint_Write_Byte(NVMAttemptsRemaining ? XPRG_ERR_OK : XPRG_ERR_FAILED);
Endpoint_ClearIN();
}
@@ -135,6 +139,10 @@ static void PDIProtocol_LeaveXPROGMode(void)
Endpoint_ClearOUT();
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
+ /* Clear the RESET key into the RESET PDI register to allow the XMEGA to run */
+ PDITarget_SendByte(PDI_CMD_STCS | PD_RESET_REG);
+ PDITarget_SendByte(0x00);
+
PDITarget_DisableTargetPDI();
Endpoint_Write_Byte(CMD_XPROG);