CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Getenv("PROGRAM_MEMORY") value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

Getenv("PROGRAM_MEMORY") value
PostPosted: Sat Dec 07, 2019 2:05 pm     Reply with quote

And the quest for knowledge continues.

The manual says that PROGRAM_MEMORY returns "size of memory for code (in words)".

On my PIC24, it returns 175096. If I multiply that by two to get the size, I get 350,192... but this is supposedly a 256K part.

Someone elsewhere mentioned needing to subtract something to get the actual flash size, but I can’t seem to figure out search terms to get me back to where I saw it.

?
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 07, 2019 2:14 pm     Reply with quote

If I take this value, divide it by 2 (words to bytes), then multiply that by 3 (Three bytes per instruction?) I get 262,664... 256K.

I expect this means something.

I am trying to find the last two bytes of flash memory to store a CRC there.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Sat Dec 07, 2019 2:21 pm     Reply with quote

Yes, some of these things are designed to confuse. It is the total memory
size of the chip in 16bit words (not instruction words). Since there are
two 16bit words per instruction, the instruction size is 1/2 this. The number
of actual bytes is then 3 per instruction.
Remember that physical write addresses are based on the 16bit word
address, which is why it is in this format.
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 07, 2019 2:38 pm     Reply with quote

So if I use the value returned by PROGRAM_MEMORY, should that word address be the end of my program memory I’d use for read/write_program_memory()? I seem to be able to write past it, clear the buffer, read it back and get what I expect.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Sat Dec 07, 2019 4:42 pm     Reply with quote

I'm pretty sure the value returned by PROGRAM_MEMORY is actually related to the location of the fuses stored on the last page. If you look up what address they start at, it should either be that number or a proportional to that number. It should be returning the amount of program memory available to you which is everything up to the FUSES

And yes, you can write past this, but you really really don't want to right over the FUSES section

For example in the PIC24FJ256GA106, the FUSES start at 0x2ABFA according to section 25.1 of the data sheet.

0x2ABFA => 175098 in decimal

Which is 2 off, but it might be a device table error in CCS (EDIT: yep, the device table says the config words start at 0x2BF8 instead of 0x2ABFA like the datasheet)

If you look at the PIC24fj64GA004, the FUSES are at 0xABFC, which is exactly the value returned by PROGRAM_MEMORY for that chip.

Side note: Since you are looking at write_program_memory() in a few other threads, I'll say be careful NOT to write data to the last page of FLASH as the FUSES reside at the end of it, so if you happen to call write_program_memory() on the start of that page, it will erase it and along with it the fuses, meaning the next time you boot, you can have "interesting" results
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 07, 2019 6:13 pm     Reply with quote

jeremiah wrote:
Side note: Since you are looking at write_program_memory() in a few other threads, I'll say be careful NOT to write data to the last page of FLASH as the FUSES reside at the end of it, so if you happen to call write_program_memory() on the start of that page, it will erase it and along with it the fuses, meaning the next time you boot, you can have "interesting" results


Ah, I was thinking these config bytes were somewhere else and not part of the flash. How do I determine where the end of flash is, before the fuses? Fixed size for them, and just subtract?

From what you say, this means the read and write routines can write to the fuse config. Why do they seem to have special functions for reading config memory? Or is that something else?
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Sat Dec 07, 2019 6:34 pm     Reply with quote

allenhuffman wrote:
jeremiah wrote:
Side note: Since you are looking at write_program_memory() in a few other threads, I'll say be careful NOT to write data to the last page of FLASH as the FUSES reside at the end of it, so if you happen to call write_program_memory() on the start of that page, it will erase it and along with it the fuses, meaning the next time you boot, you can have "interesting" results


Ah, I was thinking these config bytes were somewhere else and not part of the flash. How do I determine where the end of flash is, before the fuses? Fixed size for them, and just subtract?

From what you say, this means the read and write routines can write to the fuse config. Why do they seem to have special functions for reading config memory? Or is that something else?


FUSES reside in 2 locations on a PIC24:
1. Top of Program Memory
2. Configuration Memory

When the PIC boots up at power on, it copies the values in #1 to #2. That's why if you write over them in #1 it doesn't affect anything until your next boot.

When you do getenv("PROGRAM_MEMORY") it returns the "start" of the configuration words, so your usable FLASH is 0x000000 to that value minus 1

So if it returns 0x2ABF8, you can use 0x00000-0x2BF7, but keep in mind that if your page size is 1024 words (number pulled from my butt, check your data sheet), then you need to do the following math:

getenv("PROGRAM_MEMORY")/page_size

175096/1024= 170.99 pages

Then page 170 (indexed 0 to 170) should be left alone because the FUSES from #1 are there.

SIDE NOTE: if your page math doesn't end in a decimal around .98/.99 you are probably dividing a byte size instead of a word size. The fuses only take up 5-10 words at the end of a page.


Last edited by jeremiah on Sat Dec 07, 2019 6:40 pm; edited 1 time in total
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 07, 2019 6:38 pm     Reply with quote

Ah, that helps a lot! Thank you.

Page size, meaning the erase size of the flash?
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Sat Dec 07, 2019 6:40 pm     Reply with quote

allenhuffman wrote:
Ah, that helps a lot! Thank you.

Page size, meaning the erase size of the flash?


Yes. I also added a note to read in the previous post.

most PIC24s erase in "pages" and write in "rows", with there being some N number of rows per page
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group