60e755265cfc47a65411602a117bf69db3451e15
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XMEGANVM.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * Target-related functions for the XMEGA target's NVM module.
34 */
35
36 #define INCLUDE_FROM_XMEGA_NVM_C
37 #include "XMEGANVM.h"
38
39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
40
41 /** Sends the given 32-bit absolute address to the target.
42 *
43 * \param[in] AbsoluteAddress Absolute address to send to the target
44 */
45 static void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress)
46 {
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]);
52 }
53
54 /** Sends the given NVM register address to the target.
55 *
56 * \param[in] Register NVM register whose absolute address is to be sent
57 */
58 static void XMEGANVM_SendNVMRegAddress(const uint8_t Register)
59 {
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;
62
63 /* Send the calculated 32-bit address to the target, LSB first */
64 XMEGANVM_SendAddress(Address);
65 }
66
67 /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read or CRC
68 * calculation.
69 *
70 * \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
71 */
72 bool XMEGANVM_WaitWhileNVMBusBusy(void)
73 {
74 /* Poll the STATUS register to check to see if NVM access has been enabled */
75 for (;;)
76 {
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
80 uint8_t StatusRegister = XPROGTarget_ReceiveByte();
81
82 /* We might have timed out waiting for the status register read response, check here */
83 if (!(TimeoutTicksRemaining))
84 return false;
85
86 /* Check the status register read response to see if the NVM bus is enabled */
87 if (StatusRegister & PDI_STATUS_NVM)
88 return true;
89 }
90 }
91
92 /** Waits while the target's NVM controller is busy performing an operation, exiting if the
93 * timeout period expires.
94 *
95 * \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
96 */
97 bool XMEGANVM_WaitWhileNVMControllerBusy(void)
98 {
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);
102
103 /* Poll the NVM STATUS register while the NVM controller is busy */
104 for (;;)
105 {
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);
108 uint8_t StatusRegister = XPROGTarget_ReceiveByte();
109
110 /* We might have timed out waiting for the status register read response, check here */
111 if (!(TimeoutTicksRemaining))
112 return false;
113
114 /* Check to see if the BUSY flag is still set */
115 if (!(StatusRegister & (1 << 7)))
116 return true;
117 }
118 }
119
120 /** Retrieves the CRC value of the given memory space.
121 *
122 * \param[in] CRCCommand NVM CRC command to issue to the target
123 * \param[out] CRCDest CRC Destination when read from the target
124 *
125 * \return Boolean true if the command sequence complete successfully
126 */
127 bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand, uint32_t* const CRCDest)
128 {
129 /* Wait until the NVM controller is no longer busy */
130 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
131 return false;
132
133 /* Set the NVM command to the correct CRC read command */
134 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
135 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
136 XPROGTarget_SendByte(CRCCommand);
137
138 /* Set CMDEX bit in NVM CTRLA register to start the CRC generation */
139 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
140 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
141 XPROGTarget_SendByte(1 << 0);
142
143 /* Wait until the NVM bus is ready again */
144 if (!(XMEGANVM_WaitWhileNVMBusBusy()))
145 return false;
146
147 /* Wait until the NVM controller is no longer busy */
148 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
149 return false;
150
151 /* Load the PDI pointer register with the DAT0 register start address */
152 XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_DIRECT << 2) | PDI_DATSIZE_4BYTES);
153 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0);
154
155 /* Send the REPEAT command to grab the CRC bytes */
156 XPROGTarget_SendByte(PDI_CMD_REPEAT | PDI_DATSIZE_1BYTE);
157 XPROGTarget_SendByte(XMEGA_CRC_LENGTH - 1);
158
159 /* Read in the CRC bytes from the target */
160 XPROGTarget_SendByte(PDI_CMD_LD | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE);
161 for (uint8_t i = 0; i < XMEGA_CRC_LENGTH; i++)
162 ((uint8_t*)CRCDest)[i] = XPROGTarget_ReceiveByte();
163
164 return (TimeoutTicksRemaining != 0);
165 }
166
167 /** Reads memory from the target's memory spaces.
168 *
169 * \param[in] ReadAddress Start address to read from within the target's address space
170 * \param[out] ReadBuffer Buffer to store read data into
171 * \param[in] ReadSize Number of bytes to read
172 *
173 * \return Boolean true if the command sequence complete successfully
174 */
175 bool XMEGANVM_ReadMemory(const uint32_t ReadAddress, uint8_t* ReadBuffer, uint16_t ReadSize)
176 {
177 /* Wait until the NVM controller is no longer busy */
178 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
179 return false;
180
181 /* Send the READNVM command to the NVM controller for reading of an arbitrary location */
182 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
183 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
184 XPROGTarget_SendByte(XMEGA_NVM_CMD_READNVM);
185
186 /* Load the PDI pointer register with the start address we want to read from */
187 XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_DIRECT << 2) | PDI_DATSIZE_4BYTES);
188 XMEGANVM_SendAddress(ReadAddress);
189
190 /* Send the REPEAT command with the specified number of bytes to read */
191 XPROGTarget_SendByte(PDI_CMD_REPEAT | PDI_DATSIZE_1BYTE);
192 XPROGTarget_SendByte(ReadSize - 1);
193
194 /* Send a LD command with indirect access and post-increment to read out the bytes */
195 XPROGTarget_SendByte(PDI_CMD_LD | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE);
196 while (ReadSize-- && TimeoutTicksRemaining)
197 *(ReadBuffer++) = XPROGTarget_ReceiveByte();
198
199 return (TimeoutTicksRemaining != 0);
200 }
201
202 /** Writes byte addressed memory to the target's memory spaces.
203 *
204 * \param[in] WriteCommand Command to send to the device to write each memory byte
205 * \param[in] WriteAddress Address to write to within the target's address space
206 * \param[in] Byte Byte to write to the target
207 *
208 * \return Boolean true if the command sequence complete successfully
209 */
210 bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand, const uint32_t WriteAddress, const uint8_t Byte)
211 {
212 /* Wait until the NVM controller is no longer busy */
213 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
214 return false;
215
216 /* Send the memory write command to the target */
217 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
218 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
219 XPROGTarget_SendByte(WriteCommand);
220
221 /* Send new memory byte to the memory of the target */
222 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
223 XMEGANVM_SendAddress(WriteAddress);
224 XPROGTarget_SendByte(Byte);
225
226 return true;
227 }
228
229 /** Writes page addressed memory to the target's memory spaces.
230 *
231 * \param[in] WriteBuffCommand Command to send to the device to write a byte to the memory page buffer
232 * \param[in] EraseBuffCommand Command to send to the device to erase the memory page buffer
233 * \param[in] WritePageCommand Command to send to the device to write the page buffer to the destination memory
234 * \param[in] PageMode Bitfield indicating what operations need to be executed on the specified page
235 * \param[in] WriteAddress Start address to write the page data to within the target's address space
236 * \param[in] WriteBuffer Buffer to source data from
237 * \param[in] WriteSize Number of bytes to write
238 *
239 * \return Boolean true if the command sequence complete successfully
240 */
241 bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand, const uint8_t EraseBuffCommand,
242 const uint8_t WritePageCommand, const uint8_t PageMode, const uint32_t WriteAddress,
243 const uint8_t* WriteBuffer, uint16_t WriteSize)
244 {
245 if (PageMode & XPRG_PAGEMODE_ERASE)
246 {
247 /* Wait until the NVM controller is no longer busy */
248 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
249 return false;
250
251 /* Send the memory buffer erase command to the target */
252 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
253 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
254 XPROGTarget_SendByte(EraseBuffCommand);
255
256 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
257 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
258 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
259 XPROGTarget_SendByte(1 << 0);
260 }
261
262 if (WriteSize)
263 {
264 /* Wait until the NVM controller is no longer busy */
265 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
266 return false;
267
268 /* Send the memory buffer write command to the target */
269 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
270 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
271 XPROGTarget_SendByte(WriteBuffCommand);
272
273 /* Load the PDI pointer register with the start address we want to write to */
274 XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_DIRECT << 2) | PDI_DATSIZE_4BYTES);
275 XMEGANVM_SendAddress(WriteAddress);
276
277 /* Send the REPEAT command with the specified number of bytes to write */
278 XPROGTarget_SendByte(PDI_CMD_REPEAT | PDI_DATSIZE_1BYTE);
279 XPROGTarget_SendByte(WriteSize - 1);
280
281 /* Send a ST command with indirect access and post-increment to write the bytes */
282 XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE);
283 while (WriteSize--)
284 XPROGTarget_SendByte(*(WriteBuffer++));
285 }
286
287 if (PageMode & XPRG_PAGEMODE_WRITE)
288 {
289 /* Wait until the NVM controller is no longer busy */
290 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
291 return false;
292
293 /* Send the memory write command to the target */
294 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
295 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
296 XPROGTarget_SendByte(WritePageCommand);
297
298 /* Send the address of the first page location to write the memory page */
299 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
300 XMEGANVM_SendAddress(WriteAddress);
301 XPROGTarget_SendByte(0x00);
302 }
303
304 return true;
305 }
306
307 /** Erases a specific memory space of the target.
308 *
309 * \param[in] EraseCommand NVM erase command to send to the device
310 * \param[in] Address Address inside the memory space to erase
311 *
312 * \return Boolean true if the command sequence complete successfully
313 */
314 bool XMEGANVM_EraseMemory(const uint8_t EraseCommand, const uint32_t Address)
315 {
316 /* Wait until the NVM controller is no longer busy */
317 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
318 return false;
319
320 /* EEPROM and Chip erasures are triggered differently to FLASH section erasures */
321 if (EraseCommand == XMEGA_NVM_CMD_CHIPERASE)
322 {
323 /* Send the memory erase command to the target */
324 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
325 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
326 XPROGTarget_SendByte(EraseCommand);
327
328 /* Set CMDEX bit in NVM CTRLA register to start the erase sequence */
329 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
330 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
331 XPROGTarget_SendByte(1 << 0);
332 }
333 else if (EraseCommand == XMEGA_NVM_CMD_ERASEEEPROM)
334 {
335 /* Send the EEPROM page buffer erase command to the target */
336 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
337 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
338 XPROGTarget_SendByte(XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF);
339
340 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
341 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
342 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
343 XPROGTarget_SendByte(1 << 0);
344
345 /* Wait until the NVM controller is no longer busy */
346 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
347 return false;
348
349 /* Send the EEPROM memory buffer write command to the target */
350 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
351 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
352 XPROGTarget_SendByte(XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF);
353
354 /* Load the PDI pointer register with the EEPROM page start address */
355 XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_DIRECT << 2) | PDI_DATSIZE_4BYTES);
356 XMEGANVM_SendAddress(Address);
357
358 /* Send the REPEAT command with the specified number of bytes to write */
359 XPROGTarget_SendByte(PDI_CMD_REPEAT | PDI_DATSIZE_1BYTE);
360 XPROGTarget_SendByte(XPROG_Param_EEPageSize - 1);
361
362 /* Send a ST command with indirect access and post-increment to tag each byte in the EEPROM page buffer */
363 XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE);
364 for (uint8_t PageByte = 0; PageByte < XPROG_Param_EEPageSize; PageByte++)
365 XPROGTarget_SendByte(0x00);
366
367 /* Send the memory erase command to the target */
368 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
369 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
370 XPROGTarget_SendByte(EraseCommand);
371
372 /* Set CMDEX bit in NVM CTRLA register to start the EEPROM erase sequence */
373 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
374 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
375 XPROGTarget_SendByte(1 << 0);
376 }
377 else
378 {
379 /* Send the memory erase command to the target */
380 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
381 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
382 XPROGTarget_SendByte(EraseCommand);
383
384 /* Other erase modes just need us to address a byte within the target memory space */
385 XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));
386 XMEGANVM_SendAddress(Address);
387 XPROGTarget_SendByte(0x00);
388 }
389
390 /* Wait until the NVM bus is ready again */
391 if (!(XMEGANVM_WaitWhileNVMBusBusy()))
392 return false;
393
394 return true;
395 }
396
397 #endif
398