1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef ZEPHYR_INCLUDE_USBD_ENDPOINT_H
8 #define ZEPHYR_INCLUDE_USBD_ENDPOINT_H
9
10 #include <zephyr/usb/usbd.h>
11
12 /**
13 * @brief Set bit associated with the endpoint
14 *
15 * The IN endpoints are mapped in the upper nibble.
16 *
17 * @param[in] ep_bm Pointer to endpoint bitmap
18 * @param[in] ep Endpoint address
19 */
usbd_ep_bm_set(uint32_t * const ep_bm,const uint8_t ep)20 static inline void usbd_ep_bm_set(uint32_t *const ep_bm, const uint8_t ep)
21 {
22 if (USB_EP_DIR_IS_IN(ep)) {
23 *ep_bm |= BIT(USB_EP_GET_IDX(ep) + 16U);
24 } else {
25 *ep_bm |= BIT(USB_EP_GET_IDX(ep));
26 }
27 }
28
29 /**
30 * @brief Clear bit associated with the endpoint
31 *
32 * The IN endpoints are mapped in the upper nibble.
33 *
34 * @param[in] ep_bm Pointer to endpoint bitmap
35 * @param[in] ep Endpoint address
36 */
usbd_ep_bm_clear(uint32_t * const ep_bm,const uint8_t ep)37 static inline void usbd_ep_bm_clear(uint32_t *const ep_bm, const uint8_t ep)
38 {
39 if (USB_EP_DIR_IS_IN(ep)) {
40 *ep_bm &= ~BIT(USB_EP_GET_IDX(ep) + 16U);
41 } else {
42 *ep_bm &= ~BIT(USB_EP_GET_IDX(ep));
43 }
44 }
45
46 /**
47 * @brief Check whether bit associated with the endpoint is set
48 *
49 * The IN endpoints are mapped in the upper nibble.
50 *
51 * @param[in] ep_bm Pointer to endpoint bitmap
52 * @param[in] ep Endpoint address
53 *
54 * @return true if bit is set, false otherwise
55 */
usbd_ep_bm_is_set(const uint32_t * const ep_bm,const uint8_t ep)56 static inline bool usbd_ep_bm_is_set(const uint32_t *const ep_bm, const uint8_t ep)
57 {
58 unsigned int bit;
59
60 if (USB_EP_DIR_IS_IN(ep)) {
61 bit = USB_EP_GET_IDX(ep) + 16U;
62 } else {
63 bit = USB_EP_GET_IDX(ep);
64 }
65
66 return (*ep_bm & BIT(bit)) ? true : false;
67 }
68
69 /**
70 * @brief Enable endpoint
71 *
72 * This function enables endpoint and sets corresponding bit.
73 *
74 * @param[in] dev Pointer to UDC device
75 * @param[in] ed Pointer to endpoint descriptor
76 * @param[in] ep_bm Pointer to endpoint bitmap
77 *
78 * @return 0 on success, other values on fail.
79 */
80 int usbd_ep_enable(const struct device *dev,
81 const struct usb_ep_descriptor *const ed,
82 uint32_t *const ep_bm);
83
84 /**
85 * @brief Disable endpoint
86 *
87 * This function disables endpoint and clears corresponding bit.
88 *
89 * @param[in] dev Pointer to UDC device
90 * @param[in] ep Endpoint address
91 * @param[in] ep_bm Pointer to endpoint bitmap
92 *
93 * @return 0 on success, other values on fail.
94 */
95 int usbd_ep_disable(const struct device *dev,
96 const uint8_t ep,
97 uint32_t *const ep_bm);
98
99 #endif /* ZEPHYR_INCLUDE_USBD_ENDPOINT_H */
100