1 /*****************************************************************************
2  * Copyright (c) 2022, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @file usb_regs.h
30  * @author Nations
31  * @version v1.2.0
32  *
33  * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
34  */
35 #ifndef __USB_REGS_H__
36 #define __USB_REGS_H__
37 
38 #include "n32g43x.h"
39 
40 /**
41  * @addtogroup N32G43X_USB_Driver
42  * @{
43  */
44 
45 typedef enum _EP_DBUF_DIR
46 {
47     /* double buffered endpoint direction */
48     EP_DBUF_ERR,
49     EP_DBUF_OUT,
50     EP_DBUF_IN
51 } EP_DBUF_DIR;
52 
53 /* endpoint buffer number */
54 enum EP_BUF_NUM
55 {
56     EP_NOBUF,
57     EP_BUF0,
58     EP_BUF1
59 };
60 
61 #define RegBase (0x40005C00L) /* USB_IP Peripheral Registers base address */
62 #define PMAAddr (0x40006000L) /* USB_IP Packet Memory Area base address   */
63 
64 /******************************************************************************/
65 /*                         Special registers                                  */
66 /******************************************************************************/
67 /* Pull up controller register */
68 #define DP_CTRL ((__IO unsigned*)(0x40001824))
69 
70 #define _ClrDPCtrl() (*DP_CTRL = (*DP_CTRL) & (~0x8000000));
71 #define _EnPortPullup() (*DP_CTRL = (*DP_CTRL) | 0x02000000);
72 #define _DisPortPullup() (*DP_CTRL = (*DP_CTRL) & 0xFDFFFFFF);
73 
74 /******************************************************************************/
75 /*                         General registers                                  */
76 /******************************************************************************/
77 
78 /* Control register */
79 #define USB_CTRL ((__IO unsigned*)(RegBase + 0x40))
80 /* Interrupt status register */
81 #define USB_STS ((__IO unsigned*)(RegBase + 0x44))
82 /* Frame number register */
83 #define USB_FN ((__IO unsigned*)(RegBase + 0x48))
84 /* Device address register */
85 #define USB_ADDR ((__IO unsigned*)(RegBase + 0x4C))
86 /* Buffer Table address register */
87 #define USB_BUFTAB ((__IO unsigned*)(RegBase + 0x50))
88 /******************************************************************************/
89 /*                         Endpoint registers                                 */
90 /******************************************************************************/
91 #define EP0REG ((__IO unsigned*)(RegBase)) /* endpoint 0 register address */
92 
93 /* Endpoint Addresses (w/direction) */
94 #define EP0_OUT ((uint8_t)0x00)
95 #define EP0_IN  ((uint8_t)0x80)
96 #define EP1_OUT ((uint8_t)0x01)
97 #define EP1_IN  ((uint8_t)0x81)
98 #define EP2_OUT ((uint8_t)0x02)
99 #define EP2_IN  ((uint8_t)0x82)
100 #define EP3_OUT ((uint8_t)0x03)
101 #define EP3_IN  ((uint8_t)0x83)
102 #define EP4_OUT ((uint8_t)0x04)
103 #define EP4_IN  ((uint8_t)0x84)
104 #define EP5_OUT ((uint8_t)0x05)
105 #define EP5_IN  ((uint8_t)0x85)
106 #define EP6_OUT ((uint8_t)0x06)
107 #define EP6_IN  ((uint8_t)0x86)
108 #define EP7_OUT ((uint8_t)0x07)
109 #define EP7_IN  ((uint8_t)0x87)
110 
111 /* endpoints enumeration */
112 #define ENDP0 ((uint8_t)0)
113 #define ENDP1 ((uint8_t)1)
114 #define ENDP2 ((uint8_t)2)
115 #define ENDP3 ((uint8_t)3)
116 #define ENDP4 ((uint8_t)4)
117 #define ENDP5 ((uint8_t)5)
118 #define ENDP6 ((uint8_t)6)
119 #define ENDP7 ((uint8_t)7)
120 
121 /******************************************************************************/
122 /*                       USB_STS interrupt events                                */
123 /******************************************************************************/
124 #define STS_CTRS  (0x8000) /* Correct TRansfer (clear-only bit) */
125 #define STS_DOVR  (0x4000) /* DMA OVeR/underrun (clear-only bit) */
126 #define STS_ERROR (0x2000) /* ERRor (clear-only bit) */
127 #define STS_WKUP  (0x1000) /* WaKe UP (clear-only bit) */
128 #define STS_SUSPD (0x0800) /* SUSPend (clear-only bit) */
129 #define STS_RST   (0x0400) /* RESET (clear-only bit) */
130 #define STS_SOF   (0x0200) /* Start Of Frame (clear-only bit) */
131 #define STS_ESOF  (0x0100) /* Expected Start Of Frame (clear-only bit) */
132 
133 #define STS_DIR   (0x0010) /* DIRection of transaction (read-only bit)  */
134 #define STS_EP_ID (0x000F) /* EndPoint IDentifier (read-only bit)  */
135 
136 #define CLR_CTRS  (~STS_CTRS)  /* clear Correct TRansfer bit */
137 #define CLR_DOVR  (~STS_DOVR)  /* clear DMA OVeR/underrun bit*/
138 #define CLR_ERROR (~STS_ERROR) /* clear ERRor bit */
139 #define CLR_WKUP  (~STS_WKUP)  /* clear WaKe UP bit     */
140 #define CLR_SUSPD (~STS_SUSPD) /* clear SUSPend bit     */
141 #define CLR_RST   (~STS_RST)   /* clear RESET bit      */
142 #define CLR_SOF   (~STS_SOF)   /* clear Start Of Frame bit   */
143 #define CLR_ESOF  (~STS_ESOF)  /* clear Expected Start Of Frame bit */
144 
145 /******************************************************************************/
146 /*             USB_CTRL control register bits definitions                         */
147 /******************************************************************************/
148 #define CTRL_CTRSM  (0x8000) /* Correct TRansfer Mask */
149 #define CTRL_DOVRM  (0x4000) /* DMA OVeR/underrun Mask */
150 #define CTRL_ERRORM (0x2000) /* ERRor Mask */
151 #define CTRL_WKUPM  (0x1000) /* WaKe UP Mask */
152 #define CTRL_SUSPDM (0x0800) /* SUSPend Mask */
153 #define CTRL_RSTM   (0x0400) /* RESET Mask   */
154 #define CTRL_SOFM   (0x0200) /* Start Of Frame Mask */
155 #define CTRL_ESOFM  (0x0100) /* Expected Start Of Frame Mask */
156 
157 #define CTRL_RESUM   (0x0010) /* RESUME request */
158 #define CTRL_FSUSPD  (0x0008) /* Force SUSPend */
159 #define CTRL_LP_MODE (0x0004) /* Low-power MODE */
160 #define CTRL_PD      (0x0002) /* Power DoWN */
161 #define CTRL_FRST    (0x0001) /* Force USB RESet */
162 
163 /******************************************************************************/
164 /*                USB_FN Frame Number Register bit definitions                   */
165 /******************************************************************************/
166 #define FN_RXDP (0x8000) /* status of D+ data line */
167 #define FN_RXDM (0x4000) /* status of D- data line */
168 #define FN_LCK  (0x2000) /* LoCKed */
169 #define FN_LSOF (0x1800) /* Lost SOF */
170 #define FN_FNUM (0x07FF) /* Frame Number */
171 /******************************************************************************/
172 /*               USB_ADDR Device ADDRess bit definitions                         */
173 /******************************************************************************/
174 #define ADDR_EFUC (0x80)
175 #define ADDR_ADDR (0x7F)
176 /******************************************************************************/
177 /*                            Endpoint register                               */
178 /******************************************************************************/
179 /* bit positions */
180 #define EP_CTRS_RX   (0x8000) /* EndPoint Correct TRansfer RX */
181 #define EP_DATTOG_RX (0x4000) /* EndPoint Data TOGGLE RX */
182 #define EPRX_STS     (0x3000) /* EndPoint RX STATus bit field */
183 #define EP_SETUP     (0x0800) /* EndPoint SETUP */
184 #define EP_T_FIELD   (0x0600) /* EndPoint TYPE */
185 #define EP_KIND      (0x0100) /* EndPoint KIND */
186 #define EP_CTRS_TX   (0x0080) /* EndPoint Correct TRansfer TX */
187 #define EP_DATTOG_TX (0x0040) /* EndPoint Data TOGGLE TX */
188 #define EPTX_STS     (0x0030) /* EndPoint TX STATus bit field */
189 #define EPADDR_FIELD (0x000F) /* EndPoint ADDRess FIELD */
190 
191 /* EndPoint REGister INTEN (no toggle fields) */
192 #define EPREG_MASK (EP_CTRS_RX | EP_SETUP | EP_T_FIELD | EP_KIND | EP_CTRS_TX | EPADDR_FIELD)
193 
194 /* EP_TYPE[1:0] EndPoint TYPE */
195 #define EP_TYPE_MASK   (0x0600) /* EndPoint TYPE Mask */
196 #define EP_BULK        (0x0000) /* EndPoint BULK */
197 #define EP_CONTROL     (0x0200) /* EndPoint CONTROL */
198 #define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */
199 #define EP_INTERRUPT   (0x0600) /* EndPoint INTERRUPT */
200 #define EP_T_MASK      (~EP_T_FIELD & EPREG_MASK)
201 
202 /* EP_KIND EndPoint KIND */
203 #define EPKIND_MASK (~EP_KIND & EPREG_MASK)
204 
205 /* STAT_TX[1:0] STATus for TX transfer */
206 #define EP_TX_DIS       (0x0000) /* EndPoint TX DISabled */
207 #define EP_TX_STALL     (0x0010) /* EndPoint TX STALLed */
208 #define EP_TX_NAK       (0x0020) /* EndPoint TX NAKed */
209 #define EP_TX_VALID     (0x0030) /* EndPoint TX VALID */
210 #define EPTX_DATTOG1    (0x0010) /* EndPoint TX Data TOGgle bit1 */
211 #define EPTX_DATTOG2    (0x0020) /* EndPoint TX Data TOGgle bit2 */
212 #define EPTX_DATTOGMASK (EPTX_STS | EPREG_MASK)
213 
214 /* STAT_RX[1:0] STATus for RX transfer */
215 #define EP_RX_DIS       (0x0000) /* EndPoint RX DISabled */
216 #define EP_RX_STALL     (0x1000) /* EndPoint RX STALLed */
217 #define EP_RX_NAK       (0x2000) /* EndPoint RX NAKed */
218 #define EP_RX_VALID     (0x3000) /* EndPoint RX VALID */
219 #define EPRX_DATTOG1    (0x1000) /* EndPoint RX Data TOGgle bit1 */
220 #define EPRX_DATTOG2    (0x2000) /* EndPoint RX Data TOGgle bit1 */
221 #define EPRX_DATTOGMASK (EPRX_STS | EPREG_MASK)
222 
223 /* USB_SetCtrl */
224 #define _SetCNTR(wRegValue) (*USB_CTRL = (uint16_t)wRegValue)
225 
226 /* USB_SetSts */
227 #define _SetISTR(wRegValue) (*USB_STS = (uint16_t)wRegValue)
228 
229 /* USB_SetAddr */
230 #define _SetDADDR(wRegValue) (*USB_ADDR = (uint16_t)wRegValue)
231 
232 /* USB_SetBuftab */
233 #define _SetBTABLE(wRegValue) (*USB_BUFTAB = (uint16_t)(wRegValue & 0xFFF8))
234 
235 /* USB_GetCtrl */
236 #define _GetCNTR() ((uint16_t)*USB_CTRL)
237 
238 /* USB_GetSts */
239 #define _GetISTR() ((uint16_t)*USB_STS)
240 
241 /* USB_GetFn */
242 #define _GetFNR() ((uint16_t)*USB_FN)
243 
244 /* USB_GetAddr */
245 #define _GetDADDR() ((uint16_t)*USB_ADDR)
246 
247 /* USB_GetBTABLE */
248 #define _GetBTABLE() ((uint16_t)*USB_BUFTAB)
249 
250 /* USB_SetEndPoint */
251 #define _SetENDPOINT(bEpNum, wRegValue) (*(EP0REG + bEpNum) = (uint16_t)wRegValue)
252 
253 /* USB_GetEndPoint */
254 #define _GetENDPOINT(bEpNum) ((uint16_t)(*(EP0REG + bEpNum)))
255 
256 /*******************************************************************************
257  * Macro Name     : USB_SetEpType
258  * Description    : sets the type in the endpoint register(bits EP_TYPE[1:0])
259  * Input          : bEpNum: Endpoint Number.
260  *                  wType
261  * Output         : None.
262  * Return         : None.
263  *******************************************************************************/
264 #define _SetEPType(bEpNum, wType) (_SetENDPOINT(bEpNum, ((_GetENDPOINT(bEpNum) & EP_T_MASK) | wType)))
265 
266 /*******************************************************************************
267  * Macro Name     : USB_GetEpType
268  * Description    : gets the type in the endpoint register(bits EP_TYPE[1:0])
269  * Input          : bEpNum: Endpoint Number.
270  * Output         : None.
271  * Return         : Endpoint Type
272  *******************************************************************************/
273 #define _GetEPType(bEpNum) (_GetENDPOINT(bEpNum) & EP_T_FIELD)
274 
275 /*******************************************************************************
276  * Macro Name     : SetEPTxStatus
277  * Description    : sets the status for tx transfer (bits STAT_TX[1:0]).
278  * Input          : bEpNum: Endpoint Number.
279  *                  wState: new state
280  * Output         : None.
281  * Return         : None.
282  *******************************************************************************/
283 #define _SetEPTxStatus(bEpNum, wState)                                                                                 \
284     {                                                                                                                  \
285         register uint16_t _wRegVal;                                                                                    \
286         _wRegVal = _GetENDPOINT(bEpNum) & EPTX_DATTOGMASK;                                                             \
287         /* toggle first bit ? */                                                                                       \
288         if ((EPTX_DATTOG1 & wState) != 0)                                                                              \
289             _wRegVal ^= EPTX_DATTOG1;                                                                                  \
290         /* toggle second bit ?  */                                                                                     \
291         if ((EPTX_DATTOG2 & wState) != 0)                                                                              \
292             _wRegVal ^= EPTX_DATTOG2;                                                                                  \
293         _SetENDPOINT(bEpNum, (_wRegVal | EP_CTRS_RX | EP_CTRS_TX));                                                    \
294     } /* _SetEPTxStatus */
295 
296 /*******************************************************************************
297  * Macro Name     : SetEPRxStatus
298  * Description    : sets the status for rx transfer (bits STAT_TX[1:0])
299  * Input          : bEpNum: Endpoint Number.
300  *                  wState: new state.
301  * Output         : None.
302  * Return         : None.
303  *******************************************************************************/
304 #define _SetEPRxStatus(bEpNum, wState)                                                                                 \
305     {                                                                                                                  \
306         register uint16_t _wRegVal;                                                                                    \
307                                                                                                                        \
308         _wRegVal = _GetENDPOINT(bEpNum) & EPRX_DATTOGMASK;                                                             \
309         /* toggle first bit ? */                                                                                       \
310         if ((EPRX_DATTOG1 & wState) != 0)                                                                              \
311             _wRegVal ^= EPRX_DATTOG1;                                                                                  \
312         /* toggle second bit ? */                                                                                      \
313         if ((EPRX_DATTOG2 & wState) != 0)                                                                              \
314             _wRegVal ^= EPRX_DATTOG2;                                                                                  \
315         _SetENDPOINT(bEpNum, (_wRegVal | EP_CTRS_RX | EP_CTRS_TX));                                                    \
316     } /* _SetEPRxStatus */
317 
318 /*******************************************************************************
319  * Macro Name     : SetEPRxTxStatus
320  * Description    : sets the status for rx & tx (bits STAT_TX[1:0] & STAT_RX[1:0])
321  * Input          : bEpNum: Endpoint Number.
322  *                  wStaterx: new state.
323  *                  wStatetx: new state.
324  * Output         : None.
325  * Return         : None.
326  *******************************************************************************/
327 #define _SetEPRxTxStatus(bEpNum, wStaterx, wStatetx)                                                                   \
328     {                                                                                                                  \
329         register uint32_t _wRegVal;                                                                                    \
330                                                                                                                        \
331         _wRegVal = _GetENDPOINT(bEpNum) & (EPRX_DATTOGMASK | EPTX_STS);                                                \
332         /* toggle first bit ? */                                                                                       \
333         if ((EPRX_DATTOG1 & wStaterx) != 0)                                                                            \
334             _wRegVal ^= EPRX_DATTOG1;                                                                                  \
335         /* toggle second bit ? */                                                                                      \
336         if ((EPRX_DATTOG2 & wStaterx) != 0)                                                                            \
337             _wRegVal ^= EPRX_DATTOG2;                                                                                  \
338         /* toggle first bit ? */                                                                                       \
339         if ((EPTX_DATTOG1 & wStatetx) != 0)                                                                            \
340             _wRegVal ^= EPTX_DATTOG1;                                                                                  \
341         /* toggle second bit ?  */                                                                                     \
342         if ((EPTX_DATTOG2 & wStatetx) != 0)                                                                            \
343             _wRegVal ^= EPTX_DATTOG2;                                                                                  \
344         _SetENDPOINT(bEpNum, _wRegVal | EP_CTRS_RX | EP_CTRS_TX);                                                      \
345     } /* _SetEPRxTxStatus */
346 /*******************************************************************************
347  * Macro Name     : USB_GetEpTxSts / USB_GetEpRxSts
348  * Description    : gets the status for tx/rx transfer (bits STAT_TX[1:0]
349  *                  /STAT_RX[1:0])
350  * Input          : bEpNum: Endpoint Number.
351  * Output         : None.
352  * Return         : status .
353  *******************************************************************************/
354 #define _GetEPTxStatus(bEpNum) ((uint16_t)_GetENDPOINT(bEpNum) & EPTX_STS)
355 
356 #define _GetEPRxStatus(bEpNum) ((uint16_t)_GetENDPOINT(bEpNum) & EPRX_STS)
357 
358 /*******************************************************************************
359  * Macro Name     : USB_SetEpTxValid / USB_SetEpRxValid
360  * Description    : sets directly the VALID tx/rx-status into the enpoint register
361  * Input          : bEpNum: Endpoint Number.
362  * Output         : None.
363  * Return         : None.
364  *******************************************************************************/
365 #define _SetEPTxValid(bEpNum) (_SetEPTxStatus(bEpNum, EP_TX_VALID))
366 
367 #define _SetEPRxValid(bEpNum) (_SetEPRxStatus(bEpNum, EP_RX_VALID))
368 
369 /*******************************************************************************
370  * Macro Name     : USB_GetTxStallSts / USB_GetRxStallSts.
371  * Description    : checks stall condition in an endpoint.
372  * Input          : bEpNum: Endpoint Number.
373  * Output         : None.
374  * Return         : TRUE = endpoint in stall condition.
375  *******************************************************************************/
376 #define _GetTxStallStatus(bEpNum) (_GetEPTxStatus(bEpNum) == EP_TX_STALL)
377 #define _GetRxStallStatus(bEpNum) (_GetEPRxStatus(bEpNum) == EP_RX_STALL)
378 
379 /*******************************************************************************
380  * Macro Name     : USB_SetEpKind / USB_ClrEpKind.
381  * Description    : set & clear EP_KIND bit.
382  * Input          : bEpNum: Endpoint Number.
383  * Output         : None.
384  * Return         : None.
385  *******************************************************************************/
386 #define _SetEP_KIND(bEpNum)                                                                                            \
387     (_SetENDPOINT(bEpNum, (EP_CTRS_RX | EP_CTRS_TX | ((_GetENDPOINT(bEpNum) | EP_KIND) & EPREG_MASK))))
388 #define _ClearEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, (EP_CTRS_RX | EP_CTRS_TX | (_GetENDPOINT(bEpNum) & EPKIND_MASK))))
389 
390 /*******************************************************************************
391  * Macro Name     : USB_SetStsOut / USB_ClrStsOut.
392  * Description    : Sets/clears directly STATUS_OUT bit in the endpoint register.
393  * Input          : bEpNum: Endpoint Number.
394  * Output         : None.
395  * Return         : None.
396  *******************************************************************************/
397 #define _Set_Status_Out(bEpNum)   _SetEP_KIND(bEpNum)
398 #define _Clear_Status_Out(bEpNum) _ClearEP_KIND(bEpNum)
399 
400 /*******************************************************************************
401  * Macro Name     : USB_SetEpDoubleBufer / USB_ClrEpDoubleBufer.
402  * Description    : Sets/clears directly EP_KIND bit in the endpoint register.
403  * Input          : bEpNum: Endpoint Number.
404  * Output         : None.
405  * Return         : None.
406  *******************************************************************************/
407 #define _SetEPDoubleBuff(bEpNum)   _SetEP_KIND(bEpNum)
408 #define _ClearEPDoubleBuff(bEpNum) _ClearEP_KIND(bEpNum)
409 
410 /*******************************************************************************
411  * Macro Name     : USB_ClrEpCtrsRx / USB_ClrEpCtrsTx.
412  * Description    : Clears bit CTR_RX / CTR_TX in the endpoint register.
413  * Input          : bEpNum: Endpoint Number.
414  * Output         : None.
415  * Return         : None.
416  *******************************************************************************/
417 #define _ClearEP_CTR_RX(bEpNum) (_SetENDPOINT(bEpNum, _GetENDPOINT(bEpNum) & 0x7FFF & EPREG_MASK))
418 #define _ClearEP_CTR_TX(bEpNum) (_SetENDPOINT(bEpNum, _GetENDPOINT(bEpNum) & 0xFF7F & EPREG_MASK))
419 
420 /*******************************************************************************
421  * Macro Name     : USB_DattogRx / USB_DattogTx .
422  * Description    : Toggles DTOG_RX / DTOG_TX bit in the endpoint register.
423  * Input          : bEpNum: Endpoint Number.
424  * Output         : None.
425  * Return         : None.
426  *******************************************************************************/
427 #define _ToggleDTOG_RX(bEpNum)                                                                                         \
428     (_SetENDPOINT(bEpNum, EP_CTRS_RX | EP_CTRS_TX | EP_DATTOG_RX | (_GetENDPOINT(bEpNum) & EPREG_MASK)))
429 #define _ToggleDTOG_TX(bEpNum)                                                                                         \
430     (_SetENDPOINT(bEpNum, EP_CTRS_RX | EP_CTRS_TX | EP_DATTOG_TX | (_GetENDPOINT(bEpNum) & EPREG_MASK)))
431 
432 /*******************************************************************************
433  * Macro Name     : USB_ClrDattogRx / USB_ClrDattogTx.
434  * Description    : Clears DTOG_RX / DTOG_TX bit in the endpoint register.
435  * Input          : bEpNum: Endpoint Number.
436  * Output         : None.
437  * Return         : None.
438  *******************************************************************************/
439 #define _ClearDTOG_RX(bEpNum)                                                                                          \
440     if ((_GetENDPOINT(bEpNum) & EP_DATTOG_RX) != 0)                                                                    \
441     _ToggleDTOG_RX(bEpNum)
442 #define _ClearDTOG_TX(bEpNum)                                                                                          \
443     if ((_GetENDPOINT(bEpNum) & EP_DATTOG_TX) != 0)                                                                    \
444     _ToggleDTOG_TX(bEpNum)
445 /*******************************************************************************
446  * Macro Name     : USB_SetEpAddress.
447  * Description    : Sets address in an endpoint register.
448  * Input          : bEpNum: Endpoint Number.
449  *                  bAddr: Address.
450  * Output         : None.
451  * Return         : None.
452  *******************************************************************************/
453 #define _SetEPAddress(bEpNum, bAddr)                                                                                   \
454     _SetENDPOINT(bEpNum, EP_CTRS_RX | EP_CTRS_TX | (_GetENDPOINT(bEpNum) & EPREG_MASK) | bAddr)
455 
456 /*******************************************************************************
457  * Macro Name     : USB_GetEpAddress.
458  * Description    : Gets address in an endpoint register.
459  * Input          : bEpNum: Endpoint Number.
460  * Output         : None.
461  * Return         : None.
462  *******************************************************************************/
463 #define _GetEPAddress(bEpNum) ((uint8_t)(_GetENDPOINT(bEpNum) & EPADDR_FIELD))
464 
465 #define _pEPTxAddr(bEpNum)  ((uint32_t*)((_GetBTABLE() + bEpNum * 8) * 2 + PMAAddr))
466 #define _pEPTxCount(bEpNum) ((uint32_t*)((_GetBTABLE() + bEpNum * 8 + 2) * 2 + PMAAddr))
467 #define _pEPRxAddr(bEpNum)  ((uint32_t*)((_GetBTABLE() + bEpNum * 8 + 4) * 2 + PMAAddr))
468 #define _pEPRxCount(bEpNum) ((uint32_t*)((_GetBTABLE() + bEpNum * 8 + 6) * 2 + PMAAddr))
469 
470 /*******************************************************************************
471  * Macro Name     : USB_SetEpTxAddr / USB_SetEpRxAddr.
472  * Description    : sets address of the tx/rx buffer.
473  * Input          : bEpNum: Endpoint Number.
474  *                  wAddr: address to be set (must be word aligned).
475  * Output         : None.
476  * Return         : None.
477  *******************************************************************************/
478 #define _SetEPTxAddr(bEpNum, wAddr) (*_pEPTxAddr(bEpNum) = ((wAddr >> 1) << 1))
479 #define _SetEPRxAddr(bEpNum, wAddr) (*_pEPRxAddr(bEpNum) = ((wAddr >> 1) << 1))
480 
481 /*******************************************************************************
482  * Macro Name     : USB_GetEpTxAddr / USB_GetEpRxAddr.
483  * Description    : Gets address of the tx/rx buffer.
484  * Input          : bEpNum: Endpoint Number.
485  * Output         : None.
486  * Return         : address of the buffer.
487  *******************************************************************************/
488 #define _GetEPTxAddr(bEpNum) ((uint16_t)*_pEPTxAddr(bEpNum))
489 #define _GetEPRxAddr(bEpNum) ((uint16_t)*_pEPRxAddr(bEpNum))
490 
491 /*******************************************************************************
492  * Macro Name     : USB_SetEpCntRxReg.
493  * Description    : Sets counter of rx buffer with no. of blocks.
494  * Input          : pdwReg: pointer to counter.
495  *                  wCount: Counter.
496  * Output         : None.
497  * Return         : None.
498  *******************************************************************************/
499 #define _BlocksOf32(dwReg, wCount, wNBlocks)                                                                           \
500     {                                                                                                                  \
501         wNBlocks = wCount >> 5;                                                                                        \
502         if ((wCount & 0x1f) == 0)                                                                                      \
503             wNBlocks--;                                                                                                \
504         *pdwReg = (uint32_t)((wNBlocks << 10) | 0x8000);                                                               \
505     } /* _BlocksOf32 */
506 
507 #define _BlocksOf2(dwReg, wCount, wNBlocks)                                                                            \
508     {                                                                                                                  \
509         wNBlocks = wCount >> 1;                                                                                        \
510         if ((wCount & 0x1) != 0)                                                                                       \
511             wNBlocks++;                                                                                                \
512         *pdwReg = (uint32_t)(wNBlocks << 10);                                                                          \
513     } /* _BlocksOf2 */
514 
515 #define _SetEPCountRxReg(dwReg, wCount)                                                                                \
516     {                                                                                                                  \
517         uint16_t wNBlocks;                                                                                             \
518         if (wCount > 62)                                                                                               \
519         {                                                                                                              \
520             _BlocksOf32(dwReg, wCount, wNBlocks);                                                                      \
521         }                                                                                                              \
522         else                                                                                                           \
523         {                                                                                                              \
524             _BlocksOf2(dwReg, wCount, wNBlocks);                                                                       \
525         }                                                                                                              \
526     } /* _SetEPCountRxReg */
527 
528 #define _SetEPRxDblBuf0Count(bEpNum, wCount)                                                                           \
529     {                                                                                                                  \
530         uint32_t* pdwReg = _pEPTxCount(bEpNum);                                                                        \
531         _SetEPCountRxReg(pdwReg, wCount);                                                                              \
532     }
533 /*******************************************************************************
534  * Macro Name     : USB_SetEpTxCnt / USB_SetEpRxCnt.
535  * Description    : sets counter for the tx/rx buffer.
536  * Input          : bEpNum: endpoint number.
537  *                  wCount: Counter value.
538  * Output         : None.
539  * Return         : None.
540  *******************************************************************************/
541 #define _SetEPTxCount(bEpNum, wCount) (*_pEPTxCount(bEpNum) = wCount)
542 #define _SetEPRxCount(bEpNum, wCount)                                                                                  \
543     {                                                                                                                  \
544         uint32_t* pdwReg = _pEPRxCount(bEpNum);                                                                        \
545         _SetEPCountRxReg(pdwReg, wCount);                                                                              \
546     }
547 /*******************************************************************************
548  * Macro Name     : USB_GetEpTxCnt / USB_GetEpRxCnt.
549  * Description    : gets counter of the tx buffer.
550  * Input          : bEpNum: endpoint number.
551  * Output         : None.
552  * Return         : Counter value.
553  *******************************************************************************/
554 #define _GetEPTxCount(bEpNum) ((uint16_t)(*_pEPTxCount(bEpNum)) & 0x3ff)
555 #define _GetEPRxCount(bEpNum) ((uint16_t)(*_pEPRxCount(bEpNum)) & 0x3ff)
556 
557 /*******************************************************************************
558  * Macro Name     : USB_SetEpDblBuf0Addr / USB_SetEpDblBuf1Addr.
559  * Description    : Sets buffer 0/1 address in a double buffer endpoint.
560  * Input          : bEpNum: endpoint number.
561  *                : wBuf0Addr: buffer 0 address.
562  * Output         : None.
563  * Return         : None.
564  *******************************************************************************/
565 #define _SetEPDblBuf0Addr(bEpNum, wBuf0Addr)                                                                           \
566     {                                                                                                                  \
567         _SetEPTxAddr(bEpNum, wBuf0Addr);                                                                               \
568     }
569 #define _SetEPDblBuf1Addr(bEpNum, wBuf1Addr)                                                                           \
570     {                                                                                                                  \
571         _SetEPRxAddr(bEpNum, wBuf1Addr);                                                                               \
572     }
573 
574 /*******************************************************************************
575  * Macro Name     : USB_SetEpDblBuferAddr.
576  * Description    : Sets addresses in a double buffer endpoint.
577  * Input          : bEpNum: endpoint number.
578  *                : wBuf0Addr: buffer 0 address.
579  *                : wBuf1Addr = buffer 1 address.
580  * Output         : None.
581  * Return         : None.
582  *******************************************************************************/
583 #define _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr)                                                                \
584     {                                                                                                                  \
585         _SetEPDblBuf0Addr(bEpNum, wBuf0Addr);                                                                          \
586         _SetEPDblBuf1Addr(bEpNum, wBuf1Addr);                                                                          \
587     } /* _SetEPDblBuffAddr */
588 
589 /*******************************************************************************
590  * Macro Name     : USB_GetEpDblBuf0Addr / USB_GetEpDblBuf1Addr.
591  * Description    : Gets buffer 0/1 address of a double buffer endpoint.
592  * Input          : bEpNum: endpoint number.
593  * Output         : None.
594  * Return         : None.
595  *******************************************************************************/
596 #define _GetEPDblBuf0Addr(bEpNum) (_GetEPTxAddr(bEpNum))
597 #define _GetEPDblBuf1Addr(bEpNum) (_GetEPRxAddr(bEpNum))
598 
599 /*******************************************************************************
600  * Macro Name     : USB_SetEpDblBuferCnt / USB_SetEpDblBuf0Cnt / USB_SetEpDblBuf1Cnt.
601  * Description    : Gets buffer 0/1 address of a double buffer endpoint.
602  * Input          : bEpNum: endpoint number.
603  *                : bDir: endpoint dir  EP_DBUF_OUT = OUT
604  *                                      EP_DBUF_IN  = IN
605  *                : wCount: Counter value
606  * Output         : None.
607  * Return         : None.
608  *******************************************************************************/
609 #define _SetEPDblBuf0Count(bEpNum, bDir, wCount)                                                                       \
610     {                                                                                                                  \
611         if (bDir == EP_DBUF_OUT)                                                                                       \
612         /* OUT endpoint */                                                                                             \
613         {                                                                                                              \
614             _SetEPRxDblBuf0Count(bEpNum, wCount);                                                                      \
615         }                                                                                                              \
616         else if (bDir == EP_DBUF_IN)                                                                                   \
617             /* IN endpoint */                                                                                          \
618             *_pEPTxCount(bEpNum) = (uint32_t)wCount;                                                                   \
619     } /* USB_SetEpDblBuf0Cnt*/
620 
621 #define _SetEPDblBuf1Count(bEpNum, bDir, wCount)                                                                       \
622     {                                                                                                                  \
623         if (bDir == EP_DBUF_OUT)                                                                                       \
624         /* OUT endpoint */                                                                                             \
625         {                                                                                                              \
626             _SetEPRxCount(bEpNum, wCount);                                                                             \
627         }                                                                                                              \
628         else if (bDir == EP_DBUF_IN)                                                                                   \
629             /* IN endpoint */                                                                                          \
630             *_pEPRxCount(bEpNum) = (uint32_t)wCount;                                                                   \
631     } /* USB_SetEpDblBuf1Cnt */
632 
633 #define _SetEPDblBuffCount(bEpNum, bDir, wCount)                                                                       \
634     {                                                                                                                  \
635         _SetEPDblBuf0Count(bEpNum, bDir, wCount);                                                                      \
636         _SetEPDblBuf1Count(bEpNum, bDir, wCount);                                                                      \
637     } /* _SetEPDblBuffCount  */
638 
639 /*******************************************************************************
640  * Macro Name     : USB_GetEpDblBuf0Cnt / USB_GetEpDblBuf1Cnt.
641  * Description    : Gets buffer 0/1 rx/tx counter for double buffering.
642  * Input          : bEpNum: endpoint number.
643  * Output         : None.
644  * Return         : None.
645  *******************************************************************************/
646 #define _GetEPDblBuf0Count(bEpNum) (_GetEPTxCount(bEpNum))
647 #define _GetEPDblBuf1Count(bEpNum) (_GetEPRxCount(bEpNum))
648 
649 extern __IO uint16_t wIstr; /* USB_STS register last read value */
650 
651 void USB_SetCtrl(uint16_t /*wRegValue*/);
652 void USB_SetSts(uint16_t /*wRegValue*/);
653 void USB_SetAddr(uint16_t /*wRegValue*/);
654 void USB_SetBuftab(uint16_t /*wRegValue*/);
655 void USB_SetBuftab(uint16_t /*wRegValue*/);
656 uint16_t USB_GetCtrl(void);
657 uint16_t USB_GetSts(void);
658 uint16_t USB_GetFn(void);
659 uint16_t USB_GetAddr(void);
660 uint16_t USB_GetBTABLE(void);
661 void USB_SetEndPoint(uint8_t /*bEpNum*/, uint16_t /*wRegValue*/);
662 uint16_t USB_GetEndPoint(uint8_t /*bEpNum*/);
663 void USB_SetEpType(uint8_t /*bEpNum*/, uint16_t /*wType*/);
664 uint16_t USB_GetEpType(uint8_t /*bEpNum*/);
665 void SetEPTxStatus(uint8_t /*bEpNum*/, uint16_t /*wState*/);
666 void SetEPRxStatus(uint8_t /*bEpNum*/, uint16_t /*wState*/);
667 void USB_SetDouBleBuferEpStall(uint8_t /*bEpNum*/, uint8_t bDir);
668 uint16_t USB_GetEpTxSts(uint8_t /*bEpNum*/);
669 uint16_t USB_GetEpRxSts(uint8_t /*bEpNum*/);
670 void USB_SetEpTxValid(uint8_t /*bEpNum*/);
671 void USB_SetEpRxValid(uint8_t /*bEpNum*/);
672 uint16_t USB_GetTxStallSts(uint8_t /*bEpNum*/);
673 uint16_t USB_GetRxStallSts(uint8_t /*bEpNum*/);
674 void USB_SetEpKind(uint8_t /*bEpNum*/);
675 void USB_ClrEpKind(uint8_t /*bEpNum*/);
676 void USB_SetStsOut(uint8_t /*bEpNum*/);
677 void USB_ClrStsOut(uint8_t /*bEpNum*/);
678 void USB_SetEpDoubleBufer(uint8_t /*bEpNum*/);
679 void USB_ClrEpDoubleBufer(uint8_t /*bEpNum*/);
680 void USB_ClrEpCtrsRx(uint8_t /*bEpNum*/);
681 void USB_ClrEpCtrsTx(uint8_t /*bEpNum*/);
682 void USB_DattogRx(uint8_t /*bEpNum*/);
683 void USB_DattogTx(uint8_t /*bEpNum*/);
684 void USB_ClrDattogRx(uint8_t /*bEpNum*/);
685 void USB_ClrDattogTx(uint8_t /*bEpNum*/);
686 void USB_SetEpAddress(uint8_t /*bEpNum*/, uint8_t /*bAddr*/);
687 uint8_t USB_GetEpAddress(uint8_t /*bEpNum*/);
688 void USB_SetEpTxAddr(uint8_t /*bEpNum*/, uint16_t /*wAddr*/);
689 void USB_SetEpRxAddr(uint8_t /*bEpNum*/, uint16_t /*wAddr*/);
690 uint16_t USB_GetEpTxAddr(uint8_t /*bEpNum*/);
691 uint16_t USB_GetEpRxAddr(uint8_t /*bEpNum*/);
692 void USB_SetEpCntRxReg(uint32_t* /*pdwReg*/, uint16_t /*wCount*/);
693 void USB_SetEpTxCnt(uint8_t /*bEpNum*/, uint16_t /*wCount*/);
694 void USB_SetEpRxCnt(uint8_t /*bEpNum*/, uint16_t /*wCount*/);
695 uint16_t USB_GetEpTxCnt(uint8_t /*bEpNum*/);
696 uint16_t USB_GetEpRxCnt(uint8_t /*bEpNum*/);
697 void USB_SetEpDblBuf0Addr(uint8_t /*bEpNum*/, uint16_t /*wBuf0Addr*/);
698 void USB_SetEpDblBuf1Addr(uint8_t /*bEpNum*/, uint16_t /*wBuf1Addr*/);
699 void USB_SetEpDblBuferAddr(uint8_t /*bEpNum*/, uint16_t /*wBuf0Addr*/, uint16_t /*wBuf1Addr*/);
700 uint16_t USB_GetEpDblBuf0Addr(uint8_t /*bEpNum*/);
701 uint16_t USB_GetEpDblBuf1Addr(uint8_t /*bEpNum*/);
702 void USB_SetEpDblBuferCnt(uint8_t /*bEpNum*/, uint8_t /*bDir*/, uint16_t /*wCount*/);
703 void USB_SetEpDblBuf0Cnt(uint8_t /*bEpNum*/, uint8_t /*bDir*/, uint16_t /*wCount*/);
704 void USB_SetEpDblBuf1Cnt(uint8_t /*bEpNum*/, uint8_t /*bDir*/, uint16_t /*wCount*/);
705 uint16_t USB_GetEpDblBuf0Cnt(uint8_t /*bEpNum*/);
706 uint16_t USB_GetEpDblBuf1Cnt(uint8_t /*bEpNum*/);
707 EP_DBUF_DIR GetEPDblBufDir(uint8_t /*bEpNum*/);
708 void USB_FreeUserBuf(uint8_t bEpNum /*bEpNum*/, uint8_t bDir);
709 uint16_t USB_ToWord(uint8_t, uint8_t);
710 uint16_t USB_ByteSwap(uint16_t);
711 
712 /**
713  * @}
714  */
715 
716 #endif /* __USB_REGS_H__ */
717