Search code examples
stm32initi2chal

trying to do some i2c communication with my BNO055 IMU but cant seem to get passed the MX_I2C1_Init function


I have been trying to do some i2c communication with my BNO055 IMU but cant seem to get passed the MX_I2C1_Init function when i run it in debug.

It gives me this error:

Error: jtag status contains invalid mode value - communication failure
Polling target STM32F303RETx.cpu failed, trying to reexamine
Examination failed, GDB will be halted. Polling again in 6300ms
Info : Previous state query failed, trying to reconnect
Error: jtag status contains invalid mode value - communication failure
Polling target STM32F303RETx.cpu failed, trying to reexamine
Examination failed, GDB will be halted. Polling again in 6300ms
shutdown command invoked

I'm currently using a nucleo stm32F303RE board and had my base code generated by cubemx. it genreated code for the hal functions, the i2c1 pins, uart4 pins. here is my code.

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f3xx_hal.h"

/* USER CODE BEGIN Includes */
#define DEV_ADD          (0x28<<1)
#define UNIT_SELECT_ADD  0x3B
#define UNIT_SELECT_DATA 0x68
#define OPR_MODE_ADD     0x3D
#define OPR_MODE_DATA    0xFC
#define EUL_X_ADD        0x1A
#define EUL_Y_ADD        0x1C
#define EUL_Z_ADD        0x1E
#define LIA_X_ADD        0x28
#define LIA_Y_ADD        0x2A
#define LIA_Z_ADD        0x2C
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef huart4;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

unsigned char eul_x_msb = 0, eul_x_lsb = 0, eul_y_msb = 0, eul_y_lsb = 0, eul_z_msb = 0, eul_z_lsb = 0;
unsigned char lia_x_msb = 0, lia_x_lsb = 0, lia_y_msb = 0, lia_y_lsb = 0, lia_z_msb = 0, lia_z_lsb = 0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_UART4_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

void i2c_init_function(void){
    unsigned char aRxBuffer[3], txBuffer[3];
    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, UNIT_SELECT_ADD, I2C_MEMADD_SIZE_8BIT, aRxBuffer, 1, 100);
    aRxBuffer[0] = aRxBuffer[0] & UNIT_SELECT_DATA;
    txBuffer[0] = UNIT_SELECT_ADD;
    txBuffer[1] = aRxBuffer[0];
    HAL_I2C_Master_Transmit(&hi2c1, DEV_ADD, txBuffer, 1, 100);


    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, OPR_MODE_ADD, I2C_MEMADD_SIZE_8BIT, aRxBuffer, 1, 100);
    aRxBuffer[0] = aRxBuffer[0] & OPR_MODE_DATA;
    txBuffer[0] = OPR_MODE_ADD;
    txBuffer[1] = aRxBuffer[0];
    HAL_I2C_Master_Transmit(&hi2c1, DEV_ADD, txBuffer, 1, 100);
}

void eul_x_read(unsigned char * tab){
    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, EUL_X_ADD, I2C_MEMADD_SIZE_8BIT, tab, 2, 100);
}

void eul_y_read(unsigned char * tab){
    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, EUL_Y_ADD, I2C_MEMADD_SIZE_8BIT, tab, 2, 100);
}

void eul_z_read(unsigned char * tab){
    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, EUL_Z_ADD, I2C_MEMADD_SIZE_8BIT, tab, 2, 100);
}

void lia_x_read(unsigned char * tab){
    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, LIA_X_ADD, I2C_MEMADD_SIZE_8BIT, tab, 2, 100);
}

void lia_y_read(unsigned char * tab){
    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, LIA_Y_ADD, I2C_MEMADD_SIZE_8BIT, tab, 2, 100);
}

void lia_z_read(unsigned char * tab){
    HAL_I2C_Mem_Read(&hi2c1, DEV_ADD, LIA_Z_ADD, I2C_MEMADD_SIZE_8BIT, tab, 2, 100);
}

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  *
  * @retval None
  */
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();
  MX_UART4_Init();
  /* USER CODE BEGIN 2 */


  i2c_init_function();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

  /* USER CODE END WHILE */
      unsigned char buff[3];

      eul_x_read(buff);
      HAL_Delay(25);
      eul_x_lsb = buff[0];
      eul_x_msb = buff[1];

      eul_y_read(buff);
      HAL_Delay(25);
      eul_y_lsb = buff[0];
      eul_y_msb = buff[1];

      eul_z_read(buff);
      HAL_Delay(25);
      eul_z_lsb = buff[0];
      eul_z_msb = buff[1];

      lia_x_read(buff);
      HAL_Delay(25);
      lia_x_lsb = buff[0];
      lia_x_msb = buff[1];

      lia_y_read(buff);
      HAL_Delay(25);
      lia_y_lsb = buff[0];
      lia_y_msb = buff[1];

      lia_z_read(buff);
      HAL_Delay(25);
      lia_z_lsb = buff[0];
      lia_z_msb = buff[1];


      unsigned char mapping[12] = {eul_x_msb, eul_x_lsb, eul_y_msb, eul_y_lsb, eul_z_msb, eul_z_lsb,
                                   lia_x_msb, lia_x_lsb, lia_y_msb, lia_y_lsb, lia_z_msb, lia_z_lsb};


      HAL_UART_Transmit(&huart4, mapping, 12, 100);
  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

    /**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.PLLMUL = RCC_PLL_MUL9;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**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(__FILE__, __LINE__);
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_UART4|RCC_PERIPHCLK_I2C1;
  PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**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, 0, 0);
}

/* I2C1 init function */
static void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x2000090E;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Analogue filter 
    */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Digital filter 
    */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

/* UART4 init function */
static void MX_UART4_Init(void)
{

  huart4.Instance = UART4;
  huart4.Init.BaudRate = 115200;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();

  /*Configure GPIO pins : PA0 PA1 PA2 PA3 
                           PA4 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 
                          |GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  file: The file name as string.
  * @param  line: The line in file as a number.
  * @retval None
  */
void _Error_Handler(char *file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* 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 */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Solution

  • i figured it out XD i was accidentally using the JTags clock pin for my I2C communication so when it would enter the GPIO initializing the debugger would stop working XD i was initially using the pins PA14 and PA15. PA14 is the JTAG clock. so i changed them for PB8 and PB9 witch are a alternative pin selection for the I2C1 communication.here's the pin out description that enlightened me