optiboot_w25x20cl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. // Send the initial magic string.
  115. while (ptr != end)
  116. putch(pgm_read_byte(ptr ++));
  117. wdt_reset();
  118. // Wait for two seconds until a magic string (constant entry_magic) is received
  119. // from the serial line.
  120. ptr = entry_magic_receive;
  121. end = strlen_P(entry_magic_receive) + ptr;
  122. while (ptr != end) {
  123. unsigned long boot_timer = 2000000;
  124. // Beware of this volatile pointer - it is important since the while-cycle below
  125. // doesn't contain any obvious references to rx_buffer.head
  126. // thus the compiler is allowed to remove the check from the cycle
  127. // i.e. rx_buffer.head == SerialHead would not be checked at all!
  128. // With the volatile keyword the compiler generates exactly the same code as without it with only one difference:
  129. // the last brne instruction jumps onto the (*rx_head == SerialHead) check and NOT onto the wdr instruction bypassing the check.
  130. volatile int *rx_head = &rx_buffer.head;
  131. int SerialHead = 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. while (ptr != end)
  154. putch(pgm_read_byte(ptr ++));
  155. }
  156. spi_init();
  157. w25x20cl_init();
  158. watchdogConfig(WATCHDOG_OFF);
  159. /* Forever loop: exits by causing WDT reset */
  160. for (;;) {
  161. /* get character from UART */
  162. ch = getch();
  163. if(ch == STK_GET_PARAMETER) {
  164. unsigned char which = getch();
  165. verifySpace();
  166. /*
  167. * Send optiboot version as "SW version"
  168. * Note that the references to memory are optimized away.
  169. */
  170. if (which == STK_SW_MINOR) {
  171. putch(optiboot_version & 0xFF);
  172. } else if (which == STK_SW_MAJOR) {
  173. putch(optiboot_version >> 8);
  174. } else {
  175. /*
  176. * GET PARAMETER returns a generic 0x03 reply for
  177. * other parameters - enough to keep Avrdude happy
  178. */
  179. putch(0x03);
  180. }
  181. }
  182. else if(ch == STK_SET_DEVICE) {
  183. // SET DEVICE is ignored
  184. getNch(20);
  185. }
  186. else if(ch == STK_SET_DEVICE_EXT) {
  187. // SET DEVICE EXT is ignored
  188. getNch(5);
  189. }
  190. else if(ch == STK_LOAD_ADDRESS) {
  191. // LOAD ADDRESS
  192. uint16_t newAddress;
  193. // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
  194. // Send the binary data by nibbles to avoid transmitting the ';' character.
  195. newAddress = getch();
  196. newAddress |= getch();
  197. newAddress |= (((uint16_t)getch()) << 8);
  198. newAddress |= (((uint16_t)getch()) << 8);
  199. // Transfer top bit to LSB in rampz
  200. if (newAddress & 0x8000)
  201. rampz |= 0x01;
  202. else
  203. rampz &= 0xFE;
  204. newAddress += newAddress; // Convert from word address to byte address
  205. address = newAddress;
  206. verifySpace();
  207. }
  208. else if(ch == STK_UNIVERSAL) {
  209. // LOAD_EXTENDED_ADDRESS is needed in STK_UNIVERSAL for addressing more than 128kB
  210. if ( AVR_OP_LOAD_EXT_ADDR == getch() ) {
  211. // get address
  212. getch(); // get '0'
  213. rampz = (rampz & 0x01) | ((getch() << 1) & 0xff); // get address and put it in rampz
  214. getNch(1); // get last '0'
  215. // response
  216. putch(0x00);
  217. }
  218. else {
  219. // everything else is ignored
  220. getNch(3);
  221. putch(0x00);
  222. }
  223. }
  224. /* Write memory, length is big endian and is in bytes */
  225. else if(ch == STK_PROG_PAGE) {
  226. // PROGRAM PAGE - we support flash programming only, not EEPROM
  227. uint8_t desttype;
  228. uint8_t *bufPtr;
  229. pagelen_t savelength;
  230. // Read the page length, with the length transferred each nibble separately to work around
  231. // the Prusa's USB to serial infamous semicolon issue.
  232. length = ((pagelen_t)getch()) << 8;
  233. length |= ((pagelen_t)getch()) << 8;
  234. length |= getch();
  235. length |= getch();
  236. savelength = length;
  237. // Read the destination type. It should always be 'F' as flash.
  238. desttype = getch();
  239. // read a page worth of contents
  240. bufPtr = buff;
  241. do *bufPtr++ = getch();
  242. while (--length);
  243. // Read command terminator, start reply
  244. verifySpace();
  245. if (desttype == 'E') {
  246. while (1) ; // Error: wait for WDT
  247. } else {
  248. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  249. // During a single bootloader run, only erase a 64kB block once.
  250. // An 8bit bitmask 'pages_erased' covers 512kB of FLASH memory.
  251. if ((address == 0) && (pages_erased & (1 << (addr >> 16))) == 0) {
  252. w25x20cl_wait_busy();
  253. w25x20cl_enable_wr();
  254. w25x20cl_block64_erase(addr);
  255. pages_erased |= (1 << (addr >> 16));
  256. }
  257. w25x20cl_wait_busy();
  258. w25x20cl_enable_wr();
  259. w25x20cl_page_program(addr, buff, savelength);
  260. w25x20cl_wait_busy();
  261. w25x20cl_disable_wr();
  262. }
  263. }
  264. /* Read memory block mode, length is big endian. */
  265. else if(ch == STK_READ_PAGE) {
  266. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  267. register pagelen_t i;
  268. // Read the page length, with the length transferred each nibble separately to work around
  269. // the Prusa's USB to serial infamous semicolon issue.
  270. length = ((pagelen_t)getch()) << 8;
  271. length |= ((pagelen_t)getch()) << 8;
  272. length |= getch();
  273. length |= getch();
  274. // Read the destination type. It should always be 'F' as flash. It is not checked.
  275. (void)getch();
  276. verifySpace();
  277. w25x20cl_wait_busy();
  278. w25x20cl_rd_data(addr, buff, length);
  279. for (i = 0; i < length; ++ i)
  280. putch(buff[i]);
  281. }
  282. /* Get device signature bytes */
  283. else if(ch == STK_READ_SIGN) {
  284. // READ SIGN - return what Avrdude wants to hear
  285. verifySpace();
  286. putch(W25X20CL_SIGNATURE_0);
  287. putch(W25X20CL_SIGNATURE_1);
  288. putch(W25X20CL_SIGNATURE_2);
  289. }
  290. else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */
  291. // Adaboot no-wait mod
  292. watchdogConfig(WATCHDOG_16MS);
  293. verifySpace();
  294. }
  295. else {
  296. // This covers the response to commands like STK_ENTER_PROGMODE
  297. verifySpace();
  298. }
  299. putch(STK_OK);
  300. }
  301. }