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 */
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 /* Load the PDI pointer register with the DAT0 register start address */
140 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
141 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0
);
143 /* Send the REPEAT command to grab the CRC bytes */
144 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
145 XPROGTarget_SendByte(XMEGA_CRC_LENGTH
- 1);
147 /* Read in the CRC bytes from the target */
148 XPROGTarget_SendByte(PDI_CMD_LD
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
149 for (uint8_t i
= 0; i
< XMEGA_CRC_LENGTH
; i
++)
150 ((uint8_t*)CRCDest
)[i
] = XPROGTarget_ReceiveByte();
155 /** Reads memory from the target's memory spaces.
157 * \param[in] ReadAddress Start address to read from within the target's address space
158 * \param[out] ReadBuffer Buffer to store read data into
159 * \param[in] ReadSize Number of bytes to read
161 * \return Boolean true if the command sequence complete successfully
163 bool XMEGANVM_ReadMemory(const uint32_t ReadAddress
, uint8_t* ReadBuffer
, uint16_t ReadSize
)
165 /* Wait until the NVM controller is no longer busy */
166 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
169 /* Send the READNVM command to the NVM controller for reading of an arbitrary location */
170 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
171 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
172 XPROGTarget_SendByte(XMEGA_NVM_CMD_READNVM
);
174 /* Load the PDI pointer register with the start address we want to read from */
175 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
176 XMEGANVM_SendAddress(ReadAddress
);
178 /* Send the REPEAT command with the specified number of bytes to read */
179 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
180 XPROGTarget_SendByte(ReadSize
- 1);
182 /* Send a LD command with indirect access and postincrement to read out the bytes */
183 XPROGTarget_SendByte(PDI_CMD_LD
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
185 *(ReadBuffer
++) = XPROGTarget_ReceiveByte();
190 /** Writes byte addressed memory to the target's memory spaces.
192 * \param[in] WriteCommand Command to send to the device to write each memory byte
193 * \param[in] WriteAddress Address to write to within the target's address space
194 * \param[in] Byte Byte to write to the target
196 * \return Boolean true if the command sequence complete successfully
198 bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand
, const uint32_t WriteAddress
, const uint8_t Byte
)
200 /* Wait until the NVM controller is no longer busy */
201 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
204 /* Send the memory write command to the target */
205 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
206 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
207 XPROGTarget_SendByte(WriteCommand
);
209 /* Send new memory byte to the memory to the target */
210 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
211 XMEGANVM_SendAddress(WriteAddress
);
212 XPROGTarget_SendByte(Byte
);
217 /** Writes page addressed memory to the target's memory spaces.
219 * \param[in] WriteBuffCommand Command to send to the device to write a byte to the memory page buffer
220 * \param[in] EraseBuffCommand Command to send to the device to erase the memory page buffer
221 * \param[in] WritePageCommand Command to send to the device to write the page buffer to the destination memory
222 * \param[in] PageMode Bitfield indicating what operations need to be executed on the specified page
223 * \param[in] WriteAddress Start address to write the page data to within the target's address space
224 * \param[in] WriteBuffer Buffer to source data from
225 * \param[in] WriteSize Number of bytes to write
227 * \return Boolean true if the command sequence complete successfully
229 bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand
, const uint8_t EraseBuffCommand
,
230 const uint8_t WritePageCommand
, const uint8_t PageMode
, const uint32_t WriteAddress
,
231 const uint8_t* WriteBuffer
, uint16_t WriteSize
)
233 if (PageMode
& XPRG_PAGEMODE_ERASE
)
235 /* Wait until the NVM controller is no longer busy */
236 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
239 /* Send the memory buffer erase command to the target */
240 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
241 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
242 XPROGTarget_SendByte(EraseBuffCommand
);
244 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
245 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
246 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
247 XPROGTarget_SendByte(1 << 0);
252 /* Wait until the NVM controller is no longer busy */
253 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
256 /* Send the memory buffer write command to the target */
257 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
258 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
259 XPROGTarget_SendByte(WriteBuffCommand
);
261 /* Load the PDI pointer register with the start address we want to write to */
262 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_DIRECT
<< 2) | PDI_DATSIZE_4BYTES
);
263 XMEGANVM_SendAddress(WriteAddress
);
265 /* Send the REPEAT command with the specified number of bytes to write */
266 XPROGTarget_SendByte(PDI_CMD_REPEAT
| PDI_DATSIZE_1BYTE
);
267 XPROGTarget_SendByte(WriteSize
- 1);
269 /* Send a ST command with indirect access and postincrement to write the bytes */
270 XPROGTarget_SendByte(PDI_CMD_ST
| (PDI_POINTER_INDIRECT_PI
<< 2) | PDI_DATSIZE_1BYTE
);
272 XPROGTarget_SendByte(*(WriteBuffer
++));
275 if (PageMode
& XPRG_PAGEMODE_WRITE
)
277 /* Wait until the NVM controller is no longer busy */
278 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
281 /* Send the memory write command to the target */
282 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
283 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
284 XPROGTarget_SendByte(WritePageCommand
);
286 /* Send the address of the first page location to write the memory page */
287 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
288 XMEGANVM_SendAddress(WriteAddress
);
289 XPROGTarget_SendByte(0x00);
295 /** Erases a specific memory space of the target.
297 * \param[in] EraseCommand NVM erase command to send to the device
298 * \param[in] Address Address inside the memory space to erase
300 * \return Boolean true if the command sequence complete successfully
302 bool XMEGANVM_EraseMemory(const uint8_t EraseCommand
, const uint32_t Address
)
304 /* Wait until the NVM controller is no longer busy */
305 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
308 /* Send the memory erase command to the target */
309 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
310 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD
);
311 XPROGTarget_SendByte(EraseCommand
);
313 /* Chip erase is handled separately, since it's procedure is different to other erase types */
314 if (EraseCommand
== XMEGA_NVM_CMD_CHIPERASE
)
316 /* Set CMDEX bit in NVM CTRLA register to start the chip erase */
317 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
318 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA
);
319 XPROGTarget_SendByte(1 << 0);
323 /* Other erase modes just need us to address a byte within the target memory space */
324 XPROGTarget_SendByte(PDI_CMD_STS
| (PDI_DATSIZE_4BYTES
<< 2));
325 XMEGANVM_SendAddress(Address
);
326 XPROGTarget_SendByte(0x00);
329 /* Wait until the NVM bus is ready again */
330 if (!(XMEGANVM_WaitWhileNVMBusBusy()))