L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
i2c.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file i2c.c
5 * @brief This file provides code for the configuration
6 * of the I2C instances.
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2025 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20/* Includes ------------------------------------------------------------------*/
21#include "i2c.h"
22
23/* USER CODE BEGIN 0 */
24
25/* USER CODE END 0 */
26
27I2C_HandleTypeDef hi2c2;
28
29/* I2C2 init function */
30void MX_I2C2_Init(void)
31{
32
33 /* USER CODE BEGIN I2C2_Init 0 */
34
35 /* USER CODE END I2C2_Init 0 */
36
37 /* USER CODE BEGIN I2C2_Init 1 */
38
39 /* USER CODE END I2C2_Init 1 */
40 hi2c2.Instance = I2C2;
41 hi2c2.Init.Timing = 0x30108DFF;
42 hi2c2.Init.OwnAddress1 = 0;
43 hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
44 hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
45 hi2c2.Init.OwnAddress2 = 0;
46 hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
47 hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
48 hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
49 if (HAL_I2C_Init(&hi2c2) != HAL_OK)
50 {
52 }
53
54 /** Configure Analogue filter
55 */
56 if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
57 {
59 }
60
61 /** Configure Digital filter
62 */
63 if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
64 {
66 }
67 /* USER CODE BEGIN I2C2_Init 2 */
68
69 /* USER CODE END I2C2_Init 2 */
70
71}
72
73void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
74{
75
76 GPIO_InitTypeDef GPIO_InitStruct = {0};
77 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
78 if(i2cHandle->Instance==I2C2)
79 {
80 /* USER CODE BEGIN I2C2_MspInit 0 */
81
82 /* USER CODE END I2C2_MspInit 0 */
83
84 /** Initializes the peripherals clocks
85 */
86 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2;
87 PeriphClkInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;
88 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
89 {
91 }
92
93 __HAL_RCC_GPIOA_CLK_ENABLE();
94 /**I2C2 GPIO Configuration
95 PA11 ------> I2C2_SDA
96 PA12 ------> I2C2_SCL
97 */
98 GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
99 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
100 GPIO_InitStruct.Pull = GPIO_NOPULL;
101 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
102 GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
103 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
104
105 /* I2C2 clock enable */
106 __HAL_RCC_I2C2_CLK_ENABLE();
107 /* USER CODE BEGIN I2C2_MspInit 1 */
108
109 /* USER CODE END I2C2_MspInit 1 */
110 }
111}
112
113void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
114{
115
116 if(i2cHandle->Instance==I2C2)
117 {
118 /* USER CODE BEGIN I2C2_MspDeInit 0 */
119
120 /* USER CODE END I2C2_MspDeInit 0 */
121 /* Peripheral clock disable */
122 __HAL_RCC_I2C2_CLK_DISABLE();
123
124 /**I2C2 GPIO Configuration
125 PA11 ------> I2C2_SDA
126 PA12 ------> I2C2_SCL
127 */
128 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11);
129
130 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);
131
132 /* USER CODE BEGIN I2C2_MspDeInit 1 */
133
134 /*
135 GPIO_InitTypeDef GPIO_InitStruct = { 0 };
136 GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
137 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
138 GPIO_InitStruct.Pull = GPIO_PULLUP;
139 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
140*/
141 /* USER CODE END I2C2_MspDeInit 1 */
142 }
143}
144
145/* USER CODE BEGIN 1 */
146
147/**
148 * @brief Deinitialize I2C2 peripheral for low power mode
149 */
151{
152 HAL_I2C_DeInit(&hi2c2);
153 // MT 10.1.2026 set to HI, becuase of resistor to VCC
154 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
155 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
156}
157
158HAL_StatusTypeDef I2C_IsDeviceReadyMT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
159{
160 HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(hi2c, DevAddress, Trials, Timeout);
161 if (status == HAL_BUSY)
162 {
163 HAL_I2C_DeInit(hi2c);
164 HAL_Delay(100);
165 HAL_I2C_Init(hi2c);
166 }
167 return status;
168}
169/* USER CODE END 1 */
void MX_I2C2_Init(void)
Initialise the I2C2 peripheral (hi2c2) as configured by STM32CubeMX. Sets clock speed,...
Definition i2c.c:30
void HAL_I2C_MspInit(I2C_HandleTypeDef *i2cHandle)
Definition i2c.c:73
HAL_StatusTypeDef I2C_IsDeviceReadyMT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
Wrapper around HAL_I2C_IsDeviceReady() that recovers from a busy bus. If the HAL I2C bus is in a BUSY...
Definition i2c.c:158
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *i2cHandle)
Definition i2c.c:113
void MX_I2C2_DeInit(void)
Deinitialize I2C2 peripheral for low power mode.
Definition i2c.c:150
This file contains all the function prototypes for the i2c.c file.
I2C_HandleTypeDef hi2c2
Definition i2c.c:27
void Error_Handler(void)
Default error handler called by HAL on unrecoverable errors. Disables interrupts and enters an infini...
Definition main.c:505
void HAL_Delay(__IO uint32_t Delay)
Definition sys_app.c:369