?? main.c
字號:
/*********************************************************/
/* BootLoader for ATMega128 & MP3 Ver:3.0 @ 16Mhz */
/* Version 1.1 April 2003 */
/* 1.0 : First Release */
/* 1.1 : Add auto baud rate, code smaller */
/* 1.2 : Correct a bug with file larger than 65280 bytes.*/
/* */
/* Sylvain.Bissonnette@MicroSyl.com */
/*********************************************************/
/*********************************************************/
/* I N C L U D E */
/*********************************************************/
#include "main.h"
#include "assembly.h"
#include "macros.h"
#include "iom128v.h"
/*********************************************************/
/* D E F I N E */
/*********************************************************/
//#define DeviceID 'A' // Mega8
//#define DeviceID 'B' // Mega16
//#define DeviceID 'C' // Mega64
#define DeviceID 'D' // Mega128
//#define FlashSize 'l' // Flash 8k
//#define FlashSize 'm' // Flash 16k
//#define FlashSize 'n' // Flash 32k
//#define FlashSize 'o' // Flash 64k
#define FlashSize 'p' // Flash 128k
//#define BootSize 'a' // 128 word
//#define BootSize 'b' // 256 word
#define BootSize 'c' // 512 word
//#define BootSize 'd' // 1024 word
//#define BootSize 'e' // 2048 word
//#define BootSize 'f' // 4096 word
//#define PageSize 'Q' // 32 Bytes
//#define PageSize 'R' // 64 Bytes
//#define PageSize 'S' // 128 Bytes
#define PageSize 'T' // 256 Bytes
//#define PageSize 'U' // 512 Bytes
//#define PageByte 32 // 32 Bytes
//#define PageByte 64 // 64 Bytes
//#define PageByte 128 // 128 Bytes
#define PageByte 256 // 256 Bytes
//#define PageByte 512 // 512 Bytes
#define FALSE 0
#define TRUE 1
#define EXECCODE 0xffff
/*********************************************************/
/* G L O B A L V A R I A B L E S */
/*********************************************************/
unsigned char PageBuffer[PageByte];
unsigned int PageAddress;
unsigned char PageAddress16 = 0;
unsigned int RealPageAddress;
/*********************************************************/
void BootLoad(void)
{
SendDeviceID();
TxChar('!');
while(1)
{
GetPageNumber();
if (RealPageAddress == EXECCODE) ExecCode();
if (GetPage())
{
WriteFlash();
if (CheckFlash()) TxChar('!');
else TxChar('@');
}
else TxChar('@');
}
}
/*********************************************************/
void SendDeviceID(void)
{
TxChar(DeviceID);
TxChar(FlashSize);
TxChar(BootSize);
TxChar(PageSize);
RxChar();
}
/*********************************************************/
void GetPageNumber(void)
{
unsigned char PageAddressHigh;
unsigned char PageAddressLow;
while(!IsChar());
PageAddressHigh = RxChar();
while(!IsChar());
PageAddressLow = RxChar();
RealPageAddress = (int)((PageAddressHigh << 8) + PageAddressLow);
PageAddress = ((unsigned int)PageAddressLow << 8);
if (PageAddressHigh) PageAddress16 = 1;
else PageAddress16 = 0;
}
/*********************************************************/
void ExecCode(void)
{
RAMPZ = 0;
MCUCR = 0x01; // Enable interrupt vector select
MCUCR = 0x00; // Move interrupt vector to flash
asm("jmp 0x0000"); // Run application code
}
/*********************************************************/
char GetPage(void)
{
unsigned int i;
unsigned char LocalCheckSum = 0;
unsigned char CheckSum = 0;
for (i=0;i<PageByte;i++)
{
while(!IsChar());
PageBuffer[i]=RxChar();
LocalCheckSum += PageBuffer[i];
}
while(!IsChar());
CheckSum = RxChar();
if (LocalCheckSum == CheckSum) return 1;
else return 0;
}
/*********************************************************/
void WriteFlash(void)
{
unsigned int i;
unsigned int TempInt;
for (i=0;i<PageByte;i+=2)
{
TempInt=PageBuffer[i]+(PageBuffer[i+1]<<8);
fill_temp_buffer(TempInt,i); //call asm routine.
}
if (PageAddress16) RAMPZ = 1;
write_page(PageAddress,0x03); //Perform page ERASE
write_page(PageAddress,0x05); //Perform page write
enableRWW();
RAMPZ = 0;
}
/*********************************************************/
char CheckFlash(void)
{
unsigned int i;
unsigned int TempInt;
unsigned int TempInt2;
for (i=0;i<PageByte;i+=2)
{
if (PageAddress16) RAMPZ = 1;
TempInt = read_program_memory(PageAddress + i,0x00);
RAMPZ = 0;
TempInt2 = PageBuffer[i] +(PageBuffer[i+1]<<8);
if (TempInt != TempInt2) return 0;
}
return 1;
}
/*********************************************************/
unsigned char IsChar(void)
{
if (UCSR0A & 0x80) return 1;
return 0;
}
/*********************************************************/
unsigned char RxChar(void)
{
return UDR0; // Read char
}
/*********************************************************/
void TxChar(unsigned char ch)
{
while (!(UCSR0A & 0x20)); // Wait for empty transmit buffer
UDR0 = ch; // Write char
}
/*********************************************************/
void Wait()
{
int i;
for (i=-32000;i<32000;i++); // Minimum for 16Mhz
}
/*********************************************************/
void main(void)
{
DDRF = 0xff; // for ata
PORTF = 0xff;
DDRG = 0x02;
PORTG = 0x02;
PORTE = 0x01; // Pull up on RX line
UCSR0C = 0x86; // Asyn,NoParity,1StopBit,8Bit,
UBRR0L = 16; // 8->115200 at 16Mhz // 16->57600 at 16Mhz // 25->38400bps at 16Mhz
UCSR0B = 0x18; // Rx enable Tx Enable
RxChar();
TxChar('>');
Wait();
if (RxChar() == '<')
{
BootLoad();
ExecCode();
}
ExecCode();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -