]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Projects / AVRISP-MKII / Lib / XPROG / TINYNVM.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 TINY target's NVM module.
34 */
35
36 #define INCLUDE_FROM_TINYNVM_C
37 #include "TINYNVM.h"
38
39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
40
41 /** Sends the given pointer address to the target's TPI pointer register */
42 static void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress)
43 {
44 /* Send the given 16-bit address to the target, LSB first */
45 XPROGTarget_SendByte(TPI_CMD_SSTPR | 0);
46 XPROGTarget_SendByte(AbsoluteAddress & 0xFF);
47 XPROGTarget_SendByte(TPI_CMD_SSTPR | 1);
48 XPROGTarget_SendByte(AbsoluteAddress >> 8);
49 }
50
51 /** Sends a SIN command to the target with the specified I/O address, ready for the data byte to be written.
52 *
53 * \param[in] Address 6-bit I/O address to write to in the target's I/O memory space
54 */
55 static void TINYNVM_SendReadNVMRegister(const uint8_t Address)
56 {
57 /* The TPI command for reading from the I/O space uses strange addressing, where the I/O address's upper
58 * two bits of the 6-bit address are shifted left once - use function to reduce code size */
59 XPROGTarget_SendByte(TPI_CMD_SIN(Address));
60 }
61
62 /** Sends a SOUT command to the target with the specified I/O address, ready for the data byte to be read.
63 *
64 * \param[in] Address 6-bit I/O address to read from in the target's I/O memory space
65 */
66 static void TINYNVM_SendWriteNVMRegister(const uint8_t Address)
67 {
68 /* The TPI command for reading from the I/O space uses strange addressing, where the I/O address's upper
69 * two bits of the 6-bit address are shifted left once - use function to reduce code size */
70 XPROGTarget_SendByte(TPI_CMD_SOUT(Address));
71 }
72
73 /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read.
74 *
75 * \return Boolean \c true if the NVM controller became ready within the timeout period, \c false otherwise
76 */
77 bool TINYNVM_WaitWhileNVMBusBusy(void)
78 {
79 /* Poll the STATUS register to check to see if NVM access has been enabled */
80 for (;;)
81 {
82 /* Send the SLDCS command to read the TPI STATUS register to see the NVM bus is active */
83 XPROGTarget_SendByte(TPI_CMD_SLDCS(TPI_REG_STATUS));
84
85 uint8_t StatusRegister = XPROGTarget_ReceiveByte();
86
87 /* We might have timed out waiting for the status register read response, check here */
88 if (!(TimeoutTicksRemaining))
89 return false;
90
91 /* Check the status register read response to see if the NVM bus is enabled */
92 if (StatusRegister & TPI_STATUS_NVM)
93 return true;
94 }
95 }
96
97 /** Waits while the target's NVM controller is busy performing an operation, exiting if the
98 * timeout period expires.
99 *
100 * \return Boolean \c true if the NVM controller became ready within the timeout period, \c false otherwise
101 */
102 bool TINYNVM_WaitWhileNVMControllerBusy(void)
103 {
104 /* Poll the STATUS register to check to see if NVM access has been enabled */
105 for (;;)
106 {
107 /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */
108 TINYNVM_SendReadNVMRegister(XPROG_Param_NVMCSRRegAddr);
109
110 uint8_t StatusRegister = XPROGTarget_ReceiveByte();
111
112 /* We might have timed out waiting for the status register read response, check here */
113 if (!(TimeoutTicksRemaining))
114 return false;
115
116 /* Check to see if the BUSY flag is still set */
117 if (!(StatusRegister & (1 << 7)))
118 return true;
119 }
120 }
121
122 /** Enables the physical TPI interface on the target and enables access to the internal NVM controller.
123 *
124 * \return Boolean \c true if the TPI interface was enabled successfully, \c false otherwise
125 */
126 bool TINYNVM_EnableTPI(void)
127 {
128 /* Enable TPI programming mode with the attached target */
129 XPROGTarget_EnableTargetTPI();
130
131 /* Lower direction change guard time to 32 USART bits */
132 XPROGTarget_SendByte(TPI_CMD_SSTCS(TPI_REG_CTRL));
133 XPROGTarget_SendByte(0x02);
134
135 /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */
136 XPROGTarget_SendByte(TPI_CMD_SKEY);
137 for (uint8_t i = sizeof(TPI_NVMENABLE_KEY); i > 0; i--)
138 XPROGTarget_SendByte(TPI_NVMENABLE_KEY[i - 1]);
139
140 /* Wait until the NVM bus becomes active */
141 return TINYNVM_WaitWhileNVMBusBusy();
142 }
143
144 /** Removes access to the target's NVM controller and physically disables the target's physical TPI interface. */
145 void TINYNVM_DisableTPI(void)
146 {
147 TINYNVM_WaitWhileNVMBusBusy();
148
149 do
150 {
151 /* Clear the NVMEN bit in the TPI STATUS register to disable TPI mode */
152 XPROGTarget_SendByte(TPI_CMD_SSTCS(TPI_REG_STATUS));
153 XPROGTarget_SendByte(0x00);
154
155 /* Read back the STATUS register, check to see if it took effect */
156 XPROGTarget_SendByte(TPI_CMD_SLDCS(TPI_REG_STATUS));
157 } while (XPROGTarget_ReceiveByte() != 0x00);
158
159 XPROGTarget_DisableTargetTPI();
160 }
161
162 /** Reads memory from the target's memory spaces.
163 *
164 * \param[in] ReadAddress Start address to read from within the target's address space
165 * \param[out] ReadBuffer Buffer to store read data into
166 * \param[in] ReadSize Length of the data to read from the device
167 *
168 * \return Boolean \c true if the command sequence complete successfully
169 */
170 bool TINYNVM_ReadMemory(const uint16_t ReadAddress,
171 uint8_t* ReadBuffer,
172 uint16_t ReadSize)
173 {
174 /* Wait until the NVM controller is no longer busy */
175 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
176 return false;
177
178 /* Set the NVM control register to the NO OP command for memory reading */
179 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
180 XPROGTarget_SendByte(TINY_NVM_CMD_NOOP);
181
182 /* Send the address of the location to read from */
183 TINYNVM_SendPointerAddress(ReadAddress);
184
185 while (ReadSize-- && TimeoutTicksRemaining)
186 {
187 /* Read the byte of data from the target */
188 XPROGTarget_SendByte(TPI_CMD_SLD(TPI_POINTER_INDIRECT_PI));
189 *(ReadBuffer++) = XPROGTarget_ReceiveByte();
190 }
191
192 return (TimeoutTicksRemaining > 0);
193 }
194
195 /** Writes word addressed memory to the target's memory spaces.
196 *
197 * \param[in] WriteAddress Start address to write to within the target's address space
198 * \param[in] WriteBuffer Buffer to source data from
199 * \param[in] WriteLength Total number of bytes to write to the device (must be an integer multiple of 2)
200 *
201 * \return Boolean \c true if the command sequence complete successfully
202 */
203 bool TINYNVM_WriteMemory(const uint16_t WriteAddress,
204 uint8_t* WriteBuffer,
205 uint16_t WriteLength)
206 {
207 /* Wait until the NVM controller is no longer busy */
208 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
209 return false;
210
211 /* Must have an integer number of words to write - if extra byte, word-align via a dummy high byte */
212 if (WriteLength & 0x01)
213 WriteBuffer[WriteLength++] = 0xFF;
214
215 /* Set the NVM control register to the WORD WRITE command for memory writing */
216 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
217 XPROGTarget_SendByte(TINY_NVM_CMD_WORDWRITE);
218
219 /* Send the address of the location to write to */
220 TINYNVM_SendPointerAddress(WriteAddress);
221
222 while (WriteLength)
223 {
224 /* Wait until the NVM controller is no longer busy */
225 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
226 return false;
227
228 /* Write the low byte of data to the target */
229 XPROGTarget_SendByte(TPI_CMD_SST(TPI_POINTER_INDIRECT_PI));
230 XPROGTarget_SendByte(*(WriteBuffer++));
231
232 /* Write the high byte of data to the target */
233 XPROGTarget_SendByte(TPI_CMD_SST(TPI_POINTER_INDIRECT_PI));
234 XPROGTarget_SendByte(*(WriteBuffer++));
235
236 /* Need to decrement the write length twice, since we wrote a whole two-byte word */
237 WriteLength -= 2;
238 }
239
240 return true;
241 }
242
243 /** Erases the target's memory space.
244 *
245 * \param[in] EraseCommand NVM erase command to send to the device
246 * \param[in] Address Address inside the memory space to erase
247 *
248 * \return Boolean \c true if the command sequence complete successfully
249 */
250 bool TINYNVM_EraseMemory(const uint8_t EraseCommand,
251 const uint16_t Address)
252 {
253 /* Wait until the NVM controller is no longer busy */
254 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
255 return false;
256
257 /* Set the NVM control register to the target memory erase command */
258 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
259 XPROGTarget_SendByte(EraseCommand);
260
261 /* Write to a high byte location within the target address space to start the erase process */
262 TINYNVM_SendPointerAddress(Address | 0x0001);
263 XPROGTarget_SendByte(TPI_CMD_SST(TPI_POINTER_INDIRECT));
264 XPROGTarget_SendByte(0x00);
265
266 /* Wait until the NVM controller is no longer busy */
267 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
268 return false;
269
270 return true;
271 }
272
273 #endif
274
Imprint / Impressum