Atmel Studio- ATmega128 bootloader

时间:2016-01-28 08:47:38

标签: avr bootloader atmega avr-gcc atmelstudio

我正在尝试为ATmega AVR编写一个自定义的启动加载程序。我编写了一个代码,它在小型AVR中完美运行,如ATmega32A和ATmega8A。但是当我想在ATmega128A中使用它时,它在闪存段中什么也没写。 我确定保险丝是正确的,ATmega103模式被禁用,程序在启动部分启动,但它什么也没做。 在调用函数之前" boot_program_page"我设置了PORTC并打开了一些LED,之后我清除了PORTC并且LED熄灭了。所以代码也完全执行了。 我正在使用的函数是avr / boot.h中提供的示例。 它应该在闪存的第0页写一些数据(实际上是0x00)。 这是我的代码:

#define F_CPU 8000000UL

#include <inttypes.h>
#include <avr/io.h>
#include <avr/boot.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

void boot_program_page (uint32_t page, uint8_t *buf);


int main(void)
{
    uint8_t data[SPM_PAGESIZE] = {0};
    uint32_t page = 0;

    DDRC = 255;
    PORTC = 255;

    page *= (uint32_t)SPM_PAGESIZE;
    boot_program_page(page,data);

    _delay_ms(1000);
    PORTC = 0;
    while (1)
    {
    }
}

void boot_program_page (uint32_t page, uint8_t *buf)
{
    uint16_t i;
    uint8_t sreg;

    // Disable interrupts.

    sreg = SREG;
    cli();

    eeprom_busy_wait ();

    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;
}

0 个答案:

没有答案
相关问题