123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- #include "Marlin.h"
- #include "xflash.h"
- #include "stk500.h"
- #include "bootapp.h"
- #include <avr/wdt.h>
- #define OPTIBOOT_MAJVER 6
- #define OPTIBOOT_CUSTOMVER 0
- #define OPTIBOOT_MINVER 2
- static unsigned const int __attribute__((section(".version")))
- optiboot_version = 256*(OPTIBOOT_MAJVER + OPTIBOOT_CUSTOMVER) + OPTIBOOT_MINVER;
- #if 0
- #define XFLASH_SIGNATURE_0 9
- #define XFLASH_SIGNATURE_1 8
- #define XFLASH_SIGNATURE_2 7
- #else
- #define XFLASH_SIGNATURE_0 0x1E
- #define XFLASH_SIGNATURE_1 0x98
- #define XFLASH_SIGNATURE_2 0x01
- #endif
- #define RECV_READY ((UCSR0A & _BV(RXC0)) != 0)
- static uint8_t getch(void) {
- uint8_t ch;
- while(! RECV_READY) ;
- if (!(UCSR0A & _BV(FE0))) {
-
- wdt_reset();
- }
- ch = UDR0;
- return ch;
- }
- static void putch(char ch) {
- while (!(UCSR0A & _BV(UDRE0)));
- UDR0 = ch;
- }
- static void verifySpace() {
- if (getch() != CRC_EOP) {
- putch(STK_FAILED);
- wdt_enable(WDTO_15MS);
- while (1)
- ;
- }
- putch(STK_INSYNC);
- }
- static void getNch(uint8_t count) {
- do getch(); while (--count);
- verifySpace();
- }
- typedef uint16_t pagelen_t;
- static const char entry_magic_send [] PROGMEM = "start\n";
- static const char entry_magic_receive[] PROGMEM = "w25x20cl_enter\n";
- static const char entry_magic_cfm [] PROGMEM = "w25x20cl_cfm\n";
- struct block_t;
- extern struct block_t *block_buffer;
- uint8_t optiboot_xflash_enter()
- {
- if ((boot_app_magic == BOOT_APP_MAGIC) && (boot_app_flags & BOOT_APP_FLG_USER0)) return 1;
- uint8_t ch;
- uint8_t rampz = 0;
- register uint16_t address = 0;
- register pagelen_t length;
-
- uint8_t buff[260];
-
- uint8_t pages_erased = 0;
-
-
- {
- wdt_reset();
- const char *ptr = entry_magic_send;
- const char *end = strlen_P(entry_magic_send) + ptr;
- const uint8_t selectedSerialPort_bak = selectedSerialPort;
-
- while (RECV_READY) {
- wdt_reset();
-
- (void)(*(char *)UDR0);
- }
- selectedSerialPort = 0;
- MYSERIAL.flush();
- int SerialHead = rx_buffer.head;
-
- while (ptr != end)
- putch(pgm_read_byte(ptr ++));
- wdt_reset();
-
-
- ptr = entry_magic_receive;
- end = strlen_P(entry_magic_receive) + ptr;
- while (ptr != end) {
- unsigned long boot_timer = 2000000;
-
-
-
-
-
-
- volatile int *rx_head = &rx_buffer.head;
- while (*rx_head == SerialHead) {
- wdt_reset();
- if ( --boot_timer == 0) {
-
- selectedSerialPort = selectedSerialPort_bak;
- return 0;
- }
- }
- ch = rx_buffer.buffer[SerialHead];
- SerialHead = (unsigned int)(SerialHead + 1) % RX_BUFFER_SIZE;
- if (pgm_read_byte(ptr ++) != ch)
- {
-
- selectedSerialPort = selectedSerialPort_bak;
- return 0;
- }
- wdt_reset();
- }
- cbi(UCSR0B, RXCIE0);
-
- ptr = entry_magic_cfm;
- end = strlen_P(entry_magic_cfm) + ptr;
- while (ptr != end)
- putch(pgm_read_byte(ptr ++));
- }
- spi_init();
- xflash_init();
- wdt_disable();
-
- for (;;) {
-
- ch = getch();
- if(ch == STK_GET_PARAMETER) {
- unsigned char which = getch();
- verifySpace();
-
- if (which == STK_SW_MINOR) {
- putch(optiboot_version & 0xFF);
- } else if (which == STK_SW_MAJOR) {
- putch(optiboot_version >> 8);
- } else {
-
- putch(0x03);
- }
- }
- else if(ch == STK_SET_DEVICE) {
-
- getNch(20);
- }
- else if(ch == STK_SET_DEVICE_EXT) {
-
- getNch(5);
- }
- else if(ch == STK_LOAD_ADDRESS) {
-
- uint16_t newAddress;
-
-
- newAddress = getch();
- newAddress |= getch();
- newAddress |= (((uint16_t)getch()) << 8);
- newAddress |= (((uint16_t)getch()) << 8);
-
- if (newAddress & 0x8000)
- rampz |= 0x01;
- else
- rampz &= 0xFE;
- newAddress += newAddress;
- address = newAddress;
- verifySpace();
- }
- else if(ch == STK_UNIVERSAL) {
-
- if ( AVR_OP_LOAD_EXT_ADDR == getch() ) {
-
- getch();
- rampz = (rampz & 0x01) | ((getch() << 1) & 0xff);
- getNch(1);
-
- putch(0x00);
- }
- else {
-
- getNch(3);
- putch(0x00);
- }
- }
-
- else if(ch == STK_PROG_PAGE) {
-
- uint8_t desttype;
- uint8_t *bufPtr;
- pagelen_t savelength;
-
-
- length = ((pagelen_t)getch()) << 8;
- length |= ((pagelen_t)getch()) << 8;
- length |= getch();
- length |= getch();
- savelength = length;
-
- desttype = getch();
-
- bufPtr = buff;
- do *bufPtr++ = getch();
- while (--length);
-
- verifySpace();
- if (desttype == 'E') {
- while (1) ;
- } else {
- uint32_t addr = (((uint32_t)rampz) << 16) | address;
-
-
- if ((address == 0) && (pages_erased & (1 << (addr >> 16))) == 0) {
- xflash_wait_busy();
- xflash_enable_wr();
- xflash_block64_erase(addr);
- pages_erased |= (1 << (addr >> 16));
- }
- xflash_wait_busy();
- xflash_enable_wr();
- xflash_page_program(addr, buff, savelength);
- xflash_wait_busy();
- xflash_disable_wr();
- }
- }
-
- else if(ch == STK_READ_PAGE) {
- uint32_t addr = (((uint32_t)rampz) << 16) | address;
- register pagelen_t i;
-
-
- length = ((pagelen_t)getch()) << 8;
- length |= ((pagelen_t)getch()) << 8;
- length |= getch();
- length |= getch();
-
- (void)getch();
- verifySpace();
- xflash_wait_busy();
- xflash_rd_data(addr, buff, length);
- for (i = 0; i < length; ++ i)
- putch(buff[i]);
- }
-
- else if(ch == STK_READ_SIGN) {
-
- verifySpace();
- putch(XFLASH_SIGNATURE_0);
- putch(XFLASH_SIGNATURE_1);
- putch(XFLASH_SIGNATURE_2);
- }
- else if (ch == STK_LEAVE_PROGMODE) {
-
- wdt_enable(WDTO_15MS);
- verifySpace();
- }
- else {
-
- verifySpace();
- }
- putch(STK_OK);
- }
- }
|