[1] | 1 | /**
|
---|
| 2 | ******************************************************************************
|
---|
| 3 | * @file i2c.c
|
---|
| 4 | * @brief This file provides code for the configuration
|
---|
| 5 | * of the I2C instances.
|
---|
| 6 | ******************************************************************************
|
---|
| 7 | * @attention
|
---|
| 8 | *
|
---|
| 9 | * <h2><center>© Copyright (c) 2021 STMicroelectronics.
|
---|
| 10 | * All rights reserved.</center></h2>
|
---|
| 11 | *
|
---|
| 12 | * This software component is licensed by ST under Ultimate Liberty license
|
---|
| 13 | * SLA0044, the "License"; You may not use this file except in compliance with
|
---|
| 14 | * the License. You may obtain a copy of the License at:
|
---|
| 15 | * www.st.com/SLA0044
|
---|
| 16 | *
|
---|
| 17 | ******************************************************************************
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | /* Includes ------------------------------------------------------------------*/
|
---|
| 21 | #include "i2c.h"
|
---|
| 22 |
|
---|
| 23 | /* USER CODE BEGIN 0 */
|
---|
| 24 |
|
---|
| 25 | /* USER CODE END 0 */
|
---|
| 26 |
|
---|
| 27 | I2C_HandleTypeDef hi2c1;
|
---|
| 28 |
|
---|
| 29 | /* I2C1 init function */
|
---|
| 30 | void MX_I2C1_Init(void)
|
---|
| 31 | {
|
---|
| 32 |
|
---|
| 33 | /* USER CODE BEGIN I2C1_Init 0 */
|
---|
| 34 |
|
---|
| 35 | /* USER CODE END I2C1_Init 0 */
|
---|
| 36 |
|
---|
| 37 | /* USER CODE BEGIN I2C1_Init 1 */
|
---|
| 38 |
|
---|
| 39 | /* USER CODE END I2C1_Init 1 */
|
---|
| 40 | hi2c1.Instance = I2C1;
|
---|
| 41 | hi2c1.Init.ClockSpeed = 400000;
|
---|
| 42 | hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
---|
| 43 | hi2c1.Init.OwnAddress1 = 0;
|
---|
| 44 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
---|
| 45 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
---|
| 46 | hi2c1.Init.OwnAddress2 = 0;
|
---|
| 47 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
---|
| 48 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
---|
| 49 | if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
---|
| 50 | {
|
---|
| 51 | Error_Handler();
|
---|
| 52 | }
|
---|
| 53 | /** Configure Analogue filter
|
---|
| 54 | */
|
---|
| 55 | // if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
---|
| 56 | // {
|
---|
| 57 | // Error_Handler();
|
---|
| 58 | // }
|
---|
| 59 | // /** Configure Digital filter
|
---|
| 60 | // */
|
---|
| 61 | // if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
|
---|
| 62 | // {
|
---|
| 63 | // Error_Handler();
|
---|
| 64 | // }
|
---|
| 65 | /* USER CODE BEGIN I2C1_Init 2 */
|
---|
| 66 |
|
---|
| 67 | /* USER CODE END I2C1_Init 2 */
|
---|
| 68 |
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
|
---|
| 72 | {
|
---|
| 73 |
|
---|
| 74 | GPIO_InitTypeDef GPIO_InitStruct = {0};
|
---|
| 75 | if(i2cHandle->Instance==I2C1)
|
---|
| 76 | {
|
---|
| 77 | /* USER CODE BEGIN I2C1_MspInit 0 */
|
---|
| 78 | i2cHandle->Instance->CR1 |= I2C_CR1_SWRST;
|
---|
| 79 | HAL_Delay(100);
|
---|
| 80 | i2cHandle->Instance->CR1 &= ~I2C_CR1_SWRST;
|
---|
| 81 | /* USER CODE END I2C1_MspInit 0 */
|
---|
| 82 |
|
---|
| 83 | __HAL_RCC_GPIOB_CLK_ENABLE();
|
---|
| 84 | /**I2C1 GPIO Configuration
|
---|
| 85 | PB6 ------> I2C1_SCL
|
---|
| 86 | PB7 ------> I2C1_SDA
|
---|
| 87 | */
|
---|
| 88 | GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
|
---|
| 89 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
---|
| 90 | GPIO_InitStruct.Pull = GPIO_PULLUP;
|
---|
| 91 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
---|
| 92 | GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
|
---|
| 93 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
---|
| 94 |
|
---|
| 95 | /* I2C1 clock enable */
|
---|
| 96 | __HAL_RCC_I2C1_CLK_ENABLE();
|
---|
| 97 | /* USER CODE BEGIN I2C1_MspInit 1 */
|
---|
| 98 |
|
---|
| 99 | /* USER CODE END I2C1_MspInit 1 */
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
|
---|
| 104 | {
|
---|
| 105 |
|
---|
| 106 | if(i2cHandle->Instance==I2C1)
|
---|
| 107 | {
|
---|
| 108 | /* USER CODE BEGIN I2C1_MspDeInit 0 */
|
---|
| 109 |
|
---|
| 110 | /* USER CODE END I2C1_MspDeInit 0 */
|
---|
| 111 | /* Peripheral clock disable */
|
---|
| 112 | __HAL_RCC_I2C1_CLK_DISABLE();
|
---|
| 113 |
|
---|
| 114 | /**I2C1 GPIO Configuration
|
---|
| 115 | PB6 ------> I2C1_SCL
|
---|
| 116 | PB7 ------> I2C1_SDA
|
---|
| 117 | */
|
---|
| 118 | HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
|
---|
| 119 |
|
---|
| 120 | HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
|
---|
| 121 |
|
---|
| 122 | /* USER CODE BEGIN I2C1_MspDeInit 1 */
|
---|
| 123 |
|
---|
| 124 | /* USER CODE END I2C1_MspDeInit 1 */
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /* USER CODE BEGIN 1 */
|
---|
| 129 |
|
---|
| 130 | /* USER CODE END 1 */
|
---|
| 131 |
|
---|
| 132 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
---|