1 /**************************************************************************//**
2  * @file     otg.h
3  * @version  V3.00
4  * @brief    M2351 series OTG driver header file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8  ******************************************************************************/
9 #ifndef __OTG_H__
10 #define __OTG_H__
11 
12 /*---------------------------------------------------------------------------------------------------------*/
13 /* Include related headers                                                                                 */
14 /*---------------------------------------------------------------------------------------------------------*/
15 #include "M2351.h"
16 
17 #ifdef __cplusplus
18 extern "C"
19 {
20 #endif
21 
22 
23 /** @addtogroup Standard_Driver Standard Driver
24   @{
25 */
26 
27 /** @addtogroup OTG_Driver OTG Driver
28   @{
29 */
30 
31 
32 /** @addtogroup OTG_EXPORTED_CONSTANTS OTG Exported Constants
33   @{
34 */
35 
36 
37 
38 /*---------------------------------------------------------------------------------------------------------*/
39 /* OTG constant definitions                                                                                */
40 /*---------------------------------------------------------------------------------------------------------*/
41 #define OTG_VBUS_EN_ACTIVE_HIGH      (0UL) /*!< USB VBUS power switch enable signal is active high. */
42 #define OTG_VBUS_EN_ACTIVE_LOW       (1UL) /*!< USB VBUS power switch enable signal is active low. */
43 #define OTG_VBUS_ST_VALID_HIGH       (0UL) /*!< USB VBUS power switch valid status is high. */
44 #define OTG_VBUS_ST_VALID_LOW        (1UL) /*!< USB VBUS power switch valid status is low. */
45 
46 
47 /*@}*/ /* end of group OTG_EXPORTED_CONSTANTS */
48 
49 
50 /** @addtogroup OTG_EXPORTED_FUNCTIONS OTG Exported Functions
51   @{
52 */
53 
54 /*---------------------------------------------------------------------------------------------------------*/
55 /*  Define Macros and functions                                                                            */
56 /*---------------------------------------------------------------------------------------------------------*/
57 
58 
59 /**
60   * @brief This macro is used to enable OTG function
61   * @param None
62   * @return None
63   * @details This macro will set OTGEN bit of OTG_CTL register to enable OTG function.
64   */
65 #define OTG_ENABLE()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->CTL |= OTG_CTL_OTGEN_Msk):(OTG->CTL |= OTG_CTL_OTGEN_Msk))
66 
67 /**
68   * @brief This macro is used to disable OTG function
69   * @param None
70   * @return None
71   * @details This macro will clear OTGEN bit of OTG_CTL register to disable OTG function.
72   */
73 #define OTG_DISABLE()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->CTL &= ~OTG_CTL_OTGEN_Msk):(OTG->CTL &= ~OTG_CTL_OTGEN_Msk))
74 
75 /**
76   * @brief This macro is used to enable USB PHY
77   * @param None
78   * @return None
79   * @details When the USB role is selected as OTG device, use this macro to enable USB PHY.
80   *          This macro will set OTGPHYEN bit of OTG_PHYCTL register to enable USB PHY.
81   */
82 #define OTG_ENABLE_PHY()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->PHYCTL |= OTG_PHYCTL_OTGPHYEN_Msk):(OTG->PHYCTL |= OTG_PHYCTL_OTGPHYEN_Msk))
83 
84 /**
85   * @brief This macro is used to disable USB PHY
86   * @param None
87   * @return None
88   * @details This macro will clear OTGPHYEN bit of OTG_PHYCTL register to disable USB PHY.
89   */
90 #define OTG_DISABLE_PHY()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->PHYCTL &= ~OTG_PHYCTL_OTGPHYEN_Msk):(OTG->PHYCTL &= ~OTG_PHYCTL_OTGPHYEN_Msk))
91 
92 /**
93   * @brief This macro is used to enable ID detection function
94   * @param None
95   * @return None
96   * @details This macro will set IDDETEN bit of OTG_PHYCTL register to enable ID detection function.
97   */
98 #define OTG_ENABLE_ID_DETECT()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->PHYCTL |= OTG_PHYCTL_IDDETEN_Msk):(OTG->PHYCTL |= OTG_PHYCTL_IDDETEN_Msk))
99 
100 /**
101   * @brief This macro is used to disable ID detection function
102   * @param None
103   * @return None
104   * @details This macro will clear IDDETEN bit of OTG_PHYCTL register to disable ID detection function.
105   */
106 #define OTG_DISABLE_ID_DETECT()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->PHYCTL &= ~OTG_PHYCTL_IDDETEN_Msk):(OTG->PHYCTL &= ~OTG_PHYCTL_IDDETEN_Msk))
107 
108 /**
109   * @brief This macro is used to enable OTG wake-up function
110   * @param None
111   * @return None
112   * @details This macro will set WKEN bit of OTG_CTL register to enable OTG wake-up function.
113   */
114 #define OTG_ENABLE_WAKEUP()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->CTL |= OTG_CTL_WKEN_Msk):(OTG->CTL |= OTG_CTL_WKEN_Msk))
115 
116 /**
117   * @brief This macro is used to disable OTG wake-up function
118   * @param None
119   * @return None
120   * @details This macro will clear WKEN bit of OTG_CTL register to disable OTG wake-up function.
121   */
122 #define OTG_DISABLE_WAKEUP()    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->CTL &= ~OTG_CTL_WKEN_Msk):(OTG->CTL &= ~OTG_CTL_WKEN_Msk))
123 
124 /**
125   * @brief This macro is used to set the polarity of USB_VBUS_EN pin
126   * @param[in] u32Pol The polarity selection. Valid values are listed below.
127   *                    - \ref OTG_VBUS_EN_ACTIVE_HIGH
128   *                    - \ref OTG_VBUS_EN_ACTIVE_LOW
129   * @return None
130   * @details This macro is used to set the polarity of external USB VBUS power switch enable signal.
131   */
132 #define OTG_SET_VBUS_EN_POL(u32Pol)    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->PHYCTL = (OTG_NS->PHYCTL & (~OTG_PHYCTL_VBENPOL_Msk)) | ((u32Pol)<<OTG_PHYCTL_VBENPOL_Pos)):(OTG->PHYCTL = (OTG->PHYCTL & (~OTG_PHYCTL_VBENPOL_Msk)) | ((u32Pol)<<OTG_PHYCTL_VBENPOL_Pos)))
133 
134 /**
135   * @brief This macro is used to set the polarity of USB_VBUS_ST pin
136   * @param[in] u32Pol The polarity selection. Valid values are listed below.
137   *                    - \ref OTG_VBUS_ST_VALID_HIGH
138   *                    - \ref OTG_VBUS_ST_VALID_LOW
139   * @return None
140   * @details This macro is used to set the polarity of external USB VBUS power switch status signal.
141   */
142 #define OTG_SET_VBUS_STS_POL(u32Pol)    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->PHYCTL = (OTG_NS->PHYCTL & (~OTG_PHYCTL_VBSTSPOL_Msk)) | ((u32Pol)<<OTG_PHYCTL_VBSTSPOL_Pos)):(OTG->PHYCTL = (OTG->PHYCTL & (~OTG_PHYCTL_VBSTSPOL_Msk)) | ((u32Pol)<<OTG_PHYCTL_VBSTSPOL_Pos)))
143 
144 /**
145   * @brief This macro is used to enable OTG related interrupts
146   * @param[in] u32Mask The combination of interrupt source. Each bit corresponds to a interrupt source. Valid values are listed below.
147   *                    - \ref OTG_INTEN_ROLECHGIEN_Msk
148   *                    - \ref OTG_INTEN_VBEIEN_Msk
149   *                    - \ref OTG_INTEN_SRPFIEN_Msk
150   *                    - \ref OTG_INTEN_HNPFIEN_Msk
151   *                    - \ref OTG_INTEN_GOIDLEIEN_Msk
152   *                    - \ref OTG_INTEN_IDCHGIEN_Msk
153   *                    - \ref OTG_INTEN_PDEVIEN_Msk
154   *                    - \ref OTG_INTEN_HOSTIEN_Msk
155   *                    - \ref OTG_INTEN_BVLDCHGIEN_Msk
156   *                    - \ref OTG_INTEN_AVLDCHGIEN_Msk
157   *                    - \ref OTG_INTEN_VBCHGIEN_Msk
158   *                    - \ref OTG_INTEN_SECHGIEN_Msk
159   *                    - \ref OTG_INTEN_SRPDETIEN_Msk
160   * @return None
161   * @details This macro will enable OTG related interrupts specified by u32Mask parameter.
162   */
163 #define OTG_ENABLE_INT(u32Mask)    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->INTEN |= (u32Mask)):(OTG->INTEN |= (u32Mask)))
164 
165 /**
166   * @brief This macro is used to disable OTG related interrupts
167   * @param[in] u32Mask The combination of interrupt source. Each bit corresponds to a interrupt source. Valid values are listed below.
168   *                    - \ref OTG_INTEN_ROLECHGIEN_Msk
169   *                    - \ref OTG_INTEN_VBEIEN_Msk
170   *                    - \ref OTG_INTEN_SRPFIEN_Msk
171   *                    - \ref OTG_INTEN_HNPFIEN_Msk
172   *                    - \ref OTG_INTEN_GOIDLEIEN_Msk
173   *                    - \ref OTG_INTEN_IDCHGIEN_Msk
174   *                    - \ref OTG_INTEN_PDEVIEN_Msk
175   *                    - \ref OTG_INTEN_HOSTIEN_Msk
176   *                    - \ref OTG_INTEN_BVLDCHGIEN_Msk
177   *                    - \ref OTG_INTEN_AVLDCHGIEN_Msk
178   *                    - \ref OTG_INTEN_VBCHGIEN_Msk
179   *                    - \ref OTG_INTEN_SECHGIEN_Msk
180   *                    - \ref OTG_INTEN_SRPDETIEN_Msk
181   * @return None
182   * @details This macro will disable OTG related interrupts specified by u32Mask parameter.
183   */
184 #define OTG_DISABLE_INT(u32Mask)    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->INTEN &= ~(u32Mask)):(OTG->INTEN &= ~(u32Mask)))
185 
186 /**
187   * @brief This macro is used to get OTG related interrupt flags
188   * @param[in] u32Mask The combination of interrupt source. Each bit corresponds to a interrupt source. Valid values are listed below.
189   *                    - \ref OTG_INTSTS_ROLECHGIF_Msk
190   *                    - \ref OTG_INTSTS_VBEIF_Msk
191   *                    - \ref OTG_INTSTS_SRPFIF_Msk
192   *                    - \ref OTG_INTSTS_HNPFIF_Msk
193   *                    - \ref OTG_INTSTS_GOIDLEIF_Msk
194   *                    - \ref OTG_INTSTS_IDCHGIF_Msk
195   *                    - \ref OTG_INTSTS_PDEVIF_Msk
196   *                    - \ref OTG_INTSTS_HOSTIF_Msk
197   *                    - \ref OTG_INTSTS_BVLDCHGIF_Msk
198   *                    - \ref OTG_INTSTS_AVLDCHGIF_Msk
199   *                    - \ref OTG_INTSTS_VBCHGIF_Msk
200   *                    - \ref OTG_INTSTS_SECHGIF_Msk
201   *                    - \ref OTG_INTSTS_SRPDETIF_Msk
202   * @return Interrupt flags of selected sources.
203   * @details This macro will return OTG related interrupt flags specified by u32Mask parameter.
204   */
205 #define OTG_GET_INT_FLAG(u32Mask)    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->INTSTS & (u32Mask)):(OTG->INTSTS & (u32Mask)))
206 
207 /**
208   * @brief This macro is used to clear OTG related interrupt flags
209   * @param[in] u32Mask The combination of interrupt source. Each bit corresponds to a interrupt source. Valid values are listed below.
210   *                    - \ref OTG_INTSTS_ROLECHGIF_Msk
211   *                    - \ref OTG_INTSTS_VBEIF_Msk
212   *                    - \ref OTG_INTSTS_SRPFIF_Msk
213   *                    - \ref OTG_INTSTS_HNPFIF_Msk
214   *                    - \ref OTG_INTSTS_GOIDLEIF_Msk
215   *                    - \ref OTG_INTSTS_IDCHGIF_Msk
216   *                    - \ref OTG_INTSTS_PDEVIF_Msk
217   *                    - \ref OTG_INTSTS_HOSTIF_Msk
218   *                    - \ref OTG_INTSTS_BVLDCHGIF_Msk
219   *                    - \ref OTG_INTSTS_AVLDCHGIF_Msk
220   *                    - \ref OTG_INTSTS_VBCHGIF_Msk
221   *                    - \ref OTG_INTSTS_SECHGIF_Msk
222   *                    - \ref OTG_INTSTS_SRPDETIF_Msk
223   * @return None
224   * @details This macro will clear OTG related interrupt flags specified by u32Mask parameter.
225   */
226 #define OTG_CLR_INT_FLAG(u32Mask)    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->INTSTS = (u32Mask)):(OTG->INTSTS = (u32Mask)))
227 
228 /**
229   * @brief This macro is used to get OTG related status
230   * @param[in] u32Mask The combination of user specified source. Valid values are listed below.
231   *                    - \ref OTG_STATUS_OVERCUR_Msk
232   *                    - \ref OTG_STATUS_IDSTS_Msk
233   *                    - \ref OTG_STATUS_SESSEND_Msk
234   *                    - \ref OTG_STATUS_BVLD_Msk
235   *                    - \ref OTG_STATUS_AVLD_Msk
236   *                    - \ref OTG_STATUS_VBUSVLD_Msk
237   * @return The user specified status.
238   * @details This macro will return OTG related status specified by u32Mask parameter.
239   */
240 #define OTG_GET_STATUS(u32Mask)    (((__PC() & NS_OFFSET) == NS_OFFSET)? (OTG_NS->STATUS & (u32Mask)):(OTG->STATUS & (u32Mask)))
241 
242 
243 
244 /*@}*/ /* end of group OTG_EXPORTED_FUNCTIONS */
245 
246 /*@}*/ /* end of group OTG_Driver */
247 
248 /*@}*/ /* end of group Standard_Driver */
249 
250 #ifdef __cplusplus
251 }
252 #endif
253 
254 
255 #endif /* __OTG_H__ */
256 
257