基于新唐NUC123的FM25640C的驱动代码  

 
 

个产品用到铁电存储,要用到5V环境,我还是选择用了FM25640C了。下面是基于新唐NUC123SeriesBSP_CMSIS_V1.01.002的测试代码,希望能抛砖引玉。谢谢。
/**************************************************************************//**
* @file fm25640c.c
* @version V1.00
* $author: yuanxihua@21cn.com
* $Date:  2013/01/03 11:33a $
* @brief  NUC123 Series FM25640C Driver Sample Code
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "NUC123Series.h"
/*
_________________________________________________
| NUC123SD4AN0     FM25640C |
|            |
| PC8  SS10  ---->  CS   |
| PC9  SPICLK1  ---->  SCK   |
| PC10 MISO10  <----  SO   |
| PC11 MOSI10  ---->  SI   |
|_______________________________________________|
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
//#define  FM25640C_CS   PC8
#define  FM25640C_SIZE  8*1024
//FM25640C命令
#define   FM25640C_WREN  0x06
#define   FM25640C_WRDI  0x04
#define   FM25640C_RDSR  0x05ff
#define   FM25640C_WRSR  0x0100
#define   FM25640C_READ  0x030000FF
#define   FM25640C_WRITE  0x02000000
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void FM25640C_Init(void);
void FM25640C_Write_Byte(uint32_t addr,uint8_t wrdata);
void FM25640C_Write_Status_Register(uint8_t wrdata);
uint8_t FM25640C_Read_Byte(uint32_t addr);
uint8_t FM25640C_Read_Status_Register(void);
void SPI1_Send_Data(uint32_t wrdata,uint8_t bits);
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void FM25640C_Test(void)
{
uint32_t i;
/* Configure the FM25640C */
FM25640C_Init();
while(0)
{
//  SPI1_Send_Data(0x55, 8);
//  SPI_SET_SS0(SPI1); SPI1_Send_Data(0x55, 8);SPI_CLR_SS0(SPI1); //写0x55测试SPI接口
//  FM25640C_Write_Byte(0xCC, 0x55);
//  FM25640C_Read_Byte (0xCC);
//  FM25640C_Read_Status_Register();
}
FM25640C_Write_Status_Register(0x00);
printf("FM25640C_Read_Status_Register() = 0x%02x \n\r",FM25640C_Read_Status_Register());
FM25640C_Write_Byte(0xAA, 0x55);
FM25640C_Write_Byte(0x55, 0xAA);
printf("FM25640C_Read_Byte(0xAA) = 0x%02x \n\r",FM25640C_Read_Byte(0xAA));
printf("FM25640C_Read_Byte(0x55) = 0x%02x \n\r",FM25640C_Read_Byte(0x55));
while(1)
{
  for(i=0;i<FM25640C_SIZE;i++)
  {FM25640C_Write_Byte(i, i&0xff);}
  SYS_SysTickDelay(300000);
  for(i=0;i<FM25640C_SIZE;i++)
  {
   //for display
   if((i%16)== 0){printf("\n\r%08xh:",i);}
   printf(" %02x",FM25640C_Read_Byte(i));
   if((i%16)==15){printf(" ;");}
   //display  end
  }
  SYS_SysTickDelay(300000);
}
}
// SPI1_Send_Data(FM25640C_WREN,8);
// SPI1_Send_Data(FM25640C_WRDI,8);
uint8_t FM25640C_Read_Byte(uint32_t addr)
{
uint32_t rddata,tmpdata=FM25640C_READ;
tmpdata |= (addr << 8);  
SPI1_Send_Data(tmpdata,32);
rddata = SPI_GET_RX0_DATA(SPI1);
return (uint8_t)rddata;
}
void FM25640C_Write_Byte(uint32_t addr, uint8_t wrdata)
{
uint32_t tmpdata=FM25640C_WRITE;
SPI1_Send_Data(FM25640C_WREN,8);    //中:使能写操作//Write Enable
tmpdata |= (addr << 8) + wrdata;
SPI1_Send_Data(tmpdata,32);
}
uint8_t FM25640C_Read_Status_Register(void)
{
uint32_t rddata;
SPI1_Send_Data(FM25640C_RDSR,16);
rddata = SPI_GET_RX0_DATA(SPI1);
return (uint8_t)rddata;
}
void FM25640C_Write_Status_Register(uint8_t wrdata)
{
uint16_t dat=FM25640C_WRSR | wrdata;
SPI1_Send_Data(FM25640C_WREN,8);    //中:使能写操作//Write Enable
SPI1_Send_Data(dat,16);
}
void SPI1_Send_Data(uint32_t wrdata, uint8_t bits)
{
SPI1->CNTRL = (SPI1->CNTRL&0xFFFFFF07) | ((bits&0x1f) <<3);
SPI_WRITE_TX0(SPI1, wrdata);     //中:待发数据//en:data waiting for sending
SPI_SET_GO(SPI1);        //中:启动发送//en: Start Transfer
while(SPI_IS_BUSY(SPI1) !=0);     //中:等待发送结束//en:waiting for sending finished
}
void SPI1_Init(void)
{
/* Enable IP clock */         
    SET_SPI1_CLKSRC(CLK_SPI1_HCLK);        //En:Select SPI1 clock source
    GPIO_SPI1();                                //中:配置PC8~11为SPI功能//Set PC11~PC8 as SPI1 pin. PC8=SPISS10,PC9=SPICLK1,PC10=MISO10,PC11=MOSI10
DrvSPI_Init(SPI1, SPI_MASTER, SPI_TYPE0, 32);
  SPI_SET_DIVIDER(SPI1, 1);                       //中:设置SPI的分频值//en:Set divider of SPI_CLK  SPIclk = Pclk/((DIVIDER+1)*2)
    SPI_SET_MSB_FIRST(SPI1);        //En:MSB first
SPI_SET_SS(SPI1, SPI_SS_LOW_TRIGGER, SPI_AUTO_SS);//中:自动片选选择//en:Slave Select auto
SPI_SET_SS0(SPI1);        //中:置片选有效//En:active SS1
}
void FM25640C_Init(void)
{
SPI1_Init();
}

//程序完毕。