这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 9200,eeprom,bootload 我来贴我写的9200的eeprom的b

共2条 1/1 1 跳转至

9200,eeprom,bootload 我来贴我写的9200的eeprom的bootload代码

院士
2006-09-17 18:14:16     打赏
9200,eeprom,bootload 我来贴我写的9200的eeprom的bootload代码



关键词: eeprom     bootload     我来     贴我     写的         

院士
2006-12-22 22:43:00     打赏
2楼
问 这是main.c
我写的完整的bootload主要完成两件事:1.配置寄存器,2.烧写flash
虽然简单了些,但是可以给大家参考一下,之前的1个汇编启动文件和1个c初始化文件同atmel差不多,基本改了下sdram配置而已(我使用一片hy57v561620)
下面我改u-boot去喽

#include "at91rm9200.h"
#include "lib_at91rm9200.h"

#define XMODEM_SOH    0x01
#define XMODEM_EOT    0x04
#define XMODEM_ACK    0x06
#define XMODEM_NAK    0x15
#define XMODEM_CAN    0x18
#define XMODEM_EOF    0x1A

#define AT91C_BASE_NCS0        0x10000000        //NCS0全部16位访问
#define AT91C_BASE_SDRAM    0x20000000

extern void AT91F_DBGU_Printk(char *buffer);
void DelayMS(unsigned short time)            //ms等级定时,time=1..1000
{
    unsigned short piv;
    piv=32768*time/1000;
    AT91C_BASE_ST->ST_PIMR=piv;
    while (!(AT91C_BASE_ST->ST_SR&AT91C_ST_PITS));
}

unsigned short AT91F_CRC16(char *ptr, int count)
{
    int crc = 0;
    char i;
    while (--count >= 0)
    {
        crc = crc ^ (int) *ptr++ << 8;
        i = 8;
        do
        {
        if (crc & 0x8000)
            crc = crc << 1 ^ 0x1021;
        else
            crc = crc << 1;
        } while(--i);
    }
    return (crc);
}

int AT91F_Flash_ReadID()
{
    unsigned short DeviceID;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0xAA;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x2AAA*2)=0x55;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0x90;
    DeviceID=*(volatile unsigned short *)(AT91C_BASE_NCS0+0x1*2);
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x0000*2)=0xF0;
    return (DeviceID);
}

void AT91F_Flash_ChipErase()
{
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0xAA;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x2AAA*2)=0x55;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0x80;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0xAA;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x2AAA*2)=0x55;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0x10;
    DelayMS(100);    //wait >50 ms
}

void At91F_Flash_Write(unsigned int addr,unsigned short data)
{
    volatile short temp;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0xAA;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x2AAA*2)=0x55;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+0x5555*2)=0xA0;
    *(volatile unsigned short *)(AT91C_BASE_NCS0+addr*2)=data;
    while (1)
    {
        temp=*(volatile unsigned short *)(AT91C_BASE_NCS0+addr*2);
        temp^=*(volatile unsigned short *)(AT91C_BASE_NCS0+addr*2);
        if (!(temp&0x40)) break;
    }
}

unsigned char AT91F_DBGU_GetChar()                //带阻塞
{
    while (!AT91F_US_RxReady((AT91PS_USART)AT91C_BASE_DBGU));
    return (AT91F_US_GetChar((AT91PS_USART)AT91C_BASE_DBGU));
}

void AT91F_DBGU_PutChar(unsigned char c)        //带阻塞
{
    while (!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_DBGU));
    AT91F_US_PutChar((AT91PS_USART)AT91C_BASE_DBGU,c);
}
volatile int a,b;
int Main(void)
{
    unsigned char     bIsFlashReady=0,
                    bIsXmodemReady=0,
                    bIsFileReady=0,
                    bIsVerifyOK=1;
    volatile unsigned char c;
    unsigned short    crc16;
    unsigned int i,block=0;
    
    AT91F_DBGU_Printk("\n\r\n\r");
    AT91F_DBGU_Printk("******************************\n\r");
    AT91F_DBGU_Printk("*     ZT9200 EEPROM BIOS     *\n\r");
    AT91F_DBGU_Printk("*          VER 1.0           *\n\r");
    if (AT91F_Flash_ReadID()==0x234b)
    {    AT91F_DBGU_Printk("*      NCS0=SST39VF1601      *\n\r");
        AT91F_DBGU_Printk("*     Download by Xmodem     *\n\r");
        bIsFlashReady=1;
    }    
    else
    {
        AT91F_DBGU_Printk("*        NCS0=Unknown        *\n\r");
        bIsFlashReady=0;
    }
    AT91F_DBGU_Printk("******************************\n\r");
    if (!bIsFlashReady) while(1);
    
    // 下面开始执行读文件入SDRAM,协议为Xmodem
    while (!bIsXmodemReady)
    {
        AT91F_DBGU_PutChar('C');
        AT91C_BASE_ST->ST_PIMR=0;        //等待时间为两秒
        while (!(AT91C_BASE_ST->ST_SR&AT91C_ST_PITS))
            if (AT91F_US_RxReady((AT91PS_USART)AT91C_BASE_DBGU))
            {
                bIsXmodemReady=1;
                break;
            }
    }
    while(!bIsFileReady)
    {
        c=AT91F_DBGU_GetChar();
        if (c==XMODEM_SOH)
        {
            c=AT91F_DBGU_GetChar();
            c=~c;
            if (c!=AT91F_DBGU_GetChar()) while(1);
            for (i=0;i<128;i++)
            {
                c=AT91F_DBGU_GetChar();
                *(volatile char *)(AT91C_BASE_SDRAM+i+128*block)=c;            
            }
            crc16=AT91F_DBGU_GetChar();
            crc16=(crc16<<8)+AT91F_DBGU_GetChar();
            if (crc16==AT91F_CRC16((char *)(AT91C_BASE_SDRAM+block*128),128))
            {
                block++;
                AT91F_DBGU_PutChar(XMODEM_ACK);
            }
            else
            {
                AT91F_DBGU_PutChar(XMODEM_NAK);
            }
        }
        else if (c==XMODEM_EOT)
        {
            AT91F_DBGU_PutChar(XMODEM_ACK);
            bIsFileReady=1;
        }
    }
    
    // 下面开始执行烧写flash
    if (block>16384)
    {
        AT91F_DBGU_Printk("\n\rFile is too big for SST39VF1601\n\r");
        while(1);
    }
    AT91F_DBGU_Printk("\n\rStart erasing and writing flash");
    AT91F_Flash_ChipErase();
    for (i=0;i<block*128;i+=2)
    {
        At91F_Flash_Write(i>>1,*(volatile unsigned short *)(i+AT91C_BASE_SDRAM));
        if (i%128==0) AT91F_DBGU_PutChar('.');
    }
    AT91F_DBGU_Printk("OK!\n\r");
    
    // 下面开始校验数据
    AT91F_DBGU_Printk("Start verifying data");
    for (i=0;i<block*128;i+=2)
    {
        a=*(volatile unsigned short *)(i+AT91C_BASE_SDRAM);
        b=*(volatile unsigned short *)(i+AT91C_BASE_NCS0);
        if (a!=b)
        {
            bIsVerifyOK=0;
            break;
        }
        if (i%128==0) AT91F_DBGU_PutChar('.');
    }
    if (bIsVerifyOK)
        AT91F_DBGU_Printk("OK!\n\r");
    else
        AT91F_DBGU_Printk("Failed!\n\r");
    
    AT91C_BASE_PIOA->PIO_SODR=AT91C_PIO_PA19;
    DelayMS(100);
    AT91C_BASE_PIOA->PIO_CODR=AT91C_PIO_PA19;
    while(1)
    {
        AT91C_BASE_PIOB->PIO_SODR=AT91C_PIO_PB0;
        DelayMS(500);
        AT91C_BASE_PIOB->PIO_CODR=AT91C_PIO_PB0;
        DelayMS(500);
    }
} 1: 支持一下! 2: 厉害!

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]