jtag.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. // NOTE: This code is based on code Luke-Jr wrote originally for LPC1343CodeBase
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "ft232r.h"
  15. #include "jtag.h"
  16. // NOTE: The order of tms and tdi here are inverted from LPC1343CodeBase
  17. bool jtag_clock(struct jtag_port *jp, bool tms, bool tdi, bool *tdo)
  18. {
  19. unsigned char buf[4];
  20. memset(buf, (tms ? (1 << jp->tms) : 0)
  21. | (tdi ? (1 << jp->tdi) : 0), sizeof(buf));
  22. buf[1] |= jp->tck;
  23. if (ft232r_write_all(jp->ftdi, buf, sizeof(buf)) != sizeof(buf))
  24. return false;
  25. if (ft232r_read_all(jp->ftdi, buf, sizeof(buf)) != sizeof(buf))
  26. return false;
  27. if (tdo)
  28. *tdo = buf[3] & jp->tdo;
  29. return true;
  30. }
  31. static bool jtag_rw_bit(struct jtag_port *jp, void *buf, uint8_t mask, bool tms, bool do_read)
  32. {
  33. uint8_t *byte = buf;
  34. bool tdo;
  35. if (!jtag_clock(jp, tms, byte[0] & mask, do_read ? &tdo : NULL))
  36. return false;
  37. if (do_read) {
  38. if (tdo)
  39. byte[0] |= mask;
  40. else
  41. byte[0] &= ~mask;
  42. }
  43. return true;
  44. }
  45. // Expects to start at the Capture step, to handle 0-length gracefully
  46. bool _jtag_llrw(struct jtag_port *jp, void *buf, size_t bitlength, bool do_read, int stage)
  47. {
  48. uint8_t *data = buf;
  49. int i, j;
  50. div_t d;
  51. if (!bitlength)
  52. return jtag_clock(jp, true, false, NULL);
  53. if (stage & 1)
  54. if (!jtag_clock(jp, false, false, NULL))
  55. return false;
  56. d = div(bitlength - 1, 8);
  57. for (i = 0; i < d.quot; ++i) {
  58. for (j = 0x80; j; j /= 2) {
  59. if (!jtag_rw_bit(jp, &data[i], 0x80, false, do_read))
  60. return false;
  61. }
  62. }
  63. for (j = 0; j < d.rem; ++j)
  64. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  65. return false;
  66. if (stage & 2) {
  67. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, true, do_read))
  68. return false;
  69. if (!jtag_clock(jp, true, false, NULL)) // Update
  70. return false;
  71. }
  72. else
  73. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  74. return false;
  75. return true;
  76. }
  77. bool jtag_reset(struct jtag_port *jp)
  78. {
  79. for (int i = 0; i < 5; ++i)
  80. if (!jtag_clock(jp, true, false, NULL))
  81. return false;
  82. return jtag_clock(jp, false, false, NULL);
  83. }
  84. // Returns -1 for failure, -2 for unknown, or zero and higher for number of devices
  85. ssize_t jtag_detect(struct jtag_port *jp)
  86. {
  87. // TODO: detect more than 1 device
  88. int i;
  89. bool tdo;
  90. if (!(1
  91. && jtag_write(jp, JTAG_REG_IR, "\xff", 8)
  92. && jtag_clock(jp, true , false, NULL) // Select DR
  93. && jtag_clock(jp, false, false, NULL) // Capture DR
  94. && jtag_clock(jp, false, false, NULL) // Shift DR
  95. ))
  96. return false;
  97. for (i = 0; i < 4; ++i)
  98. if (!jtag_clock(jp, false, false, NULL))
  99. return false;
  100. if (!jtag_clock(jp, false, false, &tdo))
  101. return false;
  102. if (tdo)
  103. return -1;
  104. for (i = 0; i < 4; ++i)
  105. {
  106. if (!jtag_clock(jp, false, true, &tdo))
  107. return -1;
  108. if (tdo)
  109. break;
  110. }
  111. if (!jtag_reset(jp))
  112. return -1;
  113. return i < 2 ? i : -2;
  114. }
  115. bool _jtag_rw(struct jtag_port *jp, enum jtagreg r, void *buf, size_t bitlength, bool do_read, int stage)
  116. {
  117. if (!jtag_clock(jp, true, false, NULL)) // Select DR
  118. return false;
  119. if (r == JTAG_REG_IR)
  120. if (!jtag_clock(jp, true, false, NULL)) // Select IR
  121. return false;
  122. if (!jtag_clock(jp, false, false, NULL)) // Capture
  123. return false;
  124. return _jtag_llrw(jp, buf, bitlength, do_read, stage); // Exit1
  125. }
  126. bool jtag_run(struct jtag_port *jp)
  127. {
  128. return jtag_clock(jp, false, false, NULL);
  129. }