Created
January 23, 2023 09:54
-
-
Save M0nteCarl0/1cd4cf00412ea297670a8167112885fa to your computer and use it in GitHub Desktop.
Driver for RAMTRON FM2504 F-RAM Memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "FM25.h" | |
/****************************************************************************** | |
* @file FM25.c | |
* @author Molotaliev A.O([email protected]) | |
* @version V 0.0.1 | |
* @date 1-october-2016 | |
* @brief This Driver for RAMTRON FM2504 F-RAM Memory | |
* | |
* | |
****************************************************************************/ | |
void FM25_Init (void) | |
{ | |
FM25_Gpio_Init(); | |
SPI_InitTypeDef SPI_InitStructure; | |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); | |
SPI_I2S_DeInit(SPI2); | |
SPI_StructInit(&SPI_InitStructure); | |
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_Low; | |
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; | |
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; | |
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; | |
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; | |
SPI_InitStructure.SPI_CRCPolynomial = 7; | |
SPI_Init(SPI2, &SPI_InitStructure); | |
SPI_Cmd(SPI2, ENABLE); | |
SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternalSoft_Set); | |
} | |
/********************************************************************************/ | |
void FM25_ChipSelect(void) | |
{ | |
GPIO_WriteBit(GPIOC,GPIO_Pin_3,Bit_RESET); | |
} | |
/********************************************************************************/ | |
void FM25_ChipDeselect(void) | |
{ | |
GPIO_WriteBit(GPIOC,GPIO_Pin_3,Bit_SET); | |
} | |
/********************************************************************************/ | |
void FM25_Gpio_Init(void) | |
{ | |
GPIO_InitTypeDef GPIO_InitStructure; | |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); | |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; | |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
/* SPI SCK pin configuration */ | |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;//SPIx_SCK_PIN; | |
GPIO_Init(GPIOB, &GPIO_InitStructure); | |
/* SPI MISO pin configuration */ | |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//SPIx_MISO_PIN; | |
GPIO_Init(GPIOB, &GPIO_InitStructure); | |
/* SPI MOSI pin configuration */ | |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;//SPIx_MOSI_PIN; | |
GPIO_Init(GPIOB, &GPIO_InitStructure); | |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; | |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; | |
GPIO_Init(GPIOC, &GPIO_InitStructure); | |
} | |
/********************************************************************************/ | |
void FM25_WriteData(uint16_t Addres, const uint8_t* Data,uint16_t DataCount) | |
{ | |
FM25_SetWriteEnableLatch(); | |
for(uint16_t i = 0; i< DataCount;i++) | |
{ | |
FM25_WriteDataByte(Addres + DataCount , Data[i]); | |
} | |
FM25_ResetWriteEnableLatch(); | |
} | |
/********************************************************************************/ | |
void FM25_ReadData(uint16_t Addres, uint8_t* Data,uint16_t DataCount) | |
{ | |
for(uint16_t i = 0; i< DataCount;i++) | |
{ | |
Data[i] = FM25_ReadDataByte(Addres + DataCount); | |
} | |
} | |
/********************************************************************************/ | |
inline uint8_t FM25_WriteByteReadByte(uint8_t Data) | |
{ | |
FM25_WriteRawByte(Data); | |
return FM25_ReadRawByte(); | |
} | |
/********************************************************************************/ | |
inline void FM25_WriteRawByte(uint16_t Data) | |
{ | |
do{}while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)!=SET); | |
SPI_I2S_SendData(SPI2,Data); | |
} | |
/********************************************************************************/ | |
inline uint8_t FM25_ReadRawByte(void) | |
{ | |
do{}while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)!=SET); | |
uint8_t DataOut = SPI_I2S_ReceiveData(SPI2); | |
return DataOut; | |
} | |
/********************************************************************************/ | |
void FM25_SetWriteEnableLatch(void) | |
{ | |
FM25_ChipSelect(); | |
FM25_WriteOpcode(FM25_Opcode_WREN); | |
FM25_ChipDeselect(); | |
} | |
/********************************************************************************/ | |
void FM25_ResetWriteEnableLatch(void) | |
{ | |
FM25_ChipSelect(); | |
FM25_WriteOpcode(FM25_Opcode_WRDI); | |
FM25_ChipDeselect(); | |
} | |
/********************************************************************************/ | |
void FM25_WriteOpcode(FM25_Opcode Opcode) | |
{ | |
FM25_WriteByteReadByte(Opcode); | |
} | |
/********************************************************************************/ | |
void FM25_WriteStatusRegister(FM25_StatusRegister Register) | |
{ | |
FM25_ChipSelect(); | |
FM25_WriteOpcode(FM25_Opcode_WRSR); | |
FM25_WriteByteReadByte(Register.RegisterRaw); | |
FM25_ChipDeselect(); | |
} | |
/********************************************************************************/ | |
FM25_StatusRegister FM25_ReadStatusRegister(void) | |
{ | |
FM25_StatusRegister Register; | |
FM25_ChipSelect(); | |
FM25_WriteOpcode(FM25_Opcode_RDSR); | |
Register.RegisterRaw = FM25_WriteByteReadByte(0); | |
FM25_ChipDeselect(); | |
return Register; | |
} | |
/********************************************************************************/ | |
void FM25_WriteDataByte(uint16_t DataAdress,uint8_t Data) | |
{ | |
FM25_ChipSelect(); | |
FM25_WriteOpcode(FM25_Opcode_WRITE); | |
FM25_WriteDataAddres(DataAdress); | |
FM25_WriteByteReadByte(Data); | |
FM25_ChipDeselect(); | |
} | |
/********************************************************************************/ | |
uint8_t FM25_ReadDataByte(uint16_t DataAdress) | |
{ | |
uint8_t Data; | |
FM25_ChipSelect(); | |
FM25_WriteOpcode(FM25_Opcode_READ); | |
FM25_WriteDataAddres(DataAdress); | |
Data = FM25_WriteByteReadByte(0); | |
FM25_ChipDeselect(); | |
return Data; | |
}; | |
/********************************************************************************/ | |
uint16_t FM25_ConvertAdressto13Bit(uint16_t DataAdress) | |
{ | |
return DataAdress & 0x1FFF; | |
} | |
/********************************************************************************/ | |
void FM25_GetLSBandMSBfrom13BitAdress(uint16_t DataAdress,uint8_t& MSB,uint8_t& LSB) | |
{ | |
LSB = DataAdress & 0xFF; | |
MSB = (DataAdress >> 8) & 0x3F; | |
} | |
/********************************************************************************/ | |
void FM25_WriteDataAddres(uint16_t DataAdress) | |
{ | |
uint8_t MSB = 0; | |
uint8_t LSB = 0; | |
uint16_t SourceDataAdress = FM25_ConvertAdressto13Bit(DataAdress); | |
FM25_GetLSBandMSBfrom13BitAdress(SourceDataAdress,MSB,LSB); | |
FM25_WriteByteReadByte(MSB); | |
FM25_WriteByteReadByte(LSB); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stm32f10x.h" | |
#include "stm32f10x_spi.h" | |
#include "stm32f10x_gpio.h" | |
#ifndef _FM25_MEMORY_ | |
#define _FM25_MEMORY_ | |
/****************************************************************************** | |
* @file FM25.h | |
* @author Molotaliev A.O([email protected]) | |
* @version V 0.0.1 | |
* @date 1-october-2016 | |
* @brief This Driver for RAMTRON FM2504 F-RAM Memory | |
* | |
* | |
****************************************************************************/ | |
typedef enum FM25_Opcode | |
{ | |
FM25_Opcode_WREN = 0x6, | |
FM25_Opcode_WRDI = 0x4, | |
FM25_Opcode_RDSR = 0x5, | |
FM25_Opcode_WRSR = 0x1, | |
FM25_Opcode_READ = 0x3, | |
FM25_Opcode_WRITE = 0x2, | |
}FM25_Opcode; | |
typedef union FM25_StatusRegister | |
{ | |
typedef struct FM25_StatusRegisterS | |
{ | |
uint8_t RESERVED0:1; | |
uint8_t WEL:1; | |
uint8_t BP0:1; | |
uint8_t BP1:1; | |
uint8_t RESERVED4:1; | |
uint8_t RESERVED5:1; | |
uint8_t RESERVED6:1; | |
uint8_t WPEN:1; | |
}FM25_StatusRegisterS; | |
uint8_t RegisterRaw; | |
}FM25_StatusRegister; | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
void FM25_Init(void); | |
void FM25_Gpio_Init(void); | |
void FM25_ChipSelect(void); | |
void FM25_ChipDeselect(void); | |
uint16_t FM25_ConvertAdressto13Bit(uint16_t DataAdress); | |
void FM25_GetLSBandMSBfrom13BitAdress(uint16_t DataAdress,uint8_t& MSB,uint8_t& LSB); | |
void FM25_WriteRawByte(uint16_t Data); | |
void FM25_WriteDataAddres(uint16_t DataAdress); | |
void FM25_WriteDataByte(uint16_t DataAdress,uint8_t Data); | |
void FM25_WriteData(uint16_t Addres, const uint8_t* Data,uint16_t DataCount); | |
uint8_t FM25_ReadRawByte(void); | |
uint8_t FM25_ReadDataByte(uint16_t DataAdress); | |
void FM25_ReadData(uint16_t Addres, uint8_t* Data,uint16_t DataCount); | |
uint8_t FM25_WriteByteReadByte(uint8_t Data); | |
void FM25_SetWriteEnableLatch(void); | |
void FM25_ResetWriteEnableLatch(void); | |
void FM25_WriteStatusRegister(FM25_StatusRegister Register); | |
FM25_StatusRegister FM25_ReadStatusRegister(void); | |
void FM25_WriteOpcode(FM25_Opcode Opcode); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment