embedded code copied from jankae/VNA and adjusted for STM32G4

This commit is contained in:
Jan Käberich 2020-08-24 19:06:50 +02:00
parent 7af204b349
commit 30d4ebe37b
215 changed files with 186208 additions and 0 deletions

View file

@ -0,0 +1,939 @@
/**
******************************************************************************
* @file usbpd_core.h
* @author MCD Application Team
* @brief This file contains the core stack API.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#ifndef __USBPD_CORE_H_
#define __USBPD_CORE_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbpd_def.h"
#if defined(USBPDCORE_TCPM_SUPPORT)
#include "tcpc.h"
#endif /* USBPDCORE_TCPM_SUPPORT */
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_CORE
* @{
*/
/** @addtogroup USBPD_CORE_CAD
* @{
*/
/* Exported types ------------------------------------------------------------*/
/** @defgroup USBPD_CORE_CAD_Exported_Types USBPD CORE CAD Exported Types
* @{
*/
/**
* @brief funtion return value @ref USBPD_CORE_CAD
* @{
*/
typedef enum
{
USBPD_CAD_OK, /*!< USBPD CAD Status OK */
USBPD_CAD_INVALID_PORT, /*!< USBPD CAD Status INVALID PORT */
USBPD_CAD_ERROR, /*!< USBPD CAD Status ERROR */
USBPD_CAD_MALLOCERROR, /*!< USBPD CAD Status ERROR MALLOC */
USBPD_CAD_INVALIDRESISTOR /*!< USBPD CAD Status INVALID RESISTOR */
}
USBPD_CAD_StatusTypeDef;
/**
* @}
*/
/**
* @brief activation value @ref USBPD_CORE_CAD
* @{
*/
typedef enum
{
USBPD_CAD_DISABLE, /*!< USBPD CAD activation status Disable */
USBPD_CAD_ENABLE /*!< USBPD CAD activation status Enable */
} USBPD_CAD_activation;
/**
* @}
*/
/**
* @brief CallBacks exposed by the @ref USBPD_CORE_CAD
*/
typedef struct
{
/**
* @brief CallBack reporting events on a specified port.
* @param PortNum The handle of the port
* @param State CAD state
* @param Cc The Communication Channel for the USBPD communication
* @retval None
*/
void (*USBPD_CAD_CallbackEvent)(uint8_t PortNum, USBPD_CAD_EVENT State, CCxPin_TypeDef Cc);
/**
* @brief CallBack to wakeup the CAD.
* @retval None
*/
void (*USBPD_CAD_WakeUp)(void);
}USBPD_CAD_Callbacks;
/**
* @}
*/
/** @defgroup USBPD_CORE_CAD_Exported_Functions_Grp1 USBPD CORE CAD Exported Functions
* @{
*/
/**
* @brief Initialize the CAD module for a specified port.
* @param PortNum Index of current used port
* @param CallbackFunctions CAD port callback function
* @param Settings Pointer on @ref USBPD_SettingsTypeDef structure
* @param Params Pointer on @ref USBPD_ParamsTypeDef structure
* @retval USBPD_CAD status
*/
USBPD_CAD_StatusTypeDef USBPD_CAD_Init(uint8_t PortNum, const USBPD_CAD_Callbacks *CallbackFunctions, USBPD_SettingsTypeDef *Settings, USBPD_ParamsTypeDef *Params);
/**
* @brief Main CAD task.
* @retval Timing in ms
*/
uint32_t USBPD_CAD_Process(void);
/**
* @brief Enable or Disable CAD port.
* @param PortNum Index of current used port
* @param State The new state of the port
* @retval None
*/
void USBPD_CAD_PortEnable(uint8_t PortNum, USBPD_CAD_activation State);
/**
* @brief Set as SNK.
* @param PortNum Index of current used port
* @retval None
*/
void USBPD_CAD_AssertRd(uint8_t PortNum);
/**
* @brief Set as SRC.
* @param PortNum Index of current used port
* @retval None
*/
void USBPD_CAD_AssertRp(uint8_t PortNum);
/**
* @brief Set type C state machine in error recovery state
* @param PortNum Index of current used port
* @retval None
*/
void USBPD_CAD_EnterErrorRecovery(uint8_t PortNum);
/**
* @brief Set the default Rd resistor
* @param PortNum Index of current used port
* @param RdValue
* @retval USBPD_CAD_OK or USBPD_CAD_INVALIDRESISTOR
*/
USBPD_CAD_StatusTypeDef USBPD_CAD_SetRpResistor(uint8_t PortNum, CAD_RP_Source_Current_Adv_Typedef RdValue);
/**
* @}
*/
/**
* @}
*/
/** @addtogroup USBPD_CORE_TRACE
* @{
*/
/** @defgroup USBPD_CORE_TRACE_Exported_Types USBPD CORE TRACE Exported Types
* @{
*/
typedef enum {
USBPD_TRACE_FORMAT_TLV = 0,
USBPD_TRACE_MESSAGE_IN = 1,
USBPD_TRACE_MESSAGE_OUT = 2,
USBPD_TRACE_CADEVENT = 3,
USBPD_TRACE_PE_STATE = 4,
USBPD_TRACE_CAD_LOW = 5,
USBPD_TRACE_DEBUG = 6,
USBPD_TRACE_SRC = 7,
USBPD_TRACE_SNK = 8,
USBPD_TRACE_NOTIF = 9,
USBPD_TRACE_POWER =10
}
TRACE_EVENT;
typedef void (*TRACE_ENTRY_POINT)(TRACE_EVENT type, uint8_t port, uint8_t sop, uint8_t *ptr, uint32_t size);
extern uint32_t Crc;
extern TRACE_ENTRY_POINT USBPD_Trace;
/**
* @}
*/
/**
* @}
*/
/** @addtogroup USBPD_CORE_PE
* @{
*/
#if defined(USBPDCORE_SVDM) || defined(USBPDCORE_UVDM) || defined(USBPDCORE_CABLE)
/** @defgroup USBPD_CORE_VDM_Exported_Callback USBPD CORE VDM Exported Callback
* @{
*/
/**
* @brief CallBacks exposed by the @ref USBPD_CORE_PE
* */
typedef struct
{
#if defined(USBPDCORE_SVDM) || defined(USBPDCORE_CABLE)
/**
* @brief VDM Discovery identity callback (answer to Discover Identity message)
* @param PortNum current port number
* @param pIdentity Pointer on USBPD_DiscoveryIdentity_TypeDef structure
* @retval USBPD status : USBPD_ACK or USBPD_BUSY
*/
USBPD_StatusTypeDef (*USBPD_VDM_DiscoverIdentity)(uint8_t PortNum, USBPD_DiscoveryIdentity_TypeDef *pIdentity);
/**
* @brief VDM Discover SVID callback (retrieve SVID supported by device for answer to Discovery mode)
* @param PortNum current port number
* @param p_SVID_Info Pointer on USBPD_SVIDInfo_TypeDef structure
* @retval USBPD status USBPD_BUSY or USBPD_ACK or USBPD_NAK
*/
USBPD_StatusTypeDef (*USBPD_VDM_DiscoverSVIDs)(uint8_t PortNum, uint16_t **p_SVID_Data, uint8_t *nbSVID);
/**
* @brief VDM Discover Mode callback (report all the modes supported by SVID)
* @param PortNum current port number
* @param SVID SVID ID
* @param p_ModeTab Pointer on the mode value
* @param NumberOfMode Number of mode available
* @retval USBPD status
*/
USBPD_StatusTypeDef (*USBPD_VDM_DiscoverModes)(uint8_t PortNum, uint16_t SVID, uint32_t **p_ModeTab, uint8_t *NumberOfMode);
/**
* @brief VDM Mode enter callback
* @param PortNum current port number
* @param SVID SVID ID
* @param ModeIndex Index of the mode to be entered
* @retval USBPD status USBPD_ACK/USBPD_NAK
*/
USBPD_StatusTypeDef (*USBPD_VDM_ModeEnter)(uint8_t PortNum, uint16_t SVID, uint32_t ModeIndex);
/**
* @brief VDM Mode exit callback
* @param PortNum current port number
* @param SVID SVID ID
* @param ModeIndex Index of the mode to be exited
* @retval USBPD status USBPD_ACK/USBPD_NAK
*/
USBPD_StatusTypeDef (*USBPD_VDM_ModeExit)(uint8_t PortNum, uint16_t SVID, uint32_t ModeIndex);
/**
* @brief Inform identity callback (Identity information received in Discovery identity answer)
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param pIdentity Pointer on the discovery identity information based on @ref USBPD_DiscoveryIdentity_TypeDef
* @retval USBPD status
*/
void (*USBPD_VDM_InformIdentity)(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_DiscoveryIdentity_TypeDef *pIdentity);
/**
* @brief Inform SVID callback
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param pListSVID Pointer of list of SVID based on @ref USBPD_SVIDInfo_TypeDef
* @retval USBPD status
*/
void (*USBPD_VDM_InformSVID)(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_SVIDInfo_TypeDef *pListSVID);
/**
* @brief Inform Mode callback
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param pModesInfo Pointer of Modes info based on @ref USBPD_ModeInfo_TypeDef
* @retval None
*/
void (*USBPD_VDM_InformMode)(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_ModeInfo_TypeDef *pModesInfo);
/**
* @brief Inform Mode enter callback
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param SVID SVID ID
* @param ModeIndex Index of the mode to be entered
* @retval None
*/
void (*USBPD_VDM_InformModeEnter)(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, uint16_t SVID, uint32_t ModeIndex);
/**
* @brief Inform Mode exit callback
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param SVID SVID ID
* @param ModeIndex Index of the mode to be exited
* @retval None
*/
void (*USBPD_VDM_InformModeExit)(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, uint16_t SVID, uint32_t ModeIndex);
/**
* @brief Send VDM Attention message callback
* @param PortNum current port number
* @param pNbData Pointer of number of VDO to send
* @param pVDO Pointer of VDO to send
* @retval None
*/
void (*USBPD_VDM_SendAttention)(uint8_t PortNum, uint8_t *pNbData, uint32_t *pVDO);
/**
* @brief Receive VDM Attention callback
* @param PortNum current port number
* @param NbData Number of received VDO
* @param VDO Received VDO
* @retval None
*/
void (*USBPD_VDM_ReceiveAttention)(uint8_t PortNum, uint8_t NbData, uint32_t VDO);
/**
* @brief VDM Send Specific message callback
* @param PortNum current port number
* @param VDMCommand VDM command based on @ref USBPD_VDM_Command_Typedef
* @param pNbData Pointer of number of VDO to send
* @param pVDO Pointer of VDO to send
* @retval None
*/
void (*USBPD_VDM_SendSpecific)(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *pNbData, uint32_t *pVDO);
/**
* @brief VDM Receive Specific message callback
* @param PortNum Current port number
* @param VDMCommand VDM command based on @ref USBPD_VDM_Command_Typedef
* @param pNbData Pointer of number of received VDO and used for the answer
* @param pVDO Pointer of received VDO and use for the answer
* @retval status
*/
USBPD_StatusTypeDef (*USBPD_VDM_ReceiveSpecific)(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *pNbData, uint32_t *pVDO);
/**
* @brief VDM Specific message callback to inform user of reception of VDM specific message
* @param PortNum current port number
* @param VDMCommand VDM command based on @ref USBPD_VDM_Command_Typedef
* @param pNbData Pointer of number of received VDO
* @param pVDO Pointer of received VDO
* @retval None
*/
void (*USBPD_VDM_InformSpecific)(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *pNbData, uint32_t *pVDO);
#endif /* USBPDCORE_SVDM || defined(USBPDCORE_CABLE)*/
#if defined(USBPDCORE_UVDM)
/**
* @brief VDM Send Unstructured message callback
* @param PortNum current port number
* @param pUVDM_Header Pointer on UVDM header based on @ref USBPD_UVDMHeader_TypeDef
* @param pNbData Pointer of number of VDO to send
* @param pVDO Pointer of VDO to send
* @retval None
*/
void (*USBPD_VDM_SendUVDM)(uint8_t PortNum, USBPD_UVDMHeader_TypeDef *pUVDM_Header, uint8_t *pNbData, uint32_t *pVDO);
/**
* @brief Unstructured VDM message callback to inform user of reception of UVDM message
* @param PortNum current port number
* @param UVDM_Header UVDM header based on @ref USBPD_UVDMHeader_TypeDef
* @param pNbData Pointer of number of received VDO
* @param pVDO Pointer of received VDO
* @retval status
*/
USBPD_StatusTypeDef (*USBPD_VDM_ReceiveUVDM)(uint8_t PortNum, USBPD_UVDMHeader_TypeDef UVDM_Header, uint8_t *pNbData, uint32_t *pVDO);
#endif /* USBPDCORE_UVDM */
}USBPD_VDM_Callbacks;
/**
* @}
*/
#endif /* USBPDCORE_SVDM || USBPDCORE_UVDM || USBPDCORE_CABLE */
/** @defgroup USBPD_CORE_PE_Exported_TypesDefinitions USBPD CORE PE Exported Types Definitions
* @{
*/
/** @defgroup PE_CallBacks_structure_definition PE CallBacks structure definition
* @brief PE CallBacks exposed by the PE to the DMP
* @{
*/
typedef struct
{
/**
* @brief Request the DPM to setup the new power level.
* @param PortNum Port number
* @retval USBPD_ACCEPT, USBPD_REJECT, USBPD_WAIT
*/
USBPD_StatusTypeDef (*USBPD_PE_RequestSetupNewPower)(uint8_t PortNum);
/**
* @brief Inform the DPM to HardReset status.
* @param PortNum Port number
* @param Role of the board
* @param Status HR Status update
* @retval None
*/
void (*USBPD_PE_HardReset)(uint8_t PortNum, USBPD_PortPowerRole_TypeDef CurrentRole, USBPD_HR_Status_TypeDef Status);
/**
* @brief Get evaluation of swap request from DPM.
* @param PortNum Port number
* @retval USBPD_ACCEPT, USBPD_WAIT, USBPD_REJECT
*/
USBPD_StatusTypeDef (*USBPD_PE_EvaluatPRSwap)(uint8_t PortNum);
/**
* @brief Callback to be used by PE to notify a generic event, clear for the specific DPM
* @param PortNum Port number
* @param EventVal @ref USBPD_NotifyEventValue_TypeDef
* @retval none
*/
void (*USBPD_PE_Notify)(uint8_t PortNum, USBPD_NotifyEventValue_TypeDef EventVal);
/**
* @brief Inform DPM that an extended message has been received.
* @param PortNum port number value
* @param MsgId extended message id
* @param ptrData : pointer on the data
* @param DataSize : size of the data
* @retval None
*/
void (*USBPD_PE_ExtendedMessage)(uint8_t PortNum, USBPD_ExtendedMsg_TypeDef MsgId, uint8_t *ptrData, uint16_t DataSize);
/**
* @brief Allow PE to retrieve information from DPM/PWR_IF.
* @param PortNum Port number
* @param DataId Type of data to be read from DPM based on @ref USBPD_CORE_DataInfoType_TypeDef
* @param Ptr Pointer on address where DPM data should be written (u8 pointer)
* @param Size Pointer on nb of bytes written by DPM
* @retval None
*/
void (*USBPD_PE_GetDataInfo)(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId , uint8_t *Ptr, uint32_t *Size);
/**
* @brief Allow PE to update information in DPM/PWR_IF.
* @param PortNum Port number
* @param DataId Type of data to be read from DPM based on @ref USBPD_CORE_DataInfoType_TypeDef
* @param Ptr Pointer on address where DPM data to be updated could be read (u8 pointer)
* @param Size Nb of byte to be updated in DPM
* @retval None
*/
void (*USBPD_PE_SetDataInfo)(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId , uint8_t *Ptr, uint32_t Size);
/**
* @brief Callback to be used by PE to evaluate a Request from Sink
* @param PortNum Port number
* @param PtrPowerObject Pointer on the power data object
* @retval USBPD_ACCEPT, USBPD_REJECT, USBPD_WAIT, USBPD_GOTOMIN
*/
USBPD_StatusTypeDef (*USBPD_PE_SRC_EvaluateRequest)(uint8_t PortNum, USBPD_CORE_PDO_Type_TypeDef *PtrPowerObject);
/**
* @brief Callback to be used by PE to evaluate a Source Capabilities from Source
* @param PortNum Port number
* @param PtrRequestData Pointer on selected request data object
* @param PtrPowerObject Pointer on the power data object
* @retval None
*/
void (*USBPD_PE_SNK_EvaluateCapabilities)(uint8_t PortNum, uint32_t *RequestData, USBPD_CORE_PDO_Type_TypeDef *PtrPowerObject);
/**
* @brief Callback to be used during power role swap
* @param PortNum Port number
* @param Role of the board
* @param Status HR Status update
* @retval None
*/
void (*USBPD_PE_PowerRoleSwap)(uint8_t PortNum, USBPD_PortPowerRole_TypeDef CurrentRole, USBPD_PRS_Status_TypeDef Status);
/**
* @brief Callback to be used by PE to wakeup the state machine
* @param PortNum Port number
* @retval None
*/
void (*USBPD_PE_WakeupCallback)(uint8_t PortNum);
/**
* @brief Callback to be used by PE to evaluate a Vconn swap
* @param PortNum Port number
* @retval USBPD_ACCEPT, USBPD_REJECT, USBPD_WAIT
*/
USBPD_StatusTypeDef (*USBPD_PE_EvaluateVconnSwap)(uint8_t PortNum);
/**
* @brief Callback to be used by PE to manage VConn
* @param PortNum Port number
* @param State Enable or Disable VConn on CC lines
* @retval USBPD_ACCEPT, USBPD_REJECT
*/
USBPD_StatusTypeDef (*USBPD_PE_VconnPwr)(uint8_t PortNum, USBPD_FunctionalState State);
/**
* @brief Callback to be used by PE to manage VConn
* @param PortNum Port number
* @retval None
*/
void (*USBPD_PE_EnterErrorRecovery)(uint8_t PortNum);
/**
* @brief Callback to be used by PE to know application status about DataRoleSwap
* @param PortNum Port number
* @retval USBPD_REJECT, USBPD_ACCEPT
*/
USBPD_StatusTypeDef (*USBPD_PE_DataRoleSwap)(uint8_t PortNum);
/**
* @brief Callback to be used by PE to check is VBUS is ready or present
* @param PortNum Port number
* @param Vsafe Vsafe status based on @ref USBPD_VSAFE_StatusTypeDef
* @retval USBPD_DISABLE or USBPD_ENABLE
*/
USBPD_FunctionalState (*USBPD_PE_IsPowerReady)(uint8_t PortNum, USBPD_VSAFE_StatusTypeDef Vsafe);
}USBPD_PE_Callbacks;
/**
* @}
*/
/**
* @}
*/
/** @defgroup USBPD_CORE_PE_Exported_Functions USBPD CORE PE Exported Functions
* @{
*/
/** @defgroup USBPD_CORE_PE_Exported_Functions_Group1 USBPD CORE PE Exported Functions to DPM CORE
* @{
*/
/**
* @brief Initialize Policy Engine layer for a Port with a specified role
* @param PortNum Index of current used port
* @param pSettings Pointer on @ref USBPD_SettingsTypeDef structure
* @param pParams Pointer on @ref USBPD_ParamsTypeDef structure
* @param PECallbacks Callback function to be passed to PRL layer
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PE_Init(uint8_t PortNum, USBPD_SettingsTypeDef *pSettings, USBPD_ParamsTypeDef *pParams, const USBPD_PE_Callbacks *PECallbacks);
/**
* @brief Check coherence between lib slected and the lib include inside the project
* @param LibId Value fix defined with the switch lib.
* @retval USBPD_TRUE or USBPD_FALSE
*/
uint32_t USBPD_PE_CheckLIB(uint32_t LibId);
/**
* @brief Return the need of the stack in terms of dynamique allocation
* @note This value correspond to worst case 2 port os if application manage
* only one port divide the value by 2
* @retval Memory size
*/
uint32_t USBPD_PE_GetMemoryConsumption(void);
/**
* @brief Set the trace pointer
* @param Ptr function on trace
* @param Debug Trace debug level
* @retval None
*/
void USBPD_PE_SetTrace(TRACE_ENTRY_POINT Ptr, uint8_t Debug);
#if defined(USBPDCORE_SRC) || defined(USBPDCORE_DRP)
/**
* @brief Policy Engine Source port main state machine
* @param PortNum Index of current used port
* @retval Timing in ms
*/
uint32_t USBPD_PE_StateMachine_SRC(uint8_t PortNum);
#endif /* USBPDCORE_SRC || USBPDCORE_DRP */
#if defined(USBPDCORE_SNK) || defined(USBPDCORE_DRP)
/**
* @brief Policy Engine Sink port main state machine
* @param PortNum Index of current used port
* @retval Timing in ms
*/
uint32_t USBPD_PE_StateMachine_SNK(uint8_t PortNum);
#endif /* USBPDCORE_SNK || USBPDCORE_DRP */
#ifdef USBPDCORE_DRP
/**
* @brief Policy Engine dual role port main state machine
* @param PortNum Index of current used port
* @retval Timing in ms
*/
uint32_t USBPD_PE_StateMachine_DRP(uint8_t PortNum);
#endif /* USBPDCORE_DRP */
/**
* @brief Function called by DPM to set the cable status connected or disconnected.
* @param PortNum Index of current used port
* @param IsConnected Cable connection status: 1 if connected and 0 is disconnected
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PE_IsCableConnected(uint8_t PortNum, uint8_t IsConnected);
/**
* @brief Increment PE Timers tick
* @param PortNum Index of current used port
* @retval None
*/
void USBPD_PE_TimerCounter(uint8_t PortNum);
/**
* @}
*/
#if defined(USBPDCORE_SVDM) || defined(USBPDCORE_UVDM) || defined(USBPDCORE_CABLE)
/** @defgroup USBPD_CORE_PE_Exported_Functions_Group2 USBPD CORE PE Exported Functions to VDM USER
* @{
*/
/**
* @brief Initialize VDM callback functions in PE
* @param PortNum Index of current used port
* @param VDMCallbacks Pointer on VDM callbacks based on @ref USBPD_VDM_Callbacks
* @retval None
*/
void USBPD_PE_InitVDM_Callback(uint8_t PortNum, USBPD_VDM_Callbacks *VDMCallbacks);
/**
* @}
*/
#endif /* USBPDCORE_SVDM || USBPDCORE_UVDM || USBPDCORE_CABLE*/
/** @defgroup USBPD_CORE_PE_Exported_Functions_Group3 USBPD CORE PE Exported Functions to DPM USER
* @{
*/
/**
* @brief Generic function to send a control message
* @param PortNum Index of current used port
* @param CtrlMsg Control message id
* @param SOPType SOP Type based on @ref USBPD_SOPType_TypeDef
* @retval USBPD status USBPD_BUSY, USBPD_ERROR, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_Request_CtrlMessage(uint8_t PortNum, USBPD_ControlMsg_TypeDef CtrlMsg, USBPD_SOPType_TypeDef SOPType);
/**
* @brief Generic function to send a data message
* @param PortNum Index of current used port
* @param DataMsg Data message id based on @ref USBPD_DataMsg_TypeDef
* @param pData Pointer of PDO to send
* @retval USBPD status USBPD_BUSY, USBPD_ERROR, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_Request_DataMessage(uint8_t PortNum, USBPD_DataMsg_TypeDef DataMsg, uint32_t *pData);
/**
* @brief Called by DPM to request PE to perform a Hard Reset.
* @param PortNum Index of current used port
* @retval USBPD status USBPD_OK USBPD_BUSY USBPD_FAIL, USBPD_NOTSUPPORTED
*/
USBPD_StatusTypeDef USBPD_PE_Request_HardReset(uint8_t PortNum);
#if defined(USBPDCORE_SNK) || defined(USBPDCORE_DRP)
/**
* @brief Evaluate received Capabilities Message from Source port and prepare the request message
* @param PortNum Index of current used port
* @param Rdo Requested data object
* @param PWobject Power Object
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PE_Send_Request(uint8_t PortNum, uint32_t Rdo, USBPD_CORE_PDO_Type_TypeDef PWobject);
#endif /* USBPDCORE_SNK || USBPDCORE_DRP */
#ifdef USBPDCORE_SVDM
/**
* @brief Called by DPM to request the PE to perform a VDM identity request.
* @note On PD2.0, SVDM Identity cannot be sent by UFP.
* @param PortNum Index of current used port
* @param SOPType SOP Type (Only SOP or SOP')
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PE_SVDM_RequestIdentity(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType);
/**
* @brief Called by DPM to request the PE to perform a VDM SVID request.
* @param PortNum Index of current used port
* @param SOPType SOP Type (only valid for SOP or SOP')
* @retval USBPD status : USBPD_BUSY, USBPD_OK, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_SVDM_RequestSVID(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType);
/**
* @brief Called by DPM to request the PE to perform a VDM Discovery mode message on one SVID.
* @param PortNum Index of current used port
* @param SOPType SOP Type
* @param SVID SVID used for discovery mode message
* @retval USBPD status : USBPD_BUSY, USBPD_OK, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_SVDM_RequestMode(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, uint16_t SVID);
/**
* @brief Called by DPM to request the PE to perform a VDM Discovery mode message on one SVID.
* @param PortNum Index of current used port
* @param SOPType SOP Type
* @param SVID SVID used for discovery mode message
* @param ModeIndex Index of the mode to be entered
* @retval USBPD status : USBPD_BUSY, USBPD_OK, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_SVDM_RequestModeEnter(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, uint16_t SVID, uint8_t ModeIndex);
/**
* @brief Called by DPM to request the PE to perform a VDM mode exit.
* @param PortNum Index of current used port
* @param SOPType SOP Type
* @param SVID SVID used for discovery mode message
* @param ModeIndex Index of the mode to be exit
* @retval USBPD status : USBPD_BUSY, USBPD_OK, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_SVDM_RequestModeExit(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, uint16_t SVID, uint8_t ModeIndex);
/**
* @brief Called by DPM to request the PE to perform a VDM Attention.
* @param PortNum Index of current used port
* @param SOPType Received message type based on @ref USBPD_SOPType_TypeDef
* @param SVID Used SVID
* @retval USBPD status : USBPD_BUSY, USBPD_OK, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_SVDM_RequestAttention(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, uint16_t SVID);
/**
* @brief Called by DPM to request the PE to send a specific SVDM message.
* @param PortNum Index of current used port
* @param SOPType Received message type based on @ref USBPD_SOPType_TypeDef
* @param Command VDM command based on @ref USBPD_VDM_Command_Typedef
* @param SVID Used SVID
* @retval USBPD status : USBPD_BUSY, USBPD_OK, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_SVDM_RequestSpecific(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_Command_Typedef Command, uint16_t SVID);
#endif /* USBPDCORE_SVDM */
#if defined(USBPDCORE_UVDM)
/**
* @brief Called by DPM to request the PE to send a UVDM message.
* @param PortNum Index of current used port
* @param SOPType Received message type based on @ref USBPD_SOPType_TypeDef
* @retval USBPD status : USBPD_BUSY, USBPD_OK, USBPD_FAIL
*/
USBPD_StatusTypeDef USBPD_PE_UVDM_RequestMessage(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType);
#endif /* USBPDCORE_UVDM */
#if defined(USBPD_REV30_SUPPORT)
/**
* @brief Send an chunked extended message and store data received inside a buffer
* @param PortNum Port number value
* @param SOPType Message type based on @ref USBPD_SOPType_TypeDef
* @param MessageType Extended Msg type @ref USBPD_ExtendedMsg_TypeDef
* @param Ptrdata Data pointer to PRL layer
* @param DataSize Size of the data
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PE_SendExtendedMessage(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_ExtendedMsg_TypeDef MessageType, uint8_t *Ptrdata, uint16_t DataSize);
#if defined(USBPDCORE_FASTROLESWAP)
/**
* @brief Execute an FRS signalling.
* @param PortNum
* @retval Timing in ms.
*/
void USBPD_PE_ExecFastRoleSwapSignalling(uint8_t PortNum);
#endif
#endif /* USBPD_REV30_SUPPORT */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/** @addtogroup USBPD_CORE_PRL
* @{
*/
/** @defgroup USBPD_CORE_PRL_Exported_Functions USBPD CORE PRL Exported Functions
* @{
*/
/** @defgroup USBPD_CORE_PRL_Exported_Functions_Group1 USBPD CORE PRL Interface function for timer
* @{
*/
/**
* @brief Decrement The PRL Timers values
* @param PortNum port number value
* @retval None
*/
void USBPD_PRL_TimerCounter(uint8_t PortNum);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#if defined(USBPDCORE_TCPM_SUPPORT)
/** @addtogroup USBPD_CORE_TCPM
* @{
*/
/** @defgroup USBPD_CORE_TCPM_Exported_Functions USBPD CORE TCPM Exported Functions
* @{
*/
/** @defgroup USBPD_CORE_TCPM_Exported_Functions_Grp1 USBPD CORE TCPM Exported Functions to applications (DPM and PWR_IF)
* @{
*/
/**
* @brief Initialize TCPC devices
* @param PortNum Port number value
* @param TCPC_ToggleRole Indication if TCPC should perform a role toggle or not
* @param Params Pointer on PE parameters based on @ref USBPD_ParamsTypeDef
* @param CallbackFunctions Pointer on CAD callbacks
* @param TCPC_Driver Pointer on TCPC drivers based on @ref TCPC_DrvTypeDef
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_HWInit(uint8_t PortNum, uint8_t TCPC_ToggleRole, USBPD_ParamsTypeDef *Params, USBPD_CAD_Callbacks *CallbackFunctions, TCPC_DrvTypeDef * TCPC_Driver);
/**
* @brief Set CC line for PD connection
* @param PortNum Port number value
* @param Pull Power role can be one of the following values:
* @arg @ref TYPEC_CC_RA
* @arg @ref TYPEC_CC_RP
* @arg @ref TYPEC_CC_RD
* @arg @ref TYPEC_CC_OPEN
* @param State Activation or deactivation of connection
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_set_cc(uint32_t PortNum, TCPC_CC_Pull_TypeDef Pull, USBPD_FunctionalState State);
/**
* @brief Enable or disable VCONN
* @param PortNum Port number value
* @param State Activation or deactivation of VCONN
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_set_vconn(uint32_t PortNum, USBPD_FunctionalState State);
/**
* @brief Enable VBUS
* @param PortNum The port handle.
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_VBUS_Enable(uint32_t PortNum);
/**
* @brief Disable VBUS
* @param PortNum The port handle.
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_VBUS_Disable(uint32_t PortNum);
/**
* @brief Get VBUS voltage
* @param PortNum The port handle.
* @retval VBUS value in mV
*/
uint16_t USBPD_TCPM_VBUS_GetVoltage(uint32_t PortNum);
/**
* @brief Retrieve the VBUS VSafe0 status for a specified port.
* @param PortNum The port handle.
* @retval USBPD_StatusTypeDef
*/
USBPD_StatusTypeDef USBPD_TCPM_VBUS_IsVsafe0V(uint32_t PortNum);
/**
* @brief Retrieve the VBUS Vsafe5V status for a specified port.
* @param PortNum The port handle.
* @retval USBPD_StatusTypeDef
*/
USBPD_StatusTypeDef USBPD_TCPM_VBUS_IsVsafe5V(uint32_t PortNum);
/**
* @brief Management of ALERT
* @param Event Event reported by application
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_alert(uint32_t Event);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#endif /* USBPDCORE_TCPM_SUPPORT */
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBPD_CORE_H_ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,309 @@
/**
******************************************************************************
* @file usbpd_tcpm.h
* @author MCD Application Team
* @brief Header file containing functions prototypes of USBPD TCPM library.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USBPD_TCPM_H
#define __USBPD_TCPM_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#if defined(USBPDCORE_TCPM_SUPPORT)
#include "string.h"
#include "usbpd_cad.h"
#include "usbpd_def.h"
#include "tcpc.h"
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_CORE
* @{
*/
/** @addtogroup USBPD_CORE_TCPM
* @{
*/
/* Exported constants --------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/** @defgroup USBPD_CORE_TCPM_Exported_TypesDefinitions USBPD CORE TCPM Exported Types Definitions
* @{
*/
/**
* @brief CallBacks exposed by the @ref USBPD_CORE_TCPM to the @ref USBPD_CORE_PRL
*/
typedef struct
{
/**
* @brief Reports that a message has been received on a specified port.
* @param hport: The handle of the port
* @param Type: The type of the message received
* @retval None
* @note Received data are stored inside hport->pRxBuffPtr
*/
void (*USBPD_TCPM_MessageReceived)(uint8_t hport, USBPD_SOPType_TypeDef Type);
/**
* @brief Reports to the PRL that a Reset received from channel.
* @param hport: The handle of the port
* @param Type: The type of reset performed
* @retval None
*/
void (*USBPD_TCPM_ResetIndication)(uint8_t hport, USBPD_SOPType_TypeDef Type);
/**
* @brief Reports to the PRL that a Reset operation has been completed.
* @param hport: The handle of the port
* @param Type: The type of reset performed
* @retval None
*/
void (*USBPD_TCPM_ResetCompleted)(uint8_t hport, USBPD_SOPType_TypeDef Type);
/**
* @brief Reports to the PRL that a Bist operation has been completed.
* @param hport: The handle of the port
* @param Type: The type of Bist performed
* @retval None
*/
void (*USBPD_TCPM_BistCompleted)(uint8_t hport, USBPD_BISTMsg_TypeDef bistmode);
/**
* @brief USB-PD message sent callback from TCPC
* @param PortNum port number value
* @param Status Status of the transmission
* @retval None
*/
void (*USBPD_TCPM_MessageReceivedTC)(uint8_t PortNum, uint32_t status);
} USBPD_PHY_Callbacks;
/**
* @brief Initialization structure exposed by the @ref USBPD_CORE_TCPM to the @ref USBPD_CORE_PRL
*/
typedef struct
{
uint8_t *pRxBuffer; /*!< Pointer to @ref USBPD_CORE_PRL RX Buffer for the current port */
const USBPD_PHY_Callbacks *pCallbacks; /*!< TCPM Callbacks */
} USBPD_TCPM_HandleTypeDef;
/**
* @}
*/
/* Exported macro ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/** @addtogroup USBPD_CORE_TCPM_Exported_Functions
* @{
*/
/** @defgroup USBPD_CORE_TCPM_Exported_Functions_Grp2 USBPD CORE TCPM Exported Functions to PRL
* @{
*/
/**
* @brief Initialize TCPC devices
* @param PortNum Number of the port
* @param pCallbacks TCPM callbacks
* @param pRxBuffer Pointer on the RX buffer
* @param PowerRole Power role can be one of the following values:
* @arg @ref USBPD_PORTPOWERROLE_SNK
* @arg @ref USBPD_PORTPOWERROLE_SRC
* @param SupportedSOP Supported SOP
* @retval HAL status
*/
USBPD_StatusTypeDef USBPD_PHY_Init(uint8_t PortNum, const USBPD_PHY_Callbacks *pCallbacks, uint8_t *pRxBuffer, USBPD_PortPowerRole_TypeDef PowerRole, uint32_t SupportedSOP);
/**
* @brief Reset the PHY of a specified port.
* @param PortNum Number of the port.
* @retval None
*/
void USBPD_PHY_Reset(uint8_t PortNum);
/**
* @brief function to set the supported SOP
* @param PortNum Number of the port.
* @param SOPSupported List of the supported SOP
* @retval None.
*/
void USBPD_PHY_SOPSupported(uint8_t PortNum,uint32_t SOPSupported);
/**
* @brief De-initialize TCPC devices
* @param PortNum Number of the port
* @retval None
*/
void USBPD_TCPM_DeInit(uint8_t PortNum);
/**
* @brief Get CC line for PD connection
* @param PortNum Number of the port
* @param CC1_Level Pointer of status of the CC1 line
* @param CC2_Level Pointer of status of the CC2 line
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_get_cc(uint32_t PortNum, uint32_t *CC1_Level, uint32_t *CC2_Level);
/**
* @brief Set the polarity of the CC lines
* @param PortNum Number of the port
* @param Polarity Polarity
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_set_polarity(uint32_t PortNum, uint32_t Polarity);
/**
* @brief Set power and data role et PD message header
* @param PortNum Number of the port
* @param PowerRole Power role
* @param DataRole Data role
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_set_msg_header(uint32_t PortNum, USBPD_PortPowerRole_TypeDef PowerRole, USBPD_PortDataRole_TypeDef DataRole);
/**
* @brief Enable or disable PD reception
* @param PortNum Number of the port
* @param Pull Value of the CC pin to configure based on @ref TCPC_CC_Pull_TypeDef
* @param State Activation or deactivation of RX
* @param SupportedSOP Supported SOP by PRL
* @param HardReset Hard reset status based on @ref TCPC_hard_reset
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_set_rx_state(uint32_t PortNum, TCPC_CC_Pull_TypeDef Pull, USBPD_FunctionalState State, uint32_t SupportedSOP, TCPC_hard_reset HardReset);
/**
* @brief Retrieve the PD message
* @param PortNum Number of the port
* @param Payload Pointer on the payload
* @param Type Pointer on the message type
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_get_message(uint32_t PortNum, uint8_t *Payload, uint8_t *Type);
/**
* @brief Transmit the PD message
* @param PortNum Number of the port
* @param Type Message type
* @param pData Pointer on the data message
* @param RetryNumber Number of retry
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_transmit(uint32_t PortNum, USBPD_SOPType_TypeDef Type, const uint8_t *pData, uint32_t RetryNumber);
/**
* @brief Send bist pattern.
* @param PortNum Number of the port
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PHY_Send_BIST_Pattern(uint32_t PortNum);
/**
* @brief Request a Reset on a specified port.
* @param PortNum Number of the port
* @param Type The type of reset (hard or cable reset).
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PHY_ResetRequest(uint8_t PortNum, USBPD_SOPType_TypeDef Type);
/**
* @brief Request TCPC to enter a specific BIST test mode.
* @param PortNum Number of the port
* @param State Enable BIST carrier mode 2
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_TCPM_Send_BIST_Pattern(uint8_t PortNum, USBPD_FunctionalState State);
/**
* @brief function to set the SinkTxNg
* @param PortNum Number of the port.
* @retval none.
*/
void USBPD_PHY_SetResistor_SinkTxNG(uint8_t PortNum);
/**
* @brief function to set the SinkTxOK
* @param PortNum Number of the port.
* @retval none.
*/
void USBPD_PHY_SetResistor_SinkTxOK(uint8_t PortNum);
/**
* @brief function to check if SinkTxOK
* @param PortNum Number of the port.
* @retval USBPD status based on @ref USBPD_StatusTypeDef
*/
USBPD_StatusTypeDef USBPD_PHY_IsResistor_SinkTxOk(uint8_t PortNum);
/**
* @brief Trigger in Fast role swap signalling
* @param PortNum Number of the port.
* @retval None
*/
void USBPD_PHY_FastRoleSwapSignalling(uint8_t PortNum);
/**
* @brief Enable RX
* @param PortNum Number of the port.
* @retval None
*/
void USBPD_PHY_EnableRX(uint8_t PortNum);
/**
* @brief Disable RX
* @param PortNum Number of the port.
* @retval None
*/
void USBPD_PHY_DisableRX(uint8_t PortNum);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#endif /* USBPDCORE_TCPM_SUPPORT */
#ifdef __cplusplus
}
#endif
#endif /* __USBPD_TCPM_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,95 @@
/**
******************************************************************************
* @file usbpd_trace.h
* @author MCD Application Team
* @brief This file contains the headers of usbpd_cad.h for Cable Attach-Detach
* controls.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#ifndef __USBPD_TRACE_H_
#define __USBPD_TRACE_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_CORE
* @{
*/
/** @addtogroup USBPD_CORE_TRACE
* @{
*/
/* Exported types ------------------------------------------------------------*/
/* Exported define -----------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/** @defgroup USBPD_CORE_TRACE_Exported_Functions USBPD CORE TRACE Exported Functions
* @{
*/
/**
* @brief Initialize the TRACE module
* @retval None
*/
void USBPD_TRACE_Init(void);
/**
* @brief Add information in debug trace buffer
* @param Type Trace Type based on @ref TRACE_EVENT
* @param PortNum Port number value
* @param Sop SOP type
* @param Ptr Pointer on the data to send
* @param Size Size of the data to send
* @retval None.
*/
void USBPD_TRACE_Add(TRACE_EVENT Type, uint8_t PortNum, uint8_t Sop, uint8_t *Ptr, uint32_t Size);
/**
* @brief Process to send the data
* @retval None.
*/
uint32_t USBPD_TRACE_TX_Process(void);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBPD_CAD_H_ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,204 @@
/**
******************************************************************************
* @file usbpd_trace.c
* @author MCD Application Team
* @brief This file contains trace control functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#if defined(_TRACE)
#define USBPD_TRACE_C
#include "stdint.h"
#include "usbpd_def.h"
#include "usbpd_core.h"
#include "usbpd_trace.h"
#ifdef _TRACE
#include "tracer_emb.h"
#endif
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_CORE
* @{
*/
/** @addtogroup USBPD_CORE_TRACE
* @{
*/
/* Private enums -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/** @defgroup USBPD_CORE_TRACE_Private_Defines USBPD TRACE Private Defines
* @{
*/
#define TRACE_SIZE_HEADER_TRACE 9u /* Type + Time x 2 + PortNum + Sop + Size */
#define TRACE_PORT_BIT_POSITION 5u /* Bit position of port number in TAG id */
#define TLV_SOF (uint8_t)0xFDu
#define TLV_EOF (uint8_t)0xA5u
#define TLV_SIZE_MAX 256u
#define TLV_HEADER_SIZE 3u /* Size of TLV header (TAG(1) + LENGTH(2) */
#define TLV_SOF_SIZE 4u /* TLV_SOF * 4 */
#define TLV_EOF_SIZE 4u /* TLV_EOF * 4 */
#define DEBUG_STACK_MESSAGE 0x12u
/**
* @}
*/
/* Private macro -------------------------------------------------------------*/
/** @defgroup USBPD_CORE_TRACE_Private_Macros USBPD TRACE Private Macros
* @{
*/
#define __TRACE_SET_TAG_ID(_PORT_, _TAG_) (((_PORT_) << TRACE_PORT_BIT_POSITION) | (_TAG_))
#define TRACER_EMB_WRITE_DATA(_POSITION_,_DATA_) TRACER_EMB_WriteData((_POSITION_),(_DATA_));\
(_POSITION_) = ((_POSITION_) + 1u);
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup USBPD_CORE_TRACE_Private_Variables USBPD TRACE Private Variables
* @{
*/
extern uint32_t HAL_GetTick(void);
/**
* @}
*/
/* Exported functions ---------------------------------------------------------*/
/** @addtogroup USBPD_CORE_TRACE_Exported_Functions
* @{
*/
void USBPD_TRACE_Init(void)
{
/* initialize tracer module */
TRACER_EMB_Init();
/* Initialize PE trace */
USBPD_PE_SetTrace(USBPD_TRACE_Add, 3u);
}
void USBPD_TRACE_Add(TRACE_EVENT Type, uint8_t PortNum, uint8_t Sop, uint8_t *Ptr, uint32_t Size)
{
uint32_t _time;
int32_t _writepos;
uint16_t _writepos2;
uint8_t *data_to_write;
uint32_t index;
uint32_t total_size;
#if !defined(_TRACE)
/* Do not send debug traces */
return;
#endif
TRACER_EMB_Lock();
/* Data are encapsulate inside a TLV string*/
/* Allocate buffer Size */
total_size = Size + TRACE_SIZE_HEADER_TRACE + TLV_HEADER_SIZE + TLV_SOF_SIZE + TLV_EOF_SIZE;
_writepos = TRACER_EMB_AllocateBufer(total_size);
/* Check allocation */
if (_writepos != -1)
{
_writepos2 = (uint16_t)_writepos;
data_to_write = Ptr;
/* Copy SOF bytes */
for(index = 0u; index < TLV_SOF_SIZE; index++)
{
TRACER_EMB_WRITE_DATA(_writepos2, TLV_SOF);
}
/* Copy the TAG */
TRACER_EMB_WRITE_DATA(_writepos2, __TRACE_SET_TAG_ID((PortNum + 1u), DEBUG_STACK_MESSAGE));
/* Copy the LENGTH */
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)((total_size - TLV_HEADER_SIZE - TLV_SOF_SIZE - TLV_EOF_SIZE) >> 8u));
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)(total_size - TLV_HEADER_SIZE - TLV_SOF_SIZE - TLV_EOF_SIZE));
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)Type);
_time = HAL_GetTick();
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)_time);
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)(_time >> 8u));
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)(_time >> 16u));
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)(_time >> 24u));
TRACER_EMB_WRITE_DATA(_writepos2, PortNum);
TRACER_EMB_WRITE_DATA(_writepos2, Sop);
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)(Size >> 8u));
TRACER_EMB_WRITE_DATA(_writepos2, (uint8_t)Size);
/* initialize the Ptr for Read/Write */
for (index = 0u; index < Size; index++)
{
TRACER_EMB_WRITE_DATA(_writepos2, data_to_write[index]);
}
/* Copy EOF bytes */
for(index = 0u; index < TLV_EOF_SIZE; index++)
{
TRACER_EMB_WRITE_DATA(_writepos2, TLV_EOF);
}
}
TRACER_EMB_UnLock();
}
uint32_t USBPD_TRACE_TX_Process(void)
{
return TRACER_EMB_TX_Process();
}
/**
* @}
*/
/** @defgroup USBPD_CORE_TRACE_Private_Functions USBPD TRACE Private Functions
* @{
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#endif /* _TRACE */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,80 @@
/**
******************************************************************************
* @file usbpd_cad_hw_if.h
* @author MCD Application Team
* @brief This file contains the headers of usbpd_cad.h for Cable Attach-Detach
* controls.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#ifndef __USBPD_CAD_HW_IF_H_
#define __USBPD_CAD_HW_IF_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_DEVICE
* @{
*/
/** @addtogroup USBPD_DEVICE_CAD_HW_IF
* @{
*/
/* Exported types ------------------------------------------------------------*/
/* Exported define -----------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/** @addtogroup USBPD_DEVICE_CAD_HW_IF_Exported_Functions
* @{
*/
void CAD_Init(uint8_t PortNum, USBPD_SettingsTypeDef *Settings, USBPD_ParamsTypeDef *Params, void (*PtrWakeUp)(void));
uint32_t CAD_StateMachine(uint8_t PortNum, USBPD_CAD_EVENT *Event, CCxPin_TypeDef *CCXX);
void CAD_Enter_ErrorRecovery(uint8_t PortNum);
uint32_t CAD_Set_ResistorRp(uint8_t PortNum, CAD_RP_Source_Current_Adv_Typedef RpValue);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBPD_CAD_HW_IF_H_ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,36 @@
/**
******************************************************************************
* @file usbpd_hw.h
* @author MCD Application Team
* @brief This file contains interface hw control.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#ifndef USBPD_HW_H
#define USBPD_HW_H
/* Includes ------------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Variable containing ADC conversions results */
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
UCPD_TypeDef *USBPD_HW_GetUSPDInstance(uint8_t PortNum);
DMA_Channel_TypeDef *USBPD_HW_Init_DMARxInstance(uint8_t PortNum);
void USBPD_HW_DeInit_DMARxInstance(uint8_t PortNum);
DMA_Channel_TypeDef *USBPD_HW_Init_DMATxInstance(uint8_t PortNum);
void USBPD_HW_DeInit_DMATxInstance(uint8_t PortNum);
uint32_t USBPD_HW_GetRpResistorValue(uint8_t Portnum);
void USBPD_HW_SetFRSSignalling(uint8_t Portnum, uint8_t cc);
#endif /* USBPD_BSP_HW_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,377 @@
/**
******************************************************************************
* @file usbpd_hw_if.h
* @author MCD Application Team
* @brief This file contains the headers of usbpd_hw_if.h for USB-PD Hardware
Interface layer. This file is specific for each device.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#ifndef __USBPD_HW_IF_H_
#define __USBPD_HW_IF_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbpd_def.h"
#include "usbpd_devices_conf.h"
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_DEVICE
* @{
*/
/** @addtogroup USBPD_DEVICE_HW_IF
* @{
*/
/* Exported typedef ----------------------------------------------------------*/
/** @defgroup USBPD_DEVICE_DEVICE_HW_IF_Exported_Types USBPD DEVICE HW_IF Exported Types
* @{
*/
/**
* @brief Enum used to get the status of decoding
*/
typedef enum
{
USBPD_PHY_RX_STATUS_NONE,
USBPD_PHY_RX_STATUS_OK,
USBPD_PHY_RX_STATUS_SOP_DETECTING,
USBPD_PHY_RX_STATUS_DATA,
USBPD_PHY_RX_STATUS_MESSAGE_READY,
USBPD_PHY_RX_STATUS_ERROR,
USBPD_PHY_RX_STATUS_ERROR_UNSUPPORTED_SOP,
USBPD_PHY_RX_STATUS_ERROR_INVALID_SOP,
USBPD_PHY_RX_STATUS_ERROR_INVALID_SYMBOL,
USBPD_PHY_RX_STATUS_ERROR_EOP_NOT_FOUND,
USBPD_PHY_RX_STATUS_ERROR_CRC_FAILED,
}
USBPD_PHY_RX_Status_TypeDef;
/**
* @brief CallBacks exposed by the HW_IF to the PHY
*/
typedef struct
{
/**
* @brief The message transfer has been completed
* @param PortNum The current port number
* @retval None
*/
void (*USBPD_HW_IF_TxCompleted)(uint8_t PortNum);
/**
* @brief Bist data sent callback from PHY_HW_IF
* @param PortNum Index of current used port
* @param bistmode: Bist mode
* @retval None
*/
void (*USBPD_HW_IF_BistCompleted)(uint8_t PortNum, USBPD_BISTMsg_TypeDef bistmode);
/**
* @brief A new message is incoming, need to reset the status.
* @param PortNum The current port number
* @retval The status of the decoding process
*/
USBPD_PHY_RX_Status_TypeDef(*USBPD_HW_IF_RX_Reset)(uint8_t PortNum);
/**
* @brief The reception phase of an hard reset is completed notify it.
* @param PortNum The current port number
* @retval None
*/
void (*USBPD_HW_IF_RX_ResetIndication)(uint8_t PortNum);
/**
* @brief The reception phase of the current message is completed and notify it.
* @param PortNum The current port number
* @retval None
*/
void (*USBPD_HW_IF_RX_Completed)(uint8_t PortNum, uint32_t MsgType, uint16_t RxPaySize);
/**
* @brief The emission of HRST has been completed.
* @param PortNum The current port number
* @retval None
*/
void (*USBPD_HW_IF_TX_HardResetCompleted)(uint8_t PortNum, USBPD_SOPType_TypeDef Type);
} USBPD_HW_IF_Callbacks;
/** @defgroup USBPD_PORT_HandleTypeDef USB PD handle Structure definition for USBPD_PHY_HW_IF
* @brief USBPD PORT handle Structure definition
* @{
*/
typedef struct
{
UCPD_TypeDef *husbpd; /*!< UCPD Handle parameters */
DMA_Channel_TypeDef *hdmatx; /*!< Tx DMA Handle parameters */
DMA_Channel_TypeDef *hdmarx; /*!< Rx DMA Handle parameters */
USBPD_SettingsTypeDef *settings;
USBPD_ParamsTypeDef *params;
USBPD_HW_IF_Callbacks cbs; /*!< USBPD_PHY_HW_IF callbacks */
__IO uint32_t PIN_CC1; /*!< CC1 detection state */
__IO uint32_t PIN_CC2; /*!< CC2 detection state */
void (*USBPD_CAD_WakeUp)(void); /*!< function used to wakeup cad task */
uint8_t *ptr_RxBuff; /*!< Pointer to Raw Rx transfer Buffer */
CCxPin_TypeDef CCx; /*!< CC pin used for communication */
} USBPD_PORT_HandleTypeDef;
extern USBPD_PORT_HandleTypeDef Ports[USBPD_PORT_COUNT];
/**
* @}
*/
/**
* @}
*/
/* Exported define -----------------------------------------------------------*/
/** @defgroup USBPD_DEVICE_DEVICE_HW_IF_Exported_Defines USBPD DEVICE HW_IF Exported Defines
* @{
*/
#define SIZE_MAX_PD_TRANSACTION_CHUNK 30u
#define SIZE_MAX_PD_TRANSACTION_UNCHUNK (260u + 4u)
/**
* @}
*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/** @defgroup USBPD_DEVICE_DEVICE_HW_IF_Exported_Functions USBPD DEVICE HW_IF Exported Functions
* @{
*/
/**
* @brief Enable the interrupt for the reception.
* @param PortNum The handle of the port.
* @retval None
*/
void USBPDM1_RX_EnableInterrupt(uint8_t PortNum);
/**
* @brief stop bist carrier mode 2.
* @param PortNum The port handle.
* @retval None
*/
void USBPD_HW_IF_StopBISTMode2(uint8_t PortNum);
/**
* @brief Initialize specific peripheral for the APP.
* @retval None
*/
void USBPD_HW_IF_GlobalHwInit(void);
/**
* @brief Send a Buffer .
* @note The data will be converted in bmc and send through the line
* @param PortNum The port handle.
* @param Type SOP Message Type based on @ref USBPD_SOPType_TypeDef
* @param pBuffer Data buffer to be transmitted
* @param Bitsize The number of bits to be transmitted
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_HW_IF_SendBuffer(uint8_t PortNum, USBPD_SOPType_TypeDef Type, uint8_t *pBuffer, uint32_t Bitsize);
#if defined(_SRC) || defined(_DRP)
/**
* @brief Enable the VBUS on a specified port.
* @param PortNum The port handle.
* @param State ENABLE or DISABLE.
* @param Cc CC pin based on @ref CCxPin_TypeDef
* @param VconnState VCONN State activation
* @param role The role of the port.
* @retval USBPD status
*/
USBPD_StatusTypeDef HW_IF_PWR_Enable(uint8_t PortNum, USBPD_FunctionalState State, CCxPin_TypeDef Cc, uint32_t VconnState, USBPD_PortPowerRole_TypeDef role);
#endif /* _SRC || _DRP */
/**
* @brief Retrieve the VBUS status for a specified port.
* @param PortNum The port handle.
* @retval FunctionalState
*/
USBPD_FunctionalState HW_IF_PWR_VBUSIsEnabled(uint8_t PortNum);
/**
* @brief Set the VBUS voltage level on a specified port.
* @param PortNum The port handle.
* @param Voltage voltage value to be set.
* @retval USBPD status
*/
USBPD_StatusTypeDef HW_IF_PWR_SetVoltage(uint8_t PortNum, uint16_t Voltage);
/**
* @brief Get the voltage level on a specified port.
* @param PortNum The port handle.
* @retval The voltage value
*/
uint16_t HW_IF_PWR_GetVoltage(uint8_t PortNum);
/**
* @brief Get the current level on a specified port.
* @param PortNum The port handle.
* @retval The current value
*/
int16_t HW_IF_PWR_GetCurrent(uint8_t PortNum);
/**
* @brief Connect the Rp resitors on the CC lines
* @param PortNum The port handle.
* @retval none
*/
void USBPDM1_AssertRp(uint8_t PortNum);
/**
* @brief Disconnect the Rp resitors on the CC lines
* @param PortNum The port handle.
* @retval none
*/
void USBPDM1_DeAssertRp(uint8_t PortNum);
/**
* @brief Connect the Rd resitors on the CC lines
* @param PortNum The port handle.
* @retval none
*/
void USBPDM1_AssertRd(uint8_t PortNum);
/**
* @brief Disconnect the Rd resitors on the CC lines
* @param PortNum The port handle.
* @retval none
*/
void USBPDM1_DeAssertRd(uint8_t PortNum);
/**
* @brief Set the CCx pin.
* @param PortNum The port handle.
* @param cc Specify the ccx to be selected.
* @retval None
*/
void USBPDM1_Set_CC(uint8_t PortNum, CCxPin_TypeDef cc);
/**
* @brief Sends the BIST pattern
* @param PortNum The port handle.
* @retval none
*/
void USBPD_HW_IF_Send_BIST_Pattern(uint8_t PortNum);
/**
* @brief Sends a detachemnt signal.
* @param PortNum The port handle.
* @retval none
*/
void HW_SignalDetachment(uint8_t PortNum);
/**
* @brief Sends an Attachement signal.
* @param PortNum The port handle.
* @param cc the PD pin.
* @retval none
*/
void HW_SignalAttachement(uint8_t PortNum, CCxPin_TypeDef cc);
/**
* @brief Set SinkTxNG resistor.
* @param PortNum The port handle.
* @retval none
*/
void USBPD_HW_IF_SetResistor_SinkTxNG(uint8_t PortNum);
/**
* @brief Set SinkTxOk resistor.
* @param PortNum The port handle.
* @retval none
*/
void USBPD_HW_IF_SetResistor_SinkTxOK(uint8_t PortNum);
/**
* @brief Is SinkTxOk resistor.
* @param PortNum The port handle.
* @retval TRUE FALSE
*/
uint8_t USBPD_HW_IF_IsResistor_SinkTxOk(uint8_t PortNum);
/**
* @brief send a Fast Role swap signalling.
* @param PortNum The port handle.
* @retval None
*/
void USBPD_HW_IF_FastRoleSwapSignalling(uint8_t PortNum);
/**
* @brief enter in error recovery state.
* @param PortNum The port handle.
* @retval None
*/
void USBPDM1_EnterErrorRecovery(uint8_t PortNum);
void USBPD_PORT0_IRQHandler(void);
void USBPD_PORT1_IRQHandler(void);
/**
* @brief Enable RX
* @param PortNum The port handle.
* @retval None
*/
void USBPD_HW_IF_EnableRX(uint8_t PortNum);
/**
* @brief Disable RX
* @param PortNum The port handle.
* @retval None
*/
void USBPD_HW_IF_DisableRX(uint8_t PortNum);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBPD_HW_IF_H_ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,146 @@
/**
******************************************************************************
* @file usbpd_phy.h
* @author MCD Application Team
* @brief This file contains the headers of usbpd_phy.h.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#ifndef __USBPD_PHY_H_
#define __USBPD_PHY_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_DEVICE
* @{
*/
/** @addtogroup USBPD_DEVICE_PHY
* @{
*/
/* Exported typedef ----------------------------------------------------------*/
/** @defgroup USBPD_DEVICE_PHY_Exported_TypeDef USBPD DEVICE PHY Exported TypeDef
* @{
*/
/**
* @brief CallBacks exposed by the @ref USBPD_DEVICE_PHY to the USBPD_CORE_PRL
*/
typedef struct
{
/**
* @brief Reports that a message has been received on a specified port.
* @note Received data are stored inside PortNum->pRxBuffPtr
* function called in the interrupt context
* @param PortNum The handle of the port
* @param Type The type of the message received @ref USBPD_SOPType_TypeDef
* @retval None
*/
void (*USBPD_PHY_MessageReceived)(uint8_t PortNum, USBPD_SOPType_TypeDef Type);
/**
* @brief Reports to the PRL that a Reset received from channel.
* @param PortNum The handle of the port
* @param Type The type of reset performed @ref USBPD_SOPTYPE_HARD_RESET or @ref USBPD_SOPTYPE_CABLE_RESET
* @retval None
*/
void (*USBPD_PHY_ResetIndication)(uint8_t PortNum, USBPD_SOPType_TypeDef Type);
/**
* @brief Reports to the PRL that a Reset operation has been completed.
* @param PortNum The handle of the port
* @param Type The type of reset performed @ref USBPD_SOPTYPE_HARD_RESET or @ref USBPD_SOPTYPE_CABLE_RESET
* @retval None
*/
void (*USBPD_PHY_ResetCompleted)(uint8_t PortNum, USBPD_SOPType_TypeDef Type);
/**
* @brief Reports to the PRL that a Bist operation has been completed.
* @param PortNum The handle of the port
* @param Type The type of Bist performed @ref USBPD_BISTMsg_TypeDef
* @retval None
*/
void (*USBPD_PHY_BistCompleted)(uint8_t PortNum, USBPD_BISTMsg_TypeDef bistmode);
/**
* @brief Reports to the PRL that a tx operation has been completed.
* @param PortNum: The handle of the port
* @retval None
*/
void (*USBPD_PHY_TxCompleted)(uint8_t PortNum);
} USBPD_PHY_Callbacks;
/**
* @}
*/
/* Exported define -----------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/** @addtogroup USBPD_DEVICE_PHY_Exported_Functions
* @{
*/
USBPD_StatusTypeDef USBPD_PHY_Init(uint8_t PortNum, const USBPD_PHY_Callbacks *cbs, uint8_t *pRxBuffer, USBPD_PortPowerRole_TypeDef role, uint32_t SupportedSOP);
void USBPD_PHY_Reset(uint8_t PortNum);
uint16_t USBPD_PHY_GetRetryTimerValue(uint8_t PortNum);
USBPD_StatusTypeDef USBPD_PHY_ResetRequest(uint8_t PortNum, USBPD_SOPType_TypeDef Type);
USBPD_StatusTypeDef USBPD_PHY_SendMessage(uint8_t PortNum, USBPD_SOPType_TypeDef Type, uint8_t *pBuffer, uint16_t Size);
USBPD_StatusTypeDef USBPD_PHY_Send_BIST_Pattern(uint8_t PortNum);
USBPD_StatusTypeDef USBPD_PHY_ExitTransmit(uint8_t PortNum, USBPD_SOPType_TypeDef BistType);
void USBPD_PHY_SetResistor_SinkTxNG(uint8_t PortNum);
void USBPD_PHY_SetResistor_SinkTxOK(uint8_t PortNum);
uint8_t USBPD_PHY_IsResistor_SinkTxOk(uint8_t PortNum);
void USBPD_PHY_FastRoleSwapSignalling(uint8_t PortNum);
void USBPD_PHY_SOPSupported(uint8_t PortNum, uint32_t SOPSupported);
void USBPD_PHY_EnableRX(uint8_t PortNum);
void USBPD_PHY_DisableRX(uint8_t PortNum);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBPD_PHY_H_ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,80 @@
/**
******************************************************************************
* @file usbpd_timersserver.h
* @author MCD Application Team
* @brief This file contains the headers of usbpd_timerserver.h.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* Redistribution and use in source and binary forms, with or without
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#ifndef __USBPD_TIMERSSERVER_H_
#define __USBPD_TIMERSSERVER_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_DEVICE
* @{
*/
/** @addtogroup USBPD_DEVICE_TIMESERVER
* @{
*/
/* Exported constants --------------------------------------------------------*/
typedef enum {
TIM_PORT0_CRC,
TIM_PORT0_RETRY,
TIM_PORT1_CRC,
TIM_PORT1_RETRY,
TIM_MAX
}
TIM_identifier;
#define TIM_MAX_TIME 10000u /*time in us, means 10 ms */
/* Exported types ------------------------------------------------------------*/
/* External variables --------------------------------------------------------*/
/* Exported macros -----------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
void USBPD_TIM_Init(void);
void USBPD_TIM_DeInit(void);
void USBPD_TIM_Start(TIM_identifier Id, uint32_t TimeUs);
uint32_t USBPD_TIM_IsExpired(TIM_identifier Id);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBPD_TIMERSSERVER_H_ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,949 @@
/**
******************************************************************************
* @file usbpd_cad_hw_if.c
* @author MCD Application Team
* @brief This file contains power hardware interface cad functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#define USBPD_CAD_HW_IF_C
#include "usbpd_devices_conf.h"
#include "usbpd_hw.h"
#include "usbpd_def.h"
#include "usbpd_cad_hw_if.h"
#include "usbpd_hw_if.h"
#include "usbpd_core.h"
#include "usbpd_trace.h"
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_DEVICE
* @{
*/
/** @addtogroup USBPD_DEVICE_CAD_HW_IF
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/**
* @brief CAD State value @ref USBPD_DEVICE_CAD_HW_IF
* @{
*/
#define USBPD_CAD_STATE_RESET 0u /*!< USBPD CAD State Reset */
#define USBPD_CAD_STATE_DETACHED 1u /*!< USBPD CAD State No cable detected */
#define USBPD_CAD_STATE_ATTACHED_WAIT 2u /*!< USBPD CAD State Port partner detected */
#define USBPD_CAD_STATE_ATTACHED 3u /*!< USBPD CAD State Port partner attached */
#define USBPD_CAD_STATE_EMC 4u /*!< USBPD CAD State Electronically Marked Cable detected */
#define USBPD_CAD_STATE_ATTEMC 5u /*!< USBPD CAD State Port Partner detected throug EMC */
#define USBPD_CAD_STATE_ACCESSORY 6u /*!< USBPD CAD State Accessory detected */
#define USBPD_CAD_STATE_DEBUG 7u /*!< USBPD CAD State Debug detected */
#define USBPD_CAD_STATE_SWITCH_TO_SRC 8u /*!< USBPD CAD State switch to Source */
#define USBPD_CAD_STATE_SWITCH_TO_SNK 9u /*!< USBPD CAD State switch to Sink */
#define USBPD_CAD_STATE_UNKNOW 10u /*!< USBPD CAD State unknow */
/*USBPD_CAD_STATE_ATTACHED_LEGACY 11u*/ /*!< USBPD CAD State Port partner attached to legacy cable */
#define USBPD_CAD_STATE_ERRORRECOVERY 12u /*!< USBPD CAD State error recovery */
#define USBPD_CAD_STATE_ERRORRECOVERY_EXIT 13u /*!< USBPD CAD State to exit error recovery */
typedef uint32_t USBPD_CAD_STATE;
/**
* @}
*/
/**
* @brief USB PD CC lines HW condition
*/
#define HW_Detachment 0x00UL /*!< Nothing attached */
#define HW_Attachment 0x01UL /*!< Sink attached */
#define HW_PwrCable_NoSink_Attachment 0x02UL /*!< Powered cable without Sink attached */
#define HW_PwrCable_Sink_Attachment 0x03UL /*!< Powered cable with Sink or VCONN-powered Accessory attached */
#define HW_Debug_Attachment 0x04UL /*!< Debug Accessory Mode attached */
#define HW_AudioAdapter_Attachment 0x05UL /*!< Audio Adapter Accessory Mode attached */
typedef uint32_t CAD_HW_Condition_TypeDef;
/**
* @brief CAD State value @ref USBPD_DEVICE_CAD_HW_IF
* @{
*/
typedef struct
{
USBPD_CAD_STATE cstate : 4; /* current state */
USBPD_CAD_STATE pstate : 4; /* previous state */
CCxPin_TypeDef cc : 2;
CAD_HW_Condition_TypeDef CurrentHWcondition : 3;
CAD_SNK_Source_Current_Adv_Typedef SNK_Source_Current_Adv : 2;
uint32_t CAD_tDebounce_flag : 1;
uint32_t CAD_ErrorRecoveryflag : 1;
#if defined(_DRP) || defined(_SRC)
uint32_t CAD_ResistorUpdateflag : 1;
uint32_t reserved : 12;
#else
uint32_t reserved : 13;
#endif /* _SRC || _DRP */
#if defined(_DRP)
uint32_t CAD_tToogle_start;
#endif /* _DRP */
uint32_t CAD_tDebounce_start, CAD_tDebounce; /* Variable used for attach or detach debounce timers */
} CAD_HW_HandleTypeDef;
/**
* @}
*/
/* Private define ------------------------------------------------------------*/
#define CAD_TPDDEBOUCE_THRESHOLD 12u /**< tPDDebounce threshold = 20ms */
#define CAD_TCCDEBOUCE_THRESHOLD 100u /**< tCCDebounce threshold = 100ms */
#define CAD_TSRCDISCONNECT_THRESHOLD 2u /**< tSRCDisconnect detach threshold between 0 to 20ms */
#define CAD_INFINITE_TIME 0xFFFFFFFFu /**< infinite time to wait a new interrupt event */
#define CAD_TERROR_RECOVERY_TIME 26u /**< tErrorRecovery min 25ms */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* handle to manage the detection state machine */
static CAD_HW_HandleTypeDef CAD_HW_Handles[USBPD_PORT_COUNT];
/* Private function prototypes -----------------------------------------------*/
/** @defgroup USBPD_DEVICE_CAD_HW_IF_Private_Functions USBPD DEVICE_CAD HW IF Private Functions
* @{
*/
static void CAD_Check_HW(uint8_t PortNum);
#if defined(_DRP) || defined(_SNK)
static uint8_t CAD_Check_VBus(uint8_t PortNum);
#endif /* _DRP || _SNK */
static uint32_t ManageStateDetached(uint8_t PortNum);
static uint32_t ManageStateAttachedWait(uint8_t PortNum, USBPD_CAD_EVENT *pEvent, CCxPin_TypeDef *pCCXX);
static uint32_t ManageStateAttached(uint8_t PortNum, USBPD_CAD_EVENT *pEvent, CCxPin_TypeDef *pCCXX);
static uint32_t ManageStateEMC(uint8_t PortNum, USBPD_CAD_EVENT *pEvent, CCxPin_TypeDef *pCCXX);
/**
* @}
*/
/** @defgroup USBPD_DEVICE_CAD_HW_IF_Exported_Functions USBPD DEVICE_CAD HW IF Exported Functions
* @{
*/
/**
* @brief function to initialize the cable detection state machine
* @param PortNum port
* @param pSettings Pointer on PD settings based on @ref USBPD_SettingsTypeDef
* @param pParams Pointer on PD params based on @ref USBPD_ParamsTypeDef
* @param WakeUp Wake-up callback function used for waking up CAD
* @retval None
*/
void CAD_Init(uint8_t PortNum, USBPD_SettingsTypeDef *pSettings, USBPD_ParamsTypeDef *pParams, void (*WakeUp)(void))
{
CAD_HW_HandleTypeDef *_handle = &CAD_HW_Handles[PortNum];
LL_UCPD_InitTypeDef settings;
Ports[PortNum].params = pParams;
#ifndef _VALID_RP
#define _VALID_RP vRp_3_0A
#endif
Ports[PortNum].params->RpResistor = _VALID_RP;
Ports[PortNum].settings = pSettings;
_handle->cstate = USBPD_CAD_STATE_RESET;
_handle->cc = CCNONE;
_handle->CurrentHWcondition = HW_Detachment;
_handle->SNK_Source_Current_Adv = vRd_Undefined;
Ports[PortNum].USBPD_CAD_WakeUp = WakeUp;
/* Initialise the USBPD_IP */
Ports[PortNum].husbpd = USBPD_HW_GetUSPDInstance(PortNum);
/* initialise usbpd */
LL_UCPD_StructInit(&settings);
(void)LL_UCPD_Init(Ports[PortNum].husbpd, &settings);
LL_UCPD_SetRxOrderSet(Ports[PortNum].husbpd, LL_UCPD_ORDERSET_SOP | LL_UCPD_ORDERSET_SOP1 | LL_UCPD_ORDERSET_SOP2 | LL_UCPD_ORDERSET_CABLERST | LL_UCPD_ORDERSET_HARDRST);
#ifdef _LOW_POWER
LL_UCPD_WakeUpEnable(Ports[PortNum].husbpd);
#endif
LL_PWR_DisableUSBDeadBattery(); /* PWR->CR3 |= (1 << 14); */
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB); /* GPIOB enable RCC->AHB2ENR |= 2; */
#if defined(MB1397) /* in case of eval G4 */
/* Not needed on MB1397 */
#elif defined (USE_UNIBOARD) || defined(MB1428) || defined(MB1367) /* in case of Nucleo G4 */
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_4, LL_GPIO_MODE_ANALOG); /* PB4 mode = GP analog => CC2 */
#else
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_4, LL_GPIO_MODE_OUTPUT); /* PB4 mode = GP output => CC2 */
#endif
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_4, LL_GPIO_PULL_NO);
/* Triming */
/* Ports[PortNum].husbpd->RESERVED0 = 0x1D0F1D0F;*/
/* Enable USBPD IP */
LL_UCPD_Enable(Ports[PortNum].husbpd);
/* Controls whether for pull-ups and pull-downs controls related to ANAMODE and ANASUBMODE should be applied to CC1 and CC2 analog PHYs */
/* Should be done when UCPDEN is 1 */
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_CC1CC2);
#if defined(_SRC) || defined(_DRP)
/* Intialise usbpd interrupt */
if (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].params->PE_PowerRole)
{
USBPDM1_AssertRp(PortNum);
}
else
#endif /* _SRC || _DRP */
{
USBPDM1_AssertRd(PortNum);
}
}
/**
* @brief funtion to force CAD state machine into error recovery state
* @param PortNum Index of current used port
* @retval None
*/
void CAD_Enter_ErrorRecovery(uint8_t PortNum)
{
/* remove the ucpd resistor */
USBPDM1_EnterErrorRecovery(PortNum);
/* set the error recovery flag to allow the stack to switch into errorRecovery Flag */
CAD_HW_Handles[PortNum].CAD_ErrorRecoveryflag = USBPD_TRUE;
Ports[PortNum].USBPD_CAD_WakeUp();
}
/**
* @brief function to force the value of the RP resistor
* @note Must be called only if you want change the settings value
* @param PortNum Index of current used port
* @param RpValue RP value to set in devices based on @ref CAD_RP_Source_Current_Adv_Typedef
* @retval 0 success else error
*/
uint32_t CAD_Set_ResistorRp(uint8_t PortNum, CAD_RP_Source_Current_Adv_Typedef RpValue)
{
#if defined(_DRP) || defined(_SRC)
/* update the information about the default resitor value presented in detach mode */
Ports[PortNum].params->RpResistor = RpValue;
/* inform state machine about a resistor update */
CAD_HW_Handles[PortNum].CAD_ResistorUpdateflag = USBPD_TRUE;
Ports[PortNum].USBPD_CAD_WakeUp();
#endif /* _SRC || _DRP */
return 0;
}
/**
* @brief CAD State machine
* @param PortNum Port
* @param pEvent Pointer on CAD event based on @ref USBPD_CAD_EVENT
* @param pCCXX Pointer on CC Pin based on @ref CCxPin_TypeDef
* @retval Timeout value
*/
uint32_t CAD_StateMachine(uint8_t PortNum, USBPD_CAD_EVENT *pEvent, CCxPin_TypeDef *pCCXX)
{
CAD_HW_HandleTypeDef *_handle = &CAD_HW_Handles[PortNum];
uint32_t _timing = 2;
/* set by default event to none */
*pEvent = USBPD_CAD_EVENT_NONE;
if (USBPD_TRUE == Ports[PortNum].params->PE_SwapOngoing)
{
return _timing;
}
if (_handle->CAD_ErrorRecoveryflag == USBPD_TRUE)
{
/* Force the state error recovery */
_handle->CAD_ErrorRecoveryflag = USBPD_FALSE;
_handle->cstate = USBPD_CAD_STATE_ERRORRECOVERY;
#if defined(__DEBUG_CAD) && defined(_TRACE)
USBPD_TRACE_Add(USBPD_TRACE_CAD_LOW, PortNum, (uint8_t)_handle->cstate, NULL, 0);
#endif
}
/*Check CAD STATE*/
switch (_handle->cstate)
{
case USBPD_CAD_STATE_SWITCH_TO_SRC:
case USBPD_CAD_STATE_SWITCH_TO_SNK:
{
#if defined(_DRP)
if (Ports[PortNum].settings->CAD_RoleToggle == USBPD_TRUE)
{
if (USBPD_CAD_STATE_SWITCH_TO_SRC == _handle->cstate)
{
USBPDM1_AssertRp(PortNum);
Ports[PortNum].params->PE_PowerRole = USBPD_PORTPOWERROLE_SRC;
_timing = Ports[PortNum].settings->CAD_SRCToggleTime;
}
if (USBPD_CAD_STATE_SWITCH_TO_SNK == _handle->cstate)
{
USBPDM1_AssertRd(PortNum);
Ports[PortNum].params->PE_PowerRole = USBPD_PORTPOWERROLE_SNK;
_timing = Ports[PortNum].settings->CAD_SNKToggleTime;
}
_handle->CAD_tToogle_start = HAL_GetTick();
}
#endif /* _DRP */
/* execute detach to present the new resistor according the configuration selected */
HW_SignalDetachment(PortNum);
_handle->cstate = USBPD_CAD_STATE_DETACHED;
break;
}
case USBPD_CAD_STATE_RESET:
{
/* on the first call of the usbpd state machine the interrupt and CC pin are enabled */
LL_UCPD_EnableIT_TypeCEventCC2(Ports[PortNum].husbpd);
LL_UCPD_EnableIT_TypeCEventCC1(Ports[PortNum].husbpd);
#if defined(_DRP)
if (Ports[PortNum].settings->CAD_RoleToggle == USBPD_TRUE)
{
_handle->CAD_tToogle_start = HAL_GetTick();
}
#endif /* _DRP */
if (0 == PortNum)
{
UCPD_INSTANCE0_ENABLEIRQ;
}
_handle->cstate = USBPD_CAD_STATE_DETACHED;
break;
}
case USBPD_CAD_STATE_DETACHED:
_timing = ManageStateDetached(PortNum);
break;
/*CAD STATE ATTACHED WAIT*/
case USBPD_CAD_STATE_ATTACHED_WAIT:
_timing = ManageStateAttachedWait(PortNum, pEvent, pCCXX);
break;
/* CAD ELECTRONIC CABLE ATTACHED */
case USBPD_CAD_STATE_EMC :
_timing = ManageStateEMC(PortNum, pEvent, pCCXX);
break;
/*CAD electronic cable with Sink ATTACHED*/
case USBPD_CAD_STATE_ATTEMC:
case USBPD_CAD_STATE_ATTACHED:
_timing = ManageStateAttached(PortNum, pEvent, pCCXX);
break;
/*CAD STATE AUDIO ACCESSORY ATTACHED*/
case USBPD_CAD_STATE_ACCESSORY:
{
_timing = CAD_INFINITE_TIME;
CAD_Check_HW(PortNum);
if (_handle->CurrentHWcondition != HW_AudioAdapter_Attachment)
{
HW_SignalDetachment(PortNum);
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SNK;
*pCCXX = CCNONE;
_handle->cc = CCNONE;
*pEvent = USBPD_CAD_EVENT_DETACHED;
}
break;
}
/*CAD STATE DEBUG ACCESSORY MODE ATTACHED*/
case USBPD_CAD_STATE_DEBUG:
{
_timing = CAD_INFINITE_TIME;
CAD_Check_HW(PortNum);
if (_handle->CurrentHWcondition != HW_Debug_Attachment)
{
HW_SignalDetachment(PortNum);
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SNK;
*pCCXX = CCNONE;
_handle->cc = CCNONE;
*pEvent = USBPD_CAD_EVENT_DETACHED;
}
break;
}
case USBPD_CAD_STATE_ERRORRECOVERY :
{
/* Remove the resistor */
/* Enter recovery = Switch to SRC with no resistor */
USBPDM1_EnterErrorRecovery(PortNum);
/* forward detach event to DPM */
*pCCXX = CCNONE;
_handle->cc = CCNONE;
*pEvent = USBPD_CAD_EVENT_DETACHED;
/* start tErrorRecovery timeout */
_handle->CAD_tDebounce_start = HAL_GetTick();
_timing = CAD_TERROR_RECOVERY_TIME;
_handle->cstate = USBPD_CAD_STATE_ERRORRECOVERY_EXIT;
break;
}
case USBPD_CAD_STATE_ERRORRECOVERY_EXIT :
{
if ((HAL_GetTick() - _handle->CAD_tDebounce_start) > CAD_TERROR_RECOVERY_TIME)
{
/* reconfigure the port
port source to src
port snk to snk
port drp to src */
#if defined(_DRP) || defined(_SRC)
if ((USBPD_PORTPOWERROLE_SRC == Ports[PortNum].settings->PE_DefaultRole) || (Ports[PortNum].settings->CAD_RoleToggle == USBPD_TRUE))
{
USBPDM1_AssertRp(PortNum);
Ports[PortNum].params->PE_PowerRole = USBPD_PORTPOWERROLE_SRC;
}
#if defined(_DRP)
else
#endif /* _DRP */
#endif /* _DRP || _SRC */
#if defined(_DRP) || defined(_SNK)
{
USBPDM1_AssertRd(PortNum);
Ports[PortNum].params->PE_PowerRole = USBPD_PORTPOWERROLE_SNK;
}
#endif /* _DRP || _SNK */
/* switch to state detach */
_handle->CAD_tDebounce_start = HAL_GetTick();
_handle->cstate = USBPD_CAD_STATE_DETACHED;
}
break;
}
default :
break;
}
#if defined(__DEBUG_CAD) && defined(_TRACE)
if (_handle->cstate != _handle->pstate)
{
_handle->pstate = _handle->cstate;
USBPD_TRACE_Add(USBPD_TRACE_CAD_LOW, PortNum, (uint8_t)_handle->cstate, NULL, 0);
}
#endif /* defined(__DEBUG_CAD) && defined(_TRACE) */
#if !defined(_RTOS)
/* Delay added for CC detection issue on NRTOS version */
HAL_Delay(1);
#endif /*_RTOS*/
return _timing;
}
/**
* @}
*/
/** @addtogroup USBPD_DEVICE_CAD_HW_IF_Private_Functions
* @{
*/
#if defined(_DRP) || defined(_SNK)
/**
* @brief Check if VBus is present or not
* @param PortNum port
* @retval Return 1 is VBUS is present (0 otherwise)
*/
uint8_t CAD_Check_VBus(uint8_t PortNum)
{
return (HW_IF_PWR_GetVoltage(PortNum) > BSP_PWR_HIGH_VBUS_THRESHOLD) ? USBPD_TRUE : USBPD_FALSE;
}
#endif /* _DRP || _SNK */
/**
* @brief Check CCx HW condition
* @param PortNum port
* @retval none
*/
void CAD_Check_HW(uint8_t PortNum)
{
uint32_t CC1_value, CC2_value;
#if !defined(_RTOS)
uint32_t CC1_value_temp, CC2_value_temp;
#endif /* !_RTOS */
CAD_HW_HandleTypeDef *_handle = &CAD_HW_Handles[PortNum];
CCxPin_TypeDef _CCx = CCNONE;
/*
----------------------------------------------------------------------------
| ANAMODE | ANASUBMODE[1:0] | Notes | TYPEC_VSTATE_CCx[1:0] |
| | | | 00 | 01 | 10 | 11 |
----------------------------------------------------------------------------
| 0: Source | 00: Disabled |Disabled N/A | NA |
| | 01: Default USB Rp| |vRaDef|vRdDef|vOPENDef| |
| | 10: 1.5A Rp | |vRa1.5|vRd1.5|vOPEN1.5| NA |
| | 11: 3.0A Rp | |vRa3.0|vRd3.0|vOPEN3.0| NA |
-----------------------------------------------------------------------------
| 1: Sink | | |xx vRa|vRdUSB| vRd1.5 |vRd3.0|
-----------------------------------------------------------------------------
*/
CC1_value = Ports[PortNum].PIN_CC1;
CC2_value = Ports[PortNum].PIN_CC2;
#if !defined(_RTOS)
/* Workaround linked to issue with Ellisys test TD.PC.E5
- it seems that in NRTOS version, we detect a glitch during DRP transition SNK to SRC */
CC1_value_temp = Ports[PortNum].husbpd->SR & UCPD_SR_TYPEC_VSTATE_CC1; /*Ports[PortNum].PIN_CC1;*/
CC2_value_temp = Ports[PortNum].husbpd->SR & UCPD_SR_TYPEC_VSTATE_CC2; /*Ports[PortNum].PIN_CC2;*/
if ((CC1_value_temp != CC1_value) || (CC2_value_temp != CC2_value))
{
_handle->cc = _CCx;
return;
}
#endif /* !_RTOS */
_handle->CurrentHWcondition = HW_Detachment;
_handle->SNK_Source_Current_Adv = vRd_Undefined;
#if defined(_DRP) || defined(_SRC)
if (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].params->PE_PowerRole)
{
uint8_t cc2_index = (uint8_t)(CC2_value >> UCPD_SR_TYPEC_VSTATE_CC2_Pos);
switch (CC1_value)
{
case LL_UCPD_SRC_CC1_VRA :
{
CAD_HW_Condition_TypeDef _tab_hw1[] =
{HW_AudioAdapter_Attachment, HW_PwrCable_Sink_Attachment, HW_PwrCable_NoSink_Attachment};
_handle->CurrentHWcondition = _tab_hw1[cc2_index];
_CCx = CC2;
break;
}
case LL_UCPD_SRC_CC1_VRD :
{
CAD_HW_Condition_TypeDef _tab_hw2[] =
{HW_PwrCable_Sink_Attachment, HW_Debug_Attachment, HW_Attachment};
_handle->CurrentHWcondition = _tab_hw2[cc2_index];
_CCx = CC1;
break;
}
case LL_UCPD_SRC_CC1_OPEN:
{
CAD_HW_Condition_TypeDef _tab_hw3[] =
{HW_PwrCable_NoSink_Attachment, HW_Attachment, HW_Detachment};
_handle->CurrentHWcondition = _tab_hw3[cc2_index];
if (HW_Detachment != _handle->CurrentHWcondition)
{
_CCx = CC2;
}
break;
}
default:
break;
}
}
#if defined(_DRP)
else /* USBPD_PORTPOWERROLE_SNK */
#endif /* _DRP */
#endif /*_DRP || _SRC */
#if defined(_DRP) || defined(_SNK)
{
if ((CC1_value != LL_UCPD_SNK_CC1_VOPEN) && (CC2_value == LL_UCPD_SNK_CC2_VOPEN))
{
_handle->CurrentHWcondition = HW_Attachment;
_CCx = CC1;
switch (CC1_value)
{
case LL_UCPD_SNK_CC1_VRP:
_handle->SNK_Source_Current_Adv = vRd_USB;
break;
case LL_UCPD_SNK_CC1_VRP15A:
_handle->SNK_Source_Current_Adv = vRd_1_5A;
break;
case LL_UCPD_SNK_CC1_VRP30A:
_handle->SNK_Source_Current_Adv = vRd_3_0A;
break;
default:
break;
}
}
if ((CC2_value != LL_UCPD_SNK_CC2_VOPEN) && (CC1_value == LL_UCPD_SNK_CC1_VOPEN))
{
_handle->CurrentHWcondition = HW_Attachment;
_CCx = CC2;
switch (CC2_value)
{
case LL_UCPD_SNK_CC2_VRP:
_handle->SNK_Source_Current_Adv = vRd_USB;
break;
case LL_UCPD_SNK_CC2_VRP15A:
_handle->SNK_Source_Current_Adv = vRd_1_5A;
break;
case LL_UCPD_SNK_CC2_VRP30A:
_handle->SNK_Source_Current_Adv = vRd_3_0A;
break;
default:
break;
}
}
}
#endif /*_DRP || _SNK */
_handle->cc = _CCx;
}
static uint32_t ManageStateDetached(uint8_t PortNum)
{
uint32_t _timing = 2;
CAD_HW_HandleTypeDef *_handle = &CAD_HW_Handles[PortNum];
#if defined(_DRP) || defined(_SRC)
if ((_handle->CAD_ResistorUpdateflag == USBPD_TRUE) && (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].params->PE_PowerRole))
{
/* update the reistor value */
USBPDM1_AssertRp(PortNum);
_handle->CAD_ResistorUpdateflag = USBPD_FALSE;
/* let time to internal state machine update */
HAL_Delay(1);
}
#endif /* _DRP || _SRC */
CAD_Check_HW(PortNum);
/* Change the status on the basis of the HW event given by CAD_Check_HW() */
if (_handle->CurrentHWcondition == HW_Detachment)
{
#if defined(_DRP)
/* check if role switch must be perform and set the correct sleep time allowed */
if (USBPD_TRUE == Ports[PortNum].settings->CAD_RoleToggle)
{
switch (Ports[PortNum].params->PE_PowerRole)
{
case USBPD_PORTPOWERROLE_SRC :
if ((HAL_GetTick() - _handle->CAD_tToogle_start) > Ports[PortNum].settings->CAD_SRCToggleTime)
{
_handle->CAD_tToogle_start = HAL_GetTick();
Ports[PortNum].params->PE_PowerRole = USBPD_PORTPOWERROLE_SNK;
_timing = Ports[PortNum].settings->CAD_SNKToggleTime;
USBPDM1_AssertRd(PortNum);
}
break;
case USBPD_PORTPOWERROLE_SNK :
if ((HAL_GetTick() - _handle->CAD_tToogle_start) > Ports[PortNum].settings->CAD_SNKToggleTime)
{
_handle->CAD_tToogle_start = HAL_GetTick();
Ports[PortNum].params->PE_PowerRole = USBPD_PORTPOWERROLE_SRC;
_timing = Ports[PortNum].settings->CAD_SRCToggleTime;
USBPDM1_AssertRp(PortNum);
}
break;
default:
break;
}
}
else
#endif /* _DRP */
{
_timing = CAD_INFINITE_TIME;
}
}
else
{
if (_handle->CurrentHWcondition == HW_PwrCable_NoSink_Attachment)
{
_handle->cstate = USBPD_CAD_STATE_EMC;
}
else
{
BSP_PWR_VBUSInit(PortNum);
_handle->cstate = USBPD_CAD_STATE_ATTACHED_WAIT;
/* Get the time of this event */
_handle->CAD_tDebounce_start = HAL_GetTick();
_timing = CAD_TCCDEBOUCE_THRESHOLD + 1u;
}
}
return _timing;
}
static uint32_t ManageStateAttachedWait(uint8_t PortNum, USBPD_CAD_EVENT *pEvent, CCxPin_TypeDef *pCCXX)
{
uint32_t _timing = 2;
CAD_HW_HandleTypeDef *_handle = &CAD_HW_Handles[PortNum];
/* Evaluate elapsed time in Attach_Wait state */
_handle->CAD_tDebounce = HAL_GetTick() - _handle->CAD_tDebounce_start;
CAD_Check_HW(PortNum);
if ((_handle->CurrentHWcondition != HW_Detachment) && (_handle->CurrentHWcondition != HW_PwrCable_NoSink_Attachment))
{
#if defined(_DRP) || defined(_SRC)
if ((BSP_PWR_VBUSGetVoltage(PortNum) > BSP_PWR_LOW_VBUS_THRESHOLD)
&& (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].params->PE_PowerRole))
{
/* reset the timing because VBUS threshold not yet reached */
_handle->CAD_tDebounce_start = HAL_GetTick();
return CAD_TCCDEBOUCE_THRESHOLD;
}
#endif /* _DRP || _SRC */
/* Check tCCDebounce */
if (_handle->CAD_tDebounce > CAD_TCCDEBOUCE_THRESHOLD)
{
#if defined(_DRP) || defined(_SRC)
/* if tCCDebounce has expired state must be changed*/
if (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].params->PE_PowerRole)
{
switch (_handle->CurrentHWcondition)
{
case HW_Attachment:
HW_SignalAttachement(PortNum, _handle->cc);
_handle->cstate = USBPD_CAD_STATE_ATTACHED;
*pEvent = USBPD_CAD_EVENT_ATTACHED;
*pCCXX = _handle->cc;
break;
case HW_PwrCable_NoSink_Attachment:
_handle->cstate = USBPD_CAD_STATE_EMC;
*pEvent = USBPD_CAD_EVENT_EMC;
*pCCXX = _handle->cc;
break;
case HW_PwrCable_Sink_Attachment:
HW_SignalAttachement(PortNum, _handle->cc);
_handle->cstate = USBPD_CAD_STATE_ATTEMC;
*pEvent = USBPD_CAD_EVENT_ATTEMC;
break;
case HW_Debug_Attachment:
_handle->cstate = USBPD_CAD_STATE_DEBUG;
*pEvent = USBPD_CAD_EVENT_DEBUG;
break;
case HW_AudioAdapter_Attachment:
_handle->cstate = USBPD_CAD_STATE_ACCESSORY;
*pEvent = USBPD_CAD_EVENT_ACCESSORY;
break;
case HW_Detachment:
default:
/* could not occurs */
break;
} /* end of switch */
*pCCXX = _handle->cc;
_timing = 2;
}
#if defined(_DRP)
else /* Check state transition for SNK role */
#endif /* _DRP */
#endif /* _DRP || _SRC */
#if defined(_DRP) || defined(_SNK)
{
_timing = 2;
if (1u == CAD_Check_VBus(PortNum)) /* Check if Vbus is on */
{
_handle->cstate = USBPD_CAD_STATE_ATTACHED;
*pEvent = USBPD_CAD_EVENT_ATTACHED;
*pCCXX = _handle->cc;
HW_SignalAttachement(PortNum, _handle->cc);
}
}
#endif /* _DRP || _SNK */
}
else
{
_timing = CAD_TCCDEBOUCE_THRESHOLD - _handle->CAD_tDebounce;
}
/* reset the flag for CAD_tDebounce */
_handle->CAD_tDebounce_flag = USBPD_FALSE;
}
else /* CAD_HW_Condition[PortNum] = HW_Detachment */
{
#if defined(_DRP) || defined(_SNK)
if (USBPD_PORTPOWERROLE_SNK == Ports[PortNum].params->PE_PowerRole)
{
/* start counting of CAD_tDebounce */
if (USBPD_FALSE == _handle->CAD_tDebounce_flag)
{
_handle->CAD_tDebounce_start = HAL_GetTick();
_handle->CAD_tDebounce_flag = USBPD_TRUE;
_timing = CAD_TPDDEBOUCE_THRESHOLD;
}
else /* CAD_tDebounce already running */
{
/* evaluate CAD_tDebounce */
_handle->CAD_tDebounce = HAL_GetTick() - _handle->CAD_tDebounce_start;
if ((_handle->CAD_tDebounce > CAD_TPDDEBOUCE_THRESHOLD))
{
_handle->CAD_tDebounce_flag = USBPD_FALSE;
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SRC;
HW_SignalDetachment(PortNum);
}
}
}
#if defined(_DRP)
else /* (hcad->PortPowerRole != USBPD_PORTPOWERROLE_SNK)*/
#endif /* _DRP */
#endif /* _DRP || _SNK */
#if defined(_DRP) || defined(_SRC)
{
/* start counting of CAD_tDebounce */
if (USBPD_FALSE == _handle->CAD_tDebounce_flag)
{
_handle->CAD_tDebounce_start = HAL_GetTick();
_handle->CAD_tDebounce_flag = USBPD_TRUE;
_timing = CAD_TSRCDISCONNECT_THRESHOLD;
}
else /* CAD_tDebounce already running */
{
/* evaluate CAD_tDebounce */
_handle->CAD_tDebounce = HAL_GetTick() - _handle->CAD_tDebounce_start;
if (_handle->CAD_tDebounce > CAD_TSRCDISCONNECT_THRESHOLD)
{
_handle->CAD_tDebounce_flag = USBPD_FALSE;
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SNK;
HW_SignalDetachment(PortNum);
}
}
}
#endif /* _DRP || _SRC */
}
return _timing;
}
static uint32_t ManageStateEMC(uint8_t PortNum, USBPD_CAD_EVENT *pEvent, CCxPin_TypeDef *pCCXX)
{
uint32_t _timing = CAD_INFINITE_TIME;
CAD_HW_HandleTypeDef *_handle = &CAD_HW_Handles[PortNum];
CAD_Check_HW(PortNum);
/* Change the status on the basis of the HW event given by CAD_Check_HW() */
switch (_handle->CurrentHWcondition)
{
case HW_Detachment :
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SNK;
*pEvent = USBPD_CAD_EVENT_DETACHED;
*pCCXX = CCNONE;
_timing = 1;
break;
case HW_PwrCable_Sink_Attachment:
case HW_Attachment :
_handle->cstate = USBPD_CAD_STATE_ATTACHED_WAIT;
_handle->CAD_tDebounce_start = HAL_GetTick() - 5u; /* this is only to check cable presence */
_timing = 2;
break;
case HW_PwrCable_NoSink_Attachment:
default :
/* nothing to do still the same status */
#if defined(_DRP)
if (USBPD_TRUE == Ports[PortNum].settings->CAD_RoleToggle)
{
if ((HAL_GetTick() - _handle->CAD_tToogle_start) > Ports[PortNum].settings->CAD_SRCToggleTime)
{
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SNK;
}
_timing = 0;
}
#endif /* _DRP */
break;
}
return _timing;
}
static uint32_t ManageStateAttached(uint8_t PortNum, USBPD_CAD_EVENT *pEvent, CCxPin_TypeDef *pCCXX)
{
uint32_t _timing = 2;
CAD_HW_HandleTypeDef *_handle = &CAD_HW_Handles[PortNum];
#if defined(_DRP) || defined(_SRC)
if (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].params->PE_PowerRole)
{
uint32_t ccx = (Ports[PortNum].CCx == CC1) ? (Ports[PortNum].husbpd->SR & UCPD_SR_TYPEC_VSTATE_CC1) : (Ports[PortNum].husbpd->SR & UCPD_SR_TYPEC_VSTATE_CC2);
uint32_t comp = (Ports[PortNum].CCx == CC1) ? LL_UCPD_SRC_CC1_OPEN : LL_UCPD_SRC_CC2_OPEN;
if (comp == ccx)
{
/* start counting of CAD_tDebounce */
if (USBPD_FALSE == _handle->CAD_tDebounce_flag)
{
_handle->CAD_tDebounce_flag = USBPD_TRUE;
_handle->CAD_tDebounce_start = HAL_GetTick();
_timing = CAD_TSRCDISCONNECT_THRESHOLD;
}
else /* CAD_tDebounce already running */
{
/* evaluate CAD_tDebounce */
_handle->CAD_tDebounce = HAL_GetTick() - _handle->CAD_tDebounce_start;
if (_handle->CAD_tDebounce > CAD_TSRCDISCONNECT_THRESHOLD)
{
HW_SignalDetachment(PortNum);
#ifdef _DRP
USBPDM1_AssertRd(PortNum);
#endif
_handle->CAD_tDebounce_flag = USBPD_FALSE;
/* move inside state DETACH to avoid wrong VCONN level*/
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SNK;
*pEvent = USBPD_CAD_EVENT_DETACHED;
*pCCXX = CCNONE;
_timing = 0;
}
}
}
else
{
/* Reset tPDDebounce flag*/
_handle->CAD_tDebounce_flag = USBPD_FALSE;
_timing = CAD_INFINITE_TIME;
}
}
#if defined(_DRP)
else /* USBPD_PORTPOWERROLE_SNK case */
#endif /* _DRP */
#endif /*_DRP || _SRC */
#if defined(_DRP) || defined(_SNK)
{
if (CAD_Check_VBus(PortNum) == 0u) /* Check if Vbus is off */
{
_handle->CurrentHWcondition = HW_Detachment;
HW_SignalDetachment(PortNum);
/* restart the toggle time */
#if defined(_DRP)
_handle->CAD_tToogle_start = HAL_GetTick();
#endif /* _DRP */
_handle->cstate = USBPD_CAD_STATE_SWITCH_TO_SRC;
*pEvent = USBPD_CAD_EVENT_DETACHED;
*pCCXX = CCNONE;
_timing = 0;
}
else
{
_timing = 10;
}
}
#endif /*_DRP || _SNK */
return _timing;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,122 @@
/**
******************************************************************************
* @file usbpd_phy_hw_if.c
* @author MCD Application Team
* @brief This file contains phy interface control functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#define USBPD_HW_C
/* Includes ------------------------------------------------------------------*/
#include "usbpd_devices_conf.h"
#include "usbpd_hw.h"
/* Private typedef -----------------------------------------------------------*/
/* Variable containing ADC conversions results */
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
UCPD_TypeDef *USBPD_HW_GetUSPDInstance(uint8_t PortNum)
{
return UCPD_INSTANCE0;
}
DMA_Channel_TypeDef *USBPD_HW_Init_DMARxInstance(uint8_t PortNum)
{
LL_DMA_InitTypeDef DMA_InitStruct;
/* Initialise the DMA */
LL_DMA_StructInit(&DMA_InitStruct);
DMA_InitStruct.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
DMA_InitStruct.Mode = LL_DMA_MODE_NORMAL;
DMA_InitStruct.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
DMA_InitStruct.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
DMA_InitStruct.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
DMA_InitStruct.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
DMA_InitStruct.NbData = 0;
DMA_InitStruct.MemoryOrM2MDstAddress = 0x0;
DMA_InitStruct.PeriphOrM2MSrcAddress = 0x0;
DMA_InitStruct.Priority = LL_DMA_PRIORITY_HIGH;
/* Enable the clock */
UCPDDMA_INSTANCE0_CLOCKENABLE_RX;
/* Initialise the DMA */
DMA_InitStruct.PeriphRequest = UCPDDMA_INSTANCE0_REQUEST_RX;
(void)LL_DMA_Init(UCPDDMA_INSTANCE0_DMA_RX, UCPDDMA_INSTANCE0_LL_CHANNEL_RX, &DMA_InitStruct);
return UCPDDMA_INSTANCE0_CHANNEL_RX;
}
void USBPD_HW_DeInit_DMARxInstance(uint8_t PortNum)
{
(void)PortNum;
}
DMA_Channel_TypeDef *USBPD_HW_Init_DMATxInstance(uint8_t PortNum)
{
LL_DMA_InitTypeDef DMA_InitStruct;
/* Initialise the DMA */
LL_DMA_StructInit(&DMA_InitStruct);
DMA_InitStruct.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
DMA_InitStruct.Mode = LL_DMA_MODE_NORMAL;
DMA_InitStruct.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
DMA_InitStruct.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
DMA_InitStruct.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
DMA_InitStruct.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
DMA_InitStruct.NbData = 0;
DMA_InitStruct.MemoryOrM2MDstAddress = 0x0;
DMA_InitStruct.PeriphOrM2MSrcAddress = 0x0;
/* Enable the clock */
UCPDDMA_INSTANCE0_CLOCKENABLE_TX;
DMA_InitStruct.PeriphRequest = UCPDDMA_INSTANCE0_REQUEST_TX;
DMA_InitStruct.Priority = LL_DMA_PRIORITY_MEDIUM;
(void)LL_DMA_Init(UCPDDMA_INSTANCE0_DMA_TX, UCPDDMA_INSTANCE0_LL_CHANNEL_TX, &DMA_InitStruct);
return UCPDDMA_INSTANCE0_CHANNEL_TX;
}
void USBPD_HW_DeInit_DMATxInstance(uint8_t PortNum)
{
(void)PortNum;
}
uint32_t USBPD_HW_GetRpResistorValue(uint8_t PortNum)
{
(void)PortNum;
return LL_UCPD_RESISTOR_3_0A;
}
void USBPD_HW_SetFRSSignalling(uint8_t PortNum, uint8_t cc)
{
(void)PortNum;
/* Configure the GPIO with the AF corresponding to UCPD */
if (1u == cc)
{
/* FRS_TX1 PA2 (CC1) */
UCPDFRS_INSTANCE0_FRSCC1;
}
else
{
/* FRS_TX2 PB0 (CC2) */
UCPDFRS_INSTANCE0_FRSCC2;
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,188 @@
/**
******************************************************************************
* @file usbpd_hw_if_it.c
* @author MCD Application Team
* @brief This file contains HW interface interrupt routines.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usbpd_devices_conf.h"
#include "usbpd_core.h"
#include "usbpd_hw_if.h"
#include "usbpd_trace.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void PORTx_IRQHandler(uint8_t PortNum);
void USBPD_PORT0_IRQHandler(void)
{
PORTx_IRQHandler(USBPD_PORT_0);
}
void USBPD_PORT1_IRQHandler(void)
{
PORTx_IRQHandler(USBPD_PORT_1);
}
void PORTx_IRQHandler(uint8_t PortNum)
{
UCPD_TypeDef *hucpd = Ports[PortNum].husbpd;
uint32_t _interrupt = LL_UCPD_ReadReg(hucpd, SR);
static uint8_t ovrflag = 0;
if ((hucpd->IMR & _interrupt) != 0u)
{
/* TXIS no need to enable it all the transfert are done by DMA */
if (UCPD_SR_TXMSGDISC == (_interrupt & UCPD_SR_TXMSGDISC))
{
/* the message has been discarded */
LL_UCPD_ClearFlag_TxMSGDISC(hucpd);
CLEAR_BIT(Ports[PortNum].hdmatx->CCR, DMA_CCR_EN);
return;
}
if (UCPD_SR_TXMSGSENT == (_interrupt & UCPD_SR_TXMSGSENT))
{
/* Message has been fully transfered */
Ports[PortNum].cbs.USBPD_HW_IF_TxCompleted(PortNum);
CLEAR_BIT(Ports[PortNum].hdmatx->CCR, DMA_CCR_EN);
LL_UCPD_ClearFlag_TxMSGSENT(hucpd);
return;
}
if (UCPD_SR_TXMSGABT == (_interrupt & UCPD_SR_TXMSGABT))
{
LL_UCPD_ClearFlag_TxMSGABT(hucpd);
CLEAR_BIT(Ports[PortNum].hdmatx->CCR, DMA_CCR_EN);
return;
}
/* TXUND : tx underrun detected */
if (UCPD_SR_HRSTDISC == (_interrupt & UCPD_SR_HRSTDISC))
{
LL_UCPD_ClearFlag_TxHRSTDISC(hucpd);
return;
}
/* TXUND : tx underrun detected */
if (UCPD_SR_HRSTSENT == (_interrupt & UCPD_SR_HRSTSENT))
{
/* Mhum, the stack is not expected to get this answer */
LL_UCPD_ClearFlag_TxHRSTSENT(hucpd);
Ports[PortNum].cbs.USBPD_HW_IF_TX_HardResetCompleted(PortNum, USBPD_SOPTYPE_HARD_RESET);
return;
}
/* TXUND : tx underrun detected */
if (UCPD_SR_TXUND == (_interrupt & UCPD_SR_TXUND))
{
/* nothing to do the port partner check message integrity with CRC so PRL will repeat the sent
can be used for debug purpose */
LL_UCPD_ClearFlag_TxUND(hucpd);
return;
}
/* RXNE : not needeed the stack only perform transfer by DMA */
/* RXORDDET: not needed so stack will not enabled this interrupt */
if (UCPD_SR_RXORDDET == (_interrupt & UCPD_SR_RXORDDET))
{
LL_UCPD_ClearFlag_RxOrderSet(hucpd);
return;
}
/* check RXHRSTDET */
if (UCPD_SR_RXHRSTDET == (_interrupt & UCPD_SR_RXHRSTDET))
{
Ports[PortNum].cbs.USBPD_HW_IF_RX_ResetIndication(PortNum);
LL_UCPD_ClearFlag_RxHRST(hucpd);
return;
}
/* check RXOVR */
if (UCPD_SR_RXOVR == (_interrupt & UCPD_SR_RXOVR))
{
/* nothing to do the message will be discarded and the port partner retry the send */
ovrflag = 1;
LL_UCPD_ClearFlag_RxOvr(hucpd);
return;
}
/* check RXMSGEND an Rx message has been recieved */
if (UCPD_SR_RXMSGEND == (_interrupt & UCPD_SR_RXMSGEND))
{
/* for DMA mode add a control to check if the number of data recived is corresponding with the number of
data receive by USBPD */
uint16_t _datasize = (uint16_t)hucpd->RX_PAYSZ;
LL_UCPD_ClearFlag_RxMsgEnd(hucpd);
/* disable DMA */
CLEAR_BIT(Ports[PortNum].hdmarx->CCR, DMA_CCR_EN);
if (((_interrupt & UCPD_SR_RXERR) == 0u) && (ovrflag == 0u))
{
/* Rx message has been recieved without error */
Ports[PortNum].cbs.USBPD_HW_IF_RX_Completed(PortNum, hucpd->RX_ORDSET & UCPD_RX_ORDSET_RXORDSET, _datasize);
}
ovrflag = 0;
/* Ready for next transaction */
WRITE_REG(Ports[PortNum].hdmarx->CMAR, (uint32_t)Ports[PortNum].ptr_RxBuff);
WRITE_REG(Ports[PortNum].hdmarx->CNDTR, SIZE_MAX_PD_TRANSACTION_UNCHUNK);
SET_BIT(Ports[PortNum].hdmarx->CCR, DMA_CCR_EN);
return;
}
/* check TYPECEVT1IE/TYPECEVT1IE */
if (UCPD_SR_TYPECEVT1 == (_interrupt & UCPD_SR_TYPECEVT1))
{
Ports[PortNum].PIN_CC1 = hucpd->SR & UCPD_SR_TYPEC_VSTATE_CC1;
Ports[PortNum].PIN_CC2 = hucpd->SR & UCPD_SR_TYPEC_VSTATE_CC2;
/* clear both interrupt */
LL_UCPD_ClearFlag_TypeCEventCC1(hucpd);
Ports[PortNum].USBPD_CAD_WakeUp();
/* Wakeup CAD to check the detection event */
return;
}
/* check TYPECEVT1IE/TYPECEVT1IE */
if (UCPD_SR_TYPECEVT2 == (_interrupt & UCPD_SR_TYPECEVT2))
{
Ports[PortNum].PIN_CC1 = hucpd->SR & UCPD_SR_TYPEC_VSTATE_CC1;
Ports[PortNum].PIN_CC2 = hucpd->SR & UCPD_SR_TYPEC_VSTATE_CC2;
LL_UCPD_ClearFlag_TypeCEventCC2(hucpd);
/* Wakeup CAD to check the detection event */
Ports[PortNum].USBPD_CAD_WakeUp();
return;
}
/* check FRSEVTIE */
if (_interrupt != UCPD_SR_FRSEVT)
{
/* not yet handled */
}
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,432 @@
/**
******************************************************************************
* @file usbpd_phy.c
* @author MCD Application Team
* @brief This file contains PHY layer functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usbpd_def.h"
#include "usbpd_phy.h"
#include "usbpd_hw_if.h"
#include "usbpd_pwr_if.h"
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_DEVICE
* @{
*/
/** @addtogroup USBPD_DEVICE_PHY
* @brief This file contains PHY layer functions.
* @details Receive from PRL a message and create a structured packet (according to the USBPD specifications):
* |SOP|DATA:[HEADER|DATAOBJECTS]|CRC|EOP|
* @{
*/
/* Private defines -----------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/** @defgroup USBPD_DEVICE_PHY_Private_typedef USBPD DEVICE PHY Private typedef
* @brief Structures and enums internally used by the PHY layer
* @{
*/
/**
* @brief Handle to support the data of the layer
*/
typedef struct
{
const USBPD_PHY_Callbacks *cbs;
uint32_t SupportedSOP; /*!<bit field SOP"Debug SOP'Debug SOP" SOP' SOP */
} PHY_HandleTypeDef;
/**
* @brief prototype definition shared in several callbacks
*/
typedef void (*PHY_CB_t)(uint8_t PortNum, USBPD_SOPType_TypeDef Type); /*!< Common callback definition */
/**
* @}
*/
/* Private define and macro --------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup USBPD_DEVICE_PHY_Private_variables USBPD DEVICE PHY Private variables
* @{
*/
/** Internal struct for RXTX */
static PHY_HandleTypeDef PHY_Ports[USBPD_PORT_COUNT];
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/** @defgroup USBPD_DEVICE_PHY_Private_functions USBPD DEVICE PHY Private functions
* @{
*/
USBPD_StatusTypeDef PHY_PortInit(uint8_t PortNum, const USBPD_PHY_Callbacks *cbs, uint8_t *pRxBuffer, uint32_t SupportedSOP);
void PHY_BistCompleted(uint8_t PortNum, USBPD_BISTMsg_TypeDef bistmode);
void PHY_TxCompleted(uint8_t portnum);
void PHY_ResetCompleted(uint8_t PortNum, USBPD_SOPType_TypeDef Type);
USBPD_PHY_RX_Status_TypeDef PHY_Rx_Reset(uint8_t PortNum);
void PHY_Rx_HardReset(uint8_t PortNum);
void PHY_Rx_Completed(uint8_t PortNum, uint32_t MsgType, uint16_t RxPaySize);
/**
* @}
*/
/* Exported functions ---------------------------------------------------------*/
/** @defgroup USBPD_DEVICE_PHY_Exported_Functions USBPD DEVICE PHY Exported functions
* @{
*/
/**
* @brief Initialize the PHY of a specified port.
* @param PortNum Number of the port.
* @param pCallbacks PHY callbacks
* @param pRxBuffer Buffer to storage received message.
* @param PowerRole Power Role of the board.
* @param SupportedSOP bit field of the supported SOP
* @retval status @ref USBPD_OK
*/
USBPD_StatusTypeDef USBPD_PHY_Init(uint8_t PortNum, const USBPD_PHY_Callbacks *pCallbacks, uint8_t *pRxBuffer, USBPD_PortPowerRole_TypeDef PowerRole, uint32_t SupportedSOP)
{
(void)PowerRole;
/* set all callbacks */
Ports[PortNum].cbs.USBPD_HW_IF_TxCompleted = PHY_TxCompleted;
Ports[PortNum].cbs.USBPD_HW_IF_BistCompleted = PHY_BistCompleted;
Ports[PortNum].cbs.USBPD_HW_IF_RX_Reset = PHY_Rx_Reset;
Ports[PortNum].cbs.USBPD_HW_IF_RX_ResetIndication = PHY_Rx_HardReset;
Ports[PortNum].cbs.USBPD_HW_IF_RX_Completed = PHY_Rx_Completed;
Ports[PortNum].cbs.USBPD_HW_IF_TX_HardResetCompleted = PHY_ResetCompleted;
/* Initialize the hardware for the port */
Ports[PortNum].ptr_RxBuff = pRxBuffer;
/* Initialize port related functionalities inside this layer */
PHY_Ports[PortNum].SupportedSOP = SupportedSOP;
PHY_Ports[PortNum].cbs = pCallbacks;
return USBPD_OK;
}
/**
* @brief this function return the retry counter value in us.
* @note time used to determine when the protocol layer must re-send a message not aknowledge by a goodCRC
* @param PortNum Number of the port.
* @retval retry counter value in us.
*/
uint16_t USBPD_PHY_GetRetryTimerValue(uint8_t PortNum)
{
(void)PortNum;
return 945u;
}
/**
* @brief Reset the PHY of a specified port.
* @param PortNum Number of the port.
* @retval None
*/
void USBPD_PHY_Reset(uint8_t PortNum)
{
(void)PortNum;
/* reset PHY layer */
/* reset HW_IF layer */
}
/**
* @brief Request to send a reset on a port.
* @param PortNum Number of the port
* @param Type Type of reset (hard or cable reset) @ref USBPD_SOPTYPE_HARD_RESET or @ref USBPD_SOPTYPE_CABLE_RESET
* @retval status @ref USBPD_OK
*/
USBPD_StatusTypeDef USBPD_PHY_ResetRequest(uint8_t PortNum, USBPD_SOPType_TypeDef Type)
{
/* Send the requested reset */
return USBPD_PHY_SendMessage(PortNum, Type, NULL, 0);
}
/**
* @brief Send a Message.
* @param PortNum Number of the port
* @param Type Type of the message
* @param pBuffer Pointer to the buffer to be transmitted
* @param Size Size of the buffer (bytes)
* @retval status @ref USBPD_OK
*/
USBPD_StatusTypeDef USBPD_PHY_SendMessage(uint8_t PortNum, USBPD_SOPType_TypeDef Type, uint8_t *pBuffer, uint16_t Size)
{
/* Trace to track message */
return USBPD_HW_IF_SendBuffer(PortNum, Type, pBuffer, Size);
}
/**
* @brief Send BIST pattern.
* @param PortNum Number of the port
* @retval status @ref USBPD_OK
*/
USBPD_StatusTypeDef USBPD_PHY_Send_BIST_Pattern(uint8_t PortNum)
{
/* Call the low-level function (HW_IF) to accomplish the BIST Carrier Mode Transmission */
USBPD_HW_IF_Send_BIST_Pattern(PortNum);
return USBPD_OK;
}
/**
* @brief Request PHY to exit of BIST mode 2
* @param PortNum port number value
* @param mode SOP BIST MODE 2
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PHY_ExitTransmit(uint8_t PortNum, USBPD_SOPType_TypeDef mode)
{
if (USBPD_SOPTYPE_BIST_MODE_2 == mode)
{
USBPD_HW_IF_StopBISTMode2(PortNum);
}
return USBPD_OK;
}
/**
* @brief Set the SinkTxNg value of the resistor,
* @note used to manage the collision avoidance
* @param PortNum Number of the port
* @retval None
*/
void USBPD_PHY_SetResistor_SinkTxNG(uint8_t PortNum)
{
USBPD_HW_IF_SetResistor_SinkTxNG(PortNum);
}
/**
* @brief function to set the SinkTxOK
* @note used to manage the collision avoidance
* @param PortNum Number of the port.
* @retval none.
*/
void USBPD_PHY_SetResistor_SinkTxOK(uint8_t PortNum)
{
USBPD_HW_IF_SetResistor_SinkTxOK(PortNum);
}
/**
* @brief function to set the supported SOP
* @param PortNum Number of the port.
* @param SOPSupported List of the supported SOP
* @retval None.
*/
void USBPD_PHY_SOPSupported(uint8_t PortNum, uint32_t SOPSupported)
{
PHY_Ports[PortNum].SupportedSOP = SOPSupported;
}
/**
* @brief Check if SinkTxOK is set or not
* @note used to manage the collision avoidance
* @param PortNum Number of the port.
* @retval USBPD_TRUE or USBPD_FALSE
*/
uint8_t USBPD_PHY_IsResistor_SinkTxOk(uint8_t PortNum)
{
return USBPD_HW_IF_IsResistor_SinkTxOk(PortNum);
}
/**
* @brief function to generate an FRS signalling
* @param PortNum Number of the port.
* @retval None.
*/
void USBPD_PHY_FastRoleSwapSignalling(uint8_t PortNum)
{
USBPD_HW_IF_FastRoleSwapSignalling(PortNum);
}
/**
* @brief function used to enable RX
* @param PortNum Number of the port.
* @retval None
*/
void USBPD_PHY_EnableRX(uint8_t PortNum)
{
USBPD_HW_IF_EnableRX(PortNum);
}
/**
* @brief function used to disable RX
* @param PortNum Number of the port.
* @retval None
*/
void USBPD_PHY_DisableRX(uint8_t PortNum)
{
USBPD_HW_IF_DisableRX(PortNum);
}
/**
* @}
*/
/** @addtogroup USBPD_DEVICE_PHY_Private_functions
* @brief PHY internally used functions
* @{
*/
/**
* @brief Reset completed notification.
* @param PortNum Number of the port
* @param Type PD Type
* @retval None
*/
void PHY_ResetCompleted(uint8_t PortNum, USBPD_SOPType_TypeDef Type)
{
/* perform a PHY layer reset */
USBPD_PHY_Reset(PortNum);
/* notify to upper level (PRL) */
if (PHY_Ports[PortNum].cbs->USBPD_PHY_ResetCompleted != NULL)
{
PHY_Ports[PortNum].cbs->USBPD_PHY_ResetCompleted(PortNum, Type);
}
}
/**
* @brief Callback to notify the bist is completed
* @param PortNum Number of the port.
* @param bistmode Modality of the bist.
* @retval none.
*/
void PHY_BistCompleted(uint8_t PortNum, USBPD_BISTMsg_TypeDef bistmode)
{
if (NULL != PHY_Ports[PortNum].cbs->USBPD_PHY_BistCompleted)
{
PHY_Ports[PortNum].cbs->USBPD_PHY_BistCompleted(PortNum, bistmode);
}
}
/**
* @brief Callback to notify the a transmission is completed
* @param PortNum Number of the port.
* @retval none.
*/
void PHY_TxCompleted(uint8_t PortNum)
{
if (NULL != PHY_Ports[PortNum].cbs->USBPD_PHY_TxCompleted)
{
PHY_Ports[PortNum].cbs->USBPD_PHY_TxCompleted(PortNum);
}
}
/**
* @brief Callback to notify the start of reception
* @param PortNum Number of the port.
* @retval Status of current reception.
*/
USBPD_PHY_RX_Status_TypeDef PHY_Rx_Reset(uint8_t PortNum)
{
(void)PortNum;
return USBPD_PHY_RX_STATUS_OK;
}
/**
* @brief Callback to notify the start of reception
* @param PortNum Number of the port.
* @retval Status of current reception.
*/
void PHY_Rx_HardReset(uint8_t PortNum)
{
/* nothing to do the message will be discarded and the port partner retry the send */
if (NULL != PHY_Ports[PortNum].cbs->USBPD_PHY_ResetIndication)
{
PHY_Ports[PortNum].cbs->USBPD_PHY_ResetIndication(PortNum, USBPD_SOPTYPE_HARD_RESET);
}
}
/**
* @brief Callback to notify the end of the current reception
* @param PortNum Number of the port.
* @param MsgType SOP Message Type
* @param RxPaySize Payload RX size
* @retval None.
*/
void PHY_Rx_Completed(uint8_t PortNum, uint32_t MsgType, uint16_t RxPaySize)
{
USBPD_SOPType_TypeDef _msgtype;
uint8_t _callphy = 1u;
(void)RxPaySize;
const USBPD_SOPType_TypeDef tab_sop_value[] = { USBPD_SOPTYPE_SOP, USBPD_SOPTYPE_SOP1, USBPD_SOPTYPE_SOP2,
USBPD_SOPTYPE_SOP1_DEBUG, USBPD_SOPTYPE_SOP2_DEBUG, USBPD_SOPTYPE_CABLE_RESET
};
_msgtype = tab_sop_value[MsgType];
/* check if the message must be forwarded to usbpd stack */
switch (_msgtype)
{
case USBPD_SOPTYPE_CABLE_RESET :
if (0x1Eu == (PHY_Ports[PortNum].SupportedSOP & 0x1Eu))
{
/* nothing to do the message will be discarded and the port partner retry the send */
if (PHY_Ports[PortNum].cbs->USBPD_PHY_ResetIndication != NULL)
{
PHY_Ports[PortNum].cbs->USBPD_PHY_ResetIndication(PortNum, USBPD_SOPTYPE_CABLE_RESET);
}
_callphy = 0u;
}
break;
case USBPD_SOPTYPE_SOP :
case USBPD_SOPTYPE_SOP1 :
case USBPD_SOPTYPE_SOP2 :
case USBPD_SOPTYPE_SOP1_DEBUG :
case USBPD_SOPTYPE_SOP2_DEBUG :
if ((uint8_t)(0x1u << _msgtype) != (PHY_Ports[PortNum].SupportedSOP & (uint8_t)(0x1u << _msgtype)))
{
_callphy = 0u;
}
break;
default :
_callphy = 0u;
break;
}
if ((1u == _callphy) && (PHY_Ports[PortNum].cbs->USBPD_PHY_MessageReceived != NULL))
{
PHY_Ports[PortNum].cbs->USBPD_PHY_MessageReceived(PortNum, _msgtype);
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,334 @@
/**
******************************************************************************
* @file usbpd_phy_hw_if.c
* @author MCD Application Team
* @brief This file contains phy interface control functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#define USBPD_PHY_HW_IF_C
#include "usbpd_devices_conf.h"
#include "usbpd_hw.h"
#include "usbpd_core.h"
#include "usbpd_hw_if.h"
#include "usbpd_timersserver.h"
/* Private typedef -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
USBPD_PORT_HandleTypeDef Ports[USBPD_PORT_COUNT];
/* Private functions ---------------------------------------------------------*/
void USBPD_HW_IF_GlobalHwInit(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPUART1);
/* Init timer to detect the reception of goodCRC */
USBPD_TIM_Init();
/* Initialise VBUS power */
(void)BSP_PWR_VBUSInit(0);
}
void USBPD_HW_IF_StopBISTMode2(uint8_t PortNum)
{
uint32_t _cr = READ_REG(Ports[PortNum].husbpd->CR) & ~(UCPD_CR_TXMODE | UCPD_CR_TXSEND);
LL_UCPD_Disable(Ports[PortNum].husbpd);
LL_UCPD_Enable(Ports[PortNum].husbpd);
Ports[PortNum].husbpd->CR = _cr;
}
USBPD_StatusTypeDef USBPD_HW_IF_SendBuffer(uint8_t PortNum, USBPD_SOPType_TypeDef Type, uint8_t *pBuffer, uint32_t Size)
{
USBPD_StatusTypeDef _status = USBPD_OK;
if (USBPD_SOPTYPE_HARD_RESET == Type)
{
LL_UCPD_SendHardReset(Ports[PortNum].husbpd);
}
else
{
switch (Type)
{
case USBPD_SOPTYPE_SOP :
{
LL_UCPD_WriteTxOrderSet(Ports[PortNum].husbpd, LL_UCPD_ORDERED_SET_SOP);
LL_UCPD_SetTxMode(Ports[PortNum].husbpd, LL_UCPD_TXMODE_NORMAL);
break;
}
case USBPD_SOPTYPE_SOP1 :
{
LL_UCPD_WriteTxOrderSet(Ports[PortNum].husbpd, LL_UCPD_ORDERED_SET_SOP1);
LL_UCPD_SetTxMode(Ports[PortNum].husbpd, LL_UCPD_TXMODE_NORMAL);
break;
}
case USBPD_SOPTYPE_SOP2 :
{
LL_UCPD_WriteTxOrderSet(Ports[PortNum].husbpd, LL_UCPD_ORDERED_SET_SOP2);
LL_UCPD_SetTxMode(Ports[PortNum].husbpd, LL_UCPD_TXMODE_NORMAL);
break;
}
case USBPD_SOPTYPE_CABLE_RESET :
{
LL_UCPD_SetTxMode(Ports[PortNum].husbpd, LL_UCPD_TXMODE_CABLE_RESET);
break;
}
case USBPD_SOPTYPE_BIST_MODE_2 :
{
LL_UCPD_SetTxMode(Ports[PortNum].husbpd, LL_UCPD_TXMODE_BIST_CARRIER2);
break;
}
default :
_status = USBPD_ERROR;
break;
}
if (USBPD_OK == _status)
{
WRITE_REG(Ports[PortNum].hdmatx->CMAR, (uint32_t)pBuffer);
WRITE_REG(Ports[PortNum].hdmatx->CNDTR, Size);
Ports[PortNum].hdmatx->CCR |= DMA_CCR_EN;
LL_UCPD_WriteTxPaySize(Ports[PortNum].husbpd, Size);
LL_UCPD_SendMessage(Ports[PortNum].husbpd);
}
}
return _status;
}
void USBPD_HW_IF_Send_BIST_Pattern(uint8_t PortNum)
{
LL_UCPD_SetTxMode(Ports[PortNum].husbpd, LL_UCPD_TXMODE_BIST_CARRIER2);
LL_UCPD_SendMessage(Ports[PortNum].husbpd);
}
void USBPDM1_AssertRp(uint8_t PortNum)
{
switch (Ports[PortNum].params->RpResistor)
{
case vRp_Default :
LL_UCPD_SetRpResistor(Ports[PortNum].husbpd, LL_UCPD_RESISTOR_DEFAULT);
break;
case vRp_1_5A:
LL_UCPD_SetRpResistor(Ports[PortNum].husbpd, LL_UCPD_RESISTOR_1_5A);
break;
case vRp_3_0A:
LL_UCPD_SetRpResistor(Ports[PortNum].husbpd, LL_UCPD_RESISTOR_3_0A);
break;
default:
break;
}
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_NONE);
LL_UCPD_SetSRCRole(Ports[PortNum].husbpd);
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_CC1CC2);
}
void USBPDM1_DeAssertRp(uint8_t PortNum)
{
/* not needed on G4, so nothing to do, keep only for compatibility */
UNUSED(PortNum);
}
void USBPDM1_AssertRd(uint8_t PortNum)
{
LL_UCPD_TypeCDetectionCC2Disable(Ports[PortNum].husbpd);
LL_UCPD_TypeCDetectionCC1Disable(Ports[PortNum].husbpd);
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_NONE);
LL_UCPD_SetSNKRole(Ports[PortNum].husbpd);
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_CC1CC2);
HAL_Delay(1);
LL_UCPD_TypeCDetectionCC2Enable(Ports[PortNum].husbpd);
LL_UCPD_TypeCDetectionCC1Enable(Ports[PortNum].husbpd);
}
void USBPDM1_EnterErrorRecovery(uint8_t PortNum)
{
LL_UCPD_SetSRCRole(Ports[PortNum].husbpd);
LL_UCPD_SetRpResistor(Ports[PortNum].husbpd, LL_UCPD_RESISTOR_NONE);
}
void USBPDM1_DeAssertRd(uint8_t PortNum)
{
/* not needed on G4, so nothing to do, keep only for compatibility */
UNUSED(PortNum);
}
void USBPDM1_Set_CC(uint8_t PortNum, CCxPin_TypeDef cc)
{
/* Set the correct pin on the comparator*/
Ports[PortNum].CCx = cc;
LL_UCPD_SetCCPin(Ports[PortNum].husbpd , (cc == CC1) ? LL_UCPD_CCPIN_CC1 : LL_UCPD_CCPIN_CC2);
}
void USBPDM1_RX_EnableInterrupt(uint8_t PortNum)
{
/* Enable the RX interrupt process */
MODIFY_REG(Ports[PortNum].husbpd->IMR, UCPD_IMR_RXORDDETIE | UCPD_IMR_RXHRSTDETIE | UCPD_IMR_RXOVRIE | UCPD_IMR_RXMSGENDIE,
UCPD_IMR_RXORDDETIE | UCPD_IMR_RXHRSTDETIE | UCPD_IMR_RXOVRIE | UCPD_IMR_RXMSGENDIE);
LL_UCPD_RxDMAEnable(Ports[PortNum].husbpd);
}
void USBPD_HW_IF_EnableRX(uint8_t PortNum)
{
LL_UCPD_RxEnable(Ports[PortNum].husbpd);
}
void USBPD_HW_IF_DisableRX(uint8_t PortNum)
{
LL_UCPD_RxDisable(Ports[PortNum].husbpd);
}
void HW_SignalAttachement(uint8_t PortNum, CCxPin_TypeDef cc)
{
uint32_t _temp;
/* Init timer to detect the reception of goodCRC */
USBPD_TIM_Init();
/* Prepare ucpd to handle PD message
RX message start listen
TX prepare the DMA to be transfer ready
Detection listen only the line corresponding CC=Rd for SRC/SNK */
Ports[PortNum].hdmatx = USBPD_HW_Init_DMATxInstance(PortNum);
Ports[PortNum].hdmarx = USBPD_HW_Init_DMARxInstance(PortNum);
/* Set the RX dma to allow reception */
_temp = (uint32_t)&Ports[PortNum].husbpd->RXDR;
WRITE_REG(Ports[PortNum].hdmarx->CPAR, _temp);
WRITE_REG(Ports[PortNum].hdmarx->CMAR, (uint32_t)Ports[PortNum].ptr_RxBuff);
Ports[PortNum].hdmarx->CNDTR = SIZE_MAX_PD_TRANSACTION_UNCHUNK;
Ports[PortNum].hdmarx->CCR |= DMA_CCR_EN;
/* Set the TX dma only UCPD address */
_temp = (uint32_t)&Ports[PortNum].husbpd->TXDR;
Ports[PortNum].hdmatx->CPAR = _temp;
/* disabled non Rd line set CC line enable */
#define INTERRUPT_MASK UCPD_IMR_TXMSGDISCIE | UCPD_IMR_TXMSGSENTIE | UCPD_IMR_HRSTDISCIE | UCPD_IMR_HRSTSENTIE | UCPD_IMR_TXMSGABTIE |\
UCPD_IMR_TXUNDIE | UCPD_IMR_RXORDDETIE | UCPD_IMR_RXHRSTDETIE | UCPD_IMR_RXOVRIE | UCPD_IMR_RXMSGENDIE
MODIFY_REG(Ports[PortNum].husbpd->IMR, INTERRUPT_MASK, INTERRUPT_MASK);
/* Handle CC enable */
Ports[PortNum].CCx = cc;
if (USBPD_PORTPOWERROLE_SRC == Ports[PortNum].params->PE_PowerRole)
{
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_CC1CC2);
}
else
{
/* disconnect is done by VBUS */
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, (Ports[PortNum].CCx == CC1) ? LL_UCPD_CCENABLE_CC1 : LL_UCPD_CCENABLE_CC2);
}
/* Set CC pin for PD message */
LL_UCPD_SetCCPin(Ports[PortNum].husbpd, (Ports[PortNum].CCx == CC1) ? LL_UCPD_CCPIN_CC1 : LL_UCPD_CCPIN_CC2);
#if defined(USBPD_REV30_SUPPORT)
if (Ports[PortNum].settings->PE_PD3_Support.d.PE_FastRoleSwapSupport == USBPD_TRUE)
{
/* Set GPIO to allow the FRSTX handling */
USBPD_HW_SetFRSSignalling(PortNum, (Ports[PortNum].CCx == CC1) ? 1u : 2u);
}
#endif /* USBPD_REV30_SUPPORT */
#if defined(_VCONN_SUPPORT)
/* Initialize Vconn managment */
(void)BSP_PWR_VCONNInit(PortNum, (Ports[PortNum].CCx == CC1) ? 1u : 2u);
#endif /* _VCONN_SUPPORT */
/* Disable the Resistor on Vconn PIN */
(Ports[PortNum].CCx == CC1) ? LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_CC1) : LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_CC2);
/* Prepare the rx processing */
LL_UCPD_SetRxMode(Ports[PortNum].husbpd, LL_UCPD_RXMODE_NORMAL);
LL_UCPD_RxDMAEnable(Ports[PortNum].husbpd);
LL_UCPD_TxDMAEnable(Ports[PortNum].husbpd);
LL_UCPD_RxEnable(Ports[PortNum].husbpd);
}
void HW_SignalDetachment(uint8_t PortNum)
{
LL_UCPD_RxDMADisable(Ports[PortNum].husbpd);
LL_UCPD_TxDMADisable(Ports[PortNum].husbpd);
LL_UCPD_RxDisable(Ports[PortNum].husbpd);
/* Enable only detection interrupt */
WRITE_REG(Ports[PortNum].husbpd->IMR, UCPD_IMR_TYPECEVT1IE | UCPD_IMR_TYPECEVT2IE);
/* stop DMA RX/TX */
LL_UCPD_RxDMADisable(Ports[PortNum].husbpd);
LL_UCPD_TxDMADisable(Ports[PortNum].husbpd);
USBPD_HW_DeInit_DMATxInstance(PortNum);
USBPD_HW_DeInit_DMARxInstance(PortNum);
LL_UCPD_SetccEnable(Ports[PortNum].husbpd, LL_UCPD_CCENABLE_CC1CC2);
}
void USBPD_HW_IF_SetResistor_SinkTxNG(uint8_t PortNum)
{
/* set the resistor SinkTxNG 1.5A5V */
LL_UCPD_SetRpResistor(Ports[PortNum].husbpd, LL_UCPD_RESISTOR_1_5A);
}
void USBPD_HW_IF_SetResistor_SinkTxOK(uint8_t PortNum)
{
/* set the resistor SinkTxNG 3.0A5V */
LL_UCPD_SetRpResistor(Ports[PortNum].husbpd, LL_UCPD_RESISTOR_3_0A);
}
uint8_t USBPD_HW_IF_IsResistor_SinkTxOk(uint8_t PortNum)
{
switch (Ports[PortNum].CCx)
{
case CC1 :
if (Ports[PortNum].PIN_CC1 == LL_UCPD_SNK_CC1_VRP30A)
{
return USBPD_TRUE;
}
break;
case CC2 :
if (Ports[PortNum].PIN_CC2 == LL_UCPD_SNK_CC2_VRP30A)
{
return USBPD_TRUE;
}
break;
default:
break;
}
return USBPD_FALSE;
}
void USBPD_HW_IF_FastRoleSwapSignalling(uint8_t PortNum)
{
LL_UCPD_SignalFRSTX(Ports[PortNum].husbpd);
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,115 @@
/**
******************************************************************************
* @file usbpd_pwr_hw_if.c
* @author MCD Application Team
* @brief This file contains power hardware interface functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usbpd_hw_if.h"
#define _USBPD_POWER_DEBUG
#ifdef _USBPD_POWER_DEBUG
#include "usbpd_core.h"
#include "usbpd_trace.h"
#endif
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#if defined(_USBPD_POWER_DEBUG) && defined(_TRACE)
#define POWER_DEBUG(__MSG__,__SIZE__) USBPD_TRACE_Add(USBPD_TRACE_DEBUG, PortNum, 0,__MSG__,__SIZE__);
#else
#define POWER_DEBUG(__MSG__,__SIZE__)
#endif
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
USBPD_StatusTypeDef HW_IF_PWR_SetVoltage(uint8_t PortNum, uint16_t voltage)
{
UNUSED(PortNum);
UNUSED(voltage);
/* Section below has been commented for the P-Nucleo Demo */
/*
uint32_t off = (PortNum == 0) ? 0 : 2;
switch (voltage)
{
case 12000:
HAL_GPIO_WritePin(USBPDM1_POWSELs[0+off].GPIOx, USBPDM1_POWSELs[0+off].GPIO_Pin, GPIO_PIN_SET);
break;
case 5000:
default:
HAL_GPIO_WritePin(USBPDM1_POWSELs[0+off].GPIOx, USBPDM1_POWSELs[0+off].GPIO_Pin, GPIO_PIN_RESET);
break;
}
*/
return USBPD_OK;
}
uint16_t HW_IF_PWR_GetVoltage(uint8_t PortNum)
{
return (uint16_t)BSP_PWR_VBUSGetVoltage(PortNum);;
}
int16_t HW_IF_PWR_GetCurrent(uint8_t PortNum)
{
return (int16_t)BSP_PWR_VBUSGetCurrent(PortNum);;
}
#if defined(_SRC) || defined(_DRP)
USBPD_StatusTypeDef HW_IF_PWR_Enable(uint8_t PortNum, USBPD_FunctionalState state, CCxPin_TypeDef Cc, uint32_t VconnState, USBPD_PortPowerRole_TypeDef role)
{
UNUSED(role);
PWR_StatusTypeDef status;
if (USBPD_ENABLE == state)
{
#if defined(_VCONN_SUPPORT)
if (USBPD_TRUE == VconnState)
{
POWER_DEBUG((uint8_t *)"VCONN ON", 8);
(void)BSP_PWR_VCONNOn(PortNum, Cc);
}
#endif /* _VCONN_SUPPORT */
POWER_DEBUG((uint8_t *)"VBUS ON", 7);
status = BSP_PWR_VBUSOn(PortNum);
}
else
{
#if defined(_VCONN_SUPPORT)
if (VconnState == USBPD_TRUE)
{
POWER_DEBUG((uint8_t *)"VCONN OFF", 9);
(void)BSP_PWR_VCONNOff(PortNum, Cc);
}
#endif /* _VCONN_SUPPORT */
POWER_DEBUG((uint8_t *)"VBUS OFF", 8);
status = BSP_PWR_VBUSOff(PortNum);
}
return (status == PWR_OK) ? USBPD_OK : USBPD_FAIL;
}
#endif /* _SRC || _DRP */
USBPD_FunctionalState HW_IF_PWR_VBUSIsEnabled(uint8_t PortNum)
{
#if defined(_SRC)||defined(_DRP)
return (BSP_PWR_VBUSIsOn(PortNum) == 0) ? USBPD_DISABLE : USBPD_ENABLE;
#else
return USBPD_DISABLE;
#endif /* _SRC || _DRP */
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,170 @@
/**
******************************************************************************
* @file usbpd_timersserver.c
* @author MCD Application Team
* @brief This file contains timer server functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usbpd_devices_conf.h"
#include "usbpd_timersserver.h"
/** @addtogroup STM32_USBPD_LIBRARY
* @{
*/
/** @addtogroup USBPD_DEVICE
* @{
*/
/** @addtogroup USBPD_DEVICE_TIMESERVER
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
int8_t timer_initcounter = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initialize a timer to manage timing in us
* @retval None
*/
void USBPD_TIM_Init(void)
{
if (0 == timer_initcounter)
{
TIMX_CLK_ENABLE;
/***************************/
/* Time base configuration */
/***************************/
/* Counter mode: select up-counting mode */
LL_TIM_SetCounterMode(TIMX, LL_TIM_COUNTERMODE_UP);
/* Set the pre-scaler value to have TIMx counter clock equal to 1 MHz */
LL_TIM_SetPrescaler(TIMX, __LL_TIM_CALC_PSC(SystemCoreClock, 1000000u));
/* Set the auto-reload value to have a counter frequency of 100Hz */
LL_TIM_SetAutoReload(TIMX, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIMX), 100u));
/*********************************/
/* Output waveform configuration */
/*********************************/
/* Set output compare mode: TOGGLE */
LL_TIM_OC_SetMode(TIMX, TIMX_CHANNEL_CH1, LL_TIM_OCMODE_TOGGLE);
LL_TIM_OC_SetMode(TIMX, TIMX_CHANNEL_CH2, LL_TIM_OCMODE_TOGGLE);
LL_TIM_OC_SetMode(TIMX, TIMX_CHANNEL_CH3, LL_TIM_OCMODE_TOGGLE);
LL_TIM_OC_SetMode(TIMX, TIMX_CHANNEL_CH4, LL_TIM_OCMODE_TOGGLE);
/* Set output channel polarity: OC is active high */
LL_TIM_OC_SetPolarity(TIMX, TIMX_CHANNEL_CH1, LL_TIM_OCPOLARITY_HIGH);
LL_TIM_OC_SetPolarity(TIMX, TIMX_CHANNEL_CH2, LL_TIM_OCPOLARITY_HIGH);
LL_TIM_OC_SetPolarity(TIMX, TIMX_CHANNEL_CH3, LL_TIM_OCPOLARITY_HIGH);
LL_TIM_OC_SetPolarity(TIMX, TIMX_CHANNEL_CH4, LL_TIM_OCPOLARITY_HIGH);
/* Enable counter */
LL_TIM_EnableCounter(TIMX);
}
/* Enable the timer counter */
timer_initcounter++;
}
/**
* @brief UnInitialize a timer to manage timing in us
* @retval None
*/
void USBPD_TIM_DeInit(void)
{
timer_initcounter--;
if(0 == timer_initcounter)
{
TIMX_CLK_DISABLE;
}
}
/**
* @brief start the timer counting
* @param timer id @TIM_identifier
* @param time in us
* @retval None
*/
void USBPD_TIM_Start(TIM_identifier Id, uint32_t TimeUs)
{
/* Positionne l'evenement pour sa detection */
switch (Id)
{
case TIM_PORT0_CRC:
TIMX_CHANNEL1_SETEVENT;
break;
case TIM_PORT0_RETRY:
TIMX_CHANNEL2_SETEVENT;
break;
case TIM_PORT1_CRC:
TIMX_CHANNEL3_SETEVENT;
break;
case TIM_PORT1_RETRY:
TIMX_CHANNEL4_SETEVENT;
break;
default:
break;
}
}
/**
* @brief check timer expiration
* @param timer id @TIM_identifier
* @retval None
*/
uint32_t USBPD_TIM_IsExpired(TIM_identifier Id)
{
uint32_t _expired = 1u;
switch (Id)
{
case TIM_PORT0_CRC:
_expired = TIMX_CHANNEL1_GETFLAG(TIMX);
break;
case TIM_PORT0_RETRY:
_expired = TIMX_CHANNEL2_GETFLAG(TIMX);
break;
case TIM_PORT1_CRC:
_expired = TIMX_CHANNEL3_GETFLAG(TIMX);
break;
case TIM_PORT1_RETRY:
_expired = TIMX_CHANNEL4_GETFLAG(TIMX);
break;
default:
break;
}
return _expired;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/