1 /* 2 * Copyright (c) 2022, sakumisu 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #ifndef USB_DC_H 7 #define USB_DC_H 8 9 #include <stdint.h> 10 11 #ifdef __cplusplus 12 extern "C" { 13 #endif 14 15 /** 16 * @brief USB Endpoint Configuration. 17 * 18 * Structure containing the USB endpoint configuration. 19 */ 20 struct usbd_endpoint_cfg { 21 uint8_t ep_addr; /* Endpoint addr with direction */ 22 uint8_t ep_type; /* Endpoint type */ 23 uint16_t ep_mps; /* Endpoint max packet size */ 24 uint8_t ep_mult; /* Endpoint additional transcations in micro frame */ 25 }; 26 27 /** 28 * @brief init device controller registers. 29 * @return On success will return 0, and others indicate fail. 30 */ 31 int usb_dc_init(void); 32 33 /** 34 * @brief deinit device controller registers. 35 * @return On success will return 0, and others indicate fail. 36 */ 37 int usb_dc_deinit(void); 38 39 /** 40 * @brief Set USB device address 41 * 42 * @param[in] addr Device address 43 * 44 * @return On success will return 0, and others indicate fail. 45 */ 46 int usbd_set_address(const uint8_t addr); 47 48 /** 49 * @brief Get USB device speed 50 * 51 * @param[in] port port index 52 * 53 * @return port speed, USB_SPEED_LOW or USB_SPEED_FULL or USB_SPEED_HIGH 54 */ 55 uint8_t usbd_get_port_speed(const uint8_t port); 56 57 /** 58 * @brief configure and enable endpoint. 59 * 60 * @param [in] ep_cfg Endpoint config. 61 * 62 * @return On success will return 0, and others indicate fail. 63 */ 64 int usbd_ep_open(const struct usbd_endpoint_cfg *ep_cfg); 65 66 /** 67 * @brief Disable the selected endpoint 68 * 69 * @param[in] ep Endpoint address 70 * 71 * @return On success will return 0, and others indicate fail. 72 */ 73 int usbd_ep_close(const uint8_t ep); 74 75 /** 76 * @brief Set stall condition for the selected endpoint 77 * 78 * @param[in] ep Endpoint address 79 * 80 * 81 * @return On success will return 0, and others indicate fail. 82 */ 83 int usbd_ep_set_stall(const uint8_t ep); 84 85 /** 86 * @brief Clear stall condition for the selected endpoint 87 * 88 * @param[in] ep Endpoint address corresponding to the one 89 * listed in the device configuration table 90 * 91 * @return On success will return 0, and others indicate fail. 92 */ 93 int usbd_ep_clear_stall(const uint8_t ep); 94 95 /** 96 * @brief Check if the selected endpoint is stalled 97 * 98 * @param[in] ep Endpoint address 99 * 100 * @param[out] stalled Endpoint stall status 101 * 102 * @return On success will return 0, and others indicate fail. 103 */ 104 int usbd_ep_is_stalled(const uint8_t ep, uint8_t *stalled); 105 106 /** 107 * @brief Setup in ep transfer setting and start transfer. 108 * 109 * This function is asynchronous. 110 * This function is similar to uart with tx dma. 111 * 112 * This function is called to write data to the specified endpoint. The 113 * supplied usbd_endpoint_callback function will be called when data is transmitted 114 * out. 115 * 116 * @param[in] ep Endpoint address corresponding to the one 117 * listed in the device configuration table 118 * @param[in] data Pointer to data to write 119 * @param[in] data_len Length of the data requested to write. This may 120 * be zero for a zero length status packet. 121 * @return 0 on success, negative errno code on fail. 122 */ 123 int usbd_ep_start_write(const uint8_t ep, const uint8_t *data, uint32_t data_len); 124 125 /** 126 * @brief Setup out ep transfer setting and start transfer. 127 * 128 * This function is asynchronous. 129 * This function is similar to uart with rx dma. 130 * 131 * This function is called to read data to the specified endpoint. The 132 * supplied usbd_endpoint_callback function will be called when data is received 133 * in. 134 * 135 * @param[in] ep Endpoint address corresponding to the one 136 * listed in the device configuration table 137 * @param[in] data Pointer to data to read 138 * @param[in] data_len Max length of the data requested to read. 139 * 140 * @return 0 on success, negative errno code on fail. 141 */ 142 int usbd_ep_start_read(const uint8_t ep, uint8_t *data, uint32_t data_len); 143 144 /* usb dcd irq callback */ 145 146 /** 147 * @brief Usb connect irq callback. 148 */ 149 void usbd_event_connect_handler(void); 150 151 /** 152 * @brief Usb disconnect irq callback. 153 */ 154 void usbd_event_disconnect_handler(void); 155 156 /** 157 * @brief Usb resume irq callback. 158 */ 159 void usbd_event_resume_handler(void); 160 161 /** 162 * @brief Usb suspend irq callback. 163 */ 164 void usbd_event_suspend_handler(void); 165 166 /** 167 * @brief Usb reset irq callback. 168 */ 169 void usbd_event_reset_handler(void); 170 171 /** 172 * @brief Usb setup packet recv irq callback. 173 * @param[in] psetup setup packet. 174 */ 175 void usbd_event_ep0_setup_complete_handler(uint8_t *psetup); 176 177 /** 178 * @brief In ep transfer complete irq callback. 179 * @param[in] ep Endpoint address corresponding to the one 180 * listed in the device configuration table 181 * @param[in] nbytes How many nbytes have transferred. 182 */ 183 void usbd_event_ep_in_complete_handler(uint8_t ep, uint32_t nbytes); 184 185 /** 186 * @brief Out ep transfer complete irq callback. 187 * @param[in] ep Endpoint address corresponding to the one 188 * listed in the device configuration table 189 * @param[in] nbytes How many nbytes have transferred. 190 */ 191 void usbd_event_ep_out_complete_handler(uint8_t ep, uint32_t nbytes); 192 193 #ifdef __cplusplus 194 } 195 #endif 196 197 #endif /* USB_DC_H */ 198