This week I was working on flash erase, write and read. Previously I used the NVM driver to access the flash. The erase size for flash is fixed to a page, which varies based on the device. The write size, however, can vary between a row, a quad-word and a word, these information are available in the data sheet. [Reference]
In the NVM library, the size of write operation is set to row by default. It is possible to change the size by modifying NVMOP bits in NVMCON register. In order to achieve this, an unlocking sequence has to be performed.
NVMADDR = <Destination address of the flash>;
NVMDATA = <Data to be written to the flash>;
The data and address buffers should be assigned before starting the unlocking sequence.
NVMCON = <Operation to be performed>; // Ex. 0x4001 in case of word programming
Note that the write enable bit (NVMWREN) is also set to 1.
NVMKEY=0xaa996655; // Start of unlocking sequence
NVMKEY=0x556699aa; // End of unlocking sequence
NVMCONSET=0x8000; // Setting NVMWR bit to 1
Since this operation may take a while it is better to check if it is done. Upon completion, NVMWR bit will automatically go to zero. Therefore, reading that will tell us when it is completed.
while(NVMCON & 0x8000); // Waiting for it to response
Another method is to use the NVM driver to do the same thing. Likewise, the address and data register should be loaded first. In NVM driver’s library the function: _DRV_NVM_UnlockSequence(Register Value) controls the size of write operation. By passing the desired NVMOP register values to this function the appropriate mode can be selected.
There is also a flash driver that provides the same functionalities, and it has different functions for writing a row, a quad-word and a word. Which makes it much easier to switch between them. It, however, does not have a read functionality, therefore I am still using NVM_Read.