2021-07-15 11:14 PM
Hi,
I'm trying to write and read to/from M24512-R EEPROM.
Seems that everything is fine writing and reading because HAL return is HAL_OK, but allways I've reading 255 value.
My code:
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();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C2_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
MX_ADC1_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
const uint8_t num_data = 5;
uint8_t datos[num_data];
uint8_t datos_in[num_data];
uint8_t number = 100;
HAL_StatusTypeDef escritura = 4;
HAL_StatusTypeDef lectura = 4;
uint32_t error =0;
HAL_I2C_StateTypeDef state = HAL_I2C_STATE_READY;
/* USER CODE END 2 */
for (int i = 0; i<=num_data-1; i++)
{
datos[i] = 0;
}
for (int i = 0; i<=num_data-1; i++)
{
datos_in[i] = 0;
}
/* Infinite loop */
/* USER CODE BEGIN WHILE */
for (int i = 0; i<=num_data-1; i++)
{
datos[i] = number - i*2;
}
HAL_GPIO_WritePin(WP_EEPROM_GPIO_Port, WP_EEPROM_Pin, GPIO_PIN_RESET);
escritura = HAL_I2C_Mem_Write(&hi2c2, ADDMEMORY, ADDRDATA, 2, datos, num_data, 1000);
if (escritura !=HAL_OK)
{
state = HAL_I2C_GetState(&hi2c2);
error = HAL_I2C_GetError(&hi2c2);
}
HAL_GPIO_WritePin(WP_EEPROM_GPIO_Port, WP_EEPROM_Pin, GPIO_PIN_SET);
//HAL_I2C_Mem_Write(halI2C, ID esclavo, dir_memoria_inicial, tamaño palabra en bits, buffer a guardar, cantidad datos, tiempo espera);
uint8_t went_well = 0;
bool test_passed = false;
HAL_Delay(100);
lectura = HAL_I2C_Mem_Read(&hi2c2, ADDMEMORY, ADDRDATA, 2, datos_in, num_data, 1000);
for (int i=0; i<=num_data-1; i++)
{
if (datos[i] == datos_in[i])
{
went_well ++;
}
}
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/* I2C2 init function */
void MX_I2C2_Init(void)
{
hi2c2.Instance = I2C2;
hi2c2.Init.ClockSpeed = 100000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
Error_Handler();
}
}
And attached oscilloscope images:
WRITE COMMAND
READ COMMAND
I've checked all pins, VCC and E0-E2 are connected to GND.
Any idea of what I'm doing wrong?
Thanks in advantage.
Solved! Go to Solution.
2021-07-19 03:05 AM
Fixed it writting a HAL_Delay(10) after HAL_I2C_Mem_Write(). Seems WRITE pin wasn't in high level enough time to carry out write process.
2021-07-19 03:05 AM
Fixed it writting a HAL_Delay(10) after HAL_I2C_Mem_Write(). Seems WRITE pin wasn't in high level enough time to carry out write process.