// TODO 11-7.01
bool NVMCTRL_AuxPageWrite( uint32_t *data, const uint32_t address )
{
    uint32_t i = 0U;
    uint32_t * paddress = (uint32_t *)address;

    /* writing 32-bit data into the given address */
    for (i = 0U; i < (NVMCTRL_FLASH_PAGESIZE/4U); i++)
    {
        *paddress = *(data + i);
        paddress++;
    }

    /* Set address and command */
    NVMCTRL_REGS->NVMCTRL_ADDR = address >> 1U;
    NVMCTRL_REGS->NVMCTRL_CTRLA = (uint16_t)(NVMCTRL_CTRLA_CMD_WAP_Val | NVMCTRL_CTRLA_CMDEX_KEY);

    return true;
}

bool NVMCTRL_AuxRowErase( uint32_t address )
{
    /* Set address and command */
    NVMCTRL_REGS->NVMCTRL_ADDR = address >> 1U;
    NVMCTRL_REGS->NVMCTRL_CTRLA = (uint16_t)(NVMCTRL_CTRLA_CMD_EAR_Val | NVMCTRL_CTRLA_CMDEX_KEY);

    return true;
}

bool NVMCTRL_GetSecurityBitStatus( void )
{
    /* Get Security Bit Status */
    if( NVMCTRL_REGS->NVMCTRL_STATUS & NVMCTRL_STATUS_SB(1UL) )
    {
        return true;
    }

    return false;
}

void NVM_Write_AUX0( uint32_t *Word )
{
    uint32_t i;

    while( NVMCTRL_IsBusy() ) {}

    /* Auxiliary space cannot be accessed if the security bit is set */
    if( NVMCTRL_GetSecurityBitStatus() )
    {
        myprintf( "Security Bit has been set!\r\n" );
        return;
    }

    myprintf( "Erase NVM-AUX0 User Row : \r\n" );
    NVMCTRL_AuxRowErase((uint32_t)USER_PAGE_ADDR);
    while( NVMCTRL_IsBusy() ) {}
    myprintf( " - success\r\n" );

    myprintf( "Write NVM-AUX0 User Row : \r\n" );
    for( i=0 ; i<4 ; i++ )
    {
        myprintf( " - Page %d : ", i );
        NVMCTRL_AuxPageWrite((uint32_t *)Word, (uint32_t)USER_PAGE_ADDR+(i*64));
        while( NVMCTRL_IsBusy() ) {}
        myprintf( "success\r\n" );
        Word+=16;
    }
    myprintf( "done\r\n\r\n" );
}


// TODO 11-7.02
    NVM_AUX0_Word[4] = 0x12345678;
    NVM_Write_AUX0( NVM_AUX0_Word ); // Remember to Execute NVM_Read_AUX0() before this function!!!
    NVM_Read_AUX0( NVM_AUX0_Word );
