ald_usb.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_usb.c
  5. * @brief USB module driver.
  6. *
  7. * @version V1.0
  8. * @date 25 Feb 2022
  9. * @author AE Team
  10. * @note
  11. * Change Logs:
  12. * Date Author Notes
  13. * 25 Feb 2022 AE Team The first version
  14. *
  15. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  16. *
  17. * SPDX-License-Identifier: Apache-2.0
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the License); you may
  20. * not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  27. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. **********************************************************************************
  31. */
  32. #include "ald_conf.h"
  33. #include "usb_lowlayer_api.h"
  34. /** @addtogroup ES32VF2264_ALD
  35. * @{
  36. */
  37. /** @defgroup USB USB
  38. * @brief USB module driver
  39. * @{
  40. */
  41. #ifdef ALD_USB
  42. /**
  43. * @defgroup USB_Public_Functions USB Public Function
  44. * @{
  45. */
  46. /** @defgroup USB_Public_Functions_Group1 Base functions
  47. * @brief Base functions
  48. * @{
  49. */
  50. /**
  51. * @brief Gets the number of current frame.
  52. * @retval Number of the frame.
  53. */
  54. uint32_t ald_usb_frame_number_get(void)
  55. {
  56. uint8_t framel = (uint8_t)USB->FRAME1;
  57. uint8_t frameh = (uint8_t)USB->FRAME2;
  58. return ((uint32_t)(framel & 0xFF) + ((uint32_t)(frameh & 0xFF) << 8));
  59. }
  60. /**
  61. * @brief Request the session.
  62. * @param start: true/false.
  63. * @retval None
  64. */
  65. void ald_usb_otg_session_request(bool start)
  66. {
  67. if (start)
  68. USB->DEVCON |= (uint8_t)ALD_USB_DEVCON_SESSION;
  69. else
  70. USB->DEVCON &= (uint8_t)(~(ALD_USB_DEVCON_SESSION));
  71. return;
  72. }
  73. /**
  74. * @brief Gets the mode.
  75. * @retval Mode
  76. */
  77. uint32_t ald_usb_mode_get(void)
  78. {
  79. return (USB->DEVCON & ((uint8_t)(ALD_USB_DEVCON_HOST | ALD_USB_DEVCON_SESSION)));
  80. }
  81. /**
  82. * @brief Start host require.
  83. * @retval Mode
  84. */
  85. uint32_t ald_usb_mode_host_req(void)
  86. {
  87. USB->DEVCON |= (uint8_t)ALD_USB_DEVCON_HOSTREQ;
  88. return 0;
  89. }
  90. /**
  91. * @brief Clear host require.
  92. * @retval Mode
  93. */
  94. uint32_t ald_usb_mode_host_req_clear(void)
  95. {
  96. USB->DEVCON &= (uint8_t)(~ALD_USB_DEVCON_HOSTREQ);
  97. return 0;
  98. }
  99. /**
  100. * @brief Enable/Disable the high mode.
  101. * @param enable: ENABLE/DISABLE.
  102. * @retval None
  103. */
  104. void ald_usb_high_speed_enable(bool enable)
  105. {
  106. assert_param(enable == DISABLE);
  107. return;
  108. }
  109. /**
  110. * @brief Gets the speed of the device.
  111. * @retval Type of the speed.
  112. */
  113. uint32_t ald_usb_device_speed_get(void)
  114. {
  115. return ALD_USB_FULL_SPEED;
  116. }
  117. /**
  118. * @brief Gets the number of the endpoint.
  119. * @retval Number of the endpoint.
  120. */
  121. uint32_t ald_usb_num_ep_get( void)
  122. {
  123. return ALD_NUM_USB_EP;
  124. }
  125. /**
  126. * @brief Reset USB Control.
  127. * @retval None
  128. */
  129. void ald_usb_control_reset(void)
  130. {
  131. //ALD_RCU_USB_RESET_ENABLE();
  132. return;
  133. }
  134. /**
  135. * @brief Output USB clock, not support.
  136. * @retval None
  137. */
  138. void ald_usb_clock_output(void)
  139. {
  140. return;
  141. }
  142. /**
  143. * @brief Starts eye diagram for high-speed host, not support.
  144. * @param buf: Buffer for eye diagram.
  145. * @param len: Length of the buffer.
  146. * @retval Status, 0 means success, other values means failure.
  147. */
  148. int ald_usb_eye_diagram_start(uint8_t *buf, uint16_t len)
  149. {
  150. return 0;
  151. }
  152. /**
  153. * @brief Pull up or pull down USB dp line.
  154. * @param pupd: USB_DPDM_FLOATING/USB_DPDM_PUSH_UP/USB_DPDM_PUSH_DOWN.
  155. * @retval None
  156. */
  157. void ald_usb_dppud_set(ald_dpdm_push_t pupd)
  158. {
  159. USB->DPDMCON &= (uint8_t)(~USB_DPDMCON_DPPUD_MSK);
  160. USB->DPDMCON |= (uint8_t)(pupd << USB_DPDMCON_DPPUD_POSS);
  161. return;
  162. }
  163. /**
  164. * @brief Pull up or pull down USB dm line.
  165. * @param pupd: USB_DPDM_FLOATING/USB_DPDM_PUSH_UP/USB_DPDM_PUSH_DOWN.
  166. * @retval None
  167. */
  168. void ald_usb_dmpud_set(ald_dpdm_push_t pupd)
  169. {
  170. USB->DPDMCON &= (uint8_t)(~USB_DPDMCON_DMPUD_MSK);
  171. USB->DPDMCON |= (uint8_t)(pupd << USB_DPDMCON_DMPUD_POSS);
  172. return;
  173. }
  174. /**
  175. * @brief Switch usb mode by software.
  176. * @param host: 0, indicates that software force to host
  177. host: 1, indicates that software force to device.
  178. * @retval None
  179. */
  180. void ald_usb_swcid_host(uint8_t host)
  181. {
  182. if (host)
  183. USB->SWCID |= (uint8_t)(USB_SWCID_HOST_MSK);
  184. else
  185. USB->SWCID &= (uint8_t)(~USB_SWCID_HOST_MSK);
  186. return;
  187. }
  188. /**
  189. * @brief Switch the control method of CID.
  190. * @param cid: 0, indicates that use the hardware control
  191. cid: 1, indicates that use the software control.
  192. * @retval None
  193. */
  194. void ald_usb_swcid_cidctrl(uint8_t cid)
  195. {
  196. if (cid)
  197. USB->SWCID |= (uint8_t)(ALD_USB_SWCID_CIDCTRL);
  198. else
  199. USB->SWCID &= (uint8_t)(~ALD_USB_SWCID_CIDCTRL);
  200. return;
  201. }
  202. /**
  203. * @}
  204. */
  205. /** @defgroup USB_Public_Functions_Group2 Device functions
  206. * @brief Device functions
  207. * @{
  208. */
  209. /**
  210. * @brief Gets the address.
  211. * @retval Address.
  212. */
  213. uint8_t ald_usb_dev_get_addr(void)
  214. {
  215. return (USB->FADDR);
  216. }
  217. /**
  218. * @brief Sets the address.
  219. * @param addr: The address which will be set.
  220. * @retval None
  221. */
  222. void ald_usb_dev_set_addr(uint8_t addr)
  223. {
  224. USB->FADDR = addr;
  225. return;
  226. }
  227. /**
  228. * @brief Enable connection.
  229. * @retval None
  230. */
  231. void ald_usb_dev_connect(void)
  232. {
  233. USB->DPDMCON |= (uint8_t)(ALD_USB_DPDMCON_PHYPWREN);
  234. return;
  235. }
  236. /**
  237. * @brief Disable connection.
  238. * @retval None
  239. */
  240. void ald_usb_dev_disconnect(void)
  241. {
  242. USB->DPDMCON &= (uint8_t)(~(ALD_USB_DPDMCON_PHYPWREN));
  243. return;
  244. }
  245. /**
  246. * @brief Enable the devices suspend.
  247. * @retval None
  248. */
  249. void ald_usb_dev_suspend_enable(void)
  250. {
  251. USB->POWER |= (uint8_t)USB_POWER_SUSPENDEN_MSK;
  252. return;
  253. }
  254. /**
  255. * @brief Configure the endpoint in device mode.
  256. * @param ep_idx: Index of the endpoint
  257. * @param p_max: Size of the maximum package.
  258. * @param flags: Flags of the endpoint.
  259. * @retval None
  260. */
  261. void ald_usb_dev_ep_config(uint32_t ep_idx, uint32_t p_max, uint32_t flags)
  262. {
  263. uint32_t tmp = 0U;
  264. USB->INDEX = (uint8_t)ep_idx;
  265. if (flags & ALD_USB_EP_DEV_IN) {
  266. USB->TXMAXP = (uint8_t)(p_max);
  267. if (flags & ALD_USB_EP_AUTO_SET)
  268. tmp |= USB_TXCSRH_AUTOSET_MSK;
  269. if ((flags & ALD_USB_EP_MODE_MASK) == ALD_USB_EP_MODE_ISOC)
  270. tmp |= USB_TXCSRH_ISO_MSK;
  271. USB->CSR0H_TXCSRH |= (uint8_t)tmp;
  272. USB->CSR0L_TXCSRL |= (uint8_t)USB_TXCSRL_CLRDT_MSK;
  273. }
  274. else {
  275. USB->RXMAXP = (uint8_t)(p_max);
  276. if (flags & ALD_USB_EP_AUTO_CLEAR)
  277. tmp |= USB_RXCSRH_AUTOCLR_MSK;
  278. if ((flags & ALD_USB_EP_MODE_MASK) == ALD_USB_EP_MODE_ISOC)
  279. tmp |= USB_TXCSRH_ISO_MSK;
  280. USB->RXCSRH |= (uint8_t)tmp;
  281. USB->RXCSRL |= (uint8_t)USB_RXCSRL_CLRDT_MSK;
  282. }
  283. return;
  284. }
  285. /**
  286. * @brief Gets the parameters of the endpoint.
  287. * @param ep_idx: Index of the endpoint
  288. * @param p_max: Size of the maximum package.
  289. * @param flags: Flags of the endpoint.
  290. * @retval None
  291. */
  292. void ald_usb_dev_ep_get_config(uint32_t ep_idx, uint32_t *p_max, uint32_t *flags)
  293. {
  294. uint32_t tmp;
  295. USB->INDEX = (uint8_t)ep_idx;
  296. if (*flags & ALD_USB_EP_DEV_IN) {
  297. *flags = ALD_USB_EP_DEV_IN;
  298. *p_max = (uint32_t)USB->TXMAXP;
  299. tmp = (uint32_t)USB->CSR0H_TXCSRH;
  300. if (tmp & USB_TXCSRH_AUTOSET_MSK)
  301. *flags |= ALD_USB_EP_AUTO_SET;
  302. if (tmp & USB_TXCSRH_ISO_MSK)
  303. *flags |= ALD_USB_EP_MODE_ISOC;
  304. else
  305. *flags |= ALD_USB_EP_MODE_BULK;
  306. }
  307. else {
  308. *flags = ALD_USB_EP_DEV_OUT;
  309. *p_max = (uint32_t)USB->RXMAXP;
  310. tmp = (uint32_t)USB->RXCSRH;
  311. if (tmp & USB_RXCSRH_AUTOCLR_MSK)
  312. *flags |= ALD_USB_EP_AUTO_CLEAR;
  313. if (tmp & USB_RXCSRH_ISO_MSK)
  314. *flags |= ALD_USB_EP_MODE_ISOC;
  315. else
  316. *flags |= ALD_USB_EP_MODE_BULK;
  317. }
  318. return;
  319. }
  320. extern void printf_e(const char *fmt, ...);
  321. /**
  322. * @brief Acknowledge the data from host.
  323. * @param ep_idx: Index of the endpoint
  324. * @param last: true/false
  325. * @retval None
  326. */
  327. void ald_usb_dev_ep_data_ack(uint32_t ep_idx, bool last)
  328. {
  329. USB->INDEX = (uint8_t)ep_idx;
  330. if (ep_idx == ALD_USB_EP_0)
  331. USB->CSR0L_TXCSRL |= (uint8_t)(USB_CSR0L_RXRDYC_MSK | (last ? USB_CSR0L_DATAEND_MSK : 0));
  332. else
  333. USB->RXCSRL &= (uint8_t)(~USB_RXCSRL_RXRDY_MSK);
  334. return;
  335. }
  336. /**
  337. * @brief Stall the endpoint.
  338. * @param ep_idx: Index of the endpoint
  339. * @param flags: Flags.
  340. * @retval None
  341. */
  342. void ald_usb_dev_ep_stall(uint32_t ep_idx, uint32_t flags)
  343. {
  344. USB->INDEX = (uint8_t)ep_idx;
  345. if (ep_idx == ALD_USB_EP_0)
  346. USB->CSR0L_TXCSRL |= (uint8_t)(USB_CSR0L_RXRDYC_MSK | USB_CSR0L_STALL_MSK);
  347. else if (flags == ALD_USB_EP_DEV_IN)
  348. USB->CSR0L_TXCSRL |= (uint8_t)USB_TXCSRL_STALL_MSK;
  349. else
  350. USB->RXCSRL |= (uint8_t)USB_RXCSRL_STALL_MSK;
  351. return;
  352. }
  353. /**
  354. * @brief Cancel the stall status.
  355. * @param ep_idx: Index of the endpoint
  356. * @param flags: Flags.
  357. * @retval None
  358. */
  359. void ald_usb_dev_ep_stall_clear(uint32_t ep_idx, uint32_t flags)
  360. {
  361. USB->INDEX = (uint8_t)ep_idx;
  362. if (ep_idx == ALD_USB_EP_0)
  363. USB->CSR0L_TXCSRL &= (uint8_t)(~USB_CSR0L_STALLED_MSK);
  364. else if (flags == ALD_USB_EP_DEV_IN){
  365. USB->CSR0L_TXCSRL &= (uint8_t)(~(USB_TXCSRL_STALL_MSK | USB_TXCSRL_STALLED_MSK));
  366. USB->CSR0L_TXCSRL |= (uint8_t)USB_TXCSRL_CLRDT_MSK;
  367. }
  368. else{
  369. USB->RXCSRL &= (uint8_t)(~(USB_RXCSRL_STALL_MSK | USB_RXCSRL_STALLED_MSK));
  370. USB->RXCSRL |= (uint8_t)USB_RXCSRL_CLRDT_MSK;
  371. }
  372. return;
  373. }
  374. /**
  375. * @brief Clear the status of the endpoint.
  376. * @param ep_idx: Index of the endpoint
  377. * @param flags: Flags.
  378. * @retval None
  379. */
  380. void ald_usb_dev_ep_status_clear(uint32_t ep_idx, uint32_t flags)
  381. {
  382. USB->INDEX = (uint8_t)ep_idx;
  383. if (ep_idx == ALD_USB_EP_0) {
  384. if (flags & ALD_USB_DEV_EP0_OUT_PKTRDY)
  385. USB->CSR0L_TXCSRL |= (uint8_t)USB_CSR0L_RXRDYC_MSK;
  386. if (flags & ALD_USB_DEV_EP0_SETUP_END)
  387. USB->CSR0L_TXCSRL |= (uint8_t)USB_CSR0L_SETENDC_MSK;
  388. if (flags & ALD_USB_DEV_EP0_SENT_STALL)
  389. USB->CSR0L_TXCSRL &= (uint8_t)(~USB_CSR0L_STALLED_MSK);
  390. }
  391. else {
  392. USB->CSR0L_TXCSRL &= (uint8_t)(~(flags & (ALD_USB_DEV_TX_SENT_STALL | ALD_USB_DEV_TX_UNDERRUN)));
  393. USB->RXCSRL &= (uint8_t)(~((flags & (ALD_USB_DEV_RX_SENT_STALL | ALD_USB_DEV_RX_DATA_ERROR
  394. | ALD_USB_DEV_RX_OVERRUN)) >> ALD_USB_RX_EPSTATUS_SHIFT));
  395. }
  396. return;
  397. }
  398. /**
  399. * @}
  400. */
  401. /** @defgroup USB_Public_Functions_Group3 Host functions
  402. * @brief Host functions
  403. * @{
  404. */
  405. /**
  406. * @brief Gets the device's address.
  407. * @param ep_idx: Index of the endpoint
  408. * @param flags: Flags.
  409. * @retval Address
  410. */
  411. uint32_t ald_usb_host_addr_get(uint32_t ep_idx, uint32_t flags)
  412. {
  413. USB->INDEX = (uint8_t)ep_idx;
  414. return (USB->FADDR);
  415. }
  416. /**
  417. * @brief Sets the device's address.
  418. * @param ep_idx: Index of the endpoint.
  419. * @param addr: The device's address.
  420. * @param flags: Flags.
  421. * @retval None
  422. */
  423. void ald_usb_host_addr_set(uint32_t ep_idx, uint32_t addr, uint32_t flags)
  424. {
  425. USB->INDEX = (uint8_t)ep_idx;
  426. USB->FADDR = (uint8_t)addr;
  427. return;
  428. }
  429. /**
  430. * @brief Configure the endpoint in host mode.
  431. * @param ep_idx: Index of the endpoint.
  432. * @param p_max: Size of the maximum package.
  433. * @param nak_val: Value of the nack.
  434. * @param t_ep: Target endpoint.
  435. * @param flags: Flags.
  436. * @retval None
  437. */
  438. void ald_usb_host_ep_config(uint32_t ep_idx, uint32_t p_max, uint32_t nak_val, uint32_t t_ep, uint32_t flags)
  439. {
  440. uint32_t tmp = 0U;
  441. USB->INDEX = (uint8_t)ep_idx;
  442. if (ep_idx == ALD_USB_EP_0) {
  443. USB->NAKLIMIT0_TXINTERVAL = (uint8_t)nak_val;
  444. if (flags & ALD_USB_EP_SPEED_HIGH)
  445. ;
  446. else if (flags & ALD_USB_EP_SPEED_FULL)
  447. ;
  448. else
  449. ;
  450. }
  451. else {
  452. tmp = t_ep;
  453. if (flags & ALD_USB_EP_SPEED_HIGH)
  454. ;
  455. else if (flags & ALD_USB_EP_SPEED_FULL)
  456. ;
  457. else
  458. ;
  459. switch (flags & ALD_USB_EP_MODE_MASK) {
  460. case ALD_USB_EP_MODE_BULK:
  461. tmp |= ALD_USB_TXTYPE1_PROTO_BULK;
  462. break;
  463. case ALD_USB_EP_MODE_ISOC:
  464. tmp |= ALD_USB_TXTYPE1_PROTO_ISOC;
  465. break;
  466. case ALD_USB_EP_MODE_INT:
  467. tmp |= ALD_USB_TXTYPE1_PROTO_INT;
  468. break;
  469. case ALD_USB_EP_MODE_CTRL:
  470. tmp |= ALD_USB_TXTYPE1_PROTO_CTRL;
  471. break;
  472. }
  473. if (flags & ALD_USB_EP_HOST_OUT) {
  474. USB->TXTYPE = (uint8_t)tmp;
  475. USB->NAKLIMIT0_TXINTERVAL = (uint8_t)nak_val;
  476. USB->TXMAXP = (uint8_t)p_max;
  477. tmp = 0;
  478. if (flags & ALD_USB_EP_AUTO_SET)
  479. tmp = (uint8_t)ALD_USB_TXCSRH_AUTOSET;
  480. USB->CSR0H_TXCSRH |= (uint8_t)tmp;
  481. }
  482. else {
  483. USB->RXTYPE = (uint8_t)tmp;
  484. USB->RXINTERVAL = (uint8_t)nak_val;
  485. USB->RXMAXP = (uint8_t)p_max;
  486. tmp = 0;
  487. if (flags & ALD_USB_EP_AUTO_CLEAR)
  488. tmp |= (uint8_t)USB_RXCSRH_AUTOCLR_MSK;
  489. if (flags & ALD_USB_EP_AUTO_REQUEST)
  490. tmp |= (uint8_t)USB_RXCSRH_AUTOREQ_MSK;
  491. USB->RXCSRH |= (uint8_t)tmp;
  492. }
  493. }
  494. return;
  495. }
  496. /**
  497. * @brief Acknowledge the data in host mode.
  498. * @param ep_idx: Index of the endpoint.
  499. * @retval None
  500. */
  501. void ald_usb_host_ep_data_ack(uint32_t ep_idx)
  502. {
  503. USB->INDEX = (uint8_t)ep_idx;
  504. if (ep_idx == ALD_USB_EP_0)
  505. USB->CSR0L_TXCSRL &= (uint8_t)(~(USB_CSR0L_RXRDY_MSK));
  506. else
  507. USB->RXCSRL &= (uint8_t)(~(USB_RXCSRL_RXRDY_MSK));
  508. return;
  509. }
  510. /**
  511. * @brief Toggle the data in host mode.
  512. * The function is used to force the state of the data toggle in host mode.
  513. * If the value passed in the bDataToggle parameter is false, then the data
  514. * toggle is set to the DATA0 state, and if it is true it is set to the DATA1
  515. * state.
  516. * @param ep_idx: Index of the endpoint.
  517. * @param toggle: true/false.
  518. * @param flags: can be USB_EP_HOST_IN or USB_EP_HOST_OUT.
  519. * @retval None
  520. */
  521. void ald_usb_host_ep_data_toggle(uint32_t ep_idx, bool toggle, uint32_t flags)
  522. {
  523. /* not supported by es32f0271. */
  524. return;
  525. }
  526. /**
  527. * @brief Clear the status of endpoint in host mode.
  528. * @param ep_idx: Index of the endpoint.
  529. * @param flags: Flags.
  530. * @retval None
  531. */
  532. void ald_usb_host_ep_status_clear(uint32_t ep_idx, uint32_t flags)
  533. {
  534. USB->INDEX = (uint8_t)ep_idx;
  535. if (ep_idx == ALD_USB_EP_0) {
  536. USB->CSR0L_TXCSRL &= (uint8_t)(~flags & 0xFF);
  537. }
  538. else {
  539. USB->CSR0L_TXCSRL &= (uint8_t)(~flags & 0xFF);
  540. USB->RXCSRL &= (uint8_t)(~(flags >> 16) & 0xFF);
  541. }
  542. return;
  543. }
  544. /**
  545. * @brief Gets the HUB's address.
  546. * @param ep_idx: Index of the endpoint.
  547. * @param flags: Flags.
  548. * @retval Address
  549. */
  550. uint32_t ald_usb_host_hub_addr_get(uint32_t ep_idx, uint32_t flags)
  551. {
  552. /* not supported by es32f0271. */
  553. return 0;
  554. }
  555. /**
  556. * @brief Sets the HUB's address.
  557. * @param ep_idx: Index of the endpoint.
  558. * @param addr: HUB's address which will be set.
  559. * @param flags: Flags.
  560. * @retval Address
  561. */
  562. void ald_usb_host_hub_addr_set(uint32_t ep_idx, uint32_t addr, uint32_t flags)
  563. {
  564. /* not supported by es32f0271. */
  565. return;
  566. }
  567. /**
  568. * @brief Disable power.
  569. * @retval None
  570. */
  571. void ald_usb_host_pwr_disable(void)
  572. {
  573. USB->DPDMCON &= (uint8_t)(~(ALD_USB_DPDMCON_PHYPWREN));
  574. return;
  575. }
  576. /**
  577. * @brief Enable power.
  578. * @retval None
  579. */
  580. void ald_usb_host_pwr_enable(void)
  581. {
  582. USB->DPDMCON |= (uint8_t)(ALD_USB_DPDMCON_PHYPWREN);
  583. return;
  584. }
  585. /**
  586. * @brief Configure power in host mode.
  587. * @param flags: Flags
  588. * @retval None
  589. */
  590. void ald_usb_host_pwr_config(uint32_t flags)
  591. {
  592. return;
  593. }
  594. /**
  595. * @brief Disable the fault parameters of the power.
  596. * @retval None
  597. */
  598. void ald_usb_host_pwr_fault_disable(void)
  599. {
  600. return;
  601. }
  602. /**
  603. * @brief Enable the fault parameters of the power.
  604. * @retval None
  605. */
  606. void ald_usb_host_pwr_fault_enable(void)
  607. {
  608. return;
  609. }
  610. /**
  611. * @brief Request data IN(from device to host)
  612. * @param ep_idx: Index of the endpoint.
  613. * @retval None
  614. */
  615. void ald_usb_host_request_in(uint32_t ep_idx)
  616. {
  617. USB->INDEX = (uint8_t)ep_idx;
  618. if (ep_idx == ALD_USB_EP_0)
  619. USB->CSR0L_TXCSRL |= (uint8_t)USB_CSR0L_REQPKT_MSK;
  620. else
  621. USB->RXCSRL |= (uint8_t)USB_RXCSRL_REQPKT_MSK;
  622. return;
  623. }
  624. /**
  625. * @brief Clear the status of request IN.
  626. * @param ep_idx: Index of the endpoint.
  627. * @retval None
  628. */
  629. void ald_usb_host_request_in_clear(uint32_t ep_idx)
  630. {
  631. USB->INDEX = (uint8_t)ep_idx;
  632. if (ep_idx == ALD_USB_EP_0)
  633. USB->CSR0L_TXCSRL &= (uint8_t)(~USB_CSR0L_REQPKT_MSK);
  634. else
  635. USB->RXCSRL &= (uint8_t)(~USB_RXCSRL_REQPKT_MSK);
  636. return;
  637. }
  638. /**
  639. * @brief Set the request for a status IN transaction.
  640. * @retval None
  641. */
  642. void ald_usb_host_request_status(void)
  643. {
  644. USB->CSR0L_TXCSRL |= (uint8_t)(ALD_USB_CSR0L_REQPKT | ALD_USB_CSR0L_STATUSPKT);
  645. return;
  646. }
  647. /**
  648. * @brief Reset the USB's bus.
  649. * @param start: true/false.
  650. * @retval None
  651. */
  652. void ald_usb_host_reset(bool start)
  653. {
  654. if (start)
  655. USB->POWER |= ALD_USB_POWER_RESET;
  656. else
  657. USB->POWER &= ~(ALD_USB_POWER_RESET);
  658. return;
  659. }
  660. /**
  661. * @brief Resume the devices.
  662. * @param start: true/false.
  663. * @retval None
  664. */
  665. void ald_usb_host_resume(bool start)
  666. {
  667. if (start)
  668. USB->POWER |= (uint8_t)USB_POWER_RESUME_MSK;
  669. else
  670. USB->POWER &= (uint8_t)(~(ALD_USB_POWER_RESUME));
  671. return;
  672. }
  673. /**
  674. * @brief Suspend the devices.
  675. * @retval None
  676. */
  677. void ald_usb_host_suspend(void)
  678. {
  679. USB->POWER |= (uint8_t)USB_POWER_SUSPEND_MSK;
  680. return;
  681. }
  682. /**
  683. * @brief Gets the device's speed.
  684. * @retval Type of the speed.
  685. */
  686. uint32_t ald_usb_host_speed_get(void)
  687. {
  688. if (USB->DEVCON & ALD_USB_DEVCON_FSDEV)
  689. return ALD_USB_FULL_SPEED;
  690. if (USB->DEVCON & ALD_USB_DEVCON_LSDEV)
  691. return ALD_USB_LOW_SPEED;
  692. return ALD_USB_UNDEF_SPEED;
  693. }
  694. /**
  695. * @brief Sets the endpoint speed.
  696. * @param ep_idx: Index of the endpoint.
  697. * @param flags: Type of the speed.
  698. * @retval None
  699. */
  700. void ald_usb_host_ep_speed_set(uint32_t ep_idx, uint32_t flags)
  701. {
  702. /* not support in F0271 */
  703. return;
  704. }
  705. /**
  706. * @brief Ping the endpoint.
  707. * @param ep_idx: Index of the endpoint.
  708. * @param enable: ENABLE/DISABLE.
  709. * @retval None
  710. */
  711. void ald_usb_host_ep_ping(uint32_t ep_idx, bool enable)
  712. {
  713. /* not support in F0271 */
  714. return;
  715. }
  716. /**
  717. * @}
  718. */
  719. /** @defgroup USB_Public_Functions_Group4 Endpoint functions
  720. * @brief Endpoint functions
  721. * @{
  722. */
  723. /**
  724. * @brief Gets the size of the available data.
  725. * @param ep_idx: Index of the endpoint
  726. * @retval Size in bytes.
  727. */
  728. uint32_t ald_usb_ep_data_avail(uint32_t ep_idx)
  729. {
  730. USB->INDEX = (uint8_t)ep_idx;
  731. if (ep_idx == ALD_USB_EP_0) {
  732. if ((USB->CSR0L_TXCSRL & ALD_USB_CSR0L_RXRDY) == 0){
  733. return 0;
  734. }
  735. return USB->COUNT0_RX1;
  736. }
  737. else {
  738. if ((USB->RXCSRL & ALD_USB_CSR0L_RXRDY) == 0)
  739. {
  740. return 0;
  741. }
  742. return (USB->COUNT0_RX1 + (((uint32_t)USB->RXCOUNT2) << 8));
  743. }
  744. }
  745. /**
  746. * @brief Gets the data from FIFO.
  747. * @param ep_idx: Index of the endpoint
  748. * @param data: Pointer to the buffer.
  749. * @param size: Size of the data.
  750. * @retval Status.
  751. */
  752. int32_t ald_usb_ep_data_get(uint32_t ep_idx, uint8_t *data, uint32_t *size)
  753. {
  754. uint32_t i, rx_fifo_addr;
  755. USB->INDEX = (uint8_t)ep_idx;
  756. if (ep_idx == ALD_USB_EP_0) {
  757. if ((USB->CSR0L_TXCSRL & ALD_USB_CSR0L_RXRDY) == 0) {
  758. *size = 0;
  759. return -1;
  760. }
  761. i = USB->COUNT0_RX1;
  762. }
  763. else {
  764. if ((USB->RXCSRL & ALD_USB_CSR0L_RXRDY) == 0) {
  765. *size = 0;
  766. return -1;
  767. }
  768. i = USB->COUNT0_RX1 + (((uint32_t)USB->RXCOUNT2) << 8);
  769. }
  770. i = (i < *size) ? i : *size;
  771. *size = i;
  772. rx_fifo_addr = (uint32_t)(&USB->EP0FIFO) + 4 * ep_idx;
  773. for (; i > 0; i--)
  774. *data++ = *(volatile uint8_t *)(rx_fifo_addr);
  775. return 0;
  776. }
  777. /**
  778. * @brief Puts data to the FIFO.
  779. * @param ep_idx: Index of the endpoint
  780. * @param data: Pointer to the data.
  781. * @param size: Size of the data.
  782. * @retval Status.
  783. */
  784. int32_t ald_usb_ep_data_put(uint32_t ep_idx, uint8_t *data, uint32_t size)
  785. {
  786. uint32_t tx_fifo_addr;
  787. USB->INDEX = (uint8_t)ep_idx;
  788. if (ep_idx == ALD_USB_EP_0) {
  789. if (USB->CSR0L_TXCSRL & ALD_USB_CSR0L_TXRDY)
  790. return -1;
  791. }
  792. else {
  793. if (USB->CSR0L_TXCSRL & ALD_USB_TXCSRL_TXRDY)
  794. return -1;
  795. }
  796. tx_fifo_addr = (uint32_t)(&USB->EP0FIFO) + 4 * ep_idx;
  797. for (; size > 0; size--)
  798. *(volatile uint8_t *)tx_fifo_addr = *data++;
  799. return 0;
  800. }
  801. /**
  802. * @brief Send data.
  803. * @param ep_idx: Index of the endpoint
  804. * @param tx_type: Type.
  805. * @retval Status.
  806. */
  807. int32_t ald_usb_ep_data_send(uint32_t ep_idx, uint32_t tx_type)
  808. {
  809. uint32_t tmp;
  810. USB->INDEX = (uint8_t)ep_idx;
  811. if (ep_idx == ALD_USB_EP_0) {
  812. if (USB->CSR0L_TXCSRL & ALD_USB_CSR0L_TXRDY)
  813. return -1;
  814. tmp = tx_type & 0xFF;
  815. }
  816. else {
  817. if (USB->CSR0L_TXCSRL & ALD_USB_TXCSRL_TXRDY)
  818. return -1;
  819. tmp = (tx_type >> 8) & 0xff;
  820. }
  821. USB->CSR0L_TXCSRL = tmp;
  822. return 0;
  823. }
  824. /**
  825. * @brief Clear the status of the toggle.
  826. * @param ep_idx: Index of the endpoint
  827. * @param flags: Flags.
  828. * @retval None
  829. */
  830. void ald_usb_ep_data_toggle_clear(uint32_t ep_idx, uint32_t flags)
  831. {
  832. USB->INDEX = (uint8_t)ep_idx;
  833. if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN))
  834. USB->CSR0L_TXCSRL |= (uint8_t)ALD_USB_TXCSRL_CLRDT;
  835. else
  836. USB->RXCSRL |= (uint8_t)ALD_USB_RXCSRL_CLRDT;
  837. return;
  838. }
  839. /**
  840. * @brief Sets the size of request data IN
  841. * @param ep_idx: Index of the endpoint
  842. * @param count: Size of request data IN.
  843. * @retval None
  844. */
  845. void ald_usb_ep_req_packet_count(uint32_t ep_idx, uint32_t count)
  846. {
  847. /* not support in f0271 */
  848. return;
  849. }
  850. /**
  851. * @brief Gets the status of the endpoint.
  852. * @param ep_idx: Index of the endpoint
  853. * @retval Status.
  854. */
  855. uint32_t ald_usb_ep_status(uint32_t ep_idx)
  856. {
  857. uint32_t status;
  858. USB->INDEX = (uint8_t)ep_idx;
  859. status = (ep_idx == ALD_USB_EP_0) ? (USB->CSR0L_TXCSRL): (USB->CSR0L_TXCSRL | (USB->RXCSRL << 16));
  860. return status;
  861. }
  862. /**
  863. * @brief Configure the endpoint in DMA mode.
  864. * @param ep_idx: Index of the endpoint
  865. * @param flag: Flags.
  866. * @param en: ENABLE/DISABLE.
  867. * @retval None
  868. */
  869. void ald_usb_ep_dma_config(uint32_t ep_idx, uint32_t flag, type_func_t en)
  870. {
  871. /* Not supported in F0271 */
  872. return;
  873. }
  874. /**
  875. * @}
  876. */
  877. /** @defgroup USB_Public_Functions_Group5 FIFO functions
  878. * @brief FIFO functions
  879. * @{
  880. */
  881. /**
  882. * @brief Gets the address of the FIFO.
  883. * @param ep_idx: Index of the endpoint
  884. * @retval Address
  885. */
  886. uint32_t ald_usb_fifo_addr_get(uint32_t ep_idx)
  887. {
  888. /* Not supported in F0271 */
  889. return 0;
  890. }
  891. /**
  892. * @brief Gets the parameters of the FIFO.
  893. * @param ep_idx: Index of the endpoint
  894. * @param addr: Address.
  895. * @param size: Size of FIFO.
  896. * @param flags: Flags.
  897. * @retval None
  898. */
  899. void ald_usb_fifo_config_get(uint32_t ep_idx, uint32_t *addr, uint32_t *size, uint32_t flags)
  900. {
  901. USB->INDEX = ep_idx;
  902. if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN)) {
  903. *addr = ((uint32_t)USB->TXFIFO1 | ((uint32_t)USB->TXFIFO2 & 0x03)) << 3;
  904. *size = ((USB->TXFIFO2 & ALD_USB_TXFIFO2_DPB) == 0x00) ? (USB->TXFIFO2 & ALD_USB_TXFIFO2_MAXPKTSIZE_1024) : (2 * (USB->TXFIFO2 & ALD_USB_TXFIFO2_MAXPKTSIZE_1024));
  905. }
  906. else {
  907. *addr = ((uint32_t)USB->RXFIFO1 | ((uint32_t)USB->RXFIFO2 & 0x03)) << 3;
  908. *size = ((USB->RXFIFO2 & ALD_USB_RXFIFO2_DPB) == 0x00) ? (USB->RXFIFO2 & ALD_USB_RXFIFO2_MAXPKTSIZE_1024) : (2 * (USB->RXFIFO2 & ALD_USB_RXFIFO2_MAXPKTSIZE_1024));
  909. }
  910. return;
  911. }
  912. /**
  913. * @brief Sets the parameters of the FIFO.
  914. * @param ep_idx: Index of the endpoint
  915. * @param addr: Address.
  916. * @param size: Size of FIFO, valid parameter has defined in usb_lowlayer_api.h.
  917. * @param flags: Flags.
  918. * @retval None
  919. */
  920. void ald_usb_fifo_config_set(uint32_t ep_idx, uint32_t addr, uint32_t size, uint32_t flags)
  921. {
  922. USB->INDEX = ep_idx;
  923. if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN)) {
  924. USB->TXFIFO1 = (uint8_t)((addr >> 3) & 0xFF);
  925. USB->TXFIFO2 |= (uint8_t)(((addr >> 3) >> 8) & 0x0F);
  926. USB->TXFIFO2 |= (uint8_t)(size << USB_TXFIFO2_MAXPKTSIZE_POSS);
  927. USB->CSR0L_TXCSRL |= (uint8_t)ALD_USB_TXCSRL_FLUSH;
  928. }
  929. else {
  930. USB->RXFIFO1 = (uint8_t)((addr >> 3) & 0xFF);
  931. USB->RXFIFO2 |= (uint8_t)(((addr >> 3) >> 8) & 0x0F);
  932. USB->RXFIFO2 |= (uint8_t)(size << USB_RXFIFO2_MAXPKTSIZE_POSS);
  933. USB->RXCSRL |= (uint8_t)ALD_USB_RXCSRL_FLUSH;
  934. }
  935. return;
  936. }
  937. /**
  938. * @brief Flush the FIFO
  939. * @param ep_idx: Index of the endpoint
  940. * @param flags: Flags.
  941. * @retval None
  942. */
  943. void ald_usb_fifo_flush(uint32_t ep_idx, uint32_t flags)
  944. {
  945. USB->INDEX = ep_idx;
  946. if (ep_idx == ALD_USB_EP_0) {
  947. if ((USB->CSR0L_TXCSRL & (ALD_USB_CSR0L_RXRDY | ALD_USB_CSR0L_TXRDY)) != 0)
  948. USB->CSR0H_TXCSRH |= ALD_USB_CSR0H_FLUSH;
  949. }
  950. else {
  951. if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN)) {
  952. if (USB->CSR0L_TXCSRL & ALD_USB_TXCSRL_TXRDY)
  953. USB->CSR0L_TXCSRL |= ALD_USB_TXCSRL_FLUSH;
  954. }
  955. else {
  956. if (USB->RXCSRL & ALD_USB_RXCSRL_RXRDY)
  957. USB->RXCSRL |= ALD_USB_RXCSRL_FLUSH;
  958. }
  959. }
  960. return;
  961. }
  962. /**
  963. * @}
  964. */
  965. /** @defgroup USB_Public_Functions_Group6 Interrupt functions
  966. * @brief Interrupt functions
  967. * @{
  968. */
  969. /**
  970. * @brief Disable interrupt.
  971. * @param flags: Type of the interrupt.
  972. * @retval None
  973. */
  974. void ald_usb_int_disable(uint32_t flags)
  975. {
  976. if (flags & ALD_USB_IDR_STATUS)
  977. USB->IDR |= (uint8_t)(flags & ALD_USB_IDR_STATUS);
  978. return;
  979. }
  980. /**
  981. * @brief Enable interrupt.
  982. * @param flags: Type of the interrupt.
  983. * @retval None
  984. */
  985. void ald_usb_int_enable(uint32_t flags)
  986. {
  987. if (flags & ALD_USB_IER_STATUS)
  988. USB->IER |= (uint8_t)(flags & ALD_USB_IER_STATUS);
  989. return;
  990. }
  991. /**
  992. * @brief Gets the status of the interrupt.
  993. * @retval Status.
  994. */
  995. uint32_t ald_usb_int_status_get(void)
  996. {
  997. uint32_t Status;
  998. Status = USB->IFM & 0x7F;
  999. USB->ICR |= Status;
  1000. return Status;
  1001. }
  1002. /**
  1003. * @brief Disable interrupt of the endpoint.
  1004. * @param flags: Type of the interrupt.
  1005. * @retval None
  1006. */
  1007. void ald_usb_int_disable_ep(uint32_t flags)
  1008. {
  1009. USB->TXIDR |= (uint8_t)(flags & (ALD_USB_INTEP_HOST_OUT | ALD_USB_INTEP_DEV_IN | ALD_USB_INTEP_0));
  1010. USB->RXIDR |= (uint8_t)((flags & (ALD_USB_INTEP_HOST_IN | ALD_USB_INTEP_DEV_OUT)) >> ALD_USB_INTEP_RX_SHIFT);
  1011. return;
  1012. }
  1013. /**
  1014. * @brief Enable interrupt of the endpoint.
  1015. * @param flags: Type of the interrupt.
  1016. * @retval None
  1017. */
  1018. void ald_usb_int_enable_ep(uint32_t flags)
  1019. {
  1020. USB->TXIER |= (uint8_t)(flags & (ALD_USB_INTEP_HOST_OUT | ALD_USB_INTEP_DEV_IN | ALD_USB_INTEP_0));
  1021. USB->RXIER |= (uint8_t)((flags & (ALD_USB_INTEP_HOST_IN | ALD_USB_INTEP_DEV_OUT)) >> ALD_USB_INTEP_RX_SHIFT);
  1022. return;
  1023. }
  1024. /**
  1025. * @brief Gets the ststus of the endpoint interrupt.
  1026. * @retval Status.
  1027. */
  1028. uint32_t ald_usb_int_status_ep_get(void)
  1029. {
  1030. uint32_t status;
  1031. status = USB->TXIFM;
  1032. status |= (USB->RXIFM << ALD_USB_INTEP_RX_SHIFT);
  1033. USB->TXICR |= (uint8_t)(status & 0xFF);
  1034. USB->RXICR |= (uint8_t)((status >> ALD_USB_INTEP_RX_SHIFT) & 0xFF);
  1035. return status;
  1036. }
  1037. /**
  1038. * @brief Register USB's interrupt.
  1039. * @retval None
  1040. */
  1041. void ald_usb_int_register(void)
  1042. {
  1043. //ald_mcu_irq_config(USB_IRQn, 2, ENABLE);
  1044. csi_vic_set_prio(USB_IRQn, 3);
  1045. csi_vic_enable_sirq(USB_IRQn);
  1046. return;
  1047. }
  1048. /**
  1049. * @brief Unregister USB's interrupt.
  1050. * @retval None
  1051. */
  1052. void ald_usb_int_unregister(void)
  1053. {
  1054. //ald_mcu_irq_config(USB_IRQn, 2, DISABLE);
  1055. csi_vic_disable_sirq(USB_IRQn);
  1056. return;
  1057. }
  1058. /**
  1059. * @brief Get USB's interrupt number.
  1060. * @retval None
  1061. */
  1062. uint32_t ald_usb_int_num_get(void)
  1063. {
  1064. return USB_IRQn;
  1065. }
  1066. /**
  1067. * @}
  1068. */
  1069. /** @defgroup USB_Public_Functions_Group7 DMA functions
  1070. * @brief DMA functions
  1071. * @{
  1072. */
  1073. #if defined(__ALD_MDA_H__)
  1074. /**
  1075. * @brief Configure DMA's channel.
  1076. * @param ch: Channel.
  1077. * @param addr: Address.
  1078. * @param count: Size of the data to be moved.
  1079. * @param ctrl: Parameters of the DMA's controler
  1080. * @retval None
  1081. */
  1082. void ald_usb_dma_channel_config(uint8_t ch, uint32_t addr, uint32_t count, uint32_t ctrl)
  1083. {
  1084. return;
  1085. }
  1086. /**
  1087. * @brief Start multiple receive.
  1088. * @param ep_idx: Index of the endpoint
  1089. * @retval None
  1090. */
  1091. void ald_usb_dma_mult_recv_start(uint32_t ep_idx)
  1092. {
  1093. return;
  1094. }
  1095. /**
  1096. * @brief Start DMA's machine.
  1097. * @param ch: Channel.
  1098. * @retval None
  1099. */
  1100. void ald_usb_dma_channel_start(uint8_t ch)
  1101. {
  1102. return;
  1103. }
  1104. /**
  1105. * @brief Stop DMA's machine.
  1106. * @param ch: Channel.
  1107. * @retval None
  1108. */
  1109. void ald_usb_dma_channel_stop(uint8_t ch)
  1110. {
  1111. return;
  1112. }
  1113. /**
  1114. * @brief Gets flags of the interrupt.
  1115. * @retval Flags of the interrupt.
  1116. */
  1117. uint32_t ald_usb_dma_get_interrupt_flag(void)
  1118. {
  1119. return;
  1120. }
  1121. /**
  1122. * @brief Gets the status of the error.
  1123. * @param ch: Channel.
  1124. * @retval Status.
  1125. */
  1126. uint32_t ald_usb_dma_get_channel_error(uint8_t ch)
  1127. {
  1128. return 0;
  1129. }
  1130. /**
  1131. * @brief Clear the status of the error.
  1132. * @param ch: Channel.
  1133. * @retval None
  1134. */
  1135. void ald_usb_dma_clear_channel_error(uint8_t ch)
  1136. {
  1137. return;
  1138. }
  1139. #endif
  1140. /**
  1141. * @}
  1142. */
  1143. /** @defgroup USB_Public_Functions_Group8 LPM functions
  1144. * @brief LPM functions
  1145. * @{
  1146. */
  1147. /**
  1148. * @brief Gets status of remote wakeup.
  1149. * @retval Status.
  1150. */
  1151. uint32_t ald_usb_lpm_remote_wake_is_enable(void)
  1152. {
  1153. return 1;
  1154. }
  1155. /**
  1156. * @brief Gets the link status
  1157. * @retval Status
  1158. */
  1159. uint32_t ald_usb_lpm_link_status_get(void)
  1160. {
  1161. return 0;
  1162. }
  1163. /**
  1164. * @brief Gets the index of the endpoint.
  1165. * @retval Index of the endpoint.
  1166. */
  1167. uint32_t ald_usb_lpm_ep_get(void)
  1168. {
  1169. return 0;
  1170. }
  1171. /**
  1172. * @brief Gets the status of the interrupt.
  1173. * @retval Status.
  1174. */
  1175. uint32_t ald_usb_lpm_int_status_get(void)
  1176. {
  1177. return 0;
  1178. }
  1179. /**
  1180. * @brief Disable the LPM interrupt.
  1181. * @retval None
  1182. */
  1183. void ald_usb_lpm_int_disable(uint32_t ints)
  1184. {
  1185. return;
  1186. }
  1187. /**
  1188. * @brief Enable the LPM interrupt.
  1189. * @retval None
  1190. */
  1191. void ald_usb_lpm_int_enable(uint32_t ints)
  1192. {
  1193. return;
  1194. }
  1195. /**
  1196. * @brief Transmit a LPM transaction in host mode.
  1197. * @param addr: Address.
  1198. * @param ep_idx: Index of the endpoint.
  1199. * @retval None
  1200. */
  1201. void ald_usb_host_lpm_send(uint32_t addr, uint32_t ep_idx)
  1202. {
  1203. return;
  1204. }
  1205. /**
  1206. * @brief Configure the LPM parameters in host mode.
  1207. * @param resume_time: Resume time.
  1208. * @param config: Parameters
  1209. * @retval None
  1210. */
  1211. void ald_usb_host_lpm_config(uint32_t resume_time, uint32_t config)
  1212. {
  1213. return;
  1214. }
  1215. /**
  1216. * @brief Initiate a RESUME from the L1 state in host mode.
  1217. * @retval None
  1218. */
  1219. void ald_usb_host_lpm_resume(void)
  1220. {
  1221. return;
  1222. }
  1223. /**
  1224. * @brief Enable remote wakeup in device mode.
  1225. * @retval None
  1226. */
  1227. void ald_usb_dev_lpm_remote_wake(void)
  1228. {
  1229. return;
  1230. }
  1231. /**
  1232. * @brief Enable remote wakeup in device mode.
  1233. * @retval None
  1234. */
  1235. void ald_usb_dev_lpm_config(uint32_t config)
  1236. {
  1237. return;
  1238. }
  1239. /**
  1240. * @brief Enable LPM in device mode.
  1241. * @retval None
  1242. */
  1243. void ald_usb_dev_lpm_enable(void)
  1244. {
  1245. return;
  1246. }
  1247. /**
  1248. * @brief Disable LPM in device mode.
  1249. * @retval None
  1250. */
  1251. void ald_usb_dev_lpm_disable(void)
  1252. {
  1253. return;
  1254. }
  1255. /**
  1256. * @}
  1257. */
  1258. /** @defgroup USB_Public_Functions_Group10 LPM functions
  1259. * @brief USB SWVBUS control
  1260. * @{
  1261. */
  1262. /**
  1263. * @brief Selet the control mode of VBUS.
  1264. * @param sigctl: 0, indicates that use the hardware control
  1265. * sigctl: 1, indicates that use the software control.
  1266. * @retval Status.
  1267. */
  1268. void ald_usb_swvbus_sigctl_set(uint8_t sigctl)
  1269. {
  1270. if (sigctl == 1)
  1271. {
  1272. USB->SWVBUS |= 0x01 << 0;
  1273. }
  1274. else
  1275. {
  1276. USB->SWVBUS &= ~(0x01 << 0);
  1277. }
  1278. return;
  1279. }
  1280. /**
  1281. * @brief Selet the control mode of VBUS.
  1282. * @param None.
  1283. * @retval Status: 0, indicates that use the hardware control
  1284. * Status: 1, indicates that use the software control.
  1285. */
  1286. uint8_t ald_usb_swvbus_sigctl_get(void)
  1287. {
  1288. return (USB->SWVBUS & (0x01 << 0));
  1289. }
  1290. /**
  1291. * @brief Set session end threshold.
  1292. * @param thd: 0, indicates that lower than the session end threshold
  1293. * thd: 1, indicates that higher than the session end threshold.
  1294. * @retval Status.
  1295. */
  1296. void ald_usb_swvbus_sesendth_set(uint8_t thd)
  1297. {
  1298. if (thd == 1)
  1299. {
  1300. USB->SWVBUS |= 0x01 << 1;
  1301. }
  1302. else
  1303. {
  1304. USB->SWVBUS &= ~(0x01 << 1);
  1305. }
  1306. return;
  1307. }
  1308. /**
  1309. * @brief Set session end threshold.
  1310. * @param None.
  1311. * @retval Status: 0, indicates that lower than the session end threshold
  1312. * Status: 1, indicates that higher than the session end threshold.
  1313. */
  1314. uint8_t ald_usb_swvbus_sesendth_get(void)
  1315. {
  1316. return (USB->SWVBUS & (0x01 << 1));
  1317. }
  1318. /**
  1319. * @brief Set session valid threshold.
  1320. * @param thd: 0, indicates that lower than the session valid threshold
  1321. thd: 1, indicates that higher than the session valid threshold.
  1322. * @retval Status.
  1323. */
  1324. void ald_usb_swvbus_sesvalth_set(uint8_t thd)
  1325. {
  1326. if (thd == 1)
  1327. {
  1328. USB->SWVBUS |= 0x01 << 2;
  1329. }
  1330. else
  1331. {
  1332. USB->SWVBUS &= ~(0x01 << 2);
  1333. }
  1334. return;
  1335. }
  1336. /**
  1337. * @brief Set session valid threshold.
  1338. * @param None.
  1339. * @retval Status: 0, indicates that lower than the session valid threshold
  1340. Status: 1, indicates that higher than the session valid threshold.
  1341. */
  1342. uint8_t ald_usb_swvbus_sesvalth_get(void)
  1343. {
  1344. return (USB->SWVBUS & (0x01 << 2));
  1345. }
  1346. /**
  1347. * @brief Set VBUS valid threshold.
  1348. * @param thd: 0, indicates that lower than the vbus valid threshold
  1349. thd: 1, indicates that higher than the vbus valid threshold.
  1350. * @retval Status.
  1351. */
  1352. void ald_usb_swvbus_valth_set(uint8_t thd)
  1353. {
  1354. if (thd == 1)
  1355. {
  1356. USB->SWVBUS |= 0x01 << 3;
  1357. }
  1358. else
  1359. {
  1360. USB->SWVBUS &= ~(0x01 << 3);
  1361. }
  1362. return;
  1363. }
  1364. /**
  1365. * @brief Set VBUS valid threshold.
  1366. * @param None.
  1367. * @retval Status: 0, indicates that lower than the vbus valid threshold
  1368. Status:thd: 1, indicates that higher than the vbus valid threshold.
  1369. */
  1370. uint8_t ald_usb_swvbus_valth_get(void)
  1371. {
  1372. return (USB->SWVBUS & (0x01 << 3));
  1373. }
  1374. /**
  1375. * @}
  1376. */
  1377. /** @defgroup USB_Public_Functions_Group11 components initialization functions
  1378. * @brief USB components initialization
  1379. * @{
  1380. */
  1381. /**
  1382. * @brief Initialize usb host components.
  1383. * @retval None
  1384. */
  1385. void ald_usb_host_components_init(void)
  1386. {
  1387. WRITE_REG(SYSCFG->PROT, 0x55AA6996U);
  1388. RMU->AHB1RSTR |= 0x40;
  1389. RMU->AHB1RSTR &= 0xFFFFFFBF;
  1390. WRITE_REG(SYSCFG->PROT, 0x0);
  1391. /* Config EP0 */
  1392. ald_usb_host_ep_config(ALD_USB_EP_0, 64, 0, 0, (ALD_USB_EP_MODE_CTRL | ALD_USB_EP_SPEED_FULL | ALD_USB_EP_HOST_OUT));
  1393. /* Clear interrupts */
  1394. /* Enable PHY power */
  1395. ald_usb_host_pwr_enable();
  1396. /* clear hnp session */
  1397. ald_usb_otg_session_request(false);
  1398. /* set vbus control mode and threshold value */
  1399. ald_usb_swvbus_sigctl_set(1);
  1400. ald_usb_swvbus_sesendth_set(1);
  1401. ald_usb_swvbus_sesvalth_set(1);
  1402. ald_usb_swvbus_valth_set(1);
  1403. /* Pull down DP and DM */
  1404. ald_usb_dppud_set(ALD_USB_DPDM_PUSH_DOWN);
  1405. ald_usb_dmpud_set(ALD_USB_DPDM_PUSH_DOWN);
  1406. /* software control CID */
  1407. ald_usb_swcid_cidctrl(1);
  1408. /* force to host mode */
  1409. ald_usb_swcid_host(0);
  1410. /* start host request */
  1411. ald_usb_mode_host_req();
  1412. /* Start hnp */
  1413. ald_usb_otg_session_request(true);
  1414. /* Clear interrupts */
  1415. ald_usb_int_status_get();
  1416. /* Init interrupts */
  1417. ald_usb_int_enable(ALD_USB_INTCTRL_SESSION | ALD_USB_INTCTRL_DISCONNECT | ALD_USB_INTCTRL_CONNECT | ALD_USB_INTCTRL_SOF |
  1418. ALD_USB_INTCTRL_BABBLE | ALD_USB_INTCTRL_RESUME);
  1419. ald_usb_int_enable_ep(ALD_USB_INTEP_ALL);
  1420. ald_usb_int_register();
  1421. return;
  1422. }
  1423. /**
  1424. * @brief Initialize usb device components.
  1425. * @retval None
  1426. */
  1427. void ald_usb_device_components_init(void)
  1428. {
  1429. ald_usb_otg_session_request(true);
  1430. #if (defined(ES32VF2264))
  1431. ald_usb_dppud_set(ALD_USB_DPDM_FLOATING);/*测试板的USBDP(PA12),上拉到3.3V。具体情况请参考ReadMe.txt*/
  1432. #else
  1433. ald_usb_dppud_set(USB_DPDM_PUSH_UP);
  1434. #endif /* ES32VF2264 */
  1435. /* software control CID */
  1436. ald_usb_swcid_cidctrl(1);
  1437. /* force to dev mode */
  1438. ald_usb_swcid_host(1);
  1439. ald_usb_dev_suspend_enable();
  1440. return;
  1441. }
  1442. /**
  1443. * @}
  1444. */
  1445. /**
  1446. * @}
  1447. */
  1448. #endif /* ALD_USB */
  1449. /**
  1450. * @}
  1451. */
  1452. /**
  1453. * @}
  1454. */