3.18.3 NVMCTRL with Write-Protect
Non-Volatile Memory Controller with Write-Protect and Operation registers.
3.18.3.1 Introduction
The NVM module provides run-time read and write access to the Program Flash Memory (PFM), Data Flash Memory (DFM) and Configuration bits. PFM includes the program memory and user ID space while DFM is also referred to as EEPROM which is accessed one byte at a time and the erase before write is automatic. This NVM Module features a write-protect register that protects the memory registers from accidental writes.
3.18.3.2 Supported Device Families
PIC18F-Q40 | PIC18F-Q41 | PIC18F-Q43 | PIC18F-Q71 |
PIC18F-Q83 | PIC18F-Q84 |
3.18.3.3 Required header files:
#include "mcc_generated_files/nvm/nvm.h"
3.18.3.4 How to use the Memory Driver
The Memory driver can be used to access different types of memory. Click the links below to view examples on how to access the Flash and EEPROM memory.
- Accessing the Flash Memory
- Accessing the EEPROM Memory
- Accessing the Device ID
- Accessing the Configuration settings
3.18.3.6 File Documentation
3.18.3.6.1 source/nvm_example.dox File Reference
3.18.3.6.2 source/nvm_interrupt.c File Reference
#include "../nvm.h"
Functions
void NVM_Initialize (void)
Initializes the NVM driver.
bool NVM_IsBusy (void)
Checks if the NVM is busy with the read/write/erase commands.
nvm_status_t NVM_StatusGet (void)
Returns the status of the last NVM operation.
void NVM_StatusClear (void)
Clears the NVM error status.
void NVM_UnlockKeySet (uint16_t unlockKey)
Sets the unlock keys required to perform any erase or write operations on Flash and EEPROM.
void NVM_UnlockKeyClear (void)
Clears the previously configured unlock keys.
flash_data_t FLASH_Read (flash_address_t address)
Reads a byte from the given address of Flash.
nvm_status_t FLASH_RowRead (flash_address_t address, flash_data_t *dataBuffer)
Reads one entire Flash row/page from the given starting address of the row (the first byte location). The size of the buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_Write (flash_address_t address, uint16_t data)
Writes a word at the given Flash address. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_RowWrite (flash_address_t address, flash_data_t *dataBuffer)
Writes one entire Flash row (page) from the given starting address of the row (the first byte location). The size of the input buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row.
nvm_status_t FLASH_PageErase (flash_address_t address)
Erases one Flash page containing the given address.
flash_address_t FLASH_PageAddressGet (flash_address_t address)
Returns the starting address of the page (the first byte location) containing the given Flash address.
uint16_t FLASH_PageOffsetGet (flash_address_t address)
Returns the offset from the starting address of the page (the first byte location).
eeprom_data_t EEPROM_Read (eeprom_address_t address)
Reads one byte from the given EEPROM address.
void EEPROM_Write (eeprom_address_t address, eeprom_data_t data)
Writes one byte to the given EEPROM address. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
device_id_data_t DeviceID_Read (device_id_address_t address)
Reads the Device ID.
configuration_data_t CONFIGURATION_Read (configuration_address_t address)
Reads one Configuration byte.
void CONFIGURATION_Write (configuration_address_t address, configuration_data_t data)
Writes one Configuration byte. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
void NVM_InterruptEnable (void)
Enables the NVM interrupt.
void NVM_InterruptDisable (void)
Disables the NVM interrupt.
void NVM_ISR (void)
NVM Interrupt Service Routine (ISR) function.
void NVM_CallbackRegister (void(*CallbackHandler)(void))
Setter function for the NVM interrupt callback.
Variables
__near static volatile uint8_t unlockKeyLow
__near static volatile uint8_t unlockKeyHigh
static void(* NVM_Callback )(void) = NULL
Variable Documentation
NVM_Callback
void(* NVM_Callback) (void) = NULL[static]
unlockKeyHigh
__near static volatile uint8_t unlockKeyHigh[static]
unlockKeyLow
__near static volatile uint8_t unlockKeyLow[static]
3.18.3.6.3 source/nvm_interrupt.h File Reference
#include <xc.h> #include <stdbool.h>
Functions
void NVM_Initialize (void)
Initializes the NVM driver.
bool NVM_IsBusy (void)
Checks if the NVM is busy with the read/write/erase commands.
nvm_status_t NVM_StatusGet (void)
Returns the status of the last NVM operation.
void NVM_StatusClear (void)
Clears the NVM error status.
void NVM_UnlockKeySet (uint16_t unlockKey)
Sets the unlock keys required to perform any erase or write operations on Flash and EEPROM.
void NVM_UnlockKeyClear (void)
Clears the previously configured unlock keys.
flash_data_t FLASH_Read (flash_address_t address)
Reads a byte from the given address of Flash.
nvm_status_t FLASH_RowRead (flash_address_t address, flash_data_t *dataBuffer)
Reads one entire Flash row/page from the given starting address of the row (the first byte location). The size of the buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_Write (flash_address_t address, uint16_t data)
Writes a word at the given Flash address. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_RowWrite (flash_address_t address, flash_data_t *dataBuffer)
Writes one entire Flash row (page) from the given starting address of the row (the first byte location). The size of the input buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row.
nvm_status_t FLASH_PageErase (flash_address_t address)
Erases one Flash page containing the given address.
flash_address_t FLASH_PageAddressGet (flash_address_t address)
Returns the starting address of the page (the first byte location) containing the given Flash address.
uint16_t FLASH_PageOffsetGet (flash_address_t address)
Returns the offset from the starting address of the page (the first byte location).
eeprom_data_t EEPROM_Read (eeprom_address_t address)
Reads one byte from the given EEPROM address.
void EEPROM_Write (eeprom_address_t address, eeprom_data_t data)
Writes one byte to the given EEPROM address. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
device_id_data_t DeviceID_Read (device_id_address_t address)
Reads the Device ID.
configuration_data_t CONFIGURATION_Read (configuration_address_t address)
Reads one Configuration byte.
void CONFIGURATION_Write (configuration_address_t address, configuration_data_t data)
Writes one Configuration byte. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
void NVM_InterruptEnable (void)
Enables the NVM interrupt.
void NVM_InterruptDisable (void)
Disables the NVM interrupt.
void NVM_ISR (void)
NVM Interrupt Service Routine (ISR) function.
void NVM_CallbackRegister (void(*CallbackHandler)(void))
Setter function for the NVM interrupt callback.
Macros
#define PROGMEM_PAGE_SIZE (256U)
#define PROGMEM_SIZE (0x020000U)
#define BUFFER_RAM_START_ADDRESS (0x2500U)
#define EEPROM_START_ADDRESS (0x380000U)
#define EEPROM_SIZE (1024U)
#define UNLOCK_KEY (0xAA55U)
Typedefs
typedef uint8_t flash_data_t
Data type for the Flash data.
typedef uint24_t flash_address_t
Data type for the Flash address.
typedef uint8_t eeprom_data_t
Data type for the EEPROM data.
typedef uint24_t eeprom_address_t
Data type for the EEPROM address.
typedef uint16_t device_id_data_t
Data type for the Device ID data.
typedef uint24_t device_id_address_t
Data type for the Device ID address.
typedef uint8_t configuration_data_t
Data type for the Configurations Settings data.
typedef uint24_t configuration_address_t
Data type for the Configurations Settings address.
Enumerations
enum nvm_status_t { NVM_OK, NVM_ERROR, NVM_OK, NVM_ERROR }
Contains the return codes for the NVM driver APIs.
Macro Definition Documentation
FLASH_ErasePageAddressGet
#define FLASH_ErasePageAddressGet FLASH_PageAddressGet
FLASH_ErasePageOffsetGet
#define FLASH_ErasePageOffsetGet FLASH_PageOffsetGet
3.18.3.6.4 source/nvm_polling.c File Reference
#include "../nvm.h"
Functions
void NVM_Initialize (void)
Initializes the NVM driver.
bool NVM_IsBusy (void)
Checks if the NVM is busy with the read/write/erase commands.
nvm_status_t NVM_StatusGet (void)
Returns the status of the last NVM operation.
void NVM_StatusClear (void)
Clears the NVM error status.
void NVM_UnlockKeySet (uint16_t unlockKey)
Sets the unlock keys required to perform any erase or write operations on Flash and EEPROM.
void NVM_UnlockKeyClear (void)
Clears the previously configured unlock keys.
flash_data_t FLASH_Read (flash_address_t address)
Reads a byte from the given address of Flash.
nvm_status_t FLASH_RowRead (flash_address_t address, flash_data_t *dataBuffer)
Reads one entire Flash row/page from the given starting address of the row (the first byte location). The size of the buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_Write (flash_address_t address, uint16_t data)
Writes a word at the given Flash address. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_RowWrite (flash_address_t address, flash_data_t *dataBuffer)
Writes one entire Flash row (page) from the given starting address of the row (the first byte location). The size of the input buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row.
nvm_status_t FLASH_PageErase (flash_address_t address)
Erases one Flash page containing the given address.
flash_address_t FLASH_PageAddressGet (flash_address_t address)
Returns the starting address of the page (the first byte location) containing the given Flash address.
uint16_t FLASH_PageOffsetGet (flash_address_t address)
Returns the offset from the starting address of the page (the first byte location).
eeprom_data_t EEPROM_Read (eeprom_address_t address)
Reads one byte from the given EEPROM address.
void EEPROM_Write (eeprom_address_t address, eeprom_data_t data)
Writes one byte to the given EEPROM address. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
device_id_data_t DeviceID_Read (device_id_address_t address)
Reads the Device ID.
configuration_data_t CONFIGURATION_Read (configuration_address_t address)
Reads one Configuration byte.
void CONFIGURATION_Write (configuration_address_t address, configuration_data_t data)
Writes one Configuration byte. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
Variables
__near static volatile uint8_t unlockKeyLow
__near static volatile uint8_t unlockKeyHigh
Variable Documentation
unlockKeyHigh
__near static volatile uint8_t unlockKeyHigh[static]
unlockKeyLow
__near static volatile uint8_t unlockKeyLow[static]
3.18.3.6.5 source/nvm_polling.h File Reference
#include <xc.h> #include <stdbool.h>
Functions
void NVM_Initialize (void)
Initializes the NVM driver.
bool NVM_IsBusy (void)
Checks if the NVM is busy with the read/write/erase commands.
nvm_status_t NVM_StatusGet (void)
Returns the status of the last NVM operation.
void NVM_StatusClear (void)
Clears the NVM error status.
void NVM_UnlockKeySet (uint16_t unlockKey)
Sets the unlock keys required to perform any erase or write operations on Flash and EEPROM.
void NVM_UnlockKeyClear (void)
Clears the previously configured unlock keys.
flash_data_t FLASH_Read (flash_address_t address)
Reads a byte from the given address of Flash.
nvm_status_t FLASH_RowRead (flash_address_t address, flash_data_t *dataBuffer)
Reads one entire Flash row/page from the given starting address of the row (the first byte location). The size of the buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_Write (flash_address_t address, uint16_t data)
Writes a word at the given Flash address. As this is a non-portable API, it may not be supported by all 8-bit PIC® and AVR® devices.
nvm_status_t FLASH_RowWrite (flash_address_t address, flash_data_t *dataBuffer)
Writes one entire Flash row (page) from the given starting address of the row (the first byte location). The size of the input buffer must be one Flash row and the address must be aligned with the row boundary. Use FLASH_PageAddressGet() to obtain the starting address of the row.
nvm_status_t FLASH_PageErase (flash_address_t address)
Erases one Flash page containing the given address.
flash_address_t FLASH_PageAddressGet (flash_address_t address)
Returns the starting address of the page (the first byte location) containing the given Flash address.
uint16_t FLASH_PageOffsetGet (flash_address_t address)
Returns the offset from the starting address of the page (the first byte location).
eeprom_data_t EEPROM_Read (eeprom_address_t address)
Reads one byte from the given EEPROM address.
void EEPROM_Write (eeprom_address_t address, eeprom_data_t data)
Writes one byte to the given EEPROM address. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
device_id_data_t DeviceID_Read (device_id_address_t address)
Reads the Device ID.
configuration_data_t CONFIGURATION_Read (configuration_address_t address)
Reads one Configuration byte.
void CONFIGURATION_Write (configuration_address_t address, configuration_data_t data)
Writes one Configuration byte. The NVM busy status must be checked using the NVM_IsBusy() API to know if the write operation is completed.
Macros
#define PROGMEM_PAGE_SIZE (256U)
#define PROGMEM_SIZE (0x020000U)
#define BUFFER_RAM_START_ADDRESS (0x2500U)
#define EEPROM_START_ADDRESS (0x380000U)
#define EEPROM_SIZE (1024U)
#define UNLOCK_KEY (0xAA55U)
Typedefs
typedef uint8_t flash_data_t
Data type for the Flash data.
typedef uint24_t flash_address_t
Data type for the Flash address.
typedef uint8_t eeprom_data_t
Data type for the EEPROM data.
typedef uint24_t eeprom_address_t
Data type for the EEPROM address.
typedef uint16_t device_id_data_t
Data type for the Device ID data.
typedef uint24_t device_id_address_t
Data type for the Device ID address.
typedef uint8_t configuration_data_t
Data type for the Configurations Settings data.
typedef uint24_t configuration_address_t
Data type for the Configurations Settings address.
Enumerations
enum nvm_status_t { NVM_OK, NVM_ERROR, NVM_OK, NVM_ERROR }
Macro Definition Documentation
FLASH_ErasePageAddressGet
#define FLASH_ErasePageAddressGet FLASH_PageAddressGet
FLASH_ErasePageOffsetGet
#define FLASH_ErasePageOffsetGet FLASH_PageOffsetGet
Enumeration Type Documentation
nvm_status_t
enum nvm_status_t
NVM_OK |
0 - The NVM operation succeeded. | |
NVM_ERROR |
1 - The NVM operation failed. | |
NVM_OK |
0 - The NVM operation succeeded. | |
NVM_ERROR |
1 - The NVM operation failed. |