lpc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <serial.h>
  6. #include <uucode.h>
  7. #include <lpc.h>
  8. const struct lpc_part lpc_parts[] = {
  9. {"LPC1111/101", 0x041e502b, 2, 8},
  10. {"LPC1111/101 or 102", 0x2516d02b, 2, 8},
  11. {"LPC1111/201", 0x0416502b, 4, 8},
  12. {"LPC1111/201 or 202", 0x2516902b, 4, 8},
  13. {"LPC1112/101", 0x042d502b, 2, 16},
  14. {"LPC1112/101 or 102", 0x2524d02b, 2, 16},
  15. {"LPC1112/201", 0x0425502b, 4, 16},
  16. {"LPC1112/201 or 202", 0x2524902b, 4, 16},
  17. {"LPC1113/201", 0x0434502b, 4, 24},
  18. {"LPC1113/201 or 202", 0x2532902b, 4, 24},
  19. {"LPC1113/301", 0x0434102b, 8, 24},
  20. {"LPC1113/301 or 302", 0x2532102b, 8, 24},
  21. {"LPC1114/201", 0x0444502b, 4, 32},
  22. {"LPC1114/201 or 202", 0x2540902b, 4, 32},
  23. {"LPC1114/301", 0x0444102b, 8, 32},
  24. {"LPC1114/301 or 302", 0x2540102b, 8, 32},
  25. {"LPC11C12/301", 0x1421102b, 8, 16},
  26. {"LPC11C14/301", 0x1440102b, 8, 32},
  27. {"LPC11C22/301", 0x1431102b, 8, 16},
  28. {"LPC11C24/301", 0x1430102b, 8, 32},
  29. {NULL, 0, 0, 0}
  30. };
  31. const struct sprog_family lpc_family = {
  32. .setup = (void*(*)(struct serial_device*)) lpc_setup,
  33. .init = (void(*)(void*)) lpc_init,
  34. .exec = (void(*)(void*, const struct sprog_data*)) lpc_exec,
  35. .write = (void(*)(void*, const struct sprog_data*)) lpc_write,
  36. .close = (void(*)(void*)) lpc_close
  37. };
  38. void lpc_ispmode(struct lpc_device *dev, int state);
  39. void lpc_reset(struct lpc_device *dev, int isp);
  40. int lpc_scanf(struct lpc_device *dev, const char *text, ...);
  41. void lpc_vprintf(struct lpc_device *dev, const char *text, va_list l);
  42. void lpc_printf(struct lpc_device *dev, const char *text, ...);
  43. int lpc_getline(struct lpc_device *dev, char *buf);
  44. int lpc_command(struct lpc_device *dev, const char *text, ...);
  45. int lpc_await_reply(struct lpc_device *dev, ...);
  46. int lpc_read_partid(struct lpc_device *dev);
  47. void lpc_write_ram(struct lpc_device *dev, const struct sprog_data *d, unsigned int addr);
  48. struct lpc_device *lpc_setup(struct serial_device *port) {
  49. struct lpc_device *dev;
  50. dev = malloc(sizeof(struct lpc_device));
  51. dev->port = port;
  52. dev->part = NULL;
  53. dev->reply_time = 5000;
  54. lpc_reset(dev, 1);
  55. return dev;
  56. }
  57. void lpc_init(struct lpc_device *dev) {
  58. char buf[4096];
  59. serial_read(dev->port, buf, sizeof(buf), 0);
  60. serial_write(dev->port, "?");
  61. if(lpc_await_reply(dev, "Synchronized", NULL)==1) {
  62. sprog_info("Synchronization successful\n");
  63. lpc_printf(dev, "Synchronized\r\n");
  64. if(lpc_await_reply(dev, "OK", NULL)!=1)
  65. sprog_error("Expected OK, received '%s'\n", buf);
  66. lpc_printf(dev, "12000\r\n");
  67. if(lpc_await_reply(dev, "OK", NULL)!=1)
  68. sprog_error("Expected OK, received '%s'\n", buf);
  69. } else {
  70. serial_write(dev->port, "\r\n");
  71. if(lpc_await_reply(dev, "?", NULL)==1)
  72. sprog_info("The device appears to be already synchronized\n");
  73. else
  74. sprog_error("Invalid device response\n");
  75. lpc_getline(dev, buf); /* receive invalid command reply */
  76. }
  77. if(lpc_read_partid(dev)==0)
  78. sprog_info("Found device: %s, %dkB Flash, %dkB RAM\n", dev->part->name, dev->part->flash, dev->part->ram);
  79. }
  80. void lpc_write(struct lpc_device *dev, const struct sprog_data *d) {
  81. static const int chunk_sizes[] = {256, 512, 1024, 4096, 0}; /* keep it sorted! */
  82. struct sprog_data chunk;
  83. int sect_start;
  84. int sect_end;
  85. int i;
  86. int j;
  87. int status;
  88. int offset;
  89. int chunk_size;
  90. int max_chunk_index;
  91. int data_size;
  92. offset = 0;
  93. chunk.data = NULL;
  94. lpc_command(dev, "U 23130\r\n"); /* unlock the device */
  95. sect_end = dev->part->flash/4 - 1; /* last sector = Flash size / 4kB - 1 */
  96. lpc_command(dev, "P 0 %d\r\n", sect_end);
  97. sprog_info("Erasing Flash memory... ");
  98. j = dev->reply_time;
  99. dev->reply_time = 5000;
  100. if(lpc_command(dev, "E 0 %d\r\n", sect_end))
  101. sprog_info("Error\n");
  102. else
  103. sprog_info("OK\n");
  104. dev->reply_time = j;
  105. while(d->size-offset>0) {
  106. chunk_size = d->size - offset;
  107. for(i=0; chunk_sizes[i]; i++) {
  108. if(chunk_sizes[i]>=chunk_size)
  109. break;
  110. if(chunk_sizes[i]+1024 > dev->part->ram * 1024) {
  111. i--;
  112. break;
  113. }
  114. }
  115. if(chunk_sizes[i]==0)
  116. i--;
  117. chunk_size = chunk_sizes[i];
  118. max_chunk_index = i;
  119. data_size = chunk_size;
  120. if(data_size > d->size-offset) {
  121. data_size = d->size - offset;
  122. /* we assume that all of the available chunk sizes are divisible by the first one */
  123. /* TODO: Optimize the following code, assuming that the chunk sizes are powers of 2 */
  124. chunk_size = data_size - data_size % chunk_sizes[0];
  125. if(data_size % chunk_sizes[0])
  126. chunk_size += chunk_sizes[0];
  127. }
  128. sprog_alloc_data(&chunk, chunk_size);
  129. for(i=0; i<data_size; i++)
  130. chunk.data[i] = d->data[offset+i];
  131. for(i=data_size; i<chunk_size; i++)
  132. chunk.data[i] = 0xff;
  133. chunk.size = chunk_size;
  134. if(offset==0) {
  135. /* calculate the checksum and put it at 0x1c in the vector table */
  136. j = 0;
  137. for(i=0; i<0x1c; i+=4)
  138. j += chunk.data[i] | chunk.data[i+1]<<8 | chunk.data[i+2]<<16 | chunk.data[i+3]<<24;
  139. j = -j;
  140. for(i=0; i<4; i++) {
  141. chunk.data[0x1c+i] = j & 0xff;
  142. j >>= 8;
  143. }
  144. }
  145. lpc_write_ram(dev, &chunk, 0x10000400);
  146. sprog_info("Copying to Flash...\n");
  147. j = max_chunk_index;
  148. status = 1;
  149. for(i=chunk_size; i>0; i-=chunk_sizes[j]) {
  150. for(j=j; j>=0; j--)
  151. if(chunk_sizes[j]<=i)
  152. break;
  153. sect_start = offset/4096;
  154. sect_end = (offset+chunk_sizes[j])/4096;
  155. lpc_command(dev, "P %d %d\r\n", sect_start, sect_end); /* prepare sectors for write */
  156. if(lpc_command(dev, "C %d %u %d\r\n", offset+(chunk_size-i), 0x10000400+(chunk_size-i), chunk_sizes[j]))
  157. status = 0;
  158. }
  159. if(status)
  160. sprog_info("Done!\n");
  161. else
  162. sprog_info("Error\n");
  163. offset += data_size;
  164. }
  165. }
  166. void lpc_exec(struct lpc_device *dev, const struct sprog_data *d) {
  167. lpc_write_ram(dev, d, 0x10000400);
  168. lpc_command(dev, "U 23130\r\n");
  169. sprog_info("Executing code... ");
  170. if(lpc_command(dev, "G %u T\r\n", 0x10000400))
  171. sprog_info("Error\n");
  172. else
  173. sprog_info("OK\n");
  174. }
  175. void lpc_write_ram(struct lpc_device *dev, const struct sprog_data *d, unsigned int addr) {
  176. struct sprog_data tmp;
  177. int last_offset;
  178. int last_i;
  179. int offset;
  180. int checksum;
  181. char buf[64];
  182. int i;
  183. int j;
  184. sprog_info("Writing %d bytes\n", d->size);
  185. /* align data size to 4 */
  186. if(d->size & 3) {
  187. tmp.data = NULL;
  188. sprog_alloc_data(&tmp, d->size + 4);
  189. for(i=0; i<d->size; i++)
  190. tmp.data[i] = d->data[i];
  191. for(i=d->size; i & 3; i++)
  192. tmp.data[i] = 0;
  193. tmp.size = i;
  194. d = &tmp;
  195. }
  196. lpc_command(dev, "W %u %u\r\n", addr, d->size);
  197. last_i = 0;
  198. last_offset = 0;
  199. offset = 0;
  200. sprog_progress(offset, d->size);
  201. checksum = 0;
  202. for(i=0; (j = uuencode_line(d, buf, &offset, &checksum)); i++) {
  203. lpc_printf(dev, "%s\r\n", buf);
  204. if(j==2 || (i % 20)==19) {
  205. lpc_printf(dev, "%u\r\n", checksum);
  206. checksum = 0;
  207. switch(lpc_await_reply(dev, "OK", "RESEND", NULL)) {
  208. case 1:
  209. last_offset = offset;
  210. last_i = i;
  211. break;
  212. case 2:
  213. default:
  214. offset = last_offset;
  215. i = last_i;
  216. }
  217. }
  218. sprog_progress(offset, d->size);
  219. }
  220. sprog_info("\n");
  221. }
  222. int lpc_await_reply(struct lpc_device *dev, ...) {
  223. char buf[4096];
  224. int i;
  225. const char *t;
  226. va_list l;
  227. va_start(l, dev);
  228. if(!lpc_getline(dev, buf))
  229. return 0;
  230. for(i=0; buf[i]; i++) {
  231. if(buf[i]=='\n') {
  232. buf[i] = 0;
  233. if(i>0) {
  234. if(buf[i-1]=='\r')
  235. buf[i-1] = 0;
  236. }
  237. break;
  238. }
  239. }
  240. for(i=0; (t = va_arg(l, const char*)); i++) {
  241. if(strcmp(buf, t)==0)
  242. return i+1;
  243. }
  244. return -1;
  245. }
  246. int lpc_getline(struct lpc_device *dev, char *buf) {
  247. if(sprog_waitdata(dev->port, dev->reply_time)==0)
  248. return 0;
  249. // sprog_info("fgets...\n");
  250. fgets(buf, 4096, dev->port->f);
  251. // sprog_info("...fgets\n");
  252. // sprog_info("--> %s\n", buf);
  253. return 1;
  254. }
  255. int lpc_scanf(struct lpc_device *dev, const char *text, ...) {
  256. int r;
  257. char buf[4096];
  258. va_list l;
  259. va_start(l, text);
  260. if(!lpc_getline(dev, buf))
  261. return 0;
  262. r = vsscanf(buf, text, l);
  263. va_end(l);
  264. return r;
  265. }
  266. void lpc_vprintf(struct lpc_device *dev, const char *text, va_list l) {
  267. char buf[4096];
  268. vsprintf(buf, text, l);
  269. serial_write(dev->port, buf);
  270. lpc_getline(dev, buf);
  271. }
  272. void lpc_printf(struct lpc_device *dev, const char *text, ...) {
  273. va_list l;
  274. va_start(l, text);
  275. lpc_vprintf(dev, text, l);
  276. va_end(l);
  277. }
  278. int lpc_command(struct lpc_device *dev, const char *text, ...) {
  279. int res;
  280. va_list l;
  281. va_start(l, text);
  282. lpc_vprintf(dev, text, l);
  283. va_end(l);
  284. if(lpc_scanf(dev, "%d", &res)<1)
  285. return -1;
  286. return res;
  287. }
  288. int lpc_read_partid(struct lpc_device *dev) {
  289. int i;
  290. int res;
  291. unsigned int partid;
  292. res = lpc_command(dev, "J\r\n");
  293. if(res!=0)
  294. return res;
  295. lpc_scanf(dev, "%u", &partid);
  296. for(i=0; lpc_parts[i].name; i++)
  297. if(lpc_parts[i].part_id==partid)
  298. break;
  299. if(lpc_parts[i].name)
  300. dev->part = &lpc_parts[i];
  301. else
  302. return -1;
  303. return 0;
  304. }
  305. void lpc_ispmode(struct lpc_device *dev, int state) {
  306. serial_setline(dev->port, SERIAL_RTS, state);
  307. }
  308. void lpc_reset(struct lpc_device *dev, int isp) {
  309. /* TODO: configure the control lines */
  310. serial_setline(dev->port, SERIAL_DTR, 1);
  311. if(isp)
  312. lpc_ispmode(dev, 1);
  313. sprog_sleep(50);
  314. serial_setline(dev->port, SERIAL_DTR, 0);
  315. if(isp) {
  316. sprog_sleep(50);
  317. }
  318. }
  319. void lpc_close(struct lpc_device *dev) {
  320. lpc_ispmode(dev, 0);
  321. lpc_reset(dev, 0);
  322. serial_close(dev->port);
  323. free(dev);
  324. }