optiboot_w25x20cl.cpp 9.6 KB

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