Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Endpoint_UC3.h 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief USB Endpoint definitions for the AVR32 UC3 microcontrollers.
  28. * \copydetails Group_EndpointManagement_UC3
  29. *
  30. * \note This file should not be included directly. It is automatically included as needed by the USB driver
  31. * dispatch header located in LUFA/Drivers/USB/USB.h.
  32. */
  33. /** \ingroup Group_EndpointRW
  34. * \defgroup Group_EndpointRW_UC3 Endpoint Data Reading and Writing (UC3)
  35. * \brief Endpoint data read/write definitions for the Atmel AVR32 UC3 architecture.
  36. *
  37. * Functions, macros, variables, enums and types related to data reading and writing from and to endpoints.
  38. */
  39. /** \ingroup Group_EndpointPrimitiveRW
  40. * \defgroup Group_EndpointPrimitiveRW_UC3 Read/Write of Primitive Data Types (UC3)
  41. * \brief Endpoint primitive read/write definitions for the Atmel AVR32 UC3 architecture.
  42. *
  43. * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
  44. * from and to endpoints.
  45. */
  46. /** \ingroup Group_EndpointPacketManagement
  47. * \defgroup Group_EndpointPacketManagement_UC3 Endpoint Packet Management (UC3)
  48. * \brief Endpoint packet management definitions for the Atmel AVR32 UC3 architecture.
  49. *
  50. * Functions, macros, variables, enums and types related to packet management of endpoints.
  51. */
  52. /** \ingroup Group_EndpointManagement
  53. * \defgroup Group_EndpointManagement_UC3 Endpoint Management (UC3)
  54. * \brief Endpoint management definitions for the Atmel AVR32 UC3 architecture.
  55. *
  56. * Functions, macros and enums related to endpoint management when in USB Device mode. This
  57. * module contains the endpoint management macros, as well as endpoint interrupt and data
  58. * send/receive functions for various data types.
  59. *
  60. * @{
  61. */
  62. #ifndef __ENDPOINT_UC3_H__
  63. #define __ENDPOINT_UC3_H__
  64. /* Includes: */
  65. #include "../../../../Common/Common.h"
  66. #include "../USBTask.h"
  67. #include "../USBInterrupt.h"
  68. /* Enable C linkage for C++ Compilers: */
  69. #if defined(__cplusplus)
  70. extern "C" {
  71. #endif
  72. /* Preprocessor Checks: */
  73. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  74. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  75. #endif
  76. /* Private Interface - For use in library only: */
  77. #if !defined(__DOXYGEN__)
  78. /* Macros: */
  79. #define ENDPOINT_HSB_ADDRESS_SPACE_SIZE (64 * 1024UL)
  80. /* Inline Functions: */
  81. static inline uint32_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST
  82. ATTR_ALWAYS_INLINE;
  83. static inline uint32_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)
  84. {
  85. uint8_t MaskVal = 0;
  86. uint16_t CheckBytes = 8;
  87. while (CheckBytes < Bytes)
  88. {
  89. MaskVal++;
  90. CheckBytes <<= 1;
  91. }
  92. return (MaskVal << AVR32_USBB_EPSIZE_OFFSET);
  93. }
  94. /* Function Prototypes: */
  95. void Endpoint_ClearEndpoints(void);
  96. bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
  97. const uint32_t UECFGXData);
  98. /* External Variables: */
  99. extern volatile uint32_t USB_Endpoint_SelectedEndpoint;
  100. extern volatile uint8_t* USB_Endpoint_FIFOPos[];
  101. #endif
  102. /* Public Interface - May be used in end-application: */
  103. /* Macros: */
  104. #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
  105. /** Default size of the default control endpoint's bank, until altered by the control endpoint bank size
  106. * value in the device descriptor. Not available if the \c FIXED_CONTROL_ENDPOINT_SIZE token is defined.
  107. */
  108. #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
  109. #endif
  110. #if !defined(CONTROL_ONLY_DEVICE) || defined(__DOXYGEN__)
  111. #if defined(USB_SERIES_UC3A3_AVR32) || defined(USB_SERIES_UC3A4_AVR32)
  112. #define ENDPOINT_TOTAL_ENDPOINTS 8
  113. #else
  114. /** Total number of endpoints (including the default control endpoint at address 0) which may
  115. * be used in the device. Different AVR models support different amounts of endpoints,
  116. * this value reflects the maximum number of endpoints for the currently selected AVR model.
  117. */
  118. #define ENDPOINT_TOTAL_ENDPOINTS 7
  119. #endif
  120. #else
  121. #define ENDPOINT_TOTAL_ENDPOINTS 1
  122. #endif
  123. /* Enums: */
  124. /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
  125. *
  126. * \ingroup Group_EndpointRW_UC3
  127. */
  128. enum Endpoint_WaitUntilReady_ErrorCodes_t
  129. {
  130. ENDPOINT_READYWAIT_NoError = 0, /**< Endpoint is ready for next packet, no error. */
  131. ENDPOINT_READYWAIT_EndpointStalled = 1, /**< The endpoint was stalled during the stream
  132. * transfer by the host or device.
  133. */
  134. ENDPOINT_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while
  135. * waiting for the endpoint to become ready.
  136. */
  137. ENDPOINT_READYWAIT_BusSuspended = 3, /**< The USB bus has been suspended by the host and
  138. * no USB endpoint traffic can occur until the bus
  139. * has resumed.
  140. */
  141. ENDPOINT_READYWAIT_Timeout = 4, /**< The host failed to accept or send the next packet
  142. * within the software timeout period set by the
  143. * \ref USB_STREAM_TIMEOUT_MS macro.
  144. */
  145. };
  146. /* Inline Functions: */
  147. /** Configures the specified endpoint address with the given endpoint type, bank size and number of hardware
  148. * banks. Once configured, the endpoint may be read from or written to, depending on its direction.
  149. *
  150. * \param[in] Address Endpoint address to configure.
  151. *
  152. * \param[in] Type Type of endpoint to configure, a \c EP_TYPE_* mask. Not all endpoint types
  153. * are available on Low Speed USB devices - refer to the USB 2.0 specification.
  154. *
  155. * \param[in] Size Size of the endpoint's bank, where packets are stored before they are transmitted
  156. * to the USB host, or after they have been received from the USB host (depending on
  157. * the endpoint's data direction). The bank size must indicate the maximum packet size
  158. * that the endpoint can handle.
  159. *
  160. * \param[in] Banks Number of hardware banks to use for the endpoint being configured.
  161. *
  162. * \attention When the \c ORDERED_EP_CONFIG compile time option is used, Endpoints <b>must</b> be configured in
  163. * ascending order, or bank corruption will occur.
  164. *
  165. * \note Different endpoints may have different maximum packet sizes based on the endpoint's index - refer to
  166. * the chosen microcontroller model's datasheet to determine the maximum bank size for each endpoint.
  167. * \n\n
  168. *
  169. * \note The default control endpoint should not be manually configured by the user application, as
  170. * it is automatically configured by the library internally.
  171. * \n\n
  172. *
  173. * \note This routine will automatically select the specified endpoint upon success. Upon failure, the endpoint
  174. * which failed to reconfigure correctly will be selected.
  175. *
  176. * \return Boolean \c true if the configuration succeeded, \c false otherwise.
  177. */
  178. static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,
  179. const uint8_t Type,
  180. const uint16_t Size,
  181. const uint8_t Banks) ATTR_ALWAYS_INLINE;
  182. static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,
  183. const uint8_t Type,
  184. const uint16_t Size,
  185. const uint8_t Banks)
  186. {
  187. uint8_t Number = (Address & ENDPOINT_EPNUM_MASK);
  188. if (Number >= ENDPOINT_TOTAL_ENDPOINTS)
  189. return false;
  190. return Endpoint_ConfigureEndpoint_Prv(Number,
  191. (AVR32_USBB_ALLOC_MASK |
  192. ((uint32_t)Type << AVR32_USBB_EPTYPE_OFFSET) |
  193. ((Address & ENDPOINT_DIR_IN) ? AVR32_USBB_UECFG0_EPDIR_MASK : 0) |
  194. ((Banks > 1) ? AVR32_USBB_UECFG0_EPBK_SINGLE : AVR32_USBB_UECFG0_EPBK_DOUBLE) |
  195. Endpoint_BytesToEPSizeMask(Size)));
  196. }
  197. /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
  198. *
  199. * \ingroup Group_EndpointRW_UC3
  200. *
  201. * \return Total number of bytes in the currently selected Endpoint's FIFO buffer.
  202. */
  203. static inline uint16_t Endpoint_BytesInEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  204. static inline uint16_t Endpoint_BytesInEndpoint(void)
  205. {
  206. return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].byct;
  207. }
  208. /** Determines the currently selected endpoint's direction.
  209. *
  210. * \return The currently selected endpoint's direction, as a \c ENDPOINT_DIR_* mask.
  211. */
  212. static inline uint32_t Endpoint_GetEndpointDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  213. static inline uint32_t Endpoint_GetEndpointDirection(void)
  214. {
  215. return ((&AVR32_USBB.UECFG0)[USB_Endpoint_SelectedEndpoint].epdir ? ENDPOINT_DIR_IN : ENDPOINT_DIR_OUT);
  216. }
  217. /** Get the endpoint address of the currently selected endpoint. This is typically used to save
  218. * the currently selected endpoint so that it can be restored after another endpoint has been
  219. * manipulated.
  220. *
  221. * \return Index of the currently selected endpoint.
  222. */
  223. static inline uint8_t Endpoint_GetCurrentEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  224. static inline uint8_t Endpoint_GetCurrentEndpoint(void)
  225. {
  226. return (USB_Endpoint_SelectedEndpoint | Endpoint_GetEndpointDirection());
  227. }
  228. /** Selects the given endpoint address.
  229. *
  230. * Any endpoint operations which do not require the endpoint address to be indicated will operate on
  231. * the currently selected endpoint.
  232. *
  233. * \param[in] Address Endpoint address to select.
  234. */
  235. static inline void Endpoint_SelectEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;
  236. static inline void Endpoint_SelectEndpoint(const uint8_t Address)
  237. {
  238. USB_Endpoint_SelectedEndpoint = (Address & ENDPOINT_EPNUM_MASK);
  239. }
  240. /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
  241. * data In and Out pointers to the bank's contents.
  242. *
  243. * \param[in] Address Endpoint number whose FIFO buffers are to be reset.
  244. */
  245. static inline void Endpoint_ResetEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;
  246. static inline void Endpoint_ResetEndpoint(const uint8_t Address)
  247. {
  248. uint32_t EndpointNumber = (Address & ENDPOINT_EPNUM_MASK);
  249. AVR32_USBB.uerst |= (AVR32_USBB_EPRST0_MASK << EndpointNumber);
  250. AVR32_USBB.uerst &= ~(AVR32_USBB_EPRST0_MASK << EndpointNumber);
  251. USB_Endpoint_FIFOPos[EndpointNumber] = &AVR32_USBB_SLAVE[EndpointNumber * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];
  252. }
  253. /** Enables the currently selected endpoint so that data can be sent and received through it to
  254. * and from a host.
  255. *
  256. * \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().
  257. */
  258. static inline void Endpoint_EnableEndpoint(void) ATTR_ALWAYS_INLINE;
  259. static inline void Endpoint_EnableEndpoint(void)
  260. {
  261. AVR32_USBB.uerst |= (AVR32_USBB_EPEN0_MASK << USB_Endpoint_SelectedEndpoint);
  262. }
  263. /** Disables the currently selected endpoint so that data cannot be sent and received through it
  264. * to and from a host.
  265. */
  266. static inline void Endpoint_DisableEndpoint(void) ATTR_ALWAYS_INLINE;
  267. static inline void Endpoint_DisableEndpoint(void)
  268. {
  269. AVR32_USBB.uerst &= ~(AVR32_USBB_EPEN0_MASK << USB_Endpoint_SelectedEndpoint);
  270. }
  271. /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
  272. *
  273. * \return Boolean \c true if the currently selected endpoint is enabled, \c false otherwise.
  274. */
  275. static inline bool Endpoint_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  276. static inline bool Endpoint_IsEnabled(void)
  277. {
  278. return ((AVR32_USBB.uerst & (AVR32_USBB_EPEN0_MASK << USB_Endpoint_SelectedEndpoint)) ? true : false);
  279. }
  280. /** Retrieves the number of busy banks in the currently selected endpoint, which have been queued for
  281. * transmission via the \ref Endpoint_ClearIN() command, or are awaiting acknowledgement via the
  282. * \ref Endpoint_ClearOUT() command.
  283. *
  284. * \ingroup Group_EndpointPacketManagement_UC3
  285. *
  286. * \return Total number of busy banks in the selected endpoint.
  287. */
  288. static inline uint8_t Endpoint_GetBusyBanks(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  289. static inline uint8_t Endpoint_GetBusyBanks(void)
  290. {
  291. return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].nbusybk;
  292. }
  293. /** Aborts all pending IN transactions on the currently selected endpoint, once the bank
  294. * has been queued for transmission to the host via \ref Endpoint_ClearIN(). This function
  295. * will terminate all queued transactions, resetting the endpoint banks ready for a new
  296. * packet.
  297. *
  298. * \ingroup Group_EndpointPacketManagement_UC3
  299. */
  300. static inline void Endpoint_AbortPendingIN(void)
  301. {
  302. while (Endpoint_GetBusyBanks() != 0)
  303. {
  304. (&AVR32_USBB.UECON0SET)[USB_Endpoint_SelectedEndpoint].killbks = true;
  305. while ((&AVR32_USBB.UECON0)[USB_Endpoint_SelectedEndpoint].killbk);
  306. }
  307. }
  308. /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
  309. * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
  310. * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
  311. * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
  312. * direction and the endpoint bank is full.
  313. *
  314. * \ingroup Group_EndpointPacketManagement_UC3
  315. *
  316. * \return Boolean \c true if the currently selected endpoint may be read from or written to, depending
  317. * on its direction.
  318. */
  319. static inline bool Endpoint_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  320. static inline bool Endpoint_IsReadWriteAllowed(void)
  321. {
  322. return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].rwall;
  323. }
  324. /** Determines if the currently selected endpoint is configured.
  325. *
  326. * \return Boolean \c true if the currently selected endpoint has been configured, \c false otherwise.
  327. */
  328. static inline bool Endpoint_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  329. static inline bool Endpoint_IsConfigured(void)
  330. {
  331. return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].cfgok;
  332. }
  333. /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
  334. * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
  335. * masking the return value against <tt>(1 << <i>{Endpoint Number}</i>)</tt>.
  336. *
  337. * \return Mask whose bits indicate which endpoints have interrupted.
  338. */
  339. static inline uint32_t Endpoint_GetEndpointInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  340. static inline uint32_t Endpoint_GetEndpointInterrupts(void)
  341. {
  342. return ((AVR32_USBB.udint & (AVR32_USBB_EP6INT_MASK | AVR32_USBB_EP5INT_MASK |
  343. AVR32_USBB_EP4INT_MASK | AVR32_USBB_EP3INT_MASK |
  344. AVR32_USBB_EP2INT_MASK | AVR32_USBB_EP1INT_MASK |
  345. AVR32_USBB_EP0INT_MASK)) >> AVR32_USBB_EP0INT_OFFSET);
  346. }
  347. /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type
  348. * endpoints).
  349. *
  350. * \param[in] Address Address of the endpoint whose interrupt flag should be tested.
  351. *
  352. * \return Boolean \c true if the specified endpoint has interrupted, \c false otherwise.
  353. */
  354. static inline bool Endpoint_HasEndpointInterrupted(const uint8_t Address) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  355. static inline bool Endpoint_HasEndpointInterrupted(const uint8_t Address)
  356. {
  357. return ((Endpoint_GetEndpointInterrupts() & (AVR32_USBB_EP0INT_MASK << (Address & ENDPOINT_EPNUM_MASK))) ? true : false);
  358. }
  359. /** Determines if the selected IN endpoint is ready for a new packet to be sent to the host.
  360. *
  361. * \ingroup Group_EndpointPacketManagement_UC3
  362. *
  363. * \return Boolean \c true if the current endpoint is ready for an IN packet, \c false otherwise.
  364. */
  365. static inline bool Endpoint_IsINReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  366. static inline bool Endpoint_IsINReady(void)
  367. {
  368. return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].txini;
  369. }
  370. /** Determines if the selected OUT endpoint has received new packet from the host.
  371. *
  372. * \ingroup Group_EndpointPacketManagement_UC3
  373. *
  374. * \return Boolean \c true if current endpoint is has received an OUT packet, \c false otherwise.
  375. */
  376. static inline bool Endpoint_IsOUTReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  377. static inline bool Endpoint_IsOUTReceived(void)
  378. {
  379. return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].rxouti;
  380. }
  381. /** Determines if the current CONTROL type endpoint has received a SETUP packet.
  382. *
  383. * \ingroup Group_EndpointPacketManagement_UC3
  384. *
  385. * \return Boolean \c true if the selected endpoint has received a SETUP packet, \c false otherwise.
  386. */
  387. static inline bool Endpoint_IsSETUPReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  388. static inline bool Endpoint_IsSETUPReceived(void)
  389. {
  390. return (&AVR32_USBB.UESTA0)[USB_Endpoint_SelectedEndpoint].rxstpi;
  391. }
  392. /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the
  393. * endpoint for the next packet.
  394. *
  395. * \ingroup Group_EndpointPacketManagement_UC3
  396. *
  397. * \note This is not applicable for non CONTROL type endpoints.
  398. */
  399. static inline void Endpoint_ClearSETUP(void) ATTR_ALWAYS_INLINE;
  400. static inline void Endpoint_ClearSETUP(void)
  401. {
  402. (&AVR32_USBB.UESTA0CLR)[USB_Endpoint_SelectedEndpoint].rxstpic = true;
  403. USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint] = &AVR32_USBB_SLAVE[USB_Endpoint_SelectedEndpoint * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];
  404. }
  405. /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
  406. * next packet and switching to the alternative endpoint bank if double banked.
  407. *
  408. * \ingroup Group_EndpointPacketManagement_UC3
  409. */
  410. static inline void Endpoint_ClearIN(void) ATTR_ALWAYS_INLINE;
  411. static inline void Endpoint_ClearIN(void)
  412. {
  413. (&AVR32_USBB.UESTA0CLR)[USB_Endpoint_SelectedEndpoint].txinic = true;
  414. (&AVR32_USBB.UECON0CLR)[USB_Endpoint_SelectedEndpoint].fifoconc = true;
  415. USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint] = &AVR32_USBB_SLAVE[USB_Endpoint_SelectedEndpoint * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];
  416. }
  417. /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
  418. * for the next packet and switching to the alternative endpoint bank if double banked.
  419. *
  420. * \ingroup Group_EndpointPacketManagement_UC3
  421. */
  422. static inline void Endpoint_ClearOUT(void) ATTR_ALWAYS_INLINE;
  423. static inline void Endpoint_ClearOUT(void)
  424. {
  425. (&AVR32_USBB.UESTA0CLR)[USB_Endpoint_SelectedEndpoint].rxoutic = true;
  426. (&AVR32_USBB.UECON0CLR)[USB_Endpoint_SelectedEndpoint].fifoconc = true;
  427. USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint] = &AVR32_USBB_SLAVE[USB_Endpoint_SelectedEndpoint * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];
  428. }
  429. /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
  430. * indicated endpoint and that the current transfer sequence should be aborted. This provides a
  431. * way for devices to indicate invalid commands to the host so that the current transfer can be
  432. * aborted and the host can begin its own recovery sequence.
  433. *
  434. * The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro
  435. * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
  436. * endpoint.
  437. *
  438. * \ingroup Group_EndpointPacketManagement_UC3
  439. */
  440. static inline void Endpoint_StallTransaction(void) ATTR_ALWAYS_INLINE;
  441. static inline void Endpoint_StallTransaction(void)
  442. {
  443. (&AVR32_USBB.UECON0SET)[USB_Endpoint_SelectedEndpoint].stallrqs = true;
  444. }
  445. /** Clears the STALL condition on the currently selected endpoint.
  446. *
  447. * \ingroup Group_EndpointPacketManagement_UC3
  448. */
  449. static inline void Endpoint_ClearStall(void) ATTR_ALWAYS_INLINE;
  450. static inline void Endpoint_ClearStall(void)
  451. {
  452. (&AVR32_USBB.UECON0CLR)[USB_Endpoint_SelectedEndpoint].stallrqc = true;
  453. }
  454. /** Determines if the currently selected endpoint is stalled, \c false otherwise.
  455. *
  456. * \ingroup Group_EndpointPacketManagement_UC3
  457. *
  458. * \return Boolean \c true if the currently selected endpoint is stalled, \c false otherwise.
  459. */
  460. static inline bool Endpoint_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  461. static inline bool Endpoint_IsStalled(void)
  462. {
  463. return (&AVR32_USBB.UECON0)[USB_Endpoint_SelectedEndpoint].stallrq;
  464. }
  465. /** Resets the data toggle of the currently selected endpoint. */
  466. static inline void Endpoint_ResetDataToggle(void) ATTR_ALWAYS_INLINE;
  467. static inline void Endpoint_ResetDataToggle(void)
  468. {
  469. (&AVR32_USBB.UECON0SET)[USB_Endpoint_SelectedEndpoint].rstdts = true;
  470. }
  471. /** Sets the direction of the currently selected endpoint.
  472. *
  473. * \param[in] DirectionMask New endpoint direction, as a \c ENDPOINT_DIR_* mask.
  474. */
  475. static inline void Endpoint_SetEndpointDirection(const uint32_t DirectionMask) ATTR_ALWAYS_INLINE;
  476. static inline void Endpoint_SetEndpointDirection(const uint32_t DirectionMask)
  477. {
  478. (&AVR32_USBB.UECFG0)[USB_Endpoint_SelectedEndpoint].epdir = (DirectionMask == ENDPOINT_DIR_IN);
  479. }
  480. /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
  481. *
  482. * \ingroup Group_EndpointPrimitiveRW_UC3
  483. *
  484. * \return Next byte in the currently selected endpoint's FIFO buffer.
  485. */
  486. static inline uint8_t Endpoint_Read_8(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  487. static inline uint8_t Endpoint_Read_8(void)
  488. {
  489. return *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  490. }
  491. /** Writes one byte to the currently selected endpoint's bank, for IN direction endpoints.
  492. *
  493. * \ingroup Group_EndpointPrimitiveRW_UC3
  494. *
  495. * \param[in] Data Data to write into the the currently selected endpoint's FIFO buffer.
  496. */
  497. static inline void Endpoint_Write_8(const uint8_t Data) ATTR_ALWAYS_INLINE;
  498. static inline void Endpoint_Write_8(const uint8_t Data)
  499. {
  500. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = Data;
  501. }
  502. /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
  503. *
  504. * \ingroup Group_EndpointPrimitiveRW_UC3
  505. */
  506. static inline void Endpoint_Discard_8(void) ATTR_ALWAYS_INLINE;
  507. static inline void Endpoint_Discard_8(void)
  508. {
  509. uint8_t Dummy;
  510. Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  511. (void)Dummy;
  512. }
  513. /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
  514. * direction endpoints.
  515. *
  516. * \ingroup Group_EndpointPrimitiveRW_UC3
  517. *
  518. * \return Next two bytes in the currently selected endpoint's FIFO buffer.
  519. */
  520. static inline uint16_t Endpoint_Read_16_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  521. static inline uint16_t Endpoint_Read_16_LE(void)
  522. {
  523. uint16_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  524. uint16_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  525. return ((Byte1 << 8) | Byte0);
  526. }
  527. /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
  528. * direction endpoints.
  529. *
  530. * \ingroup Group_EndpointPrimitiveRW_UC3
  531. *
  532. * \return Next two bytes in the currently selected endpoint's FIFO buffer.
  533. */
  534. static inline uint16_t Endpoint_Read_16_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  535. static inline uint16_t Endpoint_Read_16_BE(void)
  536. {
  537. uint16_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  538. uint16_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  539. return ((Byte0 << 8) | Byte1);
  540. }
  541. /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
  542. * direction endpoints.
  543. *
  544. * \ingroup Group_EndpointPrimitiveRW_UC3
  545. *
  546. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  547. */
  548. static inline void Endpoint_Write_16_LE(const uint16_t Data) ATTR_ALWAYS_INLINE;
  549. static inline void Endpoint_Write_16_LE(const uint16_t Data)
  550. {
  551. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data & 0xFF);
  552. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);
  553. }
  554. /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
  555. * direction endpoints.
  556. *
  557. * \ingroup Group_EndpointPrimitiveRW_UC3
  558. *
  559. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  560. */
  561. static inline void Endpoint_Write_16_BE(const uint16_t Data) ATTR_ALWAYS_INLINE;
  562. static inline void Endpoint_Write_16_BE(const uint16_t Data)
  563. {
  564. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);
  565. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data & 0xFF);
  566. }
  567. /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
  568. *
  569. * \ingroup Group_EndpointPrimitiveRW_UC3
  570. */
  571. static inline void Endpoint_Discard_16(void) ATTR_ALWAYS_INLINE;
  572. static inline void Endpoint_Discard_16(void)
  573. {
  574. uint8_t Dummy;
  575. Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  576. Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  577. (void)Dummy;
  578. }
  579. /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
  580. * direction endpoints.
  581. *
  582. * \ingroup Group_EndpointPrimitiveRW_UC3
  583. *
  584. * \return Next four bytes in the currently selected endpoint's FIFO buffer.
  585. */
  586. static inline uint32_t Endpoint_Read_32_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  587. static inline uint32_t Endpoint_Read_32_LE(void)
  588. {
  589. uint32_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  590. uint32_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  591. uint32_t Byte2 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  592. uint32_t Byte3 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  593. return ((Byte3 << 24) | (Byte2 << 16) | (Byte1 << 8) | Byte0);
  594. }
  595. /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
  596. * direction endpoints.
  597. *
  598. * \ingroup Group_EndpointPrimitiveRW_UC3
  599. *
  600. * \return Next four bytes in the currently selected endpoint's FIFO buffer.
  601. */
  602. static inline uint32_t Endpoint_Read_32_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  603. static inline uint32_t Endpoint_Read_32_BE(void)
  604. {
  605. uint32_t Byte0 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  606. uint32_t Byte1 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  607. uint32_t Byte2 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  608. uint32_t Byte3 = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  609. return ((Byte0 << 24) | (Byte1 << 16) | (Byte2 << 8) | Byte3);
  610. }
  611. /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
  612. * direction endpoints.
  613. *
  614. * \ingroup Group_EndpointPrimitiveRW_UC3
  615. *
  616. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  617. */
  618. static inline void Endpoint_Write_32_LE(const uint32_t Data) ATTR_ALWAYS_INLINE;
  619. static inline void Endpoint_Write_32_LE(const uint32_t Data)
  620. {
  621. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data & 0xFF);
  622. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);
  623. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 16);
  624. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 24);
  625. }
  626. /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
  627. * direction endpoints.
  628. *
  629. * \ingroup Group_EndpointPrimitiveRW_UC3
  630. *
  631. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  632. */
  633. static inline void Endpoint_Write_32_BE(const uint32_t Data) ATTR_ALWAYS_INLINE;
  634. static inline void Endpoint_Write_32_BE(const uint32_t Data)
  635. {
  636. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 24);
  637. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 16);
  638. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data >> 8);
  639. *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++) = (Data & 0xFF);
  640. }
  641. /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
  642. *
  643. * \ingroup Group_EndpointPrimitiveRW_UC3
  644. */
  645. static inline void Endpoint_Discard_32(void) ATTR_ALWAYS_INLINE;
  646. static inline void Endpoint_Discard_32(void)
  647. {
  648. uint8_t Dummy;
  649. Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  650. Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  651. Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  652. Dummy = *(USB_Endpoint_FIFOPos[USB_Endpoint_SelectedEndpoint]++);
  653. (void)Dummy;
  654. }
  655. /* External Variables: */
  656. /** Global indicating the maximum packet size of the default control endpoint located at address
  657. * 0 in the device. This value is set to the value indicated in the device descriptor in the user
  658. * project once the USB interface is initialized into device mode.
  659. *
  660. * If space is an issue, it is possible to fix this to a static value by defining the control
  661. * endpoint size in the \c FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
  662. * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
  663. * read from the descriptors at runtime and instead fixed to the given value. When used, it is
  664. * important that the descriptor control endpoint size value matches the size given as the
  665. * \c FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the \c FIXED_CONTROL_ENDPOINT_SIZE token
  666. * be used in the device descriptors to ensure this.
  667. *
  668. * \attention This variable should be treated as read-only in the user application, and never manually
  669. * changed in value.
  670. */
  671. #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
  672. extern uint8_t USB_Device_ControlEndpointSize;
  673. #else
  674. #define USB_Device_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
  675. #endif
  676. /* Function Prototypes: */
  677. /** Configures a table of endpoint descriptions, in sequence. This function can be used to configure multiple
  678. * endpoints at the same time.
  679. *
  680. * \note Endpoints with a zero address will be ignored, thus this function cannot be used to configure the
  681. * control endpoint.
  682. *
  683. * \param[in] Table Pointer to a table of endpoint descriptions.
  684. * \param[in] Entries Number of entries in the endpoint table to configure.
  685. *
  686. * \return Boolean \c true if all endpoints configured successfully, \c false otherwise.
  687. */
  688. bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
  689. const uint8_t Entries);
  690. /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
  691. * with respect to the data direction. This is a convenience function which can be used to
  692. * simplify user control request handling.
  693. *
  694. * \note This routine should not be called on non CONTROL type endpoints.
  695. */
  696. void Endpoint_ClearStatusStage(void);
  697. /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data
  698. * to be read or written to it.
  699. *
  700. * \note This routine should not be called on CONTROL type endpoints.
  701. *
  702. * \ingroup Group_EndpointRW_UC3
  703. *
  704. * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
  705. */
  706. uint8_t Endpoint_WaitUntilReady(void);
  707. /* Disable C linkage for C++ Compilers: */
  708. #if defined(__cplusplus)
  709. }
  710. #endif
  711. #endif
  712. /** @} */