Hi guys,
So I am trying to write a bootloader function so I can write to flash (Program Memory) as I recieve data via UART(though I haven't even gotten to that yet).
At the moment all I am trying and failing to do is write some arbitrary data to a fixed address and then read it back.
I am using Atmel Studio 6.2, default install. Also I am using the default Makefile it gives me.
I have been searching around and reading this I can't seem to understand exactly what I have done wrong :(
Please help :)
#define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #include <avr/boot.h> #include <avr/pgmspace.h> #include <avr/eeprom.h> //bit manipulation definitions #define SB(x,n) (x|=(1<<n)) #define CB(x,n) (x&=~(1<<n)) #define RB(x,n) (x&(1<<n)) #define TB(x,n) (x^=(1<<n)) #define PB(x,n) do{SB(x,n);CB(x,n);}while(0) //prototypes void boot_program_page (uint32_t page, uint8_t *buf) BOOTLOADER_SECTION; volatile uint8_t revieveBuffer[128]; int main(void) { CLKPR = (1 << CLKPCE); // Enable change of CLKPS bits CLKPR = 0x00; //no prescaler //Port C[3,2,1,0] as out put DDRC|=0x0F; for(int i=0; i < 128; i++){ revieveBuffer[i] = i; } //write the data recieved to program memory boot_program_page((0x2000), revieveBuffer); //SB(PORTC, 3); uint8_t byte; for(int j=0; j < 128; j++){ byte = pgm_read_byte(0x2000 + j); if(byte == j){ SB(PORTC, 2);//toggle pin turning LED on }else{ //CB(PORTC, 2); SB(PORTC, 3);//toggle pin turning LED on } } while(1) { _delay_ms(50); } } /*image start page should always be 0x2000 giving: * 0x0000 -> 0x1FFF Application memory (16KB) * 0x2000 -> 0x3680 Image Data (11520 Bytes) * 0x3681 -> 0x37FF Free space * 0x3800 -> 0x4000 BootLoader (4KB) */ void boot_program_page(uint32_t page, uint8_t *buf){ uint16_t i; uint8_t sreg; //disable interupts sreg = SREG; cli(); eeprom_busy_wait ();//ensure eeprom is not busy as SPM cannot operate while an eeprom operation is taking place. boot_page_erase (page); boot_spm_busy_wait (); // Wait until the memory is erased. for (i=0; i<SPM_PAGESIZE; i+=2) { // Set up little-endian word. uint16_t w = *buf++; w += (*buf++) << 8; boot_page_fill (page + i, w); } boot_page_write (page); // Store buffer in flash page. boot_spm_busy_wait(); // Wait until the memory is written. // Reenable RWW-section again. We need this if we want to jump back // to the application after bootloading. boot_rww_enable (); // Re-enable interrupts (if they were ever enabled). SREG = sreg; }