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_AVR8.h 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 AVR8 microcontrollers.
  28. * \copydetails Group_EndpointManagement_AVR8
  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_AVR8 Endpoint Data Reading and Writing (AVR8)
  35. * \brief Endpoint data read/write definitions for the Atmel AVR8 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_AVR8 Read/Write of Primitive Data Types (AVR8)
  41. * \brief Endpoint primitive read/write definitions for the Atmel AVR8 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_AVR8 Endpoint Packet Management (AVR8)
  48. * \brief Endpoint packet management definitions for the Atmel AVR8 architecture.
  49. *
  50. * Functions, macros, variables, enums and types related to packet management of endpoints.
  51. */
  52. /** \ingroup Group_EndpointManagement
  53. * \defgroup Group_EndpointManagement_AVR8 Endpoint Management (AVR8)
  54. * \brief Endpoint management definitions for the Atmel AVR8 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_AVR8_H__
  63. #define __ENDPOINT_AVR8_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. /* Inline Functions: */
  79. static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST
  80. ATTR_ALWAYS_INLINE;
  81. static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)
  82. {
  83. uint8_t MaskVal = 0;
  84. uint16_t CheckBytes = 8;
  85. while (CheckBytes < Bytes)
  86. {
  87. MaskVal++;
  88. CheckBytes <<= 1;
  89. }
  90. return (MaskVal << EPSIZE0);
  91. }
  92. /* Function Prototypes: */
  93. void Endpoint_ClearEndpoints(void);
  94. bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
  95. const uint8_t UECFG0XData,
  96. const uint8_t UECFG1XData);
  97. #endif
  98. /* Public Interface - May be used in end-application: */
  99. /* Macros: */
  100. #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
  101. /** Default size of the default control endpoint's bank, until altered by the control endpoint bank size
  102. * value in the device descriptor. Not available if the \c FIXED_CONTROL_ENDPOINT_SIZE token is defined.
  103. */
  104. #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
  105. #endif
  106. #if !defined(CONTROL_ONLY_DEVICE) || defined(__DOXYGEN__)
  107. #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__)
  108. /** Total number of endpoints (including the default control endpoint at address 0) which may
  109. * be used in the device. Different USB AVR models support different amounts of endpoints,
  110. * this value reflects the maximum number of endpoints for the currently selected AVR model.
  111. */
  112. #define ENDPOINT_TOTAL_ENDPOINTS 7
  113. #else
  114. #define ENDPOINT_TOTAL_ENDPOINTS 5
  115. #endif
  116. #else
  117. #define ENDPOINT_TOTAL_ENDPOINTS 1
  118. #endif
  119. /* Enums: */
  120. /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
  121. *
  122. * \ingroup Group_EndpointRW_AVR8
  123. */
  124. enum Endpoint_WaitUntilReady_ErrorCodes_t
  125. {
  126. ENDPOINT_READYWAIT_NoError = 0, /**< Endpoint is ready for next packet, no error. */
  127. ENDPOINT_READYWAIT_EndpointStalled = 1, /**< The endpoint was stalled during the stream
  128. * transfer by the host or device.
  129. */
  130. ENDPOINT_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while
  131. * waiting for the endpoint to become ready.
  132. */
  133. ENDPOINT_READYWAIT_BusSuspended = 3, /**< The USB bus has been suspended by the host and
  134. * no USB endpoint traffic can occur until the bus
  135. * has resumed.
  136. */
  137. ENDPOINT_READYWAIT_Timeout = 4, /**< The host failed to accept or send the next packet
  138. * within the software timeout period set by the
  139. * \ref USB_STREAM_TIMEOUT_MS macro.
  140. */
  141. };
  142. /* Inline Functions: */
  143. /** Configures the specified endpoint address with the given endpoint type, bank size and number of hardware
  144. * banks. Once configured, the endpoint may be read from or written to, depending on its direction.
  145. *
  146. * \param[in] Address Endpoint address to configure.
  147. *
  148. * \param[in] Type Type of endpoint to configure, a \c EP_TYPE_* mask. Not all endpoint types
  149. * are available on Low Speed USB devices - refer to the USB 2.0 specification.
  150. *
  151. * \param[in] Size Size of the endpoint's bank, where packets are stored before they are transmitted
  152. * to the USB host, or after they have been received from the USB host (depending on
  153. * the endpoint's data direction). The bank size must indicate the maximum packet size
  154. * that the endpoint can handle.
  155. *
  156. * \param[in] Banks Number of banks to use for the endpoint being configured.
  157. *
  158. * \attention When the \c ORDERED_EP_CONFIG compile time option is used, Endpoints <b>must</b> be configured in
  159. * ascending order, or bank corruption will occur.
  160. *
  161. * \note Different endpoints may have different maximum packet sizes based on the endpoint's index - please
  162. * refer to the chosen microcontroller model's datasheet to determine the maximum bank size for each endpoint.
  163. * \n\n
  164. *
  165. * \note The default control endpoint should not be manually configured by the user application, as
  166. * it is automatically configured by the library internally.
  167. * \n\n
  168. *
  169. * \note This routine will automatically select the specified endpoint upon success. Upon failure, the endpoint
  170. * which failed to reconfigure correctly will be selected.
  171. *
  172. * \return Boolean \c true if the configuration succeeded, \c false otherwise.
  173. */
  174. static inline bool Endpoint_ConfigureEndpoint(const uint8_t Address,
  175. const uint8_t Type,
  176. const uint16_t Size,
  177. const uint8_t Banks) ATTR_ALWAYS_INLINE;
  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)
  182. {
  183. uint8_t Number = (Address & ENDPOINT_EPNUM_MASK);
  184. if (Number >= ENDPOINT_TOTAL_ENDPOINTS)
  185. return false;
  186. return Endpoint_ConfigureEndpoint_Prv(Number,
  187. ((Type << EPTYPE0) | ((Address & ENDPOINT_DIR_IN) ? (1 << EPDIR) : 0)),
  188. ((1 << ALLOC) | ((Banks > 1) ? (1 << EPBK0) : 0) | Endpoint_BytesToEPSizeMask(Size)));
  189. }
  190. /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
  191. *
  192. * \ingroup Group_EndpointRW_AVR8
  193. *
  194. * \return Total number of bytes in the currently selected Endpoint's FIFO buffer.
  195. */
  196. static inline uint16_t Endpoint_BytesInEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  197. static inline uint16_t Endpoint_BytesInEndpoint(void)
  198. {
  199. #if (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
  200. return UEBCX;
  201. #elif defined(USB_SERIES_4_AVR)
  202. return (((uint16_t)UEBCHX << 8) | UEBCLX);
  203. #elif defined(USB_SERIES_2_AVR)
  204. return UEBCLX;
  205. #endif
  206. }
  207. /** Determines the currently selected endpoint's direction.
  208. *
  209. * \return The currently selected endpoint's direction, as a \c ENDPOINT_DIR_* mask.
  210. */
  211. static inline uint8_t Endpoint_GetEndpointDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  212. static inline uint8_t Endpoint_GetEndpointDirection(void)
  213. {
  214. return (UECFG0X & (1 << EPDIR)) ? ENDPOINT_DIR_IN : ENDPOINT_DIR_OUT;
  215. }
  216. /** Get the endpoint address of the currently selected endpoint. This is typically used to save
  217. * the currently selected endpoint so that it can be restored after another endpoint has been
  218. * manipulated.
  219. *
  220. * \return Index of the currently selected endpoint.
  221. */
  222. static inline uint8_t Endpoint_GetCurrentEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  223. static inline uint8_t Endpoint_GetCurrentEndpoint(void)
  224. {
  225. #if !defined(CONTROL_ONLY_DEVICE)
  226. return ((UENUM & ENDPOINT_EPNUM_MASK) | Endpoint_GetEndpointDirection());
  227. #else
  228. return ENDPOINT_CONTROLEP;
  229. #endif
  230. }
  231. /** Selects the given endpoint address.
  232. *
  233. * Any endpoint operations which do not require the endpoint address to be indicated will operate on
  234. * the currently selected endpoint.
  235. *
  236. * \param[in] Address Endpoint address to select.
  237. */
  238. static inline void Endpoint_SelectEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;
  239. static inline void Endpoint_SelectEndpoint(const uint8_t Address)
  240. {
  241. #if !defined(CONTROL_ONLY_DEVICE)
  242. UENUM = (Address & ENDPOINT_EPNUM_MASK);
  243. #endif
  244. }
  245. /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
  246. * data In and Out pointers to the bank's contents.
  247. *
  248. * \param[in] Address Endpoint address whose FIFO buffers are to be reset.
  249. */
  250. static inline void Endpoint_ResetEndpoint(const uint8_t Address) ATTR_ALWAYS_INLINE;
  251. static inline void Endpoint_ResetEndpoint(const uint8_t Address)
  252. {
  253. UERST = (1 << (Address & ENDPOINT_EPNUM_MASK));
  254. UERST = 0;
  255. }
  256. /** Enables the currently selected endpoint so that data can be sent and received through it to
  257. * and from a host.
  258. *
  259. * \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().
  260. */
  261. static inline void Endpoint_EnableEndpoint(void) ATTR_ALWAYS_INLINE;
  262. static inline void Endpoint_EnableEndpoint(void)
  263. {
  264. UECONX |= (1 << EPEN);
  265. }
  266. /** Disables the currently selected endpoint so that data cannot be sent and received through it
  267. * to and from a host.
  268. */
  269. static inline void Endpoint_DisableEndpoint(void) ATTR_ALWAYS_INLINE;
  270. static inline void Endpoint_DisableEndpoint(void)
  271. {
  272. UECONX &= ~(1 << EPEN);
  273. }
  274. /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
  275. *
  276. * \return Boolean \c true if the currently selected endpoint is enabled, \c false otherwise.
  277. */
  278. static inline bool Endpoint_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  279. static inline bool Endpoint_IsEnabled(void)
  280. {
  281. return ((UECONX & (1 << EPEN)) ? true : false);
  282. }
  283. /** Retrieves the number of busy banks in the currently selected endpoint, which have been queued for
  284. * transmission via the \ref Endpoint_ClearIN() command, or are awaiting acknowledgment via the
  285. * \ref Endpoint_ClearOUT() command.
  286. *
  287. * \ingroup Group_EndpointPacketManagement_AVR8
  288. *
  289. * \return Total number of busy banks in the selected endpoint.
  290. */
  291. static inline uint8_t Endpoint_GetBusyBanks(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  292. static inline uint8_t Endpoint_GetBusyBanks(void)
  293. {
  294. return (UESTA0X & (0x03 << NBUSYBK0));
  295. }
  296. /** Aborts all pending IN transactions on the currently selected endpoint, once the bank
  297. * has been queued for transmission to the host via \ref Endpoint_ClearIN(). This function
  298. * will terminate all queued transactions, resetting the endpoint banks ready for a new
  299. * packet.
  300. *
  301. * \ingroup Group_EndpointPacketManagement_AVR8
  302. */
  303. static inline void Endpoint_AbortPendingIN(void)
  304. {
  305. while (Endpoint_GetBusyBanks() != 0)
  306. {
  307. UEINTX |= (1 << RXOUTI);
  308. while (UEINTX & (1 << RXOUTI));
  309. }
  310. }
  311. /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
  312. * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
  313. * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
  314. * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
  315. * direction and the endpoint bank is full.
  316. *
  317. * \ingroup Group_EndpointPacketManagement_AVR8
  318. *
  319. * \return Boolean \c true if the currently selected endpoint may be read from or written to, depending
  320. * on its direction.
  321. */
  322. static inline bool Endpoint_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  323. static inline bool Endpoint_IsReadWriteAllowed(void)
  324. {
  325. return ((UEINTX & (1 << RWAL)) ? true : false);
  326. }
  327. /** Determines if the currently selected endpoint is configured.
  328. *
  329. * \return Boolean \c true if the currently selected endpoint has been configured, \c false otherwise.
  330. */
  331. static inline bool Endpoint_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  332. static inline bool Endpoint_IsConfigured(void)
  333. {
  334. return ((UESTA0X & (1 << CFGOK)) ? true : false);
  335. }
  336. /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
  337. * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
  338. * masking the return value against <tt>(1 << <i>{Endpoint Number}</i>)</tt>.
  339. *
  340. * \return Mask whose bits indicate which endpoints have interrupted.
  341. */
  342. static inline uint8_t Endpoint_GetEndpointInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  343. static inline uint8_t Endpoint_GetEndpointInterrupts(void)
  344. {
  345. return UEINT;
  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() & (1 << (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_AVR8
  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 ((UEINTX & (1 << TXINI)) ? true : false);
  369. }
  370. /** Determines if the selected OUT endpoint has received new packet from the host.
  371. *
  372. * \ingroup Group_EndpointPacketManagement_AVR8
  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 ((UEINTX & (1 << RXOUTI)) ? true : false);
  380. }
  381. /** Determines if the current CONTROL type endpoint has received a SETUP packet.
  382. *
  383. * \ingroup Group_EndpointPacketManagement_AVR8
  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 ((UEINTX & (1 << RXSTPI)) ? true : false);
  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_AVR8
  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. UEINTX &= ~(1 << RXSTPI);
  403. }
  404. /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
  405. * next packet and switching to the alternative endpoint bank if double banked.
  406. *
  407. * \ingroup Group_EndpointPacketManagement_AVR8
  408. */
  409. static inline void Endpoint_ClearIN(void) ATTR_ALWAYS_INLINE;
  410. static inline void Endpoint_ClearIN(void)
  411. {
  412. #if !defined(CONTROL_ONLY_DEVICE)
  413. UEINTX &= ~((1 << TXINI) | (1 << FIFOCON));
  414. #else
  415. UEINTX &= ~(1 << TXINI);
  416. #endif
  417. }
  418. /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
  419. * for the next packet and switching to the alternative endpoint bank if double banked.
  420. *
  421. * \ingroup Group_EndpointPacketManagement_AVR8
  422. */
  423. static inline void Endpoint_ClearOUT(void) ATTR_ALWAYS_INLINE;
  424. static inline void Endpoint_ClearOUT(void)
  425. {
  426. #if !defined(CONTROL_ONLY_DEVICE)
  427. UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));
  428. #else
  429. UEINTX &= ~(1 << RXOUTI);
  430. #endif
  431. }
  432. /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
  433. * indicated endpoint and that the current transfer sequence should be aborted. This provides a
  434. * way for devices to indicate invalid commands to the host so that the current transfer can be
  435. * aborted and the host can begin its own recovery sequence.
  436. *
  437. * The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro
  438. * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
  439. * endpoint.
  440. *
  441. * \ingroup Group_EndpointPacketManagement_AVR8
  442. */
  443. static inline void Endpoint_StallTransaction(void) ATTR_ALWAYS_INLINE;
  444. static inline void Endpoint_StallTransaction(void)
  445. {
  446. UECONX |= (1 << STALLRQ);
  447. }
  448. /** Clears the STALL condition on the currently selected endpoint.
  449. *
  450. * \ingroup Group_EndpointPacketManagement_AVR8
  451. */
  452. static inline void Endpoint_ClearStall(void) ATTR_ALWAYS_INLINE;
  453. static inline void Endpoint_ClearStall(void)
  454. {
  455. UECONX |= (1 << STALLRQC);
  456. }
  457. /** Determines if the currently selected endpoint is stalled, \c false otherwise.
  458. *
  459. * \ingroup Group_EndpointPacketManagement_AVR8
  460. *
  461. * \return Boolean \c true if the currently selected endpoint is stalled, \c false otherwise.
  462. */
  463. static inline bool Endpoint_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  464. static inline bool Endpoint_IsStalled(void)
  465. {
  466. return ((UECONX & (1 << STALLRQ)) ? true : false);
  467. }
  468. /** Resets the data toggle of the currently selected endpoint. */
  469. static inline void Endpoint_ResetDataToggle(void) ATTR_ALWAYS_INLINE;
  470. static inline void Endpoint_ResetDataToggle(void)
  471. {
  472. UECONX |= (1 << RSTDT);
  473. }
  474. /** Sets the direction of the currently selected endpoint.
  475. *
  476. * \param[in] DirectionMask New endpoint direction, as a \c ENDPOINT_DIR_* mask.
  477. */
  478. static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask) ATTR_ALWAYS_INLINE;
  479. static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask)
  480. {
  481. UECFG0X = ((UECFG0X & ~(1 << EPDIR)) | (DirectionMask ? (1 << EPDIR) : 0));
  482. }
  483. /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
  484. *
  485. * \ingroup Group_EndpointPrimitiveRW_AVR8
  486. *
  487. * \return Next byte in the currently selected endpoint's FIFO buffer.
  488. */
  489. static inline uint8_t Endpoint_Read_8(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  490. static inline uint8_t Endpoint_Read_8(void)
  491. {
  492. return UEDATX;
  493. }
  494. /** Writes one byte to the currently selected endpoint's bank, for IN direction endpoints.
  495. *
  496. * \ingroup Group_EndpointPrimitiveRW_AVR8
  497. *
  498. * \param[in] Data Data to write into the the currently selected endpoint's FIFO buffer.
  499. */
  500. static inline void Endpoint_Write_8(const uint8_t Data) ATTR_ALWAYS_INLINE;
  501. static inline void Endpoint_Write_8(const uint8_t Data)
  502. {
  503. UEDATX = Data;
  504. }
  505. /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
  506. *
  507. * \ingroup Group_EndpointPrimitiveRW_AVR8
  508. */
  509. static inline void Endpoint_Discard_8(void) ATTR_ALWAYS_INLINE;
  510. static inline void Endpoint_Discard_8(void)
  511. {
  512. uint8_t Dummy;
  513. Dummy = UEDATX;
  514. (void)Dummy;
  515. }
  516. /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
  517. * direction endpoints.
  518. *
  519. * \ingroup Group_EndpointPrimitiveRW_AVR8
  520. *
  521. * \return Next two bytes in the currently selected endpoint's FIFO buffer.
  522. */
  523. static inline uint16_t Endpoint_Read_16_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  524. static inline uint16_t Endpoint_Read_16_LE(void)
  525. {
  526. union
  527. {
  528. uint16_t Value;
  529. uint8_t Bytes[2];
  530. } Data;
  531. Data.Bytes[0] = UEDATX;
  532. Data.Bytes[1] = UEDATX;
  533. return Data.Value;
  534. }
  535. /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
  536. * direction endpoints.
  537. *
  538. * \ingroup Group_EndpointPrimitiveRW_AVR8
  539. *
  540. * \return Next two bytes in the currently selected endpoint's FIFO buffer.
  541. */
  542. static inline uint16_t Endpoint_Read_16_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  543. static inline uint16_t Endpoint_Read_16_BE(void)
  544. {
  545. union
  546. {
  547. uint16_t Value;
  548. uint8_t Bytes[2];
  549. } Data;
  550. Data.Bytes[1] = UEDATX;
  551. Data.Bytes[0] = UEDATX;
  552. return Data.Value;
  553. }
  554. /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
  555. * direction endpoints.
  556. *
  557. * \ingroup Group_EndpointPrimitiveRW_AVR8
  558. *
  559. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  560. */
  561. static inline void Endpoint_Write_16_LE(const uint16_t Data) ATTR_ALWAYS_INLINE;
  562. static inline void Endpoint_Write_16_LE(const uint16_t Data)
  563. {
  564. UEDATX = (Data & 0xFF);
  565. UEDATX = (Data >> 8);
  566. }
  567. /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
  568. * direction endpoints.
  569. *
  570. * \ingroup Group_EndpointPrimitiveRW_AVR8
  571. *
  572. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  573. */
  574. static inline void Endpoint_Write_16_BE(const uint16_t Data) ATTR_ALWAYS_INLINE;
  575. static inline void Endpoint_Write_16_BE(const uint16_t Data)
  576. {
  577. UEDATX = (Data >> 8);
  578. UEDATX = (Data & 0xFF);
  579. }
  580. /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
  581. *
  582. * \ingroup Group_EndpointPrimitiveRW_AVR8
  583. */
  584. static inline void Endpoint_Discard_16(void) ATTR_ALWAYS_INLINE;
  585. static inline void Endpoint_Discard_16(void)
  586. {
  587. uint8_t Dummy;
  588. Dummy = UEDATX;
  589. Dummy = UEDATX;
  590. (void)Dummy;
  591. }
  592. /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
  593. * direction endpoints.
  594. *
  595. * \ingroup Group_EndpointPrimitiveRW_AVR8
  596. *
  597. * \return Next four bytes in the currently selected endpoint's FIFO buffer.
  598. */
  599. static inline uint32_t Endpoint_Read_32_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  600. static inline uint32_t Endpoint_Read_32_LE(void)
  601. {
  602. union
  603. {
  604. uint32_t Value;
  605. uint8_t Bytes[4];
  606. } Data;
  607. Data.Bytes[0] = UEDATX;
  608. Data.Bytes[1] = UEDATX;
  609. Data.Bytes[2] = UEDATX;
  610. Data.Bytes[3] = UEDATX;
  611. return Data.Value;
  612. }
  613. /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
  614. * direction endpoints.
  615. *
  616. * \ingroup Group_EndpointPrimitiveRW_AVR8
  617. *
  618. * \return Next four bytes in the currently selected endpoint's FIFO buffer.
  619. */
  620. static inline uint32_t Endpoint_Read_32_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  621. static inline uint32_t Endpoint_Read_32_BE(void)
  622. {
  623. union
  624. {
  625. uint32_t Value;
  626. uint8_t Bytes[4];
  627. } Data;
  628. Data.Bytes[3] = UEDATX;
  629. Data.Bytes[2] = UEDATX;
  630. Data.Bytes[1] = UEDATX;
  631. Data.Bytes[0] = UEDATX;
  632. return Data.Value;
  633. }
  634. /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
  635. * direction endpoints.
  636. *
  637. * \ingroup Group_EndpointPrimitiveRW_AVR8
  638. *
  639. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  640. */
  641. static inline void Endpoint_Write_32_LE(const uint32_t Data) ATTR_ALWAYS_INLINE;
  642. static inline void Endpoint_Write_32_LE(const uint32_t Data)
  643. {
  644. UEDATX = (Data & 0xFF);
  645. UEDATX = (Data >> 8);
  646. UEDATX = (Data >> 16);
  647. UEDATX = (Data >> 24);
  648. }
  649. /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
  650. * direction endpoints.
  651. *
  652. * \ingroup Group_EndpointPrimitiveRW_AVR8
  653. *
  654. * \param[in] Data Data to write to the currently selected endpoint's FIFO buffer.
  655. */
  656. static inline void Endpoint_Write_32_BE(const uint32_t Data) ATTR_ALWAYS_INLINE;
  657. static inline void Endpoint_Write_32_BE(const uint32_t Data)
  658. {
  659. UEDATX = (Data >> 24);
  660. UEDATX = (Data >> 16);
  661. UEDATX = (Data >> 8);
  662. UEDATX = (Data & 0xFF);
  663. }
  664. /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
  665. *
  666. * \ingroup Group_EndpointPrimitiveRW_AVR8
  667. */
  668. static inline void Endpoint_Discard_32(void) ATTR_ALWAYS_INLINE;
  669. static inline void Endpoint_Discard_32(void)
  670. {
  671. uint8_t Dummy;
  672. Dummy = UEDATX;
  673. Dummy = UEDATX;
  674. Dummy = UEDATX;
  675. Dummy = UEDATX;
  676. (void)Dummy;
  677. }
  678. /* External Variables: */
  679. /** Global indicating the maximum packet size of the default control endpoint located at address
  680. * 0 in the device. This value is set to the value indicated in the device descriptor in the user
  681. * project once the USB interface is initialized into device mode.
  682. *
  683. * If space is an issue, it is possible to fix this to a static value by defining the control
  684. * endpoint size in the \c FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
  685. * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
  686. * read from the descriptors at runtime and instead fixed to the given value. When used, it is
  687. * important that the descriptor control endpoint size value matches the size given as the
  688. * \c FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the \c FIXED_CONTROL_ENDPOINT_SIZE token
  689. * be used in the device descriptors to ensure this.
  690. *
  691. * \attention This variable should be treated as read-only in the user application, and never manually
  692. * changed in value.
  693. */
  694. #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
  695. extern uint8_t USB_Device_ControlEndpointSize;
  696. #else
  697. #define USB_Device_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
  698. #endif
  699. /* Function Prototypes: */
  700. /** Configures a table of endpoint descriptions, in sequence. This function can be used to configure multiple
  701. * endpoints at the same time.
  702. *
  703. * \note Endpoints with a zero address will be ignored, thus this function cannot be used to configure the
  704. * control endpoint.
  705. *
  706. * \param[in] Table Pointer to a table of endpoint descriptions.
  707. * \param[in] Entries Number of entries in the endpoint table to configure.
  708. *
  709. * \return Boolean \c true if all endpoints configured successfully, \c false otherwise.
  710. */
  711. bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
  712. const uint8_t Entries);
  713. /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
  714. * with respect to the data direction. This is a convenience function which can be used to
  715. * simplify user control request handling.
  716. *
  717. * \note This routine should not be called on non CONTROL type endpoints.
  718. */
  719. void Endpoint_ClearStatusStage(void);
  720. /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data
  721. * to be read or written to it.
  722. *
  723. * \note This routine should not be called on CONTROL type endpoints.
  724. *
  725. * \ingroup Group_EndpointRW_AVR8
  726. *
  727. * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
  728. */
  729. uint8_t Endpoint_WaitUntilReady(void);
  730. /* Disable C linkage for C++ Compilers: */
  731. #if defined(__cplusplus)
  732. }
  733. #endif
  734. #endif
  735. /** @} */