View previous topic :: View next topic |
Author |
Message |
JV625
Joined: 25 Jun 2009 Posts: 16
|
Writing to and Reading from Program Memory on 18F2450 |
Posted: Thu Jun 25, 2009 10:53 am |
|
|
Can anyone help me with this issue? It appears that either my writes to program memory are not going through, or the reads are getting invalid data.
I can not differentiate because of a strange problem with my debugger printf.
CCS Support originally said that the problem is probably not writing on the proper boundary.
Code is below, hopefully this is easy enough to read. I tried to simplify it as much as possible. (The current result is that the red LED is always on.)
Code: | #include <18F2450.h>
#device ICD=TRUE
#include <stdlib.h>
#fuses HS, NOWDT, NOPROTECT, DEBUG, NOBROWNOUT
#use delay(clock=20000000)
#use RS232(DEBUGGER, XMIT=PIN_B5, RCV=PIN_B5)
#define PROGRAM_MEMORY_SIZE getenv("PROGRAM_MEMORY") // Should be 8192x16
#define ERASE_SIZE getenv("FLASH_ERASE_SIZE") // Said to be 64 by CCS Support
#define BUFFER_SIZE ERASE_SIZE
#define BUFFER_END PROGRAM_MEMORY_SIZE-1
#define BUFFER_START (PROGRAM_MEMORY_SIZE-BUFFER_SIZE)
#org BUFFER_START, BUFFER_END {}
#define WHITE_LED PIN_B4
#define RED_LED PIN_B2
#define GREEN_LED PIN_B3
#define LED_ON output_high
#define LED_OFF output_low
/* The purpose of the program is to toggle between 2 modes - a "White" mode, and
a "green" mode, changing each time the power is turned off or on. There is also
a "red" mode which should occur only when there is an error.
The program is supposed to write to program memory to store its state.
*/
void main(){
int8 Buffer[ERASE_SIZE];
int8 i;
int8 myMode;
read_program_memory(BUFFER_START, Buffer, ERASE_SIZE);
myMode = Buffer[0];
if (myMode==1){ // Mode 1
printf("Mode 1\n");
LED_ON(WHITE_LED);
Buffer[0] = 0x00; // Write to the memory so it becomes Mode 2 next time.
write_program_memory(BUFFER_START, Buffer, ERASE_SIZE);
}
if (myMode==0){ // Mode 2
printf("Mode 2\n");
LED_ON(GREEN_LED);
Buffer[0]=0x01; // Write to the memory so it becomes Mode 1 Next Time
write_program_memory(BUFFER_START, Buffer, ERASE_SIZE);
}
if ((myMode != 0) && (myMode != 1)){ // Error, not a known mode.
printf("Unknown Mode. Read = %d\n", Buffer[0]);
LED_ON(RED_LED);
Buffer[0]=0x00; // Send us back to mode 1 for next time.
write_program_memory(BUFFER_START, Buffer, ERASE_SIZE);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 25, 2009 1:33 pm |
|
|
You're writing near the end of program memory, while using the debugger.
The debugger has a "Debug Executive" program installed at the end
of memory. CCS adjusts the environment variable for the Program
memory size downwards, to compensate for the ROM space that's used
by the Debug Executive code. But what if their number is not correct ?
Just as a test, back off the program memory size by 1K (0x400).
Substitute the following line for the one in your program:
Code: | #define PROGRAM_MEMORY_SIZE (getenv("PROGRAM_MEMORY") - 0x400) |
Doing this test will tell us if the problem was caused by over-writing
the Debug Executive code. |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
|
Posted: Thu Jun 25, 2009 2:37 pm |
|
|
It looks like I am still always seeing only the red LED light up. |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
|
Posted: Thu Jun 25, 2009 3:44 pm |
|
|
Note: It also does this if I just remove all of the debugging stuff and can only go by the LEDs. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 25, 2009 3:48 pm |
|
|
I tested it with an 18F4550, with the Program Memory size #define
statement modified as shown above. It works. I get this output
when I repeatedly run the program:
Quote: |
Unknown Mode. Read = -1
Mode 2
Mode 1
Mode 2 |
Also, the LEDs alternate between what you call the Green and White LEDs.
This was tested with vs. 4.066 on a PicDem2-Plus board. The only
change I made (besides the PIC) was to move the LEDs to pins B0, B1, B2. |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
|
Posted: Thu Jun 25, 2009 9:37 pm |
|
|
Thanks PCM, this is exactly what I expected. I'm beginning to suspect that there may be a problem with my ICD hardware, as I have also been encountering mysterious problems where printf magically stopped working.
I really can't come up with a reason why/how a faulty programmer could make the write command not work and everything else be fine, but I am running out of ideas.
In the 18F2450 datasheet, it mentions "An internal
programming timer terminates program memory writes
and erases." I can't think of how/why this would be the case with my code, but is there any such possibility? |
|
|
Ttelmah Guest
|
|
Posted: Fri Jun 26, 2009 3:11 am |
|
|
The timer, is _allways_ used for internal writes. It is not a problem. Remember that when you write to the internal program memory, code execution stops. How would the code ever run again?. The internal timer, automatically lets the programming complete, and then lets execution carry on.
There is an 'interesting' reference, in the Microchip ICD2 'limitations' reference, where it says for your device:
"If you modify Program Memory, you must reprogram the device in order to program the changes into the target."
I don't know if the same limitation applies with the PicDem debugger, but it would explain what you are seeing....
Best Wishes |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
Update |
Posted: Mon Jun 29, 2009 10:47 am |
|
|
CCS had me update my software and programmer to the newest version, but I'm still experiencing the same problem. It is really mysterious, particularly given that the code does for PCM exactly what I thought it should be doing.
One solution they did provide (if anyone else finds this thread because of a similar problem) is to make sure that the writes begin on a multiple of the flash erase size.
Is it possible that the PIC needs to be running at some voltage to be able to write to itself or something? (e.g. no LVP?) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 29, 2009 10:51 am |
|
|
Quote: |
Is it possible that the PIC needs to be running at some voltage to be able to write to itself or something? (e.g. no LVP?) |
What voltage are you using for Vdd ? I tested it at +5v. |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
|
Posted: Mon Jun 29, 2009 10:53 am |
|
|
3.3V |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 29, 2009 10:58 am |
|
|
Are you using the "LF" version of the PIC ? The data sheet says that's
required, if you want to run it at 3.3 volts. |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 29, 2009 11:04 am |
|
|
It's normal.
Look at these figures in the 18F2450 data sheet:
Quote: | FIGURE 21-1
and
FIGURE 21-2 |
These are on page 270 in the Acrobat Reader. They show clearly
that you need the "LF" version if you want to run below 4.2 volts. |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
|
Posted: Mon Jun 29, 2009 11:25 am |
|
|
Wow, thanks ... I never would've guessed that it'd work fine in every other way.
I am going to test at 5V right now and post the results. |
|
|
JV625
Joined: 25 Jun 2009 Posts: 16
|
|
Posted: Mon Jun 29, 2009 12:04 pm |
|
|
We got it going at 5V, but still no luck. |
|
|
|