大家好,我用的版子是STM32F429I DISCOVERY,目前在學習HAL庫的I2C用法。
想要讀取BOSCH 的BMA250E 這個G-sensor的 CHIP_ID,請幫我看一下我哪裡寫錯了呢?? 資料完全無法讀寫的感覺。
chip id 是一個固定的值0XF9
BMA250E的I2C位置是0X18
CHIP_ID的唯讀暫存器位置是0X00
- #include "stm32f4xx_hal.h"
- static void SystemClock_Config(void);
- #define I2C_ADDRESS 0x18
- uint8_t aTxBuffer[2],aRxBuffer;
- int main(){
- HAL_Init();
- SystemClock_Config();
-
-
- I2C_HandleTypeDef I2cHandle;
- /*##-1- Configure the I2C peripheral #######################################*/
- I2cHandle.Instance = I2C1;
-
- I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- I2cHandle.Init.ClockSpeed = 400000;
- I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
- I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- I2cHandle.Init.OwnAddress1 = 0;
- I2cHandle.Init.OwnAddress2 = 0;
- HAL_I2C_Init(&I2cHandle);
-
- aTxBuffer[0]=0x00;
- HAL_I2C_Master_Transmit(&I2cHandle, I2C_ADDRESS,aTxBuffer, 1, 10000);
- HAL_I2C_Master_Receive(&I2cHandle,I2C_ADDRESS|0x01 ,&aRxBuffer, 1, 10000);
- HAL_Delay(1000);
-
- }
- static void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_OscInitTypeDef RCC_OscInitStruct;
-
- /* The voltage scaling allows optimizing the power consumption when the device is
- clocked below the maximum system frequency, to update the voltage scaling value
- regarding system frequency refer to product datasheet. */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
-
- /* Enable HSE Oscillator and activate PLL with HSE as source */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 360;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- /* Activate the Over-Drive mode */
- HAL_PWREx_EnableOverDrive();
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
-
- }
- #define I2Cx_CLK_ENABLE() __HAL_RCC_I2C1_CLK_ENABLE()
- #define I2Cx_SDA_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
- #define I2Cx_SCL_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
- #define I2Cx_FORCE_RESET() __HAL_RCC_I2C1_FORCE_RESET()
- #define I2Cx_RELEASE_RESET() __HAL_RCC_I2C1_RELEASE_RESET()
- /* Definition for I2Cx Pins */
- #define I2Cx_SCL_PIN GPIO_PIN_6
- #define I2Cx_SCL_GPIO_PORT GPIOB
- #define I2Cx_SCL_AF GPIO_AF4_I2C1
- #define I2Cx_SDA_PIN GPIO_PIN_9
- #define I2Cx_SDA_GPIO_PORT GPIOB
- #define I2Cx_SDA_AF GPIO_AF4_I2C1
- void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- /*##-1- Enable peripherals and GPIO Clocks #################################*/
- /* Enable GPIO TX/RX clock */
- I2Cx_SCL_GPIO_CLK_ENABLE();
- I2Cx_SDA_GPIO_CLK_ENABLE();
- /* Enable I2C1 clock */
- I2Cx_CLK_ENABLE();
-
- /*##-2- Configure peripheral GPIO ##########################################*/
- /* I2C TX GPIO pin configuration */
- GPIO_InitStruct.Pin = I2Cx_SCL_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
- GPIO_InitStruct.Alternate = I2Cx_SCL_AF;
-
- HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
-
- /* I2C RX GPIO pin configuration */
- GPIO_InitStruct.Pin = I2Cx_SDA_PIN;
- GPIO_InitStruct.Alternate = I2Cx_SDA_AF;
-
- HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
- }
- /**
- * @brief I2C MSP De-Initialization
- * This function frees the hardware resources used in this example:
- * - Disable the Peripheral's clock
- * - Revert GPIO, DMA and NVIC configuration to their default state
- * @param hi2c: I2C handle pointer
- * @retval None
- */
- void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
- {
- /*##-1- Reset peripherals ##################################################*/
- I2Cx_FORCE_RESET();
- I2Cx_RELEASE_RESET();
- /*##-2- Disable peripherals and GPIO Clocks ################################*/
- /* Configure I2C Tx as alternate function */
- HAL_GPIO_DeInit(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN);
- /* Configure I2C Rx as alternate function */
- HAL_GPIO_DeInit(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_PIN);
- }