3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 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 TINY target's NVM module.
36 #define INCLUDE_FROM_TINYNVM_C
39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
41 /** Sends the given pointer address to the target's TPI pointer register */
42 static void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress
)
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);
51 /** Sends a SIN command to the target with the specified I/O address, ready for the data byte to be written.
53 * \param[in] Address 6-bit I/O address to write to in the target's I/O memory space
55 static void TINYNVM_SendReadNVMRegister(const uint8_t Address
)
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));
62 /** Sends a SOUT command to the target with the specified I/O address, ready for the data byte to be read.
64 * \param[in] Address 6-bit I/O address to read from in the target's I/O memory space
66 static void TINYNVM_SendWriteNVMRegister(const uint8_t Address
)
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));
73 /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read.
75 * \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
77 bool TINYNVM_WaitWhileNVMBusBusy(void)
79 /* Poll the STATUS register to check to see if NVM access has been enabled */
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
);
85 uint8_t StatusRegister
= XPROGTarget_ReceiveByte();
87 /* We might have timed out waiting for the status register read response, check here */
91 /* Check the status register read response to see if the NVM bus is enabled */
92 if (StatusRegister
& TPI_STATUS_NVM
)
97 /** Waits while the target's NVM controller is busy performing an operation, exiting if the
98 * timeout period expires.
100 * \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
102 bool TINYNVM_WaitWhileNVMControllerBusy(void)
104 /* Poll the STATUS register to check to see if NVM access has been enabled */
107 /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */
108 TINYNVM_SendReadNVMRegister(XPROG_Param_NVMCSRRegAddr
);
110 uint8_t StatusRegister
= XPROGTarget_ReceiveByte();
112 /* We might have timed out waiting for the status register read response, check here */
116 /* Check to see if the BUSY flag is still set */
117 if (!(StatusRegister
& (1 << 7)))
122 /** Enables the physical TPI interface on the target and enables access to the internal NVM controller.
124 * \return Boolean true if the TPI interface was enabled successfully, false otherwise
126 bool TINYNVM_EnableTPI(void)
128 /* Enable TPI programming mode with the attached target */
129 XPROGTarget_EnableTargetTPI();
131 /* Lower direction change guard time to 0 USART bits */
132 XPROGTarget_SendByte(TPI_CMD_SSTCS
| TPI_CTRL_REG
);
133 XPROGTarget_SendByte(0x07);
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]);
140 /* Wait until the NVM bus becomes active */
141 return TINYNVM_WaitWhileNVMBusBusy();
144 /** Removes access to the target's NVM controller and physically disables the target's physical TPI interface. */
145 void TINYNVM_DisableTPI(void)
147 TINYNVM_WaitWhileNVMBusBusy();
149 /* Clear the NVMEN bit in the TPI STATUS register to disable TPI mode */
150 XPROGTarget_SendByte(TPI_CMD_SSTCS
| TPI_STATUS_REG
);
151 XPROGTarget_SendByte(0x00);
153 XPROGTarget_DisableTargetTPI();
156 /** Reads memory from the target's memory spaces.
158 * \param[in] ReadAddress Start address to read from within the target's address space
159 * \param[out] ReadBuffer Buffer to store read data into
160 * \param[in] ReadSize Length of the data to read from the device
162 * \return Boolean true if the command sequence complete successfully
164 bool TINYNVM_ReadMemory(const uint16_t ReadAddress
,
168 /* Wait until the NVM controller is no longer busy */
169 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
172 /* Set the NVM control register to the NO OP command for memory reading */
173 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr
);
174 XPROGTarget_SendByte(TINY_NVM_CMD_NOOP
);
176 /* Send the address of the location to read from */
177 TINYNVM_SendPointerAddress(ReadAddress
);
179 while (ReadSize
-- && !(TimeoutExpired
))
181 /* Read the byte of data from the target */
182 XPROGTarget_SendByte(TPI_CMD_SLD
| TPI_POINTER_INDIRECT_PI
);
183 *(ReadBuffer
++) = XPROGTarget_ReceiveByte();
186 return (TimeoutExpired
== false);
189 /** Writes word addressed memory to the target's memory spaces.
191 * \param[in] WriteAddress Start address to write to within the target's address space
192 * \param[in] WriteBuffer Buffer to source data from
193 * \param[in] WriteLength Total number of bytes to write to the device (must be an integer multiple of 2)
195 * \return Boolean true if the command sequence complete successfully
197 bool TINYNVM_WriteMemory(const uint16_t WriteAddress
,
198 uint8_t* WriteBuffer
,
199 uint16_t WriteLength
)
201 /* Wait until the NVM controller is no longer busy */
202 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
205 /* Must have an integer number of words to write - if extra byte, word-align via a dummy high byte */
206 if (WriteLength
& 0x01)
207 WriteBuffer
[WriteLength
++] = 0xFF;
209 /* Set the NVM control register to the WORD WRITE command for memory writing */
210 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr
);
211 XPROGTarget_SendByte(TINY_NVM_CMD_WORDWRITE
);
213 /* Send the address of the location to write to */
214 TINYNVM_SendPointerAddress(WriteAddress
);
218 /* Wait until the NVM controller is no longer busy */
219 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
222 /* Write the low byte of data to the target */
223 XPROGTarget_SendByte(TPI_CMD_SST
| TPI_POINTER_INDIRECT_PI
);
224 XPROGTarget_SendByte(*(WriteBuffer
++));
226 /* Write the high byte of data to the target */
227 XPROGTarget_SendByte(TPI_CMD_SST
| TPI_POINTER_INDIRECT_PI
);
228 XPROGTarget_SendByte(*(WriteBuffer
++));
230 /* Need to decrement the write length twice, since we wrote a whole two-byte word */
237 /** Erases the target's memory space.
239 * \param[in] EraseCommand NVM erase command to send to the device
240 * \param[in] Address Address inside the memory space to erase
242 * \return Boolean true if the command sequence complete successfully
244 bool TINYNVM_EraseMemory(const uint8_t EraseCommand
,
245 const uint16_t Address
)
247 /* Wait until the NVM controller is no longer busy */
248 if (!(TINYNVM_WaitWhileNVMControllerBusy()))
251 /* Set the NVM control register to the target memory erase command */
252 TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr
);
253 XPROGTarget_SendByte(EraseCommand
);
255 /* Write to a high byte location within the target address space to start the erase process */
256 TINYNVM_SendPointerAddress(Address
| 0x0001);
257 XPROGTarget_SendByte(TPI_CMD_SST
| TPI_POINTER_INDIRECT
);
258 XPROGTarget_SendByte(0x00);
260 /* Wait until the NVM controller is no longer busy */
261 if (!(TINYNVM_WaitWhileNVMControllerBusy()))