完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
亲爱的Microchip专家,我必须使用内部闪存来保存很多数据。我计划把Flash分成两个区域。一个区域是用于数据的,一个是写入一次的,另一个是只读的;另一个是用于读取和写入新数据的。我的最大问题是,选择特定的内存地址。写一些例子数据。我的代码是这样的:在这个例子中,0x16000应该是非const数据的起始地址和结束地址的0x17FFE。我想将“255”写入特定闪存区域。在编译代码之后,ADRES0x16000内部的数据仍然是0xFFFFFF。我的错误是什么?我使用的是PIC24FJ256GA7 DEV板。
以上来自于百度翻译 以下为原文 Dear Microchip experts, I have to use the internal Flash to save many datas. I am planning to divide the flash in two areas. One area is for datas only for write one time and then its read only. The other is for both, reading and writing new data. My big problem is, selecting the specific memory adress and write some example data. My code is like this: In this example 0x16000 should be the start address of the non const data and 0x17FFE the end address. I want to write "255" into the specific flash area. uint8_t Erase_Memory(void) { /* Set Register for Erase Mode */ NVMCONbits.NVMOP = 0b0011; /* Enable Erase */ NVMCONbits.WREN = 1; /* Set Flash area of the non volatile data to be deleted */ NVMADRU = 0x16000 >> 16; NVMADR = 0x17FFE & 0xFFFF; /* Unlock protection */ NVMKEY = 0x55; NVMKEY = 0xAA; /* Start Erase Cycle*/ NVMCONbits.WR = 1; /* Wait until Erase is done */ while(NVMCONbits.WR); return 1; } uint8_t Flash_Write(void) { /* Set Register for Write Mode */ NVMCONbits.NVMOP = 0b0010; /* Enable Write */ NVMCONbits.WREN = 1; TBLPAG = 0x16000 >> 16; /* Set Flash area of the non volatile memory to be deleted */ NVMADRU = 0x16000 >> 16; NVMADR = 0x17FFE & 0xFFFF; __builtin_tblwtl(0, 255); __builtin_tblwth(0,255); /* Unlock protection */ NVMKEY = 0x55; NVMKEY = 0xAA; /* Start Write Cycle*/ NVMCONbits.WR = 1; /* Wait until Write is done */ while(NVMCONbits.WR); /* Flash Write ok! */ return 1; } After I compile the code, the data inside the adress 0x16000 is still 0xFFFFFF. What is my mistake? I am using PIC24FJ256GA7 dev board. |
|
相关推荐
10个回答
|
|
你怎么知道它仍然是0xfffffff?如果你使用MPLABX和调试器来检查这一点,你需要读取设备内存(从芯片上指向的绿色箭头)。无论如何,这是我只用一页内存(仅使用它的一些字节)来完成的:使用下面的HelpCal码存储器C。AN1157 Bootloader应用笔记。/ Ruben
以上来自于百度翻译 以下为原文 How do you know it still is 0xffffff? If you are using the MPLABX and the debugger to check this, you need to read back the device memory (the green arrow pointing up from the chip). Anyway, this is how I have done it for only one page of memory (using only some bytes of it): const volatile union { NVM_CONFIG flashConfig; BYTE padding[PM_PAGE_SIZE/2]; } nvFlashConfigPage __attribute__ ((space(auto_psv),aligned(PM_PAGE_SIZE/2))) = {{{PID_KP,PID_KI,PID_KD1,PID_KD2,PID_SET,1},0xa8,0x8f}}; ... ReadFlash(__builtin_tbladdress(&nvFlashConfigPage),sizeof(NVM_CONFIG),(BYTE*)&ramConfig); // Get settings from config struct in flash ... EraseFlash(__builtin_tbladdress(&nvFlashConfigPage),sizeof(NVM_CONFIG)); WriteFlash(__builtin_tbladdress(&nvFlashConfigPage),sizeof(NVM_CONFIG),(BYTE*)&ramConfig); using the following helpercode memory.c, originally taken from the AN1157 bootloader application note. /******************************************************************** * * PIC24F Flash memory read write helper functions * ********************************************************************* * FileName: memory.c * Dependencies: boot.c * Processor: PIC24F Family * Compiler: C30 v3.00 or later * Company: Microchip Technology, Inc. * * Software License Agreement: * * The software supplied herewith by Microchip Technology Incorporated * (the Company) for its PICmicro® Microcontroller is intended and * supplied to you, the Companys customer, for use solely and * exclusively on Microchip PICmicro Microcontroller products. The * software is owned by the Company and/or its supplier, and is * protected under applicable copyright laws. All rights are reserved. * Any use in violation of the foregoing restrictions may subject the * user to criminal sanctions under applicable laws, as well as to * civil liability for the breach of the terms and conditions of this * license. * * THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. * * * File Description: * * Flash program memory read and write functions for use with * PIC24F Serial Bootloader. * * Change History: * * Author Revision # Date Comment *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Brant Ivey 1.00 1-17-2008 Initial release with AN1157 * Ruben Jönsson 2.00 2014-02-19 For use with MVC/Charger IOBoard. Removed unused conditional code * for other processors and boards to make it more readable. *********************************************************************/ #include "memory.h" #include #include /************************************************** * Local functions */ /******************************************************************** ; Function: void WriteMem(WORD cmd) ; ; PreCondition: Appropriate data written to latches with WriteLatch ; ; Input: cmd - type of memory operation to perform ; ; Output: None. ; ; Side Effects: ; ; Overview: Write stored registers to flash memory ;*********************************************************************/ static void WriteMem(WORD cmd) { NVMCON = cmd; __builtin_disi(5); __builtin_write_NVM(); while(NVMCONbits.WR == 1); } /******************************************************************** ; Function: void WriteLatch(WORD page, WORD addrLo, ; WORD dataHi, WORD dataLo) ; ; PreCondition: None. ; ; Input: page - upper byte of address ; addrLo - lower word of address ; dataHi - upper byte of data ; addrLo - lower word of data ; ; Output: None. ; ; Side Effects: TBLPAG changed ; ; Overview: Stores data to write in hardware latches ;*********************************************************************/ static void WriteLatch(WORD page, WORD addrLo, WORD dataHi, WORD dataLo) { TBLPAG = page; __builtin_tblwtl(addrLo,dataLo); __builtin_tblwth(addrLo,dataHi); } /******************************************************************** ; Function: DWORD ReadLatch(WORD page, WORD addrLo) ; ; PreCondition: None. ; ; Input: page - upper byte of address ; addrLo - lower word of address ; ; Output: data - 32-bit data in W1:W0 ; ; Side Effects: TBLPAG changed ; ; Overview: Read from location in flash memory ;*********************************************************************/ static DWORD ReadLatch(WORD page, WORD addrLo) { DWORD_VAL temp; TBLPAG = page; temp.word.LW = __builtin_tblrdl(addrLo); temp.word.HW = __builtin_tblrdh(addrLo); return temp.Val; } /******************************************************************** ; Function: void Erase(WORD page, WORD addrLo, WORD cmd); ; ; PreCondition: None. ; ; Input: page - upper byte of address ; addrLo - lower word of address ; cmd - type of memory operation to perform ; ; Output: None. ; ; Side Effects: TBLPAG changed ; ; Overview: Erases page of flash memory at input address *********************************************************************/ static void Erase(WORD page, WORD addrLo, WORD cmd) { WORD temp; temp = TBLPAG; TBLPAG = page; NVMCON = cmd; __builtin_tblwtl(addrLo,addrLo); __builtin_disi(5); __builtin_write_NVM(); while(NVMCONbits.WR == 1); TBLPAG = temp; } /************************************************** * Public functions */ void ReadFlash(DWORD flashAddr,int bytes, BYTE *ramDest) { DWORD_VAL temp; int i; for(i=0;i *ramDest++=temp.v[0]; i++; if(i i++; } flashAddr+=2; // Only data in low word, skip high word (3rd + phantom bytes) } } void EraseFlash(DWORD flashStartAddr,int bytes) { DWORD aligned=flashStartAddr&(~((PM_PAGE_SIZE/2)-1)); int offs=flashStartAddr-aligned; while(offs offs+=PM_PAGE_SIZE/2; aligned+=PM_PAGE_SIZE/2; } } void WriteFlash(DWORD flashStartAddr,int bytes, BYTE *ramSrc) { int offs=0; while(offs offs+=PM_ROW_SIZE/2; flashStartAddr+=PM_ROW_SIZE; ramSrc+=PM_ROW_SIZE/2; } } /Ruben |
|
|
|
在编写代码自更新器时,我的经验是,不能使用调试器从PIC Flash中读取新写入的数据,它只会显示在启动调试会话时写入的数据。
以上来自于百度翻译 以下为原文 My experience when making a code self-updater is that you cannot use the debugger to read back your newly written data from the pic flash, it will only show the data written when starting the debug session. |
|
|
|
调试器不自动读取数据。你需要点击阅读按钮。此外,我注意到MPLABX在读取之后不更新它的窗口。你需要滚动它才能看到变化。如果你想把它作为一个普通的PSV变量读取,请确保每个位置只写16位。
以上来自于百度翻译 以下为原文 The debugger does not autmaticly read the data back. You need to hit the read button. Also I noted that MPLabX does not update its widows after the read. You need to scroll it to see the change. Make sure you only write 16 bits per location if you want to Read it as a normal psv variable. |
|
|
|
您还将有一个小问题,即“擦除”闪存已设置所有位(“闪存程序存储器”FRM部分的第3.6部分用于您的设备),因此写入255将不会显示您已写入任何东西。您可能应该使用Y-BuuthTyNoWrnEngEnEnvm宏(以及禁用中断)。s)如FRM的示例4-2所示,如MaxRuben所做的那样。这是确保解锁序列正确执行的更安全的方法。苏珊
以上来自于百度翻译 以下为原文 You will also have a small issue that "erased" FLASH memory has all bits set (Section 3.6 of the "Flash Program Memory" FRM section for your device) so writing 255 will not show that you have written anything. You probably should be using the __builtin_write_NVM macro (along with disabling interrupts) as show in example 4-2 of the FRM and as MaxRuben has done. This is a much safer way to ensure that the unlock sequence is executed correctly. Susan |
|
|
|
谢谢你的回答,感谢MasBube的代码。现在,代码看起来是这样的:所以我点击了“Run Project”,然后点击“读取设备内存”,就像你之前说的一样。然后我将编程内存,格式PSV混合并转到AdSress 0x16000,值仍然是0xFFFFFF:(应该是270,所以0x010e…
以上来自于百度翻译 以下为原文 Thank you for your answer and thanks maxruben for the code. Now for wrting, the code looks like this: uint8_t Flash_Write(uint16_t* ptr_data, uint32_t addrlo) { NVMCON = 0x4002; TBLPAG = addrlo >> 16; //addrlo = 0x16000, write 0001 to TBLPAG __builtin_tblwtl((uint16_t) addrlo,ptr_data[0]); //addrlo is now 6000 to point the adress 0x16000 // ptr_data[0] = 270 __builtin_disi(5); __builtin_write_NVM(); while(NVMCONbits.WR); /* Flash Write ok! */ return 1; } So I hit "run project" and then hit "read device memory" like as you said before. Then I am going to Program Memory, Format PSV Mixed and go to adress 0x16000 the value is still 0xFFFFFF :( It should be 270, so 0x010E... |
|
|
|
你的标题是PIC24F,但是PIC24F家族非常不同。后来你写了PIC24FJ256GA7 DEV板,所以我假设它是PIC24FJ256GA705。如果你查看你的FRM数据表DS300 10118B页78,你会看到GA705也使用“写锁存器”的概念。PDG示例中的例子是双字编程,而不是行编程,但概念应该是相同的。至少你可以试试。
以上来自于百度翻译 以下为原文 Your title says PIC24F, but the PIC24F families are very different. Later you write PIC24FJ256GA7 dev board, so I assume it's the PIC24FJ256GA705. If you look into your FRM datasheet DS30010118B-page 78, you will see that the GA705 also uses the concept of "write latches" . TBLPAG = 0xFA; // Point TBLPAG to the write latches. The example in the PDF example is for double word programming, not for row programming, but the concept should be the same. At least you can try. |
|
|
|
标题是24FJ。如果我使用TBLPAG=0xFA,我怎么可以指向地址0x16000?因为NVMADR只有16位宽。
以上来自于百度翻译 以下为原文 The title says 24FJ. If I use TBLPAG = 0xFA, how I can point to adress 0x16000? Because NVMADR is only 16bit wide. |
|
|
|
是的,我应该写PIC24FJ,但这仍然意味着PIC24FJXXXGB1做它的闪存编程,而不是PIC24FJXXXGB4或您的PIC24FJXXXGA7不同。实际上,你应该改变你的标题来反映你的PIC。我不能给你一个运行的例子,但是正如你的手册中包含了一个双字编程的C示例,它还包含一个用于行编程的汇编例子(第75页)。你要求什么,你必须阅读你的文档。你也可以在谷歌的微芯片论坛上使用TBLPAG=0xFA,进入谷歌搜索行:这将使你得到类似的线索。
以上来自于百度翻译 以下为原文 Yes, I should have written PIC24FJ, but that still means that a PIC24FJxxxGB1 does its flash programming rather different than e.g. the PIC24FJxxxGB4 or your PIC24FJxxxGA7. Actually you should change your title to reflect your PIC. I cannot give you a running example, but as said your manual contains a C example for double word programming, and it also contains an assembler example for row programming (page 75). The examples show you how to write to the latches, that's what you asked for. You have to read your documentation. You can also google in the microchip forum for tblpag=0xfa, enter into the google search line: site:www.microchip.comforums tblpag=0xfa That will lead you to similar threads. |
|
|
|
我得到了它。谢谢。现在工作了。主题可以关闭。
以上来自于百度翻译 以下为原文 I got it. Thanks. It works now. Topic can be closed. |
|
|
|
微笑:如果你想让其他人知道,一个主题被解决了,你可以编辑你的第一篇文章,并在标题前写下[解决]。
以上来自于百度翻译 以下为原文 Smile: If you would like to let the others know, that a topic is solved, you can edit your first post and write [solved] in front of your title. |
|
|
|
只有小组成员才能发言,加入小组>>
5162 浏览 9 评论
2000 浏览 8 评论
1928 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3172 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2226 浏览 5 评论
731浏览 1评论
613浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
503浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
629浏览 0评论
527浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 11:56 , Processed in 1.477181 second(s), Total 94, Slave 78 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号