source: trunk/fw_g473rct/USBPD/App/usbpd_pwr_if.c@ 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: 11.8 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 Sets the required power profile, now it works only with Fixed ones
124 * @param PortNum Port number
125 * @retval USBPD status
126*/
127USBPD_StatusTypeDef USBPD_PWR_IF_SetProfile(uint8_t PortNum)
128{
129/* USER CODE BEGIN USBPD_PWR_IF_SetProfile */
130 USBPD_StatusTypeDef _status = USBPD_ERROR;
131 PWR_IF_DEBUG_TRACE(PortNum, "ADVICE: update USBPD_PWR_IF_SetProfile");
132 if (BSP_ERROR_NONE == BSP_USBPD_PWR_VBUSSetVoltage_Fixed(PortNum, 5000, 3000, 3000))
133 {
134 _status = USBPD_OK;
135 }
136
137 return _status;
138/* USER CODE END USBPD_PWR_IF_SetProfile */
139}
140
141/**
142 * @brief Checks if the power on a specified port is ready
143 * @param PortNum Port number
144 * @param Vsafe Vsafe status based on @ref USBPD_VSAFE_StatusTypeDef
145 * @retval USBPD status
146 */
147USBPD_StatusTypeDef USBPD_PWR_IF_SupplyReady(uint8_t PortNum, USBPD_VSAFE_StatusTypeDef Vsafe)
148{
149/* USER CODE BEGIN USBPD_PWR_IF_SupplyReady */
150 USBPD_StatusTypeDef status = USBPD_ERROR;
151 uint32_t _voltage;
152
153 /* check for valid port */
154 if (!USBPD_PORT_IsValid(PortNum))
155 {
156 return USBPD_ERROR;
157 }
158
159 BSP_USBPD_PWR_VBUSGetVoltage(PortNum, &_voltage);
160 if (USBPD_VSAFE_0V == Vsafe)
161 {
162 /* Vsafe0V */
163 status = ((_voltage < USBPD_PWR_LOW_VBUS_THRESHOLD) ? USBPD_OK : USBPD_ERROR);
164 }
165 else
166 {
167 /* Vsafe5V */
168 status = ((_voltage > USBPD_PWR_HIGH_VBUS_THRESHOLD) ? USBPD_OK : USBPD_ERROR);
169 }
170
171 return status;
172/* USER CODE END USBPD_PWR_IF_SupplyReady */
173}
174
175/**
176 * @brief Enables VBUS power on a specified port
177 * @param PortNum Port number
178 * @retval USBPD status
179 */
180USBPD_StatusTypeDef USBPD_PWR_IF_VBUSEnable(uint8_t PortNum)
181{
182/* USER CODE BEGIN USBPD_PWR_IF_VBUSEnable */
183 USBPD_StatusTypeDef _status = (USBPD_StatusTypeDef)HW_IF_PWR_Enable(PortNum, USBPD_ENABLE, CCNONE, USBPD_FALSE, USBPD_PORTPOWERROLE_SRC);
184 return _status;
185/* USER CODE END USBPD_PWR_IF_VBUSEnable */
186}
187
188/**
189 * @brief Disable VBUS/VCONN the power on a specified port
190 * @param PortNum Port number
191 * @retval USBPD status
192 */
193USBPD_StatusTypeDef USBPD_PWR_IF_VBUSDisable(uint8_t PortNum)
194{
195/* USER CODE BEGIN USBPD_PWR_IF_VBUSDisable */
196 USBPD_StatusTypeDef _status = (USBPD_StatusTypeDef)HW_IF_PWR_Enable(PortNum, USBPD_DISABLE, CCNONE, USBPD_FALSE, USBPD_PORTPOWERROLE_SRC);
197 return _status;
198/* USER CODE END USBPD_PWR_IF_VBUSDisable */
199}
200
201/**
202 * @brief Checks if the power on a specified port is enabled
203 * @param PortNum Port number
204 * @retval USBPD_ENABLE or USBPD_DISABLE
205 */
206USBPD_FunctionalState USBPD_PWR_IF_VBUSIsEnabled(uint8_t PortNum)
207{
208 /* Get the Status of the port */
209 return USBPD_PORT_IsValid(PortNum) ? (USBPD_FunctionalState)HW_IF_PWR_VBUSIsEnabled(PortNum) : USBPD_DISABLE;
210}
211
212/**
213 * @brief Reads the voltage and the current on a specified port
214 * @param PortNum Port number
215 * @param pVoltage: The Voltage in mV
216 * @param pCurrent: The Current in mA
217 * @retval USBPD_ERROR or USBPD_OK
218 */
219USBPD_StatusTypeDef USBPD_PWR_IF_ReadVA(uint8_t PortNum, uint16_t *pVoltage, uint16_t *pCurrent)
220{
221/* USER CODE BEGIN USBPD_PWR_IF_ReadVA */
222 return USBPD_ERROR;
223/* USER CODE END USBPD_PWR_IF_ReadVA */
224}
225
226/**
227 * @brief Enables the VConn on the port.
228 * @param PortNum Port number
229 * @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
230 * @retval USBPD status
231 */
232USBPD_StatusTypeDef USBPD_PWR_IF_Enable_VConn(uint8_t PortNum, CCxPin_TypeDef CC)
233{
234/* USER CODE BEGIN USBPD_PWR_IF_Enable_VConn */
235 return USBPD_ERROR;
236/* USER CODE END USBPD_PWR_IF_Enable_VConn */
237}
238
239/**
240 * @brief Disable the VConn on the port.
241 * @param PortNum Port number
242 * @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
243 * @retval USBPD status
244 */
245USBPD_StatusTypeDef USBPD_PWR_IF_Disable_VConn(uint8_t PortNum, CCxPin_TypeDef CC)
246{
247/* USER CODE BEGIN USBPD_PWR_IF_Disable_VConn */
248 return USBPD_ERROR;
249/* USER CODE END USBPD_PWR_IF_Disable_VConn */
250}
251
252/**
253 * @brief Allow PDO data reading from PWR_IF storage.
254 * @param PortNum Port number
255 * @param DataId Type of data to be read from PWR_IF
256 * This parameter can be one of the following values:
257 * @arg @ref USBPD_CORE_DATATYPE_SRC_PDO Source PDO reading requested
258 * @arg @ref USBPD_CORE_DATATYPE_SNK_PDO Sink PDO reading requested
259 * @param Ptr Pointer on address where PDO values should be written (u8 pointer)
260 * @param Size Pointer on nb of u32 written by PWR_IF (nb of PDOs)
261 * @retval None
262 */
263void USBPD_PWR_IF_GetPortPDOs(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId, uint8_t *Ptr, uint32_t *Size)
264{
265 if (DataId == USBPD_CORE_DATATYPE_SRC_PDO)
266 {
267 *Size = PORT0_NB_SOURCEPDO;
268 memcpy(Ptr,PORT0_PDO_ListSRC, sizeof(uint32_t) * PORT0_NB_SOURCEPDO);
269 }
270/* USER CODE BEGIN USBPD_PWR_IF_GetPortPDOs */
271
272/* USER CODE END USBPD_PWR_IF_GetPortPDOs */
273}
274
275/**
276 * @brief Find out SRC PDO pointed out by a position provided in a Request DO (from Sink).
277 * @param PortNum Port number
278 * @param RdoPosition RDO Position in list of provided PDO
279 * @param Pdo Pointer on PDO value pointed out by RDO position (u32 pointer)
280 * @retval Status of search
281 * USBPD_OK : Src PDO found for requested DO position (output Pdo parameter is set)
282 * USBPD_FAIL : Position is not compliant with current Src PDO for this port (no corresponding PDO value)
283 */
284USBPD_StatusTypeDef USBPD_PWR_IF_SearchRequestedPDO(uint8_t PortNum, uint32_t RdoPosition, uint32_t *Pdo)
285{
286/* USER CODE BEGIN USBPD_PWR_IF_SearchRequestedPDO */
287 return USBPD_FAIL;
288/* USER CODE END USBPD_PWR_IF_SearchRequestedPDO */
289}
290
291/**
292 * @brief Function called in case of critical issue is detected to switch in safety mode.
293 * @param ErrorType Type of error detected by monitoring (based on @ref USBPD_PWR_IF_ERROR)
294 * @retval None
295 */
296void USBPD_PWR_IF_AlarmType(USBPD_PWR_IF_ERROR ErrorType)
297{
298/* USER CODE BEGIN USBPD_PWR_IF_AlarmType */
299
300/* USER CODE END USBPD_PWR_IF_AlarmType */
301}
302
303/**
304 * @brief the function is called in case of critical issue is detected to switch in safety mode.
305 * @retval None
306 */
307void USBPD_PWR_IF_Alarm()
308{
309/* USER CODE BEGIN USBPD_PWR_IF_Alarm */
310
311/* USER CODE END USBPD_PWR_IF_Alarm */
312}
313
314/**
315 * @brief Function is called to get VBUS power status.
316 * @param PortNum Port number
317 * @param PowerTypeStatus Power type status based on @ref USBPD_VBUSPOWER_STATUS
318 * @retval UBBPD_TRUE or USBPD_FALSE
319 */
320uint8_t USBPD_PWR_IF_GetVBUSStatus(uint8_t PortNum, USBPD_VBUSPOWER_STATUS PowerTypeStatus)
321{
322/* USER CODE BEGIN USBPD_PWR_IF_GetVBUSStatus */
323 uint8_t _status = USBPD_FALSE;
324 uint32_t _vbus = HW_IF_PWR_GetVoltage(PortNum);
325
326 switch(PowerTypeStatus)
327 {
328 case USBPD_PWR_BELOWVSAFE0V :
329 // Here we must make sure that voltage on Vbus is lower than 750mV,
330 // but we do not monitor Vbus with ADC, so we just assume that voltage is Ok
331 _status = USBPD_TRUE;
332 break;
333 case USBPD_PWR_VSAFE5V :
334 // Here we must make sure that voltage on Vbus is higher than 2.8V,
335 // but we do not monitor Vbus with ADC, so we just assume that voltage is Ok
336 _status = USBPD_TRUE;
337 break;
338 case USBPD_PWR_SNKDETACH:
339 // Here we must make sure that voltage on Vbus is lower than 2.8V,
340 // but we do not monitor Vbus with ADC, so we just assume that voltage is Ok
341 _status = USBPD_TRUE;
342 break;
343 default :
344 break;
345 }
346 return _status;
347/* USER CODE END USBPD_PWR_IF_GetVBUSStatus */
348}
349
350/**
351 * @brief Function is called to set the VBUS threshold when a request has been accepted.
352 * @param PortNum Port number
353 * @retval None
354 */
355void USBPD_PWR_IF_UpdateVbusThreshold(uint8_t PortNum)
356{
357/* USER CODE BEGIN USBPD_PWR_IF_UpdateVbusThreshold */
358/* USER CODE END USBPD_PWR_IF_UpdateVbusThreshold */
359}
360
361/**
362 * @brief Function is called to reset the VBUS threshold when there is a power reset.
363 * @param PortNum Port number
364 * @retval None
365 */
366void USBPD_PWR_IF_ResetVbusThreshold(uint8_t PortNum)
367{
368/* USER CODE BEGIN USBPD_PWR_IF_ResetVbusThreshold */
369/* USER CODE END USBPD_PWR_IF_ResetVbusThreshold */
370}
371
372/* USER CODE BEGIN USBPD_USER_EXPORTED_FUNCTIONS_Definition */
373
374/* USER CODE END USBPD_USER_EXPORTED_FUNCTIONS_Definition */
375/**
376 * @}
377 */
378
379/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Functions
380 * @{
381 */
382/* USER CODE BEGIN USBPD_USER_PRIVATE_FUNCTIONS_Definition */
383
384/* USER CODE END USBPD_USER_PRIVATE_FUNCTIONS_Definition */
385/**
386 * @}
387 */
388
389/**
390 * @}
391 */
392
393/**
394 * @}
395 */
Note: See TracBrowser for help on using the repository browser.