optiboot_xflash.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "xflash.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. #if 0
  16. #define XFLASH_SIGNATURE_0 9
  17. #define XFLASH_SIGNATURE_1 8
  18. #define XFLASH_SIGNATURE_2 7
  19. #else
  20. //FIXME this is a signature of ATmega2560!
  21. #define XFLASH_SIGNATURE_0 0x1E
  22. #define XFLASH_SIGNATURE_1 0x98
  23. #define XFLASH_SIGNATURE_2 0x01
  24. #endif
  25. #define RECV_READY ((UCSR0A & _BV(RXC0)) != 0)
  26. static uint8_t getch(void) {
  27. uint8_t ch;
  28. while(! RECV_READY) ;
  29. if (!(UCSR0A & _BV(FE0))) {
  30. /*
  31. * A Framing Error indicates (probably) that something is talking
  32. * to us at the wrong bit rate. Assume that this is because it
  33. * expects to be talking to the application, and DON'T reset the
  34. * watchdog. This should cause the bootloader to abort and run
  35. * the application "soon", if it keeps happening. (Note that we
  36. * don't care that an invalid char is returned...)
  37. */
  38. wdt_reset();
  39. }
  40. ch = UDR0;
  41. return ch;
  42. }
  43. static void putch(char ch) {
  44. while (!(UCSR0A & _BV(UDRE0)));
  45. UDR0 = ch;
  46. }
  47. static void verifySpace() {
  48. if (getch() != CRC_EOP) {
  49. putch(STK_FAILED);
  50. wdt_enable(WDTO_15MS); // shorten WD timeout
  51. while (1) // and busy-loop so that WD causes
  52. ; // a reset and app start.
  53. }
  54. putch(STK_INSYNC);
  55. }
  56. static void getNch(uint8_t count) {
  57. do getch(); while (--count);
  58. verifySpace();
  59. }
  60. typedef uint16_t pagelen_t;
  61. //Thou shalt not change these messages, else the avrdude-slicer xflash implementation will no longer work and the language upload will fail.
  62. //Right now we support 2 xflash chips - the original w25x20cl and a new one GD25Q20C
  63. static const char entry_magic_send [] PROGMEM = "start\n";
  64. static const char entry_magic_receive[] PROGMEM = "w25x20cl_enter\n";
  65. static const char entry_magic_cfm [] PROGMEM = "w25x20cl_cfm\n";
  66. struct block_t;
  67. extern struct block_t *block_buffer;
  68. //! @brief Enter an STK500 compatible Optiboot boot loader waiting for flashing the languages to an external flash memory.
  69. //! @return 1 if "start\n" was not sent. Optiboot was skipped
  70. //! @return 0 if "start\n" was sent. Optiboot ran normally. No need to send "start\n" in setup()
  71. uint8_t optiboot_xflash_enter()
  72. {
  73. // Make sure to check boot_app_magic as well. Since these bootapp flags are located right in the middle of the stack,
  74. // they can be unintentionally changed. As a workaround to the language upload problem, do not only check for one bit if it's set,
  75. // but rather test 33 bits for the correct value before exiting optiboot early.
  76. if ((boot_app_magic == BOOT_APP_MAGIC) && (boot_app_flags & BOOT_APP_FLG_USER0)) return 1;
  77. uint8_t ch;
  78. uint8_t rampz = 0;
  79. register uint16_t address = 0;
  80. register pagelen_t length;
  81. // Use the planner's queue for the receive / transmit buffers.
  82. // uint8_t *buff = (uint8_t*)block_buffer;
  83. uint8_t buff[260];
  84. // bitmap of pages to be written. Bit is set to 1 if the page has already been erased.
  85. uint8_t pages_erased = 0;
  86. // Handshake sequence: Initialize the serial line, flush serial line, send magic, receive magic.
  87. // If the magic is not received on time, or it is not received correctly, continue to the application.
  88. {
  89. wdt_reset();
  90. const char *ptr = entry_magic_send;
  91. const char *end = strlen_P(entry_magic_send) + ptr;
  92. const uint8_t selectedSerialPort_bak = selectedSerialPort;
  93. // Flush the serial line.
  94. while (RECV_READY) {
  95. wdt_reset();
  96. // Dummy register read (discard)
  97. (void)(*(char *)UDR0);
  98. }
  99. selectedSerialPort = 0; //switch to Serial0
  100. MYSERIAL.flush(); //clear RX buffer
  101. int SerialHead = rx_buffer.head;
  102. // Send the initial magic string.
  103. while (ptr != end)
  104. putch(pgm_read_byte(ptr ++));
  105. wdt_reset();
  106. // Wait for two seconds until a magic string (constant entry_magic) is received
  107. // from the serial line.
  108. ptr = entry_magic_receive;
  109. end = strlen_P(entry_magic_receive) + ptr;
  110. while (ptr != end) {
  111. unsigned long boot_timer = 2000000;
  112. // Beware of this volatile pointer - it is important since the while-cycle below
  113. // doesn't contain any obvious references to rx_buffer.head
  114. // thus the compiler is allowed to remove the check from the cycle
  115. // i.e. rx_buffer.head == SerialHead would not be checked at all!
  116. // With the volatile keyword the compiler generates exactly the same code as without it with only one difference:
  117. // the last brne instruction jumps onto the (*rx_head == SerialHead) check and NOT onto the wdr instruction bypassing the check.
  118. volatile int *rx_head = &rx_buffer.head;
  119. while (*rx_head == SerialHead) {
  120. wdt_reset();
  121. if ( --boot_timer == 0) {
  122. // Timeout expired, continue with the application.
  123. selectedSerialPort = selectedSerialPort_bak; //revert Serial setting
  124. return 0;
  125. }
  126. }
  127. ch = rx_buffer.buffer[SerialHead];
  128. SerialHead = (unsigned int)(SerialHead + 1) % RX_BUFFER_SIZE;
  129. if (pgm_read_byte(ptr ++) != ch)
  130. {
  131. // Magic was not received correctly, continue with the application
  132. selectedSerialPort = selectedSerialPort_bak; //revert Serial setting
  133. return 0;
  134. }
  135. wdt_reset();
  136. }
  137. cbi(UCSR0B, RXCIE0); //disable the MarlinSerial0 interrupt
  138. // Send the cfm magic string.
  139. ptr = entry_magic_cfm;
  140. end = strlen_P(entry_magic_cfm) + ptr;
  141. while (ptr != end)
  142. putch(pgm_read_byte(ptr ++));
  143. }
  144. spi_init();
  145. xflash_init();
  146. wdt_disable();
  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 >> 16))) == 0) {
  240. xflash_wait_busy();
  241. xflash_enable_wr();
  242. xflash_block64_erase(addr);
  243. pages_erased |= (1 << (addr >> 16));
  244. }
  245. xflash_wait_busy();
  246. xflash_enable_wr();
  247. xflash_page_program(addr, buff, savelength);
  248. xflash_wait_busy();
  249. xflash_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. xflash_wait_busy();
  266. xflash_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(XFLASH_SIGNATURE_0);
  275. putch(XFLASH_SIGNATURE_1);
  276. putch(XFLASH_SIGNATURE_2);
  277. }
  278. else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */
  279. // Adaboot no-wait mod
  280. wdt_enable(WDTO_15MS);
  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. }