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_XMEGA.h 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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 AVR XMEGA microcontrollers.
  28. * \copydetails Group_EndpointManagement_XMEGA
  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_XMEGA Endpoint Data Reading and Writing (XMEGA)
  35. * \brief Endpoint data read/write definitions for the Atmel AVR XMEGA 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_XMEGA Read/Write of Primitive Data Types (XMEGA)
  41. * \brief Endpoint primitive read/write definitions for the Atmel AVR XMEGA 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_XMEGA Endpoint Packet Management (XMEGA)
  48. * \brief Endpoint packet management definitions for the Atmel AVR XMEGA architecture.
  49. *
  50. * Functions, macros, variables, enums and types related to packet management of endpoints.
  51. */
  52. /** \ingroup Group_EndpointManagement
  53. * \defgroup Group_EndpointManagement_XMEGA Endpoint Management (XMEGA)
  54. * \brief Endpoint management definitions for the Atmel AVR XMEGA 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_XMEGA_H__
  63. #define __ENDPOINT_XMEGA_H__
  64. /* Includes: */
  65. #include "../../../../Common/Common.h"
  66. #include "../USBTask.h"
  67. #include "../USBInterrupt.h"
  68. #include "../USBController.h"
  69. /* Enable C linkage for C++ Compilers: */
  70. #if defined(__cplusplus)
  71. extern "C" {
  72. #endif
  73. /* Preprocessor Checks: */
  74. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  75. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  76. #endif
  77. /* Public Interface - May be used in end-application: */
  78. /* Macros: */
  79. #if (!defined(MAX_ENDPOINT_INDEX) && !defined(CONTROL_ONLY_DEVICE)) || defined(__DOXYGEN__)
  80. /** Total number of endpoints (including the default control endpoint at address 0) which may
  81. * be used in the device. Different USB AVR models support different amounts of endpoints,
  82. * this value reflects the maximum number of endpoints for the currently selected AVR model.
  83. */
  84. #define ENDPOINT_TOTAL_ENDPOINTS 16
  85. #else
  86. #if defined(CONTROL_ONLY_DEVICE)
  87. #define ENDPOINT_TOTAL_ENDPOINTS 1
  88. #else
  89. #define ENDPOINT_TOTAL_ENDPOINTS (MAX_ENDPOINT_INDEX + 1)
  90. #endif
  91. #endif
  92. /* Private Interface - For use in library only: */
  93. #if !defined(__DOXYGEN__)
  94. /* Type Defines: */
  95. typedef struct
  96. {
  97. uint8_t Data[64];
  98. uint8_t Length;
  99. uint8_t Position;
  100. } Endpoint_FIFO_t;
  101. typedef struct
  102. {
  103. Endpoint_FIFO_t OUT;
  104. Endpoint_FIFO_t IN;
  105. } Endpoint_FIFOPair_t;
  106. /* External Variables: */
  107. extern Endpoint_FIFOPair_t USB_Endpoint_FIFOs[ENDPOINT_TOTAL_ENDPOINTS];
  108. extern volatile uint8_t USB_Endpoint_SelectedEndpoint;
  109. extern volatile USB_EP_t* USB_Endpoint_SelectedHandle;
  110. extern volatile Endpoint_FIFO_t* USB_Endpoint_SelectedFIFO;
  111. /* Inline Functions: */
  112. static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST
  113. ATTR_ALWAYS_INLINE;
  114. static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)
  115. {
  116. uint8_t MaskVal = 0;
  117. uint16_t CheckBytes = 8;
  118. while (CheckBytes < Bytes)
  119. {
  120. MaskVal++;
  121. CheckBytes <<= 1;
  122. }
  123. return (MaskVal << USB_EP_BUFSIZE_gp);
  124. }
  125. /* Function Prototypes: */
  126. bool Endpoint_ConfigureEndpoint_PRV(const uint8_t Address,
  127. const uint8_t Config,
  128. const uint8_t Size);
  129. void Endpoint_ClearEndpoints(void);
  130. #endif
  131. /* Public Interface - May be used in end-application: */
  132. /* Macros: */
  133. #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
  134. /** Default size of the default control endpoint's bank, until altered by the control endpoint bank size
  135. * value in the device descriptor. Not available if the \c FIXED_CONTROL_ENDPOINT_SIZE token is defined.
  136. */
  137. #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
  138. #endif
  139. /* Enums: */
  140. /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
  141. *
  142. * \ingroup Group_EndpointRW_XMEGA
  143. */
  144. enum Endpoint_WaitUntilReady_ErrorCodes_t
  145. {
  146. ENDPOINT_READYWAIT_NoError = 0, /**< Endpoint is ready for next packet, no error. */
  147. ENDPOINT_READYWAIT_EndpointStalled = 1, /**< The endpoint was stalled during the stream
  148. * transfer by the host or device.
  149. */
  150. ENDPOINT_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while
  151. * waiting for the endpoint to become ready.
  152. */
  153. ENDPOINT_READYWAIT_BusSuspended = 3, /**< The USB bus has been suspended by the host and
  154. * no USB endpoint traffic can occur until the bus
  155. * has resumed.
  156. */
  157. ENDPOINT_READYWAIT_Timeout = 4, /**< The host failed to accept or send the next packet
  158. * within the software timeout period set by the
  159. * \ref USB_STREAM_TIMEOUT_MS macro.
  160. */
  161. };
  162. /* Inline Functions: */
  163. /** Selects the given endpoint address.
  164. *
  165. * Any endpoint operations which do not require the endpoint address to be indicated will operate on
  166. * the currently selected endpoint.
  167. *
  168. * \param[in] Address Endpoint address to select.
  169. */
  170. void Endpoint_SelectEndpoint(const uint8_t Address);
  171. /** Configures the specified endpoint address with the given endpoint type, bank size and number of hardware
  172. * banks. Once configured, the endpoint may be read from or written to, depending on its direction.
  173. *
  174. * \param[in] Address Endpoint address to configure.
  175. *
  176. * \param[in] Type Type of endpoint to configure, a \c EP_TYPE_* mask. Not all endpoint types
  177. * are available on Low Speed USB devices - refer to the USB 2.0 specification.
  178. *
  179. * \param[in] Size Size of the endpoint's bank, where packets are stored before they are transmitted
  180. * to the USB host, or after they have been received from the USB host (depending on
  181. * the endpoint's data direction). The bank size must indicate the maximum packet size
  182. * that the endpoint can handle.
  183. *
  184. * \param[in] Banks Number of hardware banks to use for the endpoint being configured.
  185. *
  186. * \note The default control endpoint should not be manually configured by the user application, as
  187. * it is automatically configured by the library internally.
  188. * \n\n
  189. *
  190. * \note This routine will automatically select the specified endpoint.
  191. *
  192. * \return Boolean \c true if the configuration succeeded, \c false otherwise.
  193. */
  194. static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,
  195. const uint8_t Type,
  196. const uint16_t Size,
  197. const uint8_t Banks) ATTR_ALWAYS_INLINE;
  198. static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,
  199. const uint8_t Type,
  200. const uint16_t Size,
  201. const uint8_t Banks)
  202. {
  203. uint8_t EPConfigMask = (USB_EP_INTDSBL_bm | ((Banks > 1) ? USB_EP_PINGPONG_bm : 0) | Endpoint_BytesToEPSizeMask(Size));
  204. if ((Address & ENDPOINT_EPNUM_MASK) >= ENDPOINT_TOTAL_ENDPOINTS)
  205. return false;
  206. // TODO - Fix once limitations are lifted
  207. EPConfigMask &= ~USB_EP_PINGPONG_bm;
  208. if (Size > 64)
  209. return false;
  210. switch (Type)
  211. {
  212. case EP_TYPE_CONTROL:
  213. EPConfigMask |= USB_EP_TYPE_CONTROL_gc;
  214. break;
  215. case EP_TYPE_ISOCHRONOUS:
  216. EPConfigMask |= USB_EP_TYPE_ISOCHRONOUS_gc;
  217. break;
  218. default:
  219. EPConfigMask |= USB_EP_TYPE_BULK_gc;
  220. break;
  221. }
  222. if (Type == EP_TYPE_CONTROL)
  223. Endpoint_ConfigureEndpoint_PRV(Address ^ ENDPOINT_DIR_IN, EPConfigMask, Size);
  224. return Endpoint_ConfigureEndpoint_PRV(Address, EPConfigMask, Size);
  225. }
  226. /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
  227. *
  228. * \ingroup Group_EndpointRW_XMEGA
  229. *
  230. * \return Total number of bytes in the currently selected Endpoint's FIFO buffer.
  231. */
  232. static inline uint16_t Endpoint_BytesInEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  233. static inline uint16_t Endpoint_BytesInEndpoint(void)
  234. {
  235. if (USB_Endpoint_SelectedEndpoint & ENDPOINT_DIR_IN)
  236. return USB_Endpoint_SelectedFIFO->Position;
  237. else
  238. return (USB_Endpoint_SelectedFIFO->Length - USB_Endpoint_SelectedFIFO->Position);
  239. }
  240. /** Get the endpoint address of the currently selected endpoint. This is typically used to save
  241. * the currently selected endpoint so that it can be restored after another endpoint has been
  242. * manipulated.
  243. *
  244. * \return Index of the currently selected endpoint.
  245. */
  246. static inline uint8_t Endpoint_GetCurrentEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  247. static inline uint8_t Endpoint_GetCurrentEndpoint(void)
  248. {
  249. return USB_Endpoint_SelectedEndpoint;
  250. }
  251. /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
  252. * data In and Out pointers to the bank's contents.
  253. *
  254. * \param[in] Address Endpoint address whose FIFO buffers are to be reset.
  255. */
  256. static inline void Endpoint_ResetEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;
  257. static inline void Endpoint_ResetEndpoint(const uint8_t Address)
  258. {
  259. if (Address & ENDPOINT_DIR_IN)
  260. USB_Endpoint_FIFOs[Address & ENDPOINT_EPNUM_MASK].IN.Position = 0;
  261. else
  262. USB_Endpoint_FIFOs[Address & ENDPOINT_EPNUM_MASK].OUT.Position = 0;
  263. }
  264. /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
  265. *
  266. * \return Boolean \c true if the currently selected endpoint is enabled, \c false otherwise.
  267. */
  268. static inline bool Endpoint_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  269. static inline bool Endpoint_IsEnabled(void)
  270. {
  271. return true;
  272. }
  273. /** Aborts all pending IN transactions on the currently selected endpoint, once the bank
  274. * has been queued for transmission to the host via \ref Endpoint_ClearIN(). This function
  275. * will terminate all queued transactions, resetting the endpoint banks ready for a new
  276. * packet.
  277. *
  278. * \ingroup Group_EndpointPacketManagement_XMEGA
  279. */
  280. static inline void Endpoint_AbortPendingIN(void)
  281. {
  282. USB_Endpoint_SelectedHandle->STATUS |= USB_EP_BUSNACK0_bm;
  283. }
  284. /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
  285. * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
  286. * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
  287. * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
  288. * direction and the endpoint bank is full.
  289. *
  290. * \ingroup Group_EndpointPacketManagement_XMEGA
  291. *
  292. * \return Boolean \c true if the currently selected endpoint may be read from or written to, depending
  293. * on its direction.
  294. */
  295. static inline bool Endpoint_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  296. static inline bool Endpoint_IsReadWriteAllowed(void)
  297. {
  298. return (USB_Endpoint_SelectedFIFO->Position < USB_Endpoint_SelectedFIFO->Length);
  299. }
  300. /** Determines if the currently selected endpoint is configured.
  301. *
  302. * \return Boolean \c true if the currently selected endpoint has been configured, \c false otherwise.
  303. */
  304. static inline bool Endpoint_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  305. static inline bool Endpoint_IsConfigured(void)
  306. {
  307. return ((USB_Endpoint_SelectedHandle->CTRL & USB_EP_TYPE_gm) ? true : false);
  308. }
  309. /** Determines if the selected IN endpoint is ready for a new packet to be sent to the host.
  310. *
  311. * \ingroup Group_EndpointPacketManagement_XMEGA
  312. *
  313. * \return Boolean \c true if the current endpoint is ready for an IN packet, \c false otherwise.
  314. */
  315. bool Endpoint_IsINReady(void) ATTR_WARN_UNUSED_RESULT;
  316. /** Determines if the selected OUT endpoint has received new packet from the host.
  317. *
  318. * \ingroup Group_EndpointPacketManagement_XMEGA
  319. *
  320. * \return Boolean \c true if current endpoint is has received an OUT packet, \c false otherwise.
  321. */
  322. bool Endpoint_IsOUTReceived(void) ATTR_WARN_UNUSED_RESULT;
  323. /** Determines if the current CONTROL type endpoint has received a SETUP packet.
  324. *
  325. * \ingroup Group_EndpointPacketManagement_XMEGA
  326. *
  327. * \return Boolean \c true if the selected endpoint has received a SETUP packet, \c false otherwise.
  328. */
  329. bool Endpoint_IsSETUPReceived(void) ATTR_WARN_UNUSED_RESULT;
  330. /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the
  331. * endpoint for the next packet.
  332. *
  333. * \ingroup Group_EndpointPacketManagement_XMEGA
  334. *
  335. * \note This is not applicable for non CONTROL type endpoints.
  336. */
  337. void Endpoint_ClearSETUP(void);
  338. /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
  339. * next packet and switching to the alternative endpoint bank if double banked.
  340. *
  341. * \ingroup Group_EndpointPacketManagement_XMEGA
  342. */
  343. void Endpoint_ClearIN(void);
  344. /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
  345. * for the next packet and switching to the alternative endpoint bank if double banked.
  346. *
  347. * \ingroup Group_EndpointPacketManagement_XMEGA
  348. */
  349. void Endpoint_ClearOUT(void);
  350. /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
  351. * indicated endpoint and that the current transfer sequence should be aborted. This provides a
  352. * way for devices to indicate invalid commands to the host so that the current transfer can be
  353. * aborted and the host can begin its own recovery sequence.
  354. *
  355. * The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro
  356. * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
  357. * endpoint.
  358. *
  359. * \ingroup Group_EndpointPacketManagement_XMEGA
  360. */
  361. void Endpoint_StallTransaction(void);
  362. /** Clears the STALL condition on the currently selected endpoint.
  363. *
  364. * \ingroup Group_EndpointPacketManagement_XMEGA
  365. */
  366. static inline void Endpoint_ClearStall(void) ATTR_ALWAYS_INLINE;
  367. static inline void Endpoint_ClearStall(void)
  368. {
  369. USB_Endpoint_SelectedHandle->CTRL &= ~USB_EP_STALL_bm;
  370. }
  371. /** Determines if the currently selected endpoint is stalled, \c false otherwise.
  372. *
  373. * \ingroup Group_EndpointPacketManagement_XMEGA
  374. *
  375. * \return Boolean \c true if the currently selected endpoint is stalled, \c false otherwise.
  376. */
  377. static inline bool Endpoint_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  378. static inline bool Endpoint_IsStalled(void)
  379. {
  380. return ((USB_Endpoint_SelectedHandle->CTRL & USB_EP_STALL_bm) ? true : false);
  381. }
  382. /** Resets the data toggle of the currently selected endpoint. */
  383. static inline void Endpoint_ResetDataToggle(void) ATTR_ALWAYS_INLINE;
  384. static inline void Endpoint_ResetDataToggle(void)
  385. {
  386. USB_Endpoint_SelectedHandle->STATUS &= ~USB_EP_TOGGLE_bm;
  387. }
  388. /** Determines the currently selected endpoint's direction.
  389. *
  390. * \return The currently selected endpoint's direction, as a \c ENDPOINT_DIR_* mask.
  391. */
  392. static inline uint8_t Endpoint_GetEndpointDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  393. static inline uint8_t Endpoint_GetEndpointDirection(void)
  394. {
  395. return (USB_Endpoint_SelectedEndpoint & ENDPOINT_DIR_IN);
  396. }
  397. /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
  398. *
  399. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  400. *
  401. * \return Next byte in the currently selected endpoint's FIFO buffer.
  402. */
  403. uint8_t Endpoint_Read_8(void) ATTR_WARN_UNUSED_RESULT;
  404. /** Writes one byte to the currently selected endpoint's bank, for IN direction endpoints.
  405. *
  406. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  407. *
  408. * \param[in] Data Data to write into the the currently selected endpoint's FIFO buffer.
  409. */
  410. void Endpoint_Write_8(const uint8_t Data);
  411. /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
  412. *
  413. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  414. */
  415. static inline void Endpoint_Discard_8(void) ATTR_ALWAYS_INLINE;
  416. static inline void Endpoint_Discard_8(void)
  417. {
  418. USB_Endpoint_SelectedFIFO->Position++;
  419. }
  420. /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
  421. * direction endpoints.
  422. *
  423. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  424. *
  425. * \return Next two bytes in the currently selected endpoint's FIFO buffer.
  426. */
  427. static inline uint16_t Endpoint_Read_16_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  428. static inline uint16_t Endpoint_Read_16_LE(void)
  429. {
  430. uint16_t Byte0 = Endpoint_Read_8();
  431. uint16_t Byte1 = Endpoint_Read_8();
  432. return ((Byte1 << 8) | Byte0);
  433. }
  434. /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
  435. * direction endpoints.
  436. *
  437. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  438. *
  439. * \return Next two bytes in the currently selected endpoint's FIFO buffer.
  440. */
  441. static inline uint16_t Endpoint_Read_16_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  442. static inline uint16_t Endpoint_Read_16_BE(void)
  443. {
  444. uint16_t Byte0 = Endpoint_Read_8();
  445. uint16_t Byte1 = Endpoint_Read_8();
  446. return ((Byte0 << 8) | Byte1);
  447. }
  448. /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
  449. * direction endpoints.
  450. *
  451. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  452. *
  453. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  454. */
  455. static inline void Endpoint_Write_16_LE(const uint16_t Data) ATTR_ALWAYS_INLINE;
  456. static inline void Endpoint_Write_16_LE(const uint16_t Data)
  457. {
  458. Endpoint_Write_8(Data & 0xFF);
  459. Endpoint_Write_8(Data >> 8);
  460. }
  461. /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
  462. * direction endpoints.
  463. *
  464. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  465. *
  466. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  467. */
  468. static inline void Endpoint_Write_16_BE(const uint16_t Data) ATTR_ALWAYS_INLINE;
  469. static inline void Endpoint_Write_16_BE(const uint16_t Data)
  470. {
  471. Endpoint_Write_8(Data >> 8);
  472. Endpoint_Write_8(Data & 0xFF);
  473. }
  474. /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
  475. *
  476. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  477. */
  478. static inline void Endpoint_Discard_16(void) ATTR_ALWAYS_INLINE;
  479. static inline void Endpoint_Discard_16(void)
  480. {
  481. Endpoint_Discard_8();
  482. Endpoint_Discard_8();
  483. }
  484. /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
  485. * direction endpoints.
  486. *
  487. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  488. *
  489. * \return Next four bytes in the currently selected endpoint's FIFO buffer.
  490. */
  491. static inline uint32_t Endpoint_Read_32_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  492. static inline uint32_t Endpoint_Read_32_LE(void)
  493. {
  494. uint32_t Byte0 = Endpoint_Read_8();
  495. uint32_t Byte1 = Endpoint_Read_8();
  496. uint32_t Byte2 = Endpoint_Read_8();
  497. uint32_t Byte3 = Endpoint_Read_8();
  498. return ((Byte3 << 24) | (Byte2 << 16) | (Byte1 << 8) | Byte0);
  499. }
  500. /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
  501. * direction endpoints.
  502. *
  503. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  504. *
  505. * \return Next four bytes in the currently selected endpoint's FIFO buffer.
  506. */
  507. static inline uint32_t Endpoint_Read_32_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  508. static inline uint32_t Endpoint_Read_32_BE(void)
  509. {
  510. uint32_t Byte0 = Endpoint_Read_8();
  511. uint32_t Byte1 = Endpoint_Read_8();
  512. uint32_t Byte2 = Endpoint_Read_8();
  513. uint32_t Byte3 = Endpoint_Read_8();
  514. return ((Byte0 << 24) | (Byte1 << 16) | (Byte2 << 8) | Byte3);
  515. }
  516. /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
  517. * direction endpoints.
  518. *
  519. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  520. *
  521. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  522. */
  523. static inline void Endpoint_Write_32_LE(const uint32_t Data) ATTR_ALWAYS_INLINE;
  524. static inline void Endpoint_Write_32_LE(const uint32_t Data)
  525. {
  526. Endpoint_Write_8(Data & 0xFF);
  527. Endpoint_Write_8(Data >> 8);
  528. Endpoint_Write_8(Data >> 16);
  529. Endpoint_Write_8(Data >> 24);
  530. }
  531. /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
  532. * direction endpoints.
  533. *
  534. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  535. *
  536. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  537. */
  538. static inline void Endpoint_Write_32_BE(const uint32_t Data) ATTR_ALWAYS_INLINE;
  539. static inline void Endpoint_Write_32_BE(const uint32_t Data)
  540. {
  541. Endpoint_Write_8(Data >> 24);
  542. Endpoint_Write_8(Data >> 16);
  543. Endpoint_Write_8(Data >> 8);
  544. Endpoint_Write_8(Data & 0xFF);
  545. }
  546. /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
  547. *
  548. * \ingroup Group_EndpointPrimitiveRW_XMEGA
  549. */
  550. static inline void Endpoint_Discard_32(void) ATTR_ALWAYS_INLINE;
  551. static inline void Endpoint_Discard_32(void)
  552. {
  553. Endpoint_Discard_8();
  554. Endpoint_Discard_8();
  555. Endpoint_Discard_8();
  556. Endpoint_Discard_8();
  557. }
  558. /* External Variables: */
  559. /** Global indicating the maximum packet size of the default control endpoint located at address
  560. * 0 in the device. This value is set to the value indicated in the device descriptor in the user
  561. * project once the USB interface is initialized into device mode.
  562. *
  563. * If space is an issue, it is possible to fix this to a static value by defining the control
  564. * endpoint size in the \c FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
  565. * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
  566. * read from the descriptors at runtime and instead fixed to the given value. When used, it is
  567. * important that the descriptor control endpoint size value matches the size given as the
  568. * \c FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the \c FIXED_CONTROL_ENDPOINT_SIZE token
  569. * be used in the device descriptors to ensure this.
  570. *
  571. * \attention This variable should be treated as read-only in the user application, and never manually
  572. * changed in value.
  573. */
  574. #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
  575. extern uint8_t USB_Device_ControlEndpointSize;
  576. #else
  577. #define USB_Device_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
  578. #endif
  579. /* Function Prototypes: */
  580. /** Configures a table of endpoint descriptions, in sequence. This function can be used to configure multiple
  581. * endpoints at the same time.
  582. *
  583. * \note Endpoints with a zero address will be ignored, thus this function cannot be used to configure the
  584. * control endpoint.
  585. *
  586. * \param[in] Table Pointer to a table of endpoint descriptions.
  587. * \param[in] Entries Number of entries in the endpoint table to configure.
  588. *
  589. * \return Boolean \c true if all endpoints configured successfully, \c false otherwise.
  590. */
  591. bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
  592. const uint8_t Entries);
  593. /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
  594. * with respect to the data direction. This is a convenience function which can be used to
  595. * simplify user control request handling.
  596. *
  597. * \note This routine should not be called on non CONTROL type endpoints.
  598. */
  599. void Endpoint_ClearStatusStage(void);
  600. /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data
  601. * to be read or written to it.
  602. *
  603. * \note This routine should not be called on CONTROL type endpoints.
  604. *
  605. * \ingroup Group_EndpointRW_XMEGA
  606. *
  607. * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
  608. */
  609. uint8_t Endpoint_WaitUntilReady(void);
  610. /* Disable C linkage for C++ Compilers: */
  611. #if defined(__cplusplus)
  612. }
  613. #endif
  614. #endif
  615. /** @} */