This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

On Chip Flash Protection - Password denied

Hello,

I'm actually trying to install sector/general protection as described in chapter 3.9 in the XC167-32 Manual.
For testing, I use the function ActivateGeneralReadProtection().
When calling InstallSecurityFeatures(..) and monitoring PROCON in the Debugger, the Controll Bits are correctly installed. (see //**)
But when trying to verify the Password executing DisableGeneralReadWriteProtection() the Password ist denied and in FSR the Bit PROER ist set (see //++).
As the Password is the same, I don't know what could be wrong. Has anyone got an idea?

Best regards,
Viviane

void ResetToReadMode()
{
        volatile unsigned int far *Flash_Command = (unsigned int far *)0x0C000AA;
        *Flash_Command = 0x00F0;
}

void ClearStatus()
{
        volatile unsigned int far *Flash_Command = (unsigned int far *)0x0C000AA;
        ResetToReadMode();
        *Flash_Command = 0x00F5;
}

int Read_FSR()
{
        volatile unsigned int far *Flash_Register = (unsigned int far *)0xFFF000;
        return *Flash_Register;
}

void Read_FSR_Busy()
{
        unsigned int Register = Read_FSR();

        while(Register & 0x0001)
        {
                Register = Read_FSR();
        }
}

int Read_PROCON()
{
        volatile unsigned int far *Flash_Register = (unsigned int far *)0xFFF004;
        return *Flash_Register;
}

int CheckError()
{
        unsigned int Register = Read_FSR();

        if(Register & 0x00C0)
        {
                return 0;
        }
        else
        {
                return 1;
        }
}

int EraseSecurityWordline(unsigned long ulSecurityWordline)
{
        volatile unsigned int far *Flash_Command_1 = (unsigned int far *)0x0C000AA;
        volatile unsigned int far *Flash_Command_2 = (unsigned int far *)0x0C00054;
        unsigned int far *Flash_Command_3 = (unsigned int far *) ulSecurityWordline;

        ClearStatus();

        *Flash_Command_1 = 0x0080;
        *Flash_Command_2 = 0x00A5;
        *Flash_Command_3 = 0x0053;

        Read_FSR_Busy();
        return (CheckError());
}


int EnterSecurityPageMode(unsigned long ulSecurityPage)
{
volatile unsigned int far *Flash_Command_1 = (unsigned int far *)0x0C000AA;
        unsigned int far *Flash_Command_2 = (unsigned int far *) ulSecurityPage;

        ClearStatus();

        *Flash_Command_1 = 0x0055;
        *Flash_Command_2 = 0x00AA;

        Read_FSR_Busy();
        return (Read_FSR() & 0x08);  //PAGE-Bit set?
}


int DisableGeneralReadWriteProtection(unsigned int *puiPassword)
{
        volatile unsigned int far *Flash_Command_1 = (unsigned int far *)0x0C0003C;
        volatile unsigned int far *Flash_Command_2 = (unsigned int far *)0x0C00054;
        volatile unsigned int far *Flash_Command_3 = (unsigned int far *)0x0C000AA;
        volatile unsigned int far *Flash_Command_4 = (unsigned int far *)0x0C0005A;

        if (Read_FSR() & 0x400) //FSR Bit 10 = PRODI set?
                return 1;

        ClearStatus();

        *Flash_Command_1 = 0x0000;
        *Flash_Command_2 = puiPassword[0];
        *Flash_Command_3 = puiPassword[1];
        *Flash_Command_2 = puiPassword[2];
        *Flash_Command_3 = puiPassword[3];
        *Flash_Command_4 = 0x0055;

        //++
        Read_FSR_Busy();

        if (Read_FSR() & 0x80)  //FSR Bit 7 = PROER set?
        {
                ResetToReadMode();
                return 0; //wrong PW
        }
        if (Read_FSR() & 0x400)
                return 1;
        return 0;
}

int InstallSecurityFeatures(unsigned int *puiPassword, unsigned int uiPROCON)
{
        char *pcBufferWord;
        volatile unsigned int far *PageBuffer = (unsigned int far *) 0xC000F2;
        volatile unsigned int far *Flash = (unsigned int far *)0x0C000AA;
        volatile unsigned int far *Write_Page = (unsigned int far *)0x0C0005A;
        unsigned long ulSecurityWordline0 =     0xC00000;
        unsigned long ulSecurityWordline1 = 0xC00100;
        unsigned long ulSecurityPage1 = 0xC00080;
        unsigned long ulSecurityPage2 = 0xC00100;

        if(!EraseSecurityWordline(ulSecurityWordline0))
                return 0;
        if(!EraseSecurityWordline(ulSecurityWordline1))
                return 0;
        if(!EnterSecurityPageMode(ulSecurityPage2))
                return 0;

        pcBufferWord = (char*) &uiPROCON;

        *PageBuffer = *pcBufferWord;
        *PageBuffer = *(pcBufferWord+1);
        *PageBuffer = puiPassword[0];
        *PageBuffer = puiPassword[1];
        *PageBuffer = puiPassword[2];
        *PageBuffer = puiPassword[3];

        //**
        *Flash      = 0x00A0;
        *Write_Page = 0x00AA;

        Read_FSR_Busy();

        if(!DisableGeneralReadWriteProtection(puiPassword))
                return 0;

        //Install Configuration Code
//      EnterSecurityPageMode(ulSecurityPage1);
//      pcBufferWord = (char*) &g_couiConfirmationCode;
//      *PageBuffer = *pcBufferWord;
//      *PageBuffer = *(pcBufferWord+1);
//      *Flash      = 0x00A0;
//      *Write_Page = 0x00AA;
//
//      Read_FSR_Busy();
//if(Read_FSR() & 0x400)

        return 1;
}

const unsigned int g_couiPW[4] ={0xFFAA, 0xFFBB, 0xFFCC, 0xFFDD};

void ActivateGeneralReadProtection()
{
        unsigned int uiProcon = Read_PROCON() | 0x8000;
        InstallSecurityFeatures(g_couiPW, uiProcon);
}