My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uotghs_device_due.c 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. /**
  2. * \file
  3. *
  4. * \brief USB Device Driver for UOTGHS. Compliant with common UDD driver.
  5. *
  6. * Copyright (c) 2012-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifdef ARDUINO_ARCH_SAM
  47. #include "compiler.h"
  48. #include "uotghs_device_due.h"
  49. #include "conf_usb.h"
  50. #include "sysclk.h"
  51. #include "udd.h"
  52. #include "uotghs_otg.h"
  53. #include <string.h>
  54. #ifndef UDD_NO_SLEEP_MGR
  55. # include "sleep.h"
  56. # include "sleepmgr.h"
  57. #endif
  58. #if !(SAM3XA)
  59. # error The current UOTGHS Device Driver supports only SAM3X and SAM3A.
  60. #endif
  61. #ifndef UDD_USB_INT_FUN
  62. # define UDD_USB_INT_FUN UOTGHS_Handler
  63. #endif
  64. #ifndef UDD_USB_INT_LEVEL
  65. # define UDD_USB_INT_LEVEL 5 // By default USB interrupt have low priority
  66. #endif
  67. #define UDD_EP_USED(ep) (USB_DEVICE_MAX_EP >= ep)
  68. #if ( (UDD_EP_USED( 1) && Is_udd_endpoint_dma_supported( 1)) \
  69. ||(UDD_EP_USED( 2) && Is_udd_endpoint_dma_supported( 2)) \
  70. ||(UDD_EP_USED( 3) && Is_udd_endpoint_dma_supported( 3)) \
  71. ||(UDD_EP_USED( 4) && Is_udd_endpoint_dma_supported( 4)) \
  72. ||(UDD_EP_USED( 5) && Is_udd_endpoint_dma_supported( 5)) \
  73. ||(UDD_EP_USED( 6) && Is_udd_endpoint_dma_supported( 6)) \
  74. ||(UDD_EP_USED( 7) && Is_udd_endpoint_dma_supported( 7)) \
  75. ||(UDD_EP_USED( 8) && Is_udd_endpoint_dma_supported( 8)) \
  76. ||(UDD_EP_USED( 9) && Is_udd_endpoint_dma_supported( 9)) \
  77. ||(UDD_EP_USED(10) && Is_udd_endpoint_dma_supported(10)) \
  78. ||(UDD_EP_USED(11) && Is_udd_endpoint_dma_supported(11)) \
  79. ||(UDD_EP_USED(12) && Is_udd_endpoint_dma_supported(12)) \
  80. ||(UDD_EP_USED(13) && Is_udd_endpoint_dma_supported(13)) \
  81. ||(UDD_EP_USED(14) && Is_udd_endpoint_dma_supported(14)) \
  82. ||(UDD_EP_USED(15) && Is_udd_endpoint_dma_supported(15)) \
  83. )
  84. # define UDD_EP_DMA_SUPPORTED
  85. #endif
  86. #if ( (UDD_EP_USED( 1) && !Is_udd_endpoint_dma_supported( 1)) \
  87. ||(UDD_EP_USED( 2) && !Is_udd_endpoint_dma_supported( 2)) \
  88. ||(UDD_EP_USED( 3) && !Is_udd_endpoint_dma_supported( 3)) \
  89. ||(UDD_EP_USED( 4) && !Is_udd_endpoint_dma_supported( 4)) \
  90. ||(UDD_EP_USED( 5) && !Is_udd_endpoint_dma_supported( 5)) \
  91. ||(UDD_EP_USED( 6) && !Is_udd_endpoint_dma_supported( 6)) \
  92. ||(UDD_EP_USED( 7) && !Is_udd_endpoint_dma_supported( 7)) \
  93. ||(UDD_EP_USED( 8) && !Is_udd_endpoint_dma_supported( 8)) \
  94. ||(UDD_EP_USED( 9) && !Is_udd_endpoint_dma_supported( 9)) \
  95. ||(UDD_EP_USED(10) && !Is_udd_endpoint_dma_supported(10)) \
  96. ||(UDD_EP_USED(11) && !Is_udd_endpoint_dma_supported(11)) \
  97. ||(UDD_EP_USED(12) && !Is_udd_endpoint_dma_supported(12)) \
  98. ||(UDD_EP_USED(13) && !Is_udd_endpoint_dma_supported(13)) \
  99. ||(UDD_EP_USED(14) && !Is_udd_endpoint_dma_supported(14)) \
  100. ||(UDD_EP_USED(15) && !Is_udd_endpoint_dma_supported(15)) \
  101. )
  102. # define UDD_EP_FIFO_SUPPORTED
  103. #endif
  104. // for debug text
  105. //#define dbg_print printf
  106. #define dbg_print(...)
  107. /**
  108. * \ingroup udd_group
  109. * \defgroup udd_udphs_group USB On-The-Go High-Speed Port for device mode (UOTGHS)
  110. *
  111. * \section UOTGHS_CONF UOTGHS Custom configuration
  112. * The following UOTGHS driver configuration must be included in the conf_usb.h
  113. * file of the application.
  114. *
  115. * UDD_USB_INT_LEVEL<br>
  116. * Option to change the interrupt priority (0 to 15) by default 5 (recommended).
  117. *
  118. * UDD_USB_INT_FUN<br>
  119. * Option to fit interrupt function to what defined in exception table.
  120. *
  121. * UDD_ISOCHRONOUS_NB_BANK(ep)<br>
  122. * Feature to reduce or increase isochronous endpoints buffering (1 to 3).
  123. * Default value 2.
  124. *
  125. * UDD_BULK_NB_BANK(ep)<br>
  126. * Feature to reduce or increase bulk endpoints buffering (1 to 2).
  127. * Default value 2.
  128. *
  129. * UDD_INTERRUPT_NB_BANK(ep)<br>
  130. * Feature to reduce or increase interrupt endpoints buffering (1 to 2).
  131. * Default value 1.
  132. *
  133. * \section Callbacks management
  134. * The USB driver is fully managed by interrupt and does not request periodique
  135. * task. Thereby, the USB events use callbacks to transfer the information.
  136. * The callbacks are declared in static during compilation or in variable during
  137. * code execution.
  138. *
  139. * Static declarations defined in conf_usb.h:
  140. * - UDC_VBUS_EVENT(bool b_present)<br>
  141. * To signal Vbus level change
  142. * - UDC_SUSPEND_EVENT()<br>
  143. * Called when USB bus enter in suspend mode
  144. * - UDC_RESUME_EVENT()<br>
  145. * Called when USB bus is wakeup
  146. * - UDC_SOF_EVENT()<br>
  147. * Called for each received SOF, Note: Each 1ms in HS/FS mode only.
  148. *
  149. * Dynamic callbacks, called "endpoint job" , are registered
  150. * in udd_ep_job_t structure via the following functions:
  151. * - udd_ep_run()<br>
  152. * To call it when a transfer is finish
  153. * - udd_ep_wait_stall_clear()<br>
  154. * To call it when a endpoint halt is disabled
  155. *
  156. * \section Power mode management
  157. * The Sleep modes authorized :
  158. * - in USB IDLE state, the UOTGHS needs of USB clock and authorizes up to sleep mode WFI.
  159. * - in USB SUSPEND state, the UOTGHS no needs USB clock and authorizes up to sleep mode WAIT.
  160. * @{
  161. */
  162. // Check USB Device configuration
  163. #ifndef USB_DEVICE_EP_CTRL_SIZE
  164. # error USB_DEVICE_EP_CTRL_SIZE not defined
  165. #endif
  166. #ifndef USB_DEVICE_MAX_EP
  167. # error USB_DEVICE_MAX_EP not defined
  168. #endif
  169. // Note: USB_DEVICE_MAX_EP does not include control endpoint
  170. #if USB_DEVICE_MAX_EP > (UDD_MAX_PEP_NB-1)
  171. # error USB_DEVICE_MAX_EP is too high and not supported by this part
  172. #endif
  173. #define UDD_EP_ISO_NBANK_ERROR(ep) \
  174. ( (UDD_ISOCHRONOUS_NB_BANK(ep) < 1) \
  175. || (UDD_ISOCHRONOUS_NB_BANK(ep) > 3) )
  176. #define UDD_EP_BULK_NBANK_ERROR(ep) \
  177. ( (UDD_BULK_NB_BANK(ep) < 1) || (UDD_BULK_NB_BANK(ep) > 2) )
  178. #define UDD_EP_INT_NBANK_ERROR(ep) \
  179. ( (UDD_INTERRUPT_NB_BANK(ep) < 1) || (UDD_INTERRUPT_NB_BANK(ep) > 2) )
  180. #define UDD_EP_ISO_NB_BANK_ERROR(ep) \
  181. (UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
  182. #define UDD_EP_BULK_NB_BANK_ERROR(ep) \
  183. (UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
  184. #define UDD_EP_INT_NB_BANK_ERROR(ep) \
  185. (UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
  186. #define UDD_EP_NB_BANK_ERROR(ep, type) \
  187. (ATPASTE3(UDD_EP_, type, _NB_BANK_ERROR(ep)))
  188. #define UDD_ISO_NB_BANK_ERROR \
  189. ( UDD_EP_NB_BANK_ERROR( 1, ISO) \
  190. || UDD_EP_NB_BANK_ERROR( 2, ISO) \
  191. || UDD_EP_NB_BANK_ERROR( 3, ISO) \
  192. || UDD_EP_NB_BANK_ERROR( 4, ISO) \
  193. || UDD_EP_NB_BANK_ERROR( 5, ISO) \
  194. || UDD_EP_NB_BANK_ERROR( 6, ISO) \
  195. || UDD_EP_NB_BANK_ERROR( 7, ISO) \
  196. || UDD_EP_NB_BANK_ERROR( 8, ISO) \
  197. || UDD_EP_NB_BANK_ERROR( 9, ISO) \
  198. || UDD_EP_NB_BANK_ERROR(10, ISO) \
  199. || UDD_EP_NB_BANK_ERROR(11, ISO) \
  200. || UDD_EP_NB_BANK_ERROR(12, ISO) \
  201. || UDD_EP_NB_BANK_ERROR(13, ISO) \
  202. || UDD_EP_NB_BANK_ERROR(14, ISO) \
  203. || UDD_EP_NB_BANK_ERROR(15, ISO) )
  204. #define UDD_BULK_NB_BANK_ERROR \
  205. ( UDD_EP_NB_BANK_ERROR( 1, BULK) \
  206. || UDD_EP_NB_BANK_ERROR( 2, BULK) \
  207. || UDD_EP_NB_BANK_ERROR( 3, BULK) \
  208. || UDD_EP_NB_BANK_ERROR( 4, BULK) \
  209. || UDD_EP_NB_BANK_ERROR( 5, BULK) \
  210. || UDD_EP_NB_BANK_ERROR( 6, BULK) \
  211. || UDD_EP_NB_BANK_ERROR( 7, BULK) \
  212. || UDD_EP_NB_BANK_ERROR( 8, BULK) \
  213. || UDD_EP_NB_BANK_ERROR( 9, BULK) \
  214. || UDD_EP_NB_BANK_ERROR(10, BULK) \
  215. || UDD_EP_NB_BANK_ERROR(11, BULK) \
  216. || UDD_EP_NB_BANK_ERROR(12, BULK) \
  217. || UDD_EP_NB_BANK_ERROR(13, BULK) \
  218. || UDD_EP_NB_BANK_ERROR(14, BULK) \
  219. || UDD_EP_NB_BANK_ERROR(15, BULK) )
  220. #define UDD_INTERRUPT_NB_BANK_ERROR \
  221. ( UDD_EP_NB_BANK_ERROR( 1, INT) \
  222. || UDD_EP_NB_BANK_ERROR( 2, INT) \
  223. || UDD_EP_NB_BANK_ERROR( 3, INT) \
  224. || UDD_EP_NB_BANK_ERROR( 4, INT) \
  225. || UDD_EP_NB_BANK_ERROR( 5, INT) \
  226. || UDD_EP_NB_BANK_ERROR( 6, INT) \
  227. || UDD_EP_NB_BANK_ERROR( 7, INT) \
  228. || UDD_EP_NB_BANK_ERROR( 8, INT) \
  229. || UDD_EP_NB_BANK_ERROR( 9, INT) \
  230. || UDD_EP_NB_BANK_ERROR(10, INT) \
  231. || UDD_EP_NB_BANK_ERROR(11, INT) \
  232. || UDD_EP_NB_BANK_ERROR(12, INT) \
  233. || UDD_EP_NB_BANK_ERROR(13, INT) \
  234. || UDD_EP_NB_BANK_ERROR(14, INT) \
  235. || UDD_EP_NB_BANK_ERROR(15, INT) )
  236. #ifndef UDD_ISOCHRONOUS_NB_BANK
  237. # define UDD_ISOCHRONOUS_NB_BANK(ep) 2
  238. #else
  239. # if UDD_ISO_NB_BANK_ERROR
  240. # error UDD_ISOCHRONOUS_NB_BANK(ep) must be define within 1 to 3.
  241. # endif
  242. #endif
  243. #ifndef UDD_BULK_NB_BANK
  244. # define UDD_BULK_NB_BANK(ep) 2
  245. #else
  246. # if UDD_BULK_NB_BANK_ERROR
  247. # error UDD_BULK_NB_BANK must be define with 1 or 2.
  248. # endif
  249. #endif
  250. #ifndef UDD_INTERRUPT_NB_BANK
  251. # define UDD_INTERRUPT_NB_BANK(ep) 1
  252. #else
  253. # if UDD_INTERRUPT_NB_BANK_ERROR
  254. # error UDD_INTERRUPT_NB_BANK must be define with 1 or 2.
  255. # endif
  256. #endif
  257. /**
  258. * \name Power management routine.
  259. */
  260. //@{
  261. #ifndef UDD_NO_SLEEP_MGR
  262. //! Definition of sleep levels
  263. #define UOTGHS_SLEEP_MODE_USB_SUSPEND SLEEPMGR_WAIT_FAST
  264. #define UOTGHS_SLEEP_MODE_USB_IDLE SLEEPMGR_SLEEP_WFI
  265. //! State of USB line
  266. static bool udd_b_idle;
  267. //! State of sleep manager
  268. static bool udd_b_sleep_initialized = false;
  269. /*! \brief Authorize or not the CPU powerdown mode
  270. *
  271. * \param b_enable true to authorize idle mode
  272. */
  273. static void udd_sleep_mode(bool b_idle)
  274. {
  275. if (!b_idle && udd_b_idle) {
  276. dbg_print("_S ");
  277. sleepmgr_unlock_mode(UOTGHS_SLEEP_MODE_USB_IDLE);
  278. }
  279. if (b_idle && !udd_b_idle) {
  280. dbg_print("_W ");
  281. sleepmgr_lock_mode(UOTGHS_SLEEP_MODE_USB_IDLE);
  282. }
  283. udd_b_idle = b_idle;
  284. }
  285. #else
  286. static void udd_sleep_mode(bool b_idle)
  287. {
  288. b_idle = b_idle;
  289. }
  290. #endif // UDD_NO_SLEEP_MGR
  291. //@}
  292. /**
  293. * \name Control endpoint low level management routine.
  294. *
  295. * This function performs control endpoint mangement.
  296. * It handle the SETUP/DATA/HANDSHAKE phases of a control transaction.
  297. */
  298. //@{
  299. //! Global variable to give and record information about setup request management
  300. COMPILER_WORD_ALIGNED udd_ctrl_request_t udd_g_ctrlreq;
  301. //! Bit definitions about endpoint control state machine for udd_ep_control_state
  302. typedef enum {
  303. UDD_EPCTRL_SETUP = 0, //!< Wait a SETUP packet
  304. UDD_EPCTRL_DATA_OUT = 1, //!< Wait a OUT data packet
  305. UDD_EPCTRL_DATA_IN = 2, //!< Wait a IN data packet
  306. UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP = 3, //!< Wait a IN ZLP packet
  307. UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP = 4, //!< Wait a OUT ZLP packet
  308. UDD_EPCTRL_STALL_REQ = 5, //!< STALL enabled on IN & OUT packet
  309. } udd_ctrl_ep_state_t;
  310. //! State of the endpoint control management
  311. static udd_ctrl_ep_state_t udd_ep_control_state;
  312. //! Total number of data received/sent during data packet phase with previous payload buffers
  313. static uint16_t udd_ctrl_prev_payload_buf_cnt;
  314. //! Number of data received/sent to/from udd_g_ctrlreq.payload buffer
  315. static uint16_t udd_ctrl_payload_buf_cnt;
  316. /**
  317. * \brief Reset control endpoint
  318. *
  319. * Called after a USB line reset or when UDD is enabled
  320. */
  321. static void udd_reset_ep_ctrl(void);
  322. /**
  323. * \brief Reset control endpoint management
  324. *
  325. * Called after a USB line reset or at the end of SETUP request (after ZLP)
  326. */
  327. static void udd_ctrl_init(void);
  328. //! \brief Managed reception of SETUP packet on control endpoint
  329. static void udd_ctrl_setup_received(void);
  330. //! \brief Managed reception of IN packet on control endpoint
  331. static void udd_ctrl_in_sent(void);
  332. //! \brief Managed reception of OUT packet on control endpoint
  333. static void udd_ctrl_out_received(void);
  334. //! \brief Managed underflow event of IN packet on control endpoint
  335. static void udd_ctrl_underflow(void);
  336. //! \brief Managed overflow event of OUT packet on control endpoint
  337. static void udd_ctrl_overflow(void);
  338. //! \brief Managed stall event of IN/OUT packet on control endpoint
  339. static void udd_ctrl_stall_data(void);
  340. //! \brief Send a ZLP IN on control endpoint
  341. static void udd_ctrl_send_zlp_in(void);
  342. //! \brief Send a ZLP OUT on control endpoint
  343. static void udd_ctrl_send_zlp_out(void);
  344. //! \brief Call callback associated to setup request
  345. static void udd_ctrl_endofrequest(void);
  346. /**
  347. * \brief Main interrupt routine for control endpoint
  348. *
  349. * This switchs control endpoint events to correct sub function.
  350. *
  351. * \return \c 1 if an event about control endpoint is occured, otherwise \c 0.
  352. */
  353. static bool udd_ctrl_interrupt(void);
  354. //@}
  355. /**
  356. * \name Management of bulk/interrupt/isochronous endpoints
  357. *
  358. * The UDD manages the data transfer on endpoints:
  359. * - Start data tranfer on endpoint with USB Device DMA
  360. * - Send a ZLP packet if requested
  361. * - Call callback registered to signal end of transfer
  362. * The transfer abort and stall feature are supported.
  363. */
  364. //@{
  365. #if (0!=USB_DEVICE_MAX_EP)
  366. //! Structure definition about job registered on an endpoint
  367. typedef struct {
  368. union {
  369. //! Callback to call at the end of transfer
  370. udd_callback_trans_t call_trans;
  371. //! Callback to call when the endpoint halt is cleared
  372. udd_callback_halt_cleared_t call_nohalt;
  373. };
  374. //! Buffer located in internal RAM to send or fill during job
  375. uint8_t *buf;
  376. //! Size of buffer to send or fill
  377. iram_size_t buf_size;
  378. //!< Size of data transfered
  379. iram_size_t buf_cnt;
  380. //!< Size of data loaded (or prepared for DMA) last time
  381. iram_size_t buf_load;
  382. //! A job is registered on this endpoint
  383. uint8_t busy:1;
  384. //! A short packet is requested for this job on endpoint IN
  385. uint8_t b_shortpacket:1;
  386. //! A stall has been requested but not executed
  387. uint8_t stall_requested:1;
  388. } udd_ep_job_t;
  389. //! Array to register a job on bulk/interrupt/isochronous endpoint
  390. static udd_ep_job_t udd_ep_job[USB_DEVICE_MAX_EP];
  391. //! \brief Reset all job table
  392. static void udd_ep_job_table_reset(void);
  393. //! \brief Abort all endpoint jobs on going
  394. static void udd_ep_job_table_kill(void);
  395. #ifdef UDD_EP_FIFO_SUPPORTED
  396. /**
  397. * \brief Fill banks and send them
  398. *
  399. * \param ep endpoint number of job to abort
  400. */
  401. static void udd_ep_in_sent(udd_ep_id_t ep);
  402. /**
  403. * \brief Store received banks
  404. *
  405. * \param ep endpoint number of job to abort
  406. */
  407. static void udd_ep_out_received(udd_ep_id_t ep);
  408. #endif
  409. /**
  410. * \brief Abort endpoint job on going
  411. *
  412. * \param ep endpoint number of job to abort
  413. */
  414. static void udd_ep_abort_job(udd_ep_id_t ep);
  415. /**
  416. * \brief Call the callback associated to the job which is finished
  417. *
  418. * \param ptr_job job to complete
  419. * \param b_abort if true then the job has been aborted
  420. */
  421. static void udd_ep_finish_job(udd_ep_job_t * ptr_job, bool b_abort, uint8_t ep_num);
  422. #ifdef UDD_EP_DMA_SUPPORTED
  423. /**
  424. * \brief Start the next transfer if necessary or complet the job associated.
  425. *
  426. * \param ep endpoint number without direction flag
  427. */
  428. static void udd_ep_trans_done(udd_ep_id_t ep);
  429. #endif
  430. /**
  431. * \brief Main interrupt routine for bulk/interrupt/isochronous endpoints
  432. *
  433. * This switchs endpoint events to correct sub function.
  434. *
  435. * \return \c 1 if an event about bulk/interrupt/isochronous endpoints has occured, otherwise \c 0.
  436. */
  437. static bool udd_ep_interrupt(void);
  438. #endif // (0!=USB_DEVICE_MAX_EP)
  439. //@}
  440. // ------------------------
  441. //--- INTERNAL ROUTINES TO MANAGED GLOBAL EVENTS
  442. /**
  443. * \internal
  444. * \brief Function called by UOTGHS interrupt to manage USB Device interrupts
  445. *
  446. * USB Device interrupt events are splited in three parts:
  447. * - USB line events (SOF, reset, suspend, resume, wakeup)
  448. * - control endpoint events (setup reception, end of data transfer, underflow, overflow, stall)
  449. * - bulk/interrupt/isochronous endpoints events (end of data transfer)
  450. *
  451. * Note:
  452. * Here, the global interrupt mask is not clear when an USB interrupt is enabled
  453. * because this one can not be occured during the USB ISR (=during INTX is masked).
  454. * See Technical reference $3.8.3 Masking interrupt requests in peripheral modules.
  455. */
  456. #ifdef UHD_ENABLE
  457. void udd_interrupt(void);
  458. void udd_interrupt(void)
  459. #else
  460. ISR(UDD_USB_INT_FUN)
  461. #endif
  462. {
  463. /* For fast wakeup clocks restore
  464. * In WAIT mode, clocks are switched to FASTRC.
  465. * After wakeup clocks should be restored, before that ISR should not
  466. * be served.
  467. */
  468. if (!pmc_is_wakeup_clocks_restored() && !Is_udd_suspend()) {
  469. cpu_irq_disable();
  470. return;
  471. }
  472. if (Is_udd_sof()) {
  473. udd_ack_sof();
  474. if (Is_udd_full_speed_mode()) {
  475. udc_sof_notify();
  476. }
  477. #ifdef UDC_SOF_EVENT
  478. UDC_SOF_EVENT();
  479. #endif
  480. goto udd_interrupt_sof_end;
  481. }
  482. if (Is_udd_msof()) {
  483. udd_ack_msof();
  484. udc_sof_notify();
  485. goto udd_interrupt_sof_end;
  486. }
  487. dbg_print("%c ", udd_is_high_speed() ? 'H' : 'F');
  488. if (udd_ctrl_interrupt()) {
  489. goto udd_interrupt_end; // Interrupt acked by control endpoint managed
  490. }
  491. #if (0 != USB_DEVICE_MAX_EP)
  492. if (udd_ep_interrupt()) {
  493. goto udd_interrupt_end; // Interrupt acked by bulk/interrupt/isochronous endpoint managed
  494. }
  495. #endif
  496. // USB bus reset detection
  497. if (Is_udd_reset()) {
  498. udd_ack_reset();
  499. dbg_print("RST ");
  500. // Abort all jobs on-going
  501. #if (USB_DEVICE_MAX_EP != 0)
  502. udd_ep_job_table_kill();
  503. #endif
  504. // Reset USB Device Stack Core
  505. udc_reset();
  506. // Reset endpoint control
  507. udd_reset_ep_ctrl();
  508. // Reset endpoint control management
  509. udd_ctrl_init();
  510. goto udd_interrupt_end;
  511. }
  512. if (Is_udd_suspend_interrupt_enabled() && Is_udd_suspend()) {
  513. otg_unfreeze_clock();
  514. // The suspend interrupt is automatic acked when a wakeup occur
  515. udd_disable_suspend_interrupt();
  516. udd_enable_wake_up_interrupt();
  517. otg_freeze_clock(); // Mandatory to exit of sleep mode after a wakeup event
  518. udd_sleep_mode(false); // Enter in SUSPEND mode
  519. #ifdef UDC_SUSPEND_EVENT
  520. UDC_SUSPEND_EVENT();
  521. #endif
  522. goto udd_interrupt_end;
  523. }
  524. if (Is_udd_wake_up_interrupt_enabled() && Is_udd_wake_up()) {
  525. // Ack wakeup interrupt and enable suspend interrupt
  526. otg_unfreeze_clock();
  527. // Check USB clock ready after suspend and eventually sleep USB clock
  528. while (!Is_otg_clock_usable()) {
  529. if (Is_udd_suspend()) {
  530. break; // In case of USB state change in HS
  531. }
  532. };
  533. // The wakeup interrupt is automatic acked when a suspend occur
  534. udd_disable_wake_up_interrupt();
  535. udd_enable_suspend_interrupt();
  536. udd_sleep_mode(true); // Enter in IDLE mode
  537. #ifdef UDC_RESUME_EVENT
  538. UDC_RESUME_EVENT();
  539. #endif
  540. goto udd_interrupt_end;
  541. }
  542. if (Is_otg_vbus_transition()) {
  543. dbg_print("VBus ");
  544. // Ack Vbus transition and send status to high level
  545. otg_unfreeze_clock();
  546. otg_ack_vbus_transition();
  547. otg_freeze_clock();
  548. #ifndef USB_DEVICE_ATTACH_AUTO_DISABLE
  549. if (Is_otg_vbus_high()) {
  550. udd_attach();
  551. } else {
  552. udd_detach();
  553. }
  554. #endif
  555. #ifdef UDC_VBUS_EVENT
  556. UDC_VBUS_EVENT(Is_otg_vbus_high());
  557. #endif
  558. goto udd_interrupt_end;
  559. }
  560. udd_interrupt_end:
  561. dbg_print("\n\r");
  562. udd_interrupt_sof_end:
  563. return;
  564. }
  565. bool udd_include_vbus_monitoring(void)
  566. {
  567. return true;
  568. }
  569. void udd_enable(void)
  570. {
  571. irqflags_t flags;
  572. flags = cpu_irq_save();
  573. #ifdef UHD_ENABLE
  574. // DUAL ROLE INITIALIZATION
  575. if (otg_dual_enable()) {
  576. // The current mode has been started by otg_dual_enable()
  577. cpu_irq_restore(flags);
  578. return;
  579. }
  580. #else
  581. // SINGLE DEVICE MODE INITIALIZATION
  582. pmc_enable_periph_clk(ID_UOTGHS);
  583. sysclk_enable_usb();
  584. // Here, only the device mode is possible, then link UOTGHS interrupt to UDD interrupt
  585. NVIC_SetPriority((IRQn_Type) ID_UOTGHS, UDD_USB_INT_LEVEL);
  586. NVIC_EnableIRQ((IRQn_Type) ID_UOTGHS);
  587. // Always authorize asynchrone USB interrupts to exit of sleep mode
  588. // For SAM USB wake up device except BACKUP mode
  589. pmc_set_fast_startup_input(PMC_FSMR_USBAL);
  590. #endif
  591. #if (defined USB_ID_GPIO) && (defined UHD_ENABLE)
  592. // Check that the device mode is selected by ID pin
  593. if (!Is_otg_id_device()) {
  594. cpu_irq_restore(flags);
  595. return; // Device is not the current mode
  596. }
  597. #else
  598. // ID pin not used then force device mode
  599. otg_disable_id_pin();
  600. otg_force_device_mode();
  601. #endif
  602. // Enable USB hardware
  603. otg_enable_pad();
  604. otg_enable();
  605. // Set the USB speed requested by configuration file
  606. #ifdef USB_DEVICE_LOW_SPEED
  607. udd_low_speed_enable();
  608. #else
  609. udd_low_speed_disable();
  610. # ifdef USB_DEVICE_HS_SUPPORT
  611. udd_high_speed_enable();
  612. # else
  613. udd_high_speed_disable();
  614. # endif
  615. #endif // USB_DEVICE_LOW_SPEED
  616. // Check USB clock
  617. otg_unfreeze_clock();
  618. while (!Is_otg_clock_usable());
  619. // Reset internal variables
  620. #if (0!=USB_DEVICE_MAX_EP)
  621. udd_ep_job_table_reset();
  622. #endif
  623. otg_ack_vbus_transition();
  624. // Force Vbus interrupt in case of Vbus always with a high level
  625. // This is possible with a short timing between a Host mode stop/start.
  626. if (Is_otg_vbus_high()) {
  627. otg_raise_vbus_transition();
  628. }
  629. otg_enable_vbus_interrupt();
  630. otg_freeze_clock();
  631. #ifndef UDD_NO_SLEEP_MGR
  632. if (!udd_b_sleep_initialized) {
  633. udd_b_sleep_initialized = true;
  634. // Initialize the sleep mode authorized for the USB suspend mode
  635. udd_b_idle = false;
  636. sleepmgr_lock_mode(UOTGHS_SLEEP_MODE_USB_SUSPEND);
  637. } else {
  638. udd_sleep_mode(false); // Enter idle mode
  639. }
  640. #endif
  641. cpu_irq_restore(flags);
  642. }
  643. void udd_disable(void)
  644. {
  645. irqflags_t flags;
  646. #ifdef UHD_ENABLE
  647. # ifdef USB_ID_GPIO
  648. if (Is_otg_id_host()) {
  649. // Freeze clock to switch mode
  650. otg_freeze_clock();
  651. udd_detach();
  652. otg_disable();
  653. return; // Host mode running, ignore UDD disable
  654. }
  655. # else
  656. if (Is_otg_host_mode_forced()) {
  657. return; // Host mode running, ignore UDD disable
  658. }
  659. # endif
  660. #endif
  661. flags = cpu_irq_save();
  662. otg_unfreeze_clock();
  663. udd_detach();
  664. #ifndef UDD_NO_SLEEP_MGR
  665. if (udd_b_sleep_initialized) {
  666. udd_b_sleep_initialized = false;
  667. sleepmgr_unlock_mode(UOTGHS_SLEEP_MODE_USB_SUSPEND);
  668. }
  669. #endif
  670. #ifndef UHD_ENABLE
  671. otg_disable();
  672. otg_disable_pad();
  673. sysclk_disable_usb();
  674. pmc_disable_periph_clk(ID_UOTGHS);
  675. // Else the USB clock disable is done by UHC which manage USB dual role
  676. #endif
  677. cpu_irq_restore(flags);
  678. }
  679. void udd_attach(void)
  680. {
  681. irqflags_t flags;
  682. flags = cpu_irq_save();
  683. // At startup the USB bus state is unknown,
  684. // therefore the state is considered IDLE to not miss any USB event
  685. udd_sleep_mode(true);
  686. otg_unfreeze_clock();
  687. // This section of clock check can be improved with a chek of
  688. // USB clock source via sysclk()
  689. // Check USB clock because the source can be a PLL
  690. while (!Is_otg_clock_usable());
  691. // Authorize attach if Vbus is present
  692. udd_attach_device();
  693. // Enable USB line events
  694. udd_enable_reset_interrupt();
  695. udd_enable_suspend_interrupt();
  696. udd_enable_wake_up_interrupt();
  697. udd_enable_sof_interrupt();
  698. #ifdef USB_DEVICE_HS_SUPPORT
  699. udd_enable_msof_interrupt();
  700. #endif
  701. // Reset following interupts flag
  702. udd_ack_reset();
  703. udd_ack_sof();
  704. udd_ack_msof();
  705. // The first suspend interrupt must be forced
  706. // The first suspend interrupt is not detected else raise it
  707. udd_raise_suspend();
  708. udd_ack_wake_up();
  709. otg_freeze_clock();
  710. cpu_irq_restore(flags);
  711. }
  712. void udd_detach(void)
  713. {
  714. otg_unfreeze_clock();
  715. // Detach device from the bus
  716. udd_detach_device();
  717. otg_freeze_clock();
  718. udd_sleep_mode(false);
  719. }
  720. bool udd_is_high_speed(void)
  721. {
  722. #ifdef USB_DEVICE_HS_SUPPORT
  723. return !Is_udd_full_speed_mode();
  724. #else
  725. return false;
  726. #endif
  727. }
  728. void udd_set_address(uint8_t address)
  729. {
  730. udd_disable_address();
  731. udd_configure_address(address);
  732. udd_enable_address();
  733. }
  734. uint8_t udd_getaddress(void)
  735. {
  736. return udd_get_configured_address();
  737. }
  738. uint16_t udd_get_frame_number(void)
  739. {
  740. return udd_frame_number();
  741. }
  742. uint16_t udd_get_micro_frame_number(void)
  743. {
  744. return udd_micro_frame_number();
  745. }
  746. void udd_send_remotewakeup(void)
  747. {
  748. #ifndef UDD_NO_SLEEP_MGR
  749. if (!udd_b_idle)
  750. #endif
  751. {
  752. udd_sleep_mode(true); // Enter in IDLE mode
  753. otg_unfreeze_clock();
  754. udd_initiate_remote_wake_up();
  755. }
  756. }
  757. void udd_set_setup_payload(uint8_t *payload, uint16_t payload_size)
  758. {
  759. udd_g_ctrlreq.payload = payload;
  760. udd_g_ctrlreq.payload_size = payload_size;
  761. }
  762. #if (0 != USB_DEVICE_MAX_EP)
  763. bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes,
  764. uint16_t MaxEndpointSize)
  765. {
  766. bool b_dir_in;
  767. uint16_t ep_allocated;
  768. uint8_t nb_bank, bank, i;
  769. b_dir_in = ep & USB_EP_DIR_IN;
  770. ep = ep & USB_EP_ADDR_MASK;
  771. if (ep > USB_DEVICE_MAX_EP) {
  772. return false;
  773. }
  774. if (Is_udd_endpoint_enabled(ep)) {
  775. return false;
  776. }
  777. dbg_print("alloc(%x, %d) ", ep, MaxEndpointSize);
  778. // Bank choise
  779. switch (bmAttributes & USB_EP_TYPE_MASK) {
  780. case USB_EP_TYPE_ISOCHRONOUS:
  781. nb_bank = UDD_ISOCHRONOUS_NB_BANK(ep);
  782. break;
  783. case USB_EP_TYPE_INTERRUPT:
  784. nb_bank = UDD_INTERRUPT_NB_BANK(ep);
  785. break;
  786. case USB_EP_TYPE_BULK:
  787. nb_bank = UDD_BULK_NB_BANK(ep);
  788. break;
  789. default:
  790. Assert(false);
  791. return false;
  792. }
  793. switch (nb_bank) {
  794. case 1:
  795. bank = UOTGHS_DEVEPTCFG_EPBK_1_BANK >>
  796. UOTGHS_DEVEPTCFG_EPBK_Pos;
  797. break;
  798. case 2:
  799. bank = UOTGHS_DEVEPTCFG_EPBK_2_BANK >>
  800. UOTGHS_DEVEPTCFG_EPBK_Pos;
  801. break;
  802. case 3:
  803. bank = UOTGHS_DEVEPTCFG_EPBK_3_BANK >>
  804. UOTGHS_DEVEPTCFG_EPBK_Pos;
  805. break;
  806. default:
  807. Assert(false);
  808. return false;
  809. }
  810. // Check if endpoint size is 8,16,32,64,128,256,512 or 1023
  811. Assert(MaxEndpointSize < 1024);
  812. Assert((MaxEndpointSize == 1023)
  813. || !(MaxEndpointSize & (MaxEndpointSize - 1)));
  814. Assert(MaxEndpointSize >= 8);
  815. // Set configuration of new endpoint
  816. udd_configure_endpoint(ep, bmAttributes, (b_dir_in ? 1 : 0),
  817. MaxEndpointSize, bank);
  818. ep_allocated = 1 << ep;
  819. // Unalloc endpoints superior
  820. for (i = USB_DEVICE_MAX_EP; i > ep; i--) {
  821. if (Is_udd_endpoint_enabled(i)) {
  822. ep_allocated |= 1 << i;
  823. udd_disable_endpoint(i);
  824. udd_unallocate_memory(i);
  825. }
  826. }
  827. // Realloc/Enable endpoints
  828. for (i = ep; i <= USB_DEVICE_MAX_EP; i++) {
  829. if (ep_allocated & (1 << i)) {
  830. udd_ep_job_t *ptr_job = &udd_ep_job[i - 1];
  831. bool b_restart = ptr_job->busy;
  832. // Restart running job because
  833. // memory window slides up and its data is lost
  834. ptr_job->busy = false;
  835. // Re-allocate memory
  836. udd_allocate_memory(i);
  837. udd_enable_endpoint(i);
  838. if (!Is_udd_endpoint_configured(i)) {
  839. dbg_print("ErrRealloc%d ", i);
  840. if (NULL == ptr_job->call_trans) {
  841. return false;
  842. }
  843. if (Is_udd_endpoint_in(i)) {
  844. i |= USB_EP_DIR_IN;
  845. }
  846. ptr_job->call_trans(UDD_EP_TRANSFER_ABORT,
  847. ptr_job->buf_cnt, i);
  848. return false;
  849. }
  850. udd_enable_endpoint_bank_autoswitch(i);
  851. if (b_restart) {
  852. // Re-run the job remaining part
  853. # ifdef UDD_EP_FIFO_SUPPORTED
  854. if (!Is_udd_endpoint_dma_supported(i)
  855. && !Is_udd_endpoint_in(i)) {
  856. ptr_job->buf_cnt -= ptr_job->buf_load;
  857. }
  858. # else
  859. ptr_job->buf_cnt -= ptr_job->buf_load;
  860. # endif
  861. b_restart = udd_ep_run(Is_udd_endpoint_in(i) ?
  862. (i | USB_EP_DIR_IN) : i,
  863. ptr_job->b_shortpacket,
  864. &ptr_job->buf[ptr_job->buf_cnt],
  865. ptr_job->buf_size
  866. - ptr_job->buf_cnt,
  867. ptr_job->call_trans);
  868. if (!b_restart) {
  869. dbg_print("ErrReRun%d ", i);
  870. return false;
  871. }
  872. }
  873. }
  874. }
  875. return true;
  876. }
  877. void udd_ep_free(udd_ep_id_t ep)
  878. {
  879. uint8_t ep_index = ep & USB_EP_ADDR_MASK;
  880. if (USB_DEVICE_MAX_EP < ep_index) {
  881. return;
  882. }
  883. udd_disable_endpoint(ep_index);
  884. udd_unallocate_memory(ep_index);
  885. udd_ep_abort_job(ep);
  886. udd_ep_job[ep_index - 1].stall_requested = false;
  887. }
  888. bool udd_ep_is_halted(udd_ep_id_t ep)
  889. {
  890. uint8_t ep_index = ep & USB_EP_ADDR_MASK;
  891. return Is_udd_endpoint_stall_requested(ep_index);
  892. }
  893. bool udd_ep_set_halt(udd_ep_id_t ep)
  894. {
  895. uint8_t ep_index = ep & USB_EP_ADDR_MASK;
  896. udd_ep_job_t *ptr_job = &udd_ep_job[ep_index - 1];
  897. irqflags_t flags;
  898. if (USB_DEVICE_MAX_EP < ep_index) {
  899. return false;
  900. }
  901. if (Is_udd_endpoint_stall_requested(ep_index) // Endpoint stalled
  902. || ptr_job->stall_requested) { // Endpoint stall is requested
  903. return true; // Already STALL
  904. }
  905. if (ptr_job->busy == true) {
  906. return false; // Job on going, stall impossible
  907. }
  908. flags = cpu_irq_save();
  909. if ((ep & USB_EP_DIR_IN) && (0 != udd_nb_busy_bank(ep_index))) {
  910. // Delay the stall after the end of IN transfer on USB line
  911. ptr_job->stall_requested = true;
  912. #ifdef UDD_EP_FIFO_SUPPORTED
  913. udd_disable_in_send_interrupt(ep_index);
  914. udd_enable_endpoint_bank_autoswitch(ep_index);
  915. #endif
  916. udd_enable_bank_interrupt(ep_index);
  917. udd_enable_endpoint_interrupt(ep_index);
  918. cpu_irq_restore(flags);
  919. return true;
  920. }
  921. // Stall endpoint immediately
  922. udd_disable_endpoint_bank_autoswitch(ep_index);
  923. udd_ack_stall(ep_index);
  924. udd_enable_stall_handshake(ep_index);
  925. cpu_irq_restore(flags);
  926. return true;
  927. }
  928. bool udd_ep_clear_halt(udd_ep_id_t ep)
  929. {
  930. uint8_t ep_index = ep & USB_EP_ADDR_MASK;
  931. udd_ep_job_t *ptr_job = &udd_ep_job[ep_index - 1];
  932. bool b_stall_cleared = false;
  933. if (USB_DEVICE_MAX_EP < ep_index)
  934. return false;
  935. if (ptr_job->stall_requested) {
  936. // Endpoint stall has been requested but not done
  937. // Remove stall request
  938. ptr_job->stall_requested = false;
  939. udd_disable_bank_interrupt(ep_index);
  940. udd_disable_endpoint_interrupt(ep_index);
  941. b_stall_cleared = true;
  942. }
  943. if (Is_udd_endpoint_stall_requested(ep_index)) {
  944. if (Is_udd_stall(ep_index)) {
  945. udd_ack_stall(ep_index);
  946. // A packet has been stalled
  947. // then reset datatoggle
  948. udd_reset_data_toggle(ep_index);
  949. }
  950. // Disable stall
  951. udd_disable_stall_handshake(ep_index);
  952. udd_enable_endpoint_bank_autoswitch(ep_index);
  953. b_stall_cleared = true;
  954. }
  955. if (b_stall_cleared) {
  956. // If a job is register on clear halt action
  957. // then execute callback
  958. if (ptr_job->busy == true) {
  959. ptr_job->busy = false;
  960. ptr_job->call_nohalt();
  961. }
  962. }
  963. return true;
  964. }
  965. bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket,
  966. uint8_t * buf, iram_size_t buf_size,
  967. udd_callback_trans_t callback)
  968. {
  969. #ifdef UDD_EP_FIFO_SUPPORTED
  970. bool b_dir_in = Is_udd_endpoint_in(ep & USB_EP_ADDR_MASK);
  971. #endif
  972. udd_ep_job_t *ptr_job;
  973. irqflags_t flags;
  974. ep &= USB_EP_ADDR_MASK;
  975. if (USB_DEVICE_MAX_EP < ep) {
  976. return false;
  977. }
  978. // Get job about endpoint
  979. ptr_job = &udd_ep_job[ep - 1];
  980. if ((!Is_udd_endpoint_enabled(ep))
  981. || Is_udd_endpoint_stall_requested(ep)
  982. || ptr_job->stall_requested) {
  983. return false; // Endpoint is halted
  984. }
  985. flags = cpu_irq_save();
  986. if (ptr_job->busy == true) {
  987. cpu_irq_restore(flags);
  988. return false; // Job already on going
  989. }
  990. ptr_job->busy = true;
  991. cpu_irq_restore(flags);
  992. // No job running. Let's setup a new one.
  993. ptr_job->buf = buf;
  994. ptr_job->buf_size = buf_size;
  995. ptr_job->buf_cnt = 0;
  996. ptr_job->buf_load = 0;
  997. ptr_job->call_trans = callback;
  998. ptr_job->b_shortpacket = b_shortpacket || (buf_size == 0);
  999. #ifdef UDD_EP_FIFO_SUPPORTED
  1000. // No DMA support
  1001. if (!Is_udd_endpoint_dma_supported(ep)) {
  1002. dbg_print("ex%x.%c%d\n\r", ep, b_dir_in ? 'i':'o', buf_size);
  1003. flags = cpu_irq_save();
  1004. udd_enable_endpoint_interrupt(ep);
  1005. if (b_dir_in) {
  1006. udd_disable_endpoint_bank_autoswitch(ep);
  1007. udd_enable_in_send_interrupt(ep);
  1008. } else {
  1009. udd_disable_endpoint_bank_autoswitch(ep);
  1010. udd_enable_out_received_interrupt(ep);
  1011. }
  1012. cpu_irq_restore(flags);
  1013. return true;
  1014. }
  1015. #endif // UDD_EP_FIFO_SUPPORTED
  1016. #ifdef UDD_EP_DMA_SUPPORTED
  1017. // Request first DMA transfer
  1018. dbg_print("(exDMA%x) ", ep);
  1019. udd_ep_trans_done(ep);
  1020. return true;
  1021. #endif
  1022. }
  1023. void udd_ep_abort(udd_ep_id_t ep)
  1024. {
  1025. uint8_t ep_index = ep & USB_EP_ADDR_MASK;
  1026. #ifdef UDD_EP_FIFO_SUPPORTED
  1027. if (!Is_udd_endpoint_dma_supported(ep_index)) {
  1028. // Disable interrupts
  1029. udd_disable_endpoint_interrupt(ep_index);
  1030. udd_disable_out_received_interrupt(ep_index);
  1031. udd_disable_in_send_interrupt(ep_index);
  1032. } else
  1033. #endif
  1034. {
  1035. // Stop DMA transfer
  1036. udd_disable_endpoint_dma_interrupt(ep_index);
  1037. udd_endpoint_dma_set_control(ep_index, 0);
  1038. }
  1039. udd_disable_endpoint_interrupt(ep_index);
  1040. // Kill IN banks
  1041. if (ep & USB_EP_DIR_IN) {
  1042. while(udd_nb_busy_bank(ep_index)) {
  1043. udd_kill_last_in_bank(ep_index);
  1044. while(Is_udd_kill_last(ep_index));
  1045. }
  1046. }
  1047. udd_ep_abort_job(ep);
  1048. }
  1049. bool udd_ep_wait_stall_clear(udd_ep_id_t ep,
  1050. udd_callback_halt_cleared_t callback)
  1051. {
  1052. udd_ep_job_t *ptr_job;
  1053. ep &= USB_EP_ADDR_MASK;
  1054. if (USB_DEVICE_MAX_EP < ep) {
  1055. return false;
  1056. }
  1057. ptr_job = &udd_ep_job[ep - 1];
  1058. if (!Is_udd_endpoint_enabled(ep)) {
  1059. return false; // Endpoint not enabled
  1060. }
  1061. // Wait clear halt endpoint
  1062. if (ptr_job->busy == true) {
  1063. return false; // Job already on going
  1064. }
  1065. if (Is_udd_endpoint_stall_requested(ep)
  1066. || ptr_job->stall_requested) {
  1067. // Endpoint halted then registes the callback
  1068. ptr_job->busy = true;
  1069. ptr_job->call_nohalt = callback;
  1070. } else {
  1071. // endpoint not halted then call directly callback
  1072. callback();
  1073. }
  1074. return true;
  1075. }
  1076. #endif // (0 != USB_DEVICE_MAX_EP)
  1077. #ifdef USB_DEVICE_HS_SUPPORT
  1078. void udd_test_mode_j(void)
  1079. {
  1080. udd_enable_hs_test_mode();
  1081. udd_enable_hs_test_mode_j();
  1082. }
  1083. void udd_test_mode_k(void)
  1084. {
  1085. udd_enable_hs_test_mode();
  1086. udd_enable_hs_test_mode_k();
  1087. }
  1088. void udd_test_mode_se0_nak(void)
  1089. {
  1090. udd_enable_hs_test_mode();
  1091. }
  1092. void udd_test_mode_packet(void)
  1093. {
  1094. uint8_t i;
  1095. uint8_t *ptr_dest;
  1096. const uint8_t *ptr_src;
  1097. const uint8_t test_packet[] = {
  1098. // 00000000 * 9
  1099. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1100. // 01010101 * 8
  1101. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  1102. // 01110111 * 8
  1103. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  1104. // 0, {111111S * 15}, 111111
  1105. 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  1106. 0xFF, 0xFF,
  1107. // S, 111111S, {0111111S * 7}
  1108. 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
  1109. // 00111111, {S0111111 * 9}, S0
  1110. 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
  1111. };
  1112. // Reconfigure control endpoint to bulk IN endpoint
  1113. udd_disable_endpoint(0);
  1114. udd_configure_endpoint(0, USB_EP_TYPE_BULK, 1,
  1115. 64, UOTGHS_DEVEPTCFG_EPBK_1_BANK);
  1116. udd_allocate_memory(0);
  1117. udd_enable_endpoint(0);
  1118. udd_enable_hs_test_mode();
  1119. udd_enable_hs_test_mode_packet();
  1120. // Send packet on endpoint 0
  1121. ptr_dest = (uint8_t *) & udd_get_endpoint_fifo_access(0, 8);
  1122. ptr_src = test_packet;
  1123. for (i = 0; i < sizeof(test_packet); i++) {
  1124. *ptr_dest++ = *ptr_src++;
  1125. }
  1126. udd_ack_fifocon(0);
  1127. }
  1128. #endif // USB_DEVICE_HS_SUPPORT
  1129. // ------------------------
  1130. //--- INTERNAL ROUTINES TO MANAGED THE CONTROL ENDPOINT
  1131. static void udd_reset_ep_ctrl(void)
  1132. {
  1133. irqflags_t flags;
  1134. // Reset USB address to 0
  1135. udd_configure_address(0);
  1136. udd_enable_address();
  1137. // Alloc and configure control endpoint
  1138. udd_configure_endpoint(0,
  1139. USB_EP_TYPE_CONTROL,
  1140. 0,
  1141. USB_DEVICE_EP_CTRL_SIZE,
  1142. UOTGHS_DEVEPTCFG_EPBK_1_BANK);
  1143. udd_allocate_memory(0);
  1144. udd_enable_endpoint(0);
  1145. flags = cpu_irq_save();
  1146. udd_enable_setup_received_interrupt(0);
  1147. udd_enable_out_received_interrupt(0);
  1148. udd_enable_endpoint_interrupt(0);
  1149. cpu_irq_restore(flags);
  1150. }
  1151. static void udd_ctrl_init(void)
  1152. {
  1153. irqflags_t flags;
  1154. flags = cpu_irq_save();
  1155. // In case of abort of IN Data Phase:
  1156. // No need to abort IN transfer (rise TXINI),
  1157. // because it is automatically done by hardware when a Setup packet is received.
  1158. // But the interrupt must be disabled to don't generate interrupt TXINI
  1159. // after SETUP reception.
  1160. udd_disable_in_send_interrupt(0);
  1161. cpu_irq_restore(flags);
  1162. // In case of OUT ZLP event is no processed before Setup event occurs
  1163. udd_ack_out_received(0);
  1164. udd_g_ctrlreq.callback = NULL;
  1165. udd_g_ctrlreq.over_under_run = NULL;
  1166. udd_g_ctrlreq.payload_size = 0;
  1167. udd_ep_control_state = UDD_EPCTRL_SETUP;
  1168. }
  1169. static void udd_ctrl_setup_received(void)
  1170. {
  1171. irqflags_t flags;
  1172. uint8_t i;
  1173. if (UDD_EPCTRL_SETUP != udd_ep_control_state) {
  1174. // May be a hidden DATA or ZLP phase or protocol abort
  1175. udd_ctrl_endofrequest();
  1176. // Reinitializes control endpoint management
  1177. udd_ctrl_init();
  1178. }
  1179. // Fill setup request structure
  1180. if (8 != udd_byte_count(0)) {
  1181. udd_ctrl_stall_data();
  1182. udd_ack_setup_received(0);
  1183. return; // Error data number doesn't correspond to SETUP packet
  1184. }
  1185. uint8_t *ptr = (uint8_t *) & udd_get_endpoint_fifo_access(0,8);
  1186. for (i = 0; i < 8; i++) {
  1187. ((uint8_t*) &udd_g_ctrlreq.req)[i] = *ptr++;
  1188. }
  1189. // Manage LSB/MSB to fit with CPU usage
  1190. udd_g_ctrlreq.req.wValue = le16_to_cpu(udd_g_ctrlreq.req.wValue);
  1191. udd_g_ctrlreq.req.wIndex = le16_to_cpu(udd_g_ctrlreq.req.wIndex);
  1192. udd_g_ctrlreq.req.wLength = le16_to_cpu(udd_g_ctrlreq.req.wLength);
  1193. // Decode setup request
  1194. if (udc_process_setup() == false) {
  1195. // Setup request unknow then stall it
  1196. udd_ctrl_stall_data();
  1197. udd_ack_setup_received(0);
  1198. return;
  1199. }
  1200. udd_ack_setup_received(0);
  1201. if (Udd_setup_is_in()) {
  1202. // IN data phase requested
  1203. udd_ctrl_prev_payload_buf_cnt = 0;
  1204. udd_ctrl_payload_buf_cnt = 0;
  1205. udd_ep_control_state = UDD_EPCTRL_DATA_IN;
  1206. udd_ctrl_in_sent(); // Send first data transfer
  1207. } else {
  1208. if (0 == udd_g_ctrlreq.req.wLength) {
  1209. // No data phase requested
  1210. // Send IN ZLP to ACK setup request
  1211. udd_ctrl_send_zlp_in();
  1212. return;
  1213. }
  1214. // OUT data phase requested
  1215. udd_ctrl_prev_payload_buf_cnt = 0;
  1216. udd_ctrl_payload_buf_cnt = 0;
  1217. udd_ep_control_state = UDD_EPCTRL_DATA_OUT;
  1218. // To detect a protocol error, enable nak interrupt on data IN phase
  1219. udd_ack_nak_in(0);
  1220. flags = cpu_irq_save();
  1221. udd_enable_nak_in_interrupt(0);
  1222. cpu_irq_restore(flags);
  1223. }
  1224. }
  1225. static void udd_ctrl_in_sent(void)
  1226. {
  1227. static bool b_shortpacket = false;
  1228. uint16_t nb_remain;
  1229. uint8_t i;
  1230. uint8_t *ptr_dest, *ptr_src;
  1231. irqflags_t flags;
  1232. flags = cpu_irq_save();
  1233. udd_disable_in_send_interrupt(0);
  1234. cpu_irq_restore(flags);
  1235. if (UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP == udd_ep_control_state) {
  1236. // ZLP on IN is sent, then valid end of setup request
  1237. udd_ctrl_endofrequest();
  1238. // Reinitializes control endpoint management
  1239. udd_ctrl_init();
  1240. return;
  1241. }
  1242. Assert(udd_ep_control_state == UDD_EPCTRL_DATA_IN);
  1243. nb_remain = udd_g_ctrlreq.payload_size - udd_ctrl_payload_buf_cnt;
  1244. if (0 == nb_remain) {
  1245. // All content of current buffer payload are sent
  1246. // Update number of total data sending by previous playlaod buffer
  1247. udd_ctrl_prev_payload_buf_cnt += udd_ctrl_payload_buf_cnt;
  1248. if ((udd_g_ctrlreq.req.wLength == udd_ctrl_prev_payload_buf_cnt)
  1249. || b_shortpacket) {
  1250. // All data requested are transfered or a short packet has been sent
  1251. // then it is the end of data phase.
  1252. // Generate an OUT ZLP for handshake phase.
  1253. udd_ctrl_send_zlp_out();
  1254. return;
  1255. }
  1256. // Need of new buffer because the data phase is not complete
  1257. if ((!udd_g_ctrlreq.over_under_run)
  1258. || (!udd_g_ctrlreq.over_under_run())) {
  1259. // Underrun then send zlp on IN
  1260. // Here nb_remain=0 and allows to send a IN ZLP
  1261. } else {
  1262. // A new payload buffer is given
  1263. udd_ctrl_payload_buf_cnt = 0;
  1264. nb_remain = udd_g_ctrlreq.payload_size;
  1265. }
  1266. }
  1267. // Continue transfer and send next data
  1268. if (nb_remain >= USB_DEVICE_EP_CTRL_SIZE) {
  1269. nb_remain = USB_DEVICE_EP_CTRL_SIZE;
  1270. b_shortpacket = false;
  1271. } else {
  1272. b_shortpacket = true;
  1273. }
  1274. // Fill buffer of endpoint control
  1275. ptr_dest = (uint8_t *) & udd_get_endpoint_fifo_access(0, 8);
  1276. ptr_src = udd_g_ctrlreq.payload + udd_ctrl_payload_buf_cnt;
  1277. // Critical section
  1278. // Only in case of DATA IN phase abort without USB Reset signal after.
  1279. // The IN data don't must be written in endpoint 0 DPRAM during
  1280. // a next setup reception in same endpoint 0 DPRAM.
  1281. // Thereby, an OUT ZLP reception must check before IN data write
  1282. // and if no OUT ZLP is received the data must be written quickly (800µs)
  1283. // before an eventually ZLP OUT and SETUP reception
  1284. flags = cpu_irq_save();
  1285. if (Is_udd_out_received(0)) {
  1286. // IN DATA phase aborted by OUT ZLP
  1287. cpu_irq_restore(flags);
  1288. udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP;
  1289. return; // Exit of IN DATA phase
  1290. }
  1291. // Write quickly the IN data
  1292. for (i = 0; i < nb_remain; i++) {
  1293. *ptr_dest++ = *ptr_src++;
  1294. }
  1295. udd_ctrl_payload_buf_cnt += nb_remain;
  1296. // Validate and send the data available in the control endpoint buffer
  1297. udd_ack_in_send(0);
  1298. udd_enable_in_send_interrupt(0);
  1299. // In case of abort of DATA IN phase, no need to enable nak OUT interrupt
  1300. // because OUT endpoint is already free and ZLP OUT accepted.
  1301. cpu_irq_restore(flags);
  1302. }
  1303. static void udd_ctrl_out_received(void)
  1304. {
  1305. irqflags_t flags;
  1306. uint8_t i;
  1307. uint16_t nb_data;
  1308. if (UDD_EPCTRL_DATA_OUT != udd_ep_control_state) {
  1309. if ((UDD_EPCTRL_DATA_IN == udd_ep_control_state)
  1310. || (UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP ==
  1311. udd_ep_control_state)) {
  1312. // End of SETUP request:
  1313. // - Data IN Phase aborted,
  1314. // - or last Data IN Phase hidden by ZLP OUT sending quiclky,
  1315. // - or ZLP OUT received normaly.
  1316. udd_ctrl_endofrequest();
  1317. } else {
  1318. // Protocol error during SETUP request
  1319. udd_ctrl_stall_data();
  1320. }
  1321. // Reinitializes control endpoint management
  1322. udd_ctrl_init();
  1323. return;
  1324. }
  1325. // Read data received during OUT phase
  1326. nb_data = udd_byte_count(0);
  1327. if (udd_g_ctrlreq.payload_size < (udd_ctrl_payload_buf_cnt + nb_data)) {
  1328. // Payload buffer too small
  1329. nb_data = udd_g_ctrlreq.payload_size - udd_ctrl_payload_buf_cnt;
  1330. }
  1331. uint8_t *ptr_src = (uint8_t *) & udd_get_endpoint_fifo_access(0, 8);
  1332. uint8_t *ptr_dest = udd_g_ctrlreq.payload + udd_ctrl_payload_buf_cnt;
  1333. for (i = 0; i < nb_data; i++) {
  1334. *ptr_dest++ = *ptr_src++;
  1335. }
  1336. udd_ctrl_payload_buf_cnt += nb_data;
  1337. if ((USB_DEVICE_EP_CTRL_SIZE != nb_data)
  1338. || (udd_g_ctrlreq.req.wLength <=
  1339. (udd_ctrl_prev_payload_buf_cnt +
  1340. udd_ctrl_payload_buf_cnt))) {
  1341. // End of reception because it is a short packet
  1342. // Before send ZLP, call intermediat calback
  1343. // in case of data receiv generate a stall
  1344. udd_g_ctrlreq.payload_size = udd_ctrl_payload_buf_cnt;
  1345. if (NULL != udd_g_ctrlreq.over_under_run) {
  1346. if (!udd_g_ctrlreq.over_under_run()) {
  1347. // Stall ZLP
  1348. udd_ctrl_stall_data();
  1349. // Ack reception of OUT to replace NAK by a STALL
  1350. udd_ack_out_received(0);
  1351. return;
  1352. }
  1353. }
  1354. // Send IN ZLP to ACK setup request
  1355. udd_ack_out_received(0);
  1356. udd_ctrl_send_zlp_in();
  1357. return;
  1358. }
  1359. if (udd_g_ctrlreq.payload_size == udd_ctrl_payload_buf_cnt) {
  1360. // Overrun then request a new payload buffer
  1361. if (!udd_g_ctrlreq.over_under_run) {
  1362. // No callback availabled to request a new payload buffer
  1363. udd_ctrl_stall_data();
  1364. // Ack reception of OUT to replace NAK by a STALL
  1365. udd_ack_out_received(0);
  1366. return;
  1367. }
  1368. if (!udd_g_ctrlreq.over_under_run()) {
  1369. // No new payload buffer delivered
  1370. udd_ctrl_stall_data();
  1371. // Ack reception of OUT to replace NAK by a STALL
  1372. udd_ack_out_received(0);
  1373. return;
  1374. }
  1375. // New payload buffer available
  1376. // Update number of total data received
  1377. udd_ctrl_prev_payload_buf_cnt += udd_ctrl_payload_buf_cnt;
  1378. // Reinit reception on payload buffer
  1379. udd_ctrl_payload_buf_cnt = 0;
  1380. }
  1381. // Free buffer of control endpoint to authorize next reception
  1382. udd_ack_out_received(0);
  1383. // To detect a protocol error, enable nak interrupt on data IN phase
  1384. udd_ack_nak_in(0);
  1385. flags = cpu_irq_save();
  1386. udd_enable_nak_in_interrupt(0);
  1387. cpu_irq_restore(flags);
  1388. }
  1389. static void udd_ctrl_underflow(void)
  1390. {
  1391. if (Is_udd_out_received(0))
  1392. return; // Underflow ignored if OUT data is received
  1393. if (UDD_EPCTRL_DATA_OUT == udd_ep_control_state) {
  1394. // Host want to stop OUT transaction
  1395. // then stop to wait OUT data phase and wait IN ZLP handshake
  1396. udd_ctrl_send_zlp_in();
  1397. } else if (UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP == udd_ep_control_state) {
  1398. // A OUT handshake is waiting by device,
  1399. // but host want extra IN data then stall extra IN data
  1400. udd_enable_stall_handshake(0);
  1401. }
  1402. }
  1403. static void udd_ctrl_overflow(void)
  1404. {
  1405. if (Is_udd_in_send(0))
  1406. return; // Overflow ignored if IN data is received
  1407. // The case of UDD_EPCTRL_DATA_IN is not managed
  1408. // because the OUT endpoint is already free and OUT ZLP accepted
  1409. if (UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP == udd_ep_control_state) {
  1410. // A IN handshake is waiting by device,
  1411. // but host want extra OUT data then stall extra OUT data
  1412. udd_enable_stall_handshake(0);
  1413. }
  1414. }
  1415. static void udd_ctrl_stall_data(void)
  1416. {
  1417. // Stall all packets on IN & OUT control endpoint
  1418. udd_ep_control_state = UDD_EPCTRL_STALL_REQ;
  1419. udd_enable_stall_handshake(0);
  1420. }
  1421. static void udd_ctrl_send_zlp_in(void)
  1422. {
  1423. irqflags_t flags;
  1424. udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP;
  1425. // Validate and send empty IN packet on control endpoint
  1426. flags = cpu_irq_save();
  1427. // Send ZLP on IN endpoint
  1428. udd_ack_in_send(0);
  1429. udd_enable_in_send_interrupt(0);
  1430. // To detect a protocol error, enable nak interrupt on data OUT phase
  1431. udd_ack_nak_out(0);
  1432. udd_enable_nak_out_interrupt(0);
  1433. cpu_irq_restore(flags);
  1434. }
  1435. static void udd_ctrl_send_zlp_out(void)
  1436. {
  1437. irqflags_t flags;
  1438. udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP;
  1439. // No action is necessary to accept OUT ZLP
  1440. // because the buffer of control endpoint is already free
  1441. // To detect a protocol error, enable nak interrupt on data IN phase
  1442. flags = cpu_irq_save();
  1443. udd_ack_nak_in(0);
  1444. udd_enable_nak_in_interrupt(0);
  1445. cpu_irq_restore(flags);
  1446. }
  1447. static void udd_ctrl_endofrequest(void)
  1448. {
  1449. // If a callback is registered then call it
  1450. if (udd_g_ctrlreq.callback) {
  1451. udd_g_ctrlreq.callback();
  1452. }
  1453. }
  1454. static bool udd_ctrl_interrupt(void)
  1455. {
  1456. if (!Is_udd_endpoint_interrupt(0)) {
  1457. return false; // No interrupt events on control endpoint
  1458. }
  1459. dbg_print("0: ");
  1460. // By default disable overflow and underflow interrupt
  1461. udd_disable_nak_in_interrupt(0);
  1462. udd_disable_nak_out_interrupt(0);
  1463. // Search event on control endpoint
  1464. if (Is_udd_setup_received(0)) {
  1465. dbg_print("stup ");
  1466. // SETUP packet received
  1467. udd_ctrl_setup_received();
  1468. return true;
  1469. }
  1470. if (Is_udd_in_send(0) && Is_udd_in_send_interrupt_enabled(0)) {
  1471. dbg_print("in ");
  1472. // IN packet sent
  1473. udd_ctrl_in_sent();
  1474. return true;
  1475. }
  1476. if (Is_udd_out_received(0)) {
  1477. dbg_print("out ");
  1478. // OUT packet received
  1479. udd_ctrl_out_received();
  1480. return true;
  1481. }
  1482. if (Is_udd_nak_out(0)) {
  1483. dbg_print("nako ");
  1484. // Overflow on OUT packet
  1485. udd_ack_nak_out(0);
  1486. udd_ctrl_overflow();
  1487. return true;
  1488. }
  1489. if (Is_udd_nak_in(0)) {
  1490. dbg_print("naki ");
  1491. // Underflow on IN packet
  1492. udd_ack_nak_in(0);
  1493. udd_ctrl_underflow();
  1494. return true;
  1495. }
  1496. dbg_print("n%x ", UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], 0));
  1497. return false;
  1498. }
  1499. // ------------------------
  1500. //--- INTERNAL ROUTINES TO MANAGED THE BULK/INTERRUPT/ISOCHRONOUS ENDPOINTS
  1501. #if (0 != USB_DEVICE_MAX_EP)
  1502. static void udd_ep_job_table_reset(void)
  1503. {
  1504. uint8_t i;
  1505. for (i = 0; i < USB_DEVICE_MAX_EP; i++) {
  1506. udd_ep_job[i].busy = false;
  1507. udd_ep_job[i].stall_requested = false;
  1508. }
  1509. }
  1510. static void udd_ep_job_table_kill(void)
  1511. {
  1512. uint8_t i;
  1513. // For each endpoint, kill job
  1514. for (i = 0; i < USB_DEVICE_MAX_EP; i++) {
  1515. udd_ep_finish_job(&udd_ep_job[i], true, i + 1);
  1516. }
  1517. }
  1518. static void udd_ep_abort_job(udd_ep_id_t ep)
  1519. {
  1520. ep &= USB_EP_ADDR_MASK;
  1521. // Abort job on endpoint
  1522. udd_ep_finish_job(&udd_ep_job[ep - 1], true, ep);
  1523. }
  1524. static void udd_ep_finish_job(udd_ep_job_t * ptr_job, bool b_abort, uint8_t ep_num)
  1525. {
  1526. if (ptr_job->busy == false) {
  1527. return; // No on-going job
  1528. }
  1529. dbg_print("(JobE%x:%d) ", (ptr_job-udd_ep_job)+1, b_abort);
  1530. ptr_job->busy = false;
  1531. if (NULL == ptr_job->call_trans) {
  1532. return; // No callback linked to job
  1533. }
  1534. if (Is_udd_endpoint_in(ep_num)) {
  1535. ep_num |= USB_EP_DIR_IN;
  1536. }
  1537. ptr_job->call_trans((b_abort) ? UDD_EP_TRANSFER_ABORT :
  1538. UDD_EP_TRANSFER_OK, ptr_job->buf_size, ep_num);
  1539. }
  1540. #ifdef UDD_EP_DMA_SUPPORTED
  1541. static void udd_ep_trans_done(udd_ep_id_t ep)
  1542. {
  1543. uint32_t udd_dma_ctrl = 0;
  1544. udd_ep_job_t *ptr_job;
  1545. iram_size_t next_trans;
  1546. irqflags_t flags;
  1547. // Get job corresponding at endpoint
  1548. ptr_job = &udd_ep_job[ep - 1];
  1549. if (!ptr_job->busy) {
  1550. return; // No job is running, then ignore it (system error)
  1551. }
  1552. if (ptr_job->buf_cnt != ptr_job->buf_size) {
  1553. // Need to send or receiv other data
  1554. next_trans = ptr_job->buf_size - ptr_job->buf_cnt;
  1555. if (UDD_ENDPOINT_MAX_TRANS < next_trans) {
  1556. // The USB hardware support a maximum
  1557. // transfer size of UDD_ENDPOINT_MAX_TRANS Bytes
  1558. next_trans = UDD_ENDPOINT_MAX_TRANS;
  1559. // Set 0 to tranfer the maximum
  1560. udd_dma_ctrl = UOTGHS_DEVDMACONTROL_BUFF_LENGTH(0);
  1561. } else {
  1562. udd_dma_ctrl = UOTGHS_DEVDMACONTROL_BUFF_LENGTH(next_trans);
  1563. }
  1564. if (Is_udd_endpoint_in(ep)) {
  1565. if (0 != (next_trans % udd_get_endpoint_size(ep))) {
  1566. // Enable short packet option
  1567. // else the DMA transfer is accepted
  1568. // and interrupt DMA valid but nothing is sent.
  1569. udd_dma_ctrl |= UOTGHS_DEVDMACONTROL_END_B_EN;
  1570. // No need to request another ZLP
  1571. ptr_job->b_shortpacket = false;
  1572. }
  1573. } else {
  1574. if ((USB_EP_TYPE_ISOCHRONOUS != udd_get_endpoint_type(ep))
  1575. || (next_trans <= (iram_size_t) udd_get_endpoint_size(ep))) {
  1576. // Enable short packet reception
  1577. udd_dma_ctrl |= UOTGHS_DEVDMACONTROL_END_TR_IT
  1578. | UOTGHS_DEVDMACONTROL_END_TR_EN;
  1579. }
  1580. }
  1581. // Start USB DMA to fill or read fifo of the selected endpoint
  1582. udd_endpoint_dma_set_addr(ep, (uint32_t) & ptr_job->buf[ptr_job->buf_cnt]);
  1583. udd_dma_ctrl |= UOTGHS_DEVDMACONTROL_END_BUFFIT |
  1584. UOTGHS_DEVDMACONTROL_CHANN_ENB;
  1585. // Disable IRQs to have a short sequence
  1586. // between read of EOT_STA and DMA enable
  1587. flags = cpu_irq_save();
  1588. if (!(udd_endpoint_dma_get_status(ep)
  1589. & UOTGHS_DEVDMASTATUS_END_TR_ST)) {
  1590. dbg_print("dmaS%x ", ep);
  1591. udd_endpoint_dma_set_control(ep, udd_dma_ctrl);
  1592. ptr_job->buf_cnt += next_trans;
  1593. ptr_job->buf_load = next_trans;
  1594. udd_enable_endpoint_dma_interrupt(ep);
  1595. cpu_irq_restore(flags);
  1596. return;
  1597. }
  1598. cpu_irq_restore(flags);
  1599. // Here a ZLP has been recieved
  1600. // and the DMA transfer must be not started.
  1601. // It is the end of transfer
  1602. ptr_job->buf_size = ptr_job->buf_cnt;
  1603. }
  1604. if (Is_udd_endpoint_in(ep)) {
  1605. if (ptr_job->b_shortpacket) {
  1606. dbg_print("zlpS%x ", ep);
  1607. // Need to send a ZLP (No possible with USB DMA)
  1608. // enable interrupt to wait a free bank to sent ZLP
  1609. udd_ack_in_send(ep);
  1610. if (Is_udd_write_enabled(ep)) {
  1611. // Force interrupt in case of ep already free
  1612. udd_raise_in_send(ep);
  1613. }
  1614. udd_enable_in_send_interrupt(ep);
  1615. udd_enable_endpoint_interrupt(ep);
  1616. return;
  1617. }
  1618. }
  1619. dbg_print("dmaE ");
  1620. // Call callback to signal end of transfer
  1621. udd_ep_finish_job(ptr_job, false, ep);
  1622. }
  1623. #endif
  1624. #ifdef UDD_EP_FIFO_SUPPORTED
  1625. static void udd_ep_in_sent(udd_ep_id_t ep)
  1626. {
  1627. udd_ep_job_t *ptr_job = &udd_ep_job[ep - 1];
  1628. uint8_t *ptr_src = &ptr_job->buf[ptr_job->buf_cnt];
  1629. uint8_t *ptr_dst = (uint8_t *) & udd_get_endpoint_fifo_access(ep, 8);
  1630. uint32_t pkt_size = udd_get_endpoint_size(ep);
  1631. uint32_t nb_data = 0, i;
  1632. uint32_t nb_remain;
  1633. irqflags_t flags;
  1634. // All transfer done, including ZLP, Finish Job
  1635. if (ptr_job->buf_cnt >= ptr_job->buf_size && !ptr_job->b_shortpacket) {
  1636. flags = cpu_irq_save();
  1637. udd_disable_in_send_interrupt(ep);
  1638. udd_disable_endpoint_interrupt(ep);
  1639. cpu_irq_restore(flags);
  1640. ptr_job->buf_size = ptr_job->buf_cnt; // buf_size is passed to callback as XFR count
  1641. udd_ep_finish_job(ptr_job, false, ep);
  1642. return;
  1643. } else {
  1644. // ACK TXINI
  1645. udd_ack_in_send(ep);
  1646. // Fill FIFO
  1647. ptr_dst = (uint8_t *) & udd_get_endpoint_fifo_access(ep, 8);
  1648. ptr_src = &ptr_job->buf[ptr_job->buf_cnt];
  1649. nb_remain = ptr_job->buf_size - ptr_job->buf_cnt;
  1650. // Fill a bank even if no data (ZLP)
  1651. nb_data = min(nb_remain, pkt_size);
  1652. // Modify job information
  1653. ptr_job->buf_cnt += nb_data;
  1654. ptr_job->buf_load = nb_data;
  1655. // Copy buffer to FIFO
  1656. for (i = 0; i < nb_data; i++) {
  1657. *ptr_dst++ = *ptr_src++;
  1658. }
  1659. // Switch to next bank
  1660. udd_ack_fifocon(ep);
  1661. // ZLP?
  1662. if (nb_data < pkt_size) {
  1663. ptr_job->b_shortpacket = false;
  1664. }
  1665. }
  1666. }
  1667. static void udd_ep_out_received(udd_ep_id_t ep)
  1668. {
  1669. udd_ep_job_t *ptr_job = &udd_ep_job[ep - 1];
  1670. uint32_t nb_data = 0, i;
  1671. uint32_t nb_remain = ptr_job->buf_size - ptr_job->buf_cnt;
  1672. uint32_t pkt_size = udd_get_endpoint_size(ep);
  1673. uint8_t *ptr_src = (uint8_t *) & udd_get_endpoint_fifo_access(ep, 8);
  1674. uint8_t *ptr_dst = &ptr_job->buf[ptr_job->buf_cnt];
  1675. bool b_full = false, b_short = false;
  1676. // Clear RX OUT
  1677. udd_ack_out_received(ep);
  1678. // Read byte count
  1679. nb_data = udd_byte_count(ep);
  1680. if (nb_data < pkt_size) {
  1681. b_short = true;
  1682. }
  1683. //dbg_print("o%d ", ep);
  1684. //dbg_print("%d ", nb_data);
  1685. // Copy data if there is
  1686. if (nb_data > 0) {
  1687. if (nb_data >= nb_remain) {
  1688. nb_data = nb_remain;
  1689. b_full = true;
  1690. }
  1691. // Modify job information
  1692. ptr_job->buf_cnt += nb_data;
  1693. ptr_job->buf_load = nb_data;
  1694. // Copy FIFO to buffer
  1695. for (i = 0; i < nb_data; i++) {
  1696. *ptr_dst++ = *ptr_src++;
  1697. }
  1698. }
  1699. // Clear FIFO Status
  1700. udd_ack_fifocon(ep);
  1701. // Finish job on error or short packet
  1702. if (b_full || b_short) {
  1703. //dbg_print("EoO%d\n\r", ep);
  1704. udd_disable_out_received_interrupt(ep);
  1705. udd_disable_endpoint_interrupt(ep);
  1706. ptr_job->buf_size = ptr_job->buf_cnt; // buf_size is passed to callback as XFR count
  1707. udd_ep_finish_job(ptr_job, false, ep);
  1708. }
  1709. }
  1710. #endif // #ifdef UDD_EP_FIFO_SUPPORTED
  1711. static bool udd_ep_interrupt(void)
  1712. {
  1713. udd_ep_id_t ep;
  1714. udd_ep_job_t *ptr_job;
  1715. // For each endpoint different of control endpoint (0)
  1716. for (ep = 1; ep <= USB_DEVICE_MAX_EP; ep++) {
  1717. // Get job corresponding at endpoint
  1718. ptr_job = &udd_ep_job[ep - 1];
  1719. #ifdef UDD_EP_DMA_SUPPORTED
  1720. // Check DMA event
  1721. if (Is_udd_endpoint_dma_interrupt_enabled(ep)
  1722. && Is_udd_endpoint_dma_interrupt(ep)) {
  1723. uint32_t nb_remaining;
  1724. if (udd_endpoint_dma_get_status(ep)
  1725. & UOTGHS_DEVDMASTATUS_CHANN_ENB) {
  1726. return true; // Ignore EOT_STA interrupt
  1727. }
  1728. dbg_print("dma%x: ", ep);
  1729. udd_disable_endpoint_dma_interrupt(ep);
  1730. // Save number of data no transfered
  1731. nb_remaining = (udd_endpoint_dma_get_status(ep) &
  1732. UOTGHS_DEVDMASTATUS_BUFF_COUNT_Msk)
  1733. >> UOTGHS_DEVDMASTATUS_BUFF_COUNT_Pos;
  1734. if (nb_remaining) {
  1735. // Transfer no complete (short packet or ZLP) then:
  1736. // Update number of data transfered
  1737. ptr_job->buf_cnt -= nb_remaining;
  1738. // Set transfer complete to stop the transfer
  1739. ptr_job->buf_size = ptr_job->buf_cnt;
  1740. }
  1741. udd_ep_trans_done(ep);
  1742. return true;
  1743. }
  1744. #endif
  1745. #ifdef UDD_EP_FIFO_SUPPORTED
  1746. // Check RXRDY and TXEMPTY event for none DMA endpoints
  1747. if (!Is_udd_endpoint_dma_supported(ep)
  1748. && Is_udd_endpoint_interrupt_enabled(ep)) {
  1749. dbg_print("ep%x: ", ep);
  1750. // RXOUT: Full packet received
  1751. if (Is_udd_out_received(ep)
  1752. && Is_udd_out_received_interrupt_enabled(ep)) {
  1753. dbg_print("Out ");
  1754. udd_ep_out_received(ep);
  1755. return true;
  1756. }
  1757. // TXIN: packet sent
  1758. if (Is_udd_in_send(ep)
  1759. && Is_udd_in_send_interrupt_enabled(ep)) {
  1760. dbg_print("In ");
  1761. udd_ep_in_sent(ep);
  1762. return true;
  1763. }
  1764. // Errors: Abort?
  1765. if (Is_udd_overflow(ep)
  1766. || Is_udd_underflow(ep)
  1767. || Is_udd_crc_error(ep)) {
  1768. dbg_print("Err ");
  1769. udd_ep_abort(ep);
  1770. return true;
  1771. }
  1772. }
  1773. #endif // UDD_EP_FIFO_SUPPORTED
  1774. // Check empty bank interrupt event
  1775. if (Is_udd_endpoint_interrupt_enabled(ep)) {
  1776. dbg_print("bg%x: ", ep);
  1777. if (Is_udd_in_send_interrupt_enabled(ep)
  1778. && Is_udd_in_send(ep)) {
  1779. dbg_print("I ");
  1780. udd_disable_in_send_interrupt(ep);
  1781. // One bank is free then send a ZLP
  1782. udd_ack_in_send(ep);
  1783. udd_ack_fifocon(ep);
  1784. udd_ep_finish_job(ptr_job, false, ep);
  1785. return true;
  1786. }
  1787. if (Is_udd_bank_interrupt_enabled(ep)
  1788. && (0 == udd_nb_busy_bank(ep))) {
  1789. dbg_print("EoT ");
  1790. // End of background transfer on IN endpoint
  1791. udd_disable_bank_interrupt(ep);
  1792. udd_disable_endpoint_interrupt(ep);
  1793. Assert(ptr_job->stall_requested);
  1794. // A stall has been requested during backgound transfer
  1795. ptr_job->stall_requested = false;
  1796. udd_disable_endpoint_bank_autoswitch(ep);
  1797. udd_enable_stall_handshake(ep);
  1798. udd_reset_data_toggle(ep);
  1799. return true;
  1800. }
  1801. }
  1802. }
  1803. return false;
  1804. }
  1805. #endif // (0 != USB_DEVICE_MAX_EP)
  1806. //@}
  1807. #endif // ARDUINO_ARCH_SAM