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

📄 nvm.c

📁 uCOS-II example for MC9S12DPxxx
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*********************************************************************************************************
*                               Freescale MC9S12 Processor Support
*   
*                                Non Volatile Memory Read / Write
*
* File       : nvm.c
*
* By         : GeorgeWong
*            : Eric Shufro
*
* Rev. Hist. : 08/12/2005 - Created
*              08/25/2005 - EEPROM_Erase_Sector
*                           Flash_Erase_Block
*              09/17/2006 - Cleaned up
*
* Description: Provides low level hardware functions for use with portable application code
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                        INCLUDES
*********************************************************************************************************
*/

#include <includes.h> 

/*
*********************************************************************************************************
*                                       CONSTANTS
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                        EEPROM INITIALIZATION
*
* Description : This function initializes the Non Volatile EEPROM control registers and must be called
*               before attempting to write or erase an EEPROM sector.
*
* Arguments   : sysclk, the CPU clock frequency (SYSCLK) driven by the onboard oscillator or the 
*               PLL if enabled.
*
* Returns     : None.
*
* Notes       : 1) See bsp.c, BSP_CPU_ClkFreq(), as this function may be used to determine the SYSCLK
*                  frequency.
*********************************************************************************************************
*/

void  EEPROM_Init (INT32U sysclk)
{
    INT8U  eclk_val;
	
	
    if (sysclk >= 12000) {                                              /* If the SYSCLK is > 12MHz, then set FDIV8 bit             */          
        eclk_val  =  (sysclk  / (8*200)) - 1;                           /* Compute the correct divider value                        */
        ECLKDIV  |=   ECLKDIV_PRDIV8_MASK | eclk_val;                   /* Write the ECLKDIV register with the correct settings     */
    } else {
        eclk_val  =  (sysclk / 200) - 1;                                /* Compute the correct divider value                        */
        ECLKDIV  |=   eclk_val;                                         /* Write the ECLKDIV register with the correct settings     */
    }

    ESTAT        |=  (ESTAT_PVIOL_MASK | ESTAT_ACCERR_MASK);              /* Clear any error flags                                  */
}

/*
*********************************************************************************************************
*                                        EEPROM WRITE
*
* Description : This function writes a 16-bit word to EEPROM
*
* Arguments   : address, the destination EEPROM address to write the data
*               data,    the data to write to argument address.
*
* Returns     : NVM_NO_ERR           - EEPROM Write Success
*               NVM_ODD_ACCESS_ERR   - EEPROM Write Error, Address not on an even address boundry
*               NVM_ACCESS_ERR       - EEPROM Write Error, Access Violation
*               NVM_PROTECTION_ERR   - EEPROM Write Error, Attempted to write a protected sector
*
* Notes       : None.
*********************************************************************************************************
*/

INT8S  EEPROM_Write_Word (INT16U address, INT16U data)
{
    while (!ESTAT_CBEIF) {                                              /* Wait for EEPROM access controller to become ready        */
        ;
    }
    
    ESTAT = (ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK);                     /* Clear existing error flags                               */
    
    if (address & 0x0001) {
        return (NVM_ODD_ACCESS_ERR);                                    /* Address is NOT aligned on an even boundry?               */
    }
    
    (*(INT16U *)address) = data;                                        /* Write the data to the specified address                  */

    ECMD = ECMD_CMDB5_MASK;	                                            /* Store programming command in FCMD                        */
    ESTAT_CBEIF = 1;                                                    /* Execute the command                                      */

    if (ESTAT_ACCERR) {                                                 /* Check if there has been an access error                  */
        return (NVM_ACCESS_ERR);                                        /* Return an Access Error code                              */
    }
    
    if (ESTAT_PVIOL) {                                                  /* Check if there has been a protection error               */
        return (NVM_PROTECTION_ERR);                                    /* Return a Protection Error code                           */
    }
    
    return (NVM_NO_ERR);                                                /* Return No Error                                          */
}

/*
*********************************************************************************************************
*                                        EEPROM ERASE SECTOR
*
* Description : This function erases a 4-byte sector of EEPROM
*
* Arguments   : address, the start of the 4-byte sector to address
*
* Returns     : NVM_NO_ERR           - EEPROM Write Success
*               NVM_ODD_ACCESS_ERR   - EEPROM Write Error, Address not on an even address boundry
*               NVM_ACCESS_ERR       - EEPROM Write Error, Access Violation
*               NVM_PROTECTION_ERR   - EEPROM Write Error, Attempted to write a protected sector
*
* Notes       : None.
*********************************************************************************************************
*/

INT8S  EEPROM_Erase_Sector (INT16U address)
{
    while (!ESTAT_CBEIF) {                                              /* Wait for EEPROM access controller to become ready        */
        ;
    }
    
    ESTAT = (ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK);                     /* Clear existing error flags                               */
    
    if (address & 0x0001) {
        return (NVM_ODD_ACCESS_ERR);                                    /* Address is NOT aligned on an even boundry?               */
    }
    
    (*(INT16U *)address) = 0xFFFF;                                      /* Write the data to the specified address                  */

    ECMD = ECMD_CMDB6_MASK;	                                            /* Store programming command in FCMD                        */
    ESTAT_CBEIF = 1;                                                    /* Execute the command                                      */

    if (ESTAT_ACCERR) {                                                 /* Check if there has been an access error                  */
        return (NVM_ACCESS_ERR);                                        /* Return an Access Error code                              */
    }
    
    if (ESTAT_PVIOL) {                                                  /* Check if there has been a protection error               */
        return (NVM_PROTECTION_ERR);                                    /* Return a Protection Error code                           */
    }
    
    return (NVM_NO_ERR);                                                /* Return No Error                                          */
}

/*
*********************************************************************************************************
*                                        EEPROM Read
*
* Description : This function reads a 16-bit word from the specified address in EEPROM
*
* Arguments   : address, the start of the 16-bit data to read
*
* Returns     : The 16-bit word stored in location 'address'
*
* Notes       : None.
*********************************************************************************************************
*/

INT16U  EEPROM_Read_Word (INT16U address)

⌨️ 快捷键说明

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