cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072RB discovery board The STM32 in slave mode is to be used to read sensor data at every 2 second interval by an ESP8266 master through I2C. I am using the HAL library for this.

SD.7
Associate

The I2C interrupts were enabled and HAL_I2C_Slave_Receive_IT() and HAL_I2C_Slave_Transmit_IT() functions were called. The slave can receive and transmit the data only once and then it pulls the SCL line low disabling further communications. I have tried to software reset by clearing the PE bit inside the callbacks and also tried to flush the TXDR by setting TXE among other things but nothing worked.

The slave code is below:

#include "main.h"

I2C_HandleTypeDef hi2c2;

volatile uint8_t data_receive;

volatile uint8_t data_transmit = 0x12;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C2_Init(void);

void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c2)

{

HAL_GPIO_TogglePin(LED_3_GPIO_Port, LED_3_Pin);

}

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c2)

{

HAL_GPIO_TogglePin(LED_5_GPIO_Port, LED_5_Pin);

}

int main(void)

{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_I2C2_Init();

HAL_I2C_Slave_Receive_IT(&hi2c2, (uint8_t *)&data_receive, 1);

HAL_I2C_Slave_Transmit_IT(&hi2c2, (uint8_t *)&data_transmit, 1);

while (1)

{

}

}

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;

RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)

{

Error_Handler();

}

}

static void MX_I2C2_Init(void)

{

hi2c2.Instance = I2C2;

hi2c2.Init.Timing = 0x00B0DBFF;

hi2c2.Init.OwnAddress1 = 0x55;

hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

hi2c2.Init.OwnAddress2 = 0;

hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;

hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_ENABLE;

hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

if (HAL_I2C_Init(&hi2c2) != HAL_OK)

{

Error_Handler();

}

if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_DISABLE) != HAL_OK)

{

Error_Handler();

}

if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)

{

Error_Handler();

}

}

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

__HAL_RCC_GPIOB_CLK_ENABLE();

__HAL_RCC_GPIOC_CLK_ENABLE();

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);

/*Configure GPIO pins : PC6 PC7 PC8 PC9 */

GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

void Error_Handler(void)

{

__disable_irq();

while (1)

{

}

}

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)

{

}

#endif

1 REPLY 1
TDK
Guru

Follow an I2C example.

https://github.com/STMicroelectronics/STM32CubeF0/blob/4390ff6bfb693104cf97192f98c3dc9e3a7c296a/Projects/STM32F091RC-Nucleo/Examples/I2C/I2C_TwoBoards_ComIT/Src/main.c

You can't both send and receive data at the same time. Monitor HAL return values.

If you feel a post has answered your question, please click "Accept as Solution".