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.

XPROGProtocol.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. * XPROG Protocol handler, to process V2 Protocol wrapped XPROG commands used in Atmel programmer devices.
  29. */
  30. #define INCLUDE_FROM_XPROGPROTOCOL_C
  31. #include "XPROGProtocol.h"
  32. #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
  33. /** Base absolute address for the target's NVM controller for PDI programming */
  34. uint32_t XPROG_Param_NVMBase = 0x010001C0;
  35. /** Size in bytes of the target's EEPROM page */
  36. uint16_t XPROG_Param_EEPageSize = 32;
  37. /** Address of the TPI device's NVMCMD register for TPI programming */
  38. uint8_t XPROG_Param_NVMCMDRegAddr = 0x33;
  39. /** Address of the TPI device's NVMCSR register for TPI programming */
  40. uint8_t XPROG_Param_NVMCSRRegAddr = 0x32;
  41. /** Currently selected XPROG programming protocol */
  42. uint8_t XPROG_SelectedProtocol = XPROG_PROTOCOL_PDI;
  43. /** Handler for the CMD_XPROG_SETMODE command, which sets the programmer-to-target protocol used for PDI/TPI
  44. * programming.
  45. */
  46. void XPROGProtocol_SetMode(void)
  47. {
  48. struct
  49. {
  50. uint8_t Protocol;
  51. } SetMode_XPROG_Params;
  52. Endpoint_Read_Stream_LE(&SetMode_XPROG_Params, sizeof(SetMode_XPROG_Params), NULL);
  53. Endpoint_ClearOUT();
  54. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  55. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  56. XPROG_SelectedProtocol = SetMode_XPROG_Params.Protocol;
  57. Endpoint_Write_8(CMD_XPROG_SETMODE);
  58. Endpoint_Write_8((SetMode_XPROG_Params.Protocol != XPROG_PROTOCOL_JTAG) ? STATUS_CMD_OK : STATUS_CMD_FAILED);
  59. Endpoint_ClearIN();
  60. }
  61. /** Handler for the CMD_XPROG command, which wraps up XPROG commands in a V2 wrapper which need to be
  62. * removed and processed so that the underlying XPROG command can be handled.
  63. */
  64. void XPROGProtocol_Command(void)
  65. {
  66. uint8_t XPROGCommand = Endpoint_Read_8();
  67. switch (XPROGCommand)
  68. {
  69. case XPROG_CMD_ENTER_PROGMODE:
  70. XPROGProtocol_EnterXPROGMode();
  71. break;
  72. case XPROG_CMD_LEAVE_PROGMODE:
  73. XPROGProtocol_LeaveXPROGMode();
  74. break;
  75. case XPROG_CMD_ERASE:
  76. XPROGProtocol_Erase();
  77. break;
  78. case XPROG_CMD_WRITE_MEM:
  79. XPROGProtocol_WriteMemory();
  80. break;
  81. case XPROG_CMD_READ_MEM:
  82. XPROGProtocol_ReadMemory();
  83. break;
  84. case XPROG_CMD_CRC:
  85. XPROGProtocol_ReadCRC();
  86. break;
  87. case XPROG_CMD_SET_PARAM:
  88. XPROGProtocol_SetParam();
  89. break;
  90. }
  91. }
  92. /** Handler for the XPROG ENTER_PROGMODE command to establish a connection with the attached device. */
  93. static void XPROGProtocol_EnterXPROGMode(void)
  94. {
  95. Endpoint_ClearOUT();
  96. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  97. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  98. bool NVMBusEnabled = false;
  99. if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
  100. NVMBusEnabled = XMEGANVM_EnablePDI();
  101. else if (XPROG_SelectedProtocol == XPROG_PROTOCOL_TPI)
  102. NVMBusEnabled = TINYNVM_EnableTPI();
  103. Endpoint_Write_8(CMD_XPROG);
  104. Endpoint_Write_8(XPROG_CMD_ENTER_PROGMODE);
  105. Endpoint_Write_8(NVMBusEnabled ? XPROG_ERR_OK : XPROG_ERR_FAILED);
  106. Endpoint_ClearIN();
  107. }
  108. /** Handler for the XPROG LEAVE_PROGMODE command to terminate the PDI programming connection with
  109. * the attached device.
  110. */
  111. static void XPROGProtocol_LeaveXPROGMode(void)
  112. {
  113. Endpoint_ClearOUT();
  114. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  115. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  116. if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
  117. XMEGANVM_DisablePDI();
  118. else
  119. TINYNVM_DisableTPI();
  120. #if defined(XCK_RESCUE_CLOCK_ENABLE) && defined(ENABLE_ISP_PROTOCOL)
  121. /* If the XCK rescue clock option is enabled, we need to restart it once the
  122. * XPROG mode has been exited, since the XPROG protocol stops it after use. */
  123. ISPTarget_ConfigureRescueClock();
  124. #endif
  125. Endpoint_Write_8(CMD_XPROG);
  126. Endpoint_Write_8(XPROG_CMD_LEAVE_PROGMODE);
  127. Endpoint_Write_8(XPROG_ERR_OK);
  128. Endpoint_ClearIN();
  129. }
  130. /** Handler for the XPRG ERASE command to erase a specific memory address space in the attached device. */
  131. static void XPROGProtocol_Erase(void)
  132. {
  133. uint8_t ReturnStatus = XPROG_ERR_OK;
  134. struct
  135. {
  136. uint8_t MemoryType;
  137. uint32_t Address;
  138. } Erase_XPROG_Params;
  139. Endpoint_Read_Stream_LE(&Erase_XPROG_Params, sizeof(Erase_XPROG_Params), NULL);
  140. Erase_XPROG_Params.Address = SwapEndian_32(Erase_XPROG_Params.Address);
  141. Endpoint_ClearOUT();
  142. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  143. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  144. uint8_t EraseCommand;
  145. if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
  146. {
  147. /* Determine which NVM command to send to the device depending on the memory to erase */
  148. switch (Erase_XPROG_Params.MemoryType)
  149. {
  150. case XPROG_ERASE_CHIP:
  151. EraseCommand = XMEGA_NVM_CMD_CHIPERASE;
  152. break;
  153. case XPROG_ERASE_APP:
  154. EraseCommand = XMEGA_NVM_CMD_ERASEAPPSEC;
  155. break;
  156. case XPROG_ERASE_BOOT:
  157. EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSEC;
  158. break;
  159. case XPROG_ERASE_EEPROM:
  160. EraseCommand = XMEGA_NVM_CMD_ERASEEEPROM;
  161. break;
  162. case XPROG_ERASE_APP_PAGE:
  163. EraseCommand = XMEGA_NVM_CMD_ERASEAPPSECPAGE;
  164. break;
  165. case XPROG_ERASE_BOOT_PAGE:
  166. EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSECPAGE;
  167. break;
  168. case XPROG_ERASE_EEPROM_PAGE:
  169. EraseCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGE;
  170. break;
  171. case XPROG_ERASE_USERSIG:
  172. EraseCommand = XMEGA_NVM_CMD_ERASEUSERSIG;
  173. break;
  174. default:
  175. EraseCommand = XMEGA_NVM_CMD_NOOP;
  176. break;
  177. }
  178. /* Erase the target memory, indicate timeout if occurred */
  179. if (!(XMEGANVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address)))
  180. ReturnStatus = XPROG_ERR_TIMEOUT;
  181. }
  182. else
  183. {
  184. if (Erase_XPROG_Params.MemoryType == XPROG_ERASE_CHIP)
  185. EraseCommand = TINY_NVM_CMD_CHIPERASE;
  186. else
  187. EraseCommand = TINY_NVM_CMD_SECTIONERASE;
  188. /* Erase the target memory, indicate timeout if occurred */
  189. if (!(TINYNVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address)))
  190. ReturnStatus = XPROG_ERR_TIMEOUT;
  191. }
  192. Endpoint_Write_8(CMD_XPROG);
  193. Endpoint_Write_8(XPROG_CMD_ERASE);
  194. Endpoint_Write_8(ReturnStatus);
  195. Endpoint_ClearIN();
  196. }
  197. /** Handler for the XPROG WRITE_MEMORY command to write to a specific memory space within the attached device. */
  198. static void XPROGProtocol_WriteMemory(void)
  199. {
  200. uint8_t ReturnStatus = XPROG_ERR_OK;
  201. struct
  202. {
  203. uint8_t MemoryType;
  204. uint8_t PageMode;
  205. uint32_t Address;
  206. uint16_t Length;
  207. uint8_t ProgData[256];
  208. } WriteMemory_XPROG_Params;
  209. Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params, (sizeof(WriteMemory_XPROG_Params) -
  210. sizeof(WriteMemory_XPROG_Params).ProgData), NULL);
  211. WriteMemory_XPROG_Params.Address = SwapEndian_32(WriteMemory_XPROG_Params.Address);
  212. WriteMemory_XPROG_Params.Length = SwapEndian_16(WriteMemory_XPROG_Params.Length);
  213. Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length, NULL);
  214. // The driver will terminate transfers that are a round multiple of the endpoint bank in size with a ZLP, need
  215. // to catch this and discard it before continuing on with packet processing to prevent communication issues
  216. if (((sizeof(uint8_t) + sizeof(WriteMemory_XPROG_Params) - sizeof(WriteMemory_XPROG_Params.ProgData)) +
  217. WriteMemory_XPROG_Params.Length) % AVRISP_DATA_EPSIZE == 0)
  218. {
  219. Endpoint_ClearOUT();
  220. Endpoint_WaitUntilReady();
  221. }
  222. Endpoint_ClearOUT();
  223. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  224. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  225. if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
  226. {
  227. /* Assume FLASH page programming by default, as it is the common case */
  228. uint8_t WriteCommand = XMEGA_NVM_CMD_WRITEFLASHPAGE;
  229. uint8_t WriteBuffCommand = XMEGA_NVM_CMD_LOADFLASHPAGEBUFF;
  230. uint8_t EraseBuffCommand = XMEGA_NVM_CMD_ERASEFLASHPAGEBUFF;
  231. bool PagedMemory = true;
  232. switch (WriteMemory_XPROG_Params.MemoryType)
  233. {
  234. case XPROG_MEM_TYPE_APPL:
  235. WriteCommand = XMEGA_NVM_CMD_WRITEAPPSECPAGE;
  236. break;
  237. case XPROG_MEM_TYPE_BOOT:
  238. WriteCommand = XMEGA_NVM_CMD_WRITEBOOTSECPAGE;
  239. break;
  240. case XPROG_MEM_TYPE_EEPROM:
  241. WriteCommand = XMEGA_NVM_CMD_ERASEWRITEEEPROMPAGE;
  242. WriteBuffCommand = XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF;
  243. EraseBuffCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF;
  244. break;
  245. case XPROG_MEM_TYPE_USERSIG:
  246. WriteCommand = XMEGA_NVM_CMD_WRITEUSERSIG;
  247. break;
  248. case XPROG_MEM_TYPE_FUSE:
  249. WriteCommand = XMEGA_NVM_CMD_WRITEFUSE;
  250. PagedMemory = false;
  251. break;
  252. case XPROG_MEM_TYPE_LOCKBITS:
  253. WriteCommand = XMEGA_NVM_CMD_WRITELOCK;
  254. PagedMemory = false;
  255. break;
  256. }
  257. /* Send the appropriate memory write commands to the device, indicate timeout if occurred */
  258. if ((PagedMemory && !(XMEGANVM_WritePageMemory(WriteBuffCommand, EraseBuffCommand, WriteCommand,
  259. WriteMemory_XPROG_Params.PageMode, WriteMemory_XPROG_Params.Address,
  260. WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length))) ||
  261. (!PagedMemory && !(XMEGANVM_WriteByteMemory(WriteCommand, WriteMemory_XPROG_Params.Address,
  262. WriteMemory_XPROG_Params.ProgData[0]))))
  263. {
  264. ReturnStatus = XPROG_ERR_TIMEOUT;
  265. }
  266. }
  267. else
  268. {
  269. /* Send write command to the TPI device, indicate timeout if occurred */
  270. if (!(TINYNVM_WriteMemory(WriteMemory_XPROG_Params.Address, WriteMemory_XPROG_Params.ProgData,
  271. WriteMemory_XPROG_Params.Length)))
  272. {
  273. ReturnStatus = XPROG_ERR_TIMEOUT;
  274. }
  275. }
  276. Endpoint_Write_8(CMD_XPROG);
  277. Endpoint_Write_8(XPROG_CMD_WRITE_MEM);
  278. Endpoint_Write_8(ReturnStatus);
  279. Endpoint_ClearIN();
  280. }
  281. /** Handler for the XPROG READ_MEMORY command to read data from a specific address space within the
  282. * attached device.
  283. */
  284. static void XPROGProtocol_ReadMemory(void)
  285. {
  286. uint8_t ReturnStatus = XPROG_ERR_OK;
  287. struct
  288. {
  289. uint8_t MemoryType;
  290. uint32_t Address;
  291. uint16_t Length;
  292. } ReadMemory_XPROG_Params;
  293. Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params, sizeof(ReadMemory_XPROG_Params), NULL);
  294. ReadMemory_XPROG_Params.Address = SwapEndian_32(ReadMemory_XPROG_Params.Address);
  295. ReadMemory_XPROG_Params.Length = SwapEndian_16(ReadMemory_XPROG_Params.Length);
  296. Endpoint_ClearOUT();
  297. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  298. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  299. uint8_t ReadBuffer[256];
  300. if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
  301. {
  302. /* Read the PDI target's memory, indicate timeout if occurred */
  303. if (!(XMEGANVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))
  304. ReturnStatus = XPROG_ERR_TIMEOUT;
  305. }
  306. else
  307. {
  308. /* Read the TPI target's memory, indicate timeout if occurred */
  309. if (!(TINYNVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))
  310. ReturnStatus = XPROG_ERR_TIMEOUT;
  311. }
  312. Endpoint_Write_8(CMD_XPROG);
  313. Endpoint_Write_8(XPROG_CMD_READ_MEM);
  314. Endpoint_Write_8(ReturnStatus);
  315. if (ReturnStatus == XPROG_ERR_OK)
  316. Endpoint_Write_Stream_LE(ReadBuffer, ReadMemory_XPROG_Params.Length, NULL);
  317. Endpoint_ClearIN();
  318. }
  319. /** Handler for the XPROG CRC command to read a specific memory space's CRC value for comparison between the
  320. * attached device's memory and a data set on the host.
  321. */
  322. static void XPROGProtocol_ReadCRC(void)
  323. {
  324. uint8_t ReturnStatus = XPROG_ERR_OK;
  325. struct
  326. {
  327. uint8_t CRCType;
  328. } ReadCRC_XPROG_Params;
  329. Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params, sizeof(ReadCRC_XPROG_Params), NULL);
  330. Endpoint_ClearOUT();
  331. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  332. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  333. uint32_t MemoryCRC;
  334. if (XPROG_SelectedProtocol == XPROG_PROTOCOL_PDI)
  335. {
  336. uint8_t CRCCommand;
  337. /* Determine which NVM command to send to the device depending on the memory to CRC */
  338. switch (ReadCRC_XPROG_Params.CRCType)
  339. {
  340. case XPROG_CRC_APP:
  341. CRCCommand = XMEGA_NVM_CMD_APPCRC;
  342. break;
  343. case XPROG_CRC_BOOT:
  344. CRCCommand = XMEGA_NVM_CMD_BOOTCRC;
  345. break;
  346. default:
  347. CRCCommand = XMEGA_NVM_CMD_FLASHCRC;
  348. break;
  349. }
  350. /* Perform and retrieve the memory CRC, indicate timeout if occurred */
  351. if (!(XMEGANVM_GetMemoryCRC(CRCCommand, &MemoryCRC)))
  352. ReturnStatus = XPROG_ERR_TIMEOUT;
  353. }
  354. else
  355. {
  356. /* TPI does not support memory CRC */
  357. ReturnStatus = XPROG_ERR_FAILED;
  358. }
  359. Endpoint_Write_8(CMD_XPROG);
  360. Endpoint_Write_8(XPROG_CMD_CRC);
  361. Endpoint_Write_8(ReturnStatus);
  362. if (ReturnStatus == XPROG_ERR_OK)
  363. {
  364. Endpoint_Write_8(MemoryCRC >> 16);
  365. Endpoint_Write_16_LE(MemoryCRC & 0xFFFF);
  366. }
  367. Endpoint_ClearIN();
  368. }
  369. /** Handler for the XPROG SET_PARAM command to set a XPROG parameter for use when communicating with the
  370. * attached device.
  371. */
  372. static void XPROGProtocol_SetParam(void)
  373. {
  374. uint8_t ReturnStatus = XPROG_ERR_OK;
  375. uint8_t XPROGParam = Endpoint_Read_8();
  376. /* Determine which parameter is being set, store the new parameter value */
  377. switch (XPROGParam)
  378. {
  379. case XPROG_PARAM_NVMBASE:
  380. XPROG_Param_NVMBase = Endpoint_Read_32_BE();
  381. break;
  382. case XPROG_PARAM_EEPPAGESIZE:
  383. XPROG_Param_EEPageSize = Endpoint_Read_16_BE();
  384. break;
  385. case XPROG_PARAM_NVMCMD_REG:
  386. XPROG_Param_NVMCMDRegAddr = Endpoint_Read_8();
  387. break;
  388. case XPROG_PARAM_NVMCSR_REG:
  389. XPROG_Param_NVMCSRRegAddr = Endpoint_Read_8();
  390. break;
  391. case XPROG_PARAM_UNKNOWN_1:
  392. /* TODO: Undocumented parameter added in AVRStudio 5.1, purpose unknown. Must ACK and discard or
  393. the communication with AVRStudio 5.1 will fail.
  394. */
  395. Endpoint_Discard_16();
  396. break;
  397. default:
  398. ReturnStatus = XPROG_ERR_FAILED;
  399. break;
  400. }
  401. Endpoint_ClearOUT();
  402. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  403. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  404. Endpoint_Write_8(CMD_XPROG);
  405. Endpoint_Write_8(XPROG_CMD_SET_PARAM);
  406. Endpoint_Write_8(ReturnStatus);
  407. Endpoint_ClearIN();
  408. }
  409. #endif