So I have a design that is extremely similar to the moteino mega and am using the dual optiboot bootloader to fascilitate over the air programming. For some reason the bytes written to the flash memory before the reset are changed during reset and programming is not executed.
Here are the differences in the board and the changes in the bootloader. This prototype moved a few pins around and foolishly uses a separate ldo from the mcu to power the flash memory. That LDO is on Pin B0 and the flashSS is now on B1. The changes to the bootloader are as follows
accounting for the new pin to power on the LDO
#elif defined (__AVR_ATmega1284P__) || defined (__AVR_ATmega644P__)
#define FLASHSS_DDR DDRB
#define FLASHSS_PORT PORTB
#define FLASHSS PINB1
#define SS PINB4
////////////////////////////////////////
#define MEMON_DDR DDRB
#define MEMON_PORT PORTB
#define MEMON_PIN PINB0
#endif
and turning on the LDO
void CheckFlashImage() {
#ifdef DEBUG_ON
putch('F');
#endif
watchdogConfig(WATCHDOG_OFF);
// POWER ON THE FLASH MEMORY -ADDED BY CHARLIE
MEMON_DDR |= _BV(MEMON_PIN);
MEMON_PORT |= _BV(MEMON_PIN);
/////////////////////////////////////////////////
The code has been delivered and verified intact and uncorrupted to the device over the air and has been successfully and accurately written to the flash. After the code is verified I add the FLXIMG:<Size>: to the first 10 address on the flash and issue the reset. For these test I am triggering the reset manually. The reset itself is caused by a watch dog time out.
Here is the code to finish that final part, as I just described
flash.writeByte(0,(byte)'F');while(flash.busy());
flash.writeByte(1,(byte)'L');while(flash.busy());
flash.writeByte(2,(byte)'X');while(flash.busy());
flash.writeByte(3,(byte)'I');while(flash.busy());
flash.writeByte(4,(byte)'M');while(flash.busy());
flash.writeByte(5,(byte)'G');while(flash.busy());
flash.writeByte(6,(byte)':');while(flash.busy());
flash.writeByte(9,(byte)':');while(flash.busy());
flash.writeByte(7,(ProgramSize>>8));while(flash.busy());
flash.writeByte(8,(ProgramSize&0xFF));while(flash.busy());
for(int i=0;i<7;i++){
Serial.print(char(flash.readByte(i)));
}
uint16_t Size=(flash.readByte(7)<<8)|(flash.readByte(8)&0xFF);
Serial.print(Size);
Serial.println(char(flash.readByte(9)));
Serial.println("Press enter to reset");
Serial.readString();
while(!Serial.available()){
wdt_reset();
}
delay(30000);
Here is the output from the serial monitor of the device
The first line is the first 10 bytes read from the flash memory cast as their correct data types, which verifies that the code has written correctly. The second to last line copied is the same bytes read after reset, and for good measure I also printed those same bytes hex values in the final line.
04:17:38.557 -> FLXIMG:65534:
04:17:38.557 -> Press enter to reset
04:18:04.361 -> ------------------------------------------------------------------
04:18:04.361 -> Starting--------------------------
04:18:04.361 -> Firmware Version: 0x1
04:18:04.361 -> ------------------------------------------------------------------
04:18:09.347 -> SerialNumber: E C A 5
04:18:09.347 -> Report Time is: 16:30
04:18:10.965 -> Turning off Radio
04:18:11.001 -> NetID: 14. 12. 10. 4
04:18:11.001 -> NodeID: 3
04:18:11.001 -> Frequency: 915
04:18:11.367 -> Radio Started
04:18:11.367 ->
04:18:11.367 -> 0 0 22 3 0 4 0 20 0 0 0 20 0 0 20 20
What I'm trying to understand is what in the bootloader could be causing this?
I thought maybe the flashmemory wasn't turned on correctly so I jumped a wire from 3v3 to its enable pin so the flash memory never actually lost power but the problem persisted.
If someone has some insight please please let me know. I am really scratching my head here