program.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "program.h"
  2. #include <Arduino.h>
  3. #include <ESPAsyncWebServer.h>
  4. #include "netlog.h"
  5. #include "max80_gpio.h"
  6. uint8_t codeBuffer[BUFFER_SIZE];
  7. extern "C" {
  8. int xsvftool_esp_scan (void);
  9. uint32_t xsvftool_esp_id (void);
  10. int xsvftool_esp_program (int (*file_getbyte)(), int x);
  11. int xsvftool_esp_svf_packet (int (*packet_getbyte)(), int index, int final, char *report);
  12. void xsvftool_esp_set_pins (uint8_t tdi, uint8_t tdo, uint8_t tck, uint8_t tms);
  13. }
  14. uint8_t ledpin = 2;
  15. struct buffer_state {
  16. int count; // how many bytes in buffer
  17. int ptr; // current reading pointer
  18. uint8_t blink; // for the LED
  19. uint8_t mode;
  20. } rd;
  21. int get_next_byte() {
  22. if(rd.ptr >= rd.count) {
  23. // refill the buffer and update content
  24. rd.ptr = 0;
  25. if (rd.mode == MODE_SERIAL) {
  26. rd.count = fetch_next_block(codeBuffer, BUFFER_SIZE);
  27. } else {
  28. //rd.count = fetch_next_block_wifi(codeBuffer, BUFFER_SIZE);
  29. }
  30. if(rd.count <= 0 || rd.count > BUFFER_SIZE) {
  31. return -1;
  32. }
  33. digitalWrite(ledpin, (rd.blink++) & 1);
  34. }
  35. return codeBuffer[rd.ptr++];
  36. }
  37. uint32_t jtag_chip_id() {
  38. xsvftool_esp_scan();
  39. return xsvftool_esp_id();
  40. }
  41. int jtag_program(int dataType, uint8_t mode) {
  42. int retval = -1;
  43. if (dataType != DATA_TYPE_SVF && dataType != DATA_TYPE_XSVF) {
  44. Error("[JTAG] Invalid data type\r\n");
  45. return retval;
  46. }
  47. uint32_t chipId = xsvftool_esp_id();
  48. if (!chipId) {
  49. Error("[JTAG] No devices found!\r\n");
  50. return retval;
  51. }
  52. Info("[JTAG] Found device %08x\r\n", chipId);
  53. Info("[JTAG] Waiting first block\r\n");
  54. rd.ptr = 0;
  55. rd.mode = mode;
  56. if (mode == MODE_SERIAL) {
  57. rd.count = fetch_next_block(codeBuffer, BUFFER_SIZE);
  58. } else {
  59. //rd.count = fetch_next_block_wifi(codeBuffer, BUFFER_SIZE);
  60. }
  61. if (rd.count <= 0) {
  62. Error("[JTAG] No data available\r\n");
  63. return retval;
  64. }
  65. Info("[JTAG] Programming...\r\n");
  66. pinMode(MAX80_LED3, OUTPUT);
  67. retval = xsvftool_esp_program(get_next_byte, dataType);
  68. pinMode(MAX80_LED3, INPUT);
  69. Info("[JTAG] Programming finished with status %d\r\n", retval);
  70. return retval;
  71. }
  72. void set_pins(uint8_t tdi, uint8_t tdo, uint8_t tck, uint8_t tms, uint8_t led) {
  73. xsvftool_esp_set_pins(tdi, tdo, tck, tms);
  74. ledpin = led;
  75. }