Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2015
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.25 KB | None | 0 0
  1. // ArduinoISP
  2. // Copyright (c) 2008-2011 Randall Bohn
  3. // If you require a license, see
  4. //     http://www.opensource.org/licenses/bsd-license.php
  5. //
  6. // This sketch turns the Arduino into a AVRISP
  7. // using the following arduino pins:
  8. //
  9. // Pin 10 is used to reset the target microcontroller.
  10. //
  11. // By default, the hardware SPI pins MISO, MOSI and SCK pins are used
  12. // to communicate with the target. On all Arduinos, these pins can be found
  13. // on the ICSP/SPI header:
  14. //
  15. //               MISO °. . 5V (!) Avoid this pin on Due, Zero...
  16. //               SCK   . . MOSI
  17. //                     . . GND
  18. //
  19. // On some Arduinos (Uno,...), pins MOSI, MISO and SCK are the same pins
  20. // as digital pin 11, 12 and 13, respectively. That is why many tutorials
  21. // instruct you to hook up the target to these pins. If you find this wiring
  22. // more practical, have a define USE_OLD_STYLE_WIRING. This will work even
  23. // even when not using an Uno. (On an Uno this is not needed).
  24. //
  25. // Alternatively you can use any other digital pin by configuring software ('BitBanged')
  26. // SPI and having appropriate defines for PIN_MOSI, PIN_MISO and PIN_SCK.
  27. //
  28. // IMPORTANT: When using an Arduino that is not 5V tolerant (Due, Zero, ...)
  29. // as the programmer, make sure to not expose any of the programmer's pins to 5V.
  30. // A simple way to accomplish this is to power the complete system (programmer
  31. // and target) at 3V3.
  32. //
  33. // Put an LED (with resistor) on the following pins:
  34. // 9: Heartbeat   - shows the programmer is running
  35. // 8: Error       - Lights up if something goes wrong (use red if that makes sense)
  36. // 7: Programming - In communication with the slave
  37. //
  38.  
  39. #include "Arduino.h"
  40. #undef SERIAL
  41.  
  42.  
  43. #define PROG_FLICKER true
  44.  
  45. // Configure SPI clock (in Hz).
  46. // E.g. for an attiny @128 kHz: the datasheet states that both the high
  47. // and low spi clock pulse must be > 2 cpu cycles, so take 3 cycles i.e.
  48. // divide target f_cpu by 6:
  49. //     #define SPI_CLOCK            (128000/6)
  50. //
  51. // A clock slow enough for an attiny85 @ 1MHz, is a reasonable default:
  52.  
  53. #define SPI_CLOCK       (1000000/6)
  54.  
  55.  
  56. // Select hardware or software SPI, depending on SPI clock.
  57. // Currently only for AVR, for other archs (Due, Zero,...),
  58. // hardware SPI is probably too fast anyway.
  59.  
  60. #if defined(ARDUINO_ARCH_AVR)
  61.  
  62. #if SPI_CLOCK > (F_CPU / 128)
  63. #define USE_HARDWARE_SPI
  64. #endif
  65.  
  66. #endif
  67.  
  68. // Configure which pins to use:
  69.  
  70. // The standard pin configuration.
  71. #ifndef ARDUINO_HOODLOADER2
  72.  
  73. #define RESET     10 // Use pin 10 to reset the target rather than SS
  74. #define LED_HB    9
  75. #define LED_ERR   8
  76. #define LED_PMODE 7
  77.  
  78. // Uncomment following line to use the old Uno style wiring
  79. // (using pin 11, 12 and 13 instead of the SPI header) on Leonardo, Due...
  80.  
  81. // #define USE_OLD_STYLE_WIRING
  82.  
  83. #ifdef USE_OLD_STYLE_WIRING
  84.  
  85. #define PIN_MOSI    11
  86. #define PIN_MISO    12
  87. #define PIN_SCK     13
  88.  
  89. #endif
  90.  
  91. // HOODLOADER2 means running sketches on the atmega16u2
  92. // serial converter chips on Uno or Mega boards.
  93. // We must use pins that are broken out:
  94. #else
  95.  
  96. #define RESET       4
  97. #define LED_HB      7
  98. #define LED_ERR     6
  99. #define LED_PMODE   5
  100.  
  101. #endif
  102.  
  103. // By default, use hardware SPI pins:
  104. #ifndef PIN_MOSI
  105. #define PIN_MOSI    MOSI
  106. #endif
  107.  
  108. #ifndef PIN_MISO
  109. #define PIN_MISO    MISO
  110. #endif
  111.  
  112. #ifndef PIN_SCK
  113. #define PIN_SCK     SCK
  114. #endif
  115.  
  116. // Force bitbanged SPI if not using the hardware SPI pins:
  117. #if (PIN_MISO != MISO) ||  (PIN_MOSI != MOSI) || (PIN_SCK != SCK)
  118. #undef USE_HARDWARE_SPI
  119. #endif
  120.  
  121.  
  122. // Configure the serial port to use.
  123. //
  124. // Prefer the USB virtual serial port (aka. native USB port), if the Arduino has one:
  125. //   - it does not autoreset (except for the magic baud rate of 1200).
  126. //   - it is more reliable because of USB handshaking.
  127. //
  128. // Leonardo and similar have an USB virtual serial port: 'Serial'.
  129. // Due and Zero have an USB virtual serial port: 'SerialUSB'.
  130. //
  131. // On the Due and Zero, 'Serial' can be used too, provided you disable autoreset.
  132. // To use 'Serial': #define SERIAL Serial
  133.  
  134. #ifdef SERIAL_PORT_USBVIRTUAL
  135. #define SERIAL SERIAL_PORT_USBVIRTUAL
  136. #else
  137. #define SERIAL Serial
  138. #endif
  139.  
  140.  
  141. // Configure the baud rate:
  142.  
  143. #define BAUDRATE    19200
  144. // #define BAUDRATE 115200
  145. // #define BAUDRATE 1000000
  146.  
  147.  
  148. #define HWVER 2
  149. #define SWMAJ 1
  150. #define SWMIN 18
  151.  
  152. // STK Definitions
  153. #define STK_OK      0x10
  154. #define STK_FAILED  0x11
  155. #define STK_UNKNOWN 0x12
  156. #define STK_INSYNC  0x14
  157. #define STK_NOSYNC  0x15
  158. #define CRC_EOP     0x20 //ok it is a space...
  159.  
  160. void pulse(int pin, int times);
  161.  
  162. #ifdef USE_HARDWARE_SPI
  163. #include "SPI.h"
  164. #else
  165.  
  166. #define SPI_MODE0 0x00
  167.  
  168. class SPISettings {
  169. public:
  170.   // clock is in Hz
  171.   SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clock(clock){
  172.     (void) bitOrder;
  173.     (void) dataMode;
  174.   };
  175.  
  176. private:
  177.   uint32_t clock;
  178.  
  179. friend class BitBangedSPI;
  180. };
  181.  
  182. class BitBangedSPI {
  183. public:
  184.   void begin() {
  185.     digitalWrite(PIN_SCK, LOW);
  186.     digitalWrite(PIN_MOSI, LOW);
  187.     pinMode(PIN_SCK, OUTPUT);
  188.     pinMode(PIN_MOSI, OUTPUT);
  189.     pinMode(PIN_MISO, INPUT);
  190.   }
  191.  
  192.   void beginTransaction(SPISettings settings) {
  193.     pulseWidth = (500000 + settings.clock - 1) / settings.clock;
  194.     if (pulseWidth == 0)
  195.       pulseWidth = 1;
  196.   }
  197.  
  198.   void end() {}
  199.  
  200.   uint8_t transfer (uint8_t b) {
  201.     for (unsigned int i = 0; i < 8; ++i) {
  202.       digitalWrite(PIN_MOSI, (b & 0x80) ? HIGH : LOW);
  203.       digitalWrite(PIN_SCK, HIGH);
  204.       delayMicroseconds(pulseWidth);
  205.       b = (b << 1) | digitalRead(PIN_MISO);
  206.       digitalWrite(PIN_SCK, LOW); // slow pulse
  207.       delayMicroseconds(pulseWidth);
  208.     }
  209.     return b;
  210.   }
  211.  
  212. private:
  213.   unsigned long pulseWidth; // in microseconds
  214. };
  215.  
  216. static BitBangedSPI SPI;
  217.  
  218. #endif
  219.  
  220. void setup() {
  221.   SERIAL.begin(BAUDRATE);
  222.  
  223.   pinMode(LED_PMODE, OUTPUT);
  224.   pulse(LED_PMODE, 2);
  225.   pinMode(LED_ERR, OUTPUT);
  226.   pulse(LED_ERR, 2);
  227.   pinMode(LED_HB, OUTPUT);
  228.   pulse(LED_HB, 2);
  229.  
  230. }
  231.  
  232. int error = 0;
  233. int pmode = 0;
  234. // address for reading and writing, set by 'U' command
  235. unsigned int here;
  236. uint8_t buff[256]; // global block storage
  237.  
  238. #define beget16(addr) (*addr * 256 + *(addr+1) )
  239. typedef struct param {
  240.   uint8_t devicecode;
  241.   uint8_t revision;
  242.   uint8_t progtype;
  243.   uint8_t parmode;
  244.   uint8_t polling;
  245.   uint8_t selftimed;
  246.   uint8_t lockbytes;
  247.   uint8_t fusebytes;
  248.   uint8_t flashpoll;
  249.   uint16_t eeprompoll;
  250.   uint16_t pagesize;
  251.   uint16_t eepromsize;
  252.   uint32_t flashsize;
  253. }
  254. parameter;
  255.  
  256. parameter param;
  257.  
  258. // this provides a heartbeat on pin 9, so you can tell the software is running.
  259. uint8_t hbval = 128;
  260. int8_t hbdelta = 8;
  261. void heartbeat() {
  262.   static unsigned long last_time = 0;
  263.   unsigned long now = millis();
  264.   if ((now - last_time) < 40)
  265.     return;
  266.   last_time = now;
  267.   if (hbval > 192) hbdelta = -hbdelta;
  268.   if (hbval < 32) hbdelta = -hbdelta;
  269.   hbval += hbdelta;
  270.   analogWrite(LED_HB, hbval);
  271. }
  272.  
  273. static bool rst_active_high;
  274.  
  275. void reset_target(bool reset) {
  276.   digitalWrite(RESET, ((reset && rst_active_high) || (!reset && !rst_active_high)) ? HIGH : LOW);
  277. }
  278.  
  279. void loop(void) {
  280.   // is pmode active?
  281.   if (pmode) {
  282.     digitalWrite(LED_PMODE, HIGH);
  283.   } else {
  284.     digitalWrite(LED_PMODE, LOW);
  285.   }
  286.   // is there an error?
  287.   if (error) {
  288.     digitalWrite(LED_ERR, HIGH);
  289.   } else {
  290.     digitalWrite(LED_ERR, LOW);
  291.   }
  292.  
  293.   // light the heartbeat LED
  294.   heartbeat();
  295.   if (SERIAL.available()) {
  296.     avrisp();
  297.   }
  298. }
  299.  
  300. uint8_t getch() {
  301.   while (!SERIAL.available());
  302.   return SERIAL.read();
  303. }
  304. void fill(int n) {
  305.   for (int x = 0; x < n; x++) {
  306.     buff[x] = getch();
  307.   }
  308. }
  309.  
  310. #define PTIME 30
  311. void pulse(int pin, int times) {
  312.   do {
  313.     digitalWrite(pin, HIGH);
  314.     delay(PTIME);
  315.     digitalWrite(pin, LOW);
  316.     delay(PTIME);
  317.   } while (times--);
  318. }
  319.  
  320. void prog_lamp(int state) {
  321.   if (PROG_FLICKER) {
  322.     digitalWrite(LED_PMODE, state);
  323.   }
  324. }
  325.  
  326. uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  327.   SPI.transfer(a);
  328.   SPI.transfer(b);
  329.   SPI.transfer(c);
  330.   return SPI.transfer(d);
  331. }
  332.  
  333. void empty_reply() {
  334.   if (CRC_EOP == getch()) {
  335.     SERIAL.print((char)STK_INSYNC);
  336.     SERIAL.print((char)STK_OK);
  337.   } else {
  338.     error++;
  339.     SERIAL.print((char)STK_NOSYNC);
  340.   }
  341. }
  342.  
  343. void breply(uint8_t b) {
  344.   if (CRC_EOP == getch()) {
  345.     SERIAL.print((char)STK_INSYNC);
  346.     SERIAL.print((char)b);
  347.     SERIAL.print((char)STK_OK);
  348.   } else {
  349.     error++;
  350.     SERIAL.print((char)STK_NOSYNC);
  351.   }
  352. }
  353.  
  354. void get_version(uint8_t c) {
  355.   switch (c) {
  356.     case 0x80:
  357.       breply(HWVER);
  358.       break;
  359.     case 0x81:
  360.       breply(SWMAJ);
  361.       break;
  362.     case 0x82:
  363.       breply(SWMIN);
  364.       break;
  365.     case 0x93:
  366.       breply('S'); // serial programmer
  367.       break;
  368.     default:
  369.       breply(0);
  370.   }
  371. }
  372.  
  373. void set_parameters() {
  374.   // call this after reading paramter packet into buff[]
  375.   param.devicecode = buff[0];
  376.   param.revision   = buff[1];
  377.   param.progtype   = buff[2];
  378.   param.parmode    = buff[3];
  379.   param.polling    = buff[4];
  380.   param.selftimed  = buff[5];
  381.   param.lockbytes  = buff[6];
  382.   param.fusebytes  = buff[7];
  383.   param.flashpoll  = buff[8];
  384.   // ignore buff[9] (= buff[8])
  385.   // following are 16 bits (big endian)
  386.   param.eeprompoll = beget16(&buff[10]);
  387.   param.pagesize   = beget16(&buff[12]);
  388.   param.eepromsize = beget16(&buff[14]);
  389.  
  390.   // 32 bits flashsize (big endian)
  391.   param.flashsize = buff[16] * 0x01000000
  392.                     + buff[17] * 0x00010000
  393.                     + buff[18] * 0x00000100
  394.                     + buff[19];
  395.  
  396.   // avr devices have active low reset, at89sx are active high
  397.   rst_active_high = (param.devicecode >= 0xe0);
  398. }
  399.  
  400. void start_pmode() {
  401.  
  402.   // Reset target before driving PIN_SCK or PIN_MOSI
  403.  
  404.   // SPI.begin() will configure SS as output,
  405.   // so SPI master mode is selected.
  406.   // We have defined RESET as pin 10,
  407.   // which for many arduino's is not the SS pin.
  408.   // So we have to configure RESET as output here,
  409.   // (reset_target() first sets the correct level)
  410.   reset_target(true);
  411.   pinMode(RESET, OUTPUT);
  412.   SPI.begin();
  413.   SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
  414.  
  415.   // See avr datasheets, chapter "SERIAL_PRG Programming Algorithm":
  416.  
  417.   // Pulse RESET after PIN_SCK is low:
  418.   digitalWrite(PIN_SCK, LOW);
  419.   delay(20); // discharge PIN_SCK, value arbitrally chosen
  420.   reset_target(false);
  421.   // Pulse must be minimum 2 target CPU clock cycles
  422.   // so 100 usec is ok for CPU speeds above 20KHz
  423.   delayMicroseconds(100);
  424.   reset_target(true);
  425.  
  426.   // Send the enable programming command:
  427.   delay(50); // datasheet: must be > 20 msec
  428.   spi_transaction(0xAC, 0x53, 0x00, 0x00);
  429.   pmode = 1;
  430. }
  431.  
  432. void end_pmode() {
  433.   SPI.end();
  434.   // We're about to take the target out of reset
  435.   // so configure SPI pins as input
  436.   pinMode(PIN_MOSI, INPUT);
  437.   pinMode(PIN_SCK, INPUT);
  438.   reset_target(false);
  439.   pinMode(RESET, INPUT);
  440.   pmode = 0;
  441. }
  442.  
  443. void universal() {
  444.   uint8_t ch;
  445.  
  446.   fill(4);
  447.   ch = spi_transaction(buff[0], buff[1], buff[2], buff[3]);
  448.   breply(ch);
  449. }
  450.  
  451. void flash(uint8_t hilo, unsigned int addr, uint8_t data) {
  452.   spi_transaction(0x40 + 8 * hilo,
  453.                   addr >> 8 & 0xFF,
  454.                   addr & 0xFF,
  455.                   data);
  456. }
  457. void commit(unsigned int addr) {
  458.   if (PROG_FLICKER) {
  459.     prog_lamp(LOW);
  460.   }
  461.   spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
  462.   if (PROG_FLICKER) {
  463.     delay(PTIME);
  464.     prog_lamp(HIGH);
  465.   }
  466. }
  467.  
  468. unsigned int current_page() {
  469.   if (param.pagesize == 32) {
  470.     return here & 0xFFFFFFF0;
  471.   }
  472.   if (param.pagesize == 64) {
  473.     return here & 0xFFFFFFE0;
  474.   }
  475.   if (param.pagesize == 128) {
  476.     return here & 0xFFFFFFC0;
  477.   }
  478.   if (param.pagesize == 256) {
  479.     return here & 0xFFFFFF80;
  480.   }
  481.   return here;
  482. }
  483.  
  484.  
  485. void write_flash(int length) {
  486.   fill(length);
  487.   if (CRC_EOP == getch()) {
  488.     SERIAL.print((char) STK_INSYNC);
  489.     SERIAL.print((char) write_flash_pages(length));
  490.   } else {
  491.     error++;
  492.     SERIAL.print((char) STK_NOSYNC);
  493.   }
  494. }
  495.  
  496. uint8_t write_flash_pages(int length) {
  497.   int x = 0;
  498.   unsigned int page = current_page();
  499.   while (x < length) {
  500.     if (page != current_page()) {
  501.       commit(page);
  502.       page = current_page();
  503.     }
  504.     flash(LOW, here, buff[x++]);
  505.     flash(HIGH, here, buff[x++]);
  506.     here++;
  507.   }
  508.  
  509.   commit(page);
  510.  
  511.   return STK_OK;
  512. }
  513.  
  514. #define EECHUNK (32)
  515. uint8_t write_eeprom(unsigned int length) {
  516.   // here is a word address, get the byte address
  517.   unsigned int start = here * 2;
  518.   unsigned int remaining = length;
  519.   if (length > param.eepromsize) {
  520.     error++;
  521.     return STK_FAILED;
  522.   }
  523.   while (remaining > EECHUNK) {
  524.     write_eeprom_chunk(start, EECHUNK);
  525.     start += EECHUNK;
  526.     remaining -= EECHUNK;
  527.   }
  528.   write_eeprom_chunk(start, remaining);
  529.   return STK_OK;
  530. }
  531. // write (length) bytes, (start) is a byte address
  532. uint8_t write_eeprom_chunk(unsigned int start, unsigned int length) {
  533.   // this writes byte-by-byte,
  534.   // page writing may be faster (4 bytes at a time)
  535.   fill(length);
  536.   prog_lamp(LOW);
  537.   for (unsigned int x = 0; x < length; x++) {
  538.     unsigned int addr = start + x;
  539.     spi_transaction(0xC0, (addr >> 8) & 0xFF, addr & 0xFF, buff[x]);
  540.     delay(45);
  541.   }
  542.   prog_lamp(HIGH);
  543.   return STK_OK;
  544. }
  545.  
  546. void program_page() {
  547.   char result = (char) STK_FAILED;
  548.   unsigned int length = 256 * getch();
  549.   length += getch();
  550.   char memtype = getch();
  551.   // flash memory @here, (length) bytes
  552.   if (memtype == 'F') {
  553.     write_flash(length);
  554.     return;
  555.   }
  556.   if (memtype == 'E') {
  557.     result = (char)write_eeprom(length);
  558.     if (CRC_EOP == getch()) {
  559.       SERIAL.print((char) STK_INSYNC);
  560.       SERIAL.print(result);
  561.     } else {
  562.       error++;
  563.       SERIAL.print((char) STK_NOSYNC);
  564.     }
  565.     return;
  566.   }
  567.   SERIAL.print((char)STK_FAILED);
  568.   return;
  569. }
  570.  
  571. uint8_t flash_read(uint8_t hilo, unsigned int addr) {
  572.   return spi_transaction(0x20 + hilo * 8,
  573.                          (addr >> 8) & 0xFF,
  574.                          addr & 0xFF,
  575.                          0);
  576. }
  577.  
  578. char flash_read_page(int length) {
  579.   for (int x = 0; x < length; x += 2) {
  580.     uint8_t low = flash_read(LOW, here);
  581.     SERIAL.print((char) low);
  582.     uint8_t high = flash_read(HIGH, here);
  583.     SERIAL.print((char) high);
  584.     here++;
  585.   }
  586.   return STK_OK;
  587. }
  588.  
  589. char eeprom_read_page(int length) {
  590.   // here again we have a word address
  591.   int start = here * 2;
  592.   for (int x = 0; x < length; x++) {
  593.     int addr = start + x;
  594.     uint8_t ee = spi_transaction(0xA0, (addr >> 8) & 0xFF, addr & 0xFF, 0xFF);
  595.     SERIAL.print((char) ee);
  596.   }
  597.   return STK_OK;
  598. }
  599.  
  600. void read_page() {
  601.   char result = (char)STK_FAILED;
  602.   int length = 256 * getch();
  603.   length += getch();
  604.   char memtype = getch();
  605.   if (CRC_EOP != getch()) {
  606.     error++;
  607.     SERIAL.print((char) STK_NOSYNC);
  608.     return;
  609.   }
  610.   SERIAL.print((char) STK_INSYNC);
  611.   if (memtype == 'F') result = flash_read_page(length);
  612.   if (memtype == 'E') result = eeprom_read_page(length);
  613.   SERIAL.print(result);
  614. }
  615.  
  616. void read_signature() {
  617.   if (CRC_EOP != getch()) {
  618.     error++;
  619.     SERIAL.print((char) STK_NOSYNC);
  620.     return;
  621.   }
  622.   SERIAL.print((char) STK_INSYNC);
  623.   uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
  624.   SERIAL.print((char) high);
  625.   uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
  626.   SERIAL.print((char) middle);
  627.   uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
  628.   SERIAL.print((char) low);
  629.   SERIAL.print((char) STK_OK);
  630. }
  631. //////////////////////////////////////////
  632. //////////////////////////////////////////
  633.  
  634.  
  635. ////////////////////////////////////
  636. ////////////////////////////////////
  637. void avrisp() {
  638.   uint8_t ch = getch();
  639.   switch (ch) {
  640.     case '0': // signon
  641.       error = 0;
  642.       empty_reply();
  643.       break;
  644.     case '1':
  645.       if (getch() == CRC_EOP) {
  646.         SERIAL.print((char) STK_INSYNC);
  647.         SERIAL.print("AVR ISP");
  648.         SERIAL.print((char) STK_OK);
  649.       }
  650.       else {
  651.         error++;
  652.         SERIAL.print((char) STK_NOSYNC);
  653.       }
  654.       break;
  655.     case 'A':
  656.       get_version(getch());
  657.       break;
  658.     case 'B':
  659.       fill(20);
  660.       set_parameters();
  661.       empty_reply();
  662.       break;
  663.     case 'E': // extended parameters - ignore for now
  664.       fill(5);
  665.       empty_reply();
  666.       break;
  667.     case 'P':
  668.       if (!pmode)
  669.         start_pmode();
  670.       empty_reply();
  671.       break;
  672.     case 'U': // set address (word)
  673.       here = getch();
  674.       here += 256 * getch();
  675.       empty_reply();
  676.       break;
  677.  
  678.     case 0x60: //STK_PROG_FLASH
  679.       getch(); // low addr
  680.       getch(); // high addr
  681.       empty_reply();
  682.       break;
  683.     case 0x61: //STK_PROG_DATA
  684.       getch(); // data
  685.       empty_reply();
  686.       break;
  687.  
  688.     case 0x64: //STK_PROG_PAGE
  689.       program_page();
  690.       break;
  691.  
  692.     case 0x74: //STK_READ_PAGE 't'
  693.       read_page();
  694.       break;
  695.  
  696.     case 'V': //0x56
  697.       universal();
  698.       break;
  699.     case 'Q': //0x51
  700.       error = 0;
  701.       end_pmode();
  702.       empty_reply();
  703.       break;
  704.  
  705.     case 0x75: //STK_READ_SIGN 'u'
  706.       read_signature();
  707.       break;
  708.  
  709.     // expecting a command, not CRC_EOP
  710.     // this is how we can get back in sync
  711.     case CRC_EOP:
  712.       error++;
  713.       SERIAL.print((char) STK_NOSYNC);
  714.       break;
  715.  
  716.     // anything else we will return STK_UNKNOWN
  717.     default:
  718.       error++;
  719.       if (CRC_EOP == getch())
  720.         SERIAL.print((char)STK_UNKNOWN);
  721.       else
  722.         SERIAL.print((char)STK_NOSYNC);
  723.   }
  724. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement