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  *  Target-related functions for the XMEGA target's NVM module. 
  36 #define  INCLUDE_FROM_XMEGA_NVM_C 
  39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__) 
  41 /** Sends the given 32-bit absolute address to the target. 
  43  *  \param[in] AbsoluteAddress  Absolute address to send to the target 
  45 static void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress
) 
  47         /* Send the given 32-bit address to the target, LSB first */ 
  48         XPROGTarget_SendByte(AbsoluteAddress 
&  0xFF); 
  49         XPROGTarget_SendByte(AbsoluteAddress 
>> 8); 
  50         XPROGTarget_SendByte(AbsoluteAddress 
>> 16); 
  51         XPROGTarget_SendByte(AbsoluteAddress 
>> 24); 
  54 /** Sends the given NVM register address to the target. 
  56  *  \param[in] Register  NVM register whose absolute address is to be sent 
  58 static void XMEGANVM_SendNVMRegAddress(const uint8_t Register
) 
  60         /* Determine the absolute register address from the NVM base memory address and the NVM register address */ 
  61         uint32_t Address 
= XPROG_Param_NVMBase 
| Register
; 
  63         /* Send the calculated 32-bit address to the target, LSB first */ 
  64         XMEGANVM_SendAddress(Address
); 
  67 /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read or CRC 
  70  *  \return Boolean \c true if the NVM controller became ready within the timeout period, \c false otherwise 
  72 bool XMEGANVM_WaitWhileNVMBusBusy(void) 
  74         /* Poll the STATUS register to check to see if NVM access has been enabled */ 
  77                 /* Send the LDCS command to read the PDI STATUS register to see the NVM bus is active */ 
  78                 XPROGTarget_SendByte(PDI_CMD_LDCS 
| PDI_STATUS_REG
); 
  80                 uint8_t StatusRegister 
= XPROGTarget_ReceiveByte(); 
  82                 /* We might have timed out waiting for the status register read response, check here */ 
  83                 if (!(TimeoutTicksRemaining
)) 
  86                 /* Check the status register read response to see if the NVM bus is enabled */ 
  87                 if (StatusRegister 
& PDI_STATUS_NVM
) 
  92 /** Waits while the target's NVM controller is busy performing an operation, exiting if the 
  93  *  timeout period expires. 
  95  *  \return Boolean \c true if the NVM controller became ready within the timeout period, \c false otherwise 
  97 bool XMEGANVM_WaitWhileNVMControllerBusy(void) 
  99         /* Preload the pointer register with the NVM STATUS register address to check the BUSY flag */ 
 100         XPROGTarget_SendByte(PDI_CMD_ST 
| (PDI_POINTER_DIRECT 
<< 2) | PDI_DATSIZE_4BYTES
); 
 101         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS
); 
 103         /* Poll the NVM STATUS register while the NVM controller is busy */ 
 106                 /* Fetch the current status value via the pointer register (without auto-increment afterwards) */ 
 107                 XPROGTarget_SendByte(PDI_CMD_LD 
| (PDI_POINTER_INDIRECT 
<< 2) | PDI_DATSIZE_1BYTE
); 
 109                 uint8_t StatusRegister 
= XPROGTarget_ReceiveByte(); 
 111                 /* We might have timed out waiting for the status register read response, check here */ 
 112                 if (!(TimeoutTicksRemaining
)) 
 115                 /* Check to see if the BUSY flag is still set */ 
 116                 if (!(StatusRegister 
& (1 << 7))) 
 121 /** Enables the physical PDI interface on the target and enables access to the internal NVM controller. 
 123  *  \return Boolean \c true if the PDI interface was enabled successfully, \c false otherwise 
 125 bool XMEGANVM_EnablePDI(void) 
 127         /* Enable PDI programming mode with the attached target */ 
 128         XPROGTarget_EnableTargetPDI(); 
 130         /* Store the RESET key into the RESET PDI register to keep the XMEGA in reset */ 
 131         XPROGTarget_SendByte(PDI_CMD_STCS 
| PDI_RESET_REG
); 
 132         XPROGTarget_SendByte(PDI_RESET_KEY
); 
 134         /* Lower direction change guard time to 32 USART bits */ 
 135         XPROGTarget_SendByte(PDI_CMD_STCS 
| PDI_CTRL_REG
); 
 136         XPROGTarget_SendByte(0x02); 
 138         /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */ 
 139         XPROGTarget_SendByte(PDI_CMD_KEY
); 
 140         for (uint8_t i 
= sizeof(PDI_NVMENABLE_KEY
); i 
> 0; i
--) 
 141           XPROGTarget_SendByte(PDI_NVMENABLE_KEY
[i 
- 1]); 
 143         /* Wait until the NVM bus becomes active */ 
 144         return XMEGANVM_WaitWhileNVMBusBusy(); 
 147 /** Removes access to the target's NVM controller and physically disables the target's physical PDI interface. */ 
 148 void XMEGANVM_DisablePDI(void) 
 150         XMEGANVM_WaitWhileNVMBusBusy(); 
 152         /* Clear the RESET key in the RESET PDI register to allow the XMEGA to run - must perform this until the 
 153          * change takes effect, as in some cases it takes multiple writes (silicon bug?). 
 157                 /* Clear reset register */ 
 158                 XPROGTarget_SendByte(PDI_CMD_STCS 
| PDI_RESET_REG
); 
 159                 XPROGTarget_SendByte(0x00); 
 161                 /* Read back the reset register, check to see if it took effect */ 
 162                 XPROGTarget_SendByte(PDI_CMD_LDCS 
| PDI_RESET_REG
); 
 163         } while (XPROGTarget_ReceiveByte() != 0x00); 
 165         XPROGTarget_DisableTargetPDI(); 
 168 /** Retrieves the CRC value of the given memory space. 
 170  *  \param[in]  CRCCommand  NVM CRC command to issue to the target 
 171  *  \param[out] CRCDest     CRC Destination when read from the target 
 173  *  \return Boolean \c true if the command sequence complete successfully 
 175 bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand
, 
 176                            uint32_t* const CRCDest
) 
 180         /* Wait until the NVM controller is no longer busy */ 
 181         if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 184         /* Set the NVM command to the correct CRC read command */ 
 185         XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 186         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 187         XPROGTarget_SendByte(CRCCommand
); 
 189         /* Set CMDEX bit in NVM CTRLA register to start the CRC generation */ 
 190         XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 191         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
); 
 192         XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX
); 
 194         /* Wait until the NVM bus is ready again */ 
 195         if (!(XMEGANVM_WaitWhileNVMBusBusy())) 
 198         /* Wait until the NVM controller is no longer busy */ 
 199         if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 202         /* Load the PDI pointer register with the DAT0 register start address */ 
 203         XPROGTarget_SendByte(PDI_CMD_ST 
| (PDI_POINTER_DIRECT 
<< 2) | PDI_DATSIZE_4BYTES
); 
 204         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0
); 
 206         /* Send the REPEAT command to grab the CRC bytes */ 
 207         XPROGTarget_SendByte(PDI_CMD_REPEAT 
| PDI_DATSIZE_1BYTE
); 
 208         XPROGTarget_SendByte(XMEGA_CRC_LENGTH_BYTES 
- 1); 
 210         /* Read in the CRC bytes from the target */ 
 211         XPROGTarget_SendByte(PDI_CMD_LD 
| (PDI_POINTER_INDIRECT_PI 
<< 2) | PDI_DATSIZE_1BYTE
); 
 212         for (uint8_t i 
= 0; i 
< XMEGA_CRC_LENGTH_BYTES
; i
++) 
 213           ((uint8_t*)CRCDest
)[i
] = XPROGTarget_ReceiveByte(); 
 215         return (TimeoutTicksRemaining 
> 0); 
 218 /** Reads memory from the target's memory spaces. 
 220  *  \param[in]  ReadAddress  Start address to read from within the target's address space 
 221  *  \param[out] ReadBuffer   Buffer to store read data into 
 222  *  \param[in]  ReadSize     Number of bytes to read 
 224  *  \return Boolean \c true if the command sequence complete successfully 
 226 bool XMEGANVM_ReadMemory(const uint32_t ReadAddress
, 
 230         /* Wait until the NVM controller is no longer busy */ 
 231         if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 234         /* Send the READNVM command to the NVM controller for reading of an arbitrary location */ 
 235         XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 236         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 237         XPROGTarget_SendByte(XMEGA_NVM_CMD_READNVM
); 
 239         /* Load the PDI pointer register with the start address we want to read from */ 
 240         XPROGTarget_SendByte(PDI_CMD_ST 
| (PDI_POINTER_DIRECT 
<< 2) | PDI_DATSIZE_4BYTES
); 
 241         XMEGANVM_SendAddress(ReadAddress
); 
 243         /* Send the REPEAT command with the specified number of bytes to read */ 
 244         XPROGTarget_SendByte(PDI_CMD_REPEAT 
| PDI_DATSIZE_1BYTE
); 
 245         XPROGTarget_SendByte(ReadSize 
- 1); 
 247         /* Send a LD command with indirect access and post-increment to read out the bytes */ 
 248         XPROGTarget_SendByte(PDI_CMD_LD 
| (PDI_POINTER_INDIRECT_PI 
<< 2) | PDI_DATSIZE_1BYTE
); 
 249         while (ReadSize
-- && TimeoutTicksRemaining
) 
 250           *(ReadBuffer
++) = XPROGTarget_ReceiveByte(); 
 252         return (TimeoutTicksRemaining 
> 0); 
 255 /** Writes byte addressed memory to the target's memory spaces. 
 257  *  \param[in]  WriteCommand  Command to send to the device to write each memory byte 
 258  *  \param[in]  WriteAddress  Address to write to within the target's address space 
 259  *  \param[in]  Byte          Byte to write to the target 
 261  *  \return Boolean \c true if the command sequence complete successfully 
 263 bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand
, 
 264                               const uint32_t WriteAddress
, 
 267         /* Wait until the NVM controller is no longer busy */ 
 268         if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 271         /* Send the memory write command to the target */ 
 272         XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 273         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 274         XPROGTarget_SendByte(WriteCommand
); 
 276         /* Send new memory byte to the memory of the target */ 
 277         XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 278         XMEGANVM_SendAddress(WriteAddress
); 
 279         XPROGTarget_SendByte(Byte
); 
 284 /** Writes page addressed memory to the target's memory spaces. 
 286  *  \param[in]  WriteBuffCommand  Command to send to the device to write a byte to the memory page buffer 
 287  *  \param[in]  EraseBuffCommand  Command to send to the device to erase the memory page buffer 
 288  *  \param[in]  WritePageCommand  Command to send to the device to write the page buffer to the destination memory 
 289  *  \param[in]  PageMode          Bitfield indicating what operations need to be executed on the specified page 
 290  *  \param[in]  WriteAddress      Start address to write the page data to within the target's address space 
 291  *  \param[in]  WriteBuffer       Buffer to source data from 
 292  *  \param[in]  WriteSize         Number of bytes to write 
 294  *  \return Boolean \c true if the command sequence complete successfully 
 296 bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand
, 
 297                               const uint8_t EraseBuffCommand
, 
 298                               const uint8_t WritePageCommand
, 
 299                               const uint8_t PageMode
, 
 300                               const uint32_t WriteAddress
, 
 301                               const uint8_t* WriteBuffer
, 
 304         if (PageMode 
& XPRG_PAGEMODE_ERASE
) 
 306                 /* Wait until the NVM controller is no longer busy */ 
 307                 if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 310                 /* Send the memory buffer erase command to the target */ 
 311                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 312                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 313                 XPROGTarget_SendByte(EraseBuffCommand
); 
 315                 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */ 
 316                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 317                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
); 
 318                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX
); 
 323                 /* Wait until the NVM controller is no longer busy */ 
 324                 if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 327                 /* Send the memory buffer write command to the target */ 
 328                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 329                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 330                 XPROGTarget_SendByte(WriteBuffCommand
); 
 332                 /* Load the PDI pointer register with the start address we want to write to */ 
 333                 XPROGTarget_SendByte(PDI_CMD_ST 
| (PDI_POINTER_DIRECT 
<< 2) | PDI_DATSIZE_4BYTES
); 
 334                 XMEGANVM_SendAddress(WriteAddress
); 
 336                 /* Send the REPEAT command with the specified number of bytes to write */ 
 337                 XPROGTarget_SendByte(PDI_CMD_REPEAT 
| PDI_DATSIZE_1BYTE
); 
 338                 XPROGTarget_SendByte(WriteSize 
- 1); 
 340                 /* Send a ST command with indirect access and post-increment to write the bytes */ 
 341                 XPROGTarget_SendByte(PDI_CMD_ST 
| (PDI_POINTER_INDIRECT_PI 
<< 2) | PDI_DATSIZE_1BYTE
); 
 343                   XPROGTarget_SendByte(*(WriteBuffer
++)); 
 346         if (PageMode 
& XPRG_PAGEMODE_WRITE
) 
 348                 /* Wait until the NVM controller is no longer busy */ 
 349                 if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 352                 /* Send the memory write command to the target */ 
 353                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 354                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 355                 XPROGTarget_SendByte(WritePageCommand
); 
 357                 /* Send the address of the first page location to write the memory page */ 
 358                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 359                 XMEGANVM_SendAddress(WriteAddress
); 
 360                 XPROGTarget_SendByte(0x00); 
 366 /** Erases a specific memory space of the target. 
 368  *  \param[in] EraseCommand  NVM erase command to send to the device 
 369  *  \param[in] Address       Address inside the memory space to erase 
 371  *  \return Boolean \c true if the command sequence complete successfully 
 373 bool XMEGANVM_EraseMemory(const uint8_t EraseCommand
, 
 374                           const uint32_t Address
) 
 376         /* Wait until the NVM controller is no longer busy */ 
 377         if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 380         /* EEPROM and Chip erasures are triggered differently to FLASH section erasures */ 
 381         if (EraseCommand 
== XMEGA_NVM_CMD_CHIPERASE
) 
 383                 /* Send the memory erase command to the target */ 
 384                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 385                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 386                 XPROGTarget_SendByte(EraseCommand
); 
 388                 /* Set CMDEX bit in NVM CTRLA register to start the erase sequence */ 
 389                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 390                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
); 
 391                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX
); 
 393         else if (EraseCommand 
== XMEGA_NVM_CMD_ERASEEEPROM
) 
 395                 /* Send the EEPROM page buffer erase command to the target */ 
 396                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 397                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 398                 XPROGTarget_SendByte(XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF
); 
 400                 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */ 
 401                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 402                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
); 
 403                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX
); 
 405                 /* Wait until the NVM controller is no longer busy */ 
 406                 if (!(XMEGANVM_WaitWhileNVMControllerBusy())) 
 409                 /* Send the EEPROM memory buffer write command to the target */ 
 410                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 411                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 412                 XPROGTarget_SendByte(XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF
); 
 414                 /* Load the PDI pointer register with the EEPROM page start address */ 
 415                 XPROGTarget_SendByte(PDI_CMD_ST 
| (PDI_POINTER_DIRECT 
<< 2) | PDI_DATSIZE_4BYTES
); 
 416                 XMEGANVM_SendAddress(Address
); 
 418                 /* Send the REPEAT command with the specified number of bytes to write */ 
 419                 XPROGTarget_SendByte(PDI_CMD_REPEAT 
| PDI_DATSIZE_1BYTE
); 
 420                 XPROGTarget_SendByte(XPROG_Param_EEPageSize 
- 1); 
 422                 /* Send a ST command with indirect access and post-increment to tag each byte in the EEPROM page buffer */ 
 423                 XPROGTarget_SendByte(PDI_CMD_ST 
| (PDI_POINTER_INDIRECT_PI 
<< 2) | PDI_DATSIZE_1BYTE
); 
 424                 for (uint8_t PageByte 
= 0; PageByte 
< XPROG_Param_EEPageSize
; PageByte
++) 
 425                   XPROGTarget_SendByte(0x00); 
 427                 /* Send the memory erase command to the target */ 
 428                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 429                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 430                 XPROGTarget_SendByte(EraseCommand
); 
 432                 /* Set CMDEX bit in NVM CTRLA register to start the EEPROM erase sequence */ 
 433                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 434                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
); 
 435                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX
); 
 439                 /* Send the memory erase command to the target */ 
 440                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 441                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
); 
 442                 XPROGTarget_SendByte(EraseCommand
); 
 444                 /* Other erase modes just need us to address a byte within the target memory space */ 
 445                 XPROGTarget_SendByte(PDI_CMD_STS 
| (PDI_DATSIZE_4BYTES 
<< 2)); 
 446                 XMEGANVM_SendAddress(Address
); 
 447                 XPROGTarget_SendByte(0x00); 
 450         /* Wait until the NVM bus is ready again */ 
 451         if (!(XMEGANVM_WaitWhileNVMBusBusy()))