optiboot_w25x20cl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #include <avr/wdt.h>
  10. #define OPTIBOOT_MAJVER 6
  11. #define OPTIBOOT_CUSTOMVER 0
  12. #define OPTIBOOT_MINVER 2
  13. static unsigned const int __attribute__((section(".version")))
  14. optiboot_version = 256*(OPTIBOOT_MAJVER + OPTIBOOT_CUSTOMVER) + OPTIBOOT_MINVER;
  15. /* Watchdog settings */
  16. #define WATCHDOG_OFF (0)
  17. #define WATCHDOG_16MS (_BV(WDE))
  18. #define WATCHDOG_32MS (_BV(WDP0) | _BV(WDE))
  19. #define WATCHDOG_64MS (_BV(WDP1) | _BV(WDE))
  20. #define WATCHDOG_125MS (_BV(WDP1) | _BV(WDP0) | _BV(WDE))
  21. #define WATCHDOG_250MS (_BV(WDP2) | _BV(WDE))
  22. #define WATCHDOG_500MS (_BV(WDP2) | _BV(WDP0) | _BV(WDE))
  23. #define WATCHDOG_1S (_BV(WDP2) | _BV(WDP1) | _BV(WDE))
  24. #define WATCHDOG_2S (_BV(WDP2) | _BV(WDP1) | _BV(WDP0) | _BV(WDE))
  25. #define WATCHDOG_4S (_BV(WDP3) | _BV(WDE))
  26. #define WATCHDOG_8S (_BV(WDP3) | _BV(WDP0) | _BV(WDE))
  27. #if 0
  28. #define W25X20CL_SIGNATURE_0 9
  29. #define W25X20CL_SIGNATURE_1 8
  30. #define W25X20CL_SIGNATURE_2 7
  31. #else
  32. //FIXME this is a signature of ATmega2560!
  33. #define W25X20CL_SIGNATURE_0 0x1E
  34. #define W25X20CL_SIGNATURE_1 0x98
  35. #define W25X20CL_SIGNATURE_2 0x01
  36. #endif
  37. static void watchdogConfig(uint8_t x) {
  38. CRITICAL_SECTION_START
  39. WDTCSR = _BV(WDCE) | _BV(WDE);
  40. WDTCSR = x;
  41. CRITICAL_SECTION_END
  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. wdt_reset();
  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. //! @brief Enter an STK500 compatible Optiboot boot loader waiting for flashing the languages to an external flash memory.
  85. //! @return 1 if "start\n" was not sent. Optiboot was skipped
  86. //! @return 0 if "start\n" was sent. Optiboot ran normally. No need to send "start\n" in setup()
  87. uint8_t optiboot_w25x20cl_enter()
  88. {
  89. if (boot_app_flags & BOOT_APP_FLG_USER0) return 1;
  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. wdt_reset();
  103. const char *ptr = entry_magic_send;
  104. const char *end = strlen_P(entry_magic_send) + ptr;
  105. const uint8_t selectedSerialPort_bak = selectedSerialPort;
  106. // Flush the serial line.
  107. while (RECV_READY) {
  108. wdt_reset();
  109. // Dummy register read (discard)
  110. (void)(*(char *)UDR0);
  111. }
  112. selectedSerialPort = 0; //switch to Serial0
  113. MYSERIAL.flush(); //clear RX buffer
  114. int SerialHead = rx_buffer.head;
  115. // Send the initial magic string.
  116. while (ptr != end)
  117. putch(pgm_read_byte(ptr ++));
  118. wdt_reset();
  119. // Wait for two seconds until a magic string (constant entry_magic) is received
  120. // from the serial line.
  121. ptr = entry_magic_receive;
  122. end = strlen_P(entry_magic_receive) + ptr;
  123. while (ptr != end) {
  124. unsigned long boot_timer = 2000000;
  125. // Beware of this volatile pointer - it is important since the while-cycle below
  126. // doesn't contain any obvious references to rx_buffer.head
  127. // thus the compiler is allowed to remove the check from the cycle
  128. // i.e. rx_buffer.head == SerialHead would not be checked at all!
  129. // With the volatile keyword the compiler generates exactly the same code as without it with only one difference:
  130. // the last brne instruction jumps onto the (*rx_head == SerialHead) check and NOT onto the wdr instruction bypassing the check.
  131. volatile int *rx_head = &rx_buffer.head;
  132. while (*rx_head == SerialHead) {
  133. wdt_reset();
  134. if ( --boot_timer == 0) {
  135. // Timeout expired, continue with the application.
  136. selectedSerialPort = selectedSerialPort_bak; //revert Serial setting
  137. return 0;
  138. }
  139. }
  140. ch = rx_buffer.buffer[SerialHead];
  141. SerialHead = (unsigned int)(SerialHead + 1) % RX_BUFFER_SIZE;
  142. if (pgm_read_byte(ptr ++) != ch)
  143. {
  144. // Magic was not received correctly, continue with the application
  145. selectedSerialPort = selectedSerialPort_bak; //revert Serial setting
  146. return 0;
  147. }
  148. wdt_reset();
  149. }
  150. cbi(UCSR0B, RXCIE0); //disable the MarlinSerial0 interrupt
  151. // Send the cfm magic string.
  152. ptr = entry_magic_cfm;
  153. end = strlen_P(entry_magic_cfm) + ptr;
  154. while (ptr != end)
  155. putch(pgm_read_byte(ptr ++));
  156. }
  157. spi_init();
  158. w25x20cl_init();
  159. watchdogConfig(WATCHDOG_OFF);
  160. /* Forever loop: exits by causing WDT reset */
  161. for (;;) {
  162. /* get character from UART */
  163. ch = getch();
  164. if(ch == STK_GET_PARAMETER) {
  165. unsigned char which = getch();
  166. verifySpace();
  167. /*
  168. * Send optiboot version as "SW version"
  169. * Note that the references to memory are optimized away.
  170. */
  171. if (which == STK_SW_MINOR) {
  172. putch(optiboot_version & 0xFF);
  173. } else if (which == STK_SW_MAJOR) {
  174. putch(optiboot_version >> 8);
  175. } else {
  176. /*
  177. * GET PARAMETER returns a generic 0x03 reply for
  178. * other parameters - enough to keep Avrdude happy
  179. */
  180. putch(0x03);
  181. }
  182. }
  183. else if(ch == STK_SET_DEVICE) {
  184. // SET DEVICE is ignored
  185. getNch(20);
  186. }
  187. else if(ch == STK_SET_DEVICE_EXT) {
  188. // SET DEVICE EXT is ignored
  189. getNch(5);
  190. }
  191. else if(ch == STK_LOAD_ADDRESS) {
  192. // LOAD ADDRESS
  193. uint16_t newAddress;
  194. // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
  195. // Send the binary data by nibbles to avoid transmitting the ';' character.
  196. newAddress = getch();
  197. newAddress |= getch();
  198. newAddress |= (((uint16_t)getch()) << 8);
  199. newAddress |= (((uint16_t)getch()) << 8);
  200. // Transfer top bit to LSB in rampz
  201. if (newAddress & 0x8000)
  202. rampz |= 0x01;
  203. else
  204. rampz &= 0xFE;
  205. newAddress += newAddress; // Convert from word address to byte address
  206. address = newAddress;
  207. verifySpace();
  208. }
  209. else if(ch == STK_UNIVERSAL) {
  210. // LOAD_EXTENDED_ADDRESS is needed in STK_UNIVERSAL for addressing more than 128kB
  211. if ( AVR_OP_LOAD_EXT_ADDR == getch() ) {
  212. // get address
  213. getch(); // get '0'
  214. rampz = (rampz & 0x01) | ((getch() << 1) & 0xff); // get address and put it in rampz
  215. getNch(1); // get last '0'
  216. // response
  217. putch(0x00);
  218. }
  219. else {
  220. // everything else is ignored
  221. getNch(3);
  222. putch(0x00);
  223. }
  224. }
  225. /* Write memory, length is big endian and is in bytes */
  226. else if(ch == STK_PROG_PAGE) {
  227. // PROGRAM PAGE - we support flash programming only, not EEPROM
  228. uint8_t desttype;
  229. uint8_t *bufPtr;
  230. pagelen_t savelength;
  231. // Read the page length, with the length transferred each nibble separately to work around
  232. // the Prusa's USB to serial infamous semicolon issue.
  233. length = ((pagelen_t)getch()) << 8;
  234. length |= ((pagelen_t)getch()) << 8;
  235. length |= getch();
  236. length |= getch();
  237. savelength = length;
  238. // Read the destination type. It should always be 'F' as flash.
  239. desttype = getch();
  240. // read a page worth of contents
  241. bufPtr = buff;
  242. do *bufPtr++ = getch();
  243. while (--length);
  244. // Read command terminator, start reply
  245. verifySpace();
  246. if (desttype == 'E') {
  247. while (1) ; // Error: wait for WDT
  248. } else {
  249. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  250. // During a single bootloader run, only erase a 64kB block once.
  251. // An 8bit bitmask 'pages_erased' covers 512kB of FLASH memory.
  252. if ((address == 0) && (pages_erased & (1 << (addr >> 16))) == 0) {
  253. w25x20cl_wait_busy();
  254. w25x20cl_enable_wr();
  255. w25x20cl_block64_erase(addr);
  256. pages_erased |= (1 << (addr >> 16));
  257. }
  258. w25x20cl_wait_busy();
  259. w25x20cl_enable_wr();
  260. w25x20cl_page_program(addr, buff, savelength);
  261. w25x20cl_wait_busy();
  262. w25x20cl_disable_wr();
  263. }
  264. }
  265. /* Read memory block mode, length is big endian. */
  266. else if(ch == STK_READ_PAGE) {
  267. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  268. register pagelen_t i;
  269. // Read the page length, with the length transferred each nibble separately to work around
  270. // the Prusa's USB to serial infamous semicolon issue.
  271. length = ((pagelen_t)getch()) << 8;
  272. length |= ((pagelen_t)getch()) << 8;
  273. length |= getch();
  274. length |= getch();
  275. // Read the destination type. It should always be 'F' as flash. It is not checked.
  276. (void)getch();
  277. verifySpace();
  278. w25x20cl_wait_busy();
  279. w25x20cl_rd_data(addr, buff, length);
  280. for (i = 0; i < length; ++ i)
  281. putch(buff[i]);
  282. }
  283. /* Get device signature bytes */
  284. else if(ch == STK_READ_SIGN) {
  285. // READ SIGN - return what Avrdude wants to hear
  286. verifySpace();
  287. putch(W25X20CL_SIGNATURE_0);
  288. putch(W25X20CL_SIGNATURE_1);
  289. putch(W25X20CL_SIGNATURE_2);
  290. }
  291. else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */
  292. // Adaboot no-wait mod
  293. watchdogConfig(WATCHDOG_16MS);
  294. verifySpace();
  295. }
  296. else {
  297. // This covers the response to commands like STK_ENTER_PROGMODE
  298. verifySpace();
  299. }
  300. putch(STK_OK);
  301. }
  302. }