📄 lcodu_cmdrun.c
字号:
//*----------------------------------------------------------------------------
//* File Name : lcodu_cmdrun.c
//* Object : handle the idu to odu communication
//* Creation : wp 22/10/2007
//* Modif :
//*----------------------------------------------------------------------------
//#include "usart.h"
#include "vars.h"
//#define __inline inline
#include "board.h"
//#include "spi.h"
//#include "hardctrl.h"
#define FLASH_PAGE_SIZE_BYTE 256
#define FLASH_PAGE_SIZE_LONG 32
#define FLASH_LOCK_BITS_SECTOR 16
#define FLASH_SECTOR_PAGE 32
/* 16 lock bits, each protecting 16 sectors of 32 pages*/
#define FLASH_LOCK_BITS 16
#define FLASH_BASE_ADDRESS 0x00100000
#define FLASH_END_ADDRESS 0x0011ffff
#define MAX_ONE_FRAME_LENTH 93
extern void AT91F_disable_interrupt(void);
extern void AT91F_enable_interrupt(void);
__ramfunc void reset_pro(unsigned char c);
//extern void sw_reset(void);
//unsigned int flash_counter;
unsigned int max_flash_counter;
//*----------------------------------------------------------------------------
//* \fn AT91F_Flash_Init
//* \brief Flash init
//*----------------------------------------------------------------------------
__ramfunc void at91f_flash_init (void)
{
//* Set number of Flash Waite sate
// SAM7S64 features Single Cycle Access at Up to 30 MHz
// if MCK = 47923200, 72 Cycles for 1 econde ( field MC_FMR->FMCN)
AT91C_BASE_MC->MC_FMR = ((AT91C_MC_FMCN)&(72 <<16)) | AT91C_MC_FWS_1FWS ;
}
//*----------------------------------------------------------------------------
//* \fn AT91F_Flash_Ready
//* \brief Wait the flash ready
//*----------------------------------------------------------------------------
__ramfunc int at91f_flash_ready (void)
{
unsigned int status,i;
status = 0;
i=0;
//flash_counter=0;
//* Wait the end of command
while ((status & AT91C_MC_FRDY) != AT91C_MC_FRDY )
{
status = AT91C_BASE_MC->MC_FSR;
if(i++>500000)
{
reset_pro(1);
break;
}
// flash_counter++;
}
if(max_flash_counter<i)
max_flash_counter=i;
//status = AT91C_BASE_MC->MC_FSR;
return status;
}
/*erase all,it must not be executed*/
/*
unsigned char erase_flash_region(unsigned char c)
{
AT91PS_MC reg_MC=AT91C_BASE_MC;
reg_MC->MC_FCR=(0x05a<<24)+(FLASH_REGION1_PAGE<<17)+AT91C_MC_FCMD_START_PROG;
return 0;
}
*/
//*----------------------------------------------------------------------------
//* \fn AT91F_Flash_Write
//* \brief Write in one or two Flash page located in AT91C_IFLASH, size in 32 bits
//* \input Flash_Address: start at 0x0010 0000; size: in byte,less than 256 bytes
//*----------------------------------------------------------------------------
__ramfunc int at91f_flash_write( unsigned int Flash_Address ,int size ,unsigned char * buff)
{
//* set the Flash controller base address
AT91PS_MC ptMC = AT91C_BASE_MC;
unsigned int i, page, status;
unsigned int pb_addr,index;
unsigned int flashbuf[128];
unsigned char *bufch;
unsigned int * Flash;
//* init flash pointer
/*if the address is invalid,then the command won't be executed*/
pb_addr=Flash_Address&0xffffff00;
if((Flash_Address<FLASH_BASE_ADDRESS)||((Flash_Address+size)>FLASH_END_ADDRESS))
return ACK_CMDNOTEXE;
if(size>MAX_ONE_FRAME_LENTH) return ACK_CMDNOTEXE;
Flash = (unsigned int *) pb_addr;
/*backup the current flash data*/
for(i=0;i<64;i++) flashbuf[i]=Flash[i];
index=Flash_Address&0x00ff;
bufch=(unsigned char *)flashbuf;
for(i=0;i<size;i++)
{
bufch[index+i]=buff[i];
}
at91f_flash_init();
//* Get the Flash page number
page = ((Flash_Address - (unsigned int)AT91C_IFLASH ) /AT91C_IFLASH_PAGE_SIZE);
//* copy the new value
for(i=0;i<64;i++,Flash++) *Flash=flashbuf[i];
//* Protect
AT91F_disable_interrupt();
//* Write the write page command
ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
//* Wait the end of command
status = at91f_flash_ready();
//* Protect
AT91F_enable_interrupt();
#if(1)
/*------------------------------------------------------------------------*/
/*if datas must be stored in two different pages,then write the other page*/
/*------------------------------------------------------------------------*/
if((size+index)>FLASH_PAGE_SIZE_BYTE)
{
pb_addr+=FLASH_PAGE_SIZE_BYTE;
Flash=(unsigned int *) pb_addr;
page++;
//for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size > 0) ;i+=4, Flash++,buff++,size-=4 ){
//* copy the flash to the write buffer ensuring code generation
// *Flash=*buff;
//}
for(i=0;i<64;i++) flashbuf[i]=Flash[i];
size+=index;
size-=256;
index=256-index;
//bufch=(unsigned char *)flashbuf;
for(i=0;i<size;i++)
{
bufch[i]=buff[index+i];
}
for(i=0;i<64;i++,Flash++) *Flash=flashbuf[i];
AT91F_disable_interrupt();
//* Write the write page command
ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
//* Wait the end of command
status |= at91f_flash_ready();
//* Protect
AT91F_enable_interrupt();
}
#endif
//* Check the result
if ( (status & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;
return true;
}
__ramfunc int at91f_flash_read( unsigned int Flash_Address ,int size ,unsigned char * buff)
{
//* set the Flash controller base address
//AT91PS_MC ptMC = AT91C_BASE_MC;
unsigned int i;
unsigned char * Flash;
//* init flash pointer
Flash = (unsigned char *) Flash_Address;
// at91f_flash_init();
//* Get the Flash page number
//page = ((Flash_Address - (unsigned int)AT91C_IFLASH ) /AT91C_IFLASH_PAGE_SIZE);
//* read the flash address,and save to buff. the max lenth is 256 bytes
for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size > 0) ;i++, Flash++,buff++,size-- ){
//* copy the flash to the write buffer ensuring code generation
*buff=*Flash;
}
//* Protect
//AT91F_disable_interrupt();
//* Write the write page command
//ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
//* Wait the end of command
// status = at91f_flash_ready();
//* Protect
//AT91F_enable_interrupt();
//* Check the result
//if ( (status & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;
return true;
}
__arm
unsigned char switch_flash_region(unsigned char c)
{
//AT91PS_MC reg_MC=AT91C_BASE_MC;
/*for adding.............................*/
if(c==0)
asm("B 0");
else if(c==1)
asm("B 108000h");
else
asm("B 110000h");
return 0;
}
__ramfunc void reset_pro(unsigned char c) //c=1:reset
{ AT91PS_RSTC a_pRSTC = AT91C_BASE_RSTC;
if(c==1) a_pRSTC->RSTC_RCR=0xa5000000+AT91C_RSTC_PROCRST;
else a_pRSTC->RSTC_RCR=0xa5000000;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -