3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 NVM register address to the target.
43 * \param[in] Register NVM register whose absolute address is to be sent
45 void XMEGANVM_SendNVMRegAddress(const uint8_t Register
)
47 /* Determine the absolute register address from the NVM base memory address and the NVM register address */
48 uint32_t Address
= XPROG_Param_NVMBase
| Register
;
50 /* Send the calculated 32-bit address to the target, LSB first */
51 XMEGANVM_SendAddress(Address
);
54 /** Sends the given 32-bit absolute address to the target.
56 * \param[in] AbsoluteAddress Absolute address to send to the target
58 void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress
)
60 /* Send the given 32-bit address to the target, LSB first */
61 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress
)[0]);
62 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress
)[1]);
63 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress
)[2]);
64 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress
)[3]);
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 */
75 while (TimeoutMSRemaining
)
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
);
79 if (XPROGTarget_ReceiveByte() & PDI_STATUS_NVM
)
86 /** Waits while the target's NVM controller is busy performing an operation, exiting if the
87 * timeout period expires.
89 * \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
91 bool XMEGANVM_WaitWhileNVMControllerBusy(void)
93 /* Poll the NVM STATUS register while the NVM controller is busy */
94 while (TimeoutMSRemaining
)
96 /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */
97 XPROGTarget_SendByte(PDI_CMD_LDS
| (PDI_DATSIZE_4BYTES
<< 2));
98 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS
);
100 /* Check to see if the BUSY flag is still set */
101 if (!(XPROGTarget_ReceiveByte() & (1 << 7)))
108 /** Retrieves the CRC value of the given memory space.
110 * \param[in] CRCCommand NVM CRC command to issue to the target
111 * \param[out] CRCDest CRC Destination when read from the target
113 * \return Boolean true if the command sequence complete successfully
115 bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand
, uint32_t* const CRCDest
)
117 /* Wait until the NVM controller is no longer busy */
118 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
121 /* Set the NVM command to the correct CRC read command */
122 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
123 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
124 XPROGTarget_SendByte(CRCCommand
);
126 /* Set CMDEX bit in NVM CTRLA register to start the CRC generation */
127 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
128 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
129 XPROGTarget_SendByte(1 << 0);
131 /* Wait until the NVM bus is ready again */
132 if (!(XMEGANVM_WaitWhileNVMBusBusy()))
135 /* Wait until the NVM controller is no longer busy */
136 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
139 uint32_t MemoryCRC
= 0;
141 /* Read the first generated CRC byte value */
142 XPROGTarget_SendByte(PDI_CMD_LDS
| (PDI_DATSIZE_4BYTES
<< 2));
143 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0
);
144 MemoryCRC
= XPROGTarget_ReceiveByte();
146 /* Read the second generated CRC byte value */
147 XPROGTarget_SendByte(PDI_CMD_LDS
| (PDI_DATSIZE_4BYTES
<< 2));
148 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT1
);
149 MemoryCRC
|= ((uint16_t)XPROGTarget_ReceiveByte() << 8);
151 /* Read the third generated CRC byte value */
152 XPROGTarget_SendByte(PDI_CMD_LDS
| (PDI_DATSIZE_4BYTES
<< 2));
153 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT2
);
154 MemoryCRC
|= ((uint32_t)XPROGTarget_ReceiveByte() << 16);
156 *CRCDest
= MemoryCRC
;
161 /** Reads memory from the target's memory spaces.
163 * \param[in] ReadAddress Start address to read from within the target's address space
164 * \param[out] ReadBuffer Buffer to store read data into
165 * \param[in] ReadSize Number of bytes to read
167 * \return Boolean true if the command sequence complete successfully
169 bool XMEGANVM_ReadMemory(const uint32_t ReadAddress
, uint8_t* ReadBuffer
, uint16_t ReadSize
)
171 /* Wait until the NVM controller is no longer busy */
172 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
175 /* Send the READNVM command to the NVM controller for reading of an arbitrary location */
176 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
177 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
178 XPROGTarget_SendByte(XMEGA_NVM_CMD_READNVM
);
180 /* Load the PDI pointer register with the start address we want to read from */
181 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
182 XMEGANVM_SendAddress(ReadAddress
);
184 /* Send the REPEAT command with the specified number of bytes to read */
185 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
186 XPROGTarget_SendByte(ReadSize
- 1);
188 /* Send a LD command with indirect access and postincrement to read out the bytes */
189 XPROGTarget_SendByte(PDI_CMD_LD
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
191 *(ReadBuffer
++) = XPROGTarget_ReceiveByte();
196 /** Writes byte addressed memory to the target's memory spaces.
198 * \param[in] WriteCommand Command to send to the device to write each memory byte
199 * \param[in] WriteAddress Start address to write to within the target's address space
200 * \param[in] WriteBuffer Buffer to source data from
202 * \return Boolean true if the command sequence complete successfully
204 bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand
, const uint32_t WriteAddress
, const uint8_t* WriteBuffer
)
206 /* Wait until the NVM controller is no longer busy */
207 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
210 /* Send the memory write command to the target */
211 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
212 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
213 XPROGTarget_SendByte(WriteCommand
);
215 /* Send new memory byte to the memory to the target */
216 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
217 XMEGANVM_SendAddress(WriteAddress
);
218 XPROGTarget_SendByte(*(WriteBuffer
++));
223 /** Writes page addressed memory to the target's memory spaces.
225 * \param[in] WriteBuffCommand Command to send to the device to write a byte to the memory page buffer
226 * \param[in] EraseBuffCommand Command to send to the device to erase the memory page buffer
227 * \param[in] WritePageCommand Command to send to the device to write the page buffer to the destination memory
228 * \param[in] PageMode Bitfield indicating what operations need to be executed on the specified page
229 * \param[in] WriteAddress Start address to write the page data to within the target's address space
230 * \param[in] WriteBuffer Buffer to source data from
231 * \param[in] WriteSize Number of bytes to write
233 * \return Boolean true if the command sequence complete successfully
235 bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand
, const uint8_t EraseBuffCommand
,
236 const uint8_t WritePageCommand
, const uint8_t PageMode
, const uint32_t WriteAddress
,
237 const uint8_t* WriteBuffer
, uint16_t WriteSize
)
239 if (PageMode
& XPRG_PAGEMODE_ERASE
)
241 /* Wait until the NVM controller is no longer busy */
242 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
245 /* Send the memory buffer erase command to the target */
246 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
247 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
248 XPROGTarget_SendByte(EraseBuffCommand
);
250 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
251 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
252 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
253 XPROGTarget_SendByte(1 << 0);
258 /* Wait until the NVM controller is no longer busy */
259 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
262 /* Send the memory buffer write command to the target */
263 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
264 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
265 XPROGTarget_SendByte(WriteBuffCommand
);
267 /* Load the PDI pointer register with the start address we want to write to */
268 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
269 XMEGANVM_SendAddress(WriteAddress
);
271 /* Send the REPEAT command with the specified number of bytes to write */
272 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
273 XPROGTarget_SendByte(WriteSize
- 1);
275 /* Send a ST command with indirect access and postincrement to write the bytes */
276 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
278 XPROGTarget_SendByte(*(WriteBuffer
++));
281 if (PageMode
& XPRG_PAGEMODE_WRITE
)
283 /* Wait until the NVM controller is no longer busy */
284 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
287 /* Send the memory write command to the target */
288 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
289 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
290 XPROGTarget_SendByte(WritePageCommand
);
292 /* Send the address of the first page location to write the memory page */
293 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
294 XMEGANVM_SendAddress(WriteAddress
);
295 XPROGTarget_SendByte(0x00);
301 /** Erases a specific memory space of the target.
303 * \param[in] EraseCommand NVM erase command to send to the device
304 * \param[in] Address Address inside the memory space to erase
306 * \return Boolean true if the command sequence complete successfully
308 bool XMEGANVM_EraseMemory(const uint8_t EraseCommand
, const uint32_t Address
)
310 /* Wait until the NVM controller is no longer busy */
311 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
314 /* Send the memory erase command to the target */
315 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
316 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
317 XPROGTarget_SendByte(EraseCommand
);
319 /* Chip erase is handled separately, since it's procedure is different to other erase types */
320 if (EraseCommand
== XMEGA_NVM_CMD_CHIPERASE
)
322 /* Set CMDEX bit in NVM CTRLA register to start the chip erase */
323 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
324 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
325 XPROGTarget_SendByte(1 << 0);
329 /* Other erase modes just need us to address a byte within the target memory space */
330 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
331 XMEGANVM_SendAddress(Address
);
332 XPROGTarget_SendByte(0x00);
335 /* Wait until the NVM bus is ready again */
336 if (!(XMEGANVM_WaitWhileNVMBusBusy()))