Bootloader problem [SD,Atmega128L]

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the spm instruction is not located in the bootloader section, it will not do anything. Where is your code located?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Its in the bootloader, or at least it should be.

From make

avr-gcc  uart.o ../compact.o ../mmc_spi.o ../dir.o ../fat.o ../dos.o ../readraw.
o ../find_x.o ../mem-check.o doswrite3.o  -Wl,--section-start=.text=0xF000,--def
sym,__init_wdtcr__=0x07,--defsym,__init_mcucr__=0x80,-Map=doswrite3.map,--cref,-
-gc-sections,--relax -mmcu=atmega128 -o doswrite3.elf
avr-objcopy -O ihex doswrite3.elf doswrite3.hex
avr-objcopy -O binary doswrite3.elf doswrite3.bin
avr-size -B -d doswrite3.elf
   text    data     bss     dec     hex filename
   7490     144    1190    8824    2278 doswrite3.elf
avr-size -B -d  uart.o ../compact.o ../mmc_spi.o ../dir.o ../fat.o ../dos.o ../r
eadraw.o ../find_x.o ../mem-check.o doswrite3.o
   text    data     bss     dec     hex filename
     24       0       0      24      18 uart.o
      0       0       0       0       0 ../compact.o
   1190       0       0    1190     4a6 ../mmc_spi.o
   1214       0       4    1218     4c2 ../dir.o
   1500       0      34    1534     5fe ../fat.o
   1182       0      32    1214     4be ../dos.o
      0       0       0       0       0 ../readraw.o
    110       0       0     110      6e ../find_x.o
     48       0       0      48      30 ../mem-check.o
    590     143       4     737     2e1 doswrite3.o
rm ../dir.o ../readraw.o uart.o ../find_x.o ../mem-check.o ../compact.o ../dos.o
 ../fat.o ../mmc_spi.o doswrite3.o
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

-Wl,--section-start=.text=0xF000

that should be a byte address, not a word address. No SPM for you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes! that was the problem, and I had to remove the wait loop, but its working now.

Thank you very much :D