Hi,
I am trying to make a bootloader which will load a binary file from a microSD card.
I have been working with AVR for 6months, and before that PIC for a year, so I have some knowledge about C.
The bootloader is working, well, except for the flashing...
I have looked at the tutorial as a base, but is using another fat base, since I am already using that in the main program.
The chip is a Atmega128L running at 8mhZ 3.3V
SDcard is working, both in bootloader and regular program, it reads out the file, and calls the writeflash for each page, but the program is not loaded.
When the flash is read and converted to a bin file, its full of 0xFF.
Heres my code, is someone able to spot any errors ?
Any help will be much appreciated!
#include#include #include #include #include #include #include #include #include "mydefs.h" #include "../dos.h" #include "uart.h" #include "bootcfg.h" #include "bootldr.h" #define FLASHFILE "new.fsh" #define PROG_START 0x0000 #define PAGESIZE 256 uint32_t FlashAddr = 0; unsigned int bufptr, pagptr; void write_one_page(unsigned char *buf); FILE uart0_str = FDEV_SETUP_STREAM(uart_putchar0, NULL, _FDEV_SETUP_WRITE); int main(void) { /* shut down the watchdog timer and clear any pending interrupts */ wdt_reset(); MCUSR &= ~(1<<WDRF); wdt_disable(); stdout = &uart0_str; _delay_ms(1000); SFIOR&=!(1<<PUD); //Pullups Enable MMC_IO_Init(); uart_init(); sei(); //seriel bruger interrupt if(GetDriveInformation()!=F_OK) // get drive parameters { printf("No Flash !"); while(1); } if (FindName(FLASHFILE)==FULL_MATCH) { printf("Flash file found\r\n"); boot_rww_enable_safe(); //Allow programming of progmem unsigned char buffer[PAGESIZE]; uint16_t len = 0; unsigned char result=Fopen(FLASHFILE,F_READ); if(result==F_OK) { while ((len = Fread(buffer,PAGESIZE)) > 0) { printf("Read loop (/)lu\r\n",FlashAddr); //Maybe need padding if buffer is not full ? //Erase progmem page //Write page while(boot_rww_busy()) {} write_one_page(buffer); FlashAddr += len; } //If not EOF then Loop Fclose(); } } else { printf("No flash file found\r\n"); } printf("Flashing done - jumping.\r\n"); //Reset/jump to program boot_rww_enable(); //enable application section (*((void(*)(void))PROG_START))(); //jump for(;;) // loop forever { } } //write one Flash page void write_one_page(unsigned char *buf) { boot_page_erase(FlashAddr); //erase one Flash page boot_spm_busy_wait(); for(pagptr = 0; pagptr < SPM_PAGESIZE; pagptr += 2) //fill data to Flash buffer { boot_page_fill(pagptr, buf[pagptr] | (buf[pagptr + 1] << 8)); } boot_page_write(FlashAddr); //write buffer to one Flash page boot_spm_busy_wait(); //wait Flash page write finish }