/******************************************************************************
Copyright (C), 2014-2015, 西安XX技研电子科技有限公司
File name: fm25640.c
Author:
Version:
Date:
Platform: Stm32F103VE
Tools: keilMDK
OS Version: UCOS-II v2.86
Description: // 用于详细说明此程序文件完成的主要功能,与其他模块
// 或函数的接口,输出值、取值范围、含义及参数间的控
// 制、顺序、独立或依赖等关系
Others: // 其它内容的说明
Function List: // 主要函数列表,每条记录应包括函数名及功能简要说明
1. ....
History: // 修改历史记录列表,每条修改记录应包括修改日期、修改
// 者及修改内容简述
1. Date:
Author:
Modification:
2. ...
******************************************************************************/
#include <stdio.h>
#include "fm25640.h"
void FM25640_CS_OUT(char status)
{
if(status)
{
GPIOA->BSRR = GPIO_Pin_15;
}
else
{
GPIOA->BRR = GPIO_Pin_15;
}
}
void SPI_FM25640_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
// config SPI 1 port
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
FM25640_CS_OUT(1);
}
static uint8 spi1_write(unsigned char d)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
//NotSelect_Flash(); while(1);
/* Send byte through the SPI2 peripheral */
SPI_I2S_SendData(SPI1, d);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1);
}
static uint8 spi1_read(void)
{
SPI_I2S_SendData(SPI1, 0);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1);
}
/*********************************************************************************
;* Name: WREN_CMD
;* Description: Set write enable latch
;* Function: This routine sends the command to enable writes to the FRAM memory array or
;* status register
;* Calls: spi1_write
;* Input: None
;* Outputs: None
;*********************************************************************************/
void fm25640_wren_cmd(void)
{
FM25640_CS_OUT(0);
spi1_write(WREN_INST); /* Send WREN instruction */
FM25640_CS_OUT(1);
}
/*********************************************************************************
;* Name: WRDI_CMD
;* Description: Set write disable latch
;* Function: This routine sends the command to disable writes to the FRAM memory array or
;* status register
;* Calls: spi1_write
;* Input: None
;* Outputs: None
;*********************************************************************************/
void fm25640_wrdi_cmd(void)
{
FM25640_CS_OUT(0);
spi1_write(WRDI_INST); /* Send WRDI instruction */
FM25640_CS_OUT(1);
}
/**********************************************************************************
;* Name: RDSR_CMD
;* Description: Read Status Register
;* Function: This routine sends the command to read the status register
;* Calls: spi1_write, spi1_read
;* Input: None
;* Outputs: aa= status register
;************************************************************************************/
uint8 fm25640_rdsr_cmd(void)
{
uint8 status;
FM25640_CS_OUT(0);
spi1_write(RDSR_INST);
status = spi1_read();
FM25640_CS_OUT(1);
return status;
}
/*********************************************************************************
;* Name: WRSR_CMD
;* Description: Write Status Register
;* Function: This routine sends the command to write the BP0 and BP0 FRAM
;* bits in the status register
;* Calls: spi1_write
;* Input: None
;* Outputs: None
;********************************************************************************/
void fm25640_wrsr_cmd(uint8 status)
{
fm25640_wren_cmd();
FM25640_CS_OUT(0);
spi1_write(WRSR_INST); /* Send WRSR instruction */
spi1_write(status); /* Send status register */
FM25640_CS_OUT(1);
}
/*****************************************************************************
;* Name: BYTE_WRITE
;* Description: Single Byte Write
;* Function: This routine sends the command to write a single byte to the FRAM memory array
;* Calls: spi1_write
;* Input: aa = The data want to be writen, dd = The address of the menory want to be writen
;* Outputs: None
;*******************************************************************************/
void fm25640_byte_write(uint16 address, uint8 data)
{
fm25640_wren_cmd();
FM25640_CS_OUT(0);
spi1_write(WRITE_INST); /* Send WRITE instruction including MSB of address */
spi1_write((uint8)(address >> 8));
spi1_write((uint8)(address));
spi1_write(data);
FM25640_CS_OUT(1);
}
/*****************************************************************************
;* Name: BYTE_SEQ_WRITE
;* Description: Subsequent Byte Write
;* Function: This routine sends the command to write Subsequent byte to the FRAM memory array
;* Calls: spi1_write
;* Input: PTR is the data pointer want to be writen,dd = The address of the menory want to be writen
NUM is the byte number want to be writen to the FRAM menory
;* Outputs: None
;*******************************************************************************/
void fm25640_write_seq(uint16 address, uint8 *ptr, uint8 num)
{
uint8 i;
fm25640_wren_cmd();
FM25640_CS_OUT(0);
spi1_write(WRITE_INST); /* Send WRITE instruction including MSB of address */
spi1_write((uint8)(address >> 8));
spi1_write((uint8)(address));
for(i = 0; i < num; i++)
{
spi1_write(*(ptr + i));
}
FM25640_CS_OUT(1);
}
/******************************************************************************
;* Name: BYTE_READ
;* Description: Single Byte Read
;* Function: This routine sends the command to read a single byte from the FRAM memory array
;* Calls: spi1_write, spi1_read
;* Input: dd = The address of the menory want to be readed
;* Outputs: temp = read byte
;******************************************************************************/
uint8 fm25640_byte_read(uint16 address)
{
uint8 temp;
FM25640_CS_OUT(0);
spi1_write(READ_INST); /* Send READ_INST instruction including MSB of address */
spi1_write((uint8)(address >> 8));
spi1_write((uint8)(address));
temp = spi1_read();
FM25640_CS_OUT(1);
return temp;
}
/******************************************************************************
;* Name: BYTE_SEQ_READ
;* Description: Subsequent Byte Read
;* Function: This routine sends the command to read Subsequent byte from the FRAM memory array
;* Calls: spi1_write, spi1_read
;* Input: PTR is the first address of the saved buffer
dd=The address of the menory want to be readed
NUM is the byte numb