lpcprog.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /******************************************************************************
  2. Project: Portable command line ISP for NXP LPC1000 / LPC2000 family
  3. and Analog Devices ADUC70xx
  4. Filename: lpcprog.c
  5. Compiler: Microsoft VC 6/7, Microsoft VS2008, Microsoft VS2010,
  6. GCC Cygwin, GCC Linux, GCC ARM ELF
  7. Author: Martin Maurer (Martin.Maurer@clibb.de)
  8. Copyright: (c) Martin Maurer 2003-2011, All rights reserved
  9. Portions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.com
  10. This file is part of lpc21isp.
  11. lpc21isp is free software: you can redistribute it and/or modify
  12. it under the terms of the GNU Lesser General Public License as published by
  13. the Free Software Foundation, either version 3 of the License, or
  14. any later version.
  15. lpc21isp is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public License
  20. and GNU General Public License along with lpc21isp.
  21. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. // This file is for the Actual Programming of the LPC Chips
  24. #if defined(_WIN32)
  25. #if !defined __BORLANDC__
  26. #include "StdAfx.h"
  27. #endif
  28. #endif // defined(_WIN32)
  29. #include "lpc21isp.h"
  30. #ifdef LPC_SUPPORT
  31. #include "lpcprog.h"
  32. static const unsigned int SectorTable_210x[] =
  33. {
  34. 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
  35. 8192, 8192, 8192, 8192, 8192, 8192, 8192
  36. };
  37. static const unsigned int SectorTable_2103[] =
  38. {
  39. 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096
  40. };
  41. static const unsigned int SectorTable_2109[] =
  42. {
  43. 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192
  44. };
  45. static const unsigned int SectorTable_211x[] =
  46. {
  47. 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
  48. 8192, 8192, 8192, 8192, 8192, 8192, 8192,
  49. };
  50. static const unsigned int SectorTable_212x[] =
  51. {
  52. 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
  53. 65536, 65536, 8192, 8192, 8192, 8192, 8192, 8192, 8192
  54. };
  55. // Used for devices with 500K (LPC2138 and LPC2148) and
  56. // for devices with 504K (1 extra 4k block at the end)
  57. static const unsigned int SectorTable_213x[] =
  58. {
  59. 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096,
  60. 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768,
  61. 32768, 32768, 32768, 32768, 32768, 32768, 4096, 4096,
  62. 4096, 4096, 4096, 4096
  63. };
  64. // Used for LPC17xx devices
  65. static const unsigned int SectorTable_17xx[] =
  66. {
  67. 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096,
  68. 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096,
  69. 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768,
  70. 32768, 32768, 32768, 32768, 32768, 32768
  71. };
  72. static int unsigned SectorTable_RAM[] = { 65000 };
  73. static LPC_DEVICE_TYPE LPCtypes[] =
  74. {
  75. { 0, 0, 0, 0, 0, 0, 0, CHIP_VARIANT_NONE }, /* unknown */
  76. // id, name of product, flash size, ram size, total number of sector, max copy size, sector table, chip variant
  77. { 0x2500102B, "1102", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  78. { 0x041E502B, "1111.../101", 8, 2, 2, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  79. { 0x2516D02B, "1111.../102", 8, 2, 2, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  80. { 0x0416502B, "1111.../201", 8, 4, 2, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  81. { 0x2516902B, "1111.../202", 8, 4, 2, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  82. { 0x042D502B, "1112.../101", 16, 2, 4, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  83. { 0x2524D02B, "1112.../102", 16, 2, 4, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  84. { 0x0425502B, "1112.../201", 16, 4, 4, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  85. { 0x2524902B, "1112.../202", 16, 4, 4, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  86. { 0x0434502B, "1113.../201", 24, 4, 6, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  87. { 0x2532902B, "1113.../202", 24, 4, 6, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  88. { 0x0434102B, "1113.../301", 24, 8, 6, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  89. { 0x2532102B, "1113.../302", 24, 8, 6, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  90. { 0x0444502B, "1114.../201", 32, 4, 8, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  91. { 0x2540902B, "1114.../202", 32, 4, 8, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  92. { 0x1A40902B, "1114FN.../102", 32, 4, 8, 1024, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  93. { 0x0444102B, "1114.../301", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  94. { 0x2540102B, "1114.../302", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  95. { 0x1421102B, "11C12.../301", 16, 8, 4, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  96. { 0x1440102B, "11C14.../301", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  97. { 0x1431102B, "11C22.../301", 16, 8, 4, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  98. { 0x1430102B, "11C24.../301", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  99. { 0x0364002B, "1224.../101", 32, 8, 4, 2048, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  100. { 0x0364202B, "1224.../121", 48, 12, 32, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  101. { 0x0365002B, "1225.../301", 64, 16, 32, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  102. { 0x0365202B, "1225.../321", 80, 20, 32, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  103. { 0x0366002B, "1226", 96, 24, 32, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  104. { 0x0367002B, "1227", 128, 32, 32, 4096, SectorTable_17xx, CHIP_VARIANT_LPC11XX },
  105. { 0x2C42502B, "1311", 8, 4, 2, 1024, SectorTable_17xx, CHIP_VARIANT_LPC13XX },
  106. { 0x1816902B, "1311/01", 8, 4, 2, 1024, SectorTable_17xx, CHIP_VARIANT_LPC13XX },
  107. { 0x2C40102B, "1313", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC13XX },
  108. { 0x1830102B, "1313/01", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC13XX },
  109. { 0x3D01402B, "1342", 16, 4, 4, 1024, SectorTable_17xx, CHIP_VARIANT_LPC13XX },
  110. { 0x3D00002B, "1343", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC13XX },
  111. { 0x25001118, "1751", 32, 8, 8, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  112. { 0x25001121, "1752", 64, 16, 16, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  113. { 0x25011722, "1754", 128, 32, 18, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  114. { 0x25011723, "1756", 256, 32, 22, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  115. { 0x25013F37, "1758", 512, 64, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  116. { 0x25113737, "1759", 512, 64, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  117. { 0x26011922, "1764", 128, 32, 18, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  118. { 0x26013733, "1765", 256, 64, 22, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  119. { 0x26013F33, "1766", 256, 64, 22, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  120. { 0x26012837, "1767", 512, 64, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  121. { 0x26013F37, "1768", 512, 64, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  122. { 0x26113F37, "1769", 512, 64, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  123. { 0x27011132, "1774", 128, 40, 18, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  124. { 0x27191F43, "1776", 256, 80, 22, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  125. { 0x27193747, "1777", 512, 96, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  126. { 0x27193F47, "1778", 512, 96, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  127. { 0x281D1743, "1785", 256, 80, 22, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  128. { 0x281D1F43, "1786", 256, 80, 22, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  129. { 0x281D3747, "1787", 512, 96, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  130. { 0x281D3F47, "1788", 512, 96, 30, 4096, SectorTable_17xx, CHIP_VARIANT_LPC17XX },
  131. { 0x0004FF11, "2103", 32, 8, 8, 4096, SectorTable_2103, CHIP_VARIANT_LPC2XXX },
  132. { 0xFFF0FF12, "2104", 128, 16, 15, 8192, SectorTable_210x, CHIP_VARIANT_LPC2XXX },
  133. { 0xFFF0FF22, "2105", 128, 32, 15, 8192, SectorTable_210x, CHIP_VARIANT_LPC2XXX },
  134. { 0xFFF0FF32, "2106", 128, 64, 15, 8192, SectorTable_210x, CHIP_VARIANT_LPC2XXX },
  135. { 0x0201FF01, "2109", 64, 8, 8, 4096, SectorTable_2109, CHIP_VARIANT_LPC2XXX },
  136. { 0x0101FF12, "2114", 128, 16, 15, 8192, SectorTable_211x, CHIP_VARIANT_LPC2XXX },
  137. { 0x0201FF12, "2119", 128, 16, 15, 8192, SectorTable_211x, CHIP_VARIANT_LPC2XXX },
  138. { 0x0101FF13, "2124", 256, 16, 17, 8192, SectorTable_212x, CHIP_VARIANT_LPC2XXX },
  139. { 0x0201FF13, "2129", 256, 16, 17, 8192, SectorTable_212x, CHIP_VARIANT_LPC2XXX },
  140. { 0x0002FF01, "2131", 32, 8, 8, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  141. { 0x0002FF11, "2132", 64, 16, 9, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  142. { 0x0002FF12, "2134", 128, 16, 11, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  143. { 0x0002FF23, "2136", 256, 32, 15, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  144. { 0x0002FF25, "2138", 512, 32, 27, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  145. { 0x0402FF01, "2141", 32, 8, 8, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  146. { 0x0402FF11, "2142", 64, 16, 9, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  147. { 0x0402FF12, "2144", 128, 16, 11, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  148. { 0x0402FF23, "2146", 256, 40, 15, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  149. { 0x0402FF25, "2148", 512, 40, 27, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  150. { 0x0301FF13, "2194", 256, 16, 17, 8192, SectorTable_212x, CHIP_VARIANT_LPC2XXX },
  151. { 0x0301FF12, "2210", 0, 16, 0, 8192, SectorTable_211x, CHIP_VARIANT_LPC2XXX }, /* table is a "don't care" */
  152. { 0x0401FF12, "2212", 128, 16, 15, 8192, SectorTable_211x, CHIP_VARIANT_LPC2XXX },
  153. { 0x0601FF13, "2214", 256, 16, 17, 8192, SectorTable_212x, CHIP_VARIANT_LPC2XXX },
  154. /* "2290"; same id as the LPC2210 */
  155. { 0x0401FF13, "2292", 256, 16, 17, 8192, SectorTable_212x, CHIP_VARIANT_LPC2XXX },
  156. { 0x0501FF13, "2294", 256, 16, 17, 8192, SectorTable_212x, CHIP_VARIANT_LPC2XXX },
  157. { 0x00000000, "2361", 128, 34, 11, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  158. { 0x00000000, "2362", 128, 34, 11, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  159. { 0x0603FB02, "2364", 128, 34, 11, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX }, /* From UM10211 Rev. 01 -- 6 July 2007 */
  160. { 0x1600F902, "2364", 128, 34, 11, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  161. { 0x1600E823, "2365", 256, 58, 15, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  162. { 0x0603FB23, "2366", 256, 58, 15, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX }, /* From UM10211 Rev. 01 -- 6 July 2007 */
  163. { 0x1600F923, "2366", 256, 58, 15, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  164. { 0x1600E825, "2367", 512, 58, 15, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  165. { 0x0603FB25, "2368", 512, 58, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX }, /* From UM10211 Rev. 01 -- 6 July 2007 */
  166. { 0x1600F925, "2368", 512, 58, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  167. { 0x1700E825, "2377", 512, 58, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  168. { 0x0703FF25, "2378", 512, 58, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX }, /* From UM10211 Rev. 01 -- 6 July 2007 */
  169. { 0x1600FD25, "2378", 512, 58, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX }, /* From UM10211 Rev. 01 -- 29 October 2007 */
  170. { 0x1700FD25, "2378", 512, 58, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  171. { 0x1700FF35, "2387", 512, 98, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX }, /* From UM10211 Rev. 03 -- 25 August 2008 */
  172. { 0x1800F935, "2387", 512, 98, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  173. { 0x1800FF35, "2388", 512, 98, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  174. { 0x1500FF35, "2458", 512, 98, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  175. { 0x1600FF30, "2460", 0, 98, 0, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  176. { 0x1600FF35, "2468", 512, 98, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  177. { 0x1701FF30, "2470", 0, 98, 0, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX },
  178. { 0x1701FF35, "2478", 512, 98, 28, 4096, SectorTable_213x, CHIP_VARIANT_LPC2XXX }
  179. };
  180. /***************************** NXP Download *********************************/
  181. /** Download the file from the internal memory image to the NXP microcontroller.
  182. * This function is visible from outside if COMPILE_FOR_LPC21
  183. */
  184. static int SendAndVerify(ISP_ENVIRONMENT *IspEnvironment, const char *Command,
  185. char *AnswerBuffer, int AnswerLength)
  186. {
  187. unsigned long realsize;
  188. int cmdlen;
  189. SendComPort(IspEnvironment, Command);
  190. ReceiveComPort(IspEnvironment, AnswerBuffer, AnswerLength - 1, &realsize, 2, 5000);
  191. cmdlen = strlen(Command);
  192. return (strncmp(AnswerBuffer, Command, cmdlen) == 0
  193. && strcmp(AnswerBuffer + cmdlen, "0\r\n") == 0);
  194. }
  195. /***************************** NxpOutputErrorMessage ***********************/
  196. /** Given an error number find and print the appropriate error message.
  197. \param [in] ErrorNumber The number of the error.
  198. */
  199. #if defined COMPILE_FOR_LPC21
  200. #define NxpOutputErrorMessage(in) // Cleanly remove this feature from the embedded version !!
  201. #else
  202. static void NxpOutputErrorMessage(unsigned char ErrorNumber)
  203. {
  204. switch (ErrorNumber)
  205. {
  206. case 0:
  207. DebugPrintf(1, "CMD_SUCCESS\n");
  208. break;
  209. case 1:
  210. DebugPrintf(1, "INVALID_COMMAND\n");
  211. break;
  212. case 2:
  213. DebugPrintf(1, "SRC_ADDR_ERROR: Source address is not on word boundary.\n");
  214. break;
  215. case 3:
  216. DebugPrintf(1, "DST_ADDR_ERROR: Destination address is not on a correct boundary.\n");
  217. break;
  218. case 4:
  219. DebugPrintf(1, "SRC_ADDR_NOT_MAPPED: Source address is not mapped in the memory map.\n"
  220. " Count value is taken into consideration where applicable.\n");
  221. break;
  222. case 5:
  223. DebugPrintf(1, "DST_ADDR_NOT_MAPPED: Destination address is not mapped in the memory map.\n"
  224. " Count value is taken into consideration where applicable.\n");
  225. break;
  226. case 6:
  227. DebugPrintf(1, "COUNT_ERROR: Byte count is not multiple of 4 or is not a permitted value.\n");
  228. break;
  229. case 7:
  230. DebugPrintf(1, "INVALID_SECTOR: Sector number is invalid or end sector number is\n"
  231. " greater than start sector number.\n");
  232. break;
  233. case 8:
  234. DebugPrintf(1, "SECTOR_NOT_BLANK\n");
  235. break;
  236. case 9:
  237. DebugPrintf(1, "SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION:\n"
  238. "Command to prepare sector for write operation was not executed.\n");
  239. break;
  240. case 10:
  241. DebugPrintf(1, "COMPARE_ERROR: Source and destination data not equal.\n");
  242. break;
  243. case 11:
  244. DebugPrintf(1, "BUSY: Flash programming hardware interface is busy.\n");
  245. break;
  246. case 12:
  247. DebugPrintf(1, "PARAM_ERROR: Insufficient number of parameters or invalid parameter.\n");
  248. break;
  249. case 13:
  250. DebugPrintf(1, "ADDR_ERROR: Address is not on word boundary.\n");
  251. break;
  252. case 14:
  253. DebugPrintf(1, "ADDR_NOT_MAPPED: Address is not mapped in the memory map.\n"
  254. " Count value is taken in to consideration where applicable.\n");
  255. break;
  256. case 15:
  257. DebugPrintf(1, "CMD_LOCKED\n");
  258. break;
  259. case 16:
  260. DebugPrintf(1, "INVALID_CODE: Unlock code is invalid.\n");
  261. break;
  262. case 17:
  263. DebugPrintf(1, "INVALID_BAUD_RATE: Invalid baud rate setting.\n");
  264. break;
  265. case 18:
  266. DebugPrintf(1, "INVALID_STOP_BIT: Invalid stop bit setting.\n");
  267. break;
  268. case 19:
  269. DebugPrintf( 1, "CODE READ PROTECTION ENABLED\n");
  270. break;
  271. case 255:
  272. break;
  273. default:
  274. DebugPrintf(1, "unknown error %u\n", ErrorNumber);
  275. break;
  276. }
  277. //DebugPrintf(1, "error (%u), see NxpOutputErrorMessage() in lpc21isp.c for help \n\r", ErrorNumber);
  278. }
  279. #endif // !defined COMPILE_FOR_LPC21
  280. /***************************** GetAndReportErrorNumber ***************************/
  281. /** Find error number in string. This will normally be the string
  282. returned from the microcontroller.
  283. \param [in] Answer the buffer to search for the error number.
  284. \return the error number found, if no linefeed found before the end of the
  285. string an error value of 255 is returned. If a non-numeric value is found
  286. then it is printed to stdout and an error value of 255 is returned.
  287. */
  288. static unsigned char GetAndReportErrorNumber(const char *Answer)
  289. {
  290. unsigned char Result = 0xFF; // Error !!!
  291. unsigned int i = 0;
  292. while (1)
  293. {
  294. if (Answer[i] == 0x00)
  295. {
  296. break;
  297. }
  298. if (Answer[i] == 0x0a)
  299. {
  300. i++;
  301. if (Answer[i] < '0' || Answer[i] > '9')
  302. {
  303. DebugPrintf(1, "ErrorString: %s", &Answer[i]);
  304. break;
  305. }
  306. Result = (unsigned char) (atoi(&Answer[i]));
  307. break;
  308. }
  309. i++;
  310. }
  311. NxpOutputErrorMessage(Result);
  312. return Result;
  313. }
  314. int NxpDownload(ISP_ENVIRONMENT *IspEnvironment)
  315. {
  316. unsigned long realsize;
  317. char Answer[128];
  318. char ExpectedAnswer[128];
  319. char temp[128];
  320. /*const*/ char *strippedAnswer, *endPtr;
  321. int strippedsize;
  322. int nQuestionMarks;
  323. int found;
  324. unsigned long Sector;
  325. unsigned long SectorLength;
  326. unsigned long SectorStart, SectorOffset, SectorChunk;
  327. char tmpString[128];
  328. char uuencode_table[64];
  329. int Line;
  330. unsigned long tmpStringPos;
  331. unsigned long BlockOffset;
  332. unsigned long Block;
  333. unsigned long Pos;
  334. unsigned long CopyLength;
  335. int c,k=0,i;
  336. unsigned long ivt_CRC; // CRC over interrupt vector table
  337. unsigned long block_CRC;
  338. time_t tStartUpload=0, tDoneUpload=0;
  339. char tmp_string[64];
  340. char * cmdstr;
  341. #if !defined COMPILE_FOR_LPC21
  342. #if defined __BORLANDC__
  343. #define local_static static
  344. #else
  345. #define local_static
  346. #endif
  347. // char * cmdstr;
  348. int repeat = 0;
  349. // Puffer for data to resend after "RESEND\r\n" Target responce
  350. local_static char sendbuf0[128];
  351. local_static char sendbuf1[128];
  352. local_static char sendbuf2[128];
  353. local_static char sendbuf3[128];
  354. local_static char sendbuf4[128];
  355. local_static char sendbuf5[128];
  356. local_static char sendbuf6[128];
  357. local_static char sendbuf7[128];
  358. local_static char sendbuf8[128];
  359. local_static char sendbuf9[128];
  360. local_static char sendbuf10[128];
  361. local_static char sendbuf11[128];
  362. local_static char sendbuf12[128];
  363. local_static char sendbuf13[128];
  364. local_static char sendbuf14[128];
  365. local_static char sendbuf15[128];
  366. local_static char sendbuf16[128];
  367. local_static char sendbuf17[128];
  368. local_static char sendbuf18[128];
  369. local_static char sendbuf19[128];
  370. char * sendbuf[20] = { sendbuf0, sendbuf1, sendbuf2, sendbuf3, sendbuf4,
  371. sendbuf5, sendbuf6, sendbuf7, sendbuf8, sendbuf9,
  372. sendbuf10, sendbuf11, sendbuf12, sendbuf13, sendbuf14,
  373. sendbuf15, sendbuf16, sendbuf17, sendbuf18, sendbuf19};
  374. #endif
  375. DebugPrintf(2, "Synchronizing (ESC to abort)");
  376. PrepareKeyboardTtySettings();
  377. #if defined INTEGRATED_IN_WIN_APP
  378. if (IspEnvironment->NoSync)
  379. {
  380. found = 1;
  381. }
  382. else
  383. #endif
  384. {
  385. for (nQuestionMarks = found = 0; !found && nQuestionMarks < IspEnvironment->nQuestionMarks; nQuestionMarks++)
  386. {
  387. #if defined INTEGRATED_IN_WIN_APP
  388. // allow calling application to abort when syncing takes too long
  389. if (!AppSyncing(nQuestionMarks))
  390. {
  391. return (USER_ABORT_SYNC);
  392. }
  393. #else
  394. #ifndef Exclude_kbhit
  395. if (kbhit())
  396. {
  397. if (getch() == 0x1b)
  398. {
  399. ResetKeyboardTtySettings();
  400. DebugPrintf(2, "\nUser aborted during synchronisation\n");
  401. return (USER_ABORT_SYNC);
  402. }
  403. }
  404. #endif
  405. #endif
  406. DebugPrintf(2, ".");
  407. SendComPort(IspEnvironment, "?");
  408. memset(Answer,0,sizeof(Answer));
  409. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,100);
  410. strippedAnswer = Answer;
  411. strippedsize = realsize;
  412. while ((strippedsize > 0) && ((*strippedAnswer == '?') || (*strippedAnswer == 0)))
  413. {
  414. strippedAnswer++;
  415. strippedsize--;
  416. }
  417. sprintf(tmp_string, "StrippedAnswer(Length=%d): '", strippedsize);
  418. DumpString(3, strippedAnswer, strippedsize, tmp_string);
  419. tStartUpload = time(NULL);
  420. if (strcmp(strippedAnswer, "Synchronized\r\n") == 0)
  421. {
  422. found = 1;
  423. }
  424. #if !defined COMPILE_FOR_LPC21
  425. else
  426. {
  427. ResetTarget(IspEnvironment, PROGRAM_MODE);
  428. }
  429. #endif
  430. }
  431. }
  432. ResetKeyboardTtySettings();
  433. if (!found)
  434. {
  435. DebugPrintf(1, " no answer on '?'\n");
  436. return (NO_ANSWER_QM);
  437. }
  438. #if defined INTEGRATED_IN_WIN_APP
  439. AppSyncing(-1); // flag syncing done
  440. #endif
  441. DebugPrintf(2, " OK\n");
  442. SendComPort(IspEnvironment, "Synchronized\n");
  443. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer) - 1, &realsize, 2, 1000);
  444. if ((strcmp(Answer, "Synchronized\r\nOK\r\n") != 0) && (strcmp(Answer, "Synchronized\rOK\r\n") != 0) &&
  445. (strcmp(Answer, "Synchronized\nOK\r\n") != 0))
  446. {
  447. DebugPrintf(1, "No answer on 'Synchronized'\n");
  448. return (NO_ANSWER_SYNC);
  449. }
  450. DebugPrintf(3, "Synchronized 1\n");
  451. DebugPrintf(3, "Setting oscillator\n");
  452. sprintf(temp, "%s\n", IspEnvironment->StringOscillator);
  453. SendComPort(IspEnvironment, temp);
  454. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2, 1000);
  455. sprintf(temp, "%s\nOK\r\n", IspEnvironment->StringOscillator);
  456. if (strcmp(Answer, temp) != 0)
  457. {
  458. DebugPrintf(1, "No answer on Oscillator-Command\n");
  459. return (NO_ANSWER_OSC);
  460. }
  461. DebugPrintf(3, "Unlock\n");
  462. cmdstr = "U 23130\n";
  463. if (!SendAndVerify(IspEnvironment, cmdstr, Answer, sizeof Answer))
  464. {
  465. DebugPrintf(1, "Unlock-Command:\n");
  466. return (UNLOCK_ERROR + GetAndReportErrorNumber(Answer));
  467. }
  468. DebugPrintf(2, "Read bootcode version: ");
  469. cmdstr = "K\n";
  470. SendComPort(IspEnvironment, cmdstr);
  471. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 4,5000);
  472. if (strncmp(Answer, cmdstr, strlen(cmdstr)) != 0)
  473. {
  474. DebugPrintf(1, "no answer on Read Boot Code Version\n");
  475. return (NO_ANSWER_RBV);
  476. }
  477. if (strncmp(Answer + strlen(cmdstr), "0\r\n", 3) == 0)
  478. {
  479. strippedAnswer = Answer + strlen(cmdstr) + 3;
  480. /*
  481. int maj, min, build;
  482. if (sscanf(strippedAnswer, "%d %d %d", &build, &min, &maj) == 2) {
  483. maj = min;
  484. min = build;
  485. build = 0;
  486. } // if
  487. DebugPrintf(2, "%d.%d.%d\n", maj, min, build);
  488. */
  489. DebugPrintf(2, strippedAnswer);
  490. }
  491. else
  492. {
  493. DebugPrintf(2, "unknown\n");
  494. }
  495. DebugPrintf(2, "Read part ID: ");
  496. cmdstr = "J\n";
  497. SendComPort(IspEnvironment, cmdstr);
  498. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 3,5000);
  499. if (strncmp(Answer, cmdstr, strlen(cmdstr)) != 0)
  500. {
  501. DebugPrintf(1, "no answer on Read Part Id\n");
  502. return (NO_ANSWER_RPID);
  503. }
  504. strippedAnswer = (strncmp(Answer, "J\n0\r\n", 5) == 0) ? Answer + 5 : Answer;
  505. Pos = strtoul(strippedAnswer, &endPtr, 10);
  506. *endPtr = '\0'; /* delete \r\n */
  507. for (i = sizeof LPCtypes / sizeof LPCtypes[0] - 1; i > 0 && LPCtypes[i].id != Pos; i--)
  508. /* nothing */;
  509. IspEnvironment->DetectedDevice = i;
  510. if (IspEnvironment->DetectedDevice == 0) {
  511. DebugPrintf(2, "unknown");
  512. }
  513. else {
  514. DebugPrintf(2, "LPC%s, %d kiB ROM / %d kiB SRAM",
  515. LPCtypes[IspEnvironment->DetectedDevice].Product,
  516. LPCtypes[IspEnvironment->DetectedDevice].FlashSize,
  517. LPCtypes[IspEnvironment->DetectedDevice].RAMSize);
  518. }
  519. DebugPrintf(2, " (0x%X)\n", Pos);//strippedAnswer);
  520. if (!IspEnvironment->DetectOnly)
  521. {
  522. // Build up uuencode table
  523. uuencode_table[0] = 0x60; // 0x20 is translated to 0x60 !
  524. for (i = 1; i < 64; i++)
  525. {
  526. uuencode_table[i] = (char)(0x20 + i);
  527. }
  528. if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC2XXX)
  529. {
  530. // Patch 0x14, otherwise it is not running and jumps to boot mode
  531. ivt_CRC = 0;
  532. // Clear the vector at 0x14 so it doesn't affect the checksum:
  533. for (i = 0; i < 4; i++)
  534. {
  535. IspEnvironment->BinaryContent[i + 0x14] = 0;
  536. }
  537. // Calculate a native checksum of the little endian vector table:
  538. for (i = 0; i < (4 * 8);) {
  539. ivt_CRC += IspEnvironment->BinaryContent[i++];
  540. ivt_CRC += IspEnvironment->BinaryContent[i++] << 8;
  541. ivt_CRC += IspEnvironment->BinaryContent[i++] << 16;
  542. ivt_CRC += IspEnvironment->BinaryContent[i++] << 24;
  543. }
  544. /* Negate the result and place in the vector at 0x14 as little endian
  545. * again. The resulting vector table should checksum to 0. */
  546. ivt_CRC = (unsigned long) (0 - ivt_CRC);
  547. for (i = 0; i < 4; i++)
  548. {
  549. IspEnvironment->BinaryContent[i + 0x14] = (unsigned char)(ivt_CRC >> (8 * i));
  550. }
  551. DebugPrintf(3, "Position 0x14 patched: ivt_CRC = 0x%08lX\n", ivt_CRC);
  552. }
  553. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC17XX ||
  554. LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC13XX ||
  555. LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC11XX)
  556. {
  557. // Patch 0x1C, otherwise it is not running and jumps to boot mode
  558. ivt_CRC = 0;
  559. // Clear the vector at 0x1C so it doesn't affect the checksum:
  560. for (i = 0; i < 4; i++)
  561. {
  562. IspEnvironment->BinaryContent[i + 0x1C] = 0;
  563. }
  564. // Calculate a native checksum of the little endian vector table:
  565. for (i = 0; i < (4 * 8);) {
  566. ivt_CRC += IspEnvironment->BinaryContent[i++];
  567. ivt_CRC += IspEnvironment->BinaryContent[i++] << 8;
  568. ivt_CRC += IspEnvironment->BinaryContent[i++] << 16;
  569. ivt_CRC += IspEnvironment->BinaryContent[i++] << 24;
  570. }
  571. /* Negate the result and place in the vector at 0x1C as little endian
  572. * again. The resulting vector table should checksum to 0. */
  573. ivt_CRC = (unsigned long) (0 - ivt_CRC);
  574. for (i = 0; i < 4; i++)
  575. {
  576. IspEnvironment->BinaryContent[i + 0x1C] = (unsigned char)(ivt_CRC >> (8 * i));
  577. }
  578. DebugPrintf(3, "Position 0x1C patched: ivt_CRC = 0x%08lX\n", ivt_CRC);
  579. }
  580. else
  581. {
  582. DebugPrintf(1, "Internal error: wrong chip variant %d (detected device %d)\n", LPCtypes[IspEnvironment->DetectedDevice].ChipVariant, IspEnvironment->DetectedDevice);
  583. exit(1);
  584. }
  585. }
  586. #if 0
  587. DebugPrintf(2, "Read Unique ID:\n");
  588. cmdstr = "N\n";
  589. SendComPort(IspEnvironment, cmdstr);
  590. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 5,5000);
  591. if (strncmp(Answer, cmdstr, strlen(cmdstr)) != 0)
  592. {
  593. DebugPrintf(1, "no answer on Read Unique ID\n");
  594. return (NO_ANSWER_RBV);
  595. }
  596. if (strncmp(Answer + strlen(cmdstr), "0\r\n", 3) == 0)
  597. {
  598. strippedAnswer = Answer + strlen(cmdstr) + 3;
  599. DebugPrintf(2, strippedAnswer);
  600. }
  601. else
  602. {
  603. DebugPrintf(2, "unknown\n");
  604. }
  605. #endif // 0
  606. /* In case of a download to RAM, use full RAM for downloading
  607. * set the flash parameters to full RAM also.
  608. * This makes sure that all code is downloaded as one big sector
  609. */
  610. if (IspEnvironment->BinaryOffset >= ReturnValueLpcRamStart(IspEnvironment))
  611. {
  612. LPCtypes[IspEnvironment->DetectedDevice].FlashSectors = 1;
  613. LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize = LPCtypes[IspEnvironment->DetectedDevice].RAMSize*1024 - (ReturnValueLpcRamBase(IspEnvironment) - ReturnValueLpcRamStart(IspEnvironment));
  614. LPCtypes[IspEnvironment->DetectedDevice].SectorTable = SectorTable_RAM;
  615. SectorTable_RAM[0] = LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize;
  616. }
  617. if (IspEnvironment->DetectOnly)
  618. return (0);
  619. // Start with sector 1 and go upward... Sector 0 containing the interrupt vectors
  620. // will be loaded last, since it contains a checksum and device will re-enter
  621. // bootloader mode as long as this checksum is invalid.
  622. DebugPrintf(2, "Will start programming at Sector 1 if possible, and conclude with Sector 0 to ensure that checksum is written last.\n");
  623. if (LPCtypes[IspEnvironment->DetectedDevice].SectorTable[0] >= IspEnvironment->BinaryLength)
  624. {
  625. Sector = 0;
  626. SectorStart = 0;
  627. }
  628. else
  629. {
  630. SectorStart = LPCtypes[IspEnvironment->DetectedDevice].SectorTable[0];
  631. Sector = 1;
  632. }
  633. if (IspEnvironment->WipeDevice == 1)
  634. {
  635. DebugPrintf(2, "Wiping Device. ");
  636. sprintf(tmpString, "P %d %d\n", 0, LPCtypes[IspEnvironment->DetectedDevice].FlashSectors-1);
  637. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  638. {
  639. DebugPrintf(1, "Wrong answer on Prepare-Command\n");
  640. return (WRONG_ANSWER_PREP + GetAndReportErrorNumber(Answer));
  641. }
  642. sprintf(tmpString, "E %d %d\n", 0, LPCtypes[IspEnvironment->DetectedDevice].FlashSectors-1);
  643. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  644. {
  645. DebugPrintf(1, "Wrong answer on Erase-Command\n");
  646. return (WRONG_ANSWER_ERAS + GetAndReportErrorNumber(Answer));
  647. }
  648. DebugPrintf(2, "OK \n");
  649. }
  650. else{
  651. //no wiping requested: erasing sector 0 first
  652. DebugPrintf(2, "Erasing sector 0 first, to invalidate checksum. ");
  653. sprintf(tmpString, "P %d %d\n", 0, 0);
  654. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  655. {
  656. DebugPrintf(1, "Wrong answer on Prepare-Command\n");
  657. return (WRONG_ANSWER_PREP + GetAndReportErrorNumber(Answer));
  658. }
  659. sprintf(tmpString, "E %d %d\n", 0, 0);
  660. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  661. {
  662. DebugPrintf(1, "Wrong answer on Erase-Command\n");
  663. return (WRONG_ANSWER_ERAS + GetAndReportErrorNumber(Answer));
  664. }
  665. DebugPrintf(2, "OK \n");
  666. }
  667. while (1)
  668. {
  669. if (Sector >= LPCtypes[IspEnvironment->DetectedDevice].FlashSectors)
  670. {
  671. DebugPrintf(1, "Program too large; running out of Flash sectors.\n");
  672. return (PROGRAM_TOO_LARGE);
  673. }
  674. DebugPrintf(2, "Sector %ld: ", Sector);
  675. fflush(stdout);
  676. if (IspEnvironment->BinaryOffset < ReturnValueLpcRamStart(IspEnvironment)) // Skip Erase when running from RAM
  677. {
  678. sprintf(tmpString, "P %ld %ld\n", Sector, Sector);
  679. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  680. {
  681. DebugPrintf(1, "Wrong answer on Prepare-Command (1) (Sector %ld)\n", Sector);
  682. return (WRONG_ANSWER_PREP + GetAndReportErrorNumber(Answer));
  683. }
  684. DebugPrintf(2, ".");
  685. fflush(stdout);
  686. if (IspEnvironment->WipeDevice == 0 && (Sector!=0)) //Sector 0 already erased
  687. {
  688. sprintf(tmpString, "E %ld %ld\n", Sector, Sector);
  689. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  690. {
  691. DebugPrintf(1, "Wrong answer on Erase-Command (Sector %ld)\n", Sector);
  692. return (WRONG_ANSWER_ERAS + GetAndReportErrorNumber(Answer));
  693. }
  694. DebugPrintf(2, ".");
  695. fflush(stdout);
  696. }
  697. }
  698. SectorLength = LPCtypes[IspEnvironment->DetectedDevice].SectorTable[Sector];
  699. if (SectorLength > IspEnvironment->BinaryLength - SectorStart)
  700. {
  701. SectorLength = IspEnvironment->BinaryLength - SectorStart;
  702. }
  703. for (SectorOffset = 0; SectorOffset < SectorLength; SectorOffset += SectorChunk)
  704. {
  705. if (SectorOffset > 0)
  706. {
  707. // Add a visible marker between segments in a sector
  708. DebugPrintf(2, "|"); /* means: partial segment copied */
  709. fflush(stdout);
  710. }
  711. // If the Flash ROM sector size is bigger than the number of bytes
  712. // we can copy from RAM to Flash, we must "chop up" the sector and
  713. // copy these individually.
  714. // This is especially needed in the case where a Flash sector is
  715. // bigger than the amount of SRAM.
  716. SectorChunk = SectorLength - SectorOffset;
  717. if (SectorChunk > (unsigned)LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize)
  718. {
  719. SectorChunk = LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize;
  720. }
  721. // Write multiple of 45 * 4 Byte blocks to RAM, but copy maximum of on sector to Flash
  722. // In worst case we transfer up to 180 byte to much to RAM
  723. // but then we can always use full 45 byte blocks and length is multiple of 4
  724. CopyLength = SectorChunk;
  725. if ((CopyLength % (45 * 4)) != 0)
  726. {
  727. CopyLength += ((45 * 4) - (CopyLength % (45 * 4)));
  728. }
  729. sprintf(tmpString, "W %ld %ld\n", ReturnValueLpcRamBase(IspEnvironment), CopyLength);
  730. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  731. {
  732. DebugPrintf(1, "Wrong answer on Write-Command\n");
  733. return (WRONG_ANSWER_WRIT + GetAndReportErrorNumber(Answer));
  734. }
  735. DebugPrintf(2, ".");
  736. fflush(stdout);
  737. block_CRC = 0;
  738. Line = 0;
  739. // Transfer blocks of 45 * 4 bytes to RAM
  740. for (Pos = SectorStart + SectorOffset; (Pos < SectorStart + SectorOffset + CopyLength) && (Pos < IspEnvironment->BinaryLength); Pos += (45 * 4))
  741. {
  742. for (Block = 0; Block < 4; Block++) // Each block 45 bytes
  743. {
  744. DebugPrintf(2, ".");
  745. fflush(stdout);
  746. #if defined INTEGRATED_IN_WIN_APP
  747. // inform the calling application about having written another chuck of data
  748. AppWritten(45);
  749. #endif
  750. // Uuencode one 45 byte block
  751. tmpStringPos = 0;
  752. #if !defined COMPILE_FOR_LPC21
  753. sendbuf[Line][tmpStringPos++] = (char)(' ' + 45); // Encode Length of block
  754. #else
  755. tmpString[tmpStringPos++] = (char)(' ' + 45); // Encode Length of block
  756. #endif
  757. for (BlockOffset = 0; BlockOffset < 45; BlockOffset++)
  758. {
  759. if (IspEnvironment->BinaryOffset < ReturnValueLpcRamStart(IspEnvironment))
  760. { // Flash: use full memory
  761. c = IspEnvironment->BinaryContent[Pos + Block * 45 + BlockOffset];
  762. }
  763. else
  764. { // RAM: Skip first 0x200 bytes, these are used by the download program in LPC21xx
  765. c = IspEnvironment->BinaryContent[Pos + Block * 45 + BlockOffset + 0x200];
  766. }
  767. block_CRC += c;
  768. k = (k << 8) + (c & 255);
  769. if ((BlockOffset % 3) == 2) // Collecting always 3 Bytes, then do processing in 4 Bytes
  770. {
  771. #if !defined COMPILE_FOR_LPC21
  772. sendbuf[Line][tmpStringPos++] = uuencode_table[(k >> 18) & 63];
  773. sendbuf[Line][tmpStringPos++] = uuencode_table[(k >> 12) & 63];
  774. sendbuf[Line][tmpStringPos++] = uuencode_table[(k >> 6) & 63];
  775. sendbuf[Line][tmpStringPos++] = uuencode_table[ k & 63];
  776. #else
  777. tmpString[tmpStringPos++] = uuencode_table[(k >> 18) & 63];
  778. tmpString[tmpStringPos++] = uuencode_table[(k >> 12) & 63];
  779. tmpString[tmpStringPos++] = uuencode_table[(k >> 6) & 63];
  780. tmpString[tmpStringPos++] = uuencode_table[ k & 63];
  781. #endif
  782. }
  783. }
  784. #if !defined COMPILE_FOR_LPC21
  785. sendbuf[Line][tmpStringPos++] = '\n';
  786. sendbuf[Line][tmpStringPos++] = 0;
  787. SendComPort(IspEnvironment, sendbuf[Line]);
  788. // receive only for debug proposes
  789. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,5000);
  790. #else
  791. tmpString[tmpStringPos++] = '\n';
  792. tmpString[tmpStringPos++] = 0;
  793. SendComPort(IspEnvironment, tmpString);
  794. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,5000);
  795. if (strncmp(Answer, tmpString, tmpStringPos) != 0)
  796. {
  797. DebugPrintf(1, "Error on writing data (1)\n");
  798. return (ERROR_WRITE_DATA);
  799. }
  800. #endif
  801. Line++;
  802. DebugPrintf(3, "Line = %d\n", Line);
  803. if (Line == 20)
  804. {
  805. #if !defined COMPILE_FOR_LPC21
  806. for (repeat = 0; repeat < 3; repeat++)
  807. {
  808. // printf("block_CRC = %ld\n", block_CRC);
  809. sprintf(tmpString, "%ld\n", block_CRC);
  810. SendComPort(IspEnvironment, tmpString);
  811. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,5000);
  812. sprintf(tmpString, "%ld\nOK\r\n", block_CRC);
  813. if (strcmp(Answer, tmpString) != 0)
  814. {
  815. for (i = 0; i < Line; i++)
  816. {
  817. SendComPort(IspEnvironment, sendbuf[i]);
  818. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,5000);
  819. }
  820. }
  821. else
  822. break;
  823. }
  824. if (repeat >= 3)
  825. {
  826. DebugPrintf(1, "Error on writing block_CRC (1)\n");
  827. return (ERROR_WRITE_CRC);
  828. }
  829. #else
  830. // printf("block_CRC = %ld\n", block_CRC);
  831. sprintf(tmpString, "%ld\r\n", block_CRC);
  832. SendComPort(IspEnvironment, tmpString);
  833. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,5000);
  834. sprintf(tmpString, "%ld\r\nOK\r\n", block_CRC);
  835. if (strcmp(Answer, tmpString) != 0)
  836. {
  837. DebugPrintf(1, "Error on writing block_CRC (2)\n");
  838. return (ERROR_WRITE_CRC);
  839. }
  840. #endif
  841. Line = 0;
  842. block_CRC = 0;
  843. }
  844. }
  845. }
  846. if (Line != 0)
  847. {
  848. #if !defined COMPILE_FOR_LPC21
  849. for (repeat = 0; repeat < 3; repeat++)
  850. {
  851. sprintf(tmpString, "%ld\n", block_CRC);
  852. SendComPort(IspEnvironment, tmpString);
  853. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,5000);
  854. sprintf(tmpString, "%ld\nOK\r\n", block_CRC);
  855. if (strcmp(Answer, tmpString) != 0)
  856. {
  857. for (i = 0; i < Line; i++)
  858. {
  859. SendComPort(IspEnvironment, sendbuf[i]);
  860. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,5000);
  861. }
  862. }
  863. else
  864. break;
  865. }
  866. if (repeat >= 3)
  867. {
  868. DebugPrintf(1, "Error on writing block_CRC (3)\n");
  869. return (ERROR_WRITE_CRC2);
  870. }
  871. #else
  872. sprintf(tmpString, "%ld\r\n", block_CRC);
  873. SendComPort(IspEnvironment, tmpString);
  874. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,5000);
  875. sprintf(tmpString, "%ld\r\nOK\r\n", block_CRC);
  876. if (strcmp(Answer, tmpString) != 0)
  877. {
  878. DebugPrintf(1, "Error on writing block_CRC (4)\n");
  879. return (ERROR_WRITE_CRC2);
  880. }
  881. #endif
  882. }
  883. if (IspEnvironment->BinaryOffset < ReturnValueLpcRamStart(IspEnvironment))
  884. {
  885. // Prepare command must be repeated before every write
  886. sprintf(tmpString, "P %ld %ld\n", Sector, Sector);
  887. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  888. {
  889. DebugPrintf(1, "Wrong answer on Prepare-Command (2) (Sector %ld)\n", Sector);
  890. return (WRONG_ANSWER_PREP2 + GetAndReportErrorNumber(Answer));
  891. }
  892. // Round CopyLength up to one of the following values: 512, 1024,
  893. // 4096, 8192; but do not exceed the maximum copy size (usually
  894. // 8192, but chip-dependent)
  895. if (CopyLength < 512)
  896. {
  897. CopyLength = 512;
  898. }
  899. else if (SectorLength < 1024)
  900. {
  901. CopyLength = 1024;
  902. }
  903. else if (SectorLength < 4096)
  904. {
  905. CopyLength = 4096;
  906. }
  907. else
  908. {
  909. CopyLength = 8192;
  910. }
  911. if (CopyLength > (unsigned)LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize)
  912. {
  913. CopyLength = LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize;
  914. }
  915. sprintf(tmpString, "C %ld %ld %ld\n", SectorStart + SectorOffset, ReturnValueLpcRamBase(IspEnvironment), CopyLength);
  916. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  917. {
  918. DebugPrintf(1, "Wrong answer on Copy-Command\n");
  919. return (WRONG_ANSWER_COPY + GetAndReportErrorNumber(Answer));
  920. }
  921. if (IspEnvironment->Verify)
  922. {
  923. //Avoid compare first 64 bytes.
  924. //Because first 64 bytes are re-mapped to flash boot sector,
  925. //and the compare result may not be correct.
  926. if (SectorStart + SectorOffset<64)
  927. {
  928. sprintf(tmpString, "M %d %ld %ld\n", 64, ReturnValueLpcRamBase(IspEnvironment) + (64 - SectorStart - SectorOffset), CopyLength-(64 - SectorStart - SectorOffset));
  929. }
  930. else
  931. {
  932. sprintf(tmpString, "M %ld %ld %ld\n", SectorStart + SectorOffset, ReturnValueLpcRamBase(IspEnvironment), CopyLength);
  933. }
  934. if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
  935. {
  936. DebugPrintf(1, "Wrong answer on Compare-Command\n");
  937. return (WRONG_ANSWER_COPY + GetAndReportErrorNumber(Answer));
  938. }
  939. }
  940. }
  941. }
  942. DebugPrintf(2, "\n");
  943. fflush(stdout);
  944. if ((SectorStart + SectorLength) >= IspEnvironment->BinaryLength && Sector!=0)
  945. {
  946. Sector = 0;
  947. SectorStart = 0;
  948. }
  949. else if (Sector == 0) {
  950. break;
  951. }
  952. else {
  953. SectorStart += LPCtypes[IspEnvironment->DetectedDevice].SectorTable[Sector];
  954. Sector++;
  955. }
  956. }
  957. tDoneUpload = time(NULL);
  958. if (IspEnvironment->Verify)
  959. DebugPrintf(2, "Download Finished and Verified correct... taking %d seconds\n", tDoneUpload - tStartUpload);
  960. else
  961. DebugPrintf(2, "Download Finished... taking %d seconds\n", tDoneUpload - tStartUpload);
  962. if(IspEnvironment->DoNotStart == 0)
  963. {
  964. DebugPrintf(2, "Now launching the brand new code\n");
  965. fflush(stdout);
  966. if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC2XXX)
  967. {
  968. sprintf(tmpString, "G %ld A\n", IspEnvironment->StartAddress);
  969. }
  970. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC17XX ||
  971. LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC13XX ||
  972. LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC11XX)
  973. {
  974. sprintf(tmpString, "G %ld T\n", IspEnvironment->StartAddress & ~1);
  975. }
  976. else
  977. {
  978. printf("Internal Error %s %d\n", __FILE__, __LINE__);
  979. exit(1);
  980. }
  981. SendComPort(IspEnvironment, tmpString); //goto 0 : run this fresh new downloaded code code
  982. if (IspEnvironment->BinaryOffset < ReturnValueLpcRamStart(IspEnvironment))
  983. { // Skip response on G command - show response on Terminal instead
  984. ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2, 5000);
  985. /* the reply string is frequently terminated with a -1 (EOF) because the
  986. * connection gets broken; zero-terminate the string ourselves
  987. */
  988. while (realsize > 0 && ((signed char) Answer[(int)realsize - 1]) < 0)
  989. realsize--;
  990. Answer[(int)realsize] = '\0';
  991. /* Better to check only the first 9 chars instead of complete receive buffer,
  992. * because the answer can contain the output by the started programm
  993. */
  994. if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC2XXX)
  995. {
  996. sprintf(ExpectedAnswer, "G %ld A\n0", IspEnvironment->StartAddress);
  997. }
  998. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC17XX ||
  999. LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC13XX ||
  1000. LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC11XX)
  1001. {
  1002. sprintf(ExpectedAnswer, "G %ld T\n0", IspEnvironment->StartAddress & ~1);
  1003. }
  1004. else
  1005. {
  1006. printf("Internal Error %s %d\n", __FILE__, __LINE__);
  1007. exit(1);
  1008. }
  1009. if (realsize == 0 || strncmp((const char *)Answer, /*cmdstr*/ExpectedAnswer, strlen(/*cmdstr*/ExpectedAnswer)) != 0)
  1010. {
  1011. DebugPrintf(2, "Failed to run the new downloaded code: ");
  1012. return (FAILED_RUN + GetAndReportErrorNumber(Answer));
  1013. }
  1014. }
  1015. fflush(stdout);
  1016. }
  1017. return (0);
  1018. }
  1019. #endif // LPC_SUPPORT
  1020. unsigned long ReturnValueLpcRamStart(ISP_ENVIRONMENT *IspEnvironment)
  1021. {
  1022. if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC2XXX)
  1023. {
  1024. return LPC_RAMSTART_LPC2XXX;
  1025. }
  1026. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC17XX)
  1027. {
  1028. return LPC_RAMSTART_LPC17XX;
  1029. }
  1030. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC13XX)
  1031. {
  1032. return LPC_RAMSTART_LPC13XX;
  1033. }
  1034. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC11XX)
  1035. {
  1036. return LPC_RAMSTART_LPC11XX;
  1037. }
  1038. printf("Error in ReturnValueLpcRamStart (%d)\n", LPCtypes[IspEnvironment->DetectedDevice].ChipVariant);
  1039. exit(1);
  1040. }
  1041. unsigned long ReturnValueLpcRamBase(ISP_ENVIRONMENT *IspEnvironment)
  1042. {
  1043. if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC2XXX)
  1044. {
  1045. return LPC_RAMBASE_LPC2XXX;
  1046. }
  1047. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC17XX)
  1048. {
  1049. return LPC_RAMBASE_LPC17XX;
  1050. }
  1051. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC13XX)
  1052. {
  1053. return LPC_RAMBASE_LPC13XX;
  1054. }
  1055. else if(LPCtypes[IspEnvironment->DetectedDevice].ChipVariant == CHIP_VARIANT_LPC11XX)
  1056. {
  1057. return LPC_RAMBASE_LPC11XX;
  1058. }
  1059. printf("Error in ReturnValueLpcRamBase (%d)\n", LPCtypes[IspEnvironment->DetectedDevice].ChipVariant);
  1060. exit(1);
  1061. }