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

@Bangou_boy 

 

Please check the slave own address; it might be set incorrectly.

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

Thanks
Omar

Bangou_boy_0-1722610218534.png

I set the slave own address

Hello @Bangou_boy 

 

Could you please share with us the content of the CR1, CR2, and ISR registers after I2C initialization?

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

Thanks
Omar

@Saket_Om 

I disable the cklock stretching in the stm32 and after that I now scan and found my stm32 on the bus I2C but what data send to master after request it still don t work.

@Saket_Om  sorry Sir but I think I don't understand what you mean with  CR1, CR2 . Sorry but i am beginner with STM32

 

 

Hello @Bangou_boy 

 

Does the HAL_I2C_AddrCallback() function get called? 

 

The CR1 and CR2 are the control register 1 and 2. You can get their value from the debugger that you are using. STM32Cube IDE for example. 

The ISR is the interrupt status register. 

 

You can refer to this thread to know how to get a peripheral register value.

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

Thanks
Omar

Hi @Saket_Om 

THank you . please find attached an screenshot of the CR1 CR2  ISR

 

Bangou_boy_0-1722942720631.png

Yes the HAL_I2C_AddrCallback() function get called.

Bangou_boy_1-1722943134146.png

 

 

Hello @Bangou_boy

 

Is the value of the register taken before the callback gets executed or after?

I ran your code on my side and obtained the value of the CR1 and CR2 registers after enabling listen IT. Please see the screenshot below.

Saket_Om_0-1722950025279.png

 

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

Thanks
Omar

@Saket_Om  when i debbug it stop at this point , it seems it didn't run the the Enablelisten_IT other it run it but didn't whrite anything in the CR regsister look like yours. Anyway the debbug stop at Enablelisten_IT

Bangou_boy_0-1722966232118.png

 

 

Hello @Bangou_boy ,

 

Could you please, if possible, sent me your whole project to check it in my side.

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

Thanks
Omar

Hello@Saket_Om 

it's not possible to send more than 3 files. i'm going to send them in several parts.