Added incomplete MIDIToneGenerator project.
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / TINYNVM.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
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 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(((uint8_t*)&AbsoluteAddress)[0]);
47 XPROGTarget_SendByte(TPI_CMD_SSTPR | 1);
48 XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[1]);
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 */
59 XPROGTarget_SendByte(TPI_CMD_SIN | ((Address & 0x30) << 1) | (Address & 0x0F));
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 */
70 XPROGTarget_SendByte(TPI_CMD_SOUT | ((Address & 0x30) << 1) | (Address & 0x0F));
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 true if the NVM controller became ready within the timeout period, 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 while (TimeoutMSRemaining)
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_STATUS_REG);
84 if (XPROGTarget_ReceiveByte() & TPI_STATUS_NVM)
85 {
86 TimeoutMSRemaining = COMMAND_TIMEOUT_MS;
87 return true;
88 }
89
90 /* Manage software timeout */
91 if (TIFR0 & (1 << OCF0A))
92 {
93 TIFR0 |= (1 << OCF0A);
94 TimeoutMSRemaining--;
95 }
96 }
97
98 return false;
99 }
100
101 /** Waits while the target's NVM controller is busy performing an operation, exiting if the
102 * timeout period expires.
103 *
104 * \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
105 */
106 bool TINYNVM_WaitWhileNVMControllerBusy(void)
107 {
108 /* Poll the STATUS register to check to see if NVM access has been enabled */
109 while (TimeoutMSRemaining)
110 {
111 /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */
112 TINYNVM_SendReadNVMRegister(XPROG_Param_NVMCSRRegAddr);
113
114 /* Check to see if the BUSY flag is still set */
115 if (!(XPROGTarget_ReceiveByte() & (1 << 7)))
116 {
117 TimeoutMSRemaining = COMMAND_TIMEOUT_MS;
118 return true;
119 }
120
121 /* Manage software timeout */
122 if (TIFR0 & (1 << OCF0A))
123 {
124 TIFR0 |= (1 << OCF0A);
125 TimeoutMSRemaining--;
126 }
127 }
128
129 return false;
130 }
131
132 /** Reads memory from the target's memory spaces.
133 *
134 * \param[in] ReadAddress Start address to read from within the target's address space
135 * \param[out] ReadBuffer Buffer to store read data into
136 * \param[in] ReadSize Length of the data to read from the device
137 *
138 * \return Boolean true if the command sequence complete successfully
139 */
140 bool TINYNVM_ReadMemory(const uint16_t ReadAddress, uint8_t* ReadBuffer, uint16_t ReadSize)
141 {
142 /* Wait until the NVM controller is no longer busy */
143 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
144 return false;
145
146 /* Set the NVM control register to the NO OP command for memory reading */
147 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
148 XPROGTarget_SendByte(TINY_NVM_CMD_NOOP);
149
150 /* Send the address of the location to read from */
151 TINYNVM_SendPointerAddress(ReadAddress);
152
153 while (ReadSize--)
154 {
155 /* Read the byte of data from the target */
156 XPROGTarget_SendByte(TPI_CMD_SLD | TPI_POINTER_INDIRECT_PI);
157 *(ReadBuffer++) = XPROGTarget_ReceiveByte();
158 }
159
160 return true;
161 }
162
163 /** Writes word addressed memory to the target's memory spaces.
164 *
165 * \param[in] WriteAddress Start address to write to within the target's address space
166 * \param[in] WriteBuffer Buffer to source data from
167 * \param[in] WriteLength Total number of bytes to write to the device (must be an integer multiple of 2)
168 *
169 * \return Boolean true if the command sequence complete successfully
170 */
171 bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint16_t WriteLength)
172 {
173 /* Wait until the NVM controller is no longer busy */
174 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
175 return false;
176
177 /* Must have an integer number of words to write - if extra byte, word-align via a dummy high byte */
178 if (WriteLength & 0x01)
179 WriteBuffer[WriteLength++] = 0xFF;
180
181 /* Set the NVM control register to the WORD WRITE command for memory reading */
182 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
183 XPROGTarget_SendByte(TINY_NVM_CMD_WORDWRITE);
184
185 /* Send the address of the location to write to */
186 TINYNVM_SendPointerAddress(WriteAddress);
187
188 while (WriteLength)
189 {
190 /* Wait until the NVM controller is no longer busy */
191 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
192 return false;
193
194 /* Write the low byte of data to the target */
195 XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI);
196 XPROGTarget_SendByte(*(WriteBuffer++));
197
198 /* Write the high byte of data to the target */
199 XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI);
200 XPROGTarget_SendByte(*(WriteBuffer++));
201
202 /* Need to decrement the write length twice, since we read out a whole word */
203 WriteLength -= 2;
204 }
205
206 return true;
207 }
208
209 /** Erases the target's memory space.
210 *
211 * \param[in] EraseCommand NVM erase command to send to the device
212 * \param[in] Address Address inside the memory space to erase
213 *
214 * \return Boolean true if the command sequence complete successfully
215 */
216 bool TINYNVM_EraseMemory(const uint8_t EraseCommand, const uint16_t Address)
217 {
218 /* Wait until the NVM controller is no longer busy */
219 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
220 return false;
221
222 /* Set the NVM control register to the target memory erase command */
223 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
224 XPROGTarget_SendByte(EraseCommand);
225
226 /* Write to a high byte location within the target address space to start the erase process */
227 TINYNVM_SendPointerAddress(Address | 0x0001);
228 XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT);
229 XPROGTarget_SendByte(0x00);
230
231 /* Wait until the NVM controller is no longer busy */
232 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
233 return false;
234
235 return true;
236 }
237
238 #endif