cancel
Showing results for 
Search instead for 
Did you mean: 

I2C question ?

antonius
Senior

Dear Members,

I tried to initialize I2C in STM32F107

but got the straight line on Logic Analyzer,

I have no idea why ????

The code

void test()
{
	data_t[0] = 0x0C;  //en=1, rs=0
	data_t[1] = 0x08;  //en=0, rs=0
	data_t[2] = 0x0C;  //en=1, rs=0
	data_t[3] = 0x08;  //en=0, rs=0
	HAL_I2C_Master_Transmit(&hi2c1, 0x4E,(uint8_t *) data_t, 4, 100);
}	

Is it by default in STM32CubeMX, the cpu acts as master ??

I tested PB7 and PB6, they are connected properly into my header pins...

I have no clue why...??

0690X000009YiLbQAK.jpg

0690X000009YiLgQAK.jpg

11 REPLIES 11
antonius
Senior
I2C_HandleTypeDef hi2c1;
 
/* I2C1 init function */
void MX_I2C1_Init(void)
{
 
  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
 
}
 
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
 
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(i2cHandle->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */
 
  /* USER CODE END I2C1_MspInit 0 */
  
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    /* I2C1 clock enable */
    __HAL_RCC_I2C1_CLK_ENABLE();
  /* USER CODE BEGIN I2C1_MspInit 1 */
 
  /* USER CODE END I2C1_MspInit 1 */
  }
}

antonius
Senior

I upload the code with J-Flash

antonius
Senior

in main()

while (1)
  {
		test();
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }

antonius
Senior

0690X000009YiLlQAK.jpg

antonius
Senior

another question, do I need a pull up resistor for logic analyzer only ?

because it's open drain....

The pins look to be high. but yes I'd really think you'd either need internal or external pull-ups for correct operation.

Code doesn't look unreasonable, but not using F1+HAL here.

Going to have to walk hw registers, and external circuit, and check for error/status thrown via functions.

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

When I test the port as GPIO PB7 and PB6 are ok :

	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);
		HAL_Delay(1);
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
		HAL_Delay(10);
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET);
		HAL_Delay(1);
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET);
		

0690X000009YidkQAC.jpg

but as I2C, no luck yet ??

.....

antonius
Senior

Is my APB1 Clock not working ?

antonius
Senior

the I2C register

0690X000009Yie9QAC.jpg