host.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (C) 2004-2007 by egnite Software GmbH
  3. * Copyright (C) 2008 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. * and Xilinx Application Note XAPP058.
  35. */
  36. /*
  37. * $Log$
  38. * Revision 1.1 2008/10/20 13:10:05 haraldkipp
  39. * Checked in.
  40. *
  41. */
  42. #include <stdio.h>
  43. #include <io.h>
  44. #include <fcntl.h>
  45. #if defined(ETHERNUT2) || defined(ETHERNUT3)
  46. #include <sys/atom.h>
  47. #include <sys/timer.h>
  48. #include <sys/version.h>
  49. #include <dev/debug.h>
  50. #include <dev/urom.h>
  51. #elif defined(_MSC_VER)
  52. #include <winsock2.h>
  53. #endif
  54. #include "xsvf.h"
  55. #include "tapsm.h"
  56. #include "host.h"
  57. /*!
  58. * \file host.c
  59. * \brief Platform dependant routines..
  60. */
  61. /*!
  62. * \addtogroup xgHost
  63. */
  64. /*@{*/
  65. #ifdef XSVF_DEBUG
  66. /*!
  67. * \brief XSVF command names.
  68. *
  69. * Used for debugging output.
  70. */
  71. static char *cmd_names[] = {
  72. "XCOMPLETE",
  73. "XTDOMASK",
  74. "XSIR",
  75. "XSDR",
  76. "XRUNTEST",
  77. "UNKNOWN",
  78. "UNKNOWN",
  79. "XREPEAT",
  80. "XSDRSIZE",
  81. "XSDRTDO",
  82. "XSETSDRMASKS",
  83. "XSDRINC",
  84. "XSDRB",
  85. "XSDRC",
  86. "XSDRE",
  87. "XSDRTDOB",
  88. "XSDRTDOC",
  89. "XSDRTDOE",
  90. "XSTATE",
  91. "XENDIR",
  92. "XENDDR",
  93. "XSIR2",
  94. "XCOMMENT",
  95. "XWAIT",
  96. "UNKNOWN"
  97. };
  98. /*!
  99. * \brief TAP state names.
  100. *
  101. * Used for debugging output.
  102. */
  103. static char *tap_names[] = {
  104. "Test-Logic-Reset",
  105. "Run-Test-Idle",
  106. "Select-DR-Scan",
  107. "Capture-DR",
  108. "Shift-DR",
  109. "Exit1-DR",
  110. "Pause-DR",
  111. "Exit2-DR",
  112. "Update-DR",
  113. "Select-IR-Scan",
  114. "Capute-IR",
  115. "Shift-IR",
  116. "Exit1-IR",
  117. "Pause-IR",
  118. "Exit2-IR",
  119. "Update-IR",
  120. "Unknown"
  121. };
  122. #endif
  123. /*!
  124. * \brief Handle of XSVF file.
  125. */
  126. static int fh;
  127. /*!
  128. * \brief Last error occured in this module.
  129. */
  130. static int xsvf_err;
  131. /*!
  132. * \brief Initialize the platform dependant interface.
  133. *
  134. * All required hardware initializations should be done in this
  135. * routine. We may also initiate debug output. If the XSVF date
  136. * is located in a file system, then the file will be opened
  137. * here as well.
  138. *
  139. * \return Zero on success, otherwise an error code is returned.
  140. */
  141. int XsvfInit(void)
  142. {
  143. #if defined(ETHERNUT2) || defined(ETHERNUT3)
  144. uint32_t baud = 115200;
  145. /*
  146. * Prepare standard output and display a banner.
  147. */
  148. NutRegisterDevice(&devDebug0, 0, 0);
  149. freopen("uart0", "w", stdout);
  150. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  151. printf("\n100 XSVF-Executor " XSVFEXEC_VERSION " on Nut/OS %s", NutVersionString());
  152. NutSleep(100);
  153. /*
  154. * Register our device for the file system.
  155. */
  156. NutRegisterDevice(&devUrom, 0, 0);
  157. #ifdef ETHERNUT2
  158. /*
  159. * Disable JTAG. Ethernut 2 is using
  160. *
  161. * PF4 for target TCK.
  162. * PF5 for target TMS.
  163. * PF6 for target TDI.
  164. * PF7 for target TDO.
  165. */
  166. NutEnterCritical();
  167. outb(MCUCSR, _BV(JTD));
  168. outb(MCUCSR, _BV(JTD));
  169. NutExitCritical();
  170. outb(DDRF, 0x70);
  171. #endif
  172. #ifdef ETHERNUT3
  173. /*
  174. * Ethernut 3 is using
  175. *
  176. * P18 for target TCK.
  177. * P16 for target TMS.
  178. * P23 for target TDI.
  179. * P19 for target TDO.
  180. */
  181. outr(PIO_OER, _BV(16) | _BV(18) | _BV(23));
  182. outr(PIO_ODR, _BV(19));
  183. #endif
  184. #elif defined(_WIN32)
  185. printf("\n100 XSVF-Executor " XSVFEXEC_VERSION " on WIN32");
  186. #endif
  187. /* The XSVF file is expected in the UROM file system. */
  188. fh = _open(XSVFNAME, _O_BINARY | _O_RDONLY);
  189. return (fh == -1) ? XE_DATAUNDERFLOW : 0;
  190. }
  191. /*!
  192. * \brief Shutdown the platform dependant interface.
  193. *
  194. * On most embedded platforms this routine will never return.
  195. *
  196. * \param rc Programming result code.
  197. */
  198. void XsvfExit(int rc)
  199. {
  200. /* Close XSVF file. */
  201. if(fh != -1) {
  202. _close(fh);
  203. }
  204. #ifdef ETHERNUT3
  205. /* Re-enable UART0. */
  206. outb(0x21000000, 0x20);
  207. NutSleep(100);
  208. #endif
  209. /* Display programming result. */
  210. if(rc) {
  211. printf("\n4%02d ERROR\n", rc);
  212. }
  213. else {
  214. puts("\n199 OK");
  215. }
  216. #if defined(ETHERNUT2) || defined(ETHERNUT3)
  217. /* Nut/OS applications never return. */
  218. for (;;) {
  219. NutSleep(1000);
  220. }
  221. #endif
  222. }
  223. /*!
  224. * \brief Retrieve the last error occured in this module.
  225. *
  226. * \return Error code or 0 if no error occured.
  227. */
  228. int XsvfGetError(void)
  229. {
  230. return xsvf_err;
  231. }
  232. /*!
  233. * \brief Get next byte from XSVF buffer.
  234. *
  235. * Call XsvfGetError() to check for errors,
  236. *
  237. * \return Byte value.
  238. */
  239. uint8_t XsvfGetByte(void)
  240. {
  241. uint8_t rc;
  242. if(_read(fh, &rc, sizeof(rc)) != sizeof(rc)) {
  243. xsvf_err = XE_DATAUNDERFLOW;
  244. }
  245. #ifdef XSVF_DEBUG
  246. printf("[%u]", rc);
  247. #endif
  248. return rc;
  249. }
  250. /*!
  251. * \brief Get next command byte from XSVF buffer.
  252. *
  253. * \return XSVF command or XUNKNOWN if an error occured.
  254. */
  255. uint8_t XsvfGetCmd(void)
  256. {
  257. uint8_t rc;
  258. #ifdef ETHERNUT2
  259. static char cnt;
  260. if (++cnt == 0) {
  261. NutSleep(1);
  262. }
  263. #endif
  264. if(_read(fh, &rc, sizeof(rc)) != sizeof(rc) || rc >= XUNKNOWN) {
  265. rc = XUNKNOWN;
  266. }
  267. #ifdef XSVF_DEBUG
  268. printf("\n2%02d %s", rc, cmd_names[rc]);
  269. #endif
  270. return rc;
  271. }
  272. /*!
  273. * \brief Get next byte from XSVF buffer and select a TAP state.
  274. *
  275. * \param state0 Returned state, if the byte value is zero.
  276. * \param state1 Returned state, if the byte value is one.
  277. *
  278. * \return TAP state or UNKNOWN_STATE if an error occured.
  279. */
  280. uint8_t XsvfGetState(uint8_t state0, uint8_t state1)
  281. {
  282. uint8_t rc;
  283. if(_read(fh, &rc, sizeof(rc)) != sizeof(rc) || rc > 1) {
  284. rc = UNKNOWN_STATE;
  285. }
  286. else if(rc) {
  287. rc = state1;
  288. }
  289. else {
  290. rc = state0;
  291. }
  292. #ifdef XSVF_DEBUG
  293. printf("{%s}", tap_names[rc]);
  294. #endif
  295. return rc;
  296. }
  297. /*!
  298. * \brief Get next short value from XSVF buffer.
  299. *
  300. * Call XsvfGetError() to check for errors,
  301. *
  302. * \return Short value.
  303. */
  304. short XsvfGetShort(void)
  305. {
  306. short rc;
  307. if(_read(fh, &rc, sizeof(rc)) != sizeof(rc)) {
  308. xsvf_err = XE_DATAUNDERFLOW;
  309. return -1;
  310. }
  311. rc = ntohs(rc);
  312. #ifdef XSVF_DEBUG
  313. printf("[%d]", rc);
  314. #endif
  315. return rc;
  316. }
  317. /*!
  318. * \brief Get next long value from XSVF buffer.
  319. *
  320. * Call XsvfGetError() to check for errors,
  321. *
  322. * \return Long value.
  323. */
  324. long XsvfGetLong(void)
  325. {
  326. long rc;
  327. if(_read(fh, &rc, sizeof(rc)) != sizeof(rc)) {
  328. xsvf_err = XE_DATAUNDERFLOW;
  329. rc = 0;
  330. }
  331. else {
  332. rc = ntohl(rc);
  333. }
  334. #ifdef XSVF_DEBUG
  335. printf("[%ld]", rc);
  336. #endif
  337. return rc;
  338. }
  339. /*!
  340. * \brief Read a specified number of bits from XSVF buffer.
  341. *
  342. * \param buf Pointer to the buffer which receives the bit string.
  343. * \param num Number of bits to read.
  344. *
  345. * \return Error code or 0 if no error occured.
  346. */
  347. int XsvfReadBitString(void *buf, int num)
  348. {
  349. int len = (num + 7) / 8;
  350. if (len > MAX_BITVEC_BYTES) {
  351. xsvf_err = len = XE_DATAOVERFLOW;
  352. }
  353. else if(_read(fh, buf, len) < len) {
  354. xsvf_err = len = XE_DATAUNDERFLOW;
  355. }
  356. #ifdef XSVF_DEBUG
  357. else {
  358. uint8_t *cp = buf;
  359. printf("[%u:", num);
  360. while(len-- > 0) {
  361. printf("%02X", *cp);
  362. cp++;
  363. }
  364. printf("]");
  365. }
  366. #endif
  367. return len;
  368. }
  369. /*!
  370. * \brief Skip comment in the XSVF buffer.
  371. *
  372. * \return Error code or 0 if no error occured.
  373. */
  374. int XsvfSkipComment(void)
  375. {
  376. uint8_t ch;
  377. for(;;) {
  378. if(_read(fh, &ch, sizeof(ch)) != sizeof(ch)) {
  379. return (xsvf_err = XE_DATAUNDERFLOW);
  380. }
  381. if(ch == 0) {
  382. break;
  383. }
  384. #ifdef XSVF_DEBUG
  385. putchar(ch);
  386. #endif
  387. }
  388. return 0;
  389. }
  390. /*!
  391. * \brief Microsecond delay.
  392. *
  393. * \param usecs Number of microseconds.
  394. */
  395. void XsvfDelay(long usecs)
  396. {
  397. #if defined(ETHERNUT2) || defined(ETHERNUT3)
  398. usecs = usecs / 1000L;
  399. NutSleep(usecs + 1);
  400. #elif defined(_MSC_VER)
  401. Sleep((usecs + 500L) / 1000L);
  402. #endif
  403. }
  404. /*@}*/