您尚未登录。

楼主 #1 2020-05-09 18:31:06

缥缈九哥
会员
注册时间: 2020-05-09
已发帖子: 93
积分: 96

基于新唐NUC123的3.0的BSP包的FM25CL64BG驱动开发--缥缈九哥

基于新唐NUC123的3.0的BSP包的FM25CL64BG驱动开发

源码如下:

/**************************************************************************//**
 * @file     fm25640c.c
 * @version  V3.00
 * $Revision: 5 $
 * $Date: 16/08/04 11:18a $
 * @brief    Demonstrate how to perform timer counting in periodic mode.
 * @note
 * Copyright (C) 2014~2016 PM9GZY Technology Corp. yuanxihua@21cn.com All rights reserved.
 ******************************************************************************/
 
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "NUC123.h"
/*
_________________________________________________
| NUC123ZD4AN0     FM25CL64BG |
|            |
| PC8  SS10  ---->  CS   |
| PC9  SPICLK1  ---->  SCK   |
| PC10 MISO10  <----  SO   |
| PC11 MOSI10  ---->  SI   |
|_______________________________________________|
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
//#define  FM25CL64BG_CS   PC8
#define  FM25CL64BG_SIZE  8*1024
//FM25CL64BG命令
#define   FM25CL64BG_WREN  0x06
#define   FM25CL64BG_WRDI  0x04
#define   FM25CL64BG_RDSR  0x05ff
#define   FM25CL64BG_WRSR  0x0100
#define   FM25CL64BG_READ  0x030000FF
#define   FM25CL64BG_WRITE 0x02000000
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void FM25CL64BG_Init(void);
void FM25CL64BG_Read (uint32_t addr, uint8_t *buf, uint32_t len);
void FM25CL64BG_Write(uint32_t addr, uint8_t *buf, uint32_t len);
void FM25CL64BG_Write_Byte(uint32_t addr,uint8_t wrdata);
void FM25CL64BG_Write_Status_Register(uint8_t wrdata);
uint8_t FM25CL64BG_Read_Byte(uint32_t addr);
uint8_t FM25CL64BG_Read_Status_Register(void);
void SPI1_Send_Data(uint32_t wrdata,uint8_t bits);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void FM25CL64BG_Test(void)
{
 uint32_t i;
 
 /* Configure the FM25CL64BG */
 FM25CL64BG_Init();

 while(0)
 { 
//  SPI1_Send_Data(0x55, 8);
//  SPI_SET_SS0(SPI1); SPI1_Send_Data(0x55, 8);SPI_CLR_SS0(SPI1); //写0x55测试SPI接口
//  FM25CL64BG_Write_Byte(0xCC, 0x55);
//  FM25CL64BG_Read_Byte (0xCC); 
//  FM25CL64BG_Read_Status_Register();
 }
 
 FM25CL64BG_Write_Status_Register(0x00);
 printf("FM25CL64BG_Read_Status_Register() = 0x%02x \n\r",FM25CL64BG_Read_Status_Register());
 FM25CL64BG_Write_Byte(0xAA, 0x55);
 FM25CL64BG_Write_Byte(0x55, 0xAA);
 printf("FM25CL64BG_Read_Byte(0xAA) = 0x%02x \n\r",FM25CL64BG_Read_Byte(0xAA));
 printf("FM25CL64BG_Read_Byte(0x55) = 0x%02x \n\r",FM25CL64BG_Read_Byte(0x55));

 while(1)
 {
  for(i=0;i<FM25CL64BG_SIZE;i++)
  {FM25CL64BG_Write_Byte(i, i&0xff);}
  CLK_SysTickDelay(300000);
  for(i=0;i<FM25CL64BG_SIZE;i++)
  {
   //for display
   if((i%16)== 0){printf("\n\r%08xh:",i);}
   printf(" %02x",FM25CL64BG_Read_Byte(i));
   if((i%16)==15){printf(" ;");}
   //display  end
  }
  CLK_SysTickDelay(300000); 
 }
}
// SPI1_Send_Data(FM25640C_WREN,8);
// SPI1_Send_Data(FM25640C_WRDI,8);

void FM25CL64BG_Read(uint32_t addr, uint8_t *buf, uint32_t len)
{
 uint32_t i;
 for(i=0;i<len;i++)
 {buf[i] = FM25CL64BG_Read_Byte(addr+i);}
}
void FM25CL64BG_Write(uint32_t addr, uint8_t *buf, uint32_t len)
{
 uint32_t i;
 for(i=0;i<len;i++)
 {FM25CL64BG_Write_Byte(addr+i,buf[i]);}
}
uint8_t FM25CL64BG_Read_Byte(uint32_t addr)
{
 uint32_t rddata,tmpdata=FM25CL64BG_READ;

 tmpdata |= (addr << 8); 
 SPI1_Send_Data(tmpdata,32);
 rddata = SPI_READ_RX0(SPI1);
 return (uint8_t)rddata;
}
void FM25CL64BG_Write_Byte(uint32_t addr, uint8_t wrdata)
{
 uint32_t tmpdata=FM25CL64BG_WRITE;
 
 SPI1_Send_Data(FM25CL64BG_WREN,8);    //中:使能写操作//Write Enable
 tmpdata |= (addr << 8) + wrdata;
 SPI1_Send_Data(tmpdata,32);
}
uint8_t FM25CL64BG_Read_Status_Register(void)
{
 uint32_t rddata;
 
 SPI1_Send_Data(FM25CL64BG_RDSR,16);
 rddata = SPI_READ_RX0(SPI1);
 return (uint8_t)rddata;
}
void FM25CL64BG_Write_Status_Register(uint8_t wrdata)
{
 uint16_t dat=FM25CL64BG_WRSR | wrdata;
 
 SPI1_Send_Data(FM25CL64BG_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) << SPI_CNTRL_TX_BIT_LEN_Pos);
 
 /* Write to TX register */
 SPI_WRITE_TX0(SPI1, wrdata);
 /* Trigger SPI1 data transfer */
 SPI_TRIGGER(SPI1);
 /* Check SPI1 busy status */
 while(SPI_IS_BUSY(SPI1));
}
void SPI1_Init(void)
{
 /* Select HCLK as the clock source of SPI1 */
    CLK_SetModuleClock(SPI1_MODULE, CLK_CLKSEL1_SPI1_S_HCLK, MODULE_NoMsk);

    /* Enable SPI1 peripheral clock */
    CLK_EnableModuleClock(SPI1_MODULE);

    /* Setup SPI1 multi-function pins */
 SYS->GPC_MFP &= ~(SYS_GPC_MFP_PC8_Msk | SYS_GPC_MFP_PC9_Msk | SYS_GPC_MFP_PC10_Msk | SYS_GPC_MFP_PC11_Msk);
 SYS->GPC_MFP |= SYS_GPC_MFP_PC8_SPI1_SS0 | SYS_GPC_MFP_PC9_SPI1_CLK | SYS_GPC_MFP_PC10_SPI1_MISO0 | SYS_GPC_MFP_PC11_SPI1_MOSI0;
 
 SYS->ALT_MFP1 &= ~(SYS_ALT_MFP1_PC8_Msk | SYS_ALT_MFP1_PC9_Msk | SYS_ALT_MFP1_PC10_Msk | SYS_ALT_MFP1_PC11_Msk);
 SYS->ALT_MFP1 |= SYS_ALT_MFP1_PC8_SPI1_SS0 | SYS_ALT_MFP1_PC9_SPI1_CLK | SYS_ALT_MFP1_PC10_SPI1_MISO0 | SYS_ALT_MFP1_PC11_SPI1_MOSI0;

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init SPI                                                                                                */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */
    /* Set IP clock divider. SPI clock rate = 36MHz */
    SPI_Open(SPI1, SPI_MASTER, SPI_MODE_0, 32, 36000000);

    /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
    SPI_EnableAutoSS(SPI1, SPI_SS0, SPI_SS_ACTIVE_LOW);
}

void FM25CL64BG_Init(void)
{
 SPI1_Init();
}

//源码完毕

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn