optiboot_w25x20cl.cpp 10.0 KB

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