cancel
Showing results for 
Search instead for 
Did you mean: 

Random signals on I2C Communication?

ZHama.1
Associate II

Using Nucleo-L476RG board with I2C. Using a logic analyzer i'm getting meaningless signals as seen in the image. SDA AND SCL are connected to pins PB11 and PB10 respectively and are connected directly to the logic analyzer block directly to monitor them. This pattern happens regardless of whether transmitting, receiving or idle. When zooming each pulse is a spike around 25ns and isnt related to anything.

I2C Init:

static void MX_I2C2_Init(void)
{
 
  /* USER CODE BEGIN I2C2_Init 0 */
 
  /* USER CODE END I2C2_Init 0 */
 
  /* USER CODE BEGIN I2C2_Init 1 */
 
  /* USER CODE END I2C2_Init 1 */
  hi2c2.Instance = I2C2;
  hi2c2.Init.Timing = 0x1060669A ;
  hi2c2.Init.OwnAddress1 = 0;
  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_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C2_Init 2 */
 
  /* USER CODE END I2C2_Init 2 */
 
}

Transmitting code:

short SCCB_Write(uint8_t reg_addr, uint8_t data) {
	short opertionStatus = 0;
	uint8_t buffer[2] = { 0 };
	HAL_StatusTypeDef connectionStatus;
	buffer[0] = reg_addr;
	buffer[1] = data;
	__disable_irq();
	connectionStatus = HAL_I2C_Master_Transmit(&hi2c2, (uint16_t) 0x60, buffer,
			2, 100);
	if (connectionStatus == HAL_OK) {
		opertionStatus = 1;
	} else {
		opertionStatus = 0;
	}
	__enable_irq();
	return opertionStatus;
}

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_I2C2_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
	SCCB_Write(0xff, 0x01);
	SCCB_Write(0x12, 0x80);
	HAL_Delay(10);
 
......

 What could be causing this potentially as I have used another Nucleo board and got the same result?

0693W00000D1dPiQAJ.jpg

9 REPLIES 9

What do the signals look like on a scope?

What are you attaching the pins too?

Do you have pull-up resistors?

Common grounding?

Do you see this when you take the Nucleo out of the box, or just after you attach tings to it?

What does the schematic for the board indicate are connected to PB10/PB11 ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I don't have a scope unfortunately.

The pins are connected straight to the logic analyzer, nothing else.

I haven't added pull up resistors as I have the following settings in the MSP config:

void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hi2c->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspInit 0 */
 
  /* USER CODE END I2C2_MspInit 0 */
 
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**I2C2 GPIO Configuration
    PB10     ------> I2C2_SCL
    PB11     ------> I2C2_SDA
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    /* Peripheral clock enable */
    __HAL_RCC_I2C2_CLK_ENABLE();
  /* USER CODE BEGIN I2C2_MspInit 1 */
 
  /* USER CODE END I2C2_MspInit 1 */
  }
}

The logic analyzer and nucleo boarded share common ground

I've been using this Nucleo for a while fine along with a couple others I'm getting this same result with.

There's a whole load of peripherals PB10/PB11 can be used for however ive selected I2C2 as alternate function so the other uses are irrelevant surely?

TDK
Guru

The built-in pullups are not going to be sufficient for I2C communication unless you significantly slow down the speed.

> This pattern happens regardless of whether transmitting, receiving or idle. When zooming each pulse is a spike around 25ns and isnt related to anything.

If the pins function properly as GPIO outputs, probably what you are seeing is noise.

It also sounds like maybe you haven't actually initialized the pins as I2C. Perhaps your code never makes it to that point.

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

The pins are initialized as I2C, ive double checked that.

In regards to the pullups, what do you suggest. What value resistances should I use for pull-ups and if I add them can i leave the internal pull-up setting in the STM32 the same as it is?

Typically 2.2K or 4.7K is used for I2C. You can still have the internal pullup enabled.
I think there's something else going on though. Do the pins work if you initialize them as GPIO and toggle them?
If you feel a post has answered your question, please click "Accept as Solution".

I added 4.7k pullups and it all works now. However I want to communicate with a 3.3V device but stm32 VCC is 5V. I have a logic level shifter from spark fun but how would I connect everything up with the newly added pullups?

The STM32 is NOT running at 5V, you need a regulator to get that into the 2.7 - 3.3V type range, use that as the source of any pull-up.

Should be several points on the NUCLEO board to find the VDD used for the IC

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
You didn’t answer my question.
Connect pull-ups to 3.3V.
If you feel a post has answered your question, please click "Accept as Solution".

I've connected the pull-ups to 3.3V and it worked on another nucleo board (L476RG NUCLEO) but on the one my project is based on (F446RE Nucleo) it does not work. I've added 3.3V pullups but same issue. I then changed the pins to function as GPIO and toggle them and I got the same result meaning I2C is not the problem. I debugged line by line and discovered the behaviour happens after the clock configuration:

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  /** 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_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 16;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  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_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_PLLCLK, RCC_MCODIV_2);
}

This is what I'm getting now: (Pins are connected straight to logic analyzer as GPIO without pullups).

0693W00000D1oslQAB.jpgThe same square wave stretches the entire 5 second sample for both PB10/PC12