cancel
Showing results for 
Search instead for 
Did you mean: 

how to configure STM32f030f4p6 I2C as a slave to send data to Master after request

Bangou_boy
Associate II

hello everyone i have a small pcb made by myself with an STM32f030f4p6 which will read the data from my ADC and send it to a master (Arduino) via I2C... For a start I just wanted to write a code to test the correct configuration of I2c between the two MCUs by sending a simple constant value from the STM32f030f4p6 after requesting the master ... but it does not work, I also used the examples seen online but even so they do not work. I then decided to scan with the arduino master to see if it recognises the slave connected to the line, then I find that when I compile the scanner code in the master, it identifies the address of the slave just once, but then it doesn't do it any more, returning nop device found as I defined in the scanner code on the ... does anyone have an idea why this is?

these are my first steps with the stm32 microcontroller thank you for your understanding

 

here is my code of the main.c

/* USER CODE BEGIN Header */

 

*/

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stm32f0xx_hal.h"

#include "stm32f0xx_hal_i2c.h"

 

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

 

/* USER CODE END Includes */

 

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

 

/* USER CODE END PTD */

 

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

 

/* USER CODE END PD */

 

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

 

/* USER CODE END PM */

 

/* Private variables ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c1;

 

/* USER CODE BEGIN PV */

/* Transmit buffer with constant values */

uint8_t aTxBuffer[] = {0xAA, 0xBB, 0xCC, 0xDD};

/* USER CODE END PV */

 

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

void Error_Handler(void);

/* USER CODE BEGIN PFP */

 

/* USER CODE END PFP */

 

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

 

/* USER CODE END 0 */

 

/**

* @brief The application entry point.

* @retval int

*/

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_I2C1_Init();

/* USER CODE BEGIN 2 */

 

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

// The MCU will sleep waiting for I2C interrupts

HAL_SuspendTick();

HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

HAL_ResumeTick();

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

 

/**

* @brief System Clock Configuration

* @retval None

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 

/** Initializes the RCC Oscillators according to the specified parameters

* in the RCC_OscInitTypeDef structure.

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

 

/** Initializes the CPU, AHB and APB buses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 

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

{

Error_Handler();

}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;

PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

{

Error_Handler();

}

}

 

/**

* @brief I2C1 Initialization Function

* @param None

* @retval None

*/

static void MX_I2C1_Init(void)

{

 

/* USER CODE BEGIN I2C1_Init 0 */

 

/* USER CODE END I2C1_Init 0 */

 

/* USER CODE BEGIN I2C1_Init 1 */

 

/* USER CODE END I2C1_Init 1 */

hi2c1.Instance = I2C1;

hi2c1.Init.Timing = 0x00201D2B;

hi2c1.Init.OwnAddress1 = 78;

hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

hi2c1.Init.OwnAddress2 = 0;

hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;

hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

if (HAL_I2C_Init(&hi2c1) != HAL_OK)

{

Error_Handler();

}

 

/** Configure Analogue filter

*/

if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)

{

Error_Handler();

}

 

/** Configure Digital filter

*/

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

{

Error_Handler();

}

/* USER CODE BEGIN I2C1_Init 2 */

 

/* USER CODE END I2C1_Init 2 */

 

}

 

/**

* @brief GPIO Initialization Function

* @param None

* @retval None

*/

static void MX_GPIO_Init(void)

{

/* USER CODE BEGIN MX_GPIO_Init_1 */

/* USER CODE END MX_GPIO_Init_1 */

 

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOA_CLK_ENABLE();

 

/* USER CODE BEGIN MX_GPIO_Init_2 */

/* USER CODE END MX_GPIO_Init_2 */

}

 

/* USER CODE BEGIN 4 */

void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) {

// Assuming TransferDirection is 0 for receive and 1 for transmit

if (TransferDirection == 0) { // Check if this matches the HAL's definition

HAL_I2C_Slave_Transmit_IT(hi2c, aTxBuffer, sizeof(aTxBuffer));

}

}

 

/**

* @brief I2C error callback

*/

void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)

{

/* Restart I2C communication in case of an error */

HAL_I2C_DeInit(hi2c);

MX_I2C1_Init();

HAL_I2C_EnableListen_IT(hi2c);

}

/* USER CODE END 4 */

 

/**

* @brief This function is executed in case of error occurrence.

* @retval None

*/

void Error_Handler(void)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

__disable_irq();

while (1)

{

}

/* USER CODE END Error_Handler_Debug */

}

 

#ifdef USE_FULL_ASSERT

/**

* @brief Reports the name of the source file and the source line number

* where the assert_param error has occurred.

* @param file: pointer to the source file name

* @param line: assert_param error line source number

* @retval None

*/

void assert_failed(uint8_t *file, uint32_t line)

{

/* USER CODE BEGIN 6 */

/* User can add his own implementation to report the file name and line number,

ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

 

21 REPLIES 21

Hello @Bangou_boy 

 

I checked your code on the STM32F030RB Nucleo board. The I2C peripheral is well initialized as expected, and the execution didn't crash while listening IT. Please verify your setup.

Below, you will find a screenshot of the content of the CR1 and CR2 registers.

Saket_Om_0-1723125501497.png

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar