optiboot_w25x20cl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #if 0
  16. #define W25X20CL_SIGNATURE_0 9
  17. #define W25X20CL_SIGNATURE_1 8
  18. #define W25X20CL_SIGNATURE_2 7
  19. #else
  20. //FIXME this is a signature of ATmega2560!
  21. #define W25X20CL_SIGNATURE_0 0x1E
  22. #define W25X20CL_SIGNATURE_1 0x98
  23. #define W25X20CL_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. static const char entry_magic_send [] PROGMEM = "start\n";
  62. static const char entry_magic_receive[] PROGMEM = "w25x20cl_enter\n";
  63. static const char entry_magic_cfm [] PROGMEM = "w25x20cl_cfm\n";
  64. struct block_t;
  65. extern struct block_t *block_buffer;
  66. //! @brief Enter an STK500 compatible Optiboot boot loader waiting for flashing the languages to an external flash memory.
  67. //! @return 1 if "start\n" was not sent. Optiboot was skipped
  68. //! @return 0 if "start\n" was sent. Optiboot ran normally. No need to send "start\n" in setup()
  69. uint8_t optiboot_w25x20cl_enter()
  70. {
  71. // Make sure to check boot_app_magic as well. Since these bootapp flags are located right in the middle of the stack,
  72. // they can be unintentionally changed. As a workaround to the language upload problem, do not only check for one bit if it's set,
  73. // but rather test 33 bits for the correct value before exiting optiboot early.
  74. if ((boot_app_magic == BOOT_APP_MAGIC) && (boot_app_flags & BOOT_APP_FLG_USER0)) return 1;
  75. uint8_t ch;
  76. uint8_t rampz = 0;
  77. register uint16_t address = 0;
  78. register pagelen_t length;
  79. // Use the planner's queue for the receive / transmit buffers.
  80. // uint8_t *buff = (uint8_t*)block_buffer;
  81. uint8_t buff[260];
  82. // bitmap of pages to be written. Bit is set to 1 if the page has already been erased.
  83. uint8_t pages_erased = 0;
  84. // Handshake sequence: Initialize the serial line, flush serial line, send magic, receive magic.
  85. // If the magic is not received on time, or it is not received correctly, continue to the application.
  86. {
  87. wdt_reset();
  88. const char *ptr = entry_magic_send;
  89. const char *end = strlen_P(entry_magic_send) + ptr;
  90. const uint8_t selectedSerialPort_bak = selectedSerialPort;
  91. // Flush the serial line.
  92. while (RECV_READY) {
  93. wdt_reset();
  94. // Dummy register read (discard)
  95. (void)(*(char *)UDR0);
  96. }
  97. selectedSerialPort = 0; //switch to Serial0
  98. MYSERIAL.flush(); //clear RX buffer
  99. int SerialHead = rx_buffer.head;
  100. // Send the initial magic string.
  101. while (ptr != end)
  102. putch(pgm_read_byte(ptr ++));
  103. wdt_reset();
  104. // Wait for two seconds until a magic string (constant entry_magic) is received
  105. // from the serial line.
  106. ptr = entry_magic_receive;
  107. end = strlen_P(entry_magic_receive) + ptr;
  108. while (ptr != end) {
  109. unsigned long boot_timer = 2000000;
  110. // Beware of this volatile pointer - it is important since the while-cycle below
  111. // doesn't contain any obvious references to rx_buffer.head
  112. // thus the compiler is allowed to remove the check from the cycle
  113. // i.e. rx_buffer.head == SerialHead would not be checked at all!
  114. // With the volatile keyword the compiler generates exactly the same code as without it with only one difference:
  115. // the last brne instruction jumps onto the (*rx_head == SerialHead) check and NOT onto the wdr instruction bypassing the check.
  116. volatile int *rx_head = &rx_buffer.head;
  117. while (*rx_head == SerialHead) {
  118. wdt_reset();
  119. if ( --boot_timer == 0) {
  120. // Timeout expired, continue with the application.
  121. selectedSerialPort = selectedSerialPort_bak; //revert Serial setting
  122. return 0;
  123. }
  124. }
  125. ch = rx_buffer.buffer[SerialHead];
  126. SerialHead = (unsigned int)(SerialHead + 1) % RX_BUFFER_SIZE;
  127. if (pgm_read_byte(ptr ++) != ch)
  128. {
  129. // Magic was not received correctly, continue with the application
  130. selectedSerialPort = selectedSerialPort_bak; //revert Serial setting
  131. return 0;
  132. }
  133. wdt_reset();
  134. }
  135. cbi(UCSR0B, RXCIE0); //disable the MarlinSerial0 interrupt
  136. // Send the cfm magic string.
  137. ptr = entry_magic_cfm;
  138. end = strlen_P(entry_magic_cfm) + ptr;
  139. while (ptr != end)
  140. putch(pgm_read_byte(ptr ++));
  141. }
  142. spi_init();
  143. w25x20cl_init();
  144. wdt_disable();
  145. /* Forever loop: exits by causing WDT reset */
  146. for (;;) {
  147. /* get character from UART */
  148. ch = getch();
  149. if(ch == STK_GET_PARAMETER) {
  150. unsigned char which = getch();
  151. verifySpace();
  152. /*
  153. * Send optiboot version as "SW version"
  154. * Note that the references to memory are optimized away.
  155. */
  156. if (which == STK_SW_MINOR) {
  157. putch(optiboot_version & 0xFF);
  158. } else if (which == STK_SW_MAJOR) {
  159. putch(optiboot_version >> 8);
  160. } else {
  161. /*
  162. * GET PARAMETER returns a generic 0x03 reply for
  163. * other parameters - enough to keep Avrdude happy
  164. */
  165. putch(0x03);
  166. }
  167. }
  168. else if(ch == STK_SET_DEVICE) {
  169. // SET DEVICE is ignored
  170. getNch(20);
  171. }
  172. else if(ch == STK_SET_DEVICE_EXT) {
  173. // SET DEVICE EXT is ignored
  174. getNch(5);
  175. }
  176. else if(ch == STK_LOAD_ADDRESS) {
  177. // LOAD ADDRESS
  178. uint16_t newAddress;
  179. // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
  180. // Send the binary data by nibbles to avoid transmitting the ';' character.
  181. newAddress = getch();
  182. newAddress |= getch();
  183. newAddress |= (((uint16_t)getch()) << 8);
  184. newAddress |= (((uint16_t)getch()) << 8);
  185. // Transfer top bit to LSB in rampz
  186. if (newAddress & 0x8000)
  187. rampz |= 0x01;
  188. else
  189. rampz &= 0xFE;
  190. newAddress += newAddress; // Convert from word address to byte address
  191. address = newAddress;
  192. verifySpace();
  193. }
  194. else if(ch == STK_UNIVERSAL) {
  195. // LOAD_EXTENDED_ADDRESS is needed in STK_UNIVERSAL for addressing more than 128kB
  196. if ( AVR_OP_LOAD_EXT_ADDR == getch() ) {
  197. // get address
  198. getch(); // get '0'
  199. rampz = (rampz & 0x01) | ((getch() << 1) & 0xff); // get address and put it in rampz
  200. getNch(1); // get last '0'
  201. // response
  202. putch(0x00);
  203. }
  204. else {
  205. // everything else is ignored
  206. getNch(3);
  207. putch(0x00);
  208. }
  209. }
  210. /* Write memory, length is big endian and is in bytes */
  211. else if(ch == STK_PROG_PAGE) {
  212. // PROGRAM PAGE - we support flash programming only, not EEPROM
  213. uint8_t desttype;
  214. uint8_t *bufPtr;
  215. pagelen_t savelength;
  216. // Read the page length, with the length transferred each nibble separately to work around
  217. // the Prusa's USB to serial infamous semicolon issue.
  218. length = ((pagelen_t)getch()) << 8;
  219. length |= ((pagelen_t)getch()) << 8;
  220. length |= getch();
  221. length |= getch();
  222. savelength = length;
  223. // Read the destination type. It should always be 'F' as flash.
  224. desttype = getch();
  225. // read a page worth of contents
  226. bufPtr = buff;
  227. do *bufPtr++ = getch();
  228. while (--length);
  229. // Read command terminator, start reply
  230. verifySpace();
  231. if (desttype == 'E') {
  232. while (1) ; // Error: wait for WDT
  233. } else {
  234. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  235. // During a single bootloader run, only erase a 64kB block once.
  236. // An 8bit bitmask 'pages_erased' covers 512kB of FLASH memory.
  237. if ((address == 0) && (pages_erased & (1 << (addr >> 16))) == 0) {
  238. w25x20cl_wait_busy();
  239. w25x20cl_enable_wr();
  240. w25x20cl_block64_erase(addr);
  241. pages_erased |= (1 << (addr >> 16));
  242. }
  243. w25x20cl_wait_busy();
  244. w25x20cl_enable_wr();
  245. w25x20cl_page_program(addr, buff, savelength);
  246. w25x20cl_wait_busy();
  247. w25x20cl_disable_wr();
  248. }
  249. }
  250. /* Read memory block mode, length is big endian. */
  251. else if(ch == STK_READ_PAGE) {
  252. uint32_t addr = (((uint32_t)rampz) << 16) | address;
  253. register pagelen_t i;
  254. // Read the page length, with the length transferred each nibble separately to work around
  255. // the Prusa's USB to serial infamous semicolon issue.
  256. length = ((pagelen_t)getch()) << 8;
  257. length |= ((pagelen_t)getch()) << 8;
  258. length |= getch();
  259. length |= getch();
  260. // Read the destination type. It should always be 'F' as flash. It is not checked.
  261. (void)getch();
  262. verifySpace();
  263. w25x20cl_wait_busy();
  264. w25x20cl_rd_data(addr, buff, length);
  265. for (i = 0; i < length; ++ i)
  266. putch(buff[i]);
  267. }
  268. /* Get device signature bytes */
  269. else if(ch == STK_READ_SIGN) {
  270. // READ SIGN - return what Avrdude wants to hear
  271. verifySpace();
  272. putch(W25X20CL_SIGNATURE_0);
  273. putch(W25X20CL_SIGNATURE_1);
  274. putch(W25X20CL_SIGNATURE_2);
  275. }
  276. else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */
  277. // Adaboot no-wait mod
  278. wdt_enable(WDTO_15MS);
  279. verifySpace();
  280. }
  281. else {
  282. // This covers the response to commands like STK_ENTER_PROGMODE
  283. verifySpace();
  284. }
  285. putch(STK_OK);
  286. }
  287. }