Writing FRAM driver STM32

129 Views Asked by At

I am trying to learn how to write a driver using the datasheet for a component. Using MB85RC16 FRAM in this case. Datasheet = https://www.fujitsu.com/ca/en/Images/MB85RC16-DS501-00001-8v0-E.pdf - Page 9

I am able to write any address I want using HAL_I2C_Master_Transmit but can't get the reading back part to work without using HAL_I2C_Mem_Read.

The first four bits are fixed to 1010 and then the three bits are the device address configuration (000 in my case) resulting in 0x50 as the complete device address. This is then followed by the last eighth bit which is R/W.

Using HAL I write 0xCE to address 0x05.

uint8_t buff[10] = {0x05, 0xCE};
HAL_I2C_Master_Transmit(&hi2c1, (0x50 << 1), buff, 2, HAL_MAX_DELAY);

Then to read back I use the datasheet and first send 0x05 as the address to read from and try to use HAL_I2C_Master_Receive to complete the second part where I was hoping to read data back.

uint8_t buff2[1];
HAL_I2C_Master_Transmit(&hi2c1, (0x50 << 1), 0x05, 1, HAL_MAX_DELAY);
stat = HAL_I2C_Master_Receive(&hi2c1, (0x50 << 1) | 0x01, buff2, 1, 1000);

stat shows HAL_OK but I dont get 0xCE back in buff2. It just reads 0.

Could you please guide me what I am doing wrong because when I use HAL_I2C_Mem_Read like below it works fine.

HAL_I2C_Mem_Read(&hi2c1, (0x50 << 1) | 0x01, 0x05, 1, buff2, 1, 1000);

I tried using an oscilloscope I just havnt understood the differences between the Master_recieve signal and Mem_read signal yet.

Image: Read from a specific memory address

1

There are 1 best solutions below

0
wek On

Which STM32?

HAL_I2C_Master_Transmit(&hi2c1, (0x50 << 1), 0x05, 1, HAL_MAX_DELAY);

Third parameter is supposed to be pointer to data buffer, not a number. See description of this function in Cube's manual, or directly in the Cube/HAL sources:

/**
  * @brief  Transmits in master mode an amount of data in blocking mode.
  * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
  *                the configuration information for the specified I2C.
  * @param  DevAddress Target device address: The device 7 bits address value
  *         in datasheet must be shifted to the left before calling the interface
  * @param  pData Pointer to data buffer
  * @param  Size Amount of data to be sent
  * @param  Timeout Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)

Similarly, read description of HAL_I2C_Mem_Read() in the same sources and compare that to above description of HAL_I2C_Master_Transmit() to understand the difference.