nrf_drv_usbd.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(USBD)
  42. #include "nrf_drv_usbd.h"
  43. #include "nrf.h"
  44. #include "nrf_atomic.h"
  45. #include "app_util_platform.h"
  46. #include "nrf_delay.h"
  47. #include <stdbool.h>
  48. #include <string.h>
  49. #include <inttypes.h>
  50. #define NRF_LOG_MODULE_NAME USBD
  51. #if USBD_CONFIG_LOG_ENABLED
  52. #define NRF_LOG_LEVEL USBD_CONFIG_LOG_LEVEL
  53. #define NRF_LOG_INFO_COLOR USBD_CONFIG_INFO_COLOR
  54. #define NRF_LOG_DEBUG_COLOR USBD_CONFIG_DEBUG_COLOR
  55. #else //USBD_CONFIG_LOG_ENABLED
  56. #define NRF_LOG_LEVEL 0
  57. #endif //USBD_CONFIG_LOG_ENABLED
  58. #include "nrf_log.h"
  59. NRF_LOG_MODULE_REGISTER();
  60. #ifndef NRF_DRV_USBD_EARLY_DMA_PROCESS
  61. /* Try to process DMA request when endpoint transmission has been detected
  62. * and just after last EasyDMA has been processed.
  63. * It speeds up the transmission a little (about 10% measured)
  64. * with a cost of more CPU power used.
  65. */
  66. #define NRF_DRV_USBD_EARLY_DMA_PROCESS 1
  67. #endif
  68. #ifndef NRF_DRV_USBD_PROTO1_FIX_DEBUG
  69. /* Debug information when events are fixed*/
  70. #define NRF_DRV_USBD_PROTO1_FIX_DEBUG 1
  71. #endif
  72. #define NRF_DRV_USBD_LOG_PROTO1_FIX_PRINTF(...) \
  73. do{ \
  74. if (NRF_DRV_USBD_PROTO1_FIX_DEBUG){ NRF_LOG_DEBUG(__VA_ARGS__); }\
  75. } while (0)
  76. #ifndef NRF_DRV_USBD_STARTED_EV_ENABLE
  77. #define NRF_DRV_USBD_STARTED_EV_ENABLE 0
  78. #endif
  79. #ifndef USBD_CONFIG_ISO_IN_ZLP
  80. #define USBD_CONFIG_ISO_IN_ZLP 0
  81. #endif
  82. #ifndef NRF_USBD_ISO_DEBUG
  83. /* Also generate information about ISOCHRONOUS events and transfers.
  84. * Turn this off if no ISOCHRONOUS transfers are going to be debugged and this
  85. * option generates a lot of useless messages. */
  86. #define NRF_USBD_ISO_DEBUG 1
  87. #endif
  88. #ifndef NRF_USBD_FAILED_TRANSFERS_DEBUG
  89. /* Also generate debug information for failed transfers.
  90. * It might be useful but may generate a lot of useless debug messages
  91. * in some library usages (for example when transfer is generated and the
  92. * result is used to check whatever endpoint was busy. */
  93. #define NRF_USBD_FAILED_TRANSFERS_DEBUG 1
  94. #endif
  95. #ifndef NRF_USBD_DMAREQ_PROCESS_DEBUG
  96. /* Generate additional messages that mark the status inside
  97. * @ref usbd_dmareq_process.
  98. * It is useful to debug library internals but may generate a lot of
  99. * useless debug messages. */
  100. #define NRF_USBD_DMAREQ_PROCESS_DEBUG 1
  101. #endif
  102. /**
  103. * @defgroup nrf_drv_usbd_int USB Device driver internal part
  104. * @internal
  105. * @ingroup nrf_drv_usbd
  106. *
  107. * This part contains auxiliary internal macros, variables and functions.
  108. * @{
  109. */
  110. /**
  111. * @brief Assert endpoint number validity
  112. *
  113. * Internal macro to be used during program creation in debug mode.
  114. * Generates assertion if endpoint number is not valid.
  115. *
  116. * @param ep Endpoint number to validity check
  117. */
  118. #define USBD_ASSERT_EP_VALID(ep) ASSERT( \
  119. ((NRF_USBD_EPIN_CHECK(ep) && (NRF_USBD_EP_NR_GET(ep) < NRF_USBD_EPIN_CNT )) \
  120. || \
  121. (NRF_USBD_EPOUT_CHECK(ep) && (NRF_USBD_EP_NR_GET(ep) < NRF_USBD_EPOUT_CNT))) \
  122. );
  123. /**
  124. * @brief Lowest position of bit for IN endpoint
  125. *
  126. * The first bit position corresponding to IN endpoint.
  127. * @sa ep2bit bit2ep
  128. */
  129. #define USBD_EPIN_BITPOS_0 0
  130. /**
  131. * @brief Lowest position of bit for OUT endpoint
  132. *
  133. * The first bit position corresponding to OUT endpoint
  134. * @sa ep2bit bit2ep
  135. */
  136. #define USBD_EPOUT_BITPOS_0 16
  137. /**
  138. * @brief Input endpoint bits mask
  139. */
  140. #define USBD_EPIN_BIT_MASK (0xFFFFU << USBD_EPIN_BITPOS_0)
  141. /**
  142. * @brief Output endpoint bits mask
  143. */
  144. #define USBD_EPOUT_BIT_MASK (0xFFFFU << USBD_EPOUT_BITPOS_0)
  145. /**
  146. * @brief Isochronous endpoint bit mask
  147. */
  148. #define USBD_EPISO_BIT_MASK \
  149. ((1U << USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT8)) | (1U << USBD_EP_BITPOS(NRF_DRV_USBD_EPIN8)))
  150. /**
  151. * @brief Auxiliary macro to change EP number into bit position
  152. *
  153. * This macro is used by @ref ep2bit function but also for statically check
  154. * the bitpos values integrity during compilation.
  155. *
  156. * @param[in] ep Endpoint number.
  157. * @return Endpoint bit position.
  158. */
  159. #define USBD_EP_BITPOS(ep) \
  160. ((NRF_USBD_EPIN_CHECK(ep) ? USBD_EPIN_BITPOS_0 : USBD_EPOUT_BITPOS_0) + NRF_USBD_EP_NR_GET(ep))
  161. /**
  162. * @brief Helper macro for creating an endpoint transfer event.
  163. *
  164. * @param[in] name Name of the created transfer event variable.
  165. * @param[in] endpoint Endpoint number.
  166. * @param[in] ep_stat Endpoint state to report.
  167. *
  168. * @return Initialized event constant variable.
  169. */
  170. #define NRF_DRV_USBD_EP_TRANSFER_EVENT(name, endpont, ep_stat) \
  171. const nrf_drv_usbd_evt_t name = { \
  172. NRF_DRV_USBD_EVT_EPTRANSFER, \
  173. .data = { \
  174. .eptransfer = { \
  175. .ep = endpont, \
  176. .status = ep_stat \
  177. } \
  178. } \
  179. }
  180. /* Check it the bit positions values match defined DATAEPSTATUS bit positions */
  181. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPIN1) == USBD_EPDATASTATUS_EPIN1_Pos );
  182. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPIN2) == USBD_EPDATASTATUS_EPIN2_Pos );
  183. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPIN3) == USBD_EPDATASTATUS_EPIN3_Pos );
  184. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPIN4) == USBD_EPDATASTATUS_EPIN4_Pos );
  185. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPIN5) == USBD_EPDATASTATUS_EPIN5_Pos );
  186. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPIN6) == USBD_EPDATASTATUS_EPIN6_Pos );
  187. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPIN7) == USBD_EPDATASTATUS_EPIN7_Pos );
  188. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT1) == USBD_EPDATASTATUS_EPOUT1_Pos);
  189. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT2) == USBD_EPDATASTATUS_EPOUT2_Pos);
  190. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT3) == USBD_EPDATASTATUS_EPOUT3_Pos);
  191. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT4) == USBD_EPDATASTATUS_EPOUT4_Pos);
  192. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT5) == USBD_EPDATASTATUS_EPOUT5_Pos);
  193. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT6) == USBD_EPDATASTATUS_EPOUT6_Pos);
  194. STATIC_ASSERT(USBD_EP_BITPOS(NRF_DRV_USBD_EPOUT7) == USBD_EPDATASTATUS_EPOUT7_Pos);
  195. /**
  196. * @name Internal auxiliary definitions for SETUP packet
  197. *
  198. * Definitions used to take out the information about last SETUP packet direction
  199. * from @c bmRequestType.
  200. * @{
  201. */
  202. /** The position of DIR bit in bmRequestType inside SETUP packet */
  203. #define USBD_DRV_REQUESTTYPE_DIR_BITPOS 7
  204. /** The mask of DIR bit in bmRequestType inside SETUP packet */
  205. #define USBD_DRV_REQUESTTYPE_DIR_MASK (1U << USBD_DRV_REQUESTTYPE_DIR_BITPOS)
  206. /** The value of DIR bit for OUT direction (Host -> Device) */
  207. #define USBD_DRV_REQUESTTYPE_DIR_OUT (0U << USBD_DRV_REQUESTTYPE_DIR_BITPOS)
  208. /** The value of DIR bit for IN direction (Device -> Host) */
  209. #define USBD_DRV_REQUESTTYPE_DIR_IN (1U << USBD_DRV_REQUESTTYPE_DIR_BITPOS)
  210. /** @} */
  211. /**
  212. * @brief Current driver state
  213. */
  214. static nrfx_drv_state_t m_drv_state = NRFX_DRV_STATE_UNINITIALIZED;
  215. /**
  216. * @brief Event handler for the library
  217. *
  218. * Event handler that would be called on events.
  219. *
  220. * @note Currently it cannot be null if any interrupt is activated.
  221. */
  222. static nrf_drv_usbd_event_handler_t m_event_handler;
  223. /**
  224. * @brief Detected state of the bus
  225. *
  226. * Internal state changed in interrupts handling when
  227. * RESUME or SUSPEND event is processed.
  228. *
  229. * Values:
  230. * - true - bus suspended
  231. * - false - ongoing normal communication on the bus
  232. *
  233. * @note This is only the bus state and does not mean that the peripheral is in suspend state.
  234. */
  235. static volatile bool m_bus_suspend;
  236. /**
  237. * @brief Internal constant that contains interrupts disabled in suspend state
  238. *
  239. * Internal constant used in @ref nrf_drv_usbd_suspend_irq_config and @ref nrf_drv_usbd_active_irq_config
  240. * functions.
  241. */
  242. static const uint32_t m_irq_disabled_in_suspend =
  243. NRF_USBD_INT_ENDEPIN0_MASK |
  244. NRF_USBD_INT_EP0DATADONE_MASK |
  245. NRF_USBD_INT_ENDEPOUT0_MASK |
  246. NRF_USBD_INT_EP0SETUP_MASK |
  247. NRF_USBD_INT_DATAEP_MASK;
  248. /**
  249. * @brief Direction of last received Setup transfer
  250. *
  251. * This variable is used to redirect internal setup data event
  252. * into selected endpoint (IN or OUT).
  253. */
  254. static nrf_drv_usbd_ep_t m_last_setup_dir;
  255. /**
  256. * @brief Mark endpoint readiness for DMA transfer
  257. *
  258. * Bits in this variable are cleared and set in interrupts.
  259. * 1 means that endpoint is ready for DMA transfer.
  260. * 0 means that DMA transfer cannot be performed on selected endpoint.
  261. */
  262. static uint32_t m_ep_ready;
  263. /**
  264. * @brief Mark endpoint with prepared data to transfer by DMA
  265. *
  266. * This variable can be from any place in the code (interrupt or main thread).
  267. * It would be cleared only from USBD interrupt.
  268. *
  269. * Mask prepared USBD data for transmission.
  270. * It is cleared when no more data to transmit left.
  271. */
  272. static uint32_t m_ep_dma_waiting;
  273. /**
  274. * @brief Current EasyDMA state
  275. *
  276. * Single flag, updated only inside interrupts, that marks current EasyDMA state.
  277. * In USBD there is only one DMA channel working in background, and new transfer
  278. * cannot be started when there is ongoing transfer on any other channel.
  279. */
  280. static bool m_dma_pending;
  281. /**
  282. * @brief Simulated data EP status bits required for errata 104
  283. *
  284. * Marker to delete when not required anymore: >> NRF_DRV_USBD_ERRATA_ENABLE <<
  285. */
  286. static uint32_t m_simulated_dataepstatus;
  287. /**
  288. * @brief The structure that would hold transfer configuration to every endpoint
  289. *
  290. * The structure that holds all the data required by the endpoint to proceed
  291. * with LIST functionality and generate quick callback directly when data
  292. * buffer is ready.
  293. */
  294. typedef struct
  295. {
  296. nrf_drv_usbd_handler_t handler; //!< Handler for current transfer, function pointer
  297. void * p_context; //!< Context for transfer handler
  298. size_t transfer_cnt; //!< Number of transferred bytes in the current transfer
  299. uint16_t max_packet_size; //!< Configured endpoint size
  300. nrf_drv_usbd_ep_status_t status; //!< NRF_SUCCESS or error code, never NRF_ERROR_BUSY - this one is calculated
  301. }usbd_drv_ep_state_t;
  302. /**
  303. * @brief The array of transfer configurations for the endpoints.
  304. *
  305. * The status of the transfer on each endpoint.
  306. */
  307. static struct
  308. {
  309. usbd_drv_ep_state_t ep_out[NRF_USBD_EPOUT_CNT]; //!< Status for OUT endpoints.
  310. usbd_drv_ep_state_t ep_in [NRF_USBD_EPIN_CNT ]; //!< Status for IN endpoints.
  311. }m_ep_state;
  312. /**
  313. * @brief Status variables for integrated feeders.
  314. *
  315. * Current status for integrated feeders (IN transfers).
  316. * Integrated feeders are used for default transfers:
  317. * 1. Simple RAM transfer
  318. * 2. Simple flash transfer
  319. * 3. RAM transfer with automatic ZLP
  320. * 4. Flash transfer with automatic ZLP
  321. */
  322. nrf_drv_usbd_transfer_t m_ep_feeder_state[NRF_USBD_EPIN_CNT];
  323. /**
  324. * @brief Status variables for integrated consumers
  325. *
  326. * Current status for integrated consumers
  327. * Currently one type of transfer is supported:
  328. * 1. Transfer to RAM
  329. *
  330. * Transfer is finished automatically when received data block is smaller
  331. * than the endpoint buffer or all the required data is received.
  332. */
  333. nrf_drv_usbd_transfer_t m_ep_consumer_state[NRF_USBD_EPOUT_CNT];
  334. /**
  335. * @brief Buffer used to send data directly from FLASH
  336. *
  337. * This is internal buffer that would be used to emulate the possibility
  338. * to transfer data directly from FLASH.
  339. * We do not have to care about the source of data when calling transfer functions.
  340. *
  341. * We do not need more buffers that one, because only one transfer can be pending
  342. * at once.
  343. */
  344. static uint32_t m_tx_buffer[CEIL_DIV(
  345. NRF_DRV_USBD_FEEDER_BUFFER_SIZE, sizeof(uint32_t))];
  346. /* Early declaration. Documentation above definition. */
  347. static void usbd_dmareq_process(void);
  348. /**
  349. * @brief Change endpoint number to endpoint event code
  350. *
  351. * @param ep Endpoint number
  352. *
  353. * @return Connected endpoint event code.
  354. *
  355. * Marker to delete when not required anymore: >> NRF_DRV_USBD_ERRATA_ENABLE <<
  356. */
  357. static inline nrf_usbd_event_t nrf_drv_usbd_ep_to_endevent(nrf_drv_usbd_ep_t ep)
  358. {
  359. USBD_ASSERT_EP_VALID(ep);
  360. static const nrf_usbd_event_t epin_endev[] =
  361. {
  362. NRF_USBD_EVENT_ENDEPIN0,
  363. NRF_USBD_EVENT_ENDEPIN1,
  364. NRF_USBD_EVENT_ENDEPIN2,
  365. NRF_USBD_EVENT_ENDEPIN3,
  366. NRF_USBD_EVENT_ENDEPIN4,
  367. NRF_USBD_EVENT_ENDEPIN5,
  368. NRF_USBD_EVENT_ENDEPIN6,
  369. NRF_USBD_EVENT_ENDEPIN7,
  370. NRF_USBD_EVENT_ENDISOIN0
  371. };
  372. static const nrf_usbd_event_t epout_endev[] =
  373. {
  374. NRF_USBD_EVENT_ENDEPOUT0,
  375. NRF_USBD_EVENT_ENDEPOUT1,
  376. NRF_USBD_EVENT_ENDEPOUT2,
  377. NRF_USBD_EVENT_ENDEPOUT3,
  378. NRF_USBD_EVENT_ENDEPOUT4,
  379. NRF_USBD_EVENT_ENDEPOUT5,
  380. NRF_USBD_EVENT_ENDEPOUT6,
  381. NRF_USBD_EVENT_ENDEPOUT7,
  382. NRF_USBD_EVENT_ENDISOOUT0
  383. };
  384. return (NRF_USBD_EPIN_CHECK(ep) ? epin_endev : epout_endev)[NRF_USBD_EP_NR_GET(ep)];
  385. }
  386. /**
  387. * @brief Get interrupt mask for selected endpoint
  388. *
  389. * @param[in] ep Endpoint number
  390. *
  391. * @return Interrupt mask related to the EasyDMA transfer end for the
  392. * chosen endpoint.
  393. */
  394. static inline uint32_t nrf_drv_usbd_ep_to_int(nrf_drv_usbd_ep_t ep)
  395. {
  396. USBD_ASSERT_EP_VALID(ep);
  397. static const uint8_t epin_bitpos[] =
  398. {
  399. USBD_INTEN_ENDEPIN0_Pos,
  400. USBD_INTEN_ENDEPIN1_Pos,
  401. USBD_INTEN_ENDEPIN2_Pos,
  402. USBD_INTEN_ENDEPIN3_Pos,
  403. USBD_INTEN_ENDEPIN4_Pos,
  404. USBD_INTEN_ENDEPIN5_Pos,
  405. USBD_INTEN_ENDEPIN6_Pos,
  406. USBD_INTEN_ENDEPIN7_Pos,
  407. USBD_INTEN_ENDISOIN_Pos
  408. };
  409. static const uint8_t epout_bitpos[] =
  410. {
  411. USBD_INTEN_ENDEPOUT0_Pos,
  412. USBD_INTEN_ENDEPOUT1_Pos,
  413. USBD_INTEN_ENDEPOUT2_Pos,
  414. USBD_INTEN_ENDEPOUT3_Pos,
  415. USBD_INTEN_ENDEPOUT4_Pos,
  416. USBD_INTEN_ENDEPOUT5_Pos,
  417. USBD_INTEN_ENDEPOUT6_Pos,
  418. USBD_INTEN_ENDEPOUT7_Pos,
  419. USBD_INTEN_ENDISOOUT_Pos
  420. };
  421. return 1UL << (NRF_USBD_EPIN_CHECK(ep) ? epin_bitpos : epout_bitpos)[NRF_USBD_EP_NR_GET(ep)];
  422. }
  423. /**
  424. * @name Integrated feeders and consumers
  425. *
  426. * Internal, default functions for transfer processing.
  427. * @{
  428. */
  429. /**
  430. * @brief Integrated consumer to RAM buffer.
  431. *
  432. * @param p_next See @ref nrf_drv_usbd_consumer_t documentation.
  433. * @param p_context See @ref nrf_drv_usbd_consumer_t documentation.
  434. * @param ep_size See @ref nrf_drv_usbd_consumer_t documentation.
  435. * @param data_size See @ref nrf_drv_usbd_consumer_t documentation.
  436. *
  437. * @retval true Continue transfer.
  438. * @retval false This was the last transfer.
  439. */
  440. bool nrf_drv_usbd_consumer(
  441. nrf_drv_usbd_ep_transfer_t * p_next,
  442. void * p_context,
  443. size_t ep_size,
  444. size_t data_size)
  445. {
  446. nrf_drv_usbd_transfer_t * p_transfer = p_context;
  447. ASSERT(ep_size >= data_size);
  448. ASSERT((p_transfer->p_data.rx == NULL) ||
  449. nrfx_is_in_ram((const void*)(p_transfer->p_data.ptr)));
  450. size_t size = p_transfer->size;
  451. if (size < data_size)
  452. {
  453. NRF_LOG_DEBUG("consumer: buffer too small: r: %u, l: %u", data_size, size);
  454. /* Buffer size to small */
  455. p_next->size = 0;
  456. p_next->p_data = p_transfer->p_data;
  457. }
  458. else
  459. {
  460. p_next->size = data_size;
  461. p_next->p_data = p_transfer->p_data;
  462. size -= data_size;
  463. p_transfer->size = size;
  464. p_transfer->p_data.ptr += data_size;
  465. }
  466. return (ep_size == data_size) && (size != 0);
  467. }
  468. /**
  469. * @brief Integrated feeder from RAM source.
  470. *
  471. * @param[out] p_next See @ref nrf_drv_usbd_feeder_t documentation.
  472. * @param[in,out] p_context See @ref nrf_drv_usbd_feeder_t documentation.
  473. * @param[in] ep_size See @ref nrf_drv_usbd_feeder_t documentation.
  474. *
  475. * @retval true Continue transfer.
  476. * @retval false This was the last transfer.
  477. */
  478. bool nrf_drv_usbd_feeder_ram(
  479. nrf_drv_usbd_ep_transfer_t * p_next,
  480. void * p_context,
  481. size_t ep_size)
  482. {
  483. nrf_drv_usbd_transfer_t * p_transfer = p_context;
  484. ASSERT(nrfx_is_in_ram((const void*)(p_transfer->p_data.ptr)));
  485. size_t tx_size = p_transfer->size;
  486. if (tx_size > ep_size)
  487. {
  488. tx_size = ep_size;
  489. }
  490. p_next->p_data = p_transfer->p_data;
  491. p_next->size = tx_size;
  492. p_transfer->size -= tx_size;
  493. p_transfer->p_data.ptr += tx_size;
  494. return (p_transfer->size != 0);
  495. }
  496. /**
  497. * @brief Integrated feeder from RAM source with ZLP.
  498. *
  499. * @param[out] p_next See @ref nrf_drv_usbd_feeder_t documentation.
  500. * @param[in,out] p_context See @ref nrf_drv_usbd_feeder_t documentation.
  501. * @param[in] ep_size See @ref nrf_drv_usbd_feeder_t documentation.
  502. *
  503. * @retval true Continue transfer.
  504. * @retval false This was the last transfer.
  505. */
  506. bool nrf_drv_usbd_feeder_ram_zlp(
  507. nrf_drv_usbd_ep_transfer_t * p_next,
  508. void * p_context,
  509. size_t ep_size)
  510. {
  511. nrf_drv_usbd_transfer_t * p_transfer = p_context;
  512. ASSERT(nrfx_is_in_ram((const void*)(p_transfer->p_data.ptr)));
  513. size_t tx_size = p_transfer->size;
  514. if (tx_size > ep_size)
  515. {
  516. tx_size = ep_size;
  517. }
  518. p_next->p_data.tx = (tx_size == 0) ? NULL : p_transfer->p_data.tx;
  519. p_next->size = tx_size;
  520. p_transfer->size -= tx_size;
  521. p_transfer->p_data.ptr += tx_size;
  522. return (tx_size != 0);
  523. }
  524. /**
  525. * @brief Integrated feeder from a flash source.
  526. *
  527. * @param[out] p_next See @ref nrf_drv_usbd_feeder_t documentation.
  528. * @param[in,out] p_context See @ref nrf_drv_usbd_feeder_t documentation.
  529. * @param[in] ep_size See @ref nrf_drv_usbd_feeder_t documentation.
  530. *
  531. * @retval true Continue transfer.
  532. * @retval false This was the last transfer.
  533. */
  534. bool nrf_drv_usbd_feeder_flash(
  535. nrf_drv_usbd_ep_transfer_t * p_next,
  536. void * p_context,
  537. size_t ep_size)
  538. {
  539. nrf_drv_usbd_transfer_t * p_transfer = p_context;
  540. ASSERT(!nrfx_is_in_ram((const void*)(p_transfer->p_data.ptr)));
  541. size_t tx_size = p_transfer->size;
  542. void * p_buffer = nrf_drv_usbd_feeder_buffer_get();
  543. if (tx_size > ep_size)
  544. {
  545. tx_size = ep_size;
  546. }
  547. ASSERT(tx_size <= NRF_DRV_USBD_FEEDER_BUFFER_SIZE);
  548. memcpy(p_buffer, (p_transfer->p_data.tx), tx_size);
  549. p_next->p_data.tx = p_buffer;
  550. p_next->size = tx_size;
  551. p_transfer->size -= tx_size;
  552. p_transfer->p_data.ptr += tx_size;
  553. return (p_transfer->size != 0);
  554. }
  555. /**
  556. * @brief Integrated feeder from a flash source with ZLP.
  557. *
  558. * @param[out] p_next See @ref nrf_drv_usbd_feeder_t documentation.
  559. * @param[in,out] p_context See @ref nrf_drv_usbd_feeder_t documentation.
  560. * @param[in] ep_size See @ref nrf_drv_usbd_feeder_t documentation.
  561. *
  562. * @retval true Continue transfer.
  563. * @retval false This was the last transfer.
  564. */
  565. bool nrf_drv_usbd_feeder_flash_zlp(
  566. nrf_drv_usbd_ep_transfer_t * p_next,
  567. void * p_context,
  568. size_t ep_size)
  569. {
  570. nrf_drv_usbd_transfer_t * p_transfer = p_context;
  571. ASSERT(!nrfx_is_in_ram((const void*)(p_transfer->p_data.ptr)));
  572. size_t tx_size = p_transfer->size;
  573. void * p_buffer = nrf_drv_usbd_feeder_buffer_get();
  574. if (tx_size > ep_size)
  575. {
  576. tx_size = ep_size;
  577. }
  578. ASSERT(tx_size <= NRF_DRV_USBD_FEEDER_BUFFER_SIZE);
  579. if (tx_size != 0)
  580. {
  581. memcpy(p_buffer, (p_transfer->p_data.tx), tx_size);
  582. p_next->p_data.tx = p_buffer;
  583. }
  584. else
  585. {
  586. p_next->p_data.tx = NULL;
  587. }
  588. p_next->size = tx_size;
  589. p_transfer->size -= tx_size;
  590. p_transfer->p_data.ptr += tx_size;
  591. return (tx_size != 0);
  592. }
  593. /** @} */
  594. /**
  595. * @brief Change Driver endpoint number to HAL endpoint number
  596. *
  597. * @param ep Driver endpoint identifier
  598. *
  599. * @return Endpoint identifier in HAL
  600. *
  601. * @sa nrf_drv_usbd_ep_from_hal
  602. */
  603. static inline uint8_t ep_to_hal(nrf_drv_usbd_ep_t ep)
  604. {
  605. USBD_ASSERT_EP_VALID(ep);
  606. return (uint8_t)ep;
  607. }
  608. /**
  609. * @brief Generate start task number for selected endpoint index
  610. *
  611. * @param ep Endpoint number
  612. *
  613. * @return Task for starting EasyDMA transfer on selected endpoint.
  614. */
  615. static inline nrf_usbd_task_t task_start_ep(nrf_drv_usbd_ep_t ep)
  616. {
  617. USBD_ASSERT_EP_VALID(ep);
  618. return (nrf_usbd_task_t)(
  619. (NRF_USBD_EPIN_CHECK(ep) ? NRF_USBD_TASK_STARTEPIN0 : NRF_USBD_TASK_STARTEPOUT0) +
  620. (NRF_USBD_EP_NR_GET(ep) * sizeof(uint32_t)));
  621. }
  622. /**
  623. * @brief Access selected endpoint state structure
  624. *
  625. * Function used to change or just read the state of selected endpoint.
  626. * It is used for internal transmission state.
  627. *
  628. * @param ep Endpoint number
  629. */
  630. static inline usbd_drv_ep_state_t* ep_state_access(nrf_drv_usbd_ep_t ep)
  631. {
  632. USBD_ASSERT_EP_VALID(ep);
  633. return ((NRF_USBD_EPIN_CHECK(ep) ? m_ep_state.ep_in : m_ep_state.ep_out) +
  634. NRF_USBD_EP_NR_GET(ep));
  635. }
  636. /**
  637. * @brief Change endpoint number to bit position
  638. *
  639. * Bit positions are defined the same way as they are placed in DATAEPSTATUS register,
  640. * but bits for endpoint 0 are included.
  641. *
  642. * @param ep Endpoint number
  643. *
  644. * @return Bit position related to the given endpoint number
  645. *
  646. * @sa bit2ep
  647. */
  648. static inline uint8_t ep2bit(nrf_drv_usbd_ep_t ep)
  649. {
  650. USBD_ASSERT_EP_VALID(ep);
  651. return USBD_EP_BITPOS(ep);
  652. }
  653. /**
  654. * @brief Change bit position to endpoint number
  655. *
  656. * @param bitpos Bit position
  657. *
  658. * @return Endpoint number corresponding to given bit position.
  659. *
  660. * @sa ep2bit
  661. */
  662. static inline nrf_drv_usbd_ep_t bit2ep(uint8_t bitpos)
  663. {
  664. STATIC_ASSERT(USBD_EPOUT_BITPOS_0 > USBD_EPIN_BITPOS_0);
  665. return (nrf_drv_usbd_ep_t)((bitpos >= USBD_EPOUT_BITPOS_0) ?
  666. NRF_USBD_EPOUT(bitpos - USBD_EPOUT_BITPOS_0) : NRF_USBD_EPIN(bitpos));
  667. }
  668. /**
  669. * @brief Mark that EasyDMA is working
  670. *
  671. * Internal function to set the flag informing about EasyDMA transfer pending.
  672. * This function is called always just after the EasyDMA transfer is started.
  673. */
  674. static inline void usbd_dma_pending_set(void)
  675. {
  676. if (nrf_drv_usb_errata_199())
  677. {
  678. *((volatile uint32_t *)0x40027C1C) = 0x00000082;
  679. }
  680. m_dma_pending = true;
  681. }
  682. /**
  683. * @brief Mark that EasyDMA is free
  684. *
  685. * Internal function to clear the flag informing about EasyDMA transfer pending.
  686. * This function is called always just after the finished EasyDMA transfer is detected.
  687. */
  688. static inline void usbd_dma_pending_clear(void)
  689. {
  690. if (nrf_drv_usb_errata_199())
  691. {
  692. *((volatile uint32_t *)0x40027C1C) = 0x00000000;
  693. }
  694. m_dma_pending = false;
  695. }
  696. /**
  697. * @brief Start selected EasyDMA transmission
  698. *
  699. * This is internal auxiliary function.
  700. * No checking is made if EasyDMA is ready for new transmission.
  701. *
  702. * @param[in] ep Number of endpoint for transmission.
  703. * If it is OUT endpoint transmission would be directed from endpoint to RAM.
  704. * If it is in endpoint transmission would be directed from RAM to endpoint.
  705. */
  706. static inline void usbd_dma_start(nrf_drv_usbd_ep_t ep)
  707. {
  708. nrf_usbd_task_trigger(task_start_ep(ep));
  709. }
  710. void nrf_drv_usbd_isoinconfig_set(nrf_usbd_isoinconfig_t config)
  711. {
  712. ASSERT(!nrf_drv_usbd_errata_type_52840_eng_a());
  713. nrf_usbd_isoinconfig_set(config);
  714. }
  715. nrf_usbd_isoinconfig_t nrf_drv_usbd_isoinconfig_get(void)
  716. {
  717. ASSERT(!nrf_drv_usbd_errata_type_52840_eng_a());
  718. return nrf_usbd_isoinconfig_get();
  719. }
  720. /**
  721. * @brief Abort pending transfer on selected endpoint
  722. *
  723. * @param ep Endpoint number.
  724. *
  725. * @note
  726. * This function locks interrupts that may be costly.
  727. * It is good idea to test if the endpoint is still busy before calling this function:
  728. * @code
  729. (m_ep_dma_waiting & (1U << ep2bit(ep)))
  730. * @endcode
  731. * This function would check it again, but it makes it inside critical section.
  732. */
  733. static inline void usbd_ep_abort(nrf_drv_usbd_ep_t ep)
  734. {
  735. CRITICAL_REGION_ENTER();
  736. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  737. if (NRF_USBD_EPOUT_CHECK(ep))
  738. {
  739. /* Host -> Device */
  740. if ((~m_ep_dma_waiting) & (1U << ep2bit(ep)))
  741. {
  742. /* If the bit in m_ep_dma_waiting in cleared - nothing would be
  743. * processed inside transfer processing */
  744. nrf_drv_usbd_transfer_out_drop(ep);
  745. }
  746. else
  747. {
  748. p_state->handler.consumer = NULL;
  749. m_ep_dma_waiting &= ~(1U << ep2bit(ep));
  750. m_ep_ready &= ~(1U << ep2bit(ep));
  751. }
  752. /* Aborted */
  753. p_state->status = NRF_USBD_EP_ABORTED;
  754. }
  755. else
  756. {
  757. if(!NRF_USBD_EPISO_CHECK(ep))
  758. {
  759. if(ep != NRF_DRV_USBD_EPIN0)
  760. {
  761. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7B6 + (2u * (NRF_USBD_EP_NR_GET(ep) - 1)); //inxbsy
  762. uint8_t temp = *((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  763. temp |= (1U << 1);
  764. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) |= temp;
  765. UNUSED_VARIABLE(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  766. }
  767. else
  768. {
  769. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7B4;
  770. uint8_t temp = *((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  771. temp |= (1U << 2);
  772. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) |= temp;
  773. UNUSED_VARIABLE(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  774. }
  775. }
  776. if ((m_ep_dma_waiting | (~m_ep_ready)) & (1U << ep2bit(ep)))
  777. {
  778. /* Device -> Host */
  779. m_ep_dma_waiting &= ~(1U << ep2bit(ep));
  780. m_ep_ready |= 1U << ep2bit(ep) ;
  781. p_state->handler.feeder = NULL;
  782. p_state->status = NRF_USBD_EP_ABORTED;
  783. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_ABORTED);
  784. m_event_handler(&evt);
  785. }
  786. }
  787. CRITICAL_REGION_EXIT();
  788. }
  789. void nrf_drv_usbd_ep_abort(nrf_drv_usbd_ep_t ep)
  790. {
  791. usbd_ep_abort(ep);
  792. }
  793. /**
  794. * @brief Abort all pending endpoints
  795. *
  796. * Function aborts all pending endpoint transfers.
  797. */
  798. static void usbd_ep_abort_all(void)
  799. {
  800. uint32_t ep_waiting = m_ep_dma_waiting | (m_ep_ready & USBD_EPOUT_BIT_MASK);
  801. while (0 != ep_waiting)
  802. {
  803. uint8_t bitpos = __CLZ(__RBIT(ep_waiting));
  804. if(!NRF_USBD_EPISO_CHECK(bit2ep(bitpos)))
  805. {
  806. usbd_ep_abort(bit2ep(bitpos));
  807. }
  808. ep_waiting &= ~(1U << bitpos);
  809. }
  810. m_ep_ready = (((1U << NRF_USBD_EPIN_CNT) - 1U) << USBD_EPIN_BITPOS_0);
  811. }
  812. /**
  813. * @brief Force the USBD interrupt into pending state
  814. *
  815. * This function is used to force USBD interrupt to be processed right now.
  816. * It makes it possible to process all EasyDMA access on one thread priority level.
  817. */
  818. static inline void usbd_int_rise(void)
  819. {
  820. NVIC_SetPendingIRQ(USBD_IRQn);
  821. }
  822. /**
  823. * @name USBD interrupt runtimes
  824. *
  825. * Interrupt runtimes that would be vectorized using @ref m_ivec_isr
  826. * @{
  827. */
  828. static void ev_usbreset_handler(void)
  829. {
  830. m_bus_suspend = false;
  831. m_last_setup_dir = NRF_DRV_USBD_EPOUT0;
  832. const nrf_drv_usbd_evt_t evt = {
  833. .type = NRF_DRV_USBD_EVT_RESET
  834. };
  835. m_event_handler(&evt);
  836. }
  837. static void ev_started_handler(void)
  838. {
  839. #if NRF_DRV_USBD_STARTED_EV_ENABLE
  840. uint32_t epstatus = nrf_usbd_epstatus_get_and_clear();
  841. /* All finished endpoint have to be marked as busy */
  842. // #warning Check this one
  843. // ASSERT(epstatus == ((~m_ep_ready) & epstatus));
  844. while (epstatus)
  845. {
  846. uint8_t bitpos = __CLZ(__RBIT(epstatus));
  847. nrf_drv_usbd_ep_t ep = bit2ep(bitpos);
  848. epstatus &= ~(1UL << bitpos);
  849. UNUSED_VARIABLE(ep);
  850. }
  851. #endif
  852. }
  853. /**
  854. * @brief Handler for EasyDMA event without endpoint clearing.
  855. *
  856. * This handler would be called when EasyDMA transfer for endpoints that does not require clearing.
  857. * All in endpoints are cleared automatically when new EasyDMA transfer is initialized.
  858. * For endpoint 0 see @ref nrf_usbd_ep0out_dma_handler
  859. *
  860. * @param[in] ep Endpoint number
  861. */
  862. static inline void nrf_usbd_ep0in_dma_handler(void)
  863. {
  864. const nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPIN0;
  865. NRF_LOG_DEBUG("USB event: DMA ready IN0");
  866. usbd_dma_pending_clear();
  867. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  868. if (NRF_USBD_EP_ABORTED == p_state->status)
  869. {
  870. /* Clear transfer information just in case */
  871. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  872. }
  873. else if (p_state->handler.feeder == NULL)
  874. {
  875. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  876. }
  877. else
  878. {
  879. /* Nothing to do */
  880. }
  881. }
  882. /**
  883. * @brief Handler for EasyDMA event without endpoint clearing.
  884. *
  885. * This handler would be called when EasyDMA transfer for endpoints that does not require clearing.
  886. * All in endpoints are cleared automatically when new EasyDMA transfer is initialized.
  887. * For endpoint 0 see @ref nrf_usbd_ep0out_dma_handler
  888. *
  889. * @param[in] ep Endpoint number
  890. */
  891. static inline void nrf_usbd_epin_dma_handler(nrf_drv_usbd_ep_t ep)
  892. {
  893. NRF_LOG_DEBUG("USB event: DMA ready IN: %x", ep);
  894. ASSERT(NRF_USBD_EPIN_CHECK(ep));
  895. ASSERT(!NRF_USBD_EPISO_CHECK(ep));
  896. ASSERT(NRF_USBD_EP_NR_GET(ep) > 0);
  897. usbd_dma_pending_clear();
  898. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  899. if (NRF_USBD_EP_ABORTED == p_state->status)
  900. {
  901. /* Clear transfer information just in case */
  902. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  903. }
  904. else if (p_state->handler.feeder == NULL)
  905. {
  906. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  907. }
  908. else
  909. {
  910. /* Nothing to do */
  911. }
  912. }
  913. /**
  914. * @brief Handler for EasyDMA event from in isochronous endpoint
  915. *
  916. * @todo RK documentation
  917. */
  918. static inline void nrf_usbd_epiniso_dma_handler(nrf_drv_usbd_ep_t ep)
  919. {
  920. if (NRF_USBD_ISO_DEBUG)
  921. {
  922. NRF_LOG_DEBUG("USB event: DMA ready ISOIN: %x", ep);
  923. }
  924. ASSERT(NRF_USBD_EPIN_CHECK(ep));
  925. ASSERT(NRF_USBD_EPISO_CHECK(ep));
  926. usbd_dma_pending_clear();
  927. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  928. if (NRF_USBD_EP_ABORTED == p_state->status)
  929. {
  930. /* Clear transfer information just in case */
  931. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  932. }
  933. else if (p_state->handler.feeder == NULL)
  934. {
  935. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  936. /* Send event to the user - for an ISO IN endpoint, the whole transfer is finished in this moment */
  937. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_OK);
  938. m_event_handler(&evt);
  939. }
  940. else
  941. {
  942. /* Nothing to do */
  943. }
  944. }
  945. /**
  946. * @brief Handler for EasyDMA event for OUT endpoint 0.
  947. *
  948. * EP0 OUT have to be cleared automatically in special way - only in the middle of the transfer.
  949. * It cannot be cleared when required transfer is finished because it means the same that accepting the comment.
  950. */
  951. static inline void nrf_usbd_ep0out_dma_handler(void)
  952. {
  953. const nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPOUT0;
  954. NRF_LOG_DEBUG("USB event: DMA ready OUT0");
  955. usbd_dma_pending_clear();
  956. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  957. if (NRF_USBD_EP_ABORTED == p_state->status)
  958. {
  959. /* Clear transfer information just in case */
  960. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  961. }
  962. else if (p_state->handler.consumer == NULL)
  963. {
  964. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  965. /* Send event to the user - for an OUT endpoint, the whole transfer is finished in this moment */
  966. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_OK);
  967. m_event_handler(&evt);
  968. }
  969. else
  970. {
  971. nrf_drv_usbd_setup_data_clear();
  972. }
  973. }
  974. /**
  975. * @brief Handler for EasyDMA event from endpoinpoint that requires clearing.
  976. *
  977. * This handler would be called when EasyDMA transfer for OUT endpoint has been finished.
  978. *
  979. * @param[in] ep Endpoint number
  980. *
  981. */
  982. static inline void nrf_usbd_epout_dma_handler(nrf_drv_usbd_ep_t ep)
  983. {
  984. NRF_LOG_DEBUG("USB drv: DMA ready OUT: %x", ep);
  985. ASSERT(NRF_USBD_EPOUT_CHECK(ep));
  986. ASSERT(!NRF_USBD_EPISO_CHECK(ep));
  987. ASSERT(NRF_USBD_EP_NR_GET(ep) > 0);
  988. usbd_dma_pending_clear();
  989. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  990. if (NRF_USBD_EP_ABORTED == p_state->status)
  991. {
  992. /* Clear transfer information just in case */
  993. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  994. }
  995. else if (p_state->handler.consumer == NULL)
  996. {
  997. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  998. /* Send event to the user - for an OUT endpoint, the whole transfer is finished in this moment */
  999. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_OK);
  1000. m_event_handler(&evt);
  1001. }
  1002. else
  1003. {
  1004. /* Nothing to do */
  1005. }
  1006. #if NRF_DRV_USBD_EARLY_DMA_PROCESS
  1007. /* Speed up */
  1008. usbd_dmareq_process();
  1009. #endif
  1010. }
  1011. /**
  1012. * @brief Handler for EasyDMA event from out isochronous endpoint
  1013. *
  1014. * @todo RK documentation
  1015. */
  1016. static inline void nrf_usbd_epoutiso_dma_handler(nrf_drv_usbd_ep_t ep)
  1017. {
  1018. if (NRF_USBD_ISO_DEBUG)
  1019. {
  1020. NRF_LOG_DEBUG("USB drv: DMA ready ISOOUT: %x", ep);
  1021. }
  1022. ASSERT(NRF_USBD_EPISO_CHECK(ep));
  1023. usbd_dma_pending_clear();
  1024. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  1025. if (NRF_USBD_EP_ABORTED == p_state->status)
  1026. {
  1027. /* Nothing to do - just ignore */
  1028. }
  1029. else if (p_state->handler.consumer == NULL)
  1030. {
  1031. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << ep2bit(ep))));
  1032. /* Send event to the user - for an OUT endpoint, the whole transfer is finished in this moment */
  1033. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_OK);
  1034. m_event_handler(&evt);
  1035. }
  1036. else
  1037. {
  1038. /* Nothing to do */
  1039. }
  1040. }
  1041. static void ev_dma_epin0_handler(void) { nrf_usbd_ep0in_dma_handler(); }
  1042. static void ev_dma_epin1_handler(void) { nrf_usbd_epin_dma_handler(NRF_DRV_USBD_EPIN1 ); }
  1043. static void ev_dma_epin2_handler(void) { nrf_usbd_epin_dma_handler(NRF_DRV_USBD_EPIN2 ); }
  1044. static void ev_dma_epin3_handler(void) { nrf_usbd_epin_dma_handler(NRF_DRV_USBD_EPIN3 ); }
  1045. static void ev_dma_epin4_handler(void) { nrf_usbd_epin_dma_handler(NRF_DRV_USBD_EPIN4 ); }
  1046. static void ev_dma_epin5_handler(void) { nrf_usbd_epin_dma_handler(NRF_DRV_USBD_EPIN5 ); }
  1047. static void ev_dma_epin6_handler(void) { nrf_usbd_epin_dma_handler(NRF_DRV_USBD_EPIN6 ); }
  1048. static void ev_dma_epin7_handler(void) { nrf_usbd_epin_dma_handler(NRF_DRV_USBD_EPIN7 ); }
  1049. static void ev_dma_epin8_handler(void) { nrf_usbd_epiniso_dma_handler(NRF_DRV_USBD_EPIN8 ); }
  1050. static void ev_dma_epout0_handler(void) { nrf_usbd_ep0out_dma_handler(); }
  1051. static void ev_dma_epout1_handler(void) { nrf_usbd_epout_dma_handler(NRF_DRV_USBD_EPOUT1); }
  1052. static void ev_dma_epout2_handler(void) { nrf_usbd_epout_dma_handler(NRF_DRV_USBD_EPOUT2); }
  1053. static void ev_dma_epout3_handler(void) { nrf_usbd_epout_dma_handler(NRF_DRV_USBD_EPOUT3); }
  1054. static void ev_dma_epout4_handler(void) { nrf_usbd_epout_dma_handler(NRF_DRV_USBD_EPOUT4); }
  1055. static void ev_dma_epout5_handler(void) { nrf_usbd_epout_dma_handler(NRF_DRV_USBD_EPOUT5); }
  1056. static void ev_dma_epout6_handler(void) { nrf_usbd_epout_dma_handler(NRF_DRV_USBD_EPOUT6); }
  1057. static void ev_dma_epout7_handler(void) { nrf_usbd_epout_dma_handler(NRF_DRV_USBD_EPOUT7); }
  1058. static void ev_dma_epout8_handler(void) { nrf_usbd_epoutiso_dma_handler(NRF_DRV_USBD_EPOUT8); }
  1059. static void ev_sof_handler(void)
  1060. {
  1061. nrf_drv_usbd_evt_t evt = {
  1062. NRF_DRV_USBD_EVT_SOF,
  1063. .data = { .sof = { .framecnt = nrf_usbd_framecntr_get() }}
  1064. };
  1065. /* Process isochronous endpoints */
  1066. uint32_t iso_ready_mask = (1U << ep2bit(NRF_DRV_USBD_EPIN8));
  1067. if (nrf_usbd_episoout_size_get(NRF_DRV_USBD_EPOUT8) != NRF_USBD_EPISOOUT_NO_DATA)
  1068. {
  1069. iso_ready_mask |= (1U << ep2bit(NRF_DRV_USBD_EPOUT8));
  1070. }
  1071. m_ep_ready |= iso_ready_mask;
  1072. m_event_handler(&evt);
  1073. }
  1074. /**
  1075. * @brief React on data transfer finished
  1076. *
  1077. * Auxiliary internal function.
  1078. * @param ep Endpoint number
  1079. * @param bitpos Bit position for selected endpoint number
  1080. */
  1081. static void usbd_ep_data_handler(nrf_drv_usbd_ep_t ep, uint8_t bitpos)
  1082. {
  1083. NRF_LOG_DEBUG("USBD event: EndpointData: %x", ep);
  1084. /* Mark endpoint ready for next DMA access */
  1085. m_ep_ready |= (1U << bitpos);
  1086. if (NRF_USBD_EPIN_CHECK(ep))
  1087. {
  1088. /* IN endpoint (Device -> Host) */
  1089. if (0 == (m_ep_dma_waiting & (1U << bitpos)))
  1090. {
  1091. NRF_LOG_DEBUG("USBD event: EndpointData: In finished");
  1092. /* No more data to be send - transmission finished */
  1093. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_OK);
  1094. m_event_handler(&evt);
  1095. }
  1096. }
  1097. else
  1098. {
  1099. /* OUT endpoint (Host -> Device) */
  1100. if (0 == (m_ep_dma_waiting & (1U << bitpos)))
  1101. {
  1102. NRF_LOG_DEBUG("USBD event: EndpointData: Out waiting");
  1103. /* No buffer prepared - send event to the application */
  1104. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_WAITING);
  1105. m_event_handler(&evt);
  1106. }
  1107. }
  1108. }
  1109. static void ev_setup_data_handler(void)
  1110. {
  1111. usbd_ep_data_handler(m_last_setup_dir, ep2bit(m_last_setup_dir));
  1112. }
  1113. static void ev_setup_handler(void)
  1114. {
  1115. NRF_LOG_DEBUG("USBD event: Setup (rt:%.2x r:%.2x v:%.4x i:%.4x l:%u )",
  1116. nrf_usbd_setup_bmrequesttype_get(),
  1117. nrf_usbd_setup_brequest_get(),
  1118. nrf_usbd_setup_wvalue_get(),
  1119. nrf_usbd_setup_windex_get(),
  1120. nrf_usbd_setup_wlength_get());
  1121. uint8_t bmRequestType = nrf_usbd_setup_bmrequesttype_get();
  1122. if ((m_ep_dma_waiting | ((~m_ep_ready) & USBD_EPIN_BIT_MASK)) & (1U <<ep2bit(m_last_setup_dir)))
  1123. {
  1124. NRF_LOG_DEBUG("USBD drv: Trying to abort last transfer on EP0");
  1125. usbd_ep_abort(m_last_setup_dir);
  1126. }
  1127. m_last_setup_dir =
  1128. ((bmRequestType & USBD_DRV_REQUESTTYPE_DIR_MASK) == USBD_DRV_REQUESTTYPE_DIR_OUT) ?
  1129. NRF_DRV_USBD_EPOUT0 : NRF_DRV_USBD_EPIN0;
  1130. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(
  1131. &m_ep_dma_waiting,
  1132. ~((1U << ep2bit(NRF_DRV_USBD_EPOUT0)) | (1U << ep2bit(NRF_DRV_USBD_EPIN0)))));
  1133. m_ep_ready |= 1U << ep2bit(NRF_DRV_USBD_EPIN0);
  1134. const nrf_drv_usbd_evt_t evt = {
  1135. .type = NRF_DRV_USBD_EVT_SETUP
  1136. };
  1137. m_event_handler(&evt);
  1138. }
  1139. static void ev_usbevent_handler(void)
  1140. {
  1141. uint32_t event = nrf_usbd_eventcause_get_and_clear();
  1142. if (event & NRF_USBD_EVENTCAUSE_ISOOUTCRC_MASK)
  1143. {
  1144. NRF_LOG_DEBUG("USBD event: ISOOUTCRC");
  1145. /* Currently no support */
  1146. }
  1147. if (event & NRF_USBD_EVENTCAUSE_SUSPEND_MASK)
  1148. {
  1149. NRF_LOG_DEBUG("USBD event: SUSPEND");
  1150. m_bus_suspend = true;
  1151. const nrf_drv_usbd_evt_t evt = {
  1152. .type = NRF_DRV_USBD_EVT_SUSPEND
  1153. };
  1154. m_event_handler(&evt);
  1155. }
  1156. if (event & NRF_USBD_EVENTCAUSE_RESUME_MASK)
  1157. {
  1158. NRF_LOG_DEBUG("USBD event: RESUME");
  1159. m_bus_suspend = false;
  1160. const nrf_drv_usbd_evt_t evt = {
  1161. .type = NRF_DRV_USBD_EVT_RESUME
  1162. };
  1163. m_event_handler(&evt);
  1164. }
  1165. if (event & NRF_USBD_EVENTCAUSE_WUREQ_MASK)
  1166. {
  1167. NRF_LOG_DEBUG("USBD event: WUREQ (%s)", m_bus_suspend ? "In Suspend" : "Active");
  1168. if (m_bus_suspend)
  1169. {
  1170. ASSERT(!nrf_usbd_lowpower_check());
  1171. m_bus_suspend = false;
  1172. nrf_usbd_dpdmvalue_set(NRF_USBD_DPDMVALUE_RESUME);
  1173. nrf_usbd_task_trigger(NRF_USBD_TASK_DRIVEDPDM);
  1174. const nrf_drv_usbd_evt_t evt = {
  1175. .type = NRF_DRV_USBD_EVT_WUREQ
  1176. };
  1177. m_event_handler(&evt);
  1178. }
  1179. }
  1180. }
  1181. static void ev_epdata_handler(void)
  1182. {
  1183. /* Get all endpoints that have acknowledged transfer */
  1184. uint32_t dataepstatus = nrf_usbd_epdatastatus_get_and_clear();
  1185. if (nrf_drv_usbd_errata_104())
  1186. {
  1187. dataepstatus |= (m_simulated_dataepstatus &
  1188. ~((1U << USBD_EPOUT_BITPOS_0) | (1U << USBD_EPIN_BITPOS_0)));
  1189. m_simulated_dataepstatus &=
  1190. ((1U << USBD_EPOUT_BITPOS_0) | (1U << USBD_EPIN_BITPOS_0));
  1191. }
  1192. NRF_LOG_DEBUG("USBD event: EndpointEPStatus: %x", dataepstatus);
  1193. /* All finished endpoint have to be marked as busy */
  1194. while (dataepstatus)
  1195. {
  1196. uint8_t bitpos = __CLZ(__RBIT(dataepstatus));
  1197. nrf_drv_usbd_ep_t ep = bit2ep(bitpos);
  1198. dataepstatus &= ~(1UL << bitpos);
  1199. UNUSED_RETURN_VALUE(usbd_ep_data_handler(ep, bitpos));
  1200. }
  1201. if (NRF_DRV_USBD_EARLY_DMA_PROCESS)
  1202. {
  1203. /* Speed up */
  1204. usbd_dmareq_process();
  1205. }
  1206. }
  1207. /**
  1208. * @brief Function to select the endpoint to start
  1209. *
  1210. * Function that realizes algorithm to schedule right channel for EasyDMA transfer.
  1211. * It gets a variable with flags for the endpoints currently requiring transfer.
  1212. *
  1213. * @param[in] req Bit flags for channels currently requiring transfer.
  1214. * Bits 0...8 used for IN endpoints.
  1215. * Bits 16...24 used for OUT endpoints.
  1216. * @note
  1217. * This function would be never called with 0 as a @c req argument.
  1218. * @return The bit number of the endpoint that should be processed now.
  1219. */
  1220. static uint8_t usbd_dma_scheduler_algorithm(uint32_t req)
  1221. {
  1222. /* Only prioritized scheduling mode is supported */
  1223. STATIC_ASSERT(USBD_CONFIG_DMASCHEDULER_MODE == NRF_DRV_USBD_DMASCHEDULER_PRIORITIZED);
  1224. return __CLZ(__RBIT(req));
  1225. }
  1226. /**
  1227. * @brief Get the size of isochronous endpoint
  1228. *
  1229. * The size of isochronous endpoint is configurable.
  1230. * This function returns the size of isochronous buffer taking into account
  1231. * current configuration.
  1232. *
  1233. * @param[in] ep Endpoint number.
  1234. *
  1235. * @return The size of endpoint buffer.
  1236. */
  1237. static inline size_t usbd_ep_iso_capacity(nrf_drv_usbd_ep_t ep)
  1238. {
  1239. UNUSED_PARAMETER(ep);
  1240. nrf_usbd_isosplit_t split = nrf_usbd_isosplit_get();
  1241. if (NRF_USBD_ISOSPLIT_Half == split)
  1242. {
  1243. return NRF_DRV_USBD_ISOSIZE / 2;
  1244. }
  1245. return NRF_DRV_USBD_ISOSIZE;
  1246. }
  1247. /**
  1248. * @brief Process all DMA requests
  1249. *
  1250. * Function that have to be called from USBD interrupt handler.
  1251. * It have to be called when all the interrupts connected with endpoints transfer
  1252. * and DMA transfer are already handled.
  1253. */
  1254. static void usbd_dmareq_process(void)
  1255. {
  1256. if (!m_dma_pending)
  1257. {
  1258. uint32_t req;
  1259. while (0 != (req = m_ep_dma_waiting & m_ep_ready))
  1260. {
  1261. uint8_t pos;
  1262. if (USBD_CONFIG_DMASCHEDULER_ISO_BOOST && ((req & USBD_EPISO_BIT_MASK) != 0))
  1263. {
  1264. pos = usbd_dma_scheduler_algorithm(req & USBD_EPISO_BIT_MASK);
  1265. }
  1266. else
  1267. {
  1268. pos = usbd_dma_scheduler_algorithm(req);
  1269. }
  1270. nrf_drv_usbd_ep_t ep = bit2ep(pos);
  1271. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  1272. nrf_drv_usbd_ep_transfer_t transfer;
  1273. bool continue_transfer;
  1274. STATIC_ASSERT(offsetof(usbd_drv_ep_state_t, handler.feeder) ==
  1275. offsetof(usbd_drv_ep_state_t, handler.consumer));
  1276. ASSERT((p_state->handler.feeder) != NULL);
  1277. if (NRF_USBD_EPIN_CHECK(ep))
  1278. {
  1279. /* Device -> Host */
  1280. continue_transfer = p_state->handler.feeder(
  1281. &transfer,
  1282. p_state->p_context,
  1283. p_state->max_packet_size);
  1284. if (!continue_transfer)
  1285. {
  1286. p_state->handler.feeder = NULL;
  1287. }
  1288. }
  1289. else
  1290. {
  1291. /* Host -> Device */
  1292. const size_t rx_size = nrf_drv_usbd_epout_size_get(ep);
  1293. continue_transfer = p_state->handler.consumer(
  1294. &transfer,
  1295. p_state->p_context,
  1296. p_state->max_packet_size,
  1297. rx_size);
  1298. if (transfer.p_data.rx == NULL)
  1299. {
  1300. /* Dropping transfer - allow processing */
  1301. ASSERT(transfer.size == 0);
  1302. }
  1303. else if (transfer.size < rx_size)
  1304. {
  1305. NRF_LOG_DEBUG("Endpoint %x overload (r: %u, e: %u)", ep, rx_size, transfer.size);
  1306. p_state->status = NRF_USBD_EP_OVERLOAD;
  1307. UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&m_ep_dma_waiting, ~(1U << pos)));
  1308. NRF_DRV_USBD_EP_TRANSFER_EVENT(evt, ep, NRF_USBD_EP_OVERLOAD);
  1309. m_event_handler(&evt);
  1310. /* This endpoint will not be transmitted now, repeat the loop */
  1311. continue;
  1312. }
  1313. else
  1314. {
  1315. /* Nothing to do - only check integrity if assertions are enabled */
  1316. ASSERT(transfer.size == rx_size);
  1317. }
  1318. if (!continue_transfer)
  1319. {
  1320. p_state->handler.consumer = NULL;
  1321. }
  1322. }
  1323. usbd_dma_pending_set();
  1324. m_ep_ready &= ~(1U << pos);
  1325. if (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep)))
  1326. {
  1327. NRF_LOG_DEBUG(
  1328. "USB DMA process: Starting transfer on EP: %x, size: %u",
  1329. ep,
  1330. transfer.size);
  1331. }
  1332. /* Update number of currently transferred bytes */
  1333. p_state->transfer_cnt += transfer.size;
  1334. /* Start transfer to the endpoint buffer */
  1335. nrf_usbd_ep_easydma_set(ep, transfer.p_data.ptr, (uint32_t)transfer.size);
  1336. if (nrf_drv_usbd_errata_104())
  1337. {
  1338. uint32_t cnt_end = (uint32_t)(-1);
  1339. do
  1340. {
  1341. uint32_t cnt = (uint32_t)(-1);
  1342. do
  1343. {
  1344. nrf_usbd_event_clear(NRF_USBD_EVENT_STARTED);
  1345. usbd_dma_start(ep);
  1346. nrf_delay_us(2);
  1347. ++cnt;
  1348. }while (!nrf_usbd_event_check(NRF_USBD_EVENT_STARTED));
  1349. if (cnt)
  1350. {
  1351. NRF_DRV_USBD_LOG_PROTO1_FIX_PRINTF(" DMA restarted: %u times", cnt);
  1352. }
  1353. nrf_delay_us(30);
  1354. while (0 == (0x20 & *((volatile uint32_t *)(NRF_USBD_BASE + 0x474))))
  1355. {
  1356. nrf_delay_us(2);
  1357. }
  1358. nrf_delay_us(1);
  1359. ++cnt_end;
  1360. } while (!nrf_usbd_event_check(nrf_drv_usbd_ep_to_endevent(ep)));
  1361. if (cnt_end)
  1362. {
  1363. NRF_DRV_USBD_LOG_PROTO1_FIX_PRINTF(" DMA fully restarted: %u times", cnt_end);
  1364. }
  1365. }
  1366. else
  1367. {
  1368. usbd_dma_start(ep);
  1369. /* There is a lot of USBD registers that cannot be accessed during EasyDMA transfer.
  1370. * This is quick fix to maintain stability of the stack.
  1371. * It cost some performance but makes stack stable. */
  1372. while (!nrf_usbd_event_check(nrf_drv_usbd_ep_to_endevent(ep)))
  1373. {
  1374. /* Empty */
  1375. }
  1376. }
  1377. if (NRF_USBD_DMAREQ_PROCESS_DEBUG)
  1378. {
  1379. NRF_LOG_DEBUG("USB DMA process - finishing");
  1380. }
  1381. /* Transfer started - exit the loop */
  1382. break;
  1383. }
  1384. }
  1385. else
  1386. {
  1387. if (NRF_USBD_DMAREQ_PROCESS_DEBUG)
  1388. {
  1389. NRF_LOG_DEBUG("USB DMA process - EasyDMA busy");
  1390. }
  1391. }
  1392. }
  1393. /** @} */
  1394. typedef void (*nrf_drv_usbd_isr_t)(void);
  1395. /**
  1396. * @brief USBD interrupt service runtimes
  1397. *
  1398. */
  1399. static const nrf_drv_usbd_isr_t m_isr[] =
  1400. {
  1401. [USBD_INTEN_USBRESET_Pos ] = ev_usbreset_handler,
  1402. [USBD_INTEN_STARTED_Pos ] = ev_started_handler,
  1403. [USBD_INTEN_ENDEPIN0_Pos ] = ev_dma_epin0_handler,
  1404. [USBD_INTEN_ENDEPIN1_Pos ] = ev_dma_epin1_handler,
  1405. [USBD_INTEN_ENDEPIN2_Pos ] = ev_dma_epin2_handler,
  1406. [USBD_INTEN_ENDEPIN3_Pos ] = ev_dma_epin3_handler,
  1407. [USBD_INTEN_ENDEPIN4_Pos ] = ev_dma_epin4_handler,
  1408. [USBD_INTEN_ENDEPIN5_Pos ] = ev_dma_epin5_handler,
  1409. [USBD_INTEN_ENDEPIN6_Pos ] = ev_dma_epin6_handler,
  1410. [USBD_INTEN_ENDEPIN7_Pos ] = ev_dma_epin7_handler,
  1411. [USBD_INTEN_EP0DATADONE_Pos] = ev_setup_data_handler,
  1412. [USBD_INTEN_ENDISOIN_Pos ] = ev_dma_epin8_handler,
  1413. [USBD_INTEN_ENDEPOUT0_Pos ] = ev_dma_epout0_handler,
  1414. [USBD_INTEN_ENDEPOUT1_Pos ] = ev_dma_epout1_handler,
  1415. [USBD_INTEN_ENDEPOUT2_Pos ] = ev_dma_epout2_handler,
  1416. [USBD_INTEN_ENDEPOUT3_Pos ] = ev_dma_epout3_handler,
  1417. [USBD_INTEN_ENDEPOUT4_Pos ] = ev_dma_epout4_handler,
  1418. [USBD_INTEN_ENDEPOUT5_Pos ] = ev_dma_epout5_handler,
  1419. [USBD_INTEN_ENDEPOUT6_Pos ] = ev_dma_epout6_handler,
  1420. [USBD_INTEN_ENDEPOUT7_Pos ] = ev_dma_epout7_handler,
  1421. [USBD_INTEN_ENDISOOUT_Pos ] = ev_dma_epout8_handler,
  1422. [USBD_INTEN_SOF_Pos ] = ev_sof_handler,
  1423. [USBD_INTEN_USBEVENT_Pos ] = ev_usbevent_handler,
  1424. [USBD_INTEN_EP0SETUP_Pos ] = ev_setup_handler,
  1425. [USBD_INTEN_EPDATA_Pos ] = ev_epdata_handler
  1426. };
  1427. /**
  1428. * @name Interrupt handlers
  1429. *
  1430. * @{
  1431. */
  1432. void USBD_IRQHandler(void)
  1433. {
  1434. const uint32_t enabled = nrf_usbd_int_enable_get();
  1435. uint32_t to_process = enabled;
  1436. uint32_t active = 0;
  1437. /* Check all enabled interrupts */
  1438. while (to_process)
  1439. {
  1440. uint8_t event_nr = __CLZ(__RBIT(to_process));
  1441. if (nrf_usbd_event_get_and_clear((nrf_usbd_event_t)nrfx_bitpos_to_event(event_nr)))
  1442. {
  1443. active |= 1UL << event_nr;
  1444. }
  1445. to_process &= ~(1UL << event_nr);
  1446. }
  1447. if (nrf_drv_usbd_errata_104())
  1448. {
  1449. /* Event correcting */
  1450. if ((!m_dma_pending) && (0 != (active & (USBD_INTEN_SOF_Msk))))
  1451. {
  1452. uint8_t usbi, uoi, uii;
  1453. /* Testing */
  1454. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7A9;
  1455. uii = (uint8_t)(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  1456. if (0 != uii)
  1457. {
  1458. uii &= (uint8_t)(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  1459. }
  1460. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7AA;
  1461. uoi = (uint8_t)(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  1462. if (0 != uoi)
  1463. {
  1464. uoi &= (uint8_t)(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  1465. }
  1466. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7AB;
  1467. usbi = (uint8_t)(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  1468. if (0 != usbi)
  1469. {
  1470. usbi &= (uint8_t)(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  1471. }
  1472. /* Processing */
  1473. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7AC;
  1474. uii &= (uint8_t)*((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  1475. if (0 != uii)
  1476. {
  1477. uint8_t rb;
  1478. m_simulated_dataepstatus |= ((uint32_t)uii) << USBD_EPIN_BITPOS_0;
  1479. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7A9;
  1480. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) = uii;
  1481. rb = (uint8_t)*((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  1482. NRF_DRV_USBD_LOG_PROTO1_FIX_PRINTF(" uii: 0x%.2x (0x%.2x)", uii, rb);
  1483. }
  1484. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7AD;
  1485. uoi &= (uint8_t)*((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  1486. if (0 != uoi)
  1487. {
  1488. uint8_t rb;
  1489. m_simulated_dataepstatus |= ((uint32_t)uoi) << USBD_EPOUT_BITPOS_0;
  1490. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7AA;
  1491. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) = uoi;
  1492. rb = (uint8_t)*((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  1493. NRF_DRV_USBD_LOG_PROTO1_FIX_PRINTF(" uoi: 0x%.2u (0x%.2x)", uoi, rb);
  1494. }
  1495. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7AE;
  1496. usbi &= (uint8_t)*((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  1497. if (0 != usbi)
  1498. {
  1499. uint8_t rb;
  1500. if (usbi & 0x01)
  1501. {
  1502. active |= USBD_INTEN_EP0SETUP_Msk;
  1503. }
  1504. if (usbi & 0x10)
  1505. {
  1506. active |= USBD_INTEN_USBRESET_Msk;
  1507. }
  1508. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7AB;
  1509. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) = usbi;
  1510. rb = (uint8_t)*((volatile uint32_t *)(NRF_USBD_BASE + 0x804));
  1511. NRF_DRV_USBD_LOG_PROTO1_FIX_PRINTF(" usbi: 0x%.2u (0x%.2x)", usbi, rb);
  1512. }
  1513. if (0 != (m_simulated_dataepstatus &
  1514. ~((1U << USBD_EPOUT_BITPOS_0) | (1U << USBD_EPIN_BITPOS_0))))
  1515. {
  1516. active |= enabled & NRF_USBD_INT_DATAEP_MASK;
  1517. }
  1518. if (0 != (m_simulated_dataepstatus &
  1519. ((1U << USBD_EPOUT_BITPOS_0) | (1U << USBD_EPIN_BITPOS_0))))
  1520. {
  1521. if (0 != (enabled & NRF_USBD_INT_EP0DATADONE_MASK))
  1522. {
  1523. m_simulated_dataepstatus &=
  1524. ~((1U << USBD_EPOUT_BITPOS_0) | (1U << USBD_EPIN_BITPOS_0));
  1525. active |= NRF_USBD_INT_EP0DATADONE_MASK;
  1526. }
  1527. }
  1528. }
  1529. }
  1530. /* Process the active interrupts */
  1531. bool setup_active = 0 != (active & NRF_USBD_INT_EP0SETUP_MASK);
  1532. active &= ~NRF_USBD_INT_EP0SETUP_MASK;
  1533. while (active)
  1534. {
  1535. uint8_t event_nr = __CLZ(__RBIT(active));
  1536. m_isr[event_nr]();
  1537. active &= ~(1UL << event_nr);
  1538. }
  1539. usbd_dmareq_process();
  1540. if (setup_active)
  1541. {
  1542. m_isr[USBD_INTEN_EP0SETUP_Pos]();
  1543. }
  1544. }
  1545. /** @} */
  1546. /** @} */
  1547. ret_code_t nrf_drv_usbd_init(nrf_drv_usbd_event_handler_t const event_handler)
  1548. {
  1549. ASSERT((nrf_drv_usbd_errata_type_52840_eng_a() ||
  1550. nrf_drv_usbd_errata_type_52840_eng_b() ||
  1551. nrf_drv_usbd_errata_type_52840_eng_c() ||
  1552. nrf_drv_usbd_errata_type_52840_eng_d())
  1553. );
  1554. if (NULL == event_handler)
  1555. {
  1556. return NRF_ERROR_INVALID_PARAM;
  1557. }
  1558. if ( m_drv_state != NRFX_DRV_STATE_UNINITIALIZED)
  1559. {
  1560. return NRF_ERROR_INVALID_STATE;
  1561. }
  1562. m_event_handler = event_handler;
  1563. m_drv_state = NRFX_DRV_STATE_INITIALIZED;
  1564. uint8_t n;
  1565. for (n=0; n<NRF_USBD_EPIN_CNT; ++n)
  1566. {
  1567. nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPIN(n);
  1568. nrf_drv_usbd_ep_max_packet_size_set(ep, NRF_USBD_EPISO_CHECK(ep) ?
  1569. (NRF_DRV_USBD_ISOSIZE / 2) : NRF_DRV_USBD_EPSIZE);
  1570. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  1571. p_state->status = NRF_USBD_EP_OK;
  1572. p_state->handler.feeder = NULL;
  1573. p_state->transfer_cnt = 0;
  1574. }
  1575. for (n=0; n<NRF_USBD_EPOUT_CNT; ++n)
  1576. {
  1577. nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPOUT(n);
  1578. nrf_drv_usbd_ep_max_packet_size_set(ep, NRF_USBD_EPISO_CHECK(ep) ?
  1579. (NRF_DRV_USBD_ISOSIZE / 2) : NRF_DRV_USBD_EPSIZE);
  1580. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  1581. p_state->status = NRF_USBD_EP_OK;
  1582. p_state->handler.consumer = NULL;
  1583. p_state->transfer_cnt = 0;
  1584. }
  1585. return NRF_SUCCESS;
  1586. }
  1587. ret_code_t nrf_drv_usbd_uninit(void)
  1588. {
  1589. if (m_drv_state != NRFX_DRV_STATE_INITIALIZED)
  1590. {
  1591. return NRF_ERROR_INVALID_STATE;
  1592. }
  1593. m_event_handler = NULL;
  1594. m_drv_state = NRFX_DRV_STATE_UNINITIALIZED;
  1595. return NRF_SUCCESS;
  1596. }
  1597. void nrf_drv_usbd_enable(void)
  1598. {
  1599. ASSERT(m_drv_state == NRFX_DRV_STATE_INITIALIZED);
  1600. /* Prepare for READY event receiving */
  1601. nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
  1602. if (nrf_drv_usbd_errata_187())
  1603. {
  1604. CRITICAL_REGION_ENTER();
  1605. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1606. {
  1607. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1608. *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
  1609. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1610. }
  1611. else
  1612. {
  1613. *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
  1614. }
  1615. CRITICAL_REGION_EXIT();
  1616. }
  1617. if (nrf_drv_usbd_errata_171())
  1618. {
  1619. CRITICAL_REGION_ENTER();
  1620. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1621. {
  1622. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1623. *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
  1624. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1625. }
  1626. else
  1627. {
  1628. *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
  1629. }
  1630. CRITICAL_REGION_EXIT();
  1631. }
  1632. /* Enable the peripheral */
  1633. nrf_usbd_enable();
  1634. /* Waiting for peripheral to enable, this should take a few us */
  1635. while (0 == (NRF_USBD_EVENTCAUSE_READY_MASK & nrf_usbd_eventcause_get()))
  1636. {
  1637. /* Empty loop */
  1638. }
  1639. nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
  1640. if (nrf_drv_usbd_errata_171())
  1641. {
  1642. CRITICAL_REGION_ENTER();
  1643. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1644. {
  1645. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1646. *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
  1647. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1648. }
  1649. else
  1650. {
  1651. *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
  1652. }
  1653. CRITICAL_REGION_EXIT();
  1654. }
  1655. if (nrf_drv_usbd_errata_166())
  1656. {
  1657. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7E3;
  1658. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) = 0x40;
  1659. __ISB();
  1660. __DSB();
  1661. }
  1662. nrf_usbd_isosplit_set(NRF_USBD_ISOSPLIT_Half);
  1663. if (USBD_CONFIG_ISO_IN_ZLP)
  1664. {
  1665. nrf_drv_usbd_isoinconfig_set(NRF_USBD_ISOINCONFIG_ZERODATA);
  1666. }
  1667. else
  1668. {
  1669. nrf_drv_usbd_isoinconfig_set(NRF_USBD_ISOINCONFIG_NORESP);
  1670. }
  1671. m_ep_ready = (((1U << NRF_USBD_EPIN_CNT) - 1U) << USBD_EPIN_BITPOS_0);
  1672. m_ep_dma_waiting = 0;
  1673. usbd_dma_pending_clear();
  1674. m_last_setup_dir = NRF_DRV_USBD_EPOUT0;
  1675. m_drv_state = NRFX_DRV_STATE_POWERED_ON;
  1676. }
  1677. void nrf_drv_usbd_disable(void)
  1678. {
  1679. ASSERT(m_drv_state != NRFX_DRV_STATE_UNINITIALIZED);
  1680. /* Stop just in case */
  1681. nrf_drv_usbd_stop();
  1682. /* Disable all parts */
  1683. nrf_usbd_int_disable(nrf_usbd_int_enable_get());
  1684. nrf_usbd_disable();
  1685. usbd_dma_pending_clear();
  1686. m_drv_state = NRFX_DRV_STATE_INITIALIZED;
  1687. if (nrf_drv_usbd_errata_187())
  1688. {
  1689. CRITICAL_REGION_ENTER();
  1690. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1691. {
  1692. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1693. *((volatile uint32_t *)(0x4006ED14)) = 0x00000000;
  1694. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1695. }
  1696. else
  1697. {
  1698. *((volatile uint32_t *)(0x4006ED14)) = 0x00000000;
  1699. }
  1700. CRITICAL_REGION_EXIT();
  1701. }
  1702. }
  1703. void nrf_drv_usbd_start(bool enable_sof)
  1704. {
  1705. ASSERT(m_drv_state == NRFX_DRV_STATE_POWERED_ON);
  1706. m_bus_suspend = false;
  1707. uint32_t ints_to_enable =
  1708. NRF_USBD_INT_USBRESET_MASK |
  1709. NRF_USBD_INT_STARTED_MASK |
  1710. NRF_USBD_INT_ENDEPIN0_MASK |
  1711. NRF_USBD_INT_EP0DATADONE_MASK |
  1712. NRF_USBD_INT_ENDEPOUT0_MASK |
  1713. NRF_USBD_INT_USBEVENT_MASK |
  1714. NRF_USBD_INT_EP0SETUP_MASK |
  1715. NRF_USBD_INT_DATAEP_MASK;
  1716. if (enable_sof || nrf_drv_usbd_errata_104())
  1717. {
  1718. ints_to_enable |= NRF_USBD_INT_SOF_MASK;
  1719. }
  1720. /* Enable all required interrupts */
  1721. nrf_usbd_int_enable(ints_to_enable);
  1722. /* Enable interrupt globally */
  1723. NRFX_IRQ_PRIORITY_SET(USBD_IRQn, USBD_CONFIG_IRQ_PRIORITY);
  1724. NRFX_IRQ_ENABLE(USBD_IRQn);
  1725. /* Enable pullups */
  1726. nrf_usbd_pullup_enable();
  1727. }
  1728. void nrf_drv_usbd_stop(void)
  1729. {
  1730. ASSERT(m_drv_state == NRFX_DRV_STATE_POWERED_ON);
  1731. if(NRFX_IRQ_IS_ENABLED(USBD_IRQn))
  1732. {
  1733. /* Abort transfers */
  1734. usbd_ep_abort_all();
  1735. /* Disable pullups */
  1736. nrf_usbd_pullup_disable();
  1737. /* Disable interrupt globally */
  1738. NRFX_IRQ_DISABLE(USBD_IRQn);
  1739. /* Disable all interrupts */
  1740. nrf_usbd_int_disable(~0U);
  1741. }
  1742. }
  1743. bool nrf_drv_usbd_is_initialized(void)
  1744. {
  1745. return (m_drv_state >= NRFX_DRV_STATE_INITIALIZED);
  1746. }
  1747. bool nrf_drv_usbd_is_enabled(void)
  1748. {
  1749. return (m_drv_state >= NRFX_DRV_STATE_POWERED_ON);
  1750. }
  1751. bool nrf_drv_usbd_is_started(void)
  1752. {
  1753. return (nrf_drv_usbd_is_enabled() && NRFX_IRQ_IS_ENABLED(USBD_IRQn));
  1754. }
  1755. bool nrf_drv_usbd_suspend(void)
  1756. {
  1757. bool suspended = false;
  1758. CRITICAL_REGION_ENTER();
  1759. if (m_bus_suspend)
  1760. {
  1761. usbd_ep_abort_all();
  1762. if (!(nrf_usbd_eventcause_get() & NRF_USBD_EVENTCAUSE_RESUME_MASK))
  1763. {
  1764. nrf_usbd_lowpower_enable();
  1765. if (nrf_usbd_eventcause_get() & NRF_USBD_EVENTCAUSE_RESUME_MASK)
  1766. {
  1767. nrf_usbd_lowpower_disable();
  1768. }
  1769. else
  1770. {
  1771. suspended = true;
  1772. if (nrf_drv_usbd_errata_171())
  1773. {
  1774. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1775. {
  1776. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1777. *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
  1778. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1779. }
  1780. else
  1781. {
  1782. *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
  1783. }
  1784. }
  1785. }
  1786. }
  1787. }
  1788. CRITICAL_REGION_EXIT();
  1789. return suspended;
  1790. }
  1791. bool nrf_drv_usbd_wakeup_req(void)
  1792. {
  1793. bool started = false;
  1794. CRITICAL_REGION_ENTER();
  1795. if (m_bus_suspend && nrf_usbd_lowpower_check())
  1796. {
  1797. nrf_usbd_lowpower_disable();
  1798. started = true;
  1799. if (nrf_drv_usbd_errata_171())
  1800. {
  1801. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1802. {
  1803. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1804. *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
  1805. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1806. }
  1807. else
  1808. {
  1809. *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
  1810. }
  1811. }
  1812. }
  1813. CRITICAL_REGION_EXIT();
  1814. return started;
  1815. }
  1816. bool nrf_drv_usbd_suspend_check(void)
  1817. {
  1818. return nrf_usbd_lowpower_check();
  1819. }
  1820. void nrf_drv_usbd_suspend_irq_config(void)
  1821. {
  1822. nrf_usbd_int_disable(m_irq_disabled_in_suspend);
  1823. }
  1824. void nrf_drv_usbd_active_irq_config(void)
  1825. {
  1826. nrf_usbd_int_enable(m_irq_disabled_in_suspend);
  1827. }
  1828. bool nrf_drv_usbd_bus_suspend_check(void)
  1829. {
  1830. return m_bus_suspend;
  1831. }
  1832. void nrf_drv_usbd_force_bus_wakeup(void)
  1833. {
  1834. m_bus_suspend = false;
  1835. }
  1836. void nrf_drv_usbd_ep_max_packet_size_set(nrf_drv_usbd_ep_t ep, uint16_t size)
  1837. {
  1838. /* Only power of 2 size allowed */
  1839. ASSERT((size != 0) && (size & (size - 1)) == 0);
  1840. /* Packet size cannot be higher than maximum buffer size */
  1841. ASSERT( ( NRF_USBD_EPISO_CHECK(ep) && (size <= usbd_ep_iso_capacity(ep)))
  1842. ||
  1843. ((!NRF_USBD_EPISO_CHECK(ep)) && (size <= NRF_DRV_USBD_EPSIZE)));
  1844. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  1845. p_state->max_packet_size = size;
  1846. }
  1847. uint16_t nrf_drv_usbd_ep_max_packet_size_get(nrf_drv_usbd_ep_t ep)
  1848. {
  1849. usbd_drv_ep_state_t const * p_state = ep_state_access(ep);
  1850. return p_state->max_packet_size;
  1851. }
  1852. bool nrf_drv_usbd_ep_enable_check(nrf_drv_usbd_ep_t ep)
  1853. {
  1854. return nrf_usbd_ep_enable_check(ep_to_hal(ep));
  1855. }
  1856. void nrf_drv_usbd_ep_enable(nrf_drv_usbd_ep_t ep)
  1857. {
  1858. nrf_usbd_int_enable(nrf_drv_usbd_ep_to_int(ep));
  1859. if(nrf_usbd_ep_enable_check(ep))
  1860. {
  1861. return;
  1862. }
  1863. nrf_usbd_ep_enable(ep_to_hal(ep));
  1864. if ((NRF_USBD_EP_NR_GET(ep) != 0) && NRF_USBD_EPOUT_CHECK(ep) && (!NRF_USBD_EPISO_CHECK(ep)))
  1865. {
  1866. CRITICAL_REGION_ENTER();
  1867. nrf_drv_usbd_transfer_out_drop(ep);
  1868. m_ep_dma_waiting &= ~(1U << ep2bit(ep));
  1869. CRITICAL_REGION_EXIT();
  1870. }
  1871. }
  1872. void nrf_drv_usbd_ep_disable(nrf_drv_usbd_ep_t ep)
  1873. {
  1874. usbd_ep_abort(ep);
  1875. nrf_usbd_ep_disable(ep_to_hal(ep));
  1876. nrf_usbd_int_disable(nrf_drv_usbd_ep_to_int(ep));
  1877. }
  1878. void nrf_drv_usbd_ep_default_config(void)
  1879. {
  1880. nrf_usbd_int_disable(
  1881. NRF_USBD_INT_ENDEPIN1_MASK |
  1882. NRF_USBD_INT_ENDEPIN2_MASK |
  1883. NRF_USBD_INT_ENDEPIN3_MASK |
  1884. NRF_USBD_INT_ENDEPIN4_MASK |
  1885. NRF_USBD_INT_ENDEPIN5_MASK |
  1886. NRF_USBD_INT_ENDEPIN6_MASK |
  1887. NRF_USBD_INT_ENDEPIN7_MASK |
  1888. NRF_USBD_INT_ENDISOIN0_MASK |
  1889. NRF_USBD_INT_ENDEPOUT1_MASK |
  1890. NRF_USBD_INT_ENDEPOUT2_MASK |
  1891. NRF_USBD_INT_ENDEPOUT3_MASK |
  1892. NRF_USBD_INT_ENDEPOUT4_MASK |
  1893. NRF_USBD_INT_ENDEPOUT5_MASK |
  1894. NRF_USBD_INT_ENDEPOUT6_MASK |
  1895. NRF_USBD_INT_ENDEPOUT7_MASK |
  1896. NRF_USBD_INT_ENDISOOUT0_MASK
  1897. );
  1898. nrf_usbd_int_enable(NRF_USBD_INT_ENDEPIN0_MASK | NRF_USBD_INT_ENDEPOUT0_MASK);
  1899. nrf_usbd_ep_all_disable();
  1900. }
  1901. ret_code_t nrf_drv_usbd_ep_transfer(
  1902. nrf_drv_usbd_ep_t ep,
  1903. nrf_drv_usbd_transfer_t const * const p_transfer)
  1904. {
  1905. ret_code_t ret;
  1906. const uint8_t ep_bitpos = ep2bit(ep);
  1907. ASSERT(NULL != p_transfer);
  1908. CRITICAL_REGION_ENTER();
  1909. /* Setup data transaction can go only in one direction at a time */
  1910. if ((NRF_USBD_EP_NR_GET(ep) == 0) && (ep != m_last_setup_dir))
  1911. {
  1912. ret = NRF_ERROR_INVALID_ADDR;
  1913. if (NRF_USBD_FAILED_TRANSFERS_DEBUG && (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep))))
  1914. {
  1915. NRF_LOG_DEBUG("USB driver: Transfer failed: Invalid EPr\n");
  1916. }
  1917. }
  1918. else if ((m_ep_dma_waiting | ((~m_ep_ready) & USBD_EPIN_BIT_MASK)) & (1U << ep_bitpos))
  1919. {
  1920. /* IN (Device -> Host) transfer has to be transmitted out to allow new transmission */
  1921. ret = NRF_ERROR_BUSY;
  1922. if (NRF_USBD_FAILED_TRANSFERS_DEBUG)
  1923. {
  1924. NRF_LOG_DEBUG("USB driver: Transfer failed: EP is busy");
  1925. }
  1926. }
  1927. else
  1928. {
  1929. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  1930. /* Prepare transfer context and handler description */
  1931. nrf_drv_usbd_transfer_t * p_context;
  1932. if (NRF_USBD_EPIN_CHECK(ep))
  1933. {
  1934. p_context = m_ep_feeder_state + NRF_USBD_EP_NR_GET(ep);
  1935. if (nrfx_is_in_ram(p_transfer->p_data.tx))
  1936. {
  1937. /* RAM */
  1938. if (0 == (p_transfer->flags & NRF_DRV_USBD_TRANSFER_ZLP_FLAG))
  1939. {
  1940. p_state->handler.feeder = nrf_drv_usbd_feeder_ram;
  1941. if (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep)))
  1942. {
  1943. NRF_LOG_DEBUG(
  1944. "USB driver: Transfer called on endpoint %x, size: %u, mode: "
  1945. "RAM",
  1946. ep,
  1947. p_transfer->size);
  1948. }
  1949. }
  1950. else
  1951. {
  1952. p_state->handler.feeder = nrf_drv_usbd_feeder_ram_zlp;
  1953. if (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep)))
  1954. {
  1955. NRF_LOG_DEBUG(
  1956. "USB driver: Transfer called on endpoint %x, size: %u, mode: "
  1957. "RAM_ZLP",
  1958. ep,
  1959. p_transfer->size);
  1960. }
  1961. }
  1962. }
  1963. else
  1964. {
  1965. /* Flash */
  1966. if (0 == (p_transfer->flags & NRF_DRV_USBD_TRANSFER_ZLP_FLAG))
  1967. {
  1968. p_state->handler.feeder = nrf_drv_usbd_feeder_flash;
  1969. if (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep)))
  1970. {
  1971. NRF_LOG_DEBUG(
  1972. "USB driver: Transfer called on endpoint %x, size: %u, mode: "
  1973. "FLASH",
  1974. ep,
  1975. p_transfer->size);
  1976. }
  1977. }
  1978. else
  1979. {
  1980. p_state->handler.feeder = nrf_drv_usbd_feeder_flash_zlp;
  1981. if (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep)))
  1982. {
  1983. NRF_LOG_DEBUG(
  1984. "USB driver: Transfer called on endpoint %x, size: %u, mode: "
  1985. "FLASH_ZLP",
  1986. ep,
  1987. p_transfer->size);
  1988. }
  1989. }
  1990. }
  1991. }
  1992. else
  1993. {
  1994. p_context = m_ep_consumer_state + NRF_USBD_EP_NR_GET(ep);
  1995. ASSERT((p_transfer->p_data.rx == NULL) || (nrfx_is_in_ram(p_transfer->p_data.rx)));
  1996. p_state->handler.consumer = nrf_drv_usbd_consumer;
  1997. }
  1998. *p_context = *p_transfer;
  1999. p_state->p_context = p_context;
  2000. p_state->transfer_cnt = 0;
  2001. p_state->status = NRF_USBD_EP_OK;
  2002. m_ep_dma_waiting |= 1U << ep_bitpos;
  2003. ret = NRF_SUCCESS;
  2004. usbd_int_rise();
  2005. }
  2006. CRITICAL_REGION_EXIT();
  2007. return ret;
  2008. }
  2009. ret_code_t nrf_drv_usbd_ep_handled_transfer(
  2010. nrf_drv_usbd_ep_t ep,
  2011. nrf_drv_usbd_handler_desc_t const * const p_handler)
  2012. {
  2013. ret_code_t ret;
  2014. const uint8_t ep_bitpos = ep2bit(ep);
  2015. ASSERT(NULL != p_handler);
  2016. CRITICAL_REGION_ENTER();
  2017. /* Setup data transaction can go only in one direction at a time */
  2018. if ((NRF_USBD_EP_NR_GET(ep) == 0) && (ep != m_last_setup_dir))
  2019. {
  2020. ret = NRF_ERROR_INVALID_ADDR;
  2021. if (NRF_USBD_FAILED_TRANSFERS_DEBUG && (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep))))
  2022. {
  2023. NRF_LOG_DEBUG("USB driver: Transfer failed: Invalid EP");
  2024. }
  2025. }
  2026. else if ((m_ep_dma_waiting | ((~m_ep_ready) & USBD_EPIN_BIT_MASK)) & (1U << ep_bitpos))
  2027. {
  2028. /* IN (Device -> Host) transfer has to be transmitted out to allow a new transmission */
  2029. ret = NRF_ERROR_BUSY;
  2030. if (NRF_USBD_FAILED_TRANSFERS_DEBUG && (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep))))
  2031. {
  2032. NRF_LOG_DEBUG("USB driver: Transfer failed: EP is busy");\
  2033. }
  2034. }
  2035. else
  2036. {
  2037. /* Transfer can be configured now */
  2038. usbd_drv_ep_state_t * p_state = ep_state_access(ep);
  2039. p_state->transfer_cnt = 0;
  2040. p_state->handler = p_handler->handler;
  2041. p_state->p_context = p_handler->p_context;
  2042. p_state->status = NRF_USBD_EP_OK;
  2043. m_ep_dma_waiting |= 1U << ep_bitpos;
  2044. ret = NRF_SUCCESS;
  2045. if (NRF_USBD_ISO_DEBUG || (!NRF_USBD_EPISO_CHECK(ep)))
  2046. {
  2047. NRF_LOG_DEBUG("USB driver: Transfer called on endpoint %x, mode: Handler", ep);
  2048. }
  2049. usbd_int_rise();
  2050. }
  2051. CRITICAL_REGION_EXIT();
  2052. return ret;
  2053. }
  2054. void * nrf_drv_usbd_feeder_buffer_get(void)
  2055. {
  2056. return m_tx_buffer;
  2057. }
  2058. ret_code_t nrf_drv_usbd_ep_status_get(nrf_drv_usbd_ep_t ep, size_t * p_size)
  2059. {
  2060. ret_code_t ret;
  2061. usbd_drv_ep_state_t const * p_state = ep_state_access(ep);
  2062. CRITICAL_REGION_ENTER();
  2063. *p_size = p_state->transfer_cnt;
  2064. ret = (p_state->handler.consumer == NULL) ? p_state->status : NRF_ERROR_BUSY;
  2065. CRITICAL_REGION_EXIT();
  2066. return ret;
  2067. }
  2068. size_t nrf_drv_usbd_epout_size_get(nrf_drv_usbd_ep_t ep)
  2069. {
  2070. return nrf_usbd_epout_size_get(ep_to_hal(ep));
  2071. }
  2072. bool nrf_drv_usbd_ep_is_busy(nrf_drv_usbd_ep_t ep)
  2073. {
  2074. return (0 != ((m_ep_dma_waiting | ((~m_ep_ready) & USBD_EPIN_BIT_MASK)) & (1U << ep2bit(ep))));
  2075. }
  2076. void nrf_drv_usbd_ep_stall(nrf_drv_usbd_ep_t ep)
  2077. {
  2078. NRF_LOG_DEBUG("USB: EP %x stalled.", ep);
  2079. nrf_usbd_ep_stall(ep_to_hal(ep));
  2080. }
  2081. void nrf_drv_usbd_ep_stall_clear(nrf_drv_usbd_ep_t ep)
  2082. {
  2083. if (NRF_USBD_EPOUT_CHECK(ep) && nrf_drv_usbd_ep_stall_check(ep))
  2084. {
  2085. nrf_drv_usbd_transfer_out_drop(ep);
  2086. }
  2087. nrf_usbd_ep_unstall(ep_to_hal(ep));
  2088. }
  2089. bool nrf_drv_usbd_ep_stall_check(nrf_drv_usbd_ep_t ep)
  2090. {
  2091. return nrf_usbd_ep_is_stall(ep_to_hal(ep));
  2092. }
  2093. void nrf_drv_usbd_ep_dtoggle_clear(nrf_drv_usbd_ep_t ep)
  2094. {
  2095. nrf_usbd_dtoggle_set(ep, NRF_USBD_DTOGGLE_DATA0);
  2096. }
  2097. void nrf_drv_usbd_setup_get(nrf_drv_usbd_setup_t * const p_setup)
  2098. {
  2099. memset(p_setup, 0, sizeof(nrf_drv_usbd_setup_t));
  2100. p_setup->bmRequestType = nrf_usbd_setup_bmrequesttype_get();
  2101. p_setup->bmRequest = nrf_usbd_setup_brequest_get();
  2102. p_setup->wValue = nrf_usbd_setup_wvalue_get();
  2103. p_setup->wIndex = nrf_usbd_setup_windex_get();
  2104. p_setup->wLength = nrf_usbd_setup_wlength_get();
  2105. }
  2106. void nrf_drv_usbd_setup_data_clear(void)
  2107. {
  2108. if (nrf_drv_usbd_errata_104())
  2109. {
  2110. /* For this fix to work properly, it must be ensured that the task is
  2111. * executed twice one after another - blocking ISR. This is however a temporary
  2112. * solution to be used only before production version of the chip. */
  2113. uint32_t primask_copy = __get_PRIMASK();
  2114. __disable_irq();
  2115. nrf_usbd_task_trigger(NRF_USBD_TASK_EP0RCVOUT);
  2116. nrf_usbd_task_trigger(NRF_USBD_TASK_EP0RCVOUT);
  2117. __set_PRIMASK(primask_copy);
  2118. }
  2119. else
  2120. {
  2121. nrf_usbd_task_trigger(NRF_USBD_TASK_EP0RCVOUT);
  2122. }
  2123. }
  2124. void nrf_drv_usbd_setup_clear(void)
  2125. {
  2126. nrf_usbd_task_trigger(NRF_USBD_TASK_EP0STATUS);
  2127. }
  2128. void nrf_drv_usbd_setup_stall(void)
  2129. {
  2130. NRF_LOG_DEBUG("Setup stalled.");
  2131. nrf_usbd_task_trigger(NRF_USBD_TASK_EP0STALL);
  2132. }
  2133. nrf_drv_usbd_ep_t nrf_drv_usbd_last_setup_dir_get(void)
  2134. {
  2135. return m_last_setup_dir;
  2136. }
  2137. void nrf_drv_usbd_transfer_out_drop(nrf_drv_usbd_ep_t ep)
  2138. {
  2139. ASSERT(NRF_USBD_EPOUT_CHECK(ep));
  2140. if (nrf_drv_usbd_errata_200())
  2141. {
  2142. CRITICAL_REGION_ENTER();
  2143. m_ep_ready &= ~(1U << ep2bit(ep));
  2144. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7C5 + (2u * NRF_USBD_EP_NR_GET(ep));
  2145. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) = 0;
  2146. UNUSED_VARIABLE(*((volatile uint32_t *)(NRF_USBD_BASE + 0x804)));
  2147. CRITICAL_REGION_EXIT();
  2148. }
  2149. else
  2150. {
  2151. CRITICAL_REGION_ENTER();
  2152. m_ep_ready &= ~(1U << ep2bit(ep));
  2153. if (!NRF_USBD_EPISO_CHECK(ep))
  2154. {
  2155. nrf_usbd_epout_clear(ep);
  2156. }
  2157. CRITICAL_REGION_EXIT();
  2158. }
  2159. }
  2160. #endif // NRF_MODULE_ENABLED(USBD)