nrfx_usbd.c 73 KB

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