optiboot_w25x20cl.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Based on the OptiBoot project
  2. // https://github.com/Optiboot/optiboot
  3. // Licence GLP 2 or later.
  4. #include "Marlin.h"
  5. #include "w25x20cl.h"
  6. #include "stk500.h"
  7. #define OPTIBOOT_MAJVER 6
  8. #define OPTIBOOT_CUSTOMVER 0
  9. #define OPTIBOOT_MINVER 2
  10. static unsigned const int __attribute__((section(".version")))
  11. optiboot_version = 256*(OPTIBOOT_MAJVER + OPTIBOOT_CUSTOMVER) + OPTIBOOT_MINVER;
  12. /* Watchdog settings */
  13. #define WATCHDOG_OFF (0)
  14. #define WATCHDOG_16MS (_BV(WDE))
  15. #define WATCHDOG_32MS (_BV(WDP0) | _BV(WDE))
  16. #define WATCHDOG_64MS (_BV(WDP1) | _BV(WDE))
  17. #define WATCHDOG_125MS (_BV(WDP1) | _BV(WDP0) | _BV(WDE))
  18. #define WATCHDOG_250MS (_BV(WDP2) | _BV(WDE))
  19. #define WATCHDOG_500MS (_BV(WDP2) | _BV(WDP0) | _BV(WDE))
  20. #define WATCHDOG_1S (_BV(WDP2) | _BV(WDP1) | _BV(WDE))
  21. #define WATCHDOG_2S (_BV(WDP2) | _BV(WDP1) | _BV(WDP0) | _BV(WDE))
  22. #define WATCHDOG_4S (_BV(WDP3) | _BV(WDE))
  23. #define WATCHDOG_8S (_BV(WDP3) | _BV(WDP0) | _BV(WDE))
  24. #if 0
  25. #define W25X20CL_SIGNATURE_0 9
  26. #define W25X20CL_SIGNATURE_1 8
  27. #define W25X20CL_SIGNATURE_2 7
  28. #else
  29. //FIXME this is a signature of ATmega2560!
  30. #define W25X20CL_SIGNATURE_0 0x1E
  31. #define W25X20CL_SIGNATURE_1 0x98
  32. #define W25X20CL_SIGNATURE_2 0x01
  33. #endif
  34. static void watchdogConfig(uint8_t x) {
  35. WDTCSR = _BV(WDCE) | _BV(WDE);
  36. WDTCSR = x;
  37. }
  38. static void watchdogReset() {
  39. __asm__ __volatile__ (
  40. "wdr\n"
  41. );
  42. }
  43. #define RECV_READY ((UCSR0A & _BV(RXC0)) != 0)
  44. static uint8_t getch(void) {
  45. uint8_t ch;
  46. while(! RECV_READY) ;
  47. if (!(UCSR0A & _BV(FE0))) {
  48. /*
  49. * A Framing Error indicates (probably) that something is talking
  50. * to us at the wrong bit rate. Assume that this is because it
  51. * expects to be talking to the application, and DON'T reset the
  52. * watchdog. This should cause the bootloader to abort and run
  53. * the application "soon", if it keeps happening. (Note that we
  54. * don't care that an invalid char is returned...)
  55. */
  56. watchdogReset();
  57. }
  58. ch = UDR0;
  59. return ch;
  60. }
  61. static void putch(char ch) {
  62. while (!(UCSR0A & _BV(UDRE0)));
  63. UDR0 = ch;
  64. }
  65. static void verifySpace() {
  66. if (getch() != CRC_EOP) {
  67. putch(STK_FAILED);
  68. watchdogConfig(WATCHDOG_16MS); // shorten WD timeout
  69. while (1) // and busy-loop so that WD causes
  70. ; // a reset and app start.
  71. }
  72. putch(STK_INSYNC);
  73. }
  74. static void getNch(uint8_t count) {
  75. do getch(); while (--count);
  76. verifySpace();
  77. }
  78. typedef uint16_t pagelen_t;
  79. static const char entry_magic_send [] PROGMEM = "start\n";
  80. static const char entry_magic_receive[] PROGMEM = "w25x20cl_enter\n";
  81. static const char entry_magic_cfm [] PROGMEM = "w25x20cl_cfm\n";
  82. struct block_t;
  83. extern struct block_t *block_buffer;
  84. void optiboot_w25x20cl_enter()
  85. {
  86. uint8_t ch;
  87. uint8_t rampz = 0;
  88. register uint16_t address = 0;
  89. register pagelen_t length;
  90. // Use the planner's queue for the receive / transmit buffers.
  91. // uint8_t *buff = (uint8_t*)block_buffer;
  92. uint8_t buff[260];
  93. // bitmap of pages to be written. Bit is set to 1 if the page has already been erased.
  94. uint8_t pages_erased = 0;
  95. // Handshake sequence: Initialize the serial line, flush serial line, send magic, receive magic.
  96. // If the magic is not received on time, or it is not received correctly, continue to the application.
  97. {
  98. watchdogReset();
  99. unsigned long boot_timeout = 2000000;
  100. unsigned long boot_timer = 0;
  101. const char *ptr = entry_magic_send;
  102. const char *end = strlen_P(entry_magic_send) + ptr;
  103. // Initialize the serial line.
  104. UCSR0A |= (1 << U2X0);
  105. UBRR0L = (((float)(F_CPU))/(((float)(115200))*8.0)-1.0+0.5);
  106. UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  107. // Flush the serial line.
  108. while (RECV_READY) {
  109. watchdogReset();
  110. // Dummy register read (discard)
  111. (void)(*(char *)UDR0);
  112. }
  113. // Send the initial magic string.
  114. while (ptr != end)
  115. putch(pgm_read_byte_far(ptr ++));
  116. watchdogReset();
  117. // Wait for one second until a magic string (constant entry_magic) is received
  118. // from the serial line.
  119. ptr = entry_magic_receive;
  120. end = strlen_P(entry_magic_receive) + ptr;
  121. while (ptr != end) {
  122. while (! RECV_READY) {
  123. watchdogReset();
  124. delayMicroseconds(1);
  125. if (++ boot_timer > boot_timeout)
  126. // Timeout expired, continue with the application.
  127. return;
  128. }
  129. ch = UDR0;
  130. if (pgm_read_byte_far(ptr ++) != ch)
  131. // Magic was not received correctly, continue with the application
  132. return;
  133. watchdogReset();
  134. }
  135. // Send the cfm magic string.
  136. ptr = entry_magic_cfm;
  137. while (ptr != end)
  138. putch(pgm_read_byte_far(ptr ++));
  139. }
  140. spi_init();
  141. w25x20cl_init();
  142. watchdogConfig(WATCHDOG_OFF);
  143. /* Forever loop: exits by causing WDT reset */
  144. for (;;) {
  145. /* get character from UART */
  146. ch = getch();
  147. if(ch == STK_GET_PARAMETER) {
  148. unsigned char which = getch();
  149. verifySpace();
  150. /*
  151. * Send optiboot version as "SW version"
  152. * Note that the references to memory are optimized away.
  153. */
  154. if (which == STK_SW_MINOR) {
  155. putch(optiboot_version & 0xFF);
  156. } else if (which == STK_SW_MAJOR) {
  157. putch(optiboot_version >> 8);
  158. } else {
  159. /*
  160. * GET PARAMETER returns a generic 0x03 reply for
  161. * other parameters - enough to keep Avrdude happy
  162. */
  163. putch(0x03);
  164. }
  165. }
  166. else if(ch == STK_SET_DEVICE) {
  167. // SET DEVICE is ignored
  168. getNch(20);
  169. }
  170. else if(ch == STK_SET_DEVICE_EXT) {
  171. // SET DEVICE EXT is ignored
  172. getNch(5);
  173. }
  174. else if(ch == STK_LOAD_ADDRESS) {
  175. // LOAD ADDRESS
  176. uint16_t newAddress;
  177. // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
  178. // Send the binary data by nibbles to avoid transmitting the ';' character.
  179. newAddress = getch();
  180. newAddress |= getch();
  181. newAddress |= (((uint16_t)getch()) << 8);
  182. newAddress |= (((uint16_t)getch()) << 8);
  183. // Transfer top bit to LSB in rampz
  184. if (newAddress & 0x8000)
  185. rampz |= 0x01;
  186. else
  187. rampz &= 0xFE;
  188. newAddress += newAddress; // Convert from word address to byte address
  189. address = newAddress;
  190. verifySpace();
  191. }
  192. else if(ch == STK_UNIVERSAL) {
  193. // LOAD_EXTENDED_ADDRESS is needed in STK_UNIVERSAL for addressing more than 128kB
  194. if ( AVR_OP_LOAD_EXT_ADDR == getch() ) {
  195. // get address
  196. getch(); // get '0'
  197. rampz = (rampz & 0x01) | ((getch() << 1) & 0xff); // get address and put it in rampz
  198. getNch(1); // get last '0'
  199. // response
  200. putch(0x00);
  201. }
  202. else {
  203. // everything else is ignored
  204. getNch(3);
  205. putch(0x00);
  206. }
  207. }
  208. /* Write memory, length is big endian and is in bytes */
  209. else if(ch == STK_PROG_PAGE) {
  210. // PROGRAM PAGE - we support flash programming only, not EEPROM
  211. uint8_t desttype;
  212. uint8_t *bufPtr;
  213. pagelen_t savelength;
  214. // Read the page length, with the length transferred each nibble separately to work around
  215. // the Prusa's USB to serial infamous semicolon issue.
  216. length = ((pagelen_t)getch()) << 8;
  217. length |= ((pagelen_t)getch()) << 8;
  218. length |= getch();
  219. length |= getch();
  220. savelength = length;
  221. // Read the destination type. It should always be 'F' as flash.
  222. desttype = getch();
  223. // read a page worth of contents
  224. bufPtr = buff;
  225. do *bufPtr++ = getch();
  226. while (--length);
  227. // Read command terminator, start reply
  228. verifySpace();
  229. if (desttype == 'E') {
  230. while (1) ; // Error: wait for WDT
  231. } else {
  232. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  233. // During a single bootloader run, only erase a 64kB block once.
  234. // An 8bit bitmask 'pages_erased' covers 512kB of FLASH memory.
  235. if (address == 0 && (pages_erased & (1 << addr)) == 0) {
  236. w25x20cl_wait_busy();
  237. w25x20cl_enable_wr();
  238. w25x20cl_block64_erase(addr);
  239. pages_erased |= (1 << addr);
  240. }
  241. w25x20cl_wait_busy();
  242. w25x20cl_enable_wr();
  243. w25x20cl_page_program(addr, buff, savelength);
  244. w25x20cl_wait_busy();
  245. w25x20cl_disable_wr();
  246. }
  247. }
  248. /* Read memory block mode, length is big endian. */
  249. else if(ch == STK_READ_PAGE) {
  250. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  251. uint8_t desttype;
  252. register pagelen_t i;
  253. // Read the page length, with the length transferred each nibble separately to work around
  254. // the Prusa's USB to serial infamous semicolon issue.
  255. length = ((pagelen_t)getch()) << 8;
  256. length |= ((pagelen_t)getch()) << 8;
  257. length |= getch();
  258. length |= getch();
  259. // Read the destination type. It should always be 'F' as flash.
  260. desttype = getch();
  261. verifySpace();
  262. w25x20cl_wait_busy();
  263. w25x20cl_rd_data(addr, buff, length);
  264. for (i = 0; i < length; ++ i)
  265. putch(buff[i]);
  266. }
  267. /* Get device signature bytes */
  268. else if(ch == STK_READ_SIGN) {
  269. // READ SIGN - return what Avrdude wants to hear
  270. verifySpace();
  271. putch(W25X20CL_SIGNATURE_0);
  272. putch(W25X20CL_SIGNATURE_1);
  273. putch(W25X20CL_SIGNATURE_2);
  274. }
  275. else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */
  276. // Adaboot no-wait mod
  277. watchdogConfig(WATCHDOG_16MS);
  278. verifySpace();
  279. }
  280. else {
  281. // This covers the response to commands like STK_ENTER_PROGMODE
  282. verifySpace();
  283. }
  284. putch(STK_OK);
  285. }
  286. }