⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eeprom_drv.c

📁 飞思卡尔车身控制技术研讨会资料范例 基于LIN的收发通信
💻 C
📖 第 1 页 / 共 2 页
字号:
{
    //Verify address within range
    if ((AddrByte < EEPROMStart)    ||
            (AddrByte + Bytes > EEPROMEnd+1))
    {
        //Address out of EEPROM valid range
        return ERR_RANGE;     
    }
    //Perform command buffer empty check; if EEPROM is accessible, proceed 
    if ((FSTAT_FCBEF == 0)||(u8EEPROM_Tasks_Status != NO_TASK_PENDING))               
    {
        // EEPROM is busy, command cannot be performed now, try later
        return ERR_BUSY;
    }
    
    //Update Address and Data variables 
    u16EEPROM_ByteAddress = AddrByte;
    pu8EEPROM_ByteData_ptr = Data8_ptr;
    u8EEPROM_Bytes = Bytes;
    u16EEPROM_ByteEraseAddress = AddrByte;
    u8EEPROM_EraseBytes = Bytes;
    
    // Proceed with Word program command
    u8EEPROM_Tasks_Status = BYTE_ERASE_REQUEST;

    // Notify upper layer about request current status
    return ERR_OK;
}

/*******************************************************************************/
/**
* \brief    vfnEEPROM_Tasks  - This is were the actual programming and erasing is done!
* \author   B01246/ B07297
* \param    void
* \return   void
* \todo
*/
void vfnEEPROM_Tasks(void)
{
    /* Perform command buffer empty check; if EEPROM is accessible, proceed */
    if (FSTAT_FCBEF == 1)
    {
        switch(u8EEPROM_Tasks_Status)
        {    
            case(SECTOR_ERASE_REQUEST):
                /* Clear ACCERR/PIVOL Error flags if set */
                
                FSTAT = 0x30;
                /* Check if Sectors are blank already*/
                while (    (*(volatile UINT32 *)(u16EEPROM_SectAddress) == 0xFFFFFFFF) &&
                                (u8EEPROM_Sectors > 0) )
                {
                    u8EEPROM_Sectors--;
                    u16EEPROM_SectAddress += 4;
                }
                if (u8EEPROM_Sectors > 0)
                {    
                    /* Poll Command Completion Flag */
                    if (FSTAT_FCCF == 1)
                    {    
                        /* write dummy data to appropriate address */
                        *(volatile UINT8 *) u16EEPROM_SectAddress = 0;  
                        /* Sector is NOT blank, proceed with Sector Erase command */
                        FCMD = 0x40;
                        
                        /* Clear flag command buffer empty */
                        FSTAT_FCBEF = 1;
                        
                        asm {
                        nop
                        nop
                        nop
                        nop
                        }; //wait at least 4 clock cycles 
                        
                        /* Update Control Variables */
                        u8EEPROM_Sectors--;
                        u16EEPROM_SectAddress += 4;
                        /* Set proper state machine status */
                        u8EEPROM_Tasks_Status = SECTOR_ERASE_REQUEST;
                    }
                }
                else
                    u8EEPROM_Tasks_Status = WAIT_COMMAND_COMPLETION;
                break;
                
            case(BYTE_PROGRAM_REQUEST):
                /* Clear ACCERR/PIVOL Error flags if set */
               FSTAT = 0x30;
               
                /* Poll Command Completion Flag */
                if (FSTAT_FCCF == 1)    
                {
                   /* write Data byte to appropriate address */
                    *(volatile UINT8 *) u16EEPROM_ByteAddress = (UINT8)*pu8EEPROM_ByteData_ptr;  
                   /* Word is blank, proceed with Word Program command */
                    FCMD = 0x20;
                    /* Clear flag command buffer empty */
                    FSTAT_FCBEF = 1; 
                    asm {
                    nop
                    nop
                    nop
                    nop
                    }; //wait at least 4 clock cycles

                    /* Update Control Variables */
                    u8EEPROM_Bytes--;
                    u16EEPROM_ByteAddress ++;
                    pu8EEPROM_ByteData_ptr++;
                    /* Set proper state machine status */
                    if (u8EEPROM_Bytes > 0)    
                        u8EEPROM_Tasks_Status = BYTE_PROGRAM_REQUEST;
                    else
                        u8EEPROM_Tasks_Status = WAIT_COMMAND_COMPLETION;
                }    
                break;
            
            case(BYTE_ERASE_REQUEST): 
                /* Clear ACCERR/PIVOL Error flags if set */
                FSTAT = 0x30;
                /* Check if Words are blank already*/
                while (    (*(volatile UINT8 *)(u16EEPROM_ByteEraseAddress) == 0xFF) &&
                                (u8EEPROM_EraseBytes > 0) )
                {
                    u8EEPROM_EraseBytes--;
                    u16EEPROM_ByteEraseAddress ++;
                }
                if (u8EEPROM_EraseBytes > 0)
                {    
                    /* Poll Command Completion Flag */
                    if (FSTAT_FCCF == 1)
                    {    
                        /* write dummy data to appropriate address */
                        *(volatile UINT16 *) (u16EEPROM_ByteEraseAddress & 0xFFFC) = 0;  
                       /* Word is NOT blank, proceed with Sector Erase command */
                        FCMD = 0x40;
                        /* Clear flag command buffer empty */
                        FSTAT_FCBEF = 1; 
                        asm {
                        nop
                        nop
                        nop
                        nop
                        }; //wait at least 4 clock cycles
                        
                        /* Update Control Variables */
                        u8EEPROM_EraseBytes--;
                        u16EEPROM_ByteEraseAddress ++;
                        /* Set proper state machine status */
                        u8EEPROM_Tasks_Status = BYTE_ERASE_REQUEST;
                    }
                }
                else
                    u8EEPROM_Tasks_Status = BYTE_PROGRAM_REQUEST;
                break;
            
            case (WAIT_COMMAND_COMPLETION):
                /* Poll Command Completion Flag */
                if (FSTAT_FCCF == 1)
                {
                    /* Set proper state machine status */
                    u8EEPROM_Tasks_Status = NO_TASK_PENDING;    
                }    
                break;
                        
            case (NO_TASK_PENDING):
                break;
        }
    }
}

/*******************************************************************************/
/*                                                                             */
/* All software, source code, included documentation, and any implied know-how */
/* are property of Freescale Semiconductor and therefore considered            */ 
/* CONFIDENTIAL INFORMATION.                                                   */
/*                                                                             */
/* This confidential information is disclosed FOR DEMONSTRATION PURPOSES ONLY. */
/*                                                                             */
/* All Confidential Information remains the property of Freescale Semiconductor*/
/* and will not be copied or reproduced without the express written permission */
/* of the Discloser, except for copies that are absolutely necessary in order  */
/* to fulfill the Purpose.                                                     */
/*                                                                             */
/* Services performed by FREESCALE in this matter are performed AS IS and      */
/* without any warranty. CUSTOMER retains the final decision relative to the   */
/* total design and functionality of the end product.                          */
/*                                                                             */
/* FREESCALE neither guarantees nor will be held liable by CUSTOMER for the    */
/* success of this project.                                                    */
/*                                                                             */
/* FREESCALE disclaims all warranties, express, implied or statutory including,*/
/* but not limited to, implied warranty of merchantability or fitness for a    */
/* particular purpose on any hardware, software ore advise supplied to the     */
/* project by FREESCALE, and or any product resulting from FREESCALE services. */
/*                                                                             */
/* In no event shall FREESCALE be liable for incidental or consequential       */
/* damages arising out of this agreement. CUSTOMER agrees to hold FREESCALE    */
/* harmless against any and all claims demands or actions by anyone on account */
/* of any damage,or injury, whether commercial, contractual, or tortuous,      */
/* rising directly or indirectly as a result of the advise or assistance       */
/* supplied CUSTOMER in connectionwith product, services or goods supplied     */
/* under this Agreement.                                                       */
/*                                                                             */
/*******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -