]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c
Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Projects / AVRISP-MKII / Lib / XPROG / XMEGANVM.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 disclaims 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(AbsoluteAddress &  0xFF);
49         XPROGTarget_SendByte(AbsoluteAddress >> 8);
50         XPROGTarget_SendByte(AbsoluteAddress >> 16);
51         XPROGTarget_SendByte(AbsoluteAddress >> 24);
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 \c true if the NVM controller became ready within the timeout period, \c 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_REG_STATUS));
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 \c true if the NVM controller became ready within the timeout period, \c 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, PDI_DATASIZE_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, PDI_DATASIZE_1BYTE));
108
109                 uint8_t StatusRegister = XPROGTarget_ReceiveByte();
110
111                 /* We might have timed out waiting for the status register read response, check here */
112                 if (!(TimeoutTicksRemaining))
113                   return false;
114
115                 /* Check to see if the BUSY flag is still set */
116                 if (!(StatusRegister & (1 << 7)))
117                   return true;
118         }
119 }
120
121 /** Enables the physical PDI interface on the target and enables access to the internal NVM controller.
122  *
123  *  \return Boolean \c true if the PDI interface was enabled successfully, \c false otherwise
124  */
125 bool XMEGANVM_EnablePDI(void)
126 {
127         /* Enable PDI programming mode with the attached target */
128         XPROGTarget_EnableTargetPDI();
129
130         /* Store the RESET key into the RESET PDI register to keep the XMEGA in reset */
131         XPROGTarget_SendByte(PDI_CMD_STCS(PDI_REG_RESET));
132         XPROGTarget_SendByte(PDI_RESET_KEY);
133
134         /* Lower direction change guard time to 32 USART bits */
135         XPROGTarget_SendByte(PDI_CMD_STCS(PDI_REG_CTRL));
136         XPROGTarget_SendByte(0x02);
137
138         /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */
139         XPROGTarget_SendByte(PDI_CMD_KEY);
140         for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--)
141           XPROGTarget_SendByte(PDI_NVMENABLE_KEY[i - 1]);
142
143         /* Wait until the NVM bus becomes active */
144         return XMEGANVM_WaitWhileNVMBusBusy();
145 }
146
147 /** Removes access to the target's NVM controller and physically disables the target's physical PDI interface. */
148 void XMEGANVM_DisablePDI(void)
149 {
150         XMEGANVM_WaitWhileNVMBusBusy();
151
152         /* Clear the RESET key in the RESET PDI register to allow the XMEGA to run - must perform this until the
153          * change takes effect, as in some cases it takes multiple writes (silicon bug?).
154          */
155         do
156         {
157                 /* Clear reset register */
158                 XPROGTarget_SendByte(PDI_CMD_STCS(PDI_REG_RESET));
159                 XPROGTarget_SendByte(0x00);
160
161                 /* Read back the reset register, check to see if it took effect */
162                 XPROGTarget_SendByte(PDI_CMD_LDCS(PDI_REG_RESET));
163         } while (XPROGTarget_ReceiveByte() != 0x00);
164
165         XPROGTarget_DisableTargetPDI();
166 }
167
168 /** Retrieves the CRC value of the given memory space.
169  *
170  *  \param[in]  CRCCommand  NVM CRC command to issue to the target
171  *  \param[out] CRCDest     CRC Destination when read from the target
172  *
173  *  \return Boolean \c true if the command sequence complete successfully
174  */
175 bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand,
176                            uint32_t* const CRCDest)
177 {
178         *CRCDest = 0;
179
180         /* Wait until the NVM controller is no longer busy */
181         if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
182           return false;
183
184         /* Set the NVM command to the correct CRC read command */
185         XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
186         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
187         XPROGTarget_SendByte(CRCCommand);
188
189         /* Set CMDEX bit in NVM CTRLA register to start the CRC generation */
190         XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
191         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
192         XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX);
193
194         /* Wait until the NVM bus is ready again */
195         if (!(XMEGANVM_WaitWhileNVMBusBusy()))
196           return false;
197
198         /* Wait until the NVM controller is no longer busy */
199         if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
200           return false;
201
202         /* Load the PDI pointer register with the DAT0 register start address */
203         XPROGTarget_SendByte(PDI_CMD_ST(PDI_POINTER_DIRECT, PDI_DATASIZE_4BYTES));
204         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0);
205
206         /* Send the REPEAT command to grab the CRC bytes */
207         XPROGTarget_SendByte(PDI_CMD_REPEAT(PDI_DATASIZE_1BYTE));
208         XPROGTarget_SendByte(XMEGA_CRC_LENGTH_BYTES - 1);
209
210         /* Read in the CRC bytes from the target */
211         XPROGTarget_SendByte(PDI_CMD_LD(PDI_POINTER_INDIRECT_PI, PDI_DATASIZE_1BYTE));
212         for (uint8_t i = 0; i < XMEGA_CRC_LENGTH_BYTES; i++)
213           ((uint8_t*)CRCDest)[i] = XPROGTarget_ReceiveByte();
214
215         return (TimeoutTicksRemaining > 0);
216 }
217
218 /** Reads memory from the target's memory spaces.
219  *
220  *  \param[in]  ReadAddress  Start address to read from within the target's address space
221  *  \param[out] ReadBuffer   Buffer to store read data into
222  *  \param[in]  ReadSize     Number of bytes to read
223  *
224  *  \return Boolean \c true if the command sequence complete successfully
225  */
226 bool XMEGANVM_ReadMemory(const uint32_t ReadAddress,
227                          uint8_t* ReadBuffer,
228                          uint16_t ReadSize)
229 {
230         /* Wait until the NVM controller is no longer busy */
231         if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
232           return false;
233
234         /* Send the READNVM command to the NVM controller for reading of an arbitrary location */
235         XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
236         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
237         XPROGTarget_SendByte(XMEGA_NVM_CMD_READNVM);
238
239         if (ReadSize > 1)
240         {
241                 /* Load the PDI pointer register with the start address we want to read from */
242                 XPROGTarget_SendByte(PDI_CMD_ST(PDI_POINTER_DIRECT, PDI_DATASIZE_4BYTES));
243                 XMEGANVM_SendAddress(ReadAddress);
244
245                 /* Send the REPEAT command with the specified number of bytes to read */
246                 XPROGTarget_SendByte(PDI_CMD_REPEAT(PDI_DATASIZE_1BYTE));
247                 XPROGTarget_SendByte(ReadSize - 1);
248
249                 /* Send a LD command with indirect access and post-increment to read out the bytes */
250                 XPROGTarget_SendByte(PDI_CMD_LD(PDI_POINTER_INDIRECT_PI, PDI_DATASIZE_1BYTE));
251                 while (ReadSize-- && TimeoutTicksRemaining)
252                   *(ReadBuffer++) = XPROGTarget_ReceiveByte();
253         }
254         else
255         {
256                 /* Send a LDS command with the read address to read out the requested byte */
257                 XPROGTarget_SendByte(PDI_CMD_LDS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
258                 XMEGANVM_SendAddress(ReadAddress);
259                 *(ReadBuffer++) = XPROGTarget_ReceiveByte();
260         }
261
262         return (TimeoutTicksRemaining > 0);
263 }
264
265 /** Writes byte addressed memory to the target's memory spaces.
266  *
267  *  \param[in]  WriteCommand  Command to send to the device to write each memory byte
268  *  \param[in]  WriteAddress  Address to write to within the target's address space
269  *  \param[in]  Byte          Byte to write to the target
270  *
271  *  \return Boolean \c true if the command sequence complete successfully
272  */
273 bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand,
274                               const uint32_t WriteAddress,
275                               const uint8_t Byte)
276 {
277         /* Wait until the NVM controller is no longer busy */
278         if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
279           return false;
280
281         /* Send the memory write command to the target */
282         XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
283         XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
284         XPROGTarget_SendByte(WriteCommand);
285
286         /* Send new memory byte to the memory of the target */
287         XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
288         XMEGANVM_SendAddress(WriteAddress);
289         XPROGTarget_SendByte(Byte);
290
291         return true;
292 }
293
294 /** Writes page addressed memory to the target's memory spaces.
295  *
296  *  \param[in]  WriteBuffCommand  Command to send to the device to write a byte to the memory page buffer
297  *  \param[in]  EraseBuffCommand  Command to send to the device to erase the memory page buffer
298  *  \param[in]  WritePageCommand  Command to send to the device to write the page buffer to the destination memory
299  *  \param[in]  PageMode          Bitfield indicating what operations need to be executed on the specified page
300  *  \param[in]  WriteAddress      Start address to write the page data to within the target's address space
301  *  \param[in]  WriteBuffer       Buffer to source data from
302  *  \param[in]  WriteSize         Number of bytes to write
303  *
304  *  \return Boolean \c true if the command sequence complete successfully
305  */
306 bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand,
307                               const uint8_t EraseBuffCommand,
308                               const uint8_t WritePageCommand,
309                               const uint8_t PageMode,
310                               const uint32_t WriteAddress,
311                               const uint8_t* WriteBuffer,
312                               uint16_t WriteSize)
313 {
314         if (PageMode & XPROG_PAGEMODE_ERASE)
315         {
316                 /* Wait until the NVM controller is no longer busy */
317                 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
318                   return false;
319
320                 /* Send the memory buffer erase command to the target */
321                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
322                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
323                 XPROGTarget_SendByte(EraseBuffCommand);
324
325                 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
326                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
327                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
328                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX);
329         }
330
331         if (WriteSize)
332         {
333                 /* Wait until the NVM controller is no longer busy */
334                 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
335                   return false;
336
337                 /* Send the memory buffer write command to the target */
338                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
339                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
340                 XPROGTarget_SendByte(WriteBuffCommand);
341
342                 /* Load the PDI pointer register with the start address we want to write to */
343                 XPROGTarget_SendByte(PDI_CMD_ST(PDI_POINTER_DIRECT, PDI_DATASIZE_4BYTES));
344                 XMEGANVM_SendAddress(WriteAddress);
345
346                 /* Send the REPEAT command with the specified number of bytes to write */
347                 XPROGTarget_SendByte(PDI_CMD_REPEAT(PDI_DATASIZE_1BYTE));
348                 XPROGTarget_SendByte(WriteSize - 1);
349
350                 /* Send a ST command with indirect access and post-increment to write the bytes */
351                 XPROGTarget_SendByte(PDI_CMD_ST(PDI_POINTER_INDIRECT_PI, PDI_DATASIZE_1BYTE));
352                 while (WriteSize--)
353                   XPROGTarget_SendByte(*(WriteBuffer++));
354         }
355
356         if (PageMode & XPROG_PAGEMODE_WRITE)
357         {
358                 /* Wait until the NVM controller is no longer busy */
359                 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
360                   return false;
361
362                 /* Send the memory write command to the target */
363                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
364                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
365                 XPROGTarget_SendByte(WritePageCommand);
366
367                 /* Send the address of the first page location to write the memory page */
368                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
369                 XMEGANVM_SendAddress(WriteAddress);
370                 XPROGTarget_SendByte(0x00);
371         }
372
373         return true;
374 }
375
376 /** Erases a specific memory space of the target.
377  *
378  *  \param[in] EraseCommand  NVM erase command to send to the device
379  *  \param[in] Address       Address inside the memory space to erase
380  *
381  *  \return Boolean \c true if the command sequence complete successfully
382  */
383 bool XMEGANVM_EraseMemory(const uint8_t EraseCommand,
384                           const uint32_t Address)
385 {
386         /* Wait until the NVM controller is no longer busy */
387         if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
388           return false;
389
390         /* EEPROM and Chip erasures are triggered differently to FLASH section erasures */
391         if (EraseCommand == XMEGA_NVM_CMD_CHIPERASE)
392         {
393                 /* Send the memory erase command to the target */
394                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
395                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
396                 XPROGTarget_SendByte(EraseCommand);
397
398                 /* Set CMDEX bit in NVM CTRLA register to start the erase sequence */
399                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
400                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
401                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX);
402         }
403         else if (EraseCommand == XMEGA_NVM_CMD_ERASEEEPROM)
404         {
405                 /* Send the EEPROM page buffer erase command to the target */
406                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
407                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
408                 XPROGTarget_SendByte(XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF);
409
410                 /* Set CMDEX bit in NVM CTRLA register to start the buffer erase */
411                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
412                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
413                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX);
414
415                 /* Wait until the NVM controller is no longer busy */
416                 if (!(XMEGANVM_WaitWhileNVMControllerBusy()))
417                   return false;
418
419                 /* Send the EEPROM memory buffer write command to the target */
420                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
421                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
422                 XPROGTarget_SendByte(XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF);
423
424                 /* Load the PDI pointer register with the EEPROM page start address */
425                 XPROGTarget_SendByte(PDI_CMD_ST(PDI_POINTER_DIRECT, PDI_DATASIZE_4BYTES));
426                 XMEGANVM_SendAddress(Address);
427
428                 /* Send the REPEAT command with the specified number of bytes to write */
429                 XPROGTarget_SendByte(PDI_CMD_REPEAT(PDI_DATASIZE_1BYTE));
430                 XPROGTarget_SendByte(XPROG_Param_EEPageSize - 1);
431
432                 /* Send a ST command with indirect access and post-increment to tag each byte in the EEPROM page buffer */
433                 XPROGTarget_SendByte(PDI_CMD_ST(PDI_POINTER_INDIRECT_PI, PDI_DATASIZE_1BYTE));
434                 for (uint8_t PageByte = 0; PageByte < XPROG_Param_EEPageSize; PageByte++)
435                   XPROGTarget_SendByte(0x00);
436
437                 /* Send the memory erase command to the target */
438                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
439                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
440                 XPROGTarget_SendByte(EraseCommand);
441
442                 /* Set CMDEX bit in NVM CTRLA register to start the EEPROM erase sequence */
443                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
444                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CTRLA);
445                 XPROGTarget_SendByte(XMEGA_NVM_BIT_CTRLA_CMDEX);
446         }
447         else
448         {
449                 /* Send the memory erase command to the target */
450                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
451                 XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_CMD);
452                 XPROGTarget_SendByte(EraseCommand);
453
454                 /* Other erase modes just need us to address a byte within the target memory space */
455                 XPROGTarget_SendByte(PDI_CMD_STS(PDI_DATASIZE_4BYTES, PDI_DATASIZE_1BYTE));
456                 XMEGANVM_SendAddress(Address);
457                 XPROGTarget_SendByte(0x00);
458         }
459
460         /* Wait until the NVM bus is ready again */
461         if (!(XMEGANVM_WaitWhileNVMBusBusy()))
462           return false;
463
464         return true;
465 }
466
467 #endif
468