dsPIC33E/os_dsPIC33E_APIsB.c
// Function to write a row of flash data from the RAM buffer into the program memory.
// Author       : Fabian Kung
// Last Modified: 28 Nov 2018
// Argument:    unTablePage - Page number (for NVMADRU register) of the address
//              to the first word in program memory to read.
//              unTableOffset - Offset address to the first word in program
//              memory to read.
//              nRowOffset, from 0, 1, 2, ... 7 for page length with 8 rows.
// Return:      None
// Example of usage:
// Assuming progData[] contains an array of integer data store in the program
// memory, and we want to update the content in progData[].  We thus need to 
// provide the page and offset address of the first element of progData[0] to 
// this function.  We also need to furnish the row offset, which is integer 
// from 0,1,2, to (DSPIC33E_PAGELENGTH/DSPIC33E_ROWLENGTH)-1
// To extract the page and offset address for progData[0], we use the built-in 
// function that comes with the C-compiler.  
//
// The statement below programs the first 128 words (0-127) starting at the address 
// of progData[0] (Assume &progData[0] is already page aligned).
// ProgMemRowWrite(__builtin_tblpage(progData),__builtin_tbloffset(progData),0);
//
// The statement below programs the words 128-255 from the starting address 
// of progData[0] (Assume &progData[0] is already page aligned).
// ProgMemRowWrite(__builtin_tblpage(progData),__builtin_tbloffset(progData),0);

Function to write a row of flash data from the RAM buffer into the program memory.
Author       : Fabian Kung
Last Modified: 28 Nov 2018
Argument:    unTablePage - Page number (for NVMADRU register) of the address
to the first word in program memory to read.
unTableOffset - Offset address to the first word in program
memory to read.

The statement below programs the first 128 words (0-127) starting at the address
of progData[0] (Assume &progData[0] is already page aligned).

The statement below programs the words 128-255 from the starting address
of progData[0] (Assume &progData[0] is already page aligned).
Function to write a row of flash data from the RAM buffer into the program memory . Author : Fabian Kung Last Modified : 28 Nov 2018 Argument : unTablePage - Page number ( for NVMADRU register ) of the address to the first word in program memory to read . unTableOffset - Offset address to the first word in program memory to read . The statement below programs the first 128 words ( 0 - 127 ) starting at the address of progData [ 0 ] ( Assume &progData [ 0 ] is already page aligned ) . The statement below programs the words 128 - 255 from the starting address of progData [ 0 ] ( Assume &progData [ 0 ] is already page aligned ) .
void ProgMemRowWrite(unsigned int unTablePage, unsigned int unTableOffset, int nRowOffset)
{
    int nOffset, nTblpag_bak, i;
    if (nRowOffset < 8)             
    {
        nTblpag_bak = TBLPAG;       
        TBLPAG = 0xFA;              
        nOffset = nRowOffset*DSPIC33E_ROWLENGTH*2;
        for (i = 0; i< DSPIC33E_ROWLENGTH*2; i++)
        {
            __builtin_tblwtl(nOffset, gnRamBuffer[i++]);
            __builtin_tblwth(nOffset, gnRamBuffer[i]);
            nOffset += 2;
        }
        NVMADRU = unTablePage;
        NVMADR = (unTableOffset & 0xF800) + (nRowOffset*2*DSPIC33E_ROWLENGTH); 
        NVMCON = 0x4002;
        __builtin_disi(6);          
        __builtin_write_NVM();      
        TBLPAG = nTblpag_bak;       
    }
}
void ProgMemRowWrite ( unsigned int unTablePage , unsigned int unTableOffset , int nRowOffset ) { int nOffset , nTblpag_bak , i ; if ( nRowOffset < 8 ) { nTblpag_bak = TBLPAG ; TBLPAG = 0xFA ; nOffset = nRowOffset * DSPIC33E_ROWLENGTH * 2 ; for ( i = 0 ; i < DSPIC33E_ROWLENGTH * 2 ; i ++ ) { __builtin_tblwtl ( nOffset , gnRamBuffer [ i ++ ] ) ; __builtin_tblwth ( nOffset , gnRamBuffer [ i ] ) ; nOffset += 2 ; } NVMADRU = unTablePage ; NVMADR = ( unTableOffset & 0xF800 ) + ( nRowOffset * 2 * DSPIC33E_ROWLENGTH ) ; NVMCON = 0x4002 ; __builtin_disi ( 6 ) ; __builtin_write_NVM ( ) ; TBLPAG = nTblpag_bak ; } }
Function to write a row of flash data from the RAM buffer into the program memory.
Function to write a row of flash data from the RAM buffer into the program memory .
// 8 rows per page for dsPIC33E family.
 // The datasheet recommends backing up TBLPAG before 
 // modifying it.    
 // Base address of write latches.
 // Load row of data into write latches.
 // Set the destination address of the program memory into the
 // NVM address registers.
 // For page size of 1024 24-bits PM words.
 // Set WREN and enable row write in NVMCON.
 // Disable interrupts.
 // Initiate write process.
 // Restore TBLPAG.
