3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 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 disclaim 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(((uint8_t*)&AbsoluteAddress
)[0]);
49 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress
)[1]);
50 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress
)[2]);
51 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress
)[3]);
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 true if the NVM controller became ready within the timeout period, 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 true if the NVM controller became ready within the timeout period, false otherwise
97 bool XMEGANVM_WaitWhileNVMControllerBusy(void)
99 /* Poll the NVM STATUS register while the NVM controller is busy */
102 /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */
103 XPROGTarget_SendByte(PDI_CMD_LDS
| (PDI_DATSIZE_4BYTES
<< 2));
104 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS
);
106 uint8_t StatusRegister
= XPROGTarget_ReceiveByte();
108 /* We might have timed out waiting for the status register read response, check here */
109 if (!(TimeoutTicksRemaining
))
112 /* Check to see if the BUSY flag is still set */
113 if (!(StatusRegister
& (1 << 7)))
118 /** Retrieves the CRC value of the given memory space.
120 * \param[in] CRCCommand NVM CRC command to issue to the target
121 * \param[out] CRCDest CRC Destination when read from the target
123 * \return Boolean true if the command sequence complete successfully
125 bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand
, uint32_t* const CRCDest
)
127 /* Wait until the NVM controller is no longer busy */
128 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
131 /* Set the NVM command to the correct CRC read command */
132 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
133 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
134 XPROGTarget_SendByte(CRCCommand
);
136 /* Set CMDEX bit in NVM CTRLA register to start the CRC generation */
137 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
138 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
139 XPROGTarget_SendByte(1 << 0);
141 /* Wait until the NVM bus is ready again */
142 if (!(XMEGANVM_WaitWhileNVMBusBusy()))
145 /* Wait until the NVM controller is no longer busy */
146 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
149 /* Load the PDI pointer register with the DAT0 register start address */
150 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
151 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0
);
153 /* Send the REPEAT command to grab the CRC bytes */
154 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
155 XPROGTarget_SendByte(XMEGA_CRC_LENGTH
- 1);
157 /* Read in the CRC bytes from the target */
158 XPROGTarget_SendByte(PDI_CMD_LD
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
159 for (uint8_t i
= 0; i
< XMEGA_CRC_LENGTH
; i
++)
160 ((uint8_t*)CRCDest
)[i
] = XPROGTarget_ReceiveByte();
162 return (TimeoutTicksRemaining
!= 0);
165 /** Reads memory from the target's memory spaces.
167 * \param[in] ReadAddress Start address to read from within the target's address space
168 * \param[out] ReadBuffer Buffer to store read data into
169 * \param[in] ReadSize Number of bytes to read
171 * \return Boolean true if the command sequence complete successfully
173 bool XMEGANVM_ReadMemory(const uint32_t ReadAddress
, uint8_t* ReadBuffer
, uint16_t ReadSize
)
175 /* Wait until the NVM controller is no longer busy */
176 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
179 /* Send the READNVM command to the NVM controller for reading of an arbitrary location */
180 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
181 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
182 XPROGTarget_SendByte(XMEGA_NVM_CMD_READNVM
);
184 /* Load the PDI pointer register with the start address we want to read from */
185 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
186 XMEGANVM_SendAddress(ReadAddress
);
188 /* Send the REPEAT command with the specified number of bytes to read */
189 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
190 XPROGTarget_SendByte(ReadSize
- 1);
192 /* Send a LD command with indirect access and post-increment to read out the bytes */
193 XPROGTarget_SendByte(PDI_CMD_LD
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
194 while (ReadSize
-- && TimeoutTicksRemaining
)
195 *(ReadBuffer
++) = XPROGTarget_ReceiveByte();
197 return (TimeoutTicksRemaining
!= 0);
200 /** Writes byte addressed memory to the target's memory spaces.
202 * \param[in] WriteCommand Command to send to the device to write each memory byte
203 * \param[in] WriteAddress Address to write to within the target's address space
204 * \param[in] Byte Byte to write to the target
206 * \return Boolean true if the command sequence complete successfully
208 bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand
, const uint32_t WriteAddress
, const uint8_t Byte
)
210 /* Wait until the NVM controller is no longer busy */
211 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
214 /* Send the memory write command to the target */
215 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
216 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
217 XPROGTarget_SendByte(WriteCommand
);
219 /* Send new memory byte to the memory to the target */
220 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
221 XMEGANVM_SendAddress(WriteAddress
);
222 XPROGTarget_SendByte(Byte
);
227 /** Writes page addressed memory to the target's memory spaces.
229 * \param[in] WriteBuffCommand Command to send to the device to write a byte to the memory page buffer
230 * \param[in] EraseBuffCommand Command to send to the device to erase the memory page buffer
231 * \param[in] WritePageCommand Command to send to the device to write the page buffer to the destination memory
232 * \param[in] PageMode Bitfield indicating what operations need to be executed on the specified page
233 * \param[in] WriteAddress Start address to write the page data to within the target's address space
234 * \param[in] WriteBuffer Buffer to source data from
235 * \param[in] WriteSize Number of bytes to write
237 * \return Boolean true if the command sequence complete successfully
239 bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand
, const uint8_t EraseBuffCommand
,
240 const uint8_t WritePageCommand
, const uint8_t PageMode
, const uint32_t WriteAddress
,
241 const uint8_t* WriteBuffer
, uint16_t WriteSize
)
243 if (PageMode
& XPRG_PAGEMODE_ERASE
)
245 /* Wait until the NVM controller is no longer busy */
246 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
249 /* Send the memory buffer erase command to the target */
250 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
251 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
252 XPROGTarget_SendByte(EraseBuffCommand
);
254 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
255 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
256 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
257 XPROGTarget_SendByte(1 << 0);
262 /* Wait until the NVM controller is no longer busy */
263 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
266 /* Send the memory buffer write command to the target */
267 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
268 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
269 XPROGTarget_SendByte(WriteBuffCommand
);
271 /* Load the PDI pointer register with the start address we want to write to */
272 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
273 XMEGANVM_SendAddress(WriteAddress
);
275 /* Send the REPEAT command with the specified number of bytes to write */
276 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
277 XPROGTarget_SendByte(WriteSize
- 1);
279 /* Send a ST command with indirect access and post-increment to write the bytes */
280 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
282 XPROGTarget_SendByte(*(WriteBuffer
++));
285 if (PageMode
& XPRG_PAGEMODE_WRITE
)
287 /* Wait until the NVM controller is no longer busy */
288 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
291 /* Send the memory write command to the target */
292 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
293 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
294 XPROGTarget_SendByte(WritePageCommand
);
296 /* Send the address of the first page location to write the memory page */
297 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
298 XMEGANVM_SendAddress(WriteAddress
);
299 XPROGTarget_SendByte(0x00);
305 /** Erases a specific memory space of the target.
307 * \param[in] EraseCommand NVM erase command to send to the device
308 * \param[in] Address Address inside the memory space to erase
310 * \return Boolean true if the command sequence complete successfully
312 bool XMEGANVM_EraseMemory(const uint8_t EraseCommand
, const uint32_t Address
)
314 /* Wait until the NVM controller is no longer busy */
315 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
318 /* EEPROM and Chip erasures are triggered differently to FLASH section erasures */
319 if (EraseCommand
== XMEGA_NVM_CMD_CHIPERASE
)
321 /* Send the memory erase command to the target */
322 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
323 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
324 XPROGTarget_SendByte(EraseCommand
);
326 /* Set CMDEX bit in NVM CTRLA register to start the erase sequence */
327 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
328 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
329 XPROGTarget_SendByte(1 << 0);
331 else if (EraseCommand
== XMEGA_NVM_CMD_ERASEEEPROM
)
333 /* Send the EEPROM page buffer erase command to the target */
334 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
335 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
336 XPROGTarget_SendByte(XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF
);
338 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
339 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
340 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
341 XPROGTarget_SendByte(1 << 0);
343 /* Wait until the NVM controller is no longer busy */
344 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
347 /* Send the EEPROM memory buffer write command to the target */
348 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
349 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
350 XPROGTarget_SendByte(XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF
);
352 /* Load the PDI pointer register with the EEPROM page start address */
353 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
354 XMEGANVM_SendAddress(Address
);
356 /* Send the REPEAT command with the specified number of bytes to write */
357 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
358 XPROGTarget_SendByte(XPROG_Param_EEPageSize
- 1);
360 /* Send a ST command with indirect access and post-increment to tag each byte in the EEPROM page buffer */
361 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
362 for (uint8_t PageByte
= 0; PageByte
< XPROG_Param_EEPageSize
; PageByte
++)
363 XPROGTarget_SendByte(0x00);
365 /* Send the memory erase command to the target */
366 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
367 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
368 XPROGTarget_SendByte(EraseCommand
);
370 /* Set CMDEX bit in NVM CTRLA register to start the EEPROM erase sequence */
371 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
372 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
373 XPROGTarget_SendByte(1 << 0);
377 /* Send the memory erase command to the target */
378 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
379 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
380 XPROGTarget_SendByte(EraseCommand
);
382 /* Other erase modes just need us to address a byte within the target memory space */
383 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
384 XMEGANVM_SendAddress(Address
);
385 XPROGTarget_SendByte(0x00);
388 /* Wait until the NVM bus is ready again */
389 if (!(XMEGANVM_WaitWhileNVMBusBusy()))