过去几周我一直在努力找出为什么这不起作用。我已经尝试阅读我在 PIC32 MCU( PIC32MX795F512L )和我正在使用的 XC32 编译器上找到的所有文档(v1.34)但到目前为止还没有成功。
我需要一个特殊的常量值写入物理启动闪存地址0x1FC02FEC
(虚拟地址:0x9FC02FEC
)。此常量为0x3BDFED92
。
我已经设法在我的主程序上成功执行了此操作(如果我使用Real ICE直接编程我的pic32
,请使用以下命令行(我将xc32-ld
放在“其他选项”下) “在项目属性下”:
--fill=0x3bdfed92@0x9FC02FEC
然后我可以检查(在我的主程序中)这个地址是否确实存储了正确的值,这也有效。我使用以下代码:
if(*(int *)(0x9fc02fec) == 0x3bdfed92)
我的问题如下。我不希望我的主程序hex文件将常量写入该位置。我想要我的bootloader hex文件来执行此操作,我的主程序必须能够读取该位置并查看是否存在该常量。如果我在我的bootloader程序的xc32-ld
中使用--fill命令,它就像主程序一样成功写入常量(我已经通过在debug中使用相同的--fill命令运行我的bootloader程序来测试它模式并检查常量的0x1FC02FEC
地址。问题是,当我的引导程序通过MicroSD读入新的主程序,然后跳转到新的主程序时,一切都不起作用。似乎,在它跳转到新的主程序之前,发生了一些不好的事情,一切都崩溃了。当程序从引导加载程序跳转到主程序时,几乎就像将值写入1FC02FEC
位置一样。
这有什么理由吗?我希望我的解释没问题,如果没有,那么请告诉我,我会尝试以更容易理解的方式改写它。
我使用Microchip提供的示例代码来使用MicroSD卡进行引导加载程序。以下是代码:
int main(void)
{
volatile UINT i;
volatile BYTE led = 0;
// Setup configuration
(void)SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
InitLED();
TRISBbits.TRISB14 = 0;
LATBbits.LATB14 = 0;
ClrWdt();
// Create a startup delay to resolve trigger switch bouncing issues
unsigned char x;
WORD ms = 500;
DWORD dwCount = 25;
while(ms--)
{
ClrWdt();
x=4;
while(x--)
{
volatile DWORD _dcnt;
_dcnt = dwCount*((DWORD)(0.00001/(1.0/GetInstructionClock())/10));
while(_dcnt--)
{
#if defined(__C32__)
Nop();
Nop();
Nop();
#endif
}
}
}
if(!CheckTrigger() && ValidAppPresent())
{
// This means the switch is not pressed. Jump
// directly to the application
JumpToApp();
}
else if(CheckTrigger() && ValidAppPresent()){
if(MDD_MediaDetect()){
if(FSInit()){
myFile = FSfopen("image.hex","r");
if(myFile == NULL){
JumpToApp();
}
}
else{
JumpToApp();
}
}
else{
JumpToApp();
}
}
//Initialize the media
while (!MDD_MediaDetect())
{
// Waiting for media to be inserted.
BlinkLED();
}
// Initialize the File System
if(!FSInit())
{
//Indicate error and stay in while loop.
Error();
while(1);
}
myFile = FSfopen("image.hex","r");
if(myFile == NULL)// Make sure the file is present.
{
//Indicate error and stay in while loop.
Error();
while(1);
}
// Erase Flash (Block Erase the program Flash)
EraseFlash();
// Initialize the state-machine to read the records.
record.status = REC_NOT_FOUND;
while(1)
{
ClrWdt();
// For a faster read, read 512 bytes at a time and buffer it.
readBytes = FSfread((void *)&asciiBuffer[pointer],1,512,myFile);
if(readBytes == 0)
{
// Nothing to read. Come out of this loop
// break;
FSfclose(myFile);
// Something fishy. The hex file has ended abruptly, looks like there was no "end of hex record".
//Indicate error and stay in while loop.
Error();
while(1);
}
for(i = 0; i < (readBytes + pointer); i ++)
{
// This state machine seperates-out the valid hex records from the read 512 bytes.
switch(record.status)
{
case REC_FLASHED:
case REC_NOT_FOUND:
if(asciiBuffer[i] == ':')
{
// We have a record found in the 512 bytes of data in the buffer.
record.start = &asciiBuffer[i];
record.len = 0;
record.status = REC_FOUND_BUT_NOT_FLASHED;
}
break;
case REC_FOUND_BUT_NOT_FLASHED:
if((asciiBuffer[i] == 0x0A) || (asciiBuffer[i] == 0xFF))
{
// We have got a complete record. (0x0A is new line feed and 0xFF is End of file)
// Start the hex conversion from element
// 1. This will discard the ':' which is
// the start of the hex record.
ConvertAsciiToHex(&record.start[1],hexRec);
WriteHexRecord2Flash(hexRec);
record.status = REC_FLASHED;
}
break;
}
// Move to next byte in the buffer.
record.len ++;
}
if(record.status == REC_FOUND_BUT_NOT_FLASHED)
{
// We still have a half read record in the buffer. The next half part of the record is read
// when we read 512 bytes of data from the next file read.
memcpy(asciiBuffer, record.start, record.len);
pointer = record.len;
record.status = REC_NOT_FOUND;
}
else
{
pointer = 0;
}
// Blink LED at Faster rate to indicate programming is in progress.
led += 3;
mLED = ((led & 0x80) == 0);
}//while(1)
return 0;
}
答案 0 :(得分:1)
如果我记得很清楚(很久以前我使用过PIC32),您可以添加到链接器脚本中:
:set t_kb=^H
然后
MEMORY
{
//... other stuff
signature (RX) : ORIGIN = 0x9FC02FEC, length 0x4
}
谷歌搜索我也发现,你可以在你的源代码中做到这一点,我希望......
SECTIONS
{
.signature_section:
{
BYTE(0x3b);
BYTE(0xdf);
BYTE(0xed);
BYTE(0x92);
}>signature
}
答案 1 :(得分:0)
在我的程序中,我使用一个属性将某个值放在内存空间的某个位置。我的引导程序和应用程序可以读取此位置。这可能是一种更简单的方法。这是使用xc16和一个较小的部分,但我也在PIC32上完成了它。
#define CHECK_SUM 0x45FB
#define CH_HIGH ((CHECK_SUM & 0xFF00) >> 8)
#define CH_LOW ((CHECK_SUM & 0x00FF) >> 0)
const char __attribute__((space(prog), address(APP_CS_LOC))) CHKSUM[2] = {CH_LOW,CH_HIGH};
只需注意,当您阅读时,它将采用正确的格式:HIGH-&gt; LOW