cancel
Showing results for 
Search instead for 
Did you mean: 

STMF407VG I2C problem with STM32Cube_FW_F4_V1.14.0

Posted on January 24, 2017 at 10:50

Hi.

I'm trying to communicate with I2C peripheral using SDK package mentioned in title. I'm using HAL_I2C_Master_Transmit and HAL_I2C_Master_Receive functions to read and write to bus. However both report error and nothing is read.

Problem isn't just the read. I can see with oscilloscope, that only the address is sent(1 bytes), even tough I'm sending 50 bytes of data. No extra clocking is generated either. Read only shows the address, but no further clocking is generated.

Scope image of the read:

0690X00000603ZDQAY.jpg

Scope image from write:

0690X00000603QIQAY.jpg

I've verified with debugger that the sent buffer contains valid data, and parameters contain the necessary amounts, etc. Problem with the SDK? I'm using the HAL_I2C_MspInit found in m32f4xx_hal_msp.c.

My clock intialization:

void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
 RCC_ClkInitTypeDef RCC_ClkInitStruct;
 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
/**Configure the main internal regulator output voltage 
 */
 __HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = 16;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLM = 8;
 RCC_OscInitStruct.PLL.PLLN = 50;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
 RCC_OscInitStruct.PLL.PLLQ = 7;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
/**Initializes the CPU, AHB and APB busses 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_DIV4;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 Error_Handler();
 }
/*
 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
 PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
 PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
*/
 /**Configure the Systick interrupt time 
 */
 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick 
 */
 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
}
And I2C init:
/* I2C1 init function */
static void MX_I2C1_Init(void)
{
I2cHandle.Instance = I2C1;
 I2cHandle.Init.ClockSpeed = 100000;
 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
 I2cHandle.Init.OwnAddress1 = 0;
 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
 I2cHandle.Init.OwnAddress2 = 0;
 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
 if (HAL_I2C_Init(&I2cHandle) != HAL_OK)
 {
 printf(''I2C init fail!\n\r'');
 Error_Handler();
 } else {
 printf(''I2C init OK!\n\r'');
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

#stm32cubef4 #i2c
2 REPLIES 2
Phil1
Associate II
Posted on January 24, 2017 at 13:17

Assuming you're trying to talk to a slave (as you're talking about sending an address)

Your I2C device isn't ACKing the address, which is probably what the error is.

That write transaction you posted is -- in order: start, write bit, 7-bit address 1000001, NACK (1 bit), stop.

Philips / NXP released an I2C application note which explains this; if you haven't read this, it's worth reading.

If a slave doesn't ACK the address by pulling SDA low, the STM32 I2C module will assume there's nothing there and return an error code.

Posted on January 24, 2017 at 14:24

Hot Damn. I misunderstood the NACK as ACK.

There was a problem with the HW wiring. Thanks for the pointer! The ST I2C config is littlebit on the complicated side so I was pretty certain the SW was the culprint :/.