source: trunk/fw_g473rct/USBPD/App/usbpd_pwr_if.h@ 65

Last change on this file since 65 was 65, checked in by f.jahn, 5 weeks ago
  • USB-PD function was implemented;
  • HW parity check on RAM was activated;
  • USB Device CDC mode was activated.
File size: 16.2 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file usbpd_pwr_if.h
5 * @author MCD Application Team
6 * @brief This file contains the headers of usbpd_pw_if.h.
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#ifndef __USBPD_PW_IF_H_
22#define __USBPD_PW_IF_H_
23
24#ifdef __cplusplus
25 extern "C" {
26#endif
27
28/* Includes ------------------------------------------------------------------*/
29#include "usbpd_def.h"
30
31/** @addtogroup STM32_USBPD_APPLICATION
32 * @{
33 */
34
35/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF
36 * @{
37 */
38
39/* Exported typedef ----------------------------------------------------------*/
40/* Exported define -----------------------------------------------------------*/
41/* Exported constants --------------------------------------------------------*/
42/* Exported macro ------------------------------------------------------------*/
43/** @defgroup USBPD_USER_PWR_IF_Exported_Macros USBPD PWR IF Exported Macros
44 * @{
45 */
46
47/* enumeration of the different power status available for VBUS */
48typedef enum
49{
50 USBPD_PWR_BELOWVSAFE0V,
51 USBPD_PWR_VSAFE5V,
52 USBPD_PWR_SNKDETACH
53} USBPD_VBUSPOWER_STATUS;
54
55/* Enumeration of the different errors detected by power IF */
56typedef enum{
57 USBPD_PWR_IF_OTHER = 0U,
58 USBPD_PWR_IF_NMI = 2U,
59 USBPD_PWR_IF_HARD_FAULT = 3U,
60 USBPD_PWR_IF_OVER_CURRENT = 4U,
61} USBPD_PWR_IF_ERROR;
62
63/* Macros used to convert values into PDO representation */
64#define PWR_V_20MV(_V_) ((uint16_t)(( (_V_) * 1000) / 20)) /* From Volt to 20mV multiples */
65#define PWR_V_50MV(_V_) ((uint16_t)(( (_V_) * 1000) / 50)) /* From Volt to 50mV multiples */
66#define PWR_V_100MV(_V_) ((uint16_t)(( (_V_) * 1000) / 100)) /* From Volt to 100mV multiples */
67#define PWR_A_10MA(_A_) ((uint16_t)(( (_A_) * 1000) / 10)) /* From Ampere to 10mA multiples */
68#define PWR_A_50MA(_A_) ((uint16_t)(( (_A_) * 1000) / 50)) /* From Ampere to 50mA multiples */
69#define PWR_W(_W_) ((uint16_t)(( (_W_) * 1000) / 250)) /* From Watt to 250mW multiples */
70
71/* Macros used to get values from PDO representation */
72#define PWR_DECODE_50MV(_Value_) ((uint16_t)(((_Value_) * 50))) /* From 50mV multiples to mV */
73#define PWR_DECODE_100MV(_Value_) ((uint16_t)(((_Value_) * 100))) /* From 100mV multiples to mV */
74#define PWR_DECODE_10MA(_Value_) ((uint16_t)(((_Value_) * 10))) /* From 10mA multiples to mA */
75#define PWR_DECODE_50MA(_Value_) ((uint16_t)(((_Value_) * 50))) /* From 50mA multiples to mA */
76#define PWR_DECODE_MW(_Value_) ((uint16_t)(((_Value_) * 250))) /* From 250mW multiples to mW */
77
78/* Macros used to describe the parameters in a PDO list */
79/* ---------------------------- FIXED --------------------------- */
80
81/* Set voltage in mV in SRC Fixed PDO */
82#define USBPD_PDO_SRC_FIXED_SET_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
83 << USBPD_PDO_SRC_FIXED_VOLTAGE_Pos) \
84 & (USBPD_PDO_SRC_FIXED_VOLTAGE_Msk))
85
86/* Set max current in mA in SRC Fixed PDO */
87#define USBPD_PDO_SRC_FIXED_SET_MAX_CURRENT(_MA_) (((PWR_A_10MA((_MA_) / 1000.0)) \
88 << USBPD_PDO_SRC_FIXED_MAX_CURRENT_Pos) \
89 & (USBPD_PDO_SRC_FIXED_MAX_CURRENT_Msk))
90
91/* Set voltage in mV in SNK Fixed PDO */
92#define USBPD_PDO_SNK_FIXED_SET_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
93 << USBPD_PDO_SNK_FIXED_VOLTAGE_Pos) \
94 & (USBPD_PDO_SNK_FIXED_VOLTAGE_Msk))
95
96/* Set operating current in mA in SNK Fixed PDO */
97#define USBPD_PDO_SNK_FIXED_SET_OP_CURRENT(_MA_) (((PWR_A_10MA((_MA_) / 1000.0)) \
98 << USBPD_PDO_SNK_FIXED_OP_CURRENT_Pos) \
99 & (USBPD_PDO_SNK_FIXED_OP_CURRENT_Msk))
100
101/* ---------------------------- VARIABLE ------------------------ */
102
103/* Set max voltage in mV in SRC VARIABLE PDO */
104#define USBPD_PDO_SRC_VARIABLE_SET_MAX_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
105 << USBPD_PDO_SRC_VARIABLE_MAX_VOLTAGE_Pos) \
106 & (USBPD_PDO_SRC_VARIABLE_MAX_VOLTAGE_Msk))
107
108/* Set min voltage in mV in SRC VARIABLE PDO */
109#define USBPD_PDO_SRC_VARIABLE_SET_MIN_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
110 << USBPD_PDO_SRC_VARIABLE_MIN_VOLTAGE_Pos) \
111 & (USBPD_PDO_SRC_VARIABLE_MIN_VOLTAGE_Msk))
112
113/* Set max current in mA in SRC VARIABLE PDO */
114#define USBPD_PDO_SRC_VARIABLE_SET_MAX_CURRENT(_MA_) (((PWR_A_10MA((_MA_) / 1000.0)) \
115 << USBPD_PDO_SRC_VARIABLE_MAX_CURRENT_Pos) \
116 & (USBPD_PDO_SRC_VARIABLE_MAX_CURRENT_Msk))
117
118/* Set max voltage in mV in SNK VARIABLE PDO */
119#define USBPD_PDO_SNK_VARIABLE_SET_MAX_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
120 << USBPD_PDO_SNK_VARIABLE_MAX_VOLTAGE_Pos) \
121 & (USBPD_PDO_SNK_VARIABLE_MAX_VOLTAGE_Msk))
122
123/* Set min voltage in mV in SNK VARIABLE PDO */
124#define USBPD_PDO_SNK_VARIABLE_SET_MIN_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
125 << USBPD_PDO_SNK_VARIABLE_MIN_VOLTAGE_Pos) \
126 & (USBPD_PDO_SNK_VARIABLE_MIN_VOLTAGE_Msk))
127
128/* Set operating current in mA in SNK VARIABLE */
129#define USBPD_PDO_SNK_VARIABLE_SET_OP_CURRENT(_MA_) (((PWR_A_10MA((_MA_) / 1000.0)) \
130 << USBPD_PDO_SNK_VARIABLE_OP_CURRENT_Pos) \
131 & (USBPD_PDO_SNK_VARIABLE_OP_CURRENT_Msk))
132
133/* ---------------------------- BATTERY ------------------------ */
134
135/* Set max voltage in mV in SRC BATTERY PDO */
136#define USBPD_PDO_SRC_BATTERY_SET_MAX_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
137 << USBPD_PDO_SRC_BATTERY_MAX_VOLTAGE_Pos) \
138 & (USBPD_PDO_SRC_BATTERY_MAX_VOLTAGE_Msk))
139
140/* Set min voltage in mV in SRC BATTERY PDO */
141#define USBPD_PDO_SRC_BATTERY_SET_MIN_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
142 << USBPD_PDO_SRC_BATTERY_MIN_VOLTAGE_Pos) \
143 & (USBPD_PDO_SRC_BATTERY_MIN_VOLTAGE_Msk))
144
145/* Set max power in mW in SRC BATTERY PDO */
146#define USBPD_PDO_SRC_BATTERY_SET_MAX_POWER(_MA_) (((PWR_W((_MA_) / 1000.0)) \
147 << USBPD_PDO_SRC_BATTERY_MAX_POWER_Pos) \
148 & (USBPD_PDO_SRC_BATTERY_MAX_POWER_Msk))
149
150/* Set max voltage in mV in SNK BATTERY PDO */
151#define USBPD_PDO_SNK_BATTERY_SET_MAX_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
152 << USBPD_PDO_SNK_BATTERY_MAX_VOLTAGE_Pos) \
153 & (USBPD_PDO_SNK_BATTERY_MAX_VOLTAGE_Msk))
154
155/* Set min voltage in mV in SNK BATTERY PDO */
156#define USBPD_PDO_SNK_BATTERY_SET_MIN_VOLTAGE(_MV_) (((PWR_V_50MV((_MV_) / 1000.0)) \
157 << USBPD_PDO_SNK_BATTERY_MIN_VOLTAGE_Pos) \
158 & (USBPD_PDO_SNK_BATTERY_MIN_VOLTAGE_Msk))
159
160/* Set operating power in mW in SNK BATTERY PDO */
161#define USBPD_PDO_SNK_BATTERY_SET_OP_POWER(_MA_) (((PWR_W((_MA_) / 1000.0)) \
162 << USBPD_PDO_SNK_BATTERY_OP_POWER_Pos) \
163 & (USBPD_PDO_SNK_BATTERY_OP_POWER_Msk))
164
165/* ---------------------------- APDO ---------------------------- */
166
167/* Set min voltage in mV in SRC APDO */
168#define USBPD_PDO_SRC_APDO_SET_MIN_VOLTAGE(_MV_) (((PWR_V_100MV((_MV_) / 1000.0)) \
169 << USBPD_PDO_SRC_APDO_MIN_VOLTAGE_Pos) \
170 & (USBPD_PDO_SRC_APDO_MIN_VOLTAGE_Msk))
171
172/* Set max voltage in mV in SRC APDO */
173#define USBPD_PDO_SRC_APDO_SET_MAX_VOLTAGE(_MV_) (((PWR_V_100MV((_MV_) / 1000.0)) \
174 << USBPD_PDO_SRC_APDO_MAX_VOLTAGE_Pos) \
175 & (USBPD_PDO_SRC_APDO_MAX_VOLTAGE_Msk))
176
177/* Set max current in mA in SRC APDO */
178#define USBPD_PDO_SRC_APDO_SET_MAX_CURRENT(_MA_) (((PWR_A_50MA((_MA_) / 1000.0)) \
179 << USBPD_PDO_SRC_APDO_MAX_CURRENT_Pos) \
180 & (USBPD_PDO_SRC_APDO_MAX_CURRENT_Msk))
181
182/* Set min voltage in mV in SNK APDO */
183#define USBPD_PDO_SNK_APDO_SET_MIN_VOLTAGE(_MV_) (((PWR_V_100MV((_MV_) / 1000.0)) \
184 << USBPD_PDO_SNK_APDO_MIN_VOLTAGE_Pos) \
185 & (USBPD_PDO_SNK_APDO_MIN_VOLTAGE_Msk))
186
187/* Set max voltage in mV in SNK APDO */
188#define USBPD_PDO_SNK_APDO_SET_MAX_VOLTAGE(_MV_) (((PWR_V_100MV((_MV_) / 1000.0)) \
189 << USBPD_PDO_SNK_APDO_MAX_VOLTAGE_Pos) \
190 & (USBPD_PDO_SNK_APDO_MAX_VOLTAGE_Msk))
191
192/* Set max current in mA in SNK APDO */
193#define USBPD_PDO_SNK_APDO_SET_MAX_CURRENT(_MA_) (((PWR_A_50MA((_MA_) / 1000.0)) \
194 << USBPD_PDO_SNK_APDO_MAX_CURRENT_Pos) \
195 & (USBPD_PDO_SNK_APDO_MAX_CURRENT_Msk))
196
197#define USBPD_PORT_IsValid(__Port__) ((__Port__) < (USBPD_PORT_COUNT))
198
199/**
200 * @}
201 */
202
203/* Exported variables --------------------------------------------------------*/
204/* USER CODE BEGIN variables */
205
206/* USER CODE END variables */
207/* Exported functions --------------------------------------------------------*/
208/** @defgroup STM32_USBPD_APPLICATION_POWER_IF_Exported_Functions USBPD PWR IF Exported Functions
209 * @{
210 */
211/**
212 * @brief Initialize structures and variables related to power board profiles
213 * used by Sink and Source, for all available ports.
214 * @retval USBPD status
215 */
216USBPD_StatusTypeDef USBPD_PWR_IF_Init(void);
217
218/**
219 * @brief Sets the required power profile
220 * @param PortNum Port number
221 * @retval USBPD status
222 */
223USBPD_StatusTypeDef USBPD_PWR_IF_SetProfile(uint8_t PortNum);
224
225/**
226 * @brief Checks if the power on a specified port is ready
227 * @param PortNum Port number
228 * @param Vsafe Vsafe status based on @ref USBPD_VSAFE_StatusTypeDef
229 * @retval USBPD status
230 */
231USBPD_StatusTypeDef USBPD_PWR_IF_SupplyReady(uint8_t PortNum, USBPD_VSAFE_StatusTypeDef Vsafe);
232
233/**
234 * @brief Enable VBUS power on a specified port
235 * @param PortNum Port number
236 * @retval USBPD status
237 */
238USBPD_StatusTypeDef USBPD_PWR_IF_VBUSEnable(uint8_t PortNum);
239
240/**
241 * @brief Disable VBUS power on a specified port
242 * @param PortNum Port number
243 * @retval USBPD status
244 */
245USBPD_StatusTypeDef USBPD_PWR_IF_VBUSDisable(uint8_t PortNum);
246
247/**
248 * @brief Checks if the power on a specified port is enabled
249 * @param PortNum Port number
250 * @retval ENABLE or DISABLE
251 */
252USBPD_FunctionalState USBPD_PWR_IF_VBUSIsEnabled(uint8_t PortNum);
253
254/**
255 * @brief Reads the voltage and the current on a specified port
256 * @param PortNum Port number
257 * @param pVoltage The Voltage in mV
258 * @param pCurrent The Current in mA
259 * @retval ENABLE or DISABLE
260 */
261USBPD_StatusTypeDef USBPD_PWR_IF_ReadVA(uint8_t PortNum, uint16_t *pVoltage, uint16_t *pCurrent);
262
263/**
264 * @brief Enables the VConn on the port.
265 * @param PortNum Port number
266 * @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
267 * @retval USBPD Status
268 */
269USBPD_StatusTypeDef USBPD_PWR_IF_Enable_VConn(uint8_t PortNum, CCxPin_TypeDef CC);
270
271/**
272 * @brief Disable the VConn on the port.
273 * @param PortNum Port number
274 * @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
275 * @retval USBPD Status
276 */
277USBPD_StatusTypeDef USBPD_PWR_IF_Disable_VConn(uint8_t PortNum, CCxPin_TypeDef CC);
278
279/**
280 * @brief Allow PDO data reading from PWR_IF storage.
281 * @param PortNum Port number
282 * @param DataId Type of data to be read from PWR_IF
283 * This parameter can be one of the following values:
284 * @arg @ref USBPD_CORE_DATATYPE_SRC_PDO Source PDO reading requested
285 * @arg @ref USBPD_CORE_DATATYPE_SNK_PDO Sink PDO reading requested
286 * @param Ptr Pointer on address where PDO values should be written (u8 pointer)
287 * @param Size Pointer on nb of u32 written by PWR_IF (nb of PDOs)
288 * @retval None
289 */
290void USBPD_PWR_IF_GetPortPDOs(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId, uint8_t *Ptr, uint32_t *Size);
291
292/**
293 * @brief Find out SRC PDO pointed out by a position provided in a Request DO (from Sink).
294 * @param PortNum Port number
295 * @param RdoPosition RDO Position in list of provided PDO
296 * @param Pdo Pointer on PDO value pointed out by RDO position (u32 pointer)
297 * @retval Status of search
298 * USBPD_OK : Src PDO found for requested DO position (output Pdo parameter is set)
299 * USBPD_FAIL : Position is not compliant with current Src PDO for this port (no corresponding PDO value)
300 */
301USBPD_StatusTypeDef USBPD_PWR_IF_SearchRequestedPDO(uint8_t PortNum, uint32_t RdoPosition, uint32_t *Pdo);
302
303/**
304 * @brief Function called in case of critical issue is detected to switch in safety mode.
305 * @param ErrorType Type of error detected by monitoring (based on @ref USBPD_PWR_IF_ERROR)
306 * @retval None
307 */
308void USBPD_PWR_IF_AlarmType(USBPD_PWR_IF_ERROR ErrorType);
309
310/**
311 * @brief Function called in case of critical issue is detected to switch in safety mode.
312 * @retval None
313 */
314void USBPD_PWR_IF_Alarm(void);
315
316/**
317 * @brief Function is called to get VBUS power status.
318 * @param PortNum Port number
319 * @param PowerTypeStatus Power type status based on @ref USBPD_VBUSPOWER_STATUS
320 * @retval UBBPD_TRUE or USBPD_FALSE
321 */
322uint8_t USBPD_PWR_IF_GetVBUSStatus(uint8_t PortNum, USBPD_VBUSPOWER_STATUS PowerTypeStatus);
323
324/**
325 * @brief Function is called to set the VBUS threshold when a request has been accepted.
326 * @param PortNum Port number
327 * @retval None
328 */
329void USBPD_PWR_IF_UpdateVbusThreshold(uint8_t PortNum);
330
331/**
332 * @brief Function is called to reset the VBUS threshold when there is a power reset.
333 * @param PortNum Port number
334 * @retval None
335 */
336void USBPD_PWR_IF_ResetVbusThreshold(uint8_t PortNum);
337
338/* USER CODE BEGIN Exported Functions */
339
340/* USER CODE END Exported Functions */
341/**
342 * @}
343 */
344
345/**
346 * @}
347 */
348
349/**
350 * @}
351 */
352
353/**
354 * @}
355 */
356
357#ifdef __cplusplus
358}
359#endif
360
361#endif /* __USBPD_PW_IF_H_ */
362
Note: See TracBrowser for help on using the repository browser.