

#include "phRomHal_Eeprom.h"
#include "phRomHal_Flash.h"
#include "phCommon.h"

/* Program Page in Flash Memory
 *    Parameter:      adr:  Page Start Address
 *                    sz:   Page Size
 *                    buf:  Page Data
 *    Return Value:   0 - OK,  1 - Failed */
int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
    phStatus_t phstat;
    int istat = 1 ; /* By default failure */

    if ( WITHIN_PAGEFLASH(adr)) {
        phstat = phRomHal_Flash_WriteBuffer((uint8_t *)buf, (uint8_t *)adr, sz);
        if ( PH_STATUS_SUCCESS == phstat ) {
            istat = 0; /* success */
        }
    }
    else if ( WITHIN_EEPROM(adr)) {
        phstat = phRomHal_Eeprom_WriteBuffer((uint8_t *)buf, (uint8_t *)adr, sz);
        if ( PH_STATUS_SUCCESS == phstat ) {
            istat = 0; /* success */
        }
    } 
    else {
        istat = 1; /* failure */
    }
    return istat; 
}
