2019-07-13 08:51 PM
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...??
2019-07-13 08:52 PM
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 */
}
}
2019-07-13 08:53 PM
I upload the code with J-Flash
2019-07-13 08:53 PM
in main()
while (1)
{
test();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
2019-07-13 09:00 PM
2019-07-13 09:12 PM
another question, do I need a pull up resistor for logic analyzer only ?
because it's open drain....
2019-07-14 07:53 AM
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.
2019-07-14 08:49 PM
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);
but as I2C, no luck yet ??
.....
2019-07-14 08:57 PM
Is my APB1 Clock not working ?
2019-07-14 09:11 PM
the I2C register