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

📄 drvfmc.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "DrvFMC.h"
#include "DrvSYS.h"


#define CONFIG0         0x00300000
#define CONFIG1         0x00300004

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvFMC_Write                                                                                  */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               u32addr    - [in]      Flash address include APROM, LDROM, Data Flash, and Config         */
/*               u32data    - [in]      Word Data                                                          */
/*                                                                                                         */
/* Returns:                                                                                                */
/*               0       Success                                                                           */
/*              <0       Failed when illegal condition occurs                                              */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Write data into Flash include APROM, LDROM, Data Flash, and Config                        */
/*               The corresponding functions in Config0 & Config1 are listed in FMC section of TRM         */
/*                                                                                                         */
/* Note:                                                                                                   */
/*               Please make sure that Register Write-Protection Function has been disabled before using   */
/*               this function.                                                                            */
/*               User can check the status of Register Write-Protection Function                           */
/*               with DrvSYS_IsProtectedRegLocked().                                                       */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvFMC_Write(uint32_t u32addr, uint32_t u32data)
{
    FMC->ISPCMD.FCTRL = 1;
    FMC->ISPCMD.FCEN = 0;
    FMC->ISPCMD.FOEN = 1;
    FMC->ISPADR = u32addr;
    FMC->ISPDAT = u32data;
    __set_PRIMASK(1);
    FMC->ISPTRG.ISPGO = 1;
    __ISB();
    while (FMC->ISPTRG.ISPGO);
    __set_PRIMASK(0);
    
    if (FMC->ISPCON.ISPFF == 1)
    {
        FMC->ISPCON.ISPFF = 1;
        return E_DRVFMC_ERR_ISP_FAIL;
    }
    
    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvFMC_Read                                                                                   */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               u32addr    - [in]      Flash address include APROM, LDROM, Data Flash, and Config         */
/*               u32data    - [out]     Data                                                               */
/*                                                                                                         */
/* Returns:                                                                                                */
/*               0       Success                                                                           */
/*              <0       Failed when illegal condition occurs                                              */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Read data from Flash include APROM, LDROM, Data Flash, and Config                         */
/*                                                                                                         */
/* Note:                                                                                                   */
/*               Please make sure that Register Write-Protection Function has been disabled before using   */
/*               this function.                                                                            */
/*               User can check the status of Register Write-Protection Function                           */
/*               with DrvSYS_IsProtectedRegLocked().                                                       */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvFMC_Read(uint32_t u32addr, uint32_t * u32data)
{ 
    FMC->ISPCMD.FCTRL = 0;
    FMC->ISPCMD.FCEN = 0;
    FMC->ISPCMD.FOEN = 0;
    FMC->ISPADR = u32addr;
    FMC->ISPDAT = 0;
    __set_PRIMASK(1);
    FMC->ISPTRG.ISPGO = 1;    
    __ISB();
    while (FMC->ISPTRG.ISPGO);
    __set_PRIMASK(0);
    
    if (FMC->ISPCON.ISPFF == 1)
    {
        FMC->ISPCON.ISPFF = 1;
        return E_DRVFMC_ERR_ISP_FAIL;
    }
    
    *u32data = FMC->ISPDAT;
    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvFMC_Erase                                                                                  */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               u32addr    - [in]      Flash page base address or Config0 address                         */
/*                                                                                                         */
/* Returns:                                                                                                */
/*               0       Success                                                                           */
/*              <0       Failed when illegal condition occurs                                              */
/*                                                                                                         */
/* Description:                                                                                            */
/*             Page Erase Flash include APROM, LDROM, Data Flash, and Config. The erase unit is 512 bytes  */
/*                                                                                                         */
/* Note:                                                                                                   */
/*             Please make sure that Register Write-Protection Function has been disabled before using     */
/*             this function.                                                                              */
/*             User can check the status of Register Write-Protection Function                             */
/*             with DrvSYS_IsProtectedRegLocked().                                                         */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvFMC_Erase(uint32_t u32addr)
{
    FMC->ISPCMD.FCTRL = 2;
    FMC->ISPCMD.FCEN = 0;
    FMC->ISPCMD.FOEN = 1;
    FMC->ISPADR = u32addr;
    __set_PRIMASK(1);
    FMC->ISPTRG.ISPGO = 1;  
    __ISB();
    while (FMC->ISPTRG.ISPGO);  
    __set_PRIMASK(0);
    
    if (FMC->ISPCON.ISPFF == 1)
    {
        FMC->ISPCON.ISPFF = 1;
        return E_DRVFMC_ERR_ISP_FAIL;
    }
    
    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvFMC_EnableISP                                                                              */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               None                                                                                      */
/*                                                                                                         */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Enable ISP function. This function will check if internal oscillator 22M is enabled or not*/                                                   
/*               If not, this function will enable 22M oscillator automatically.                           */
/*                                                                                                         */
/* Note:                                                                                                   */
/*               Please make sure that Register Write-Protection Function has been disabled before using   */
/*               this function to enable ISP function.                                                     */
/*               User can check the status of Register Write-Protection Function                           */
/*               with DrvSYS_IsProtectedRegLocked().                                                       */
/*---------------------------------------------------------------------------------------------------------*/
void DrvFMC_EnableISP(void)
{
    if (SYSCLK->PWRCON.OSC22M_EN == 0)
    {
        SYSCLK->PWRCON.OSC22M_EN = 1;
        
        /* Wait 22M stable */
        DrvSYS_Delay(12);
    }

    SYSCLK->AHBCLK.ISP_EN = 1;
    FMC->ISPCON.ISPEN = 1;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvFMC_DisableISP                                                                             */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               None                                                                                      */
/*                                                                                                         */
/*                                                                                                         */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Disable ISP function.                                                                     */                                                   

⌨️ 快捷键说明

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