dcd_synopsys.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2018 Scott Shawcroft, 2019 William D. Jones for Adafruit Industries
  5. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  6. * Copyright (c) 2020 Jan Duempelmann
  7. * Copyright (c) 2020 Reinhard Panhuber
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. * This file is part of the TinyUSB stack.
  28. */
  29. #include "tusb_option.h"
  30. // Since TinyUSB doesn't use SOF for now, and this interrupt too often (1ms interval)
  31. // We disable SOF for now until needed later on
  32. #define USE_SOF 0
  33. #if defined (STM32F105x8) || defined (STM32F105xB) || defined (STM32F105xC) || \
  34. defined (STM32F107xB) || defined (STM32F107xC)
  35. #define STM32F1_SYNOPSYS
  36. #endif
  37. #if defined (STM32L475xx) || defined (STM32L476xx) || \
  38. defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || \
  39. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || \
  40. defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  41. #define STM32L4_SYNOPSYS
  42. #endif
  43. #if CFG_TUD_ENABLED && \
  44. ( (CFG_TUSB_MCU == OPT_MCU_STM32F1 && defined(STM32F1_SYNOPSYS)) || \
  45. CFG_TUSB_MCU == OPT_MCU_STM32F2 || \
  46. CFG_TUSB_MCU == OPT_MCU_STM32F4 || \
  47. CFG_TUSB_MCU == OPT_MCU_STM32F7 || \
  48. CFG_TUSB_MCU == OPT_MCU_STM32H7 || \
  49. (CFG_TUSB_MCU == OPT_MCU_STM32L4 && defined(STM32L4_SYNOPSYS) || \
  50. CFG_TUSB_MCU == OPT_MCU_GD32VF103 ) \
  51. )
  52. // EP_MAX : Max number of bi-directional endpoints including EP0
  53. // EP_FIFO_SIZE : Size of dedicated USB SRAM
  54. #if CFG_TUSB_MCU == OPT_MCU_STM32F1
  55. #include "stm32f1xx.h"
  56. #define EP_MAX_FS 4
  57. #define EP_FIFO_SIZE_FS 1280
  58. #elif CFG_TUSB_MCU == OPT_MCU_STM32F2
  59. #include "stm32f2xx.h"
  60. #define EP_MAX_FS USB_OTG_FS_MAX_IN_ENDPOINTS
  61. #define EP_FIFO_SIZE_FS USB_OTG_FS_TOTAL_FIFO_SIZE
  62. #elif CFG_TUSB_MCU == OPT_MCU_STM32F4
  63. #include "stm32f4xx.h"
  64. #define EP_MAX_FS USB_OTG_FS_MAX_IN_ENDPOINTS
  65. #define EP_FIFO_SIZE_FS USB_OTG_FS_TOTAL_FIFO_SIZE
  66. #define EP_MAX_HS USB_OTG_HS_MAX_IN_ENDPOINTS
  67. #define EP_FIFO_SIZE_HS USB_OTG_HS_TOTAL_FIFO_SIZE
  68. #elif CFG_TUSB_MCU == OPT_MCU_STM32H7
  69. #include "stm32h7xx.h"
  70. #define EP_MAX_FS 9
  71. #define EP_FIFO_SIZE_FS 4096
  72. #define EP_MAX_HS 9
  73. #define EP_FIFO_SIZE_HS 4096
  74. #elif CFG_TUSB_MCU == OPT_MCU_STM32F7
  75. #include "stm32f7xx.h"
  76. #define EP_MAX_FS 6
  77. #define EP_FIFO_SIZE_FS 1280
  78. #define EP_MAX_HS 9
  79. #define EP_FIFO_SIZE_HS 4096
  80. #elif CFG_TUSB_MCU == OPT_MCU_STM32L4
  81. #include "stm32l4xx.h"
  82. #define EP_MAX_FS 6
  83. #define EP_FIFO_SIZE_FS 1280
  84. #elif CFG_TUSB_MCU == OPT_MCU_GD32VF103
  85. #include "synopsys_common.h"
  86. // for remote wakeup delay
  87. #define __NOP() __asm volatile ("nop")
  88. // These numbers are the same for the whole GD32VF103 family.
  89. #define OTG_FS_IRQn 86
  90. #define EP_MAX_FS 4
  91. #define EP_FIFO_SIZE_FS 1280
  92. // The GD32VF103 is a RISC-V MCU, which implements the ECLIC Core-Local
  93. // Interrupt Controller by Nuclei. It is nearly API compatible to the
  94. // NVIC used by ARM MCUs.
  95. #define ECLIC_INTERRUPT_ENABLE_BASE 0xD2001001UL
  96. #define NVIC_EnableIRQ __eclic_enable_interrupt
  97. #define NVIC_DisableIRQ __eclic_disable_interrupt
  98. static inline void __eclic_enable_interrupt (uint32_t irq) {
  99. *(volatile uint8_t*)(ECLIC_INTERRUPT_ENABLE_BASE + (irq * 4)) = 1;
  100. }
  101. static inline void __eclic_disable_interrupt (uint32_t irq){
  102. *(volatile uint8_t*)(ECLIC_INTERRUPT_ENABLE_BASE + (irq * 4)) = 0;
  103. }
  104. #else
  105. #error "Unsupported MCUs"
  106. #endif
  107. #include "device/dcd.h"
  108. //--------------------------------------------------------------------+
  109. // MACRO TYPEDEF CONSTANT ENUM
  110. //--------------------------------------------------------------------+
  111. // On STM32 we associate Port0 to OTG_FS, and Port1 to OTG_HS
  112. #if TUD_OPT_RHPORT == 0
  113. #define EP_MAX EP_MAX_FS
  114. #define EP_FIFO_SIZE EP_FIFO_SIZE_FS
  115. #define RHPORT_REGS_BASE USB_OTG_FS_PERIPH_BASE
  116. #define RHPORT_IRQn OTG_FS_IRQn
  117. #else
  118. #define EP_MAX EP_MAX_HS
  119. #define EP_FIFO_SIZE EP_FIFO_SIZE_HS
  120. #define RHPORT_REGS_BASE USB_OTG_HS_PERIPH_BASE
  121. #define RHPORT_IRQn OTG_HS_IRQn
  122. #endif
  123. #define GLOBAL_BASE(_port) ((USB_OTG_GlobalTypeDef*) RHPORT_REGS_BASE)
  124. #define DEVICE_BASE(_port) (USB_OTG_DeviceTypeDef *) (RHPORT_REGS_BASE + USB_OTG_DEVICE_BASE)
  125. #define OUT_EP_BASE(_port) (USB_OTG_OUTEndpointTypeDef *) (RHPORT_REGS_BASE + USB_OTG_OUT_ENDPOINT_BASE)
  126. #define IN_EP_BASE(_port) (USB_OTG_INEndpointTypeDef *) (RHPORT_REGS_BASE + USB_OTG_IN_ENDPOINT_BASE)
  127. #define FIFO_BASE(_port, _x) ((volatile uint32_t *) (RHPORT_REGS_BASE + USB_OTG_FIFO_BASE + (_x) * USB_OTG_FIFO_SIZE))
  128. enum
  129. {
  130. DCD_HIGH_SPEED = 0, // Highspeed mode
  131. DCD_FULL_SPEED_USE_HS = 1, // Full speed in Highspeed port (probably with internal PHY)
  132. DCD_FULL_SPEED = 3, // Full speed with internal PHY
  133. };
  134. static TU_ATTR_ALIGNED(4) uint32_t _setup_packet[2];
  135. typedef struct {
  136. uint8_t * buffer;
  137. tu_fifo_t * ff;
  138. uint16_t total_len;
  139. uint16_t max_size;
  140. uint8_t interval;
  141. } xfer_ctl_t;
  142. typedef volatile uint32_t * usb_fifo_t;
  143. xfer_ctl_t xfer_status[EP_MAX][2];
  144. #define XFER_CTL_BASE(_ep, _dir) &xfer_status[_ep][_dir]
  145. // EP0 transfers are limited to 1 packet - larger sizes has to be split
  146. static uint16_t ep0_pending[2]; // Index determines direction as tusb_dir_t type
  147. // TX FIFO RAM allocation so far in words - RX FIFO size is readily available from usb_otg->GRXFSIZ
  148. static uint16_t _allocated_fifo_words_tx; // TX FIFO size in words (IN EPs)
  149. static bool _out_ep_closed; // Flag to check if RX FIFO size needs an update (reduce its size)
  150. // Calculate the RX FIFO size according to recommendations from reference manual
  151. static inline uint16_t calc_rx_ff_size(uint16_t ep_size)
  152. {
  153. return 15 + 2*(ep_size/4) + 2*EP_MAX;
  154. }
  155. static void update_grxfsiz(uint8_t rhport)
  156. {
  157. (void) rhport;
  158. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  159. // Determine largest EP size for RX FIFO
  160. uint16_t max_epsize = 0;
  161. for (uint8_t epnum = 0; epnum < EP_MAX; epnum++)
  162. {
  163. max_epsize = tu_max16(max_epsize, xfer_status[epnum][TUSB_DIR_OUT].max_size);
  164. }
  165. // Update size of RX FIFO
  166. usb_otg->GRXFSIZ = calc_rx_ff_size(max_epsize);
  167. }
  168. // Setup the control endpoint 0.
  169. static void bus_reset(uint8_t rhport)
  170. {
  171. (void) rhport;
  172. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  173. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  174. USB_OTG_OUTEndpointTypeDef * out_ep = OUT_EP_BASE(rhport);
  175. USB_OTG_INEndpointTypeDef * in_ep = IN_EP_BASE(rhport);
  176. tu_memclr(xfer_status, sizeof(xfer_status));
  177. _out_ep_closed = false;
  178. // clear device address
  179. dev->DCFG &= ~USB_OTG_DCFG_DAD_Msk;
  180. // 1. NAK for all OUT endpoints
  181. for(uint8_t n = 0; n < EP_MAX; n++) {
  182. out_ep[n].DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  183. }
  184. // 2. Un-mask interrupt bits
  185. dev->DAINTMSK = (1 << USB_OTG_DAINTMSK_OEPM_Pos) | (1 << USB_OTG_DAINTMSK_IEPM_Pos);
  186. dev->DOEPMSK = USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM;
  187. dev->DIEPMSK = USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM;
  188. // "USB Data FIFOs" section in reference manual
  189. // Peripheral FIFO architecture
  190. //
  191. // The FIFO is split up in a lower part where the RX FIFO is located and an upper part where the TX FIFOs start.
  192. // We do this to allow the RX FIFO to grow dynamically which is possible since the free space is located
  193. // between the RX and TX FIFOs. This is required by ISO OUT EPs which need a bigger FIFO than the standard
  194. // configuration done below.
  195. //
  196. // Dynamically FIFO sizes are of interest only for ISO EPs since all others are usually not opened and closed.
  197. // All EPs other than ISO are opened as soon as the driver starts up i.e. when the host sends a
  198. // configure interface command. Hence, all IN EPs other the ISO will be located at the top. IN ISO EPs are usually
  199. // opened when the host sends an additional command: setInterface. At this point in time
  200. // the ISO EP will be located next to the free space and can change its size. In case more IN EPs change its size
  201. // an additional memory
  202. //
  203. // --------------- 320 or 1024 ( 1280 or 4096 bytes )
  204. // | IN FIFO 0 |
  205. // --------------- (320 or 1024) - 16
  206. // | IN FIFO 1 |
  207. // --------------- (320 or 1024) - 16 - x
  208. // | . . . . |
  209. // --------------- (320 or 1024) - 16 - x - y - ... - z
  210. // | IN FIFO MAX |
  211. // ---------------
  212. // | FREE |
  213. // --------------- GRXFSIZ
  214. // | OUT FIFO |
  215. // | ( Shared ) |
  216. // --------------- 0
  217. //
  218. // According to "FIFO RAM allocation" section in RM, FIFO RAM are allocated as follows (each word 32-bits):
  219. // - Each EP IN needs at least max packet size, 16 words is sufficient for EP0 IN
  220. //
  221. // - All EP OUT shared a unique OUT FIFO which uses
  222. // - 13 for setup packets + control words (up to 3 setup packets).
  223. // - 1 for global NAK (not required/used here).
  224. // - Largest-EPsize / 4 + 1. ( FS: 64 bytes, HS: 512 bytes). Recommended is "2 x (Largest-EPsize/4) + 1"
  225. // - 2 for each used OUT endpoint
  226. //
  227. // Therefore GRXFSIZ = 13 + 1 + 1 + 2 x (Largest-EPsize/4) + 2 x EPOUTnum
  228. // - FullSpeed (64 Bytes ): GRXFSIZ = 15 + 2 x 16 + 2 x EP_MAX = 47 + 2 x EP_MAX
  229. // - Highspeed (512 bytes): GRXFSIZ = 15 + 2 x 128 + 2 x EP_MAX = 271 + 2 x EP_MAX
  230. //
  231. // NOTE: Largest-EPsize & EPOUTnum is actual used endpoints in configuration. Since DCD has no knowledge
  232. // of the overall picture yet. We will use the worst scenario: largest possible + EP_MAX
  233. //
  234. // For Isochronous, largest EP size can be 1023/1024 for FS/HS respectively. In addition if multiple ISO
  235. // are enabled at least "2 x (Largest-EPsize/4) + 1" are recommended. Maybe provide a macro for application to
  236. // overwrite this.
  237. usb_otg->GRXFSIZ = calc_rx_ff_size(TUD_OPT_HIGH_SPEED ? 512 : 64);
  238. _allocated_fifo_words_tx = 16;
  239. // Control IN uses FIFO 0 with 64 bytes ( 16 32-bit word )
  240. usb_otg->DIEPTXF0_HNPTXFSIZ = (16 << USB_OTG_TX0FD_Pos) | (EP_FIFO_SIZE/4 - _allocated_fifo_words_tx);
  241. // Fixed control EP0 size to 64 bytes
  242. in_ep[0].DIEPCTL &= ~(0x03 << USB_OTG_DIEPCTL_MPSIZ_Pos);
  243. xfer_status[0][TUSB_DIR_OUT].max_size = xfer_status[0][TUSB_DIR_IN].max_size = 64;
  244. out_ep[0].DOEPTSIZ |= (3 << USB_OTG_DOEPTSIZ_STUPCNT_Pos);
  245. usb_otg->GINTMSK |= USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IEPINT;
  246. }
  247. // Set turn-around timeout according to link speed
  248. extern uint32_t SystemCoreClock;
  249. static void set_turnaround(USB_OTG_GlobalTypeDef * usb_otg, tusb_speed_t speed)
  250. {
  251. usb_otg->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  252. if ( speed == TUSB_SPEED_HIGH )
  253. {
  254. // Use fixed 0x09 for Highspeed
  255. usb_otg->GUSBCFG |= (0x09 << USB_OTG_GUSBCFG_TRDT_Pos);
  256. }
  257. else
  258. {
  259. // Turnaround timeout depends on the MCU clock
  260. uint32_t turnaround;
  261. if ( SystemCoreClock >= 32000000U )
  262. turnaround = 0x6U;
  263. else if ( SystemCoreClock >= 27500000U )
  264. turnaround = 0x7U;
  265. else if ( SystemCoreClock >= 24000000U )
  266. turnaround = 0x8U;
  267. else if ( SystemCoreClock >= 21800000U )
  268. turnaround = 0x9U;
  269. else if ( SystemCoreClock >= 20000000U )
  270. turnaround = 0xAU;
  271. else if ( SystemCoreClock >= 18500000U )
  272. turnaround = 0xBU;
  273. else if ( SystemCoreClock >= 17200000U )
  274. turnaround = 0xCU;
  275. else if ( SystemCoreClock >= 16000000U )
  276. turnaround = 0xDU;
  277. else if ( SystemCoreClock >= 15000000U )
  278. turnaround = 0xEU;
  279. else
  280. turnaround = 0xFU;
  281. // Fullspeed depends on MCU clocks, but we will use 0x06 for 32+ Mhz
  282. usb_otg->GUSBCFG |= (turnaround << USB_OTG_GUSBCFG_TRDT_Pos);
  283. }
  284. }
  285. static tusb_speed_t get_speed(uint8_t rhport)
  286. {
  287. (void) rhport;
  288. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  289. uint32_t const enum_spd = (dev->DSTS & USB_OTG_DSTS_ENUMSPD_Msk) >> USB_OTG_DSTS_ENUMSPD_Pos;
  290. return (enum_spd == DCD_HIGH_SPEED) ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL;
  291. }
  292. static void set_speed(uint8_t rhport, tusb_speed_t speed)
  293. {
  294. uint32_t bitvalue;
  295. if ( rhport == 1 )
  296. {
  297. bitvalue = ((TUSB_SPEED_HIGH == speed) ? DCD_HIGH_SPEED : DCD_FULL_SPEED_USE_HS);
  298. }
  299. else
  300. {
  301. bitvalue = DCD_FULL_SPEED;
  302. }
  303. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  304. // Clear and set speed bits
  305. dev->DCFG &= ~(3 << USB_OTG_DCFG_DSPD_Pos);
  306. dev->DCFG |= (bitvalue << USB_OTG_DCFG_DSPD_Pos);
  307. }
  308. #if defined(USB_HS_PHYC)
  309. static bool USB_HS_PHYCInit(void)
  310. {
  311. USB_HS_PHYC_GlobalTypeDef *usb_hs_phyc = (USB_HS_PHYC_GlobalTypeDef*) USB_HS_PHYC_CONTROLLER_BASE;
  312. // Enable LDO: Note STM32F72/3xx Reference Manual rev 3 June 2018 incorrectly defined this bit as Disabled !!
  313. usb_hs_phyc->USB_HS_PHYC_LDO |= USB_HS_PHYC_LDO_ENABLE;
  314. // Wait until LDO ready
  315. while ( 0 == (usb_hs_phyc->USB_HS_PHYC_LDO & USB_HS_PHYC_LDO_STATUS) ) {}
  316. uint32_t phyc_pll = 0;
  317. // TODO Try to get HSE_VALUE from registers instead of depending CFLAGS
  318. switch ( HSE_VALUE )
  319. {
  320. case 12000000: phyc_pll = USB_HS_PHYC_PLL1_PLLSEL_12MHZ ; break;
  321. case 12500000: phyc_pll = USB_HS_PHYC_PLL1_PLLSEL_12_5MHZ ; break;
  322. case 16000000: phyc_pll = USB_HS_PHYC_PLL1_PLLSEL_16MHZ ; break;
  323. case 24000000: phyc_pll = USB_HS_PHYC_PLL1_PLLSEL_24MHZ ; break;
  324. case 25000000: phyc_pll = USB_HS_PHYC_PLL1_PLLSEL_25MHZ ; break;
  325. case 32000000: phyc_pll = USB_HS_PHYC_PLL1_PLLSEL_Msk ; break; // Value not defined in header
  326. default:
  327. TU_ASSERT(0);
  328. }
  329. usb_hs_phyc->USB_HS_PHYC_PLL = phyc_pll;
  330. // Control the tuning interface of the High Speed PHY
  331. // Use magic value (USB_HS_PHYC_TUNE_VALUE) from ST driver
  332. usb_hs_phyc->USB_HS_PHYC_TUNE |= 0x00000F13U;
  333. // Enable PLL internal PHY
  334. usb_hs_phyc->USB_HS_PHYC_PLL |= USB_HS_PHYC_PLL_PLLEN;
  335. // Original ST code has 2 ms delay for PLL stabilization.
  336. // Primitive test shows that more than 10 USB un/replug cycle showed no error with enumeration
  337. return true;
  338. }
  339. #endif
  340. static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t const dir, uint16_t const num_packets, uint16_t total_bytes)
  341. {
  342. (void) rhport;
  343. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  344. USB_OTG_OUTEndpointTypeDef * out_ep = OUT_EP_BASE(rhport);
  345. USB_OTG_INEndpointTypeDef * in_ep = IN_EP_BASE(rhport);
  346. // EP0 is limited to one packet each xfer
  347. // We use multiple transaction of xfer->max_size length to get a whole transfer done
  348. if(epnum == 0) {
  349. xfer_ctl_t * const xfer = XFER_CTL_BASE(epnum, dir);
  350. total_bytes = tu_min16(ep0_pending[dir], xfer->max_size);
  351. ep0_pending[dir] -= total_bytes;
  352. }
  353. // IN and OUT endpoint xfers are interrupt-driven, we just schedule them here.
  354. if(dir == TUSB_DIR_IN) {
  355. // A full IN transfer (multiple packets, possibly) triggers XFRC.
  356. in_ep[epnum].DIEPTSIZ = (num_packets << USB_OTG_DIEPTSIZ_PKTCNT_Pos) |
  357. ((total_bytes << USB_OTG_DIEPTSIZ_XFRSIZ_Pos) & USB_OTG_DIEPTSIZ_XFRSIZ_Msk);
  358. in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_EPENA | USB_OTG_DIEPCTL_CNAK;
  359. // For ISO endpoint set correct odd/even bit for next frame.
  360. if ((in_ep[epnum].DIEPCTL & USB_OTG_DIEPCTL_EPTYP) == USB_OTG_DIEPCTL_EPTYP_0 && (XFER_CTL_BASE(epnum, dir))->interval == 1)
  361. {
  362. // Take odd/even bit from frame counter.
  363. uint32_t const odd_frame_now = (dev->DSTS & (1u << USB_OTG_DSTS_FNSOF_Pos));
  364. in_ep[epnum].DIEPCTL |= (odd_frame_now ? USB_OTG_DIEPCTL_SD0PID_SEVNFRM_Msk : USB_OTG_DIEPCTL_SODDFRM_Msk);
  365. }
  366. // Enable fifo empty interrupt only if there are something to put in the fifo.
  367. if(total_bytes != 0) {
  368. dev->DIEPEMPMSK |= (1 << epnum);
  369. }
  370. } else {
  371. // A full OUT transfer (multiple packets, possibly) triggers XFRC.
  372. out_ep[epnum].DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT_Msk | USB_OTG_DOEPTSIZ_XFRSIZ);
  373. out_ep[epnum].DOEPTSIZ |= (num_packets << USB_OTG_DOEPTSIZ_PKTCNT_Pos) |
  374. ((total_bytes << USB_OTG_DOEPTSIZ_XFRSIZ_Pos) & USB_OTG_DOEPTSIZ_XFRSIZ_Msk);
  375. out_ep[epnum].DOEPCTL |= USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK;
  376. if ((out_ep[epnum].DOEPCTL & USB_OTG_DOEPCTL_EPTYP) == USB_OTG_DOEPCTL_EPTYP_0 && (XFER_CTL_BASE(epnum, dir))->interval == 1)
  377. {
  378. // Take odd/even bit from frame counter.
  379. uint32_t const odd_frame_now = (dev->DSTS & (1u << USB_OTG_DSTS_FNSOF_Pos));
  380. out_ep[epnum].DOEPCTL |= (odd_frame_now ? USB_OTG_DOEPCTL_SD0PID_SEVNFRM_Msk : USB_OTG_DOEPCTL_SODDFRM_Msk);
  381. }
  382. }
  383. }
  384. /*------------------------------------------------------------------*/
  385. /* Controller API
  386. *------------------------------------------------------------------*/
  387. void dcd_init (uint8_t rhport)
  388. {
  389. // Programming model begins in the last section of the chapter on the USB
  390. // peripheral in each Reference Manual.
  391. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  392. // No HNP/SRP (no OTG support), program timeout later.
  393. if ( rhport == 1 )
  394. {
  395. // On selected MCUs HS port1 can be used with external PHY via ULPI interface
  396. #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HIGH_SPEED
  397. // deactivate internal PHY
  398. usb_otg->GCCFG &= ~USB_OTG_GCCFG_PWRDWN;
  399. // Init The UTMI Interface
  400. usb_otg->GUSBCFG &= ~(USB_OTG_GUSBCFG_TSDPS | USB_OTG_GUSBCFG_ULPIFSLS | USB_OTG_GUSBCFG_PHYSEL);
  401. // Select default internal VBUS Indicator and Drive for ULPI
  402. usb_otg->GUSBCFG &= ~(USB_OTG_GUSBCFG_ULPIEVBUSD | USB_OTG_GUSBCFG_ULPIEVBUSI);
  403. #else
  404. usb_otg->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
  405. #endif
  406. #if defined(USB_HS_PHYC)
  407. // Highspeed with embedded UTMI PHYC
  408. // Select UTMI Interface
  409. usb_otg->GUSBCFG &= ~USB_OTG_GUSBCFG_ULPI_UTMI_SEL;
  410. usb_otg->GCCFG |= USB_OTG_GCCFG_PHYHSEN;
  411. // Enables control of a High Speed USB PHY
  412. USB_HS_PHYCInit();
  413. #endif
  414. } else
  415. {
  416. // Enable internal PHY
  417. usb_otg->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
  418. }
  419. // Reset core after selecting PHY
  420. // Wait AHB IDLE, reset then wait until it is cleared
  421. while ((usb_otg->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U) {}
  422. usb_otg->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
  423. while ((usb_otg->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST) {}
  424. // Restart PHY clock
  425. *((volatile uint32_t *)(RHPORT_REGS_BASE + USB_OTG_PCGCCTL_BASE)) = 0;
  426. // Clear all interrupts
  427. usb_otg->GINTSTS |= usb_otg->GINTSTS;
  428. // Required as part of core initialization.
  429. // TODO: How should mode mismatch be handled? It will cause
  430. // the core to stop working/require reset.
  431. usb_otg->GINTMSK |= USB_OTG_GINTMSK_OTGINT | USB_OTG_GINTMSK_MMISM;
  432. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  433. // If USB host misbehaves during status portion of control xfer
  434. // (non zero-length packet), send STALL back and discard.
  435. dev->DCFG |= USB_OTG_DCFG_NZLSOHSK;
  436. set_speed(rhport, TUD_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL);
  437. // Enable internal USB transceiver, unless using HS core (port 1) with external PHY.
  438. if (!(rhport == 1 && (CFG_TUSB_RHPORT1_MODE & OPT_MODE_HIGH_SPEED))) usb_otg->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  439. usb_otg->GINTMSK |= USB_OTG_GINTMSK_USBRST | USB_OTG_GINTMSK_ENUMDNEM |
  440. USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_WUIM |
  441. USB_OTG_GINTMSK_RXFLVLM | (USE_SOF ? USB_OTG_GINTMSK_SOFM : 0);
  442. // Enable global interrupt
  443. usb_otg->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  444. dcd_connect(rhport);
  445. }
  446. void dcd_int_enable (uint8_t rhport)
  447. {
  448. (void) rhport;
  449. NVIC_EnableIRQ(RHPORT_IRQn);
  450. }
  451. void dcd_int_disable (uint8_t rhport)
  452. {
  453. (void) rhport;
  454. NVIC_DisableIRQ(RHPORT_IRQn);
  455. }
  456. void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
  457. {
  458. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  459. dev->DCFG = (dev->DCFG & ~USB_OTG_DCFG_DAD_Msk) | (dev_addr << USB_OTG_DCFG_DAD_Pos);
  460. // Response with status after changing device address
  461. dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
  462. }
  463. static void remote_wakeup_delay(void)
  464. {
  465. // try to delay for 1 ms
  466. uint32_t count = SystemCoreClock / 1000;
  467. while ( count-- )
  468. {
  469. __NOP();
  470. }
  471. }
  472. void dcd_remote_wakeup(uint8_t rhport)
  473. {
  474. (void) rhport;
  475. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  476. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  477. // set remote wakeup
  478. dev->DCTL |= USB_OTG_DCTL_RWUSIG;
  479. // enable SOF to detect bus resume
  480. usb_otg->GINTSTS = USB_OTG_GINTSTS_SOF;
  481. usb_otg->GINTMSK |= USB_OTG_GINTMSK_SOFM;
  482. // Per specs: remote wakeup signal bit must be clear within 1-15ms
  483. remote_wakeup_delay();
  484. dev->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  485. }
  486. void dcd_connect(uint8_t rhport)
  487. {
  488. (void) rhport;
  489. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  490. dev->DCTL &= ~USB_OTG_DCTL_SDIS;
  491. }
  492. void dcd_disconnect(uint8_t rhport)
  493. {
  494. (void) rhport;
  495. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  496. dev->DCTL |= USB_OTG_DCTL_SDIS;
  497. }
  498. void dcd_sof_enable(uint8_t rhport, bool en)
  499. {
  500. (void) rhport;
  501. (void) en;
  502. // TODO implement later
  503. }
  504. /*------------------------------------------------------------------*/
  505. /* DCD Endpoint port
  506. *------------------------------------------------------------------*/
  507. bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
  508. {
  509. (void) rhport;
  510. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  511. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  512. USB_OTG_OUTEndpointTypeDef * out_ep = OUT_EP_BASE(rhport);
  513. USB_OTG_INEndpointTypeDef * in_ep = IN_EP_BASE(rhport);
  514. uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress);
  515. uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress);
  516. TU_ASSERT(epnum < EP_MAX);
  517. xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
  518. xfer->max_size = tu_edpt_packet_size(desc_edpt);
  519. xfer->interval = desc_edpt->bInterval;
  520. uint16_t const fifo_size = (xfer->max_size + 3) / 4; // Round up to next full word
  521. if(dir == TUSB_DIR_OUT)
  522. {
  523. // Calculate required size of RX FIFO
  524. uint16_t const sz = calc_rx_ff_size(4*fifo_size);
  525. // If size_rx needs to be extended check if possible and if so enlarge it
  526. if (usb_otg->GRXFSIZ < sz)
  527. {
  528. TU_ASSERT(sz + _allocated_fifo_words_tx <= EP_FIFO_SIZE/4);
  529. // Enlarge RX FIFO
  530. usb_otg->GRXFSIZ = sz;
  531. }
  532. out_ep[epnum].DOEPCTL |= (1 << USB_OTG_DOEPCTL_USBAEP_Pos) |
  533. (desc_edpt->bmAttributes.xfer << USB_OTG_DOEPCTL_EPTYP_Pos) |
  534. (desc_edpt->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS ? USB_OTG_DOEPCTL_SD0PID_SEVNFRM : 0) |
  535. (xfer->max_size << USB_OTG_DOEPCTL_MPSIZ_Pos);
  536. dev->DAINTMSK |= (1 << (USB_OTG_DAINTMSK_OEPM_Pos + epnum));
  537. }
  538. else
  539. {
  540. // "USB Data FIFOs" section in reference manual
  541. // Peripheral FIFO architecture
  542. //
  543. // --------------- 320 or 1024 ( 1280 or 4096 bytes )
  544. // | IN FIFO 0 |
  545. // --------------- (320 or 1024) - 16
  546. // | IN FIFO 1 |
  547. // --------------- (320 or 1024) - 16 - x
  548. // | . . . . |
  549. // --------------- (320 or 1024) - 16 - x - y - ... - z
  550. // | IN FIFO MAX |
  551. // ---------------
  552. // | FREE |
  553. // --------------- GRXFSIZ
  554. // | OUT FIFO |
  555. // | ( Shared ) |
  556. // --------------- 0
  557. //
  558. // In FIFO is allocated by following rules:
  559. // - IN EP 1 gets FIFO 1, IN EP "n" gets FIFO "n".
  560. // Check if free space is available
  561. TU_ASSERT(_allocated_fifo_words_tx + fifo_size + usb_otg->GRXFSIZ <= EP_FIFO_SIZE/4);
  562. _allocated_fifo_words_tx += fifo_size;
  563. TU_LOG(2, " Allocated %u bytes at offset %u", fifo_size*4, EP_FIFO_SIZE-_allocated_fifo_words_tx*4);
  564. // DIEPTXF starts at FIFO #1.
  565. // Both TXFD and TXSA are in unit of 32-bit words.
  566. usb_otg->DIEPTXF[epnum - 1] = (fifo_size << USB_OTG_DIEPTXF_INEPTXFD_Pos) | (EP_FIFO_SIZE/4 - _allocated_fifo_words_tx);
  567. in_ep[epnum].DIEPCTL |= (1 << USB_OTG_DIEPCTL_USBAEP_Pos) |
  568. (epnum << USB_OTG_DIEPCTL_TXFNUM_Pos) |
  569. (desc_edpt->bmAttributes.xfer << USB_OTG_DIEPCTL_EPTYP_Pos) |
  570. (desc_edpt->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS ? USB_OTG_DIEPCTL_SD0PID_SEVNFRM : 0) |
  571. (xfer->max_size << USB_OTG_DIEPCTL_MPSIZ_Pos);
  572. dev->DAINTMSK |= (1 << (USB_OTG_DAINTMSK_IEPM_Pos + epnum));
  573. }
  574. return true;
  575. }
  576. // Close all non-control endpoints, cancel all pending transfers if any.
  577. void dcd_edpt_close_all (uint8_t rhport)
  578. {
  579. (void) rhport;
  580. // USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  581. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  582. USB_OTG_OUTEndpointTypeDef * out_ep = OUT_EP_BASE(rhport);
  583. USB_OTG_INEndpointTypeDef * in_ep = IN_EP_BASE(rhport);
  584. // Disable non-control interrupt
  585. dev->DAINTMSK = (1 << USB_OTG_DAINTMSK_OEPM_Pos) | (1 << USB_OTG_DAINTMSK_IEPM_Pos);
  586. for(uint8_t n = 1; n < EP_MAX; n++)
  587. {
  588. // disable OUT endpoint
  589. out_ep[n].DOEPCTL = 0;
  590. xfer_status[n][TUSB_DIR_OUT].max_size = 0;
  591. // disable IN endpoint
  592. in_ep[n].DIEPCTL = 0;
  593. xfer_status[n][TUSB_DIR_IN].max_size = 0;
  594. }
  595. // reset allocated fifo IN
  596. _allocated_fifo_words_tx = 16;
  597. }
  598. bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
  599. {
  600. uint8_t const epnum = tu_edpt_number(ep_addr);
  601. uint8_t const dir = tu_edpt_dir(ep_addr);
  602. xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
  603. xfer->buffer = buffer;
  604. xfer->ff = NULL;
  605. xfer->total_len = total_bytes;
  606. // EP0 can only handle one packet
  607. if(epnum == 0) {
  608. ep0_pending[dir] = total_bytes;
  609. // Schedule the first transaction for EP0 transfer
  610. edpt_schedule_packets(rhport, epnum, dir, 1, ep0_pending[dir]);
  611. return true;
  612. }
  613. uint16_t num_packets = (total_bytes / xfer->max_size);
  614. uint16_t const short_packet_size = total_bytes % xfer->max_size;
  615. // Zero-size packet is special case.
  616. if(short_packet_size > 0 || (total_bytes == 0)) {
  617. num_packets++;
  618. }
  619. // Schedule packets to be sent within interrupt
  620. edpt_schedule_packets(rhport, epnum, dir, num_packets, total_bytes);
  621. return true;
  622. }
  623. // The number of bytes has to be given explicitly to allow more flexible control of how many
  624. // bytes should be written and second to keep the return value free to give back a boolean
  625. // success message. If total_bytes is too big, the FIFO will copy only what is available
  626. // into the USB buffer!
  627. bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes)
  628. {
  629. // USB buffers always work in bytes so to avoid unnecessary divisions we demand item_size = 1
  630. TU_ASSERT(ff->item_size == 1);
  631. uint8_t const epnum = tu_edpt_number(ep_addr);
  632. uint8_t const dir = tu_edpt_dir(ep_addr);
  633. xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
  634. xfer->buffer = NULL;
  635. xfer->ff = ff;
  636. xfer->total_len = total_bytes;
  637. uint16_t num_packets = (total_bytes / xfer->max_size);
  638. uint16_t const short_packet_size = total_bytes % xfer->max_size;
  639. // Zero-size packet is special case.
  640. if(short_packet_size > 0 || (total_bytes == 0)) num_packets++;
  641. // Schedule packets to be sent within interrupt
  642. edpt_schedule_packets(rhport, epnum, dir, num_packets, total_bytes);
  643. return true;
  644. }
  645. static void dcd_edpt_disable (uint8_t rhport, uint8_t ep_addr, bool stall)
  646. {
  647. (void) rhport;
  648. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  649. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  650. USB_OTG_OUTEndpointTypeDef * out_ep = OUT_EP_BASE(rhport);
  651. USB_OTG_INEndpointTypeDef * in_ep = IN_EP_BASE(rhport);
  652. uint8_t const epnum = tu_edpt_number(ep_addr);
  653. uint8_t const dir = tu_edpt_dir(ep_addr);
  654. if(dir == TUSB_DIR_IN) {
  655. // Only disable currently enabled non-control endpoint
  656. if ( (epnum == 0) || !(in_ep[epnum].DIEPCTL & USB_OTG_DIEPCTL_EPENA) ){
  657. in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_SNAK | (stall ? USB_OTG_DIEPCTL_STALL : 0);
  658. } else {
  659. // Stop transmitting packets and NAK IN xfers.
  660. in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
  661. while((in_ep[epnum].DIEPINT & USB_OTG_DIEPINT_INEPNE) == 0);
  662. // Disable the endpoint.
  663. in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_EPDIS | (stall ? USB_OTG_DIEPCTL_STALL : 0);
  664. while((in_ep[epnum].DIEPINT & USB_OTG_DIEPINT_EPDISD_Msk) == 0);
  665. in_ep[epnum].DIEPINT = USB_OTG_DIEPINT_EPDISD;
  666. }
  667. // Flush the FIFO, and wait until we have confirmed it cleared.
  668. usb_otg->GRSTCTL |= (epnum << USB_OTG_GRSTCTL_TXFNUM_Pos);
  669. usb_otg->GRSTCTL |= USB_OTG_GRSTCTL_TXFFLSH;
  670. while((usb_otg->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH_Msk) != 0);
  671. } else {
  672. // Only disable currently enabled non-control endpoint
  673. if ( (epnum == 0) || !(out_ep[epnum].DOEPCTL & USB_OTG_DOEPCTL_EPENA) ){
  674. out_ep[epnum].DOEPCTL |= stall ? USB_OTG_DOEPCTL_STALL : 0;
  675. } else {
  676. // Asserting GONAK is required to STALL an OUT endpoint.
  677. // Simpler to use polling here, we don't use the "B"OUTNAKEFF interrupt
  678. // anyway, and it can't be cleared by user code. If this while loop never
  679. // finishes, we have bigger problems than just the stack.
  680. dev->DCTL |= USB_OTG_DCTL_SGONAK;
  681. while((usb_otg->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF_Msk) == 0);
  682. // Ditto here- disable the endpoint.
  683. out_ep[epnum].DOEPCTL |= USB_OTG_DOEPCTL_EPDIS | (stall ? USB_OTG_DOEPCTL_STALL : 0);
  684. while((out_ep[epnum].DOEPINT & USB_OTG_DOEPINT_EPDISD_Msk) == 0);
  685. out_ep[epnum].DOEPINT = USB_OTG_DOEPINT_EPDISD;
  686. // Allow other OUT endpoints to keep receiving.
  687. dev->DCTL |= USB_OTG_DCTL_CGONAK;
  688. }
  689. }
  690. }
  691. /**
  692. * Close an endpoint.
  693. */
  694. void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
  695. {
  696. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  697. uint8_t const epnum = tu_edpt_number(ep_addr);
  698. uint8_t const dir = tu_edpt_dir(ep_addr);
  699. dcd_edpt_disable(rhport, ep_addr, false);
  700. // Update max_size
  701. xfer_status[epnum][dir].max_size = 0; // max_size = 0 marks a disabled EP - required for changing FIFO allocation
  702. if (dir == TUSB_DIR_IN)
  703. {
  704. uint16_t const fifo_size = (usb_otg->DIEPTXF[epnum - 1] & USB_OTG_DIEPTXF_INEPTXFD_Msk) >> USB_OTG_DIEPTXF_INEPTXFD_Pos;
  705. uint16_t const fifo_start = (usb_otg->DIEPTXF[epnum - 1] & USB_OTG_DIEPTXF_INEPTXSA_Msk) >> USB_OTG_DIEPTXF_INEPTXSA_Pos;
  706. // For now only the last opened endpoint can be closed without fuss.
  707. TU_ASSERT(fifo_start == EP_FIFO_SIZE/4 - _allocated_fifo_words_tx,);
  708. _allocated_fifo_words_tx -= fifo_size;
  709. }
  710. else
  711. {
  712. _out_ep_closed = true; // Set flag such that RX FIFO gets reduced in size once RX FIFO is empty
  713. }
  714. }
  715. void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
  716. {
  717. dcd_edpt_disable(rhport, ep_addr, true);
  718. }
  719. void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
  720. {
  721. (void) rhport;
  722. USB_OTG_OUTEndpointTypeDef * out_ep = OUT_EP_BASE(rhport);
  723. USB_OTG_INEndpointTypeDef * in_ep = IN_EP_BASE(rhport);
  724. uint8_t const epnum = tu_edpt_number(ep_addr);
  725. uint8_t const dir = tu_edpt_dir(ep_addr);
  726. // Clear stall and reset data toggle
  727. if(dir == TUSB_DIR_IN) {
  728. in_ep[epnum].DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  729. in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  730. } else {
  731. out_ep[epnum].DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  732. out_ep[epnum].DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
  733. }
  734. }
  735. /*------------------------------------------------------------------*/
  736. // Read a single data packet from receive FIFO
  737. static void read_fifo_packet(uint8_t rhport, uint8_t * dst, uint16_t len)
  738. {
  739. (void) rhport;
  740. usb_fifo_t rx_fifo = FIFO_BASE(rhport, 0);
  741. // Reading full available 32 bit words from fifo
  742. uint16_t full_words = len >> 2;
  743. for(uint16_t i = 0; i < full_words; i++) {
  744. uint32_t tmp = *rx_fifo;
  745. dst[0] = tmp & 0x000000FF;
  746. dst[1] = (tmp & 0x0000FF00) >> 8;
  747. dst[2] = (tmp & 0x00FF0000) >> 16;
  748. dst[3] = (tmp & 0xFF000000) >> 24;
  749. dst += 4;
  750. }
  751. // Read the remaining 1-3 bytes from fifo
  752. uint8_t bytes_rem = len & 0x03;
  753. if(bytes_rem != 0) {
  754. uint32_t tmp = *rx_fifo;
  755. dst[0] = tmp & 0x000000FF;
  756. if(bytes_rem > 1) {
  757. dst[1] = (tmp & 0x0000FF00) >> 8;
  758. }
  759. if(bytes_rem > 2) {
  760. dst[2] = (tmp & 0x00FF0000) >> 16;
  761. }
  762. }
  763. }
  764. // Write a single data packet to EPIN FIFO
  765. static void write_fifo_packet(uint8_t rhport, uint8_t fifo_num, uint8_t * src, uint16_t len)
  766. {
  767. (void) rhport;
  768. usb_fifo_t tx_fifo = FIFO_BASE(rhport, fifo_num);
  769. // Pushing full available 32 bit words to fifo
  770. uint16_t full_words = len >> 2;
  771. for(uint16_t i = 0; i < full_words; i++){
  772. *tx_fifo = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0];
  773. src += 4;
  774. }
  775. // Write the remaining 1-3 bytes into fifo
  776. uint8_t bytes_rem = len & 0x03;
  777. if(bytes_rem){
  778. uint32_t tmp_word = 0;
  779. tmp_word |= src[0];
  780. if(bytes_rem > 1){
  781. tmp_word |= src[1] << 8;
  782. }
  783. if(bytes_rem > 2){
  784. tmp_word |= src[2] << 16;
  785. }
  786. *tx_fifo = tmp_word;
  787. }
  788. }
  789. static void handle_rxflvl_ints(uint8_t rhport, USB_OTG_OUTEndpointTypeDef * out_ep) {
  790. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  791. usb_fifo_t rx_fifo = FIFO_BASE(rhport, 0);
  792. // Pop control word off FIFO
  793. uint32_t ctl_word = usb_otg->GRXSTSP;
  794. uint8_t pktsts = (ctl_word & USB_OTG_GRXSTSP_PKTSTS_Msk) >> USB_OTG_GRXSTSP_PKTSTS_Pos;
  795. uint8_t epnum = (ctl_word & USB_OTG_GRXSTSP_EPNUM_Msk) >> USB_OTG_GRXSTSP_EPNUM_Pos;
  796. uint16_t bcnt = (ctl_word & USB_OTG_GRXSTSP_BCNT_Msk) >> USB_OTG_GRXSTSP_BCNT_Pos;
  797. switch(pktsts) {
  798. case 0x01: // Global OUT NAK (Interrupt)
  799. break;
  800. case 0x02: // Out packet recvd
  801. {
  802. xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, TUSB_DIR_OUT);
  803. // Read packet off RxFIFO
  804. if (xfer->ff)
  805. {
  806. // Ring buffer
  807. tu_fifo_write_n_const_addr_full_words(xfer->ff, (const void *)(uintptr_t) rx_fifo, bcnt);
  808. }
  809. else
  810. {
  811. // Linear buffer
  812. read_fifo_packet(rhport, xfer->buffer, bcnt);
  813. // Increment pointer to xfer data
  814. xfer->buffer += bcnt;
  815. }
  816. // Truncate transfer length in case of short packet
  817. if(bcnt < xfer->max_size) {
  818. xfer->total_len -= (out_ep[epnum].DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ_Msk) >> USB_OTG_DOEPTSIZ_XFRSIZ_Pos;
  819. if(epnum == 0) {
  820. xfer->total_len -= ep0_pending[TUSB_DIR_OUT];
  821. ep0_pending[TUSB_DIR_OUT] = 0;
  822. }
  823. }
  824. }
  825. break;
  826. case 0x03: // Out packet done (Interrupt)
  827. break;
  828. case 0x04: // Setup packet done (Interrupt)
  829. out_ep[epnum].DOEPTSIZ |= (3 << USB_OTG_DOEPTSIZ_STUPCNT_Pos);
  830. break;
  831. case 0x06: // Setup packet recvd
  832. // We can receive up to three setup packets in succession, but
  833. // only the last one is valid.
  834. _setup_packet[0] = (* rx_fifo);
  835. _setup_packet[1] = (* rx_fifo);
  836. break;
  837. default: // Invalid
  838. TU_BREAKPOINT();
  839. break;
  840. }
  841. }
  842. static void handle_epout_ints(uint8_t rhport, USB_OTG_DeviceTypeDef * dev, USB_OTG_OUTEndpointTypeDef * out_ep) {
  843. // DAINT for a given EP clears when DOEPINTx is cleared.
  844. // OEPINT will be cleared when DAINT's out bits are cleared.
  845. for(uint8_t n = 0; n < EP_MAX; n++) {
  846. xfer_ctl_t * xfer = XFER_CTL_BASE(n, TUSB_DIR_OUT);
  847. if(dev->DAINT & (1 << (USB_OTG_DAINT_OEPINT_Pos + n))) {
  848. // SETUP packet Setup Phase done.
  849. if(out_ep[n].DOEPINT & USB_OTG_DOEPINT_STUP) {
  850. out_ep[n].DOEPINT = USB_OTG_DOEPINT_STUP;
  851. dcd_event_setup_received(rhport, (uint8_t*) &_setup_packet[0], true);
  852. }
  853. // OUT XFER complete
  854. if(out_ep[n].DOEPINT & USB_OTG_DOEPINT_XFRC) {
  855. out_ep[n].DOEPINT = USB_OTG_DOEPINT_XFRC;
  856. // EP0 can only handle one packet
  857. if((n == 0) && ep0_pending[TUSB_DIR_OUT]) {
  858. // Schedule another packet to be received.
  859. edpt_schedule_packets(rhport, n, TUSB_DIR_OUT, 1, ep0_pending[TUSB_DIR_OUT]);
  860. } else {
  861. dcd_event_xfer_complete(rhport, n, xfer->total_len, XFER_RESULT_SUCCESS, true);
  862. }
  863. }
  864. }
  865. }
  866. }
  867. static void handle_epin_ints(uint8_t rhport, USB_OTG_DeviceTypeDef * dev, USB_OTG_INEndpointTypeDef * in_ep) {
  868. // DAINT for a given EP clears when DIEPINTx is cleared.
  869. // IEPINT will be cleared when DAINT's out bits are cleared.
  870. for ( uint8_t n = 0; n < EP_MAX; n++ )
  871. {
  872. xfer_ctl_t *xfer = XFER_CTL_BASE(n, TUSB_DIR_IN);
  873. if ( dev->DAINT & (1 << (USB_OTG_DAINT_IEPINT_Pos + n)) )
  874. {
  875. // IN XFER complete (entire xfer).
  876. if ( in_ep[n].DIEPINT & USB_OTG_DIEPINT_XFRC )
  877. {
  878. in_ep[n].DIEPINT = USB_OTG_DIEPINT_XFRC;
  879. // EP0 can only handle one packet
  880. if((n == 0) && ep0_pending[TUSB_DIR_IN]) {
  881. // Schedule another packet to be transmitted.
  882. edpt_schedule_packets(rhport, n, TUSB_DIR_IN, 1, ep0_pending[TUSB_DIR_IN]);
  883. } else {
  884. dcd_event_xfer_complete(rhport, n | TUSB_DIR_IN_MASK, xfer->total_len, XFER_RESULT_SUCCESS, true);
  885. }
  886. }
  887. // XFER FIFO empty
  888. if ( (in_ep[n].DIEPINT & USB_OTG_DIEPINT_TXFE) && (dev->DIEPEMPMSK & (1 << n)) )
  889. {
  890. // DIEPINT's TXFE bit is read-only, software cannot clear it.
  891. // It will only be cleared by hardware when written bytes is more than
  892. // - 64 bytes or
  893. // - Half of TX FIFO size (configured by DIEPTXF)
  894. uint16_t remaining_packets = (in_ep[n].DIEPTSIZ & USB_OTG_DIEPTSIZ_PKTCNT_Msk) >> USB_OTG_DIEPTSIZ_PKTCNT_Pos;
  895. // Process every single packet (only whole packets can be written to fifo)
  896. for(uint16_t i = 0; i < remaining_packets; i++)
  897. {
  898. uint16_t const remaining_bytes = (in_ep[n].DIEPTSIZ & USB_OTG_DIEPTSIZ_XFRSIZ_Msk) >> USB_OTG_DIEPTSIZ_XFRSIZ_Pos;
  899. // Packet can not be larger than ep max size
  900. uint16_t const packet_size = tu_min16(remaining_bytes, xfer->max_size);
  901. // It's only possible to write full packets into FIFO. Therefore DTXFSTS register of current
  902. // EP has to be checked if the buffer can take another WHOLE packet
  903. if(packet_size > ((in_ep[n].DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV_Msk) << 2)) break;
  904. // Push packet to Tx-FIFO
  905. if (xfer->ff)
  906. {
  907. usb_fifo_t tx_fifo = FIFO_BASE(rhport, n);
  908. tu_fifo_read_n_const_addr_full_words(xfer->ff, (void *)(uintptr_t) tx_fifo, packet_size);
  909. }
  910. else
  911. {
  912. write_fifo_packet(rhport, n, xfer->buffer, packet_size);
  913. // Increment pointer to xfer data
  914. xfer->buffer += packet_size;
  915. }
  916. }
  917. // Turn off TXFE if all bytes are written.
  918. if (((in_ep[n].DIEPTSIZ & USB_OTG_DIEPTSIZ_XFRSIZ_Msk) >> USB_OTG_DIEPTSIZ_XFRSIZ_Pos) == 0)
  919. {
  920. dev->DIEPEMPMSK &= ~(1 << n);
  921. }
  922. }
  923. }
  924. }
  925. }
  926. void dcd_int_handler(uint8_t rhport)
  927. {
  928. USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport);
  929. USB_OTG_DeviceTypeDef * dev = DEVICE_BASE(rhport);
  930. USB_OTG_OUTEndpointTypeDef * out_ep = OUT_EP_BASE(rhport);
  931. USB_OTG_INEndpointTypeDef * in_ep = IN_EP_BASE(rhport);
  932. uint32_t const int_status = usb_otg->GINTSTS & usb_otg->GINTMSK;
  933. if(int_status & USB_OTG_GINTSTS_USBRST)
  934. {
  935. // USBRST is start of reset.
  936. usb_otg->GINTSTS = USB_OTG_GINTSTS_USBRST;
  937. bus_reset(rhport);
  938. }
  939. if(int_status & USB_OTG_GINTSTS_ENUMDNE)
  940. {
  941. // ENUMDNE is the end of reset where speed of the link is detected
  942. usb_otg->GINTSTS = USB_OTG_GINTSTS_ENUMDNE;
  943. tusb_speed_t const speed = get_speed(rhport);
  944. set_turnaround(usb_otg, speed);
  945. dcd_event_bus_reset(rhport, speed, true);
  946. }
  947. if(int_status & USB_OTG_GINTSTS_USBSUSP)
  948. {
  949. usb_otg->GINTSTS = USB_OTG_GINTSTS_USBSUSP;
  950. dcd_event_bus_signal(rhport, DCD_EVENT_SUSPEND, true);
  951. }
  952. if(int_status & USB_OTG_GINTSTS_WKUINT)
  953. {
  954. usb_otg->GINTSTS = USB_OTG_GINTSTS_WKUINT;
  955. dcd_event_bus_signal(rhport, DCD_EVENT_RESUME, true);
  956. }
  957. // TODO check USB_OTG_GINTSTS_DISCINT for disconnect detection
  958. // if(int_status & USB_OTG_GINTSTS_DISCINT)
  959. if(int_status & USB_OTG_GINTSTS_OTGINT)
  960. {
  961. // OTG INT bit is read-only
  962. uint32_t const otg_int = usb_otg->GOTGINT;
  963. if (otg_int & USB_OTG_GOTGINT_SEDET)
  964. {
  965. dcd_event_bus_signal(rhport, DCD_EVENT_UNPLUGGED, true);
  966. }
  967. usb_otg->GOTGINT = otg_int;
  968. }
  969. if(int_status & USB_OTG_GINTSTS_SOF)
  970. {
  971. usb_otg->GINTSTS = USB_OTG_GINTSTS_SOF;
  972. // Disable SOF interrupt since currently only used for remote wakeup detection
  973. usb_otg->GINTMSK &= ~USB_OTG_GINTMSK_SOFM;
  974. dcd_event_bus_signal(rhport, DCD_EVENT_SOF, true);
  975. }
  976. // RxFIFO non-empty interrupt handling.
  977. if(int_status & USB_OTG_GINTSTS_RXFLVL)
  978. {
  979. // RXFLVL bit is read-only
  980. // Mask out RXFLVL while reading data from FIFO
  981. usb_otg->GINTMSK &= ~USB_OTG_GINTMSK_RXFLVLM;
  982. // Loop until all available packets were handled
  983. do
  984. {
  985. handle_rxflvl_ints(rhport, out_ep);
  986. } while(usb_otg->GINTSTS & USB_OTG_GINTSTS_RXFLVL);
  987. // Manage RX FIFO size
  988. if (_out_ep_closed)
  989. {
  990. update_grxfsiz(rhport);
  991. // Disable flag
  992. _out_ep_closed = false;
  993. }
  994. usb_otg->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  995. }
  996. // OUT endpoint interrupt handling.
  997. if(int_status & USB_OTG_GINTSTS_OEPINT)
  998. {
  999. // OEPINT is read-only
  1000. handle_epout_ints(rhport, dev, out_ep);
  1001. }
  1002. // IN endpoint interrupt handling.
  1003. if(int_status & USB_OTG_GINTSTS_IEPINT)
  1004. {
  1005. // IEPINT bit read-only
  1006. handle_epin_ints(rhport, dev, in_ep);
  1007. }
  1008. // // Check for Incomplete isochronous IN transfer
  1009. // if(int_status & USB_OTG_GINTSTS_IISOIXFR) {
  1010. // printf(" IISOIXFR!\r\n");
  1011. //// TU_LOG2(" IISOIXFR!\r\n");
  1012. // }
  1013. }
  1014. #endif