AnsweredAssumed Answered

FM24C256 FRAM programming (STM32F0xx-DISCOVERY Board)

Question asked by Suby on Jun 3, 2015
Latest reply on Jun 6, 2015 by Edje
Hello dear members of forum,

I would like to use a FM24C256 ram of RAMTRON ( Datasheet ). I have tryed to write program for it but It do not work. When I write it the program to stop at the  red line. Please help me. I am waiting for your advise.

My program is:

#define FM_SCK      GPIO_Pin_10
#define FM_SDA      GPIO_Pin_11
 
#define FM_SCKPS    GPIO_PinSource10
#define FM_SDAPS    GPIO_PinSource11
#define FM_GAF      GPIO_AF_1
#define FM_GPIO     GPIOB
 
#define FM_I2C      I2C2
#define DS_ADDR     (0x68<<1)
 
#define I2C_CLK 1000000 
 
#define FRAM_ADDR_WR 0xA0
#define FRAM_ADDR_RD 0xA1
#define FRAM_SIZE   32768
#define I2C_WR_ACK 0         // ACK state when writing
#define I2C_RD_NAK 0         // CCS value to NAK on read
#define I2C_RD_ACK 1         // CCS value to ACK on read
 
 
 
extern void fram_init(void);
extern uint8_t I2C_FMWrReg(uint16_t Reg, uint8_t data);
uint8_t I2C_FMRdReg(uint16_t start_addr);
 
 
 
void fram_init(void)
{
     
    GPIO_InitTypeDef G;
    I2C_InitTypeDef I;
     
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
 
    G.GPIO_Pin = FM_SDA | FM_SCK;
    G.GPIO_OType = GPIO_OType_OD;
    G.GPIO_PuPd = GPIO_PuPd_UP;
    G.GPIO_Speed = GPIO_Speed_Level_1;
    G.GPIO_Mode = GPIO_Mode_AF;
    GPIO_Init(FM_GPIO, &G);
 
    GPIO_PinAFConfig(FM_GPIO, FM_SDAPS, FM_GAF);
    GPIO_PinAFConfig(FM_GPIO, FM_SCKPS, FM_GAF);
 
    I.I2C_Ack = I2C_Ack_Enable;
    I.I2C_AcknowledgedAddress = 0x00;
    I.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
    I.I2C_DigitalFilter = 0x00;
    I.I2C_Mode = I2C_Mode_I2C;
    I.I2C_OwnAddress1 = 0x01;
    I.I2C_Timing = 0x10805F87;
    I2C_Init(FM_I2C, &I);
    I2C_Cmd(FM_I2C, ENABLE);
}
 
 
uint8_t I2C_FMWrReg(uint16_t start_addr, uint8_t data){
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_BUSY) == SET);                     // to controll busy
 
    I2C_TransferHandling(FM_I2C, FRAM_ADDR_WR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);   // to send start bit and wait for setting
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TXIS) == RESET);
 
    I2C_SendData(FM_I2C, start_addr>>8);                                                            // to send address HIGH
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TCR) == RESET);
 
    I2C_TransferHandling(FM_I2C, DS_ADDR, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);           // to send acknowledment bit
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TXIS) == RESET);
 
 
 
    I2C_SendData(FM_I2C, start_addr);                                                               // to send address LOW
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TCR) == RESET);
 
    I2C_TransferHandling(FM_I2C, DS_ADDR, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);           // to send acknowledment bit
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TXIS) == RESET);  
 
    I2C_SendData(FM_I2C, data);                                 // to send data
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_STOPF) == RESET); 
    I2C_ClearFlag(FM_I2C, I2C_FLAG_STOPF);                                                  // to send end bit
}
 
 
uint8_t I2C_FMRdReg(uint16_t start_addr){
    uint8_t data;
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_BUSY) == SET);
 
    I2C_TransferHandling(FM_I2C, FRAM_ADDR_WR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TXIS) == RESET);
 
    I2C_SendData(FM_I2C, start_addr>>8);
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TC) == RESET);
 
    I2C_TransferHandling(FM_I2C, FRAM_ADDR_WR, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_RXNE) == RESET);
 
    I2C_SendData(FM_I2C, start_addr);
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_TC) == RESET);
 
    I2C_TransferHandling(FM_I2C, FRAM_ADDR_WR, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_RXNE) == RESET);
 
    data = I2C_ReceiveData(FM_I2C);
 
    while(I2C_GetFlagStatus(FM_I2C, I2C_FLAG_STOPF) == RESET);
    I2C_ClearFlag(FM_I2C, I2C_FLAG_STOPF);
 
    return data;
}

Outcomes