cancel
Showing results for 
Search instead for 
Did you mean: 

problem with PB6 & PB7 pin of stm32f103c8 mcu with respect to code generated from cubeMX

YS
Associate

Hi,

I am using STM32F103C8(blue_pill) MCU to write & read data to AT24mC02 eeprom.

I selected default I2C pin PB6 & PB7 for communication using stmcubeMX & true studio,but it is not working.

To cross verify whether my hardware circuit is working fine i tested with arduino IDE with same pin PB6 & PB7 selected for communication,it is working well.

so the code generated from stmcubemx is not working, i checked SCLK pin in oscilloscope there is no clock generated.

But when i changed pins to PB8 & PB9 it is working well with stmcubemx.

what is the problem with PB6 & PB7 pin with respect to code generated from cubeMX ?

Thanks

6 REPLIES 6
Bob S
Principal

What debugging have you done to try and figure out this problem? You have the source code, you have a debugger. Look at the code and see if it really does configure those pins. Add code to read back the GPIO and I2C config registers and make sure they contain the values you would expect. Or post those values here.

We can't read minds or divine code issues by reading tea leaves. As a last resort, post your code here and we'll see what we can see.

Most of the pros here aren't using CubeMX, and aren't looking to waste hours digging bugs from it.

Make sure all the GPIO and AFIO clocks are correctly enabled before talking to the hardware.

Make sure that any AF pin remapping is done correctly.

Use a debugger capable of single stepping, and reviewing the content of the assorted peripheral registers, and then inspect them for correctness.

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

Hi Bob,

Thanks your reply.

I amusing atollic true studio V9.2 and st-link V2 debugger to program the MCU. But st-link debugger is not supporting to true studio so i cannot debug the code online,.

I will take .hex outfile generated from true studio to program MCU. This is first time we are using ST MCU's so we don't know how to move with

Actually ,STM32F105VCT6 MCU selected for our project due to unavailability of development board for that MCU we selected stm32f103C8 blue pill which is readily available in market.

I am posting my code below. The code edited by me is in Bold & remaining code is generated by stmcubeMX.

/*******************************Main Program*************************/

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "i2c.h"

#include "gpio.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

#define EPPROM_ADDRESS 0xA0

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

uint8_t write_data=100,read_data=0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

  

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_I2C1_Init();

 /* USER CODE BEGIN 2 */

 HAL_I2C_Mem_Write(&hi2c1,EPPROM_ADDRESS, 0 , 0XFF, &write_data,1,10);

 HAL_Delay(10);

 HAL_I2C_Mem_Read(&hi2c1,EPPROM_ADDRESS, 1 , 0XFF,&read_data,1,1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

   if(read_data==100)

 HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);

 else

 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);

   HAL_Delay(2000);

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

/**

 * @brief System Clock Configuration

 * @retval None

 */

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Initializes the CPU, AHB and APB busses clocks 

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

 RCC_OscInitStruct.HSEState = RCC_HSE_ON;

 RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;

 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_DIV2;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)

 {

  Error_Handler();

 }

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

 * @brief This function is executed in case of error occurrence.

 * @retval None

 */

void Error_Handler(void)

{

 /* USER CODE BEGIN Error_Handler_Debug */

 /* User can add his own implementation to report the HAL error return state */

 /* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

 * @brief Reports the name of the source file and the source line number

 *     where the assert_param error has occurred.

 * @param file: pointer to the source file name

 * @param line: assert_param error line source number

 * @retval None

 */

void assert_failed(uint8_t *file, uint32_t line)

 /* USER CODE BEGIN 6 */

 /* User can add his own implementation to report the file name and line number,

   tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

/*************************************************************************/

/************************I2C initialization file***********************/

/* Includes ------------------------------------------------------------------*/

#include "i2c.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

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 */

 }

}

/******************************************************************************/

What @Community member​ said. Specially about using the debugger. ST-Link v2 *should* work with True Studio, so maybe something isn't right with your installation. Without a debugger you are flying blind and your life will be miserable.

jingW
Associate

The same issue happended on my ST32F103VET6 board, I use the stm32cubemx to configure the PB6/7 to the I2C1, but it doesn't work. Then I remap the I2C1 to PB8/9, everything is OK. I replaced the SOC, the same issue happened.  I think the root cause is caused by the STM32Cubemx. 

Read out and check/compare-between-working-and-not-working content of I2C and relevant GPIO and AFIO registers.

Do you enable some of the peripherals which collide with I2C1 on PB6/PB7, e.g. TIM4, USART1, CAN2?

JW