// TODO 11-8.01
bool NVMCTRL_SetSecurityBit( void )
{
    /* Send SSB(Set Security Bit) command */
    NVMCTRL_REGS->NVMCTRL_CTRLA = (uint16_t)(NVMCTRL_CTRLA_CMD_SSB_Val | NVMCTRL_CTRLA_CMDEX_KEY);

    return true;
}

void NVM_Code_Protect( void )
{
    while( NVMCTRL_IsBusy() ) {}

    if( NVMCTRL_GetSecurityBitStatus() )
    {
        /* Security Bit has been set */
        return;
    }

    NVMCTRL_SetSecurityBit();

    myprintf( "Security Bit set successfully, please cycle power to active!\r\n\r\n" );
}

#define SELF_RW_ADDRESS 0x3000
void NVM_Self_ReadWrite()
{
    uint32_t ReadWord[4];
    uint32_t WritePage[16];

    while( NVMCTRL_IsBusy() ) {}

    // Read by Byte (4 WORD = 16 Bytes)
    NVMCTRL_Read( ReadWord, 16, SELF_RW_ADDRESS );

    myprintf( "Self Read (4 Words)\r\n");
    myprintf( "0x%08X : 0x%08X\r\n", SELF_RW_ADDRESS+0x0, ReadWord[0] );
    myprintf( "0x%08X : 0x%08X\r\n", SELF_RW_ADDRESS+0x4, ReadWord[1] );
    myprintf( "0x%08X : 0x%08X\r\n", SELF_RW_ADDRESS+0x8, ReadWord[2] );
    myprintf( "0x%08X : 0x%08X\r\n", SELF_RW_ADDRESS+0xC, ReadWord[3] );

    // Erase by Row  (4 Pages = 4 x 64 = 256 Bytes)
    NVMCTRL_RowErase( SELF_RW_ADDRESS );
    while( NVMCTRL_IsBusy() ) {}
    myprintf( "Erase Row  at address 0x%08X\r\n", SELF_RW_ADDRESS );

    // Write by Page (64 Byte = 16 WORDs)
    WritePage[0] = ReadWord[0]-0x11111111;
    WritePage[1] = ReadWord[1]-0x22222222;
    WritePage[2] = ReadWord[2]-0x33333333;
    WritePage[3] = ReadWord[3]-0x44444444;
    NVMCTRL_PageWrite( WritePage, SELF_RW_ADDRESS );
    while( NVMCTRL_IsBusy() ) {}
    myprintf( "Write Page at address 0x%08X\r\n", SELF_RW_ADDRESS );
    myprintf( "done\r\n\r\n" );
}


// TODO 11-8.02
    NVM_Code_Protect();
    NVM_Self_ReadWrite();
