boot_page_write() doesn't actually write anything

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

Hello. I'm trying to write my own bootloader (just to understand how to use self-programming) to boot a program from EEPROM. Bootloader is written (the function to write a flash page is from www.nongnu.org) and the data is stored inside Flash and EEPROM. Here is the code :

 

//bootloader.c
#include "bootloader.h"

void writePage(uint32_t adress, uint8_t* buffer) {
	//Declare essential variables
	uint16_t offset, word;
	uint8_t int_reg;

	//Disable interrupts
	int_reg = SREG;
	cli();

	//Erase the memory
	eeprom_busy_wait ();
	boot_page_erase (adress);
	boot_spm_busy_wait ();

	//Fill a temporary page
	for (offset = 0; offset < SPM_PAGESIZE; offset+= 2) {
		//Create a bloock of data to store in the memory
		word = *buffer++;
		word += (*buffer++) << 8;

		//Write the word
		boot_page_fill(adress + offset, word);
	}

	//Write the page into the flash memory
	boot_page_write(adress);
	boot_spm_busy_wait();

	//
	boot_rww_enable();

	//Re-enable interrupts
	SREG = int_reg;
}

inline void boot(void) {
	//Boot the program
	asm volatile ("rjmp 0");
}

//bootloader.h
#ifndef BOOTLOADER_H
#define BOOTLOADER_H

#include <avr/boot.h>
#include <avr/io.h>
#include <avr/interrupt.h>

void loadProgram(void);
void writePage(uint32_t, uint8_t*);

extern void boot(void);

#endif

//main.c
#include <util/delay.h>
#include <avr/eeprom.h>

#include "bootloader.h"

//#define F_CPU 8000000UL

int main(void) {
	//Start of the bootloader (LED indicator)
	/*
	DDRB |= 0x1;
	PORTB |= 0x1;
	PORTB &= ~(0x1);
	*/

	DDRB |= 0x1;

	uint8_t offset, buffer[SPM_PAGESIZE];

	for (offset = 0; offset < SPM_PAGESIZE; ++offset) {
		buffer[offset] = eeprom_read_byte((uint8_t*) offset);
	}

	writePage(0, buffer);

	//Load the program from the EEPROM
	//loadProgram();

	//Boot the program
	boot();
}

The problem is there is no signs of booting my program. Dump of the flash gives me 0xFF's except for the bootloader part (BOOTRST is 0, BOOTSZ is 01 or 512 words). The bootloader is compiled with an offset "-Ttext=0x0e00". The MCU is ATMega8A. I'm working on Linux, so LED is the only way to confirm things. The buffer seems to be ok, so I don't have a clue, what to do. I'd appreciate any help.

This topic has a solution.

Programming for AVR is quite easy. Except for you are on fire, your computer is on fire and everything is on fire because it's hell.

Last Edited: Tue. Feb 25, 2020 - 08:54 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

E00 doesn't sound right for an 8K chip

 

E00 is 3584 bytes that isn't even half way up 8K.

 

Try converting that to a byte address so 1C00 and I think you'll have more mileage.

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

I used a fuse calculator from the web and it gave me such an adress. I'll try what you've written.

 

Update : yep, that was it! Thank you for your help!

Programming for AVR is quite easy. Except for you are on fire, your computer is on fire and everything is on fire because it's hell.

Last Edited: Tue. Feb 25, 2020 - 08:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atmel are probably the only company on Earth mad enough to measure the storage in an 8 bit micro in terms of 16 bit words. So in their perverse little world E00/3584 is measuring "words" not bytes. It's really 1C00/7168 bytes. This confuses absolutely everyone who encounters it the first time.