3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaims all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  XPROG Protocol handler, to process V2 Protocol wrapped XPROG commands used in Atmel programmer devices. 
  36 #define  INCLUDE_FROM_XPROGPROTOCOL_C 
  37 #include "XPROGProtocol.h" 
  39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__) 
  40 /** Base absolute address for the target's NVM controller for PDI programming */ 
  41 uint32_t XPROG_Param_NVMBase       
= 0x010001C0; 
  43 /** Size in bytes of the target's EEPROM page */ 
  44 uint16_t XPROG_Param_EEPageSize    
= 32; 
  46 /** Address of the TPI device's NVMCMD register for TPI programming */ 
  47 uint8_t  XPROG_Param_NVMCMDRegAddr 
= 0x33; 
  49 /** Address of the TPI device's NVMCSR register for TPI programming */ 
  50 uint8_t  XPROG_Param_NVMCSRRegAddr 
= 0x32; 
  52 /** Currently selected XPROG programming protocol */ 
  53 uint8_t  XPROG_SelectedProtocol    
= XPRG_PROTOCOL_PDI
; 
  55 /** Handler for the CMD_XPROG_SETMODE command, which sets the programmer-to-target protocol used for PDI/TPI 
  58 void XPROGProtocol_SetMode(void) 
  63         } SetMode_XPROG_Params
; 
  65         Endpoint_Read_Stream_LE(&SetMode_XPROG_Params
, sizeof(SetMode_XPROG_Params
), NULL
); 
  68         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
  69         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
  71         XPROG_SelectedProtocol 
= SetMode_XPROG_Params
.Protocol
; 
  73         Endpoint_Write_8(CMD_XPROG_SETMODE
); 
  74         Endpoint_Write_8((SetMode_XPROG_Params
.Protocol 
!= XPRG_PROTOCOL_JTAG
) ? STATUS_CMD_OK 
: STATUS_CMD_FAILED
); 
  78 /** Handler for the CMD_XPROG command, which wraps up XPROG commands in a V2 wrapper which need to be 
  79  *  removed and processed so that the underlying XPROG command can be handled. 
  81 void XPROGProtocol_Command(void) 
  83         uint8_t XPROGCommand 
= Endpoint_Read_8(); 
  87                 case XPRG_CMD_ENTER_PROGMODE
: 
  88                         XPROGProtocol_EnterXPROGMode(); 
  90                 case XPRG_CMD_LEAVE_PROGMODE
: 
  91                         XPROGProtocol_LeaveXPROGMode(); 
  94                         XPROGProtocol_Erase(); 
  96                 case XPRG_CMD_WRITE_MEM
: 
  97                         XPROGProtocol_WriteMemory(); 
  99                 case XPRG_CMD_READ_MEM
: 
 100                         XPROGProtocol_ReadMemory(); 
 103                         XPROGProtocol_ReadCRC(); 
 105                 case XPRG_CMD_SET_PARAM
: 
 106                         XPROGProtocol_SetParam(); 
 111 /** Handler for the XPROG ENTER_PROGMODE command to establish a connection with the attached device. */ 
 112 static void XPROGProtocol_EnterXPROGMode(void) 
 115         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
 116         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 118         bool NVMBusEnabled 
= false; 
 120         if (XPROG_SelectedProtocol 
== XPRG_PROTOCOL_PDI
) 
 121           NVMBusEnabled 
= XMEGANVM_EnablePDI(); 
 122         else if (XPROG_SelectedProtocol 
== XPRG_PROTOCOL_TPI
) 
 123           NVMBusEnabled 
= TINYNVM_EnableTPI(); 
 125         Endpoint_Write_8(CMD_XPROG
); 
 126         Endpoint_Write_8(XPRG_CMD_ENTER_PROGMODE
); 
 127         Endpoint_Write_8(NVMBusEnabled ? XPRG_ERR_OK 
: XPRG_ERR_FAILED
); 
 131 /** Handler for the XPROG LEAVE_PROGMODE command to terminate the PDI programming connection with 
 132  *  the attached device. 
 134 static void XPROGProtocol_LeaveXPROGMode(void) 
 137         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
 138         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 140         if (XPROG_SelectedProtocol 
== XPRG_PROTOCOL_PDI
) 
 141           XMEGANVM_DisablePDI(); 
 143           TINYNVM_DisableTPI(); 
 145         #if defined(XCK_RESCUE_CLOCK_ENABLE) && defined(ENABLE_ISP_PROTOCOL) 
 146         /* If the XCK rescue clock option is enabled, we need to restart it once the 
 147          * XPROG mode has been exited, since the XPROG protocol stops it after use. */ 
 148         ISPTarget_ConfigureRescueClock(); 
 151         Endpoint_Write_8(CMD_XPROG
); 
 152         Endpoint_Write_8(XPRG_CMD_LEAVE_PROGMODE
); 
 153         Endpoint_Write_8(XPRG_ERR_OK
); 
 157 /** Handler for the XPRG ERASE command to erase a specific memory address space in the attached device. */ 
 158 static void XPROGProtocol_Erase(void) 
 160         uint8_t ReturnStatus 
= XPRG_ERR_OK
; 
 166         } Erase_XPROG_Params
; 
 168         Endpoint_Read_Stream_LE(&Erase_XPROG_Params
, sizeof(Erase_XPROG_Params
), NULL
); 
 169         Erase_XPROG_Params
.Address 
= SwapEndian_32(Erase_XPROG_Params
.Address
); 
 172         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
 173         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 175         uint8_t EraseCommand
; 
 177         if (XPROG_SelectedProtocol 
== XPRG_PROTOCOL_PDI
) 
 179                 /* Determine which NVM command to send to the device depending on the memory to erase */ 
 180                 switch (Erase_XPROG_Params
.MemoryType
) 
 182                         case XPRG_ERASE_CHIP
: 
 183                                 EraseCommand 
= XMEGA_NVM_CMD_CHIPERASE
; 
 186                                 EraseCommand 
= XMEGA_NVM_CMD_ERASEAPPSEC
; 
 188                         case XPRG_ERASE_BOOT
: 
 189                                 EraseCommand 
= XMEGA_NVM_CMD_ERASEBOOTSEC
; 
 191                         case XPRG_ERASE_EEPROM
: 
 192                                 EraseCommand 
= XMEGA_NVM_CMD_ERASEEEPROM
; 
 194                         case XPRG_ERASE_APP_PAGE
: 
 195                                 EraseCommand 
= XMEGA_NVM_CMD_ERASEAPPSECPAGE
; 
 197                         case XPRG_ERASE_BOOT_PAGE
: 
 198                                 EraseCommand 
= XMEGA_NVM_CMD_ERASEBOOTSECPAGE
; 
 200                         case XPRG_ERASE_EEPROM_PAGE
: 
 201                                 EraseCommand 
= XMEGA_NVM_CMD_ERASEEEPROMPAGE
; 
 203                         case XPRG_ERASE_USERSIG
: 
 204                                 EraseCommand 
= XMEGA_NVM_CMD_ERASEUSERSIG
; 
 207                                 EraseCommand 
= XMEGA_NVM_CMD_NOOP
; 
 211                 /* Erase the target memory, indicate timeout if occurred */ 
 212                 if (!(XMEGANVM_EraseMemory(EraseCommand
, Erase_XPROG_Params
.Address
))) 
 213                   ReturnStatus 
= XPRG_ERR_TIMEOUT
; 
 217                 if (Erase_XPROG_Params
.MemoryType 
== XPRG_ERASE_CHIP
) 
 218                   EraseCommand 
= TINY_NVM_CMD_CHIPERASE
; 
 220                   EraseCommand 
= TINY_NVM_CMD_SECTIONERASE
; 
 222                 /* Erase the target memory, indicate timeout if occurred */ 
 223                 if (!(TINYNVM_EraseMemory(EraseCommand
, Erase_XPROG_Params
.Address
))) 
 224                   ReturnStatus 
= XPRG_ERR_TIMEOUT
; 
 227         Endpoint_Write_8(CMD_XPROG
); 
 228         Endpoint_Write_8(XPRG_CMD_ERASE
); 
 229         Endpoint_Write_8(ReturnStatus
); 
 233 /** Handler for the XPROG WRITE_MEMORY command to write to a specific memory space within the attached device. */ 
 234 static void XPROGProtocol_WriteMemory(void) 
 236         uint8_t ReturnStatus 
= XPRG_ERR_OK
; 
 244                 uint8_t  ProgData
[256]; 
 245         } WriteMemory_XPROG_Params
; 
 247         Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params
, (sizeof(WriteMemory_XPROG_Params
) - 
 248                                                             sizeof(WriteMemory_XPROG_Params
).ProgData
), NULL
); 
 249         WriteMemory_XPROG_Params
.Address 
= SwapEndian_32(WriteMemory_XPROG_Params
.Address
); 
 250         WriteMemory_XPROG_Params
.Length  
= SwapEndian_16(WriteMemory_XPROG_Params
.Length
); 
 251         Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params
.ProgData
, WriteMemory_XPROG_Params
.Length
, NULL
); 
 253         // The driver will terminate transfers that are a round multiple of the endpoint bank in size with a ZLP, need 
 254         // to catch this and discard it before continuing on with packet processing to prevent communication issues 
 255         if (((sizeof(uint8_t) + sizeof(WriteMemory_XPROG_Params
) - sizeof(WriteMemory_XPROG_Params
.ProgData
)) + 
 256             WriteMemory_XPROG_Params
.Length
) % AVRISP_DATA_EPSIZE 
== 0) 
 259                 Endpoint_WaitUntilReady(); 
 263         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
 264         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 266         if (XPROG_SelectedProtocol 
== XPRG_PROTOCOL_PDI
) 
 268                 /* Assume FLASH page programming by default, as it is the common case */ 
 269                 uint8_t WriteCommand     
= XMEGA_NVM_CMD_WRITEFLASHPAGE
; 
 270                 uint8_t WriteBuffCommand 
= XMEGA_NVM_CMD_LOADFLASHPAGEBUFF
; 
 271                 uint8_t EraseBuffCommand 
= XMEGA_NVM_CMD_ERASEFLASHPAGEBUFF
; 
 272                 bool    PagedMemory      
= true; 
 274                 switch (WriteMemory_XPROG_Params
.MemoryType
) 
 276                         case XPRG_MEM_TYPE_APPL
: 
 277                                 WriteCommand     
= XMEGA_NVM_CMD_WRITEAPPSECPAGE
; 
 279                         case XPRG_MEM_TYPE_BOOT
: 
 280                                 WriteCommand     
= XMEGA_NVM_CMD_WRITEBOOTSECPAGE
; 
 282                         case XPRG_MEM_TYPE_EEPROM
: 
 283                                 WriteCommand     
= XMEGA_NVM_CMD_ERASEWRITEEEPROMPAGE
; 
 284                                 WriteBuffCommand 
= XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF
; 
 285                                 EraseBuffCommand 
= XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF
; 
 287                         case XPRG_MEM_TYPE_USERSIG
: 
 288                                 WriteCommand     
= XMEGA_NVM_CMD_WRITEUSERSIG
; 
 290                         case XPRG_MEM_TYPE_FUSE
: 
 291                                 WriteCommand     
= XMEGA_NVM_CMD_WRITEFUSE
; 
 294                         case XPRG_MEM_TYPE_LOCKBITS
: 
 295                                 WriteCommand     
= XMEGA_NVM_CMD_WRITELOCK
; 
 300                 /* Send the appropriate memory write commands to the device, indicate timeout if occurred */ 
 301                 if ((PagedMemory 
&& !(XMEGANVM_WritePageMemory(WriteBuffCommand
, EraseBuffCommand
, WriteCommand
, 
 302                                                                                                            WriteMemory_XPROG_Params
.PageMode
, WriteMemory_XPROG_Params
.Address
, 
 303                                                                                                            WriteMemory_XPROG_Params
.ProgData
, WriteMemory_XPROG_Params
.Length
))) || 
 304                    (!PagedMemory 
&& !(XMEGANVM_WriteByteMemory(WriteCommand
, WriteMemory_XPROG_Params
.Address
, 
 305                                                                                                            WriteMemory_XPROG_Params
.ProgData
[0])))) 
 307                         ReturnStatus 
= XPRG_ERR_TIMEOUT
; 
 312                 /* Send write command to the TPI device, indicate timeout if occurred */ 
 313                 if (!(TINYNVM_WriteMemory(WriteMemory_XPROG_Params
.Address
, WriteMemory_XPROG_Params
.ProgData
, 
 314                       WriteMemory_XPROG_Params
.Length
))) 
 316                         ReturnStatus 
= XPRG_ERR_TIMEOUT
; 
 320         Endpoint_Write_8(CMD_XPROG
); 
 321         Endpoint_Write_8(XPRG_CMD_WRITE_MEM
); 
 322         Endpoint_Write_8(ReturnStatus
); 
 326 /** Handler for the XPROG READ_MEMORY command to read data from a specific address space within the 
 329 static void XPROGProtocol_ReadMemory(void) 
 331         uint8_t ReturnStatus 
= XPRG_ERR_OK
; 
 338         } ReadMemory_XPROG_Params
; 
 340         Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params
, sizeof(ReadMemory_XPROG_Params
), NULL
); 
 341         ReadMemory_XPROG_Params
.Address 
= SwapEndian_32(ReadMemory_XPROG_Params
.Address
); 
 342         ReadMemory_XPROG_Params
.Length  
= SwapEndian_16(ReadMemory_XPROG_Params
.Length
); 
 345         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
 346         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 348         uint8_t ReadBuffer
[256]; 
 350         if (XPROG_SelectedProtocol 
== XPRG_PROTOCOL_PDI
) 
 352                 /* Read the PDI target's memory, indicate timeout if occurred */ 
 353                 if (!(XMEGANVM_ReadMemory(ReadMemory_XPROG_Params
.Address
, ReadBuffer
, ReadMemory_XPROG_Params
.Length
))) 
 354                   ReturnStatus 
= XPRG_ERR_TIMEOUT
; 
 358                 /* Read the TPI target's memory, indicate timeout if occurred */ 
 359                 if (!(TINYNVM_ReadMemory(ReadMemory_XPROG_Params
.Address
, ReadBuffer
, ReadMemory_XPROG_Params
.Length
))) 
 360                   ReturnStatus 
= XPRG_ERR_TIMEOUT
; 
 363         Endpoint_Write_8(CMD_XPROG
); 
 364         Endpoint_Write_8(XPRG_CMD_READ_MEM
); 
 365         Endpoint_Write_8(ReturnStatus
); 
 367         if (ReturnStatus 
== XPRG_ERR_OK
) 
 368           Endpoint_Write_Stream_LE(ReadBuffer
, ReadMemory_XPROG_Params
.Length
, NULL
); 
 373 /** Handler for the XPROG CRC command to read a specific memory space's CRC value for comparison between the 
 374  *  attached device's memory and a data set on the host. 
 376 static void XPROGProtocol_ReadCRC(void) 
 378         uint8_t ReturnStatus 
= XPRG_ERR_OK
; 
 383         } ReadCRC_XPROG_Params
; 
 385         Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params
, sizeof(ReadCRC_XPROG_Params
), NULL
); 
 388         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
 389         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 393         if (XPROG_SelectedProtocol 
== XPRG_PROTOCOL_PDI
) 
 397                 /* Determine which NVM command to send to the device depending on the memory to CRC */ 
 398                 switch (ReadCRC_XPROG_Params
.CRCType
) 
 401                                 CRCCommand 
= XMEGA_NVM_CMD_APPCRC
; 
 404                                 CRCCommand 
= XMEGA_NVM_CMD_BOOTCRC
; 
 407                                 CRCCommand 
= XMEGA_NVM_CMD_FLASHCRC
; 
 411                 /* Perform and retrieve the memory CRC, indicate timeout if occurred */ 
 412                 if (!(XMEGANVM_GetMemoryCRC(CRCCommand
, &MemoryCRC
))) 
 413                   ReturnStatus 
= XPRG_ERR_TIMEOUT
; 
 417                 /* TPI does not support memory CRC */ 
 418                 ReturnStatus 
= XPRG_ERR_FAILED
; 
 421         Endpoint_Write_8(CMD_XPROG
); 
 422         Endpoint_Write_8(XPRG_CMD_CRC
); 
 423         Endpoint_Write_8(ReturnStatus
); 
 425         if (ReturnStatus 
== XPRG_ERR_OK
) 
 427                 Endpoint_Write_8(MemoryCRC 
>> 16); 
 428                 Endpoint_Write_16_LE(MemoryCRC 
& 0xFFFF); 
 434 /** Handler for the XPROG SET_PARAM command to set a XPROG parameter for use when communicating with the 
 437 static void XPROGProtocol_SetParam(void) 
 439         uint8_t ReturnStatus 
= XPRG_ERR_OK
; 
 441         uint8_t XPROGParam 
= Endpoint_Read_8(); 
 443         /* Determine which parameter is being set, store the new parameter value */ 
 446                 case XPRG_PARAM_NVMBASE
: 
 447                         XPROG_Param_NVMBase       
= Endpoint_Read_32_BE(); 
 449                 case XPRG_PARAM_EEPPAGESIZE
: 
 450                         XPROG_Param_EEPageSize    
= Endpoint_Read_16_BE(); 
 452                 case XPRG_PARAM_NVMCMD_REG
: 
 453                         XPROG_Param_NVMCMDRegAddr 
= Endpoint_Read_8(); 
 455                 case XPRG_PARAM_NVMCSR_REG
: 
 456                         XPROG_Param_NVMCSRRegAddr 
= Endpoint_Read_8(); 
 458                 case XPRG_PARAM_UNKNOWN_1
: 
 459                         /* TODO: Undocumented parameter added in AVRStudio 5.1, purpose unknown. Must ACK and discard or 
 460                                  the communication with AVRStudio 5.1 will fail. 
 462                         Endpoint_Discard_16(); 
 465                         ReturnStatus 
= XPRG_ERR_FAILED
; 
 470         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR
); 
 471         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 473         Endpoint_Write_8(CMD_XPROG
); 
 474         Endpoint_Write_8(XPRG_CMD_SET_PARAM
); 
 475         Endpoint_Write_8(ReturnStatus
);