Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TINYNVM.c 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Target-related functions for the TINY target's NVM module.
  29. */
  30. #define INCLUDE_FROM_TINYNVM_C
  31. #include "TINYNVM.h"
  32. #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
  33. /** Sends the given pointer address to the target's TPI pointer register */
  34. static void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress)
  35. {
  36. /* Send the given 16-bit address to the target, LSB first */
  37. XPROGTarget_SendByte(TPI_CMD_SSTPR | 0);
  38. XPROGTarget_SendByte(AbsoluteAddress & 0xFF);
  39. XPROGTarget_SendByte(TPI_CMD_SSTPR | 1);
  40. XPROGTarget_SendByte(AbsoluteAddress >> 8);
  41. }
  42. /** Sends a SIN command to the target with the specified I/O address, ready for the data byte to be written.
  43. *
  44. * \param[in] Address 6-bit I/O address to write to in the target's I/O memory space
  45. */
  46. static void TINYNVM_SendReadNVMRegister(const uint8_t Address)
  47. {
  48. /* The TPI command for reading from the I/O space uses strange addressing, where the I/O address's upper
  49. * two bits of the 6-bit address are shifted left once - use function to reduce code size */
  50. XPROGTarget_SendByte(TPI_CMD_SIN(Address));
  51. }
  52. /** Sends a SOUT command to the target with the specified I/O address, ready for the data byte to be read.
  53. *
  54. * \param[in] Address 6-bit I/O address to read from in the target's I/O memory space
  55. */
  56. static void TINYNVM_SendWriteNVMRegister(const uint8_t Address)
  57. {
  58. /* The TPI command for reading from the I/O space uses strange addressing, where the I/O address's upper
  59. * two bits of the 6-bit address are shifted left once - use function to reduce code size */
  60. XPROGTarget_SendByte(TPI_CMD_SOUT(Address));
  61. }
  62. /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read.
  63. *
  64. * \return Boolean \c true if the NVM controller became ready within the timeout period, \c false otherwise
  65. */
  66. bool TINYNVM_WaitWhileNVMBusBusy(void)
  67. {
  68. /* Poll the STATUS register to check to see if NVM access has been enabled */
  69. for (;;)
  70. {
  71. /* Send the SLDCS command to read the TPI STATUS register to see the NVM bus is active */
  72. XPROGTarget_SendByte(TPI_CMD_SLDCS(TPI_REG_STATUS));
  73. uint8_t StatusRegister = XPROGTarget_ReceiveByte();
  74. /* We might have timed out waiting for the status register read response, check here */
  75. if (!(TimeoutTicksRemaining))
  76. return false;
  77. /* Check the status register read response to see if the NVM bus is enabled */
  78. if (StatusRegister & TPI_STATUS_NVM)
  79. return true;
  80. }
  81. }
  82. /** Waits while the target's NVM controller is busy performing an operation, exiting if the
  83. * timeout period expires.
  84. *
  85. * \return Boolean \c true if the NVM controller became ready within the timeout period, \c false otherwise
  86. */
  87. bool TINYNVM_WaitWhileNVMControllerBusy(void)
  88. {
  89. /* Poll the STATUS register to check to see if NVM access has been enabled */
  90. for (;;)
  91. {
  92. /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */
  93. TINYNVM_SendReadNVMRegister(XPROG_Param_NVMCSRRegAddr);
  94. uint8_t StatusRegister = XPROGTarget_ReceiveByte();
  95. /* We might have timed out waiting for the status register read response, check here */
  96. if (!(TimeoutTicksRemaining))
  97. return false;
  98. /* Check to see if the BUSY flag is still set */
  99. if (!(StatusRegister & (1 << 7)))
  100. return true;
  101. }
  102. }
  103. /** Enables the physical TPI interface on the target and enables access to the internal NVM controller.
  104. *
  105. * \return Boolean \c true if the TPI interface was enabled successfully, \c false otherwise
  106. */
  107. bool TINYNVM_EnableTPI(void)
  108. {
  109. /* Enable TPI programming mode with the attached target */
  110. XPROGTarget_EnableTargetTPI();
  111. /* Lower direction change guard time to 32 USART bits */
  112. XPROGTarget_SendByte(TPI_CMD_SSTCS(TPI_REG_CTRL));
  113. XPROGTarget_SendByte(0x02);
  114. /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */
  115. XPROGTarget_SendByte(TPI_CMD_SKEY);
  116. for (uint8_t i = sizeof(TPI_NVMENABLE_KEY); i > 0; i--)
  117. XPROGTarget_SendByte(TPI_NVMENABLE_KEY[i - 1]);
  118. /* Wait until the NVM bus becomes active */
  119. return TINYNVM_WaitWhileNVMBusBusy();
  120. }
  121. /** Removes access to the target's NVM controller and physically disables the target's physical TPI interface. */
  122. void TINYNVM_DisableTPI(void)
  123. {
  124. TINYNVM_WaitWhileNVMBusBusy();
  125. do
  126. {
  127. /* Clear the NVMEN bit in the TPI STATUS register to disable TPI mode */
  128. XPROGTarget_SendByte(TPI_CMD_SSTCS(TPI_REG_STATUS));
  129. XPROGTarget_SendByte(0x00);
  130. /* Read back the STATUS register, check to see if it took effect */
  131. XPROGTarget_SendByte(TPI_CMD_SLDCS(TPI_REG_STATUS));
  132. } while (XPROGTarget_ReceiveByte() != 0x00);
  133. XPROGTarget_DisableTargetTPI();
  134. }
  135. /** Reads memory from the target's memory spaces.
  136. *
  137. * \param[in] ReadAddress Start address to read from within the target's address space
  138. * \param[out] ReadBuffer Buffer to store read data into
  139. * \param[in] ReadSize Length of the data to read from the device
  140. *
  141. * \return Boolean \c true if the command sequence complete successfully
  142. */
  143. bool TINYNVM_ReadMemory(const uint16_t ReadAddress,
  144. uint8_t* ReadBuffer,
  145. uint16_t ReadSize)
  146. {
  147. /* Wait until the NVM controller is no longer busy */
  148. if (!(TINYNVM_WaitWhileNVMControllerBusy()))
  149. return false;
  150. /* Set the NVM control register to the NO OP command for memory reading */
  151. TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
  152. XPROGTarget_SendByte(TINY_NVM_CMD_NOOP);
  153. /* Send the address of the location to read from */
  154. TINYNVM_SendPointerAddress(ReadAddress);
  155. while (ReadSize-- && TimeoutTicksRemaining)
  156. {
  157. /* Read the byte of data from the target */
  158. XPROGTarget_SendByte(TPI_CMD_SLD(TPI_POINTER_INDIRECT_PI));
  159. *(ReadBuffer++) = XPROGTarget_ReceiveByte();
  160. }
  161. return (TimeoutTicksRemaining > 0);
  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 \c true if the command sequence complete successfully
  170. */
  171. bool TINYNVM_WriteMemory(const uint16_t WriteAddress,
  172. uint8_t* WriteBuffer,
  173. uint16_t WriteLength)
  174. {
  175. /* Wait until the NVM controller is no longer busy */
  176. if (!(TINYNVM_WaitWhileNVMControllerBusy()))
  177. return false;
  178. /* Must have an integer number of words to write - if extra byte, word-align via a dummy high byte */
  179. if (WriteLength & 0x01)
  180. WriteBuffer[WriteLength++] = 0xFF;
  181. /* Set the NVM control register to the WORD WRITE command for memory writing */
  182. TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
  183. XPROGTarget_SendByte(TINY_NVM_CMD_WORDWRITE);
  184. /* Send the address of the location to write to */
  185. TINYNVM_SendPointerAddress(WriteAddress);
  186. while (WriteLength)
  187. {
  188. /* Wait until the NVM controller is no longer busy */
  189. if (!(TINYNVM_WaitWhileNVMControllerBusy()))
  190. return false;
  191. /* Write the low byte of data to the target */
  192. XPROGTarget_SendByte(TPI_CMD_SST(TPI_POINTER_INDIRECT_PI));
  193. XPROGTarget_SendByte(*(WriteBuffer++));
  194. /* Write the high byte of data to the target */
  195. XPROGTarget_SendByte(TPI_CMD_SST(TPI_POINTER_INDIRECT_PI));
  196. XPROGTarget_SendByte(*(WriteBuffer++));
  197. /* Need to decrement the write length twice, since we wrote a whole two-byte word */
  198. WriteLength -= 2;
  199. }
  200. return true;
  201. }
  202. /** Erases the target's memory space.
  203. *
  204. * \param[in] EraseCommand NVM erase command to send to the device
  205. * \param[in] Address Address inside the memory space to erase
  206. *
  207. * \return Boolean \c true if the command sequence complete successfully
  208. */
  209. bool TINYNVM_EraseMemory(const uint8_t EraseCommand,
  210. const uint16_t Address)
  211. {
  212. /* Wait until the NVM controller is no longer busy */
  213. if (!(TINYNVM_WaitWhileNVMControllerBusy()))
  214. return false;
  215. /* Set the NVM control register to the target memory erase command */
  216. TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
  217. XPROGTarget_SendByte(EraseCommand);
  218. /* Write to a high byte location within the target address space to start the erase process */
  219. TINYNVM_SendPointerAddress(Address | 0x0001);
  220. XPROGTarget_SendByte(TPI_CMD_SST(TPI_POINTER_INDIRECT));
  221. XPROGTarget_SendByte(0x00);
  222. /* Wait until the NVM controller is no longer busy */
  223. if (!(TINYNVM_WaitWhileNVMControllerBusy()))
  224. return false;
  225. return true;
  226. }
  227. #endif