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.

ISPProtocol.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. * ISP Protocol handler, to process V2 Protocol wrapped ISP commands used in Atmel programmer devices.
  29. */
  30. #include "ISPProtocol.h"
  31. #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__)
  32. /** Handler for the CMD_ENTER_PROGMODE_ISP command, which attempts to enter programming mode on
  33. * the attached device, returning success or failure back to the host.
  34. */
  35. void ISPProtocol_EnterISPMode(void)
  36. {
  37. struct
  38. {
  39. uint8_t TimeoutMS;
  40. uint8_t PinStabDelayMS;
  41. uint8_t ExecutionDelayMS;
  42. uint8_t SynchLoops;
  43. uint8_t ByteDelay;
  44. uint8_t PollValue;
  45. uint8_t PollIndex;
  46. uint8_t EnterProgBytes[4];
  47. } Enter_ISP_Params;
  48. Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params), NULL);
  49. Endpoint_ClearOUT();
  50. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  51. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  52. uint8_t ResponseStatus = STATUS_CMD_FAILED;
  53. CurrentAddress = 0;
  54. /* Perform execution delay, initialize SPI bus */
  55. ISPProtocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS);
  56. ISPTarget_EnableTargetISP();
  57. ISPTarget_ChangeTargetResetLine(true);
  58. ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
  59. /* Continuously attempt to synchronize with the target until either the number of attempts specified
  60. * by the host has exceeded, or the the device sends back the expected response values */
  61. while (Enter_ISP_Params.SynchLoops-- && TimeoutTicksRemaining)
  62. {
  63. uint8_t ResponseBytes[4];
  64. for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
  65. {
  66. ISPProtocol_DelayMS(Enter_ISP_Params.ByteDelay);
  67. ResponseBytes[RByte] = ISPTarget_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]);
  68. }
  69. /* Check if polling disabled, or if the polled value matches the expected value */
  70. if (!(Enter_ISP_Params.PollIndex) || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue))
  71. {
  72. ResponseStatus = STATUS_CMD_OK;
  73. break;
  74. }
  75. else
  76. {
  77. ISPTarget_ChangeTargetResetLine(false);
  78. ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
  79. ISPTarget_ChangeTargetResetLine(true);
  80. ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
  81. }
  82. }
  83. Endpoint_Write_8(CMD_ENTER_PROGMODE_ISP);
  84. Endpoint_Write_8(ResponseStatus);
  85. Endpoint_ClearIN();
  86. }
  87. /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */
  88. void ISPProtocol_LeaveISPMode(void)
  89. {
  90. struct
  91. {
  92. uint8_t PreDelayMS;
  93. uint8_t PostDelayMS;
  94. } Leave_ISP_Params;
  95. Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NULL);
  96. Endpoint_ClearOUT();
  97. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  98. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  99. /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */
  100. ISPProtocol_DelayMS(Leave_ISP_Params.PreDelayMS);
  101. ISPTarget_ChangeTargetResetLine(false);
  102. ISPTarget_DisableTargetISP();
  103. ISPProtocol_DelayMS(Leave_ISP_Params.PostDelayMS);
  104. Endpoint_Write_8(CMD_LEAVE_PROGMODE_ISP);
  105. Endpoint_Write_8(STATUS_CMD_OK);
  106. Endpoint_ClearIN();
  107. }
  108. /** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes,
  109. * words or pages of data to the attached device.
  110. *
  111. * \param[in] V2Command Issued V2 Protocol command byte from the host
  112. */
  113. void ISPProtocol_ProgramMemory(uint8_t V2Command)
  114. {
  115. struct
  116. {
  117. uint16_t BytesToWrite;
  118. uint8_t ProgrammingMode;
  119. uint8_t DelayMS;
  120. uint8_t ProgrammingCommands[3];
  121. uint8_t PollValue1;
  122. uint8_t PollValue2;
  123. uint8_t ProgData[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
  124. } Write_Memory_Params; // whole page and ACK the packet as fast as possible to prevent it from aborting
  125. Endpoint_Read_Stream_LE(&Write_Memory_Params, (sizeof(Write_Memory_Params) -
  126. sizeof(Write_Memory_Params.ProgData)), NULL);
  127. Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
  128. if (Write_Memory_Params.BytesToWrite > sizeof(Write_Memory_Params.ProgData))
  129. {
  130. Endpoint_ClearOUT();
  131. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  132. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  133. Endpoint_Write_8(V2Command);
  134. Endpoint_Write_8(STATUS_CMD_FAILED);
  135. Endpoint_ClearIN();
  136. return;
  137. }
  138. Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite, NULL);
  139. // The driver will terminate transfers that are a round multiple of the endpoint bank in size with a ZLP, need
  140. // to catch this and discard it before continuing on with packet processing to prevent communication issues
  141. if (((sizeof(uint8_t) + sizeof(Write_Memory_Params) - sizeof(Write_Memory_Params.ProgData)) +
  142. Write_Memory_Params.BytesToWrite) % AVRISP_DATA_EPSIZE == 0)
  143. {
  144. Endpoint_ClearOUT();
  145. Endpoint_WaitUntilReady();
  146. }
  147. Endpoint_ClearOUT();
  148. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  149. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  150. uint8_t ProgrammingStatus = STATUS_CMD_OK;
  151. uint8_t PollValue = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 :
  152. Write_Memory_Params.PollValue2;
  153. uint16_t PollAddress = 0;
  154. uint8_t* NextWriteByte = Write_Memory_Params.ProgData;
  155. uint16_t PageStartAddress = (CurrentAddress & 0xFFFF);
  156. for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
  157. {
  158. uint8_t ByteToWrite = *(NextWriteByte++);
  159. uint8_t ProgrammingMode = Write_Memory_Params.ProgrammingMode;
  160. /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
  161. if (MustLoadExtendedAddress)
  162. {
  163. ISPTarget_LoadExtendedAddress();
  164. MustLoadExtendedAddress = false;
  165. }
  166. ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
  167. ISPTarget_SendByte(CurrentAddress >> 8);
  168. ISPTarget_SendByte(CurrentAddress & 0xFF);
  169. ISPTarget_SendByte(ByteToWrite);
  170. /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
  171. * or low byte at the current word address */
  172. if (V2Command == CMD_PROGRAM_FLASH_ISP)
  173. Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK;
  174. /* Check to see if we have a valid polling address */
  175. if (!(PollAddress) && (ByteToWrite != PollValue))
  176. {
  177. if ((CurrentByte & 0x01) && (V2Command == CMD_PROGRAM_FLASH_ISP))
  178. Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;
  179. else
  180. Write_Memory_Params.ProgrammingCommands[2] &= ~READ_WRITE_HIGH_BYTE_MASK;
  181. PollAddress = (CurrentAddress & 0xFFFF);
  182. }
  183. /* If in word programming mode, commit the byte to the target's memory */
  184. if (!(ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK))
  185. {
  186. /* If the current polling address is invalid, switch to timed delay write completion mode */
  187. if (!(PollAddress) && !(ProgrammingMode & PROG_MODE_WORD_READYBUSY_MASK))
  188. ProgrammingMode = (ProgrammingMode & ~PROG_MODE_WORD_VALUE_MASK) | PROG_MODE_WORD_TIMEDELAY_MASK;
  189. ProgrammingStatus = ISPTarget_WaitForProgComplete(ProgrammingMode, PollAddress, PollValue,
  190. Write_Memory_Params.DelayMS,
  191. Write_Memory_Params.ProgrammingCommands[2]);
  192. /* Abort the programming loop early if the byte/word programming failed */
  193. if (ProgrammingStatus != STATUS_CMD_OK)
  194. break;
  195. /* Must reset the polling address afterwards, so it is not erroneously used for the next byte */
  196. PollAddress = 0;
  197. }
  198. /* EEPROM just increments the address each byte, flash needs to increment on each word and
  199. * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
  200. * address boundary has been crossed during FLASH memory programming */
  201. if ((CurrentByte & 0x01) || (V2Command == CMD_PROGRAM_EEPROM_ISP))
  202. {
  203. CurrentAddress++;
  204. if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
  205. MustLoadExtendedAddress = true;
  206. }
  207. }
  208. /* If the current page must be committed, send the PROGRAM PAGE command to the target */
  209. if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK)
  210. {
  211. ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[1]);
  212. ISPTarget_SendByte(PageStartAddress >> 8);
  213. ISPTarget_SendByte(PageStartAddress & 0xFF);
  214. ISPTarget_SendByte(0x00);
  215. /* Check if polling is enabled and possible, if not switch to timed delay mode */
  216. if ((Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_VALUE_MASK) && !(PollAddress))
  217. {
  218. Write_Memory_Params.ProgrammingMode = (Write_Memory_Params.ProgrammingMode & ~PROG_MODE_PAGED_VALUE_MASK) |
  219. PROG_MODE_PAGED_TIMEDELAY_MASK;
  220. }
  221. ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
  222. Write_Memory_Params.DelayMS,
  223. Write_Memory_Params.ProgrammingCommands[2]);
  224. /* Check to see if the FLASH address has crossed the extended address boundary */
  225. if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
  226. MustLoadExtendedAddress = true;
  227. }
  228. Endpoint_Write_8(V2Command);
  229. Endpoint_Write_8(ProgrammingStatus);
  230. Endpoint_ClearIN();
  231. }
  232. /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,
  233. * words or pages of data from the attached device.
  234. *
  235. * \param[in] V2Command Issued V2 Protocol command byte from the host
  236. */
  237. void ISPProtocol_ReadMemory(uint8_t V2Command)
  238. {
  239. struct
  240. {
  241. uint16_t BytesToRead;
  242. uint8_t ReadMemoryCommand;
  243. } Read_Memory_Params;
  244. Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params), NULL);
  245. Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
  246. Endpoint_ClearOUT();
  247. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  248. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  249. Endpoint_Write_8(V2Command);
  250. Endpoint_Write_8(STATUS_CMD_OK);
  251. /* Read each byte from the device and write them to the packet for the host */
  252. for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
  253. {
  254. /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
  255. if (MustLoadExtendedAddress)
  256. {
  257. ISPTarget_LoadExtendedAddress();
  258. MustLoadExtendedAddress = false;
  259. }
  260. /* Read the next byte from the desired memory space in the device */
  261. ISPTarget_SendByte(Read_Memory_Params.ReadMemoryCommand);
  262. ISPTarget_SendByte(CurrentAddress >> 8);
  263. ISPTarget_SendByte(CurrentAddress & 0xFF);
  264. Endpoint_Write_8(ISPTarget_ReceiveByte());
  265. /* Check if the endpoint bank is currently full, if so send the packet */
  266. if (!(Endpoint_IsReadWriteAllowed()))
  267. {
  268. Endpoint_ClearIN();
  269. Endpoint_WaitUntilReady();
  270. }
  271. /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high
  272. * or low byte at the current word address */
  273. if (V2Command == CMD_READ_FLASH_ISP)
  274. Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK;
  275. /* EEPROM just increments the address each byte, flash needs to increment on each word and
  276. * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
  277. * address boundary has been crossed */
  278. if ((CurrentByte & 0x01) || (V2Command == CMD_READ_EEPROM_ISP))
  279. {
  280. CurrentAddress++;
  281. if ((V2Command != CMD_READ_EEPROM_ISP) && !(CurrentAddress & 0xFFFF))
  282. MustLoadExtendedAddress = true;
  283. }
  284. }
  285. Endpoint_Write_8(STATUS_CMD_OK);
  286. bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
  287. Endpoint_ClearIN();
  288. /* Ensure last packet is a short packet to terminate the transfer */
  289. if (IsEndpointFull)
  290. {
  291. Endpoint_WaitUntilReady();
  292. Endpoint_ClearIN();
  293. Endpoint_WaitUntilReady();
  294. }
  295. }
  296. /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */
  297. void ISPProtocol_ChipErase(void)
  298. {
  299. struct
  300. {
  301. uint8_t EraseDelayMS;
  302. uint8_t PollMethod;
  303. uint8_t EraseCommandBytes[4];
  304. } Erase_Chip_Params;
  305. Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NULL);
  306. Endpoint_ClearOUT();
  307. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  308. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  309. uint8_t ResponseStatus = STATUS_CMD_OK;
  310. /* Send the chip erase commands as given by the host to the device */
  311. for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
  312. ISPTarget_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
  313. /* Use appropriate command completion check as given by the host (delay or busy polling) */
  314. if (!(Erase_Chip_Params.PollMethod))
  315. ISPProtocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
  316. else
  317. ResponseStatus = ISPTarget_WaitWhileTargetBusy();
  318. Endpoint_Write_8(CMD_CHIP_ERASE_ISP);
  319. Endpoint_Write_8(ResponseStatus);
  320. Endpoint_ClearIN();
  321. }
  322. /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
  323. * reading the requested configuration byte from the device.
  324. *
  325. * \param[in] V2Command Issued V2 Protocol command byte from the host
  326. */
  327. void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
  328. {
  329. struct
  330. {
  331. uint8_t RetByte;
  332. uint8_t ReadCommandBytes[4];
  333. } Read_FuseLockSigOSCCAL_Params;
  334. Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NULL);
  335. Endpoint_ClearOUT();
  336. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  337. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  338. uint8_t ResponseBytes[4];
  339. /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */
  340. for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
  341. ResponseBytes[RByte] = ISPTarget_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
  342. Endpoint_Write_8(V2Command);
  343. Endpoint_Write_8(STATUS_CMD_OK);
  344. Endpoint_Write_8(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);
  345. Endpoint_Write_8(STATUS_CMD_OK);
  346. Endpoint_ClearIN();
  347. }
  348. /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration
  349. * byte to the device.
  350. *
  351. * \param[in] V2Command Issued V2 Protocol command byte from the host
  352. */
  353. void ISPProtocol_WriteFuseLock(uint8_t V2Command)
  354. {
  355. struct
  356. {
  357. uint8_t WriteCommandBytes[4];
  358. } Write_FuseLockSig_Params;
  359. Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NULL);
  360. Endpoint_ClearOUT();
  361. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  362. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  363. /* Send the Fuse or Lock byte program commands as given by the host to the device */
  364. for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
  365. ISPTarget_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
  366. Endpoint_Write_8(V2Command);
  367. Endpoint_Write_8(STATUS_CMD_OK);
  368. Endpoint_Write_8(STATUS_CMD_OK);
  369. Endpoint_ClearIN();
  370. }
  371. /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */
  372. void ISPProtocol_SPIMulti(void)
  373. {
  374. struct
  375. {
  376. uint8_t TxBytes;
  377. uint8_t RxBytes;
  378. uint8_t RxStartAddr;
  379. uint8_t TxData[255];
  380. } SPI_Multi_Params;
  381. Endpoint_Read_Stream_LE(&SPI_Multi_Params, (sizeof(SPI_Multi_Params) - sizeof(SPI_Multi_Params.TxData)), NULL);
  382. Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes, NULL);
  383. Endpoint_ClearOUT();
  384. Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
  385. Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
  386. Endpoint_Write_8(CMD_SPI_MULTI);
  387. Endpoint_Write_8(STATUS_CMD_OK);
  388. uint8_t CurrTxPos = 0;
  389. uint8_t CurrRxPos = 0;
  390. /* Write out bytes to transmit until the start of the bytes to receive is met */
  391. while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
  392. {
  393. if (CurrTxPos < SPI_Multi_Params.TxBytes)
  394. ISPTarget_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
  395. else
  396. ISPTarget_SendByte(0);
  397. CurrTxPos++;
  398. }
  399. /* Transmit remaining bytes with padding as needed, read in response bytes */
  400. while (CurrRxPos < SPI_Multi_Params.RxBytes)
  401. {
  402. if (CurrTxPos < SPI_Multi_Params.TxBytes)
  403. Endpoint_Write_8(ISPTarget_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
  404. else
  405. Endpoint_Write_8(ISPTarget_ReceiveByte());
  406. /* Check to see if we have filled the endpoint bank and need to send the packet */
  407. if (!(Endpoint_IsReadWriteAllowed()))
  408. {
  409. Endpoint_ClearIN();
  410. Endpoint_WaitUntilReady();
  411. }
  412. CurrRxPos++;
  413. }
  414. Endpoint_Write_8(STATUS_CMD_OK);
  415. bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
  416. Endpoint_ClearIN();
  417. /* Ensure last packet is a short packet to terminate the transfer */
  418. if (IsEndpointFull)
  419. {
  420. Endpoint_WaitUntilReady();
  421. Endpoint_ClearIN();
  422. Endpoint_WaitUntilReady();
  423. }
  424. }
  425. /** Blocking delay for a given number of milliseconds. This provides a simple wrapper around
  426. * the avr-libc provided delay function, so that the delay function can be called with a
  427. * constant value (to prevent run-time floating point operations being required).
  428. *
  429. * \param[in] DelayMS Number of milliseconds to delay for
  430. */
  431. void ISPProtocol_DelayMS(uint8_t DelayMS)
  432. {
  433. while (DelayMS-- && TimeoutTicksRemaining)
  434. Delay_MS(1);
  435. }
  436. #endif