source: trunk/fw_g473rct/USBPD/App/usbpd_pwr_if.c@ 63

Last change on this file since 63 was 59, checked in by f.jahn, 5 weeks ago

Added more USBPD-stack files.

File size: 9.6 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file usbpd_pwr_if.c
5 * @author MCD Application Team
6 * @brief This file contains power interface control functions.
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 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
21#define __USBPD_PWR_IF_C
22
23/* Includes ------------------------------------------------------------------*/
24#include "usbpd_pwr_if.h"
25#include "usbpd_hw_if.h"
26#include "usbpd_dpm_core.h"
27#include "usbpd_dpm_conf.h"
28#include "usbpd_pdo_defs.h"
29#include "usbpd_core.h"
30#if defined(_TRACE)
31#include "usbpd_trace.h"
32#endif /* _TRACE */
33#include "string.h"
34/* USER CODE BEGIN Include */
35
36/* USER CODE END Include */
37
38/** @addtogroup STM32_USBPD_APPLICATION
39 * @{
40 */
41
42/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF
43 * @{
44 */
45
46/* Private typedef -----------------------------------------------------------*/
47/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_TypeDef
48 * @{
49 */
50/* USER CODE BEGIN Private_Typedef */
51
52/* USER CODE END Private_Typedef */
53/**
54 * @}
55 */
56
57/* Private define ------------------------------------------------------------*/
58/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Defines
59 * @{
60 */
61/* USER CODE BEGIN Private_Define */
62
63/* USER CODE END Private_Define */
64/**
65 * @}
66 */
67
68/* Private macros ------------------------------------------------------------*/
69/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Macros
70 * @{
71 */
72#if defined(_TRACE)
73#define PWR_IF_DEBUG_TRACE(_PORT_, __MESSAGE__) USBPD_TRACE_Add(USBPD_TRACE_DEBUG, (_PORT_), 0u, (uint8_t*)(__MESSAGE__), sizeof(__MESSAGE__) - 1u)
74#else
75#define PWR_IF_DEBUG_TRACE(_PORT_, __MESSAGE__)
76#endif /* _TRACE */
77/* USER CODE BEGIN Private_Macro */
78
79/* USER CODE END Private_Macro */
80/**
81 * @}
82 */
83
84/* Private variables ---------------------------------------------------------*/
85/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Variables
86 * @{
87 */
88/* USER CODE BEGIN Private_Variables */
89
90/* USER CODE END Private_Variables */
91/**
92 * @}
93 */
94
95/* Private function prototypes -----------------------------------------------*/
96/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Functions
97 * @{
98 */
99/* USER CODE BEGIN USBPD_USER_PRIVATE_FUNCTIONS_Prototypes */
100
101/* USER CODE END USBPD_USER_PRIVATE_FUNCTIONS_Prototypes */
102/**
103 * @}
104 */
105
106/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Exported_Functions
107 * @{
108 */
109
110/**
111 * @brief Initialize structures and variables related to power board profiles
112 * used by Sink and Source, for all available ports.
113 * @retval USBPD status
114 */
115USBPD_StatusTypeDef USBPD_PWR_IF_Init(void)
116{
117/* USER CODE BEGIN USBPD_PWR_IF_Init */
118 return USBPD_ERROR;
119/* USER CODE END USBPD_PWR_IF_Init */
120}
121
122/**
123 * @brief Checks if the power on a specified port is ready
124 * @param PortNum Port number
125 * @param Vsafe Vsafe status based on @ref USBPD_VSAFE_StatusTypeDef
126 * @retval USBPD status
127 */
128USBPD_StatusTypeDef USBPD_PWR_IF_SupplyReady(uint8_t PortNum, USBPD_VSAFE_StatusTypeDef Vsafe)
129{
130/* USER CODE BEGIN USBPD_PWR_IF_SupplyReady */
131 USBPD_StatusTypeDef status = USBPD_ERROR;
132 uint32_t _voltage;
133
134 /* check for valid port */
135 if (!USBPD_PORT_IsValid(PortNum))
136 {
137 return USBPD_ERROR;
138 }
139
140 BSP_USBPD_PWR_VBUSGetVoltage(PortNum, &_voltage);
141 if (USBPD_VSAFE_0V == Vsafe)
142 {
143 /* Vsafe0V */
144 status = ((_voltage < USBPD_PWR_LOW_VBUS_THRESHOLD) ? USBPD_OK : USBPD_ERROR);
145 }
146 else
147 {
148 /* Vsafe5V */
149 status = ((_voltage > USBPD_PWR_HIGH_VBUS_THRESHOLD) ? USBPD_OK : USBPD_ERROR);
150 }
151
152 return status;
153/* USER CODE END USBPD_PWR_IF_SupplyReady */
154}
155
156/**
157 * @brief Reads the voltage and the current on a specified port
158 * @param PortNum Port number
159 * @param pVoltage: The Voltage in mV
160 * @param pCurrent: The Current in mA
161 * @retval USBPD_ERROR or USBPD_OK
162 */
163USBPD_StatusTypeDef USBPD_PWR_IF_ReadVA(uint8_t PortNum, uint16_t *pVoltage, uint16_t *pCurrent)
164{
165/* USER CODE BEGIN USBPD_PWR_IF_ReadVA */
166 return USBPD_ERROR;
167/* USER CODE END USBPD_PWR_IF_ReadVA */
168}
169
170/**
171 * @brief Enables the VConn on the port.
172 * @param PortNum Port number
173 * @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
174 * @retval USBPD status
175 */
176USBPD_StatusTypeDef USBPD_PWR_IF_Enable_VConn(uint8_t PortNum, CCxPin_TypeDef CC)
177{
178/* USER CODE BEGIN USBPD_PWR_IF_Enable_VConn */
179 return USBPD_ERROR;
180/* USER CODE END USBPD_PWR_IF_Enable_VConn */
181}
182
183/**
184 * @brief Disable the VConn on the port.
185 * @param PortNum Port number
186 * @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
187 * @retval USBPD status
188 */
189USBPD_StatusTypeDef USBPD_PWR_IF_Disable_VConn(uint8_t PortNum, CCxPin_TypeDef CC)
190{
191/* USER CODE BEGIN USBPD_PWR_IF_Disable_VConn */
192 return USBPD_ERROR;
193/* USER CODE END USBPD_PWR_IF_Disable_VConn */
194}
195
196/**
197 * @brief Allow PDO data reading from PWR_IF storage.
198 * @param PortNum Port number
199 * @param DataId Type of data to be read from PWR_IF
200 * This parameter can be one of the following values:
201 * @arg @ref USBPD_CORE_DATATYPE_SRC_PDO Source PDO reading requested
202 * @arg @ref USBPD_CORE_DATATYPE_SNK_PDO Sink PDO reading requested
203 * @param Ptr Pointer on address where PDO values should be written (u8 pointer)
204 * @param Size Pointer on nb of u32 written by PWR_IF (nb of PDOs)
205 * @retval None
206 */
207void USBPD_PWR_IF_GetPortPDOs(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId, uint8_t *Ptr, uint32_t *Size)
208{
209 {
210 *Size = PORT0_NB_SINKPDO;
211 memcpy(Ptr,PORT0_PDO_ListSNK, sizeof(uint32_t) * PORT0_NB_SINKPDO);
212 }
213/* USER CODE BEGIN USBPD_PWR_IF_GetPortPDOs */
214
215/* USER CODE END USBPD_PWR_IF_GetPortPDOs */
216}
217
218/**
219 * @brief Find out SRC PDO pointed out by a position provided in a Request DO (from Sink).
220 * @param PortNum Port number
221 * @param RdoPosition RDO Position in list of provided PDO
222 * @param Pdo Pointer on PDO value pointed out by RDO position (u32 pointer)
223 * @retval Status of search
224 * USBPD_OK : Src PDO found for requested DO position (output Pdo parameter is set)
225 * USBPD_FAIL : Position is not compliant with current Src PDO for this port (no corresponding PDO value)
226 */
227USBPD_StatusTypeDef USBPD_PWR_IF_SearchRequestedPDO(uint8_t PortNum, uint32_t RdoPosition, uint32_t *Pdo)
228{
229/* USER CODE BEGIN USBPD_PWR_IF_SearchRequestedPDO */
230 return USBPD_FAIL;
231/* USER CODE END USBPD_PWR_IF_SearchRequestedPDO */
232}
233
234/**
235 * @brief Function called in case of critical issue is detected to switch in safety mode.
236 * @param ErrorType Type of error detected by monitoring (based on @ref USBPD_PWR_IF_ERROR)
237 * @retval None
238 */
239void USBPD_PWR_IF_AlarmType(USBPD_PWR_IF_ERROR ErrorType)
240{
241/* USER CODE BEGIN USBPD_PWR_IF_AlarmType */
242
243/* USER CODE END USBPD_PWR_IF_AlarmType */
244}
245
246/**
247 * @brief the function is called in case of critical issue is detected to switch in safety mode.
248 * @retval None
249 */
250void USBPD_PWR_IF_Alarm()
251{
252/* USER CODE BEGIN USBPD_PWR_IF_Alarm */
253
254/* USER CODE END USBPD_PWR_IF_Alarm */
255}
256
257/**
258 * @brief Function is called to get VBUS power status.
259 * @param PortNum Port number
260 * @param PowerTypeStatus Power type status based on @ref USBPD_VBUSPOWER_STATUS
261 * @retval UBBPD_TRUE or USBPD_FALSE
262 */
263uint8_t USBPD_PWR_IF_GetVBUSStatus(uint8_t PortNum, USBPD_VBUSPOWER_STATUS PowerTypeStatus)
264{
265/* USER CODE BEGIN USBPD_PWR_IF_GetVBUSStatus */
266 uint8_t _status = USBPD_FALSE;
267 uint32_t _vbus = HW_IF_PWR_GetVoltage(PortNum);
268
269 switch(PowerTypeStatus)
270 {
271 case USBPD_PWR_BELOWVSAFE0V :
272 if (_vbus < USBPD_PWR_LOW_VBUS_THRESHOLD) _status = USBPD_TRUE;
273 break;
274 case USBPD_PWR_VSAFE5V :
275 if (_vbus >= USBPD_PWR_HIGH_VBUS_THRESHOLD) _status = USBPD_TRUE;
276 break;
277 case USBPD_PWR_SNKDETACH:
278 if (_vbus < USBPD_PWR_HIGH_VBUS_THRESHOLD) _status = USBPD_TRUE;
279 break;
280 default :
281 break;
282 }
283 return _status;
284/* USER CODE END USBPD_PWR_IF_GetVBUSStatus */
285}
286
287/**
288 * @brief Function is called to set the VBUS threshold when a request has been accepted.
289 * @param PortNum Port number
290 * @retval None
291 */
292void USBPD_PWR_IF_UpdateVbusThreshold(uint8_t PortNum)
293{
294/* USER CODE BEGIN USBPD_PWR_IF_UpdateVbusThreshold */
295/* USER CODE END USBPD_PWR_IF_UpdateVbusThreshold */
296}
297
298/**
299 * @brief Function is called to reset the VBUS threshold when there is a power reset.
300 * @param PortNum Port number
301 * @retval None
302 */
303void USBPD_PWR_IF_ResetVbusThreshold(uint8_t PortNum)
304{
305/* USER CODE BEGIN USBPD_PWR_IF_ResetVbusThreshold */
306/* USER CODE END USBPD_PWR_IF_ResetVbusThreshold */
307}
308
309/* USER CODE BEGIN USBPD_USER_EXPORTED_FUNCTIONS_Definition */
310
311/* USER CODE END USBPD_USER_EXPORTED_FUNCTIONS_Definition */
312/**
313 * @}
314 */
315
316/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Functions
317 * @{
318 */
319/* USER CODE BEGIN USBPD_USER_PRIVATE_FUNCTIONS_Definition */
320
321/* USER CODE END USBPD_USER_PRIVATE_FUNCTIONS_Definition */
322/**
323 * @}
324 */
325
326/**
327 * @}
328 */
329
330/**
331 * @}
332 */
Note: See TracBrowser for help on using the repository browser.