cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Timeout Problem

PGYUY.1
Associate

Hi!

I'm using mpu6050 and Nucleo-f103RB without MXCUBE

i got problem with I2C communication.

LED is blink in infinity..

/* Wait until SB flag is set */

   if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK)

   {

    return HAL_TIMEOUT;

   }

this is part of header file which make LED blink all the time

Has anyone found a way to deal with this?

Thanks!

////////////////////////////////////////////////////////////////////////////

#include "main.h"

I2C_HandleTypeDef handle_i2c;

static uint16_t Xdata = 0x00;

static int a=0;

void SYS_CLK(void);

void LED_PA5(void);

void I2C_SET(void);

int main(void)

{

uint8_t address = 0x68;

uint8_t GYROH = 0x43;

uint8_t GYROL = 0x44;

uint8_t buffer[2];

  

SYS_CLK();

LED_PA5();

I2C_SET();

  

 while( HAL_I2C_IsDeviceReady(&handle_i2c,address<<1,1,100) == HAL_TIMEOUT)

 {

  

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

  HAL_Delay(100);

}

}

void SYS_CLK(void)

{

// SYS_CLK SETTING

RCC_OscInitTypeDef RCC_OscInitStruct = {0}; //Reset OscInit Struct 

  

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_DIV2; // div clock by 2

RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // mul clock to 16

  

HAL_RCC_OscConfig(&RCC_OscInitStruct);

  

  

  

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; // Reset Clk Struct

  

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 ; // look at tree

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  

HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);  

}

void LED_PA5(void)

{

 GPIO_InitTypeDef GPIO_PA5 = {0};  

// CLOCK SETTING

__HAL_RCC_GPIOA_CLK_ENABLE();  

// GPIO SETTING

GPIO_PA5.Pin = GPIO_PIN_5; // GPIOA's 5th Pin use

GPIO_PA5.Mode = GPIO_MODE_OUTPUT_PP; // use A5 Pin as output

GPIO_PA5.Pull = GPIO_PULLUP; // use A5 Pin as Pullup

GPIO_PA5.Speed = GPIO_SPEED_FREQ_LOW; // LOW FREQ

HAL_GPIO_Init(GPIOA, &GPIO_PA5); // SET GPIOA 

//  

}

void I2C_SET(void)

{

// SET The handle Structure

// Enable I2C1 interface Clock

// I2C pins configuration

__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable the clock for the I2C GPIOs

//Configure I2C pins as alternate function open-drain

GPIO_InitTypeDef I2CPin_PA6_SCL = {0};

GPIO_InitTypeDef I2CPin_PA7_SCA = {0};

I2CPin_PA6_SCL.Pin = GPIO_PIN_6;

I2CPin_PA6_SCL.Mode = GPIO_MODE_AF_OD;

I2CPin_PA6_SCL.Pull = GPIO_PULLUP;

I2CPin_PA6_SCL.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOB, &I2CPin_PA6_SCL);

I2CPin_PA7_SCA.Pin = GPIO_PIN_7;

I2CPin_PA6_SCL.Mode = GPIO_MODE_AF_OD;

I2CPin_PA6_SCL.Pull = GPIO_PULLUP;

I2CPin_PA6_SCL.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOB, &I2CPin_PA7_SCA);

handle_i2c.Instance = I2C1;

handle_i2c.Init.ClockSpeed = 400000;

handle_i2c.Init.DutyCycle = I2C_DUTYCYCLE_2; // 표준 듀티비

handle_i2c.Init.OwnAddress1 = 0;

handle_i2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

handle_i2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

handle_i2c.Init.OwnAddress2 = 0;

handle_i2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

handle_i2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

HAL_I2C_Init(&handle_i2c);

}

1 REPLY 1
GunkutA
Senior

Hello, sometimes I2C busy flag never gets cleared in some STM32 MCU's shuch as: STM32F103C8Tx. I am not sure if it is the case for your MCU. But you may want to check STM32 I2C errata workaround.