本帖最后由 sed2003 于 2012-11-14 18:19 编辑
IJK,我发一份我的代码给你看看怎么样,请你帮我解疑一下。
我实在搞不明白到底是哪里错了。
int main(void)
{
SysInit();
while (1)
{
//ClearWatchdog;
ClockRun( );
AccProc( );
if(bUpdateMCU)
{
bUpdateMCU = 0;
disableInterrupts();
//protocol_init();
unlock_PROG();
unlock_DATA();
_fctcpy('U');
MCUUpdateCodeProc();
}
}
}
然后是写FLASH的所有代码:#include "sys.h"
#pragma section (UPDATEFLASH)
static u8 DataBuffer[DatBufSize]={0}; // 55 aa len GR MI PAR1 128PAR2 CS
static u8 *ReceivedData;
static u8 data[10];
static u8 FlashBuf[128];
static u8 FAR* FlhDataAddress;
static u8 FlhDataCount;
static U16 blockcnt = 0;
//static u8 RdData[10]={0};
void UpdateUartTx(uchar dat)
{
u8 sr;
//wait for Tx empty
sr = UART1->SR;
while(!(sr & 0x80/*TxNE*/)) sr = UART1->SR;
//send data0
UART1->DR = dat;
//wait for transmission complete
sr = UART1->SR;
while(!(sr & 0x40/*TxNE*/)) sr = UART1->SR;
}
void Transmit(uchar *p,U8 len)
{
uchar i,OutStr[15],CheckSum=0;
OutStr[0]=0x55;
OutStr[1]=0xAA;
OutStr[2]=len+1;
for(i=3;i<len+3;i++)
{
OutStr = *p;
CheckSum += *p;
p++;
}
CheckSum = CheckSum ^ 0xFF;
OutStr[len+3] = CheckSum;
for(i=0;i<len+4;i++)
{
UpdateUartTx(OutStr); // 发送数据
}
}
void Receive(u8* ReceivedData)
{
u8 sr; // working copy of SPI_SR register
//wait for Rx full
sr = UART1->SR;
while(!(sr & 0x20 /*RXNE*/)) sr = UART1->SR ;
//receive data
*ReceivedData = UART1->DR;
}//Receive
void Mem_ProgramBlock(u16 BlockNum, FLASH_MemType_TypeDef MemType, u8 *Buffer)
{
u16 Count = 0;
u32 StartAddress = 0;
u16 timeout = (u16)0x6000;
/* Set Start address wich refers to mem_type */
if (MemType == FLASH_MEMTYPE_PROG)
{
StartAddress = FLASH_START;
}
else
{
StartAddress = EEPROM_START;
}
/* Point to the first block address */
StartAddress = StartAddress + ((u32)BlockNum * BLOCK_SIZE);
/* Standard programming mode */
FLASH->CR2 |= (u8)0x01;
FLASH->NCR2 &= (u8)~0x01;
/* Copy data bytes from RAM to FLASH memory */
for (Count = 0; Count < BLOCK_SIZE; Count++)
{
*((@far u8*)StartAddress + Count) = ((u8)(Buffer[Count]));
}
if (MemType == FLASH_MEMTYPE_DATA)
{
/* Waiting until High voltage flag is cleared*/
while ((FLASH->IAPSR & 0x40) != 0x00 || (timeout == 0x00))
{
timeout--;
}
}
}
u8 WriteBufferFlash(u8 FAR* DataAddress, u8 * DataPointer, u8 DataCount)
{
u32 Address = (u32) DataAddress;
// u8 * DataPointer = FlashBuf;
u32 Offset;
FLASH_MemType_TypeDef MemType;
//set offset according memory type
// if(MemType == FLASH_MEMTYPE_PROG)
// Offset = FLASH_START;
// else
// Offset = EEPROM_START;
Offset = FLASH_START;
MemType = FLASH_MEMTYPE_PROG;
#if 0
//program beginning bytes before words
while((Address % 4) && (DataCount))
{
*((PointerAttr u8*) Address) = *DataPointer;
while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
Address++;
DataPointer++;
DataCount--;
}
//program beginning words before blocks
while((Address % BLOCK_BYTES) && (DataCount >= 4))
{
FLASH->CR2 |= (u8)0x40;
FLASH->NCR2 &= (u8)~0x40;
*((PointerAttr u8*)Address) = (u8)*DataPointer ; /* Write one byte - from lowest address*/
*((PointerAttr u8*)(Address + 1)) = *(DataPointer + 1); /* Write one byte*/
*((PointerAttr u8*)(Address + 2)) = *(DataPointer + 2); /* Write one byte*/
*((PointerAttr u8*)(Address + 3)) = *(DataPointer + 3); /* Write one byte - from higher address*/
while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
Address += 4;
DataPointer+= 4;
DataCount -= 4;
}
#endif
//program blocks
while(DataCount >= BLOCK_BYTES)
{
Mem_ProgramBlock((Address - Offset)/BLOCK_BYTES, MemType, DataPointer);
// Address += BLOCK_BYTES;
// DataPointer+= BLOCK_BYTES;
DataCount -= BLOCK_BYTES;
}
#if 0
//program remaining words (after blocks)
while(DataCount >= 4)
{
FLASH->CR2 |= (u8)0x40;
FLASH->NCR2 &= (u8)~0x40;
*((PointerAttr u8*)Address) = (u8)*DataPointer ; /* Write one byte - from lowest address*/
*((PointerAttr u8*)(Address + 1)) = *(DataPointer + 1); /* Write one byte*/
*((PointerAttr u8*)(Address + 2)) = *(DataPointer + 2); /* Write one byte*/
*((PointerAttr u8*)(Address + 3)) = *(DataPointer + 3); /* Write one byte - from higher address*/
while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
Address += 4;
DataPointer+= 4;
DataCount -= 4;
}
//program remaining bytes (after words)
while(DataCount)
{
*((PointerAttr u8*) Address) = *DataPointer;
while( (FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS)) == 0);
Address++;
DataPointer++;
DataCount--;
}
#endif
return 1;
}//WriteBufferFlash
u8 WriteBuffer(u8 FAR* DataAddress, u8 * DataPointer, u8 DataCount)
{
u8 i;
//for Flash
if (((u32)DataAddress >= FLASH_START) && (((u32)DataAddress + DataCount - 1) <= FLASH_END))
return WriteBufferFlash(DataAddress, DataPointer, DataCount);
#if 0
//for EEPROM
if (((u32)DataAddress >= EEPROM_START) && (((u32)DataAddress + DataCount - 1) <= EEPROM_END))
return WriteBufferFlash(DataAddress, DataCount, FLASH_MEMTYPE_DATA);
//for RAM
if (((u32)DataAddress >= RAM_START) && (((u32)DataAddress + DataCount - 1) <= RAM_END))
{
for(i=0; i<DataCount; i++)
DataAddress = DataBuffer;
return 1;
}
//for Option bytes
if (((u32)DataAddress >= OPTION_START) && (((u32)DataAddress + DataCount - 1) <= OPTION_END))
{
for(i=0; i<DataCount; i++)
{
FLASH_ProgramOptionByte((u32)(&DataAddress), DataBuffer);
}
return 1;
}
#endif
//otherwise fail
return 0;
}//WriteBuffer
u8 FLASH_ReadByteData(uint32_t Address)
{
/* Read byte */
return(*(FAR uint8_t *) (uint16_t)Address);
}
void MCUUpdateCodeProc(void)
{
u8 sr,*RevData; // working copy of SPI_SR register
u8 cnt;
u8 i,cs,WrFlashSt;
#if 1
data[0] = _GR_Menu;
data[1] = _MI_Update_MCU;
Transmit(data,2);
//FlashDataAddr = (u32*)FLASH_START;
FlhDataAddress = (u8 FAR*)FLASH_START;
FlhDataCount = BLOCK_SIZE;
while(1)
{
/************* get uart data *************/
RevData = &DataBuffer[0];
cnt = 0;
while(cnt<135)
{
sr = UART1->SR;
while(!(sr & 0x20 /*RXNE*/))
{
sr = UART1->SR ;
}
*RevData = UART1->DR;
RevData++;
cnt++;
}
/************* analyse data *************/
cs = 0;
for(i=3;i<134;i++)
{
cs += DataBuffer;
}
cs = cs ^ 0xFF;
if(DataBuffer[0]!=0x55 || DataBuffer[1]!=0xAA || DataBuffer[2]!=132 || DataBuffer[134]!=cs)
{
//send err ack to host
data[0] = _GR_Menu;
data[1] = _Update_Code;
data[2] = _Fail;
Transmit(data,3);
}
else
{
#if 1
/************* write into flash rom *************/
for(i=0;i<128;i++)
{
FlashBuf = DataBuffer[6+i];
}
WriteBuffer(FlhDataAddress,FlashBuf,FlhDataCount);
#endif
if(blockcnt==256) while(1); //41
if(DataBuffer[5] == 0x00)
{// last frame data
/* Lock program memory */
//FLASH->IAPSR = ~0x02;
/* Lock data memory */
//FLASH->IAPSR = ~0x08;
data[0] = _GR_Menu;
data[1] = _Update_Code;
data[2] = _Success;
Transmit(data,3);
#if 1
//reset stack pointer (lower byte - because compiler decreases SP with some bytes)
_asm("LDW X, SP ");
_asm("LD A, $FF");
_asm("LD XL, A ");
_asm("LDW SP, X ");
_asm("JPF $8000");
#endif
}
else
{// next frame data
FlhDataAddress += FlhDataCount;
blockcnt++;
data[0] = _GR_Menu;
data[1] = _Update_Code;
data[2] = _Success;
Transmit(data,3);
}
}
}
#endif
}
#pragma section ()
|