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

Writing to ROM

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



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

Writing to ROM
PostPosted: Tue Aug 14, 2018 3:10 pm     Reply with quote

Perhaps the purpose of this thread is to get nomenclature and pointers to other threads that cover this topic.
What I would like to learn about is writing user data to ROM on a 16F PIC.

Specifically I want to write data via a serial link for calibration factors, have the PIC read them, then transfer them to ROM for nonvolatile use later. This would not happen very often, perhaps only once in it's life. Reading the data would happen at startup and be used for calibration of data.

Pointers to relevant threads would be appreciated.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Aug 14, 2018 6:25 pm     Reply with quote

re:
Quote:
writing user data to ROM on a 16F PIC.

Normally ROM (at least to me) refers to program memory and while newer PICs can read/write to 'ROM', older ones can't so it's important to specify which PIC you're using.

Some PICs (not all) have EEPROM that can be used for that purpose as well.

If you're using an RTC chip like the DS1307 et al, you can use their battery backed RAM to store 'calibration factors'.

CCS does have functions for these operations, again, you'll have to check if your PIC can use them.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Aug 15, 2018 1:00 am     Reply with quote

Nomenclature....

ROM can't be written. It is read only. Very Happy

However most modern PIC's don't actually have 'ROM'. They have 'flash program memory'. Now this is different from normal ROM, in that it can be written. However in use it is effectively a type of 'ROM', in that it can't easily be written, requiring an erase and program cycle. It also differs importantly from normal 'EEPROM' (electrically erasable programmable read only memory), which can normally be erased on a 'per cell' basis. Flash always has an 'erase page', which is the smallest element that can be erased. So sequence has to be:
Copy whole page to RAM.
Change the elements you want in this in RAM.
Erase.
Write the whole page back to 'ROM'.

Now the chips are generally built, so a write to the first cell in the page triggers an automatic erase.

Flash is actually a derivative 'type' of EEPROM, based on only having one of the large transistors needed to handle erasing the internal capacitors for every block of cells, and thereby saving massively on the transistor count needed to implement this memory. The type used in the PIC, is 'NOR' flash, rather than the NAND flash typically used in memory cards. The reason for the difference is NOR flash supports byte by byte reading, while NAND only supports block reading. Obviously in flash cards it is relatively simple to have a RAM buffer the size of one block, and then perform all operations on a block basis. Using the NOR flash allows reads to not need this buffering, but still leaves you needing the buffer when you want to change bytes inside a block. So you have to do this yourself.

The earliest PIC's dd not have the charge pump to generate the voltage needed to write a cell themselves. On these the memory could only be changed using a programmer. So if you look at something like a PIC16F84, there is no mention in the data sheet of 'flash program write'.

So you need to start by reading the data sheet for your chip, checking it does support write, and determining the erase block size.

Then work out how many blocks your configuration data will need. Hopefully possibly only one. However remember this memory is not 'byte wide'. It is typically 14bits wide on a PIC16, so if you read two bytes, you find the second has the top two bits missing. So you are going to have to organise your data to fit into this 14bit format. So you may well need an entire 14bit 'word' for every byte you want to store (though there are tricks, so for instance, you can store two 7bit ASCII characters in a single program word).

Then the sequence to change one byte or a whole page, is that outlined above. Read, change in RAM, write. So long as you ensure that your buffer is page size, and that your write is always to the start of the page, nothing else is needed.
The functions are 'read_program_memory', and 'write_program_memory'.
The key 'value', is 'FLASH_ERASE_SIZE', which the compiler knows for each chip.
Ideally checksum your data. You can then check it is legitimate.

There is a separate 'erase_program_eeprom' function, which can be used, but in general is pointless, unless you are using a chip with very large erase pages (some PIC24's have 1KB pages for example), and implementing a 'part by part' strategy, where you write several lots of data to the single page, possibly adding a flag to say which is valid. Remember cells erase to '1' and can always be written to '0' without erasing. So on a PIC24 for example, you could use the entire 24bit instruction word, and make the top byte an 'address', reserving 255 for "unused", and '0' for "don't read". Then you can write a block of data with address '1', then the next with address '2', and then write over the first block with a zero, so when you read you look and say 'oh, this one is set to ignore', and go looking further into the block for the new copy. Only erasing the whole page, when you get to the end of the block (and at that time reading all values with a 'good' address, erasing and writing these back). This is the approach used by the EEPROM emulator code available.
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Wed Aug 15, 2018 10:00 am     Reply with quote

Sorry, meant EEPROM. My term selection clouded the issue.

Wanting to use a 16F876 or open for suggestions for other chips that lend themselves better for this task. Needing USART and a few analog channels as well, the 16F876 fits the bill if it can do the EEPROM writes.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Aug 15, 2018 12:15 pm     Reply with quote

The 876, is 'not recommended for new designs'. A very old chip.
A chip like the PIC16F18875, is faster, draws less power, has an internal oscillator that might well be adequate for your needs (depends on the accuracy you need). Several of the peripherals can also be moved to different pins (PPS), increasing the flexibility. It has 6 times as much RAM. The 16F18876, has twice as much ROM. These are also much cheaper. The 876, is listed at $4.53. The PIC16F18875 is $0.97. The 16F18876 is $1.05....
Generally newer PIC's that are actually in 'mass production', are always much cheaper than older designs.
These also have actual EEPROM. Allows you to just write bytes to an address. Much simpler than trying to use flash.
If you are using the EEPROM, look at the driver file 'internal_eeprom.c'
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