/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* EEPROM演示程序.c */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvADC.h"
#include "Driver/DrvFMC.h"
#ifndef REGISTER_H
#define REGISTER_H
// 定时器相关的宏
#define M_TIMER_TDR0 (*((volatile unsigned long *)(0x4001000c)))
#define M_TIMER_TDR1 (*((volatile unsigned long *)(0x4001002c)))
// 系统时钟相关宏
#define M_CLKSEL1 (*((volatile unsigned long *)(0x50000214)))
#define M_APBCLK (*((volatile unsigned long *)(0x50000208)))
// PWM寄存器相关宏
#define M_PWM_PPRA (*((volatile unsigned long *)(0x40040000)))
#define M_PWM_CSRA (*((volatile unsigned long *)(0x40040004)))
#define M_PWM_PCRA (*((volatile unsigned long *)(0x40040008)))
#define M_PWM_CNR0 (*((volatile unsigned long *)(0x4004000C)))
#define M_PWM_CMR0 (*((volatile unsigned long *)(0x40040010)))
#define M_PWM_PDR0 (*((volatile unsigned long *)(0x40040014)))
#define M_PWM_CNR1 (*((volatile unsigned long *)(0x40040018)))
#define M_PWM_CMR1 (*((volatile unsigned long *)(0x4004001c)))
#define M_PWM_PDR1 (*((volatile unsigned long *)(0x40040020)))
#define M_PWM_PIERA (*((volatile unsigned long *)(0x40040040)))
#define M_PWM_PIIRA (*((volatile unsigned long *)(0x40040044)))
#define M_PWM_POEA (*((volatile unsigned long *)(0x4004007C)))
#define M_PWM_CCR0A (*((volatile unsigned long *)(0x40040050)))
#define M_PWM_CCR2A (*((volatile unsigned long *)(0x40040054)))
#define M_PWM_CRLR0 (*((volatile unsigned long *)(0x40040058)))
#define M_PWM_CRLR1 (*((volatile unsigned long *)(0x40040060)))
#define M_PWM_CRLR2 (*((volatile unsigned long *)(0x40040068)))
#define M_PWM_CRLR3 (*((volatile unsigned long *)(0x40040070)))
#define M_PWM_CFLR0 (*((volatile unsigned long *)(0x4004005C)))
#define M_PWM_CFLR1 (*((volatile unsigned long *)(0x40040064)))
#define M_PWM_CFLR2 (*((volatile unsigned long *)(0x4004006C)))
#define M_PWM_CFLR3 (*((volatile unsigned long *)(0x40040074)))
#define M_PWM_CAPENRA (*((volatile unsigned long *)(0x40040078)))
// IO口相关宏
#define M_GPIOA_DOUT (*((volatile unsigned long *)(0x50004008)))
#define M_GPIOA_PIN (*((volatile unsigned long *)(0x50004010)))
#define M_GPIOB_DOUT (*((volatile unsigned long *)(0x50004048)))
#define M_GPIOB_PIN (*((volatile unsigned long *)(0x50004050)))
#define M_GPIOC_DOUT (*((volatile unsigned long *)(0x50004088)))
#define M_GPIOC_PIN (*((volatile unsigned long *)(0x50004090)))
#define M_GPIOD_DOUT (*((volatile unsigned long *)(0x500040C8)))
#define M_GPIOD_PIN (*((volatile unsigned long *)(0x500040D0)))
#define M_GPIOE_DOUT (*((volatile unsigned long *)(0x50004108)))
#define M_GPIOE_PIN (*((volatile unsigned long *)(0x50004110)))
// 串口相关宏
#define M_UA_FCR_0 (*((volatile unsigned long *)(0x40050008)))
#define M_UA_FCR_1 (*((volatile unsigned long *)(0x40150008)))
//AD相关宏
#define ADCHER (*((volatile unsigned long *)(0x400E0024)))
#endif
uint32_t isp[16]; //用于存放充EEPROM中读取的数据
////////////////////////////////////////////////////////////////////////////////////////////////////
void SYS_INIT(void)
{
UNLOCKREG();
SYSCLK->PWRCON.XTL12M_EN = 1;
DrvSYS_Delay(100000);
//设置端口模式
DrvGPIO_InitFunction(E_FUNC_GPIO); //设置所有IO为GPIO
DrvGPIO_Open (E_GPA, 0, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 1, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 2, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 3, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 4, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 5, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 6, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 7, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 8, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 9, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 10, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 11, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 12, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 13, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 14, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPA, 15, E_IO_INPUT); //输入
// DrvGPIO_Open (E_GPB, 0, E_IO_OUTPUT);
// DrvGPIO_Open (E_GPB, 1, E_IO_OUTPUT);
DrvGPIO_Open (E_GPB, 2, E_IO_OUTPUT);
DrvGPIO_Open (E_GPB, 3, E_IO_OUTPUT);
// DrvGPIO_Open (E_GPB, 4, E_IO_OUTPUT);
// DrvGPIO_Open (E_GPB, 5, E_IO_OUTPUT);
DrvGPIO_Open (E_GPB, 6, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPB, 7, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPB, 8, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPB, 9, E_IO_OUTPUT);
DrvGPIO_Open (E_GPB, 10, E_IO_OUTPUT);
DrvGPIO_Open (E_GPB, 11, E_IO_OUTPUT);
DrvGPIO_Open (E_GPB, 12, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPB, 13, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPB, 14, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPB, 15, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 0, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 1, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 2, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 3, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 4, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 5, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 6, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPC, 7, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPC, 8, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPC, 9, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPC, 10, E_IO_INPUT); //输入
DrvGPIO_Open (E_GPC, 11, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 12, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 13, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 14, E_IO_OUTPUT);
DrvGPIO_Open (E_GPC, 15, E_IO_OUTPUT);
DrvGPIO_Open (E_GPE, 0, E_IO_OUTPUT);
DrvGPIO_Open (E_GPE, 1, E_IO_OUTPUT);
DrvGPIO_Open (E_GPE, 2, E_IO_OUTPUT);
DrvGPIO_Open (E_GPE, 3, E_IO_OUTPUT);
DrvGPIO_Open (E_GPE, 4, E_IO_OUTPUT);
DrvGPIO_Open (E_GPE, 5, E_IO_OUTPUT);
}
void delay_ms(int16_t i)
{
int16_t y;
for(y=i;y>0;y--)
{
DrvSYS_Delay(1000); //延迟1ms
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//函数作用: 执行EEPROM的指定页位置的擦除操作
//函数介绍: 当所在页的相应地址中写过数据时,要再次向该地址写数据时必须擦除该地址所在页;注意只能整体擦除该页
//函数使用:擦除地址u32addr位置为0-127 ,第零页(大小512K);页u32page为0
// 擦除地址u32addr位置为128-255 ,第一页(大小512K);页u32page为1
// 擦除地址u32addr位置为256-383 ,第二页(大小512K);页u32page为2
// 擦除地址u32addr位置为384-511 ,第三页(大小512K);页u32page为3
// 擦除地址u32addr位置为512-639 ,第四页(大小512K);页u32page为4
// 擦除地址u32addr位置为640-767 ,第五页(大小512K);页u32page为5
// 擦除地址u32addr位置为768-895 ,第六页(大小512K);页u32page为6
// 擦除地址u32addr位置为896-1023 ,第七页(大小512K);页u32page为7
//函数返回值: 无
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DATA_FLASH_Erase(uint32_t u8page)
{
__set_PRIMASK(1);//关闭全局中断
UNLOCKREG();
DrvFMC_EnableISP(); //使能ISP功能
switch(u8page)
{
case 0: DrvFMC_Erase (0x1F000);break;//擦除第零页, 0x1F000 to 0x1F1FF
case 1: DrvFMC_Erase (0x1F200);break;//擦除第一页, 0x1F200 to 0x1F3FF
case 2: DrvFMC_Erase (0x1F400);break;//擦除第二页, 0x1F400 to 0x1F5FF
case 3: DrvFMC_Erase (0x1F600);break;//擦除第三页, 0x1F600 to 0x1F7FF
case 4: DrvFMC_Erase (0x1F800);break;//擦除第四页, 0x1F800 to 0x1F9FF
case 5: DrvFMC_Erase (0x1FA00);break;//擦除第五页, 0x1FA00 to 0x1FBFF
case 6: DrvFMC_Erase (0x1FC00);break;//擦除第六页, 0x1FC00 to 0x1FDFF
case 7: DrvFMC_Erase (0x1FE00);break;//擦除第七页, 0x1FE00 to 0x1FFFF
default:break;
}
DrvFMC_DisableISP(); //禁止ISP功能
LOCKREG();
__set_PRIMASK(0);//打开全局中断
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//函数作用:执行EEPROM的写操作
//函数介绍:使用该功能之前,先必须将AP ROM中的空间0x0001F000---0x0001FFFF(4k*8BIT)刷为EEPROM用(只针对128K芯片),
// 页写之前要先将该页全部擦除,共8页。数据分为8*512B(数据为32位128*32)=4096B
//函数使用:写地址u32addr位置为0-127 ,写在第零页0x1F000 to 0x1F1FF (大小512K);数据u32data为32位;
// 写地址u32addr位置为128-255 ,写在第一页0x1F200 to 0x1F3FF (大小512K);数据u32data为32位;
// 写地址u32addr位置为256-383 ,写在第二页0x1F400 to 0x1F5FF (大小512K);数据u32data为32位;
// 写地址u32addr位置为384-511 ,写在第三页0x1F600 to 0x1F7FF (大小512K);数据u32data为32位;
// 写地址u32addr位置为512-639 ,写在第四页0x1F800 to 0x1F9FF (大小512K);数据u32data为32位;
// 写地址u32addr位置为640-767 ,写在第五页0x1FA00 to 0x1FBFF (大小512K);数据u32data为32位;
// 写地址u32addr位置为768-895 ,写在第六页0x1FC00 to 0x1FDFF (大小512K) ;数据u32data为32位;
// 写地址u32addr位置为896-1023 ,写在第七页0x1FE00 to 0x1FFFF (大小512K) ;数据u32data为32位;
//函数返回值:无
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DATA_FLASH_Write(uint32_t u32addr,uint32_t u32data)
{
__set_PRIMASK(1);//关闭全局中断
UNLOCKREG();
DrvFMC_EnableISP(); //使能ISP功能
DrvFMC_Write ((0x1F000+u32addr*4), u32data); //写入数据
DrvFMC_DisableISP(); //禁止ISP功能
LOCKREG();
__set_PRIMASK(0);//打开全局中断
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//函数作用: 执行EEPROM的读操作
//函数介绍: 读取指定位置的数据
//函数使用:u32addr读取地址位置为0-127 ,第零页0x1F000 to 0x1F1FF (大小512K);数据为32位;
// u32addr读取地址位置为128-255 ,第一页0x1F200 to 0x1F3FF (大小512K);数据为32位;
// u32addr读取地址位置为256-383 ,第二页0x1F400 to 0x1F5FF (大小512K);数据为32位;
// u32addr读取地址位置为384-511 ,第三页0x1F600 to 0x1F7FF (大小512K);数据为32位;
// u32addr读取地址位置为512-639 ,第四页0x1F800 to 0x1F9FF (大小512K);数据为32位;
// u32addr读取地址位置为640-767 ,第五页0x1FA00 to 0x1FBFF (大小512K);数据为32位;
// u32addr读取地址位置为768-895 ,第六页0x1FC00 to 0x1FDFF (大小512K) ;数据为32位;
// u32addr读取地址位置为896-1023 ,第七页0x1FE00 to 0x1FFFF (大小512K) ;数据为32位;
//函数返回值: uint32_t 指定位置的数据
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t DATA_FLASH_Read(uint32_t u32addr)
{
uint32_t u32data;
__set_PRIMASK(1);//关闭全局中断
UNLOCKREG();
DrvFMC_EnableISP(); //使能ISP功能
DrvFMC_Read((0x1F000+u32addr*4), &u32data);
DrvFMC_DisableISP(); //禁止ISP功能
LOCKREG();
__set_PRIMASK(0);//打开全局中断
return u32data;
}
/* EEPROM演示程序 */
int main (void)
{
SYS_INIT();
delay_ms(500);
/*擦除0--7页数据,写之前必须擦除,不然数据会出错*/
DATA_FLASH_Erase(0);
DATA_FLASH_Erase(1);
DATA_FLASH_Erase(2);
DATA_FLASH_Erase(3);
DATA_FLASH_Erase(4);
DATA_FLASH_Erase(5);
DATA_FLASH_Erase(6);
DATA_FLASH_Erase(7);
/*向0--7页写数据,每页写头两个,32位*/
DATA_FLASH_Write(0,1);
DATA_FLASH_Write(1,2);
DATA_FLASH_Write(128,3);
DATA_FLASH_Write(129,4);
DATA_FLASH_Write(256,5);
DATA_FLASH_Write(257,6);
DATA_FLASH_Write(384,1);
DATA_FLASH_Write(385,2);
DATA_FLASH_Write(512,3);
DATA_FLASH_Write(513,4);
DATA_FLASH_Write(640,5);
DATA_FLASH_Write(641,6);
DATA_FLASH_Write(768,3);
DATA_FLASH_Write(769,4);
DATA_FLASH_Write(896,5);
DATA_FLASH_Write(897,6);
/*读到isp[]数组中*/
isp[0]=DATA_FLASH_Read(0);
isp[1]=DATA_FLASH_Read(1);
isp[2]=DATA_FLASH_Read(128);
isp[3]=DATA_FLASH_Read(129);
isp[4]=DATA_FLASH_Read(256);
isp[5]=DATA_FLASH_Read(257);
isp[6]=DATA_FLASH_Read(384);
isp[7]=DATA_FLASH_Read(385);
isp[8]=DATA_FLASH_Read(512);
isp[9]=DATA_FLASH_Read(513);
isp[10]=DATA_FLASH_Read(640);
isp[11]=DATA_FLASH_Read(641);
isp[12]=DATA_FLASH_Read(768);
isp[13]=DATA_FLASH_Read(769);
isp[14]=DATA_FLASH_Read(896);
isp[15]=DATA_FLASH_Read(897);
while(1);
}