X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/1f8dfd0205d431351425f468627984280b21cd5a..7aecda6fda5bcced68d72b0cf73d00174aa5c7cd:/Projects/AVRISP/Lib/PDIProtocol.c diff --git a/Projects/AVRISP/Lib/PDIProtocol.c b/Projects/AVRISP/Lib/PDIProtocol.c index b095a49c5..864d4e9c3 100644 --- a/Projects/AVRISP/Lib/PDIProtocol.c +++ b/Projects/AVRISP/Lib/PDIProtocol.c @@ -28,14 +28,16 @@ this software. */ -#if defined(ENABLE_XPROG_PROTOCOL) +#if defined(ENABLE_PDI_PROTOCOL) || defined(__DOXYGEN__) + +#warning PDI Programming Protocol support is incomplete and not currently suitable for use. /** \file * * PDI Protocol handler, to process V2 Protocol wrapped PDI commands used in Atmel programmer devices. */ -#define INCLUDE_FROM_XPROG_C +#define INCLUDE_FROM_PDIPROTOCOL_C #include "PDIProtocol.h" uint32_t XPROG_Param_NVMBase; @@ -61,6 +63,9 @@ void PDIProtocol_XPROG_SetMode(void) Endpoint_ClearIN(); } +/** Handler for the CMD_XPROG command, which wraps up XPROG commands in a V2 wrapper which need to be + * removed and processed so that the underlying XPROG command can be handled. + */ void PDIProtocol_XPROG_Command(void) { uint8_t XPROGCommand = Endpoint_Read_Byte(); @@ -74,7 +79,7 @@ void PDIProtocol_XPROG_Command(void) PDIProtocol_LeaveXPROGMode(); break; case XPRG_CMD_ERASE: - PDIProtocol_EraseChip(); + PDIProtocol_Erase(); break; case XPRG_CMD_WRITE_MEM: PDIProtocol_WriteMemory(); @@ -91,7 +96,66 @@ void PDIProtocol_XPROG_Command(void) } } -static void PDIProtocol_EraseChip(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); + + PDIDATA_LINE_DDR |= PDIDATA_LINE_MASK; + PDICLOCK_LINE_DDR |= PDICLOCK_LINE_MASK; + + /* Must hold DATA line high for at least 90nS to enable PDI interface */ + PDIDATA_LINE_PORT |= PDIDATA_LINE_MASK; + asm volatile ("NOP"::); + asm volatile ("NOP"::); + + /* Toggle CLOCK line 16 times within 100uS of the original 90nS timeout to keep PDI interface enabled */ + for (uint8_t i = 0; i < 16; i++) + TOGGLE_PDI_CLOCK; + + /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */ + PDITarget_SendByte(PDI_CMD_KEY); + 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; + + Endpoint_Write_Byte(CMD_XPROG); + Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE); + Endpoint_Write_Byte(ReturnStatus); + Endpoint_ClearIN(); +} + +/** Handler for the XPROG LEAVE_PROGMODE command to terminate the PDI programming connection with + * the attached device. + */ +static void PDIProtocol_LeaveXPROGMode(void) +{ + Endpoint_ClearOUT(); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + /* Set DATA and CLOCK lines to inputs */ + PDIDATA_LINE_DDR &= ~PDIDATA_LINE_MASK; + PDICLOCK_LINE_DDR &= ~PDICLOCK_LINE_MASK; + + /* Tristate DATA and CLOCK lines */ + PDIDATA_LINE_PORT &= ~PDIDATA_LINE_MASK; + PDICLOCK_LINE_PORT &= ~PDICLOCK_LINE_MASK; + + Endpoint_Write_Byte(CMD_XPROG); + Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE); + Endpoint_Write_Byte(XPRG_ERR_OK); + Endpoint_ClearIN(); +} + +/** Handler for the XPRG ERASE command to erase a specific memory address space in the attached device. */ +static void PDIProtocol_Erase(void) { uint8_t ReturnStatus = XPRG_ERR_OK; @@ -114,6 +178,7 @@ static void PDIProtocol_EraseChip(void) Endpoint_ClearIN(); } +/** Handler for the XPROG WRITE_MEMORY command to write to a specific memory space within the attached device. */ static void PDIProtocol_WriteMemory(void) { uint8_t ReturnStatus = XPRG_ERR_OK; @@ -143,6 +208,9 @@ static void PDIProtocol_WriteMemory(void) Endpoint_ClearIN(); } +/** Handler for the XPROG READ_MEMORY command to read data from a specific address space within the + * attached device. + */ static void PDIProtocol_ReadMemory(void) { uint8_t ReturnStatus = XPRG_ERR_OK; @@ -166,23 +234,13 @@ static void PDIProtocol_ReadMemory(void) Endpoint_Write_Byte(CMD_XPROG); Endpoint_Write_Byte(XPRG_CMD_READ_MEM); Endpoint_Write_Byte(ReturnStatus); - - // START TEMP - uint8_t ProgData[256]; - for (uint16_t i = 0; i < ReadMemory_XPROG_Params.Length; i++) - ProgData[i] = i; - Endpoint_Write_Stream_LE(ProgData, ReadMemory_XPROG_Params.Length); - - if (!Endpoint_IsReadWriteAllowed()) - { - Endpoint_ClearIN(); - while(!(Endpoint_IsReadWriteAllowed())); - } - // END TEMP Endpoint_ClearIN(); } +/** Handler for the XPROG CRC command to read a specific memory space's CRC value for comparison between the + * attached device's memory and a data set on the host. + */ static void PDIProtocol_ReadCRC(void) { uint8_t ReturnStatus = XPRG_ERR_OK; @@ -209,34 +267,9 @@ static void PDIProtocol_ReadCRC(void) Endpoint_ClearIN(); } -static void PDIProtocol_EnterXPROGMode(void) -{ - uint8_t ReturnStatus = XPRG_ERR_OK; - - Endpoint_ClearOUT(); - Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - - // TODO: Enter device programming mode here via PDI protocol - - Endpoint_Write_Byte(CMD_XPROG); - Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE); - Endpoint_Write_Byte(ReturnStatus); - Endpoint_ClearIN(); -} - -static void PDIProtocol_LeaveXPROGMode(void) -{ - Endpoint_ClearOUT(); - Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); - - // TODO: Exit device programming mode here via PDI protocol - - Endpoint_Write_Byte(CMD_XPROG); - Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE); - Endpoint_Write_Byte(XPRG_ERR_OK); - Endpoint_ClearIN(); -} - +/** Handler for the XPROG SET_PARAM command to set a PDI parameter for use when communicating with the + * attached device. + */ static void PDIProtocol_SetParam(void) { uint8_t ReturnStatus = XPRG_ERR_OK;