Skip to content

Commit a94030c

Browse files
committed
Issue #181, 505, 543, 544 fixes, see comments in code
1 parent f157d72 commit a94030c

File tree

1 file changed

+81
-30
lines changed

1 file changed

+81
-30
lines changed

stk500boot.c

+81-30
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,26 @@ Description: add timeout feature like previous Wiring bootloader
8080
//* Aug 23, 2010 <MLS> Added support for atmega2561
8181
//* Aug 26, 2010 <MLS> Removed support for BOOT_BY_SWITCH
8282
//* Sep 8, 2010 <MLS> Added support for atmega16
83-
//* Nov 9, 2010 <MLS> Fixed bug that 3 !!! in code would cause it to jump to monitor
83+
//* Nov 9, 2010 <MLS> Issue 392:Fixed bug that 3 !!! in code would cause it to jump to monitor
8484
//* Jun 24, 2011 <MLS> Removed analogRead (was not used)
85+
//* Dec 29, 2011 <MLS> Issue 181: added watch dog timmer support
86+
//* Dec 29, 2011 <MLS> Issue 505: bootloader is comparing the seqNum to 1 or the current sequence
87+
//* Jan 1, 2012 <MLS> Issue 543: CMD_CHIP_ERASE_ISP now returns STATUS_CMD_FAILED instead of STATUS_CMD_OK
88+
//* Jan 1, 2012 <MLS> Issue 543: Write EEPROM now does something (NOT TESTED)
89+
//* Jan 1, 2012 <MLS> Issue 544: stk500v2 bootloader doesn't support reading fuses
8590
//************************************************************************
8691

87-
92+
//************************************************************************
93+
//* these are used to test issues
94+
//* http://code.google.com/p/arduino/issues/detail?id=505
95+
//* Reported by mark.stubbs, Mar 14, 2011
96+
//* The STK500V2 bootloader is comparing the seqNum to 1 or the current sequence
97+
//* (IE: Requiring the sequence to be 1 or match seqNum before continuing).
98+
//* The correct behavior is for the STK500V2 to accept the PC's sequence number, and echo it back for the reply message.
99+
#define _FIX_ISSUE_505_
100+
//************************************************************************
101+
//* Issue 181: added watch dog timmer support
102+
#define _FIX_ISSUE_181_
88103

89104
#include <inttypes.h>
90105
#include <avr/io.h>
@@ -112,7 +127,7 @@ Description: add timeout feature like previous Wiring bootloader
112127
#define EEMWE 2
113128
#endif
114129

115-
#define _DEBUG_SERIAL_
130+
//#define _DEBUG_SERIAL_
116131
//#define _DEBUG_WITH_LEDS_
117132

118133

@@ -420,7 +435,7 @@ void __jumpMain(void)
420435

421436
asm volatile ( ".set __stack, %0" :: "i" (RAMEND) );
422437

423-
//* et stack pointer to top of RAM
438+
//* set stack pointer to top of RAM
424439

425440
asm volatile ( "ldi 16, %0" :: "i" (RAMEND >> 8) );
426441
asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_HI_ADDR) );
@@ -509,6 +524,8 @@ uint32_t count = 0;
509524
return UART_DATA_REG;
510525
}
511526

527+
//* for watch dog timer startup
528+
void (*app_start)(void) = 0x0000;
512529

513530

514531
//*****************************************************************************
@@ -540,14 +557,35 @@ int main(void)
540557
asm volatile ( "ldi 16, %0" :: "i" (RAMEND & 0x0ff) );
541558
asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_LO_ADDR) );
542559

560+
#ifdef _FIX_ISSUE_181_
561+
//************************************************************************
562+
//* Dec 29, 2011 <MLS> Issue #181, added watch dog timmer support
563+
//* handle the watch dog timer
564+
uint8_t mcuStatusReg;
565+
mcuStatusReg = MCUSR;
566+
567+
__asm__ __volatile__ ("cli");
568+
__asm__ __volatile__ ("wdr");
569+
MCUSR = 0;
570+
WDTCSR |= _BV(WDCE) | _BV(WDE);
571+
WDTCSR = 0;
572+
__asm__ __volatile__ ("sei");
573+
// check if WDT generated the reset, if so, go straight to app
574+
if (mcuStatusReg & _BV(WDRF))
575+
{
576+
app_start();
577+
}
578+
//************************************************************************
579+
#endif
543580

544581

545582
boot_timer = 0;
546583
boot_state = 0;
547584

548585
#ifdef BLINK_LED_WHILE_WAITING
549-
boot_timeout = 90000; //* should be about 4 seconds
586+
// boot_timeout = 90000; //* should be about 4 seconds
550587
// boot_timeout = 170000;
588+
boot_timeout = 20000; //* should be about 1 second
551589
#else
552590
boot_timeout = 3500000; // 7 seconds , approx 2us per step when optimize "s"
553591
#endif
@@ -678,6 +716,11 @@ int main(void)
678716
break;
679717

680718
case ST_GET_SEQ_NUM:
719+
#ifdef _FIX_ISSUE_505_
720+
seqNum = c;
721+
msgParseState = ST_MSG_SIZE_1;
722+
checksum ^= c;
723+
#else
681724
if ( (c == 1) || (c == seqNum) )
682725
{
683726
seqNum = c;
@@ -688,6 +731,7 @@ int main(void)
688731
{
689732
msgParseState = ST_START;
690733
}
734+
#endif
691735
break;
692736

693737
case ST_MSG_SIZE_1:
@@ -754,20 +798,41 @@ int main(void)
754798
unsigned char signatureIndex = msgBuffer[6];
755799

756800
if ( signatureIndex == 0 )
757-
answerByte = (SIGNATURE_BYTES >>16) & 0x000000FF;
801+
{
802+
answerByte = (SIGNATURE_BYTES >> 16) & 0x000000FF;
803+
}
758804
else if ( signatureIndex == 1 )
805+
{
759806
answerByte = (SIGNATURE_BYTES >> 8) & 0x000000FF;
807+
}
760808
else
809+
{
761810
answerByte = SIGNATURE_BYTES & 0x000000FF;
811+
}
762812
}
763813
else if ( msgBuffer[4] & 0x50 )
764814
{
765-
answerByte = 0; //read fuse/lock bits not implemented, return dummy value
815+
//* Issue 544: stk500v2 bootloader doesn't support reading fuses
816+
//* I cant find the docs that say what these are supposed to be but this was figured out by trial and error
817+
// answerByte = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
818+
// answerByte = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
819+
// answerByte = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS);
820+
if (msgBuffer[4] == 0x50)
821+
{
822+
answerByte = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
823+
}
824+
else if (msgBuffer[4] == 0x58)
825+
{
826+
answerByte = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
827+
}
828+
else
829+
{
830+
answerByte = 0;
831+
}
766832
}
767833
else
768834
{
769835
answerByte = 0; // for all others command are not implemented, return dummy value for AVRDUDE happy <Worapoht>
770-
// flag = 1; // Remark this line for AVRDUDE <Worapoht>
771836
}
772837
if ( !flag )
773838
{
@@ -903,7 +968,8 @@ int main(void)
903968
case CMD_CHIP_ERASE_ISP:
904969
eraseAddress = 0;
905970
msgLength = 2;
906-
msgBuffer[1] = STATUS_CMD_OK;
971+
// msgBuffer[1] = STATUS_CMD_OK;
972+
msgBuffer[1] = STATUS_CMD_FAILED; //* isue 543, return FAILED instead of OK
907973
break;
908974

909975
case CMD_LOAD_ADDRESS:
@@ -954,8 +1020,9 @@ int main(void)
9541020
}
9551021
else
9561022
{
957-
#if (!defined(__AVR_ATmega1280__) && !defined(__AVR_ATmega2560__) && !defined(__AVR_ATmega2561__) && !defined(__AVR_ATmega1284P__) && !defined(__AVR_ATmega640__))
958-
// #if (defined(EEARL) && defined(EEARH) && defined(EEMWE) && defined(EEWE) && defined(EEDR))
1023+
//* issue 543, this should work, It has not been tested.
1024+
// #if (!defined(__AVR_ATmega1280__) && !defined(__AVR_ATmega2560__) && !defined(__AVR_ATmega2561__) && !defined(__AVR_ATmega1284P__) && !defined(__AVR_ATmega640__))
1025+
#if (defined(EEARL) && defined(EEARH) && defined(EEMWE) && defined(EEWE) && defined(EEDR))
9591026
/* write EEPROM */
9601027
do {
9611028
EEARL = address; // Setup EEPROM address
@@ -1158,25 +1225,7 @@ unsigned long gEepromIndex;
11581225
#define false 0
11591226

11601227
#include "avr_cpunames.h"
1161-
/*
1162-
#if defined(__AVR_ATmega128__)
1163-
#define kCPU_NAME "ATmega128"
1164-
#elif defined(__AVR_ATmega1280__)
1165-
#define kCPU_NAME "ATmega1280"
1166-
#elif defined(__AVR_ATmega1281__)
1167-
#define kCPU_NAME "ATmega1281"
1168-
#elif defined(__AVR_ATmega2560__)
1169-
#define kCPU_NAME "ATmega2560"
1170-
#elif defined(__AVR_ATmega2561__)
1171-
#define kCPU_NAME "ATmega2561"
1172-
#elif defined(__AVR_ATmega1284P__)
1173-
#define kCPU_NAME "ATmega1284P"
1174-
#elif defined(__AVR_ATmega640__)
1175-
#define kCPU_NAME "ATmega640"
1176-
#else
1177-
#error cpu name not defined
1178-
#endif
1179-
*/
1228+
11801229
#ifndef _AVR_CPU_NAME_
11811230
#error cpu name not defined
11821231
#endif
@@ -1577,6 +1626,8 @@ int errorCount;
15771626

15781627

15791628
#if (FLASHEND > 0x08000)
1629+
//* this includes the interrupt names for the monitor portion. There is no longer enough
1630+
//* memory to include this
15801631
// #include "avrinterruptnames.h"
15811632
// #ifndef _INTERRUPT_NAMES_DEFINED_
15821633
// #warning Interrupt vectors not defined

0 commit comments

Comments
 (0)