2024-11-29 02:53 AM - last edited on 2024-11-29 02:59 AM by SofLit
Hi people,
I am using M24C04 EEPROM CHIP WHICH OPERATES (1.8 V -5.5 ).
I am checking my prototype,I found some issues.the initialization works ,I2C scan works fine but Iam unable to read or write .Can anyone help me to find what is wrong please
uint8_t writeData = 0xAB; // Data to write
uint8_t readData = 0; // Variable to store read data
uint32_t address = 0x50; // Target address
uint8_t write_data=0;
uint8_t read_data=0;
uint16_t i=1;
int chip_ok=0;
uint16_t check_ok=0;
// FUNCTION FOR CHECKING I2C SCAN ///
void I2C_Scan()
{
check_ok=0;
for(i = 0; i < 128; i++)
{ if (HAL_I2C_IsDeviceReady(&hi2c1, i << 1, 10, 100) == HAL_OK)
{
check_ok=1;
}
}
}
// MAIN FUNCTION //
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM3_Init();
MX_RTC_Init();
MX_TIM17_Init();
/* USER CODE BEGIN 2 */
if(EEPRMA2_M24_Init(EEPRMA2_M24C02_0) == BSP_ERROR_NONE)
{
chip_ok=1;
// DOUBLEFLASH();
}
else
{
chip_ok=0;
}
//do_defaults();
HAL_TIM_Base_Start_IT(&htim17);
HAL_Delay(100);
HAL_ADC_Stop(&hadc1);
DelayUS(100);
HAL_ADCEx_Calibration_Start(&hadc1);
DelayUS(1000);
HAL_ADC_Start(&hadc1);
HAL_Delay(100);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
I2C_Scan();
if (EEPRMA2_M24_WriteByte(EEPRMA2_M24C02_0, &writeData, address) == BSP_ERROR_NONE)
{
write_data=1;
if (EEPRMA2_M24_ReadByte(EEPRMA2_M24C02_0, &readData, address) == BSP_ERROR_NONE)
{
read_data=1;
} else
{
read_data=0;
}
}
else
{
write_data=0;
}
}
/* USER CODE END 3 */
}
// READ AND WRITE FUNCTION//
int ReadByte(int selectd)
{
uint8_t readdata = 0;
EEPRMA2_M24_ReadData(M24_INSTANCE,&readdata,selectd,1);
return readdata;
}
int ProgramByte(uint16_t location,uint8_t writedata)
{
EEPRMA2_M24_WriteData(M24_INSTANCE,&writedata,location,1);
}
// HEADER FILE DEFINITION USED //
#ifndef INC_EEPROM_H_
#define INC_EEPROM_H_
#define M24_INSTANCE EEPRMA2_M24C02_0
int ProgramByte(uint16_t location, uint8_t writedata);
#endif /* INC_EEPROM_H_ */
2024-11-29 03:27 AM - edited 2024-11-29 03:45 AM
Please help whats wrong
2024-12-01 11:17 AM
I am not familiar with EEPRMA2_M24_* functions. But I am with I2C EEPROMs.
If EEPROM is M24C04, why are you using EEPRMA2_M24C02_0?
Other problem could be the possible confusion between device address and internal EEPROM address. As EEPROM is M24C04 it have 512 bytes. So internal EEPROM address goes from 0x000 to 0x1FF. EEPROM device address is 0x50. But I think you need to use it shifted by 1. In this case 0x50 << 1. Also I think you need to pass device address to EEPRMA2_M24_* functions.
As EEPRMA2_M24_Init function already have EEPRMA2_M24C02_0 as argument, I think that in other EEPRMA2_M24_* functions, the first argument should be device address. In this case 0x50 << 1.