0

I am using the STM32Wb55RGv6 board to read a sensor using the i2c protocol. I want to use it without using CubeMX (.ioc file) because I am unable to use it to add it to my project (I am using one of the provided example projects: Projects\P-NUCLEO-WB55.Nucleo\Applications\Zigbee\Zigbee_APS_Router) which does not have a .ioc file.

I can't use the .ioc file to configure the peripheral so I made a new project, set up the I2c peripheral and wrote code to read the sensor. Then I copied the code generated by CubeMX, void MX_I2C1_Init(void), over to the new project.

Whenever I try to read the sensor in the test project I made to test the sensor it read fine, so I know the sensor is connected the right way and the code to read the sensor is fine as well. But when I try to read the sensor in my new project, which does not use CubeMX it returns HAL_ERROR.

/* I2C init function */
void MX_I2C1_Init(void)
{

    /* USER CODE BEGIN I2C1_Init 0 */
    GPIO_InitTypeDef GPIO_InitStruct;

    /* Configure I2C pins */
    GPIO_InitStruct.Pin       = GPIO_PIN_9 | GPIO_PIN_10;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USER CODE END I2C1_Init 1 */
    hi2c1.Instance = I2C1;
    hi2c1.Init.Timing = 0x00000E14;
    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();
    }

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

    /** Configure Digital filter*/
    if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN I2C1_Init 2 */

    /* USER CODE END I2C1_Init 2 */
}
/* I2C init function */

besided copying this code I also went into the stm32wbxx_hal_conf.h file and enabled i2c there.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • That's not very much info. Do you activate I2C clock before using it? At what stage does it fail exactly? Have you tested your configuration? When you write some registers, read them back and check that they contain expected values. If you're not using generated code, you will have to go down to register level, you can avoid it only for so long. – Ilya Mar 21 '23 at 11:58

1 Answers1

0

But when I try to read the sensor in my new project, which does not use CubeMX it returns HAL_ERROR.

HAL has many dependencies and it is pointless to use it without generating projects using Cube.

Not only .h files have to be included, but also project-wide definitions set.

Use Cube to generate startup code - then use HAL manually

0___________
  • 60,014
  • 4
  • 34
  • 74