usb.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /**
  2. * \file
  3. *
  4. * \brief SAM USB Driver
  5. *
  6. * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef USB_H_INCLUDED
  47. #define USB_H_INCLUDED
  48. #include <compiler.h>
  49. #include <clock.h>
  50. #include <gclk.h>
  51. #include <pinmux.h>
  52. #include <system_interrupt.h>
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /**
  57. * \defgroup asfdoc_sam0_usb_group SAM Universal Serial Bus (USB)
  58. *
  59. * The Universal Serial Bus (USB) module complies with the USB 2.1 specification.
  60. *
  61. * The following peripherals are used by this module:
  62. * - USB (Universal Serial Bus)
  63. *
  64. * The following devices can use this module:
  65. * - Atmel | SMART SAM D21
  66. * - Atmel | SMART SAM R21
  67. * - Atmel | SMART SAM D11 (Only USB device support on SAM D11 device)
  68. * - Atmel | SMART SAM L21
  69. * - Atmel | SMART SAM L22 (Only USB device support on SAM L22 device)
  70. * - Atmel | SMART SAM DA1
  71. *
  72. * The USB module covers following mode:
  73. * \if USB_DEVICE_MODE
  74. * - USB Device Mode
  75. * \endif
  76. * \if USB_HOST_MODE
  77. * - USB Host Mode
  78. * \endif
  79. *
  80. * The USB module covers following speed:
  81. * \if USB_HS_MODE
  82. * - USB High Speed (480Mbit/s)
  83. * \endif
  84. * - USB Full Speed (12Mbit/s)
  85. * \if USB_LS_MODE
  86. * - USB Low Speed (1.5Mbit/s)
  87. * \endif
  88. *
  89. * \if USB_LPM_MODE
  90. * The USB module supports Link Power Management (LPM-L1) protocol.
  91. * \endif
  92. *
  93. * USB support needs whole set of enumeration process, to make the device
  94. * recognizable and usable. The USB driver is designed to interface to the
  95. * USB Stack in Atmel Software Framework (ASF).
  96. *
  97. * \if USB_DEVICE_MODE
  98. * \section asfdoc_sam0_usb_device USB Device Mode
  99. * The ASF USB Device Stack has defined the USB Device Driver (UDD) interface,
  100. * to support USB device operations. The USB module device driver complies with
  101. * this interface, so that the USB Device Stack can work based on the
  102. * USB module.
  103. *
  104. * Refer to <a href="http://www.atmel.com/images/doc8360.pdf">
  105. * "ASF - USB Device Stack"</a> for more details.
  106. * \endif
  107. *
  108. * \if USB_HOST_MODE
  109. * \section adfdoc_sam0_usb_host USB Host Mode
  110. * The ASF USB Host Stack has defined the USB Host Driver (UHD) interface,
  111. * to support USB host operations. The USB module host driver complies with
  112. * this interface, so that the USB Host Stack can work based on the USB module.
  113. *
  114. * Refer to <a href="http://www.atmel.com/images/doc8486.pdf">
  115. * "ASF - USB Host Stack"</a> for more details.
  116. * \endif
  117. */
  118. /** Enum for the speed status for the USB module */
  119. enum usb_speed {
  120. USB_SPEED_LOW,
  121. USB_SPEED_FULL,
  122. };
  123. /** Enum for the possible callback types for the USB in host module */
  124. enum usb_host_callback {
  125. USB_HOST_CALLBACK_SOF,
  126. USB_HOST_CALLBACK_RESET,
  127. USB_HOST_CALLBACK_WAKEUP,
  128. USB_HOST_CALLBACK_DNRSM,
  129. USB_HOST_CALLBACK_UPRSM,
  130. USB_HOST_CALLBACK_RAMACER,
  131. USB_HOST_CALLBACK_CONNECT,
  132. USB_HOST_CALLBACK_DISCONNECT,
  133. USB_HOST_CALLBACK_N,
  134. };
  135. /** Enum for the possible callback types for the USB pipe in host module */
  136. enum usb_host_pipe_callback {
  137. USB_HOST_PIPE_CALLBACK_TRANSFER_COMPLETE,
  138. USB_HOST_PIPE_CALLBACK_ERROR,
  139. USB_HOST_PIPE_CALLBACK_SETUP,
  140. USB_HOST_PIPE_CALLBACK_STALL,
  141. USB_HOST_PIPE_CALLBACK_N,
  142. };
  143. /**
  144. * \brief Host pipe types.
  145. */
  146. enum usb_host_pipe_type {
  147. USB_HOST_PIPE_TYPE_DISABLE,
  148. USB_HOST_PIPE_TYPE_CONTROL,
  149. USB_HOST_PIPE_TYPE_ISO,
  150. USB_HOST_PIPE_TYPE_BULK,
  151. USB_HOST_PIPE_TYPE_INTERRUPT,
  152. USB_HOST_PIPE_TYPE_EXTENDED,
  153. };
  154. /**
  155. * \brief Host pipe token types.
  156. */
  157. enum usb_host_pipe_token {
  158. USB_HOST_PIPE_TOKEN_SETUP,
  159. USB_HOST_PIPE_TOKEN_IN,
  160. USB_HOST_PIPE_TOKEN_OUT,
  161. };
  162. /**
  163. * \brief Enumeration for the possible callback types for the USB in device module
  164. */
  165. enum usb_device_callback {
  166. USB_DEVICE_CALLBACK_SOF,
  167. USB_DEVICE_CALLBACK_RESET,
  168. USB_DEVICE_CALLBACK_WAKEUP,
  169. USB_DEVICE_CALLBACK_RAMACER,
  170. USB_DEVICE_CALLBACK_SUSPEND,
  171. USB_DEVICE_CALLBACK_LPMNYET,
  172. USB_DEVICE_CALLBACK_LPMSUSP,
  173. USB_DEVICE_CALLBACK_N,
  174. };
  175. /**
  176. * \brief Enumeration for the possible callback types for the USB endpoint in device module
  177. */
  178. enum usb_device_endpoint_callback {
  179. USB_DEVICE_ENDPOINT_CALLBACK_TRCPT,
  180. USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL,
  181. USB_DEVICE_ENDPOINT_CALLBACK_RXSTP,
  182. USB_DEVICE_ENDPOINT_CALLBACK_STALL,
  183. USB_DEVICE_EP_CALLBACK_N,
  184. };
  185. /**
  186. * \brief Device Endpoint types.
  187. */
  188. enum usb_device_endpoint_type {
  189. USB_DEVICE_ENDPOINT_TYPE_DISABLE,
  190. USB_DEVICE_ENDPOINT_TYPE_CONTROL,
  191. USB_DEVICE_ENDPOINT_TYPE_ISOCHRONOUS,
  192. USB_DEVICE_ENDPOINT_TYPE_BULK,
  193. USB_DEVICE_ENDPOINT_TYPE_INTERRUPT,
  194. };
  195. /**
  196. * \brief Endpoint Size
  197. */
  198. enum usb_endpoint_size {
  199. USB_ENDPOINT_8_BYTE,
  200. USB_ENDPOINT_16_BYTE,
  201. USB_ENDPOINT_32_BYTE,
  202. USB_ENDPOINT_64_BYTE,
  203. USB_ENDPOINT_128_BYTE,
  204. USB_ENDPOINT_256_BYTE,
  205. USB_ENDPOINT_512_BYTE,
  206. USB_ENDPOINT_1023_BYTE,
  207. };
  208. /**
  209. * \brief Link Power Management Handshake.
  210. */
  211. enum usb_device_lpm_mode {
  212. USB_DEVICE_LPM_NOT_SUPPORT,
  213. USB_DEVICE_LPM_ACK,
  214. USB_DEVICE_LPM_NYET,
  215. };
  216. /**
  217. * \brief Module structure
  218. */
  219. struct usb_module;
  220. /**
  221. * \name Host Callback Functions Types
  222. * @{
  223. */
  224. typedef void (*usb_host_callback_t)(struct usb_module *module_inst);
  225. typedef void (*usb_host_pipe_callback_t)(struct usb_module *module_inst, void *);
  226. /** @} */
  227. /**
  228. * \name Device Callback Functions Types
  229. * @{
  230. */
  231. typedef void (*usb_device_callback_t)(struct usb_module *module_inst, void* pointer);
  232. typedef void (*usb_device_endpoint_callback_t)(struct usb_module *module_inst, void* pointer);
  233. /** @} */
  234. /** USB configurations */
  235. struct usb_config {
  236. /** \c true for host, \c false for device. */
  237. bool select_host_mode;
  238. /** When \c true the module is enabled during standby. */
  239. bool run_in_standby;
  240. /** Generic Clock Generator source channel. */
  241. enum gclk_generator source_generator;
  242. /** Speed mode */
  243. enum usb_speed speed_mode;
  244. };
  245. /**
  246. * \brief USB software module instance structure.
  247. *
  248. * USB software module instance structure, used to retain software state
  249. * information of an associated hardware module instance.
  250. *
  251. */
  252. struct usb_module {
  253. /** Hardware module pointer of the associated USB peripheral. */
  254. Usb *hw;
  255. #if !SAMD11 && !SAML22
  256. /** Array to store host related callback functions */
  257. usb_host_callback_t host_callback[USB_HOST_CALLBACK_N];
  258. usb_host_pipe_callback_t host_pipe_callback[USB_PIPE_NUM][USB_HOST_PIPE_CALLBACK_N];
  259. /** Bit mask for host callbacks registered */
  260. uint8_t host_registered_callback_mask;
  261. /** Bit mask for host callbacks enabled */
  262. uint8_t host_enabled_callback_mask;
  263. /** Bit mask for host pipe callbacks registered */
  264. uint8_t host_pipe_registered_callback_mask[USB_PIPE_NUM];
  265. /** Bit mask for host pipe callbacks enabled */
  266. uint8_t host_pipe_enabled_callback_mask[USB_PIPE_NUM];
  267. #endif
  268. /** Array to store device related callback functions */
  269. usb_device_callback_t device_callback[USB_DEVICE_CALLBACK_N];
  270. usb_device_endpoint_callback_t device_endpoint_callback[USB_EPT_NUM][USB_DEVICE_EP_CALLBACK_N];
  271. /** Bit mask for device callbacks registered */
  272. uint16_t device_registered_callback_mask;
  273. /** Bit mask for device callbacks enabled */
  274. uint16_t device_enabled_callback_mask;
  275. /** Bit mask for device endpoint callbacks registered */
  276. uint8_t device_endpoint_registered_callback_mask[USB_EPT_NUM];
  277. /** Bit mask for device endpoint callbacks enabled */
  278. uint8_t device_endpoint_enabled_callback_mask[USB_EPT_NUM];
  279. };
  280. /** USB host pipe configurations */
  281. struct usb_host_pipe_config {
  282. /** device address */
  283. uint8_t device_address;
  284. /** endpoint address */
  285. uint8_t endpoint_address;
  286. /** Pipe type */
  287. enum usb_host_pipe_type pipe_type;
  288. /** interval */
  289. uint8_t binterval;
  290. /** pipe size */
  291. uint16_t size;
  292. };
  293. /** USB device endpoint configurations */
  294. struct usb_device_endpoint_config {
  295. /** device address */
  296. uint8_t ep_address;
  297. /** endpoint size */
  298. enum usb_endpoint_size ep_size;
  299. /** automatic zero length packet mode, \c true to enable */
  300. bool auto_zlp;
  301. /** type of endpoint with Bank */
  302. enum usb_device_endpoint_type ep_type;
  303. };
  304. /** USB host pipe callback status parameter structure */
  305. struct usb_pipe_callback_parameter {
  306. /** current pipe number */
  307. uint8_t pipe_num;
  308. /** pipe error status */
  309. uint8_t pipe_error_status;
  310. /** actual transferred data size */
  311. uint16_t transfered_size;
  312. /** required data size */
  313. uint16_t required_size;
  314. };
  315. /** USB device endpoint callback status parameter structure */
  316. struct usb_endpoint_callback_parameter {
  317. uint16_t received_bytes;
  318. uint16_t sent_bytes;
  319. uint16_t out_buffer_size;
  320. uint8_t endpoint_address;
  321. };
  322. void usb_enable(struct usb_module *module_inst);
  323. void usb_disable(struct usb_module *module_inst);
  324. /**
  325. * \brief Get the status of USB module's state machine
  326. *
  327. * \param module_inst Pointer to USB module instance
  328. */
  329. static inline uint8_t usb_get_state_machine_status(struct usb_module *module_inst)
  330. {
  331. /* Sanity check arguments */
  332. Assert(module_inst);
  333. Assert(module_inst->hw);
  334. return module_inst->hw->DEVICE.FSMSTATUS.reg;
  335. }
  336. void usb_get_config_defaults(struct usb_config *module_config);
  337. enum status_code usb_init(struct usb_module *module_inst, Usb *const hw,
  338. struct usb_config *module_config);
  339. #if !SAMD11 && !SAML22
  340. /**
  341. * \brief Enable the USB host by setting the VBUS OK
  342. *
  343. * \param module_inst Pointer to USB software instance struct
  344. */
  345. static inline void usb_host_enable(struct usb_module *module_inst)
  346. {
  347. /* Sanity check arguments */
  348. Assert(module_inst);
  349. Assert(module_inst->hw);
  350. module_inst->hw->HOST.CTRLB.bit.VBUSOK = 1;
  351. }
  352. /**
  353. * \brief Send the USB reset
  354. *
  355. * \param module_inst Pointer to USB software instance struct
  356. */
  357. static inline void usb_host_send_reset(struct usb_module *module_inst)
  358. {
  359. /* Sanity check arguments */
  360. Assert(module_inst);
  361. Assert(module_inst->hw);
  362. module_inst->hw->HOST.CTRLB.bit.BUSRESET = 1;
  363. }
  364. /**
  365. * \brief Enable the USB SOF generation
  366. *
  367. * \param module_inst Pointer to USB software instance struct
  368. */
  369. static inline void usb_host_enable_sof(struct usb_module *module_inst)
  370. {
  371. /* Sanity check arguments */
  372. Assert(module_inst);
  373. Assert(module_inst->hw);
  374. module_inst->hw->HOST.CTRLB.bit.SOFE = 1;
  375. }
  376. /**
  377. * \brief Disable the USB SOF generation
  378. *
  379. * \param module_inst Pointer to USB software instance struct
  380. */
  381. static inline void usb_host_disable_sof(struct usb_module *module_inst)
  382. {
  383. /* Sanity check arguments */
  384. Assert(module_inst);
  385. Assert(module_inst->hw);
  386. module_inst->hw->HOST.CTRLB.bit.SOFE = 0;
  387. }
  388. /**
  389. * \brief Check the USB SOF generation status
  390. *
  391. * \param module_inst Pointer to USB software instance struct
  392. *
  393. * \return USB SOF generation status, \c true if SOF generation is ON.
  394. */
  395. static inline bool usb_host_is_sof_enabled(struct usb_module *module_inst)
  396. {
  397. /* Sanity check arguments */
  398. Assert(module_inst);
  399. Assert(module_inst->hw);
  400. return module_inst->hw->HOST.CTRLB.bit.SOFE;
  401. }
  402. /**
  403. * \brief Send the USB host resume
  404. *
  405. * \param module_inst Pointer to USB software instance struct
  406. */
  407. static inline void usb_host_send_resume(struct usb_module *module_inst)
  408. {
  409. /* Sanity check arguments */
  410. Assert(module_inst);
  411. Assert(module_inst->hw);
  412. module_inst->hw->HOST.CTRLB.bit.RESUME= 1;
  413. }
  414. /**
  415. * \brief Send the USB host LPM resume
  416. *
  417. * \param module_inst Pointer to USB software instance struct
  418. */
  419. static inline void usb_host_send_l1_resume(struct usb_module *module_inst)
  420. {
  421. /* Sanity check arguments */
  422. Assert(module_inst);
  423. Assert(module_inst->hw);
  424. module_inst->hw->HOST.CTRLB.bit.L1RESUME = 1;
  425. }
  426. /**
  427. * \brief Get the speed mode of USB host
  428. *
  429. * \param module_inst Pointer to USB module instance struct
  430. *
  431. * \return USB speed mode (\ref usb_speed).
  432. */
  433. static inline enum usb_speed usb_host_get_speed(struct usb_module *module_inst)
  434. {
  435. /* Sanity check arguments */
  436. Assert(module_inst);
  437. Assert(module_inst->hw);
  438. if (module_inst->hw->HOST.STATUS.bit.SPEED == 0) {
  439. return USB_SPEED_FULL;
  440. } else {
  441. return USB_SPEED_LOW;
  442. }
  443. }
  444. /**
  445. * \brief Get the frame number
  446. *
  447. * \param module_inst Pointer to USB software instance struct
  448. *
  449. * \return frame number value.
  450. */
  451. static inline uint16_t usb_host_get_frame_number(struct usb_module *module_inst)
  452. {
  453. /* Sanity check arguments */
  454. Assert(module_inst);
  455. Assert(module_inst->hw);
  456. return (uint16_t)(module_inst->hw->HOST.FNUM.bit.FNUM);
  457. }
  458. #endif
  459. /**
  460. * \brief Attach USB device to the bus
  461. *
  462. * \param module_inst Pointer to USB device module instance
  463. */
  464. static inline void usb_device_attach(struct usb_module *module_inst)
  465. {
  466. module_inst->hw->DEVICE.CTRLB.reg &= ~USB_DEVICE_CTRLB_DETACH;
  467. }
  468. /**
  469. * \brief Detach USB device from the bus
  470. *
  471. * \param module_inst Pointer to USB device module instance
  472. */
  473. static inline void usb_device_detach(struct usb_module *module_inst)
  474. {
  475. module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_DETACH;
  476. }
  477. /**
  478. * \brief Get the speed mode of USB device
  479. *
  480. * \param module_inst Pointer to USB device module instance
  481. * \return USB Speed mode (\ref usb_speed).
  482. */
  483. static inline enum usb_speed usb_device_get_speed(struct usb_module *module_inst)
  484. {
  485. if (!(module_inst->hw->DEVICE.STATUS.reg & USB_DEVICE_STATUS_SPEED_Msk)) {
  486. return USB_SPEED_FULL;
  487. } else {
  488. return USB_SPEED_LOW;
  489. }
  490. }
  491. /**
  492. * \brief Get the address of USB device
  493. *
  494. * \param module_inst Pointer to USB device module instance
  495. * \return USB device address value.
  496. */
  497. static inline uint8_t usb_device_get_address(struct usb_module *module_inst)
  498. {
  499. return ((uint8_t)(module_inst->hw->DEVICE.DADD.bit.DADD));
  500. }
  501. /**
  502. * \brief Set the speed mode of USB device
  503. *
  504. * \param module_inst Pointer to USB device module instance
  505. * \param address USB device address value
  506. */
  507. static inline void usb_device_set_address(struct usb_module *module_inst, uint8_t address)
  508. {
  509. module_inst->hw->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | address;
  510. }
  511. /**
  512. * \brief Get the frame number of USB device
  513. *
  514. * \param module_inst Pointer to USB device module instance
  515. * \return USB device frame number value.
  516. */
  517. static inline uint16_t usb_device_get_frame_number(struct usb_module *module_inst)
  518. {
  519. return ((uint16_t)(module_inst->hw->DEVICE.FNUM.bit.FNUM));
  520. }
  521. /**
  522. * \brief Get the micro-frame number of USB device
  523. *
  524. * \param module_inst Pointer to USB device module instance
  525. * \return USB device micro-frame number value.
  526. */
  527. static inline uint16_t usb_device_get_micro_frame_number(struct usb_module *module_inst)
  528. {
  529. return ((uint16_t)(module_inst->hw->DEVICE.FNUM.reg));
  530. }
  531. /**
  532. * \brief USB device send the resume wakeup
  533. *
  534. * \param module_inst Pointer to USB device module instance
  535. */
  536. static inline void usb_device_send_remote_wake_up(struct usb_module *module_inst)
  537. {
  538. module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_UPRSM;
  539. }
  540. /**
  541. * \brief USB device set the LPM mode
  542. *
  543. * \param module_inst Pointer to USB device module instance
  544. * \param lpm_mode LPM mode
  545. */
  546. static inline void usb_device_set_lpm_mode(struct usb_module *module_inst,
  547. enum usb_device_lpm_mode lpm_mode)
  548. {
  549. module_inst->hw->DEVICE.CTRLB.bit.LPMHDSK = lpm_mode;
  550. }
  551. /**
  552. * \name USB Host Callback Management
  553. * @{
  554. */
  555. enum status_code usb_host_register_callback(struct usb_module *module_inst,
  556. enum usb_host_callback callback_type,
  557. usb_host_callback_t callback_func);
  558. enum status_code usb_host_unregister_callback(struct usb_module *module_inst,
  559. enum usb_host_callback callback_type);
  560. enum status_code usb_host_enable_callback(struct usb_module *module_inst,
  561. enum usb_host_callback callback_type);
  562. enum status_code usb_host_disable_callback(struct usb_module *module_inst,
  563. enum usb_host_callback callback_type);
  564. /** @} */
  565. /**
  566. * \name USB Device Callback Management
  567. * @{
  568. */
  569. enum status_code usb_device_register_callback(struct usb_module *module_inst,
  570. enum usb_device_callback callback_type,
  571. usb_device_callback_t callback_func);
  572. enum status_code usb_device_unregister_callback(struct usb_module *module_inst,
  573. enum usb_device_callback callback_type);
  574. enum status_code usb_device_enable_callback(struct usb_module *module_inst,
  575. enum usb_device_callback callback_type);
  576. enum status_code usb_device_disable_callback(struct usb_module *module_inst,
  577. enum usb_device_callback callback_type);
  578. /** @} */
  579. /**
  580. * \name USB Host Pipe Configuration
  581. * @{
  582. */
  583. void usb_host_pipe_get_config_defaults(struct usb_host_pipe_config *ep_config);
  584. enum status_code usb_host_pipe_set_config(struct usb_module *module_inst, uint8_t pipe_num,
  585. struct usb_host_pipe_config *ep_config);
  586. enum status_code usb_host_pipe_get_config(struct usb_module *module_inst, uint8_t pipe_num,
  587. struct usb_host_pipe_config *ep_config);
  588. /** @} */
  589. /**
  590. * \name USB Device Endpoint Configuration
  591. * @{
  592. */
  593. void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config);
  594. enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst,
  595. struct usb_device_endpoint_config *ep_config);
  596. bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep);
  597. /** @} */
  598. /**
  599. * \name USB Host Pipe Callback Management
  600. * @{
  601. */
  602. enum status_code usb_host_pipe_register_callback(
  603. struct usb_module *module_inst, uint8_t pipe_num,
  604. enum usb_host_pipe_callback callback_type,
  605. usb_host_pipe_callback_t callback_func);
  606. enum status_code usb_host_pipe_unregister_callback(
  607. struct usb_module *module_inst, uint8_t pipe_num,
  608. enum usb_host_pipe_callback callback_type);
  609. enum status_code usb_host_pipe_enable_callback(
  610. struct usb_module *module_inst, uint8_t pipe_num,
  611. enum usb_host_pipe_callback callback_type);
  612. enum status_code usb_host_pipe_disable_callback(
  613. struct usb_module *module_inst, uint8_t pipe_num,
  614. enum usb_host_pipe_callback callback_type);
  615. /** @} */
  616. /**
  617. * \name USB Device Endpoint Callback Management
  618. * @{
  619. */
  620. enum status_code usb_device_endpoint_register_callback(
  621. struct usb_module *module_inst, uint8_t ep_num,
  622. enum usb_device_endpoint_callback callback_type,
  623. usb_device_endpoint_callback_t callback_func);
  624. enum status_code usb_device_endpoint_unregister_callback(
  625. struct usb_module *module_inst, uint8_t ep_num,
  626. enum usb_device_endpoint_callback callback_type);
  627. enum status_code usb_device_endpoint_enable_callback(
  628. struct usb_module *module_inst, uint8_t ep,
  629. enum usb_device_endpoint_callback callback_type);
  630. enum status_code usb_device_endpoint_disable_callback(
  631. struct usb_module *module_inst, uint8_t ep,
  632. enum usb_device_endpoint_callback callback_type);
  633. /** @} */
  634. /**
  635. * \name USB Host Pipe Job Management
  636. * @{
  637. */
  638. enum status_code usb_host_pipe_setup_job(struct usb_module *module_inst,
  639. uint8_t pipe_num, uint8_t *buf);
  640. enum status_code usb_host_pipe_read_job(struct usb_module *module_inst,
  641. uint8_t pipe_num, uint8_t *buf, uint32_t buf_size);
  642. enum status_code usb_host_pipe_write_job(struct usb_module *module_inst,
  643. uint8_t pipe_num, uint8_t *buf, uint32_t buf_size);
  644. enum status_code usb_host_pipe_abort_job(struct usb_module *module_inst, uint8_t pipe_num);
  645. enum status_code usb_host_pipe_lpm_job(struct usb_module *module_inst,
  646. uint8_t pipe_num, bool b_remotewakeup, uint8_t hird);
  647. /** @} */
  648. /**
  649. * \name USB Device Endpoint Job Management
  650. * @{
  651. */
  652. enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst,uint8_t ep_num,
  653. uint8_t* pbuf, uint32_t buf_size);
  654. enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst,uint8_t ep_num,
  655. uint8_t* pbuf, uint32_t buf_size);
  656. enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst,
  657. uint8_t* pbuf);
  658. void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep);
  659. /** @} */
  660. #if !SAMD11 && !SAML22
  661. /**
  662. * \name USB Host Pipe Operations
  663. * @{
  664. */
  665. /**
  666. * \brief Freeze a pipe
  667. *
  668. * \param module_inst Pointer to USB module instance
  669. * \param pipe_num Pipe number
  670. */
  671. static inline void usb_host_pipe_freeze(struct usb_module *module_inst, uint8_t pipe_num)
  672. {
  673. /* Sanity check arguments */
  674. Assert(module_inst);
  675. Assert(module_inst->hw);
  676. module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSSET.reg = USB_HOST_PSTATUSSET_PFREEZE;
  677. }
  678. /**
  679. * \brief Unfreeze a pipe
  680. *
  681. * \param module_inst Pointer to USB module instance
  682. * \param pipe_num Pipe number
  683. */
  684. static inline void usb_host_pipe_unfreeze(struct usb_module *module_inst, uint8_t pipe_num)
  685. {
  686. /* Sanity check arguments */
  687. Assert(module_inst);
  688. Assert(module_inst->hw);
  689. module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_PFREEZE;
  690. }
  691. /**
  692. * \brief Check if the pipe is frozen
  693. *
  694. * \param module_inst Pointer to USB module instance
  695. * \param pipe_num Pipe number
  696. */
  697. static inline bool usb_host_pipe_is_frozen(struct usb_module *module_inst, uint8_t pipe_num)
  698. {
  699. /* Sanity check arguments */
  700. Assert(module_inst);
  701. Assert(module_inst->hw);
  702. return (module_inst->hw->HOST.HostPipe[pipe_num].PSTATUS.bit.PFREEZE == 1);
  703. }
  704. /**
  705. * \brief Set the data toggle bit of pipe
  706. *
  707. * \param module_inst Pointer to USB module instance
  708. * \param pipe_num Pipe number
  709. */
  710. static inline void usb_host_pipe_set_toggle(struct usb_module *module_inst, uint8_t pipe_num)
  711. {
  712. /* Sanity check arguments */
  713. Assert(module_inst);
  714. Assert(module_inst->hw);
  715. module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSSET.reg = USB_HOST_PSTATUSSET_DTGL;
  716. }
  717. /**
  718. * \brief Clear the data toggle bit of pipe
  719. *
  720. * \param module_inst Pointer to USB module instance
  721. * \param pipe_num Pipe number
  722. */
  723. static inline void usb_host_pipe_clear_toggle(struct usb_module *module_inst, uint8_t pipe_num)
  724. {
  725. /* Sanity check arguments */
  726. Assert(module_inst);
  727. Assert(module_inst->hw);
  728. module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_DTGL;
  729. }
  730. /**
  731. * \brief Set the auto zero length packet of pipe
  732. *
  733. * \param module_inst Pointer to USB module instance
  734. * \param pipe_num Pipe number
  735. * \param value \c true to enable auto ZLP and \c false to disable
  736. */
  737. void usb_host_pipe_set_auto_zlp(struct usb_module *module_inst, uint8_t pipe_num, bool value);
  738. /** @} */
  739. #endif
  740. /**
  741. * \name USB Device Endpoint Operations
  742. * @{
  743. */
  744. bool usb_device_endpoint_is_halted(struct usb_module *module_inst, uint8_t ep);
  745. void usb_device_endpoint_set_halt(struct usb_module *module_inst, uint8_t ep);
  746. void usb_device_endpoint_clear_halt(struct usb_module *module_inst, uint8_t ep);
  747. /** @} */
  748. #ifdef __cplusplus
  749. }
  750. #endif
  751. #endif /* USB_H_INCLUDED */