?? demo.c
字號(hào):
/*
LuckyProg2004 Mega8 BootLoader 引導(dǎo)程序
文件名 : demo.c
作 者: 芯 藝
更新時(shí)間: 2004-11-16
CPU : ATMEGA8
時(shí) 鐘: 外部4MHz
編 譯: WinAVR-20040720
E-mail : changfutong@sina.com
MSN : changfutong@hotmail.com
OICQ : 27796915
歡迎訪問(wèn): http://bitfu.zj.com http://BitFu.yeah.net
注:本程序在CA-M8實(shí)驗(yàn)板上調(diào)試通過(guò),
與計(jì)算機(jī)程序LuckyProg2004 配合使用。
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/boot.h>
#include "lp2004.h"
#define uchar unsigned char
#define uint unsigned int
#define FREQ 4
//數(shù)據(jù)包長(zhǎng)度設(shè)置成M8頁(yè)長(zhǎng)度(按字節(jié))
//PAGE_SIZE == LP_PACKET_SIZE
#define PAGE_SIZE 64
#define DEVICE_ID 8
#define MAX_PAGE_COUNT 96
#define SET_RED_LED PORTB&=~_BV(PB1)
#define CLR_RED_LED PORTB|=_BV(PB1)
#define SET_YEL_LED PORTB&=~_BV(PB0)
#define CLR_YEL_LED PORTB|=_BV(PB0)
uint g_wPageIndex=0; //讀或?qū)懤讨杏糜谒饕?yè)
uchar g_aPageTemp[PAGE_SIZE]; //數(shù)據(jù)包緩沖區(qū)
uchar g_aCommand[5]; //命令緩沖區(qū)
void (*reset)(void)=0x0000;
void uart_putc(uchar c)
{
while( !(UCSRA & (1<<UDRE)) );
UDR=c;
}
uchar uart_getc(void)
{
while( !(UCSRA & (1<<RXC)) );
return UDR;
}
//寫(xiě)flash
void WriteFlash(void)
{
uchar i;
uint PageCount=g_aCommand[2];
PageCount<<=8;
PageCount+=g_aCommand[1];
for(g_wPageIndex=0;g_wPageIndex<PageCount;g_wPageIndex++)
{
uart_putc(LP_ACK);
// 接收當(dāng)前頁(yè)面數(shù)據(jù)
for(i=0;i<PAGE_SIZE;i++)
g_aPageTemp[i]=uart_getc();
//6K以上的不寫(xiě)
if(g_wPageIndex<MAX_PAGE_COUNT)
{
// 頁(yè)擦除操作
boot_page_erase(g_wPageIndex<<6);
while(boot_rww_busy())
boot_rww_enable();
// 填充緩沖頁(yè)
for(i = 0; i < PAGE_SIZE; i += 2)
boot_page_fill((unsigned long)i,*((uint *)(g_aPageTemp +i)));
// 頁(yè)寫(xiě)入操作
boot_page_write(g_wPageIndex<<6);
while(boot_rww_busy())
boot_rww_enable();
}
}
uart_putc(LP_ACK);
}
//讀flash
void ReadFlash(void)
{
uchar i;
uint PageCount=g_aCommand[2];
PageCount<<=8;
PageCount+=g_aCommand[1];
uart_putc(LP_ACK);
for( g_wPageIndex=0;g_wPageIndex<PageCount;g_wPageIndex++)
{
uart_getc();
if(g_wPageIndex<MAX_PAGE_COUNT)
{
for(i=0;i<PAGE_SIZE;i++)
uart_putc(pgm_read_byte(i+(g_wPageIndex*PAGE_SIZE)));
}
else//6K以上的頁(yè)面
{
for(i=0;i<PAGE_SIZE;i++)
uart_putc(0xff);
}
}
}
//寫(xiě)eeprom
void WriteEeprom(void)
{
uchar i;
uint PageCount=g_aCommand[2];
PageCount<<=8;
PageCount+=g_aCommand[1];
for(g_wPageIndex=0;g_wPageIndex<PageCount;g_wPageIndex++)
{
uart_putc(LP_ACK);
// 接收頁(yè)面數(shù)據(jù)
for(i=0;i<PAGE_SIZE;i++)
g_aPageTemp[i]=uart_getc();
eeprom_write_block (g_aPageTemp,PAGE_SIZE *g_wPageIndex, PAGE_SIZE);
}
uart_putc(LP_ACK);
}
//讀eeprom
void ReadEeprom(void)
{
uchar i;
uint PageCount=g_aCommand[2];
PageCount<<=8;
PageCount+=g_aCommand[1];
uart_putc(LP_ACK);
for( g_wPageIndex=0;g_wPageIndex<PageCount;g_wPageIndex++)
{
uart_getc();
for(i=0;i<PAGE_SIZE;i++)
uart_putc(eeprom_read_byte(i+(g_wPageIndex*PAGE_SIZE)));
}
}
int main(void)
{
uchar tmp;
DDRB=_BV(PB0)|_BV(PB1);
PORTB=0X03;
//uart 初始化
UBRRH=0;
UBRRL=25;//9600 baud 4MHz:25
UCSRB=_BV(RXEN)|_BV(TXEN);
while(1)
{
SET_YEL_LED;
CLR_RED_LED;
tmp=uart_getc();
if(tmp==LP_ACK)
{
uart_putc(LP_ACK);
continue;
}
else if(tmp!=DEVICE_ID)
continue;
uart_putc(LP_ACK);
SET_RED_LED;
CLR_YEL_LED;
for(tmp=0;tmp<5;tmp++)
g_aCommand[tmp]=uart_getc();
switch(g_aCommand[0])
{
case LP_WRITE_FLASH :
WriteFlash();
break;
case LP_READ_FLASH :
ReadFlash();
break;
case LP_WRITE_EEPROM:
WriteEeprom();
break;
case LP_READ_EEPROM:
ReadEeprom();
break;
case LP_ERASE_DEVICE:
uart_putc(LP_ACK);
break;
case LP_WRITE_BITS:
break;
case LP_READ_BITS:
break;
case LP_SCAN_HARDWAR:
break;
case LP_RESET_DEVICE:
uart_putc(LP_ACK);
reset();
break;
default:
break;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -