AnsweredAssumed Answered

HAL driver I2C with stm32F0 - init and transmit

Question asked by dormann.uli on Feb 13, 2015
Latest reply on Feb 16, 2015 by Amel N
Hello,
i want to use the I2C with the HAL driver but i can't send something.
I use the STM32CubeMX and have following code:

main.c:
I2C_HandleTypeDef hi2c1;
...
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_TSC_Init(void);
...
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TSC_Init();
...
}


/** System Clock Configuration
*/
void SystemClock_Config(void){

RCC_OscInitTypeDef RCC_OscInitStruct; 
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
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.PLLMUL = RCC_PLL_MUL12;<br>  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;<br>  HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
 __SYSCFG_CLK_ENABLE();
}

<br><br>/* I2C1 init function */<br>void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x2000090E;
hi2c1.Init.OwnAddress1 = 20;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
if(HAL_I2C_Init(&hi2c1)!= HAL_OK){
 return;
  }
/**Configure Analogue filter 
    */
  HAL_I2CEx_AnalogFilter_Config(&hi2c1, I2C_ANALOGFILTER_ENABLED);
}
   

/* TSC init function */
void MX_TSC_Init(void)
{
  /**Configure the TSC peripheral 
   */
htsc.Instance = TSC;
htsc.Init.CTPulseHighLength = TSC_CTPH_2CYCLES;
  htsc.Init.CTPulseLowLength = TSC_CTPL_2CYCLES;
  htsc.Init.SpreadSpectrum = DISABLE;
htsc.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV32;
htsc.Init.MaxCountValue = TSC_MCV_16383;
htsc.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW;
htsc.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL;
htsc.Init.ChannelIOs = TSC_GROUP3_IO3|TSC_GROUP1_IO1|TSC_GROUP1_IO2|TSC_GROUP1_IO3                   |TSC_GROUP2_IO1|TSC_GROUP3_IO1|TSC_GROUP3_IO2|TSC_GROUP5_IO1                    |TSC_GROUP5_IO2|TSC_GROUP5_IO3|TSC_GROUP6_IO2|TSC_GROUP6_IO3                   |TSC_GROUP6_IO4;<br>  htsc.Init.ShieldIOs = TSC_GROUP4_IO1;
htsc.Init.SamplingIOs = TSC_GROUP1_IO4|TSC_GROUP4_IO2|TSC_GROUP2_IO4|TSC_GROUP3_IO4                 |TSC_GROUP5_IO4|TSC_GROUP6_IO1;<br>  HAL_TSC_Init(&htsc);}


/** Configure pins as <br>        * Analog <br>        * Input 
        * Output
* EVENT_OUT * EXTI*/
void MX_GPIO_Init(void)
{ GPIO_InitTypeDef GPIO_InitStruct;
 /* GPIO Ports Clock Enable */
  __GPIOF_CLK_ENABLE();
  __GPIOC_CLK_ENABLE();
  __GPIOA_CLK_ENABLE();
  __GPIOB_CLK_ENABLE();
  /*Configure GPIO pins : PF0 PF4 PF5 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;<br>  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
 /*Configure GPIO pins : PC1 PC4 PC8 PC9 
                           PC11 */
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_8|GPIO_PIN_9
                        |GPIO_PIN_11;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;<br>  GPIO_InitStruct.Pull = GPIO_PULLUP;<br>  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 /*Configure GPIO pins : PA5 PA6 PA8 PA11 
                         PA12 PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_8|GPIO_PIN_11
                          |GPIO_PIN_12|GPIO_PIN_15;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PB15 */
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : PC6 PC7 */
 GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}

With a button i call the funktion I2C_Send:
void I2C_Send(uint8_t command, uint16_t value)     
{  
HAL_I2C_IsDeviceReady(&hi2c1, I2C_adress_tas,2,i2c_Timeout);
uint8_t _i2cMaster_Buffer[] = {0x00, 0x00, 0x00,0x00, 0x00, 0x00};    
_i2cMaster_Buffer[0] = 0x01; //command
_i2cMaster_Buffer[1] = 0x02;
_i2cMaster_Buffer[2] = 0x04;
 _i2cMaster_Buffer[3] = 0x08;
 _i2cMaster_Buffer[4] = 0x00;
_i2cMaster_Buffer[5] = 0x00; 
while( HAL_I2C_Master_Transmit( (void*)&hi2c, I2C_adress_tas, _i2cMaster_Buffer,  1,  1000) !=HAL_OK){
}

On Hardware i have external pull ups and for testing no slave devive. But it doesent matter for my problem if a second device is connected.
When i called the HAL_I2C_Master_Transmit method my SDA signal goes to low. When i debug, i get a Timeout and the HAL_I2C_Master_Transmit method is leave.
I tried to call the HAL_I2C_MspInit(&hi2c1) on different places.

Can anybody tells me where is my failure or where i can start?

regards

uli

Outcomes