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 (!(TimeoutMSRemaining
))
86 /* Check the status register read response to see if the NVM bus is enabled */
87 if (StatusRegister
& PDI_STATUS_NVM
)
89 TimeoutMSRemaining
= COMMAND_TIMEOUT_MS
;
95 /** Waits while the target's NVM controller is busy performing an operation, exiting if the
96 * timeout period expires.
98 * \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
100 bool XMEGANVM_WaitWhileNVMControllerBusy(void)
102 /* Poll the NVM STATUS register while the NVM controller is busy */
105 /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */
106 XPROGTarget_SendByte(PDI_CMD_LDS
| (PDI_DATSIZE_4BYTES
<< 2));
107 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS
);
109 uint8_t StatusRegister
= XPROGTarget_ReceiveByte();
111 /* We might have timed out waiting for the status register read response, check here */
112 if (!(TimeoutMSRemaining
))
115 /* Check to see if the BUSY flag is still set */
116 if (!(StatusRegister
& (1 << 7)))
118 TimeoutMSRemaining
= COMMAND_TIMEOUT_MS
;
124 /** Retrieves the CRC value of the given memory space.
126 * \param[in] CRCCommand NVM CRC command to issue to the target
127 * \param[out] CRCDest CRC Destination when read from the target
129 * \return Boolean true if the command sequence complete successfully
131 bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand
, uint32_t* const CRCDest
)
133 /* Wait until the NVM controller is no longer busy */
134 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
137 /* Set the NVM command to the correct CRC read command */
138 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
139 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
140 XPROGTarget_SendByte(CRCCommand
);
142 /* Set CMDEX bit in NVM CTRLA register to start the CRC generation */
143 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
144 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
145 XPROGTarget_SendByte(1 << 0);
147 /* Wait until the NVM bus is ready again */
148 if (!(XMEGANVM_WaitWhileNVMBusBusy()))
151 /* Wait until the NVM controller is no longer busy */
152 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
155 /* Load the PDI pointer register with the DAT0 register start address */
156 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
157 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0
);
159 /* Send the REPEAT command to grab the CRC bytes */
160 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
161 XPROGTarget_SendByte(XMEGA_CRC_LENGTH
- 1);
163 /* Read in the CRC bytes from the target */
164 XPROGTarget_SendByte(PDI_CMD_LD
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
165 for (uint8_t i
= 0; i
< XMEGA_CRC_LENGTH
; i
++)
166 ((uint8_t*)CRCDest
)[i
] = XPROGTarget_ReceiveByte();
168 return (TimeoutMSRemaining
!= 0);
171 /** Reads memory from the target's memory spaces.
173 * \param[in] ReadAddress Start address to read from within the target's address space
174 * \param[out] ReadBuffer Buffer to store read data into
175 * \param[in] ReadSize Number of bytes to read
177 * \return Boolean true if the command sequence complete successfully
179 bool XMEGANVM_ReadMemory(const uint32_t ReadAddress
, uint8_t* ReadBuffer
, uint16_t ReadSize
)
181 /* Wait until the NVM controller is no longer busy */
182 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
185 /* Send the READNVM command to the NVM controller for reading of an arbitrary location */
186 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
187 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
188 XPROGTarget_SendByte(XMEGA_NVM_CMD_READNVM
);
190 /* Load the PDI pointer register with the start address we want to read from */
191 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
192 XMEGANVM_SendAddress(ReadAddress
);
194 /* Send the REPEAT command with the specified number of bytes to read */
195 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
196 XPROGTarget_SendByte(ReadSize
- 1);
198 /* Send a LD command with indirect access and post-increment to read out the bytes */
199 XPROGTarget_SendByte(PDI_CMD_LD
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
200 while (ReadSize
-- && TimeoutMSRemaining
)
201 *(ReadBuffer
++) = XPROGTarget_ReceiveByte();
203 return (TimeoutMSRemaining
!= 0);
206 /** Writes byte addressed memory to the target's memory spaces.
208 * \param[in] WriteCommand Command to send to the device to write each memory byte
209 * \param[in] WriteAddress Address to write to within the target's address space
210 * \param[in] Byte Byte to write to the target
212 * \return Boolean true if the command sequence complete successfully
214 bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand
, const uint32_t WriteAddress
, const uint8_t Byte
)
216 /* Wait until the NVM controller is no longer busy */
217 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
220 /* Send the memory write command to the target */
221 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
222 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
223 XPROGTarget_SendByte(WriteCommand
);
225 /* Send new memory byte to the memory to the target */
226 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
227 XMEGANVM_SendAddress(WriteAddress
);
228 XPROGTarget_SendByte(Byte
);
233 /** Writes page addressed memory to the target's memory spaces.
235 * \param[in] WriteBuffCommand Command to send to the device to write a byte to the memory page buffer
236 * \param[in] EraseBuffCommand Command to send to the device to erase the memory page buffer
237 * \param[in] WritePageCommand Command to send to the device to write the page buffer to the destination memory
238 * \param[in] PageMode Bitfield indicating what operations need to be executed on the specified page
239 * \param[in] WriteAddress Start address to write the page data to within the target's address space
240 * \param[in] WriteBuffer Buffer to source data from
241 * \param[in] WriteSize Number of bytes to write
243 * \return Boolean true if the command sequence complete successfully
245 bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand
, const uint8_t EraseBuffCommand
,
246 const uint8_t WritePageCommand
, const uint8_t PageMode
, const uint32_t WriteAddress
,
247 const uint8_t* WriteBuffer
, uint16_t WriteSize
)
249 if (PageMode
& XPRG_PAGEMODE_ERASE
)
251 /* Wait until the NVM controller is no longer busy */
252 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
255 /* Send the memory buffer erase command to the target */
256 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
257 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
258 XPROGTarget_SendByte(EraseBuffCommand
);
260 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
261 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
262 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
263 XPROGTarget_SendByte(1 << 0);
268 /* Wait until the NVM controller is no longer busy */
269 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
272 /* Send the memory buffer write command to the target */
273 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
274 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
275 XPROGTarget_SendByte(WriteBuffCommand
);
277 /* Load the PDI pointer register with the start address we want to write to */
278 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
279 XMEGANVM_SendAddress(WriteAddress
);
281 /* Send the REPEAT command with the specified number of bytes to write */
282 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
283 XPROGTarget_SendByte(WriteSize
- 1);
285 /* Send a ST command with indirect access and post-increment to write the bytes */
286 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
288 XPROGTarget_SendByte(*(WriteBuffer
++));
291 if (PageMode
& XPRG_PAGEMODE_WRITE
)
293 /* Wait until the NVM controller is no longer busy */
294 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
297 /* Send the memory write command to the target */
298 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
299 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
300 XPROGTarget_SendByte(WritePageCommand
);
302 /* Send the address of the first page location to write the memory page */
303 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
304 XMEGANVM_SendAddress(WriteAddress
);
305 XPROGTarget_SendByte(0x00);
311 /** Erases a specific memory space of the target.
313 * \param[in] EraseCommand NVM erase command to send to the device
314 * \param[in] Address Address inside the memory space to erase
316 * \return Boolean true if the command sequence complete successfully
318 bool XMEGANVM_EraseMemory(const uint8_t EraseCommand
, const uint32_t Address
)
320 /* Wait until the NVM controller is no longer busy */
321 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
324 /* EEPROM and Chip erasures are triggered differently to FLASH section erasures */
325 if (EraseCommand
== XMEGA_NVM_CMD_CHIPERASE
)
327 /* Send the memory erase command to the target */
328 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
329 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
330 XPROGTarget_SendByte(EraseCommand
);
332 /* Set CMDEX bit in NVM CTRLA register to start the erase sequence */
333 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
334 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
335 XPROGTarget_SendByte(1 << 0);
337 else if (EraseCommand
== XMEGA_NVM_CMD_ERASEEEPROM
)
339 /* Send the EEPROM page buffer erase command to the target */
340 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
341 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
342 XPROGTarget_SendByte(XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF
);
344 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
345 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
346 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
347 XPROGTarget_SendByte(1 << 0);
349 /* Wait until the NVM controller is no longer busy */
350 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
353 /* Send the EEPROM memory buffer write command to the target */
354 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
355 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
356 XPROGTarget_SendByte(XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF
);
358 /* Load the PDI pointer register with the EEPROM page start address */
359 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
360 XMEGANVM_SendAddress(Address
);
362 /* Send the REPEAT command with the specified number of bytes to write */
363 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
364 XPROGTarget_SendByte(XPROG_Param_EEPageSize
- 1);
366 /* Send a ST command with indirect access and post-increment to tag each byte in the EEPROM page buffer */
367 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
368 for (uint8_t PageByte
= 0; PageByte
< XPROG_Param_EEPageSize
; PageByte
++)
369 XPROGTarget_SendByte(0x00);
371 /* Send the memory erase command to the target */
372 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
373 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
374 XPROGTarget_SendByte(EraseCommand
);
376 /* Set CMDEX bit in NVM CTRLA register to start the EEPROM erase sequence */
377 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
378 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
379 XPROGTarget_SendByte(1 << 0);
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 /* Other erase modes just need us to address a byte within the target memory space */
389 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
390 XMEGANVM_SendAddress(Address
);
391 XPROGTarget_SendByte(0x00);
394 /* Wait until the NVM bus is ready again */
395 if (!(XMEGANVM_WaitWhileNVMBusBusy()))