hal_usb.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_usb.h
  15. * @brief USB Driver macros and structures.
  16. *
  17. * @addtogroup USB
  18. * @{
  19. */
  20. #ifndef HAL_USB_H
  21. #define HAL_USB_H
  22. #if (HAL_USE_USB == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. #define USB_ENDPOINT_OUT(ep) (ep)
  27. #define USB_ENDPOINT_IN(ep) ((ep) | 0x80U)
  28. #define USB_RTYPE_DIR_MASK 0x80U
  29. #define USB_RTYPE_DIR_HOST2DEV 0x00U
  30. #define USB_RTYPE_DIR_DEV2HOST 0x80U
  31. #define USB_RTYPE_TYPE_MASK 0x60U
  32. #define USB_RTYPE_TYPE_STD 0x00U
  33. #define USB_RTYPE_TYPE_CLASS 0x20U
  34. #define USB_RTYPE_TYPE_VENDOR 0x40U
  35. #define USB_RTYPE_TYPE_RESERVED 0x60U
  36. #define USB_RTYPE_RECIPIENT_MASK 0x1FU
  37. #define USB_RTYPE_RECIPIENT_DEVICE 0x00U
  38. #define USB_RTYPE_RECIPIENT_INTERFACE 0x01U
  39. #define USB_RTYPE_RECIPIENT_ENDPOINT 0x02U
  40. #define USB_RTYPE_RECIPIENT_OTHER 0x03U
  41. #define USB_REQ_GET_STATUS 0U
  42. #define USB_REQ_CLEAR_FEATURE 1U
  43. #define USB_REQ_SET_FEATURE 3U
  44. #define USB_REQ_SET_ADDRESS 5U
  45. #define USB_REQ_GET_DESCRIPTOR 6U
  46. #define USB_REQ_SET_DESCRIPTOR 7U
  47. #define USB_REQ_GET_CONFIGURATION 8U
  48. #define USB_REQ_SET_CONFIGURATION 9U
  49. #define USB_REQ_GET_INTERFACE 10U
  50. #define USB_REQ_SET_INTERFACE 11U
  51. #define USB_REQ_SYNCH_FRAME 12U
  52. #define USB_DESCRIPTOR_DEVICE 1U
  53. #define USB_DESCRIPTOR_CONFIGURATION 2U
  54. #define USB_DESCRIPTOR_STRING 3U
  55. #define USB_DESCRIPTOR_INTERFACE 4U
  56. #define USB_DESCRIPTOR_ENDPOINT 5U
  57. #define USB_DESCRIPTOR_DEVICE_QUALIFIER 6U
  58. #define USB_DESCRIPTOR_OTHER_SPEED_CFG 7U
  59. #define USB_DESCRIPTOR_INTERFACE_POWER 8U
  60. #define USB_DESCRIPTOR_INTERFACE_ASSOCIATION 11U
  61. #define USB_FEATURE_ENDPOINT_HALT 0U
  62. #define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1U
  63. #define USB_FEATURE_TEST_MODE 2U
  64. #define USB_EARLY_SET_ADDRESS 0
  65. #define USB_LATE_SET_ADDRESS 1
  66. #define USB_EP0_STATUS_STAGE_SW 0
  67. #define USB_EP0_STATUS_STAGE_HW 1
  68. #define USB_SET_ADDRESS_ACK_SW 0
  69. #define USB_SET_ADDRESS_ACK_HW 1
  70. /**
  71. * @name Helper macros for USB descriptors
  72. * @{
  73. */
  74. /**
  75. * @brief Helper macro for index values into descriptor strings.
  76. */
  77. #define USB_DESC_INDEX(i) ((uint8_t)(i))
  78. /**
  79. * @brief Helper macro for byte values into descriptor strings.
  80. */
  81. #define USB_DESC_BYTE(b) ((uint8_t)(b))
  82. /**
  83. * @brief Helper macro for word values into descriptor strings.
  84. */
  85. #define USB_DESC_WORD(w) \
  86. (uint8_t)((w) & 255U), \
  87. (uint8_t)(((w) >> 8) & 255U)
  88. /**
  89. * @brief Helper macro for BCD values into descriptor strings.
  90. */
  91. #define USB_DESC_BCD(bcd) \
  92. (uint8_t)((bcd) & 255U), \
  93. (uint8_t)(((bcd) >> 8) & 255)
  94. /*
  95. * @define Device Descriptor size.
  96. */
  97. #define USB_DESC_DEVICE_SIZE 18U
  98. /**
  99. * @brief Device Descriptor helper macro.
  100. */
  101. #define USB_DESC_DEVICE(bcdUSB, bDeviceClass, bDeviceSubClass, \
  102. bDeviceProtocol, bMaxPacketSize, idVendor, \
  103. idProduct, bcdDevice, iManufacturer, \
  104. iProduct, iSerialNumber, bNumConfigurations) \
  105. USB_DESC_BYTE(USB_DESC_DEVICE_SIZE), \
  106. USB_DESC_BYTE(USB_DESCRIPTOR_DEVICE), \
  107. USB_DESC_BCD(bcdUSB), \
  108. USB_DESC_BYTE(bDeviceClass), \
  109. USB_DESC_BYTE(bDeviceSubClass), \
  110. USB_DESC_BYTE(bDeviceProtocol), \
  111. USB_DESC_BYTE(bMaxPacketSize), \
  112. USB_DESC_WORD(idVendor), \
  113. USB_DESC_WORD(idProduct), \
  114. USB_DESC_BCD(bcdDevice), \
  115. USB_DESC_INDEX(iManufacturer), \
  116. USB_DESC_INDEX(iProduct), \
  117. USB_DESC_INDEX(iSerialNumber), \
  118. USB_DESC_BYTE(bNumConfigurations)
  119. /**
  120. * @brief Configuration Descriptor size.
  121. */
  122. #define USB_DESC_CONFIGURATION_SIZE 9U
  123. /**
  124. * @brief Configuration Descriptor helper macro.
  125. */
  126. #define USB_DESC_CONFIGURATION(wTotalLength, bNumInterfaces, \
  127. bConfigurationValue, iConfiguration, \
  128. bmAttributes, bMaxPower) \
  129. USB_DESC_BYTE(USB_DESC_CONFIGURATION_SIZE), \
  130. USB_DESC_BYTE(USB_DESCRIPTOR_CONFIGURATION), \
  131. USB_DESC_WORD(wTotalLength), \
  132. USB_DESC_BYTE(bNumInterfaces), \
  133. USB_DESC_BYTE(bConfigurationValue), \
  134. USB_DESC_INDEX(iConfiguration), \
  135. USB_DESC_BYTE(bmAttributes), \
  136. USB_DESC_BYTE(bMaxPower)
  137. /**
  138. * @brief Interface Descriptor size.
  139. */
  140. #define USB_DESC_INTERFACE_SIZE 9U
  141. /**
  142. * @brief Interface Descriptor helper macro.
  143. */
  144. #define USB_DESC_INTERFACE(bInterfaceNumber, bAlternateSetting, \
  145. bNumEndpoints, bInterfaceClass, \
  146. bInterfaceSubClass, bInterfaceProtocol, \
  147. iInterface) \
  148. USB_DESC_BYTE(USB_DESC_INTERFACE_SIZE), \
  149. USB_DESC_BYTE(USB_DESCRIPTOR_INTERFACE), \
  150. USB_DESC_BYTE(bInterfaceNumber), \
  151. USB_DESC_BYTE(bAlternateSetting), \
  152. USB_DESC_BYTE(bNumEndpoints), \
  153. USB_DESC_BYTE(bInterfaceClass), \
  154. USB_DESC_BYTE(bInterfaceSubClass), \
  155. USB_DESC_BYTE(bInterfaceProtocol), \
  156. USB_DESC_INDEX(iInterface)
  157. /**
  158. * @brief Interface Association Descriptor size.
  159. */
  160. #define USB_DESC_INTERFACE_ASSOCIATION_SIZE 8U
  161. /**
  162. * @brief Interface Association Descriptor helper macro.
  163. */
  164. #define USB_DESC_INTERFACE_ASSOCIATION(bFirstInterface, \
  165. bInterfaceCount, bFunctionClass, \
  166. bFunctionSubClass, bFunctionProcotol, \
  167. iInterface) \
  168. USB_DESC_BYTE(USB_DESC_INTERFACE_ASSOCIATION_SIZE), \
  169. USB_DESC_BYTE(USB_DESCRIPTOR_INTERFACE_ASSOCIATION), \
  170. USB_DESC_BYTE(bFirstInterface), \
  171. USB_DESC_BYTE(bInterfaceCount), \
  172. USB_DESC_BYTE(bFunctionClass), \
  173. USB_DESC_BYTE(bFunctionSubClass), \
  174. USB_DESC_BYTE(bFunctionProcotol), \
  175. USB_DESC_INDEX(iInterface)
  176. /**
  177. * @brief Endpoint Descriptor size.
  178. */
  179. #define USB_DESC_ENDPOINT_SIZE 7U
  180. /**
  181. * @brief Endpoint Descriptor helper macro.
  182. */
  183. #define USB_DESC_ENDPOINT(bEndpointAddress, bmAttributes, wMaxPacketSize, \
  184. bInterval) \
  185. USB_DESC_BYTE(USB_DESC_ENDPOINT_SIZE), \
  186. USB_DESC_BYTE(USB_DESCRIPTOR_ENDPOINT), \
  187. USB_DESC_BYTE(bEndpointAddress), \
  188. USB_DESC_BYTE(bmAttributes), \
  189. USB_DESC_WORD(wMaxPacketSize), \
  190. USB_DESC_BYTE(bInterval)
  191. /** @} */
  192. /**
  193. * @name Endpoint types and settings
  194. * @{
  195. */
  196. #define USB_EP_MODE_TYPE 0x0003U /**< Endpoint type mask. */
  197. #define USB_EP_MODE_TYPE_CTRL 0x0000U /**< Control endpoint. */
  198. #define USB_EP_MODE_TYPE_ISOC 0x0001U /**< Isochronous endpoint. */
  199. #define USB_EP_MODE_TYPE_BULK 0x0002U /**< Bulk endpoint. */
  200. #define USB_EP_MODE_TYPE_INTR 0x0003U /**< Interrupt endpoint. */
  201. /** @} */
  202. #define USB_IN_STATE 0x08U
  203. #define USB_OUT_STATE 0x10U
  204. /*===========================================================================*/
  205. /* Driver pre-compile time settings. */
  206. /*===========================================================================*/
  207. /**
  208. * @brief Enables synchronous APIs.
  209. * @note Disabling this option saves both code and data space.
  210. */
  211. #if !defined(USB_USE_WAIT) || defined(__DOXYGEN__)
  212. #define USB_USE_WAIT FALSE
  213. #endif
  214. /*===========================================================================*/
  215. /* Derived constants and error checks. */
  216. /*===========================================================================*/
  217. /*===========================================================================*/
  218. /* Driver data structures and types. */
  219. /*===========================================================================*/
  220. /**
  221. * @brief Type of a structure representing an USB driver.
  222. */
  223. typedef struct USBDriver USBDriver;
  224. /**
  225. * @brief Type of an endpoint identifier.
  226. */
  227. typedef uint8_t usbep_t;
  228. /**
  229. * @brief Type of a driver state machine possible states.
  230. */
  231. typedef enum {
  232. USB_UNINIT = 0, /**< Not initialized. */
  233. USB_STOP = 1, /**< Stopped. */
  234. USB_READY = 2, /**< Ready, after bus reset. */
  235. USB_SELECTED = 3, /**< Address assigned. */
  236. USB_ACTIVE = 4, /**< Active, configuration selected.*/
  237. USB_SUSPENDED = 5 /**< Suspended, low power mode. */
  238. } usbstate_t;
  239. /**
  240. * @brief Type of an endpoint status.
  241. */
  242. typedef enum {
  243. EP_STATUS_DISABLED = 0, /**< Endpoint not active. */
  244. EP_STATUS_STALLED = 1, /**< Endpoint opened but stalled. */
  245. EP_STATUS_ACTIVE = 2 /**< Active endpoint. */
  246. } usbepstatus_t;
  247. /**
  248. * @brief Type of an endpoint zero state machine states.
  249. */
  250. typedef enum {
  251. USB_EP0_STP_WAITING = 0U, /**< Waiting for SETUP data.*/
  252. USB_EP0_IN_TX = USB_IN_STATE | 1U, /**< Transmitting. */
  253. USB_EP0_IN_WAITING_TX0 = USB_IN_STATE | 2U, /**< Waiting transmit 0. */
  254. USB_EP0_IN_SENDING_STS = USB_IN_STATE | 3U, /**< Sending status. */
  255. USB_EP0_OUT_WAITING_STS = USB_OUT_STATE | 4U, /**< Waiting status. */
  256. USB_EP0_OUT_RX = USB_OUT_STATE | 5U, /**< Receiving. */
  257. USB_EP0_ERROR = 6U /**< Error, EP0 stalled. */
  258. } usbep0state_t;
  259. /**
  260. * @brief Type of an enumeration of the possible USB events.
  261. */
  262. typedef enum {
  263. USB_EVENT_RESET = 0, /**< Driver has been reset by host. */
  264. USB_EVENT_ADDRESS = 1, /**< Address assigned. */
  265. USB_EVENT_CONFIGURED = 2, /**< Configuration selected. */
  266. USB_EVENT_UNCONFIGURED = 3, /**< Configuration removed. */
  267. USB_EVENT_SUSPEND = 4, /**< Entering suspend mode. */
  268. USB_EVENT_WAKEUP = 5, /**< Leaving suspend mode. */
  269. USB_EVENT_STALLED = 6 /**< Endpoint 0 error, stalled. */
  270. } usbevent_t;
  271. /**
  272. * @brief Type of an USB descriptor.
  273. */
  274. typedef struct {
  275. /**
  276. * @brief Descriptor size in unicode characters.
  277. */
  278. size_t ud_size;
  279. /**
  280. * @brief Pointer to the descriptor.
  281. */
  282. const uint8_t *ud_string;
  283. } USBDescriptor;
  284. /**
  285. * @brief Type of an USB generic notification callback.
  286. *
  287. * @param[in] usbp pointer to the @p USBDriver object triggering the
  288. * callback
  289. */
  290. typedef void (*usbcallback_t)(USBDriver *usbp);
  291. /**
  292. * @brief Type of an USB endpoint callback.
  293. *
  294. * @param[in] usbp pointer to the @p USBDriver object triggering the
  295. * callback
  296. * @param[in] ep endpoint number
  297. */
  298. typedef void (*usbepcallback_t)(USBDriver *usbp, usbep_t ep);
  299. /**
  300. * @brief Type of an USB event notification callback.
  301. *
  302. * @param[in] usbp pointer to the @p USBDriver object triggering the
  303. * callback
  304. * @param[in] event event type
  305. */
  306. typedef void (*usbeventcb_t)(USBDriver *usbp, usbevent_t event);
  307. /**
  308. * @brief Type of a requests handler callback.
  309. * @details The request is encoded in the @p usb_setup buffer.
  310. *
  311. * @param[in] usbp pointer to the @p USBDriver object triggering the
  312. * callback
  313. * @return The request handling exit code.
  314. * @retval false Request not recognized by the handler.
  315. * @retval true Request handled.
  316. */
  317. typedef bool (*usbreqhandler_t)(USBDriver *usbp);
  318. /**
  319. * @brief Type of an USB descriptor-retrieving callback.
  320. */
  321. typedef const USBDescriptor * (*usbgetdescriptor_t)(USBDriver *usbp,
  322. uint8_t dtype,
  323. uint8_t dindex,
  324. uint16_t lang);
  325. #include "hal_usb_lld.h"
  326. /*===========================================================================*/
  327. /* Driver macros. */
  328. /*===========================================================================*/
  329. /**
  330. * @name Macro Functions
  331. * @{
  332. */
  333. /**
  334. * @brief Returns the driver state.
  335. *
  336. * @param[in] usbp pointer to the @p USBDriver object
  337. * @return The driver state.
  338. *
  339. * @iclass
  340. */
  341. #define usbGetDriverStateI(usbp) ((usbp)->state)
  342. /**
  343. * @brief Connects the USB device.
  344. *
  345. * @param[in] usbp pointer to the @p USBDriver object
  346. *
  347. * @api
  348. */
  349. #define usbConnectBus(usbp) usb_lld_connect_bus(usbp)
  350. /**
  351. * @brief Disconnect the USB device.
  352. *
  353. * @param[in] usbp pointer to the @p USBDriver object
  354. *
  355. * @api
  356. */
  357. #define usbDisconnectBus(usbp) usb_lld_disconnect_bus(usbp)
  358. /**
  359. * @brief Returns the current frame number.
  360. *
  361. * @param[in] usbp pointer to the @p USBDriver object
  362. * @return The current frame number.
  363. *
  364. * @xclass
  365. */
  366. #define usbGetFrameNumberX(usbp) usb_lld_get_frame_number(usbp)
  367. /**
  368. * @brief Returns the status of an IN endpoint.
  369. *
  370. * @param[in] usbp pointer to the @p USBDriver object
  371. * @param[in] ep endpoint number
  372. * @return The operation status.
  373. * @retval false Endpoint ready.
  374. * @retval true Endpoint transmitting.
  375. *
  376. * @iclass
  377. */
  378. #define usbGetTransmitStatusI(usbp, ep) \
  379. (((usbp)->transmitting & (uint16_t)((unsigned)1U << (unsigned)(ep))) != 0U)
  380. /**
  381. * @brief Returns the status of an OUT endpoint.
  382. *
  383. * @param[in] usbp pointer to the @p USBDriver object
  384. * @param[in] ep endpoint number
  385. * @return The operation status.
  386. * @retval false Endpoint ready.
  387. * @retval true Endpoint receiving.
  388. *
  389. * @iclass
  390. */
  391. #define usbGetReceiveStatusI(usbp, ep) \
  392. (((usbp)->receiving & (uint16_t)((unsigned)1U << (unsigned)(ep))) != 0U)
  393. /**
  394. * @brief Returns the exact size of a receive transaction.
  395. * @details The received size can be different from the size specified in
  396. * @p usbStartReceiveI() because the last packet could have a size
  397. * different from the expected one.
  398. *
  399. * @param[in] usbp pointer to the @p USBDriver object
  400. * @param[in] ep endpoint number
  401. * @return Received data size.
  402. *
  403. * @xclass
  404. */
  405. #define usbGetReceiveTransactionSizeX(usbp, ep) \
  406. usb_lld_get_transaction_size(usbp, ep)
  407. /**
  408. * @brief Request transfer setup.
  409. * @details This macro is used by the request handling callbacks in order to
  410. * prepare a transaction over the endpoint zero.
  411. *
  412. * @param[in] usbp pointer to the @p USBDriver object
  413. * @param[in] buf pointer to a buffer for the transaction data
  414. * @param[in] n number of bytes to be transferred
  415. * @param[in] endcb callback to be invoked after the transfer or @p NULL
  416. *
  417. * @special
  418. */
  419. #define usbSetupTransfer(usbp, buf, n, endcb) { \
  420. (usbp)->ep0next = (buf); \
  421. (usbp)->ep0n = (n); \
  422. (usbp)->ep0endcb = (endcb); \
  423. }
  424. /**
  425. * @brief Reads a setup packet from the dedicated packet buffer.
  426. * @details This function must be invoked in the context of the @p setup_cb
  427. * callback in order to read the received setup packet.
  428. * @pre In order to use this function the endpoint must have been
  429. * initialized as a control endpoint.
  430. * @note This function can be invoked both in thread and IRQ context.
  431. *
  432. * @param[in] usbp pointer to the @p USBDriver object
  433. * @param[in] ep endpoint number
  434. * @param[out] buf buffer where to copy the packet data
  435. *
  436. * @special
  437. */
  438. #define usbReadSetup(usbp, ep, buf) usb_lld_read_setup(usbp, ep, buf)
  439. /** @} */
  440. /**
  441. * @name Low level driver helper macros
  442. * @{
  443. */
  444. /**
  445. * @brief Common ISR code, usb event callback.
  446. *
  447. * @param[in] usbp pointer to the @p USBDriver object
  448. * @param[in] evt USB event code
  449. *
  450. * @notapi
  451. */
  452. #define _usb_isr_invoke_event_cb(usbp, evt) { \
  453. if (((usbp)->config->event_cb) != NULL) { \
  454. (usbp)->config->event_cb(usbp, evt); \
  455. } \
  456. }
  457. /**
  458. * @brief Common ISR code, SOF callback.
  459. *
  460. * @param[in] usbp pointer to the @p USBDriver object
  461. *
  462. * @notapi
  463. */
  464. #define _usb_isr_invoke_sof_cb(usbp) { \
  465. if (((usbp)->config->sof_cb) != NULL) { \
  466. (usbp)->config->sof_cb(usbp); \
  467. } \
  468. }
  469. /**
  470. * @brief Common ISR code, setup packet callback.
  471. *
  472. * @param[in] usbp pointer to the @p USBDriver object
  473. * @param[in] ep endpoint number
  474. *
  475. * @notapi
  476. */
  477. #define _usb_isr_invoke_setup_cb(usbp, ep) { \
  478. (usbp)->epc[ep]->setup_cb(usbp, ep); \
  479. }
  480. /**
  481. * @brief Common ISR code, IN endpoint callback.
  482. *
  483. * @param[in] usbp pointer to the @p USBDriver object
  484. * @param[in] ep endpoint number
  485. *
  486. * @notapi
  487. */
  488. #if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  489. #define _usb_isr_invoke_in_cb(usbp, ep) { \
  490. (usbp)->transmitting &= ~(1 << (ep)); \
  491. if ((usbp)->epc[ep]->in_cb != NULL) { \
  492. (usbp)->epc[ep]->in_cb(usbp, ep); \
  493. } \
  494. osalSysLockFromISR(); \
  495. osalThreadResumeI(&(usbp)->epc[ep]->in_state->thread, MSG_OK); \
  496. osalSysUnlockFromISR(); \
  497. }
  498. #else
  499. #define _usb_isr_invoke_in_cb(usbp, ep) { \
  500. (usbp)->transmitting &= ~(1 << (ep)); \
  501. if ((usbp)->epc[ep]->in_cb != NULL) { \
  502. (usbp)->epc[ep]->in_cb(usbp, ep); \
  503. } \
  504. }
  505. #endif
  506. /**
  507. * @brief Common ISR code, OUT endpoint event.
  508. *
  509. * @param[in] usbp pointer to the @p USBDriver object
  510. * @param[in] ep endpoint number
  511. *
  512. * @notapi
  513. */
  514. #if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  515. #define _usb_isr_invoke_out_cb(usbp, ep) { \
  516. (usbp)->receiving &= ~(1 << (ep)); \
  517. if ((usbp)->epc[ep]->out_cb != NULL) { \
  518. (usbp)->epc[ep]->out_cb(usbp, ep); \
  519. } \
  520. osalSysLockFromISR(); \
  521. osalThreadResumeI(&(usbp)->epc[ep]->out_state->thread, \
  522. usbGetReceiveTransactionSizeX(usbp, ep)); \
  523. osalSysUnlockFromISR(); \
  524. }
  525. #else
  526. #define _usb_isr_invoke_out_cb(usbp, ep) { \
  527. (usbp)->receiving &= ~(1 << (ep)); \
  528. if ((usbp)->epc[ep]->out_cb != NULL) { \
  529. (usbp)->epc[ep]->out_cb(usbp, ep); \
  530. } \
  531. }
  532. #endif
  533. /** @} */
  534. /*===========================================================================*/
  535. /* External declarations. */
  536. /*===========================================================================*/
  537. #ifdef __cplusplus
  538. extern "C" {
  539. #endif
  540. void usbInit(void);
  541. void usbObjectInit(USBDriver *usbp);
  542. void usbStart(USBDriver *usbp, const USBConfig *config);
  543. void usbStop(USBDriver *usbp);
  544. void usbInitEndpointI(USBDriver *usbp, usbep_t ep,
  545. const USBEndpointConfig *epcp);
  546. void usbDisableEndpointsI(USBDriver *usbp);
  547. void usbReadSetupI(USBDriver *usbp, usbep_t ep, uint8_t *buf);
  548. void usbStartReceiveI(USBDriver *usbp, usbep_t ep,
  549. uint8_t *buf, size_t n);
  550. void usbStartTransmitI(USBDriver *usbp, usbep_t ep,
  551. const uint8_t *buf, size_t n);
  552. #if USB_USE_WAIT == TRUE
  553. msg_t usbReceive(USBDriver *usbp, usbep_t ep, uint8_t *buf, size_t n);
  554. msg_t usbTransmit(USBDriver *usbp, usbep_t ep, const uint8_t *buf, size_t n);
  555. #endif
  556. bool usbStallReceiveI(USBDriver *usbp, usbep_t ep);
  557. bool usbStallTransmitI(USBDriver *usbp, usbep_t ep);
  558. void usbWakeupHost(USBDriver *usbp);
  559. void _usb_reset(USBDriver *usbp);
  560. void _usb_suspend(USBDriver *usbp);
  561. void _usb_wakeup(USBDriver *usbp);
  562. void _usb_ep0setup(USBDriver *usbp, usbep_t ep);
  563. void _usb_ep0in(USBDriver *usbp, usbep_t ep);
  564. void _usb_ep0out(USBDriver *usbp, usbep_t ep);
  565. #ifdef __cplusplus
  566. }
  567. #endif
  568. #endif /* HAL_USE_USB == TRUE */
  569. #endif /* HAL_USB_H */
  570. /** @} */