usb_regs.h 40 KB

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