Simple USBHost library for STM32F746NG Discovery board. Only either the Fastspeed or the Highspeed port can be used( not both together)
Dependents: DISCO-F746NG_USB_Host
Fork of KL46Z-USBHost by
Revision 25:7d6d9fc471bf, committed 2016-06-17
- Comitter:
- DieterGraef
- Date:
- Fri Jun 17 09:00:35 2016 +0000
- Parent:
- 24:5396b6a93262
- Commit message:
- USB Host now works with both Interfaces even in parallel. Some changes in the USB MSD driver to make it operable
Changed in this revision
--- a/USBHost/TARGET_STM32F7/USBHALHost_F746NG.cpp Mon Jun 13 17:21:07 2016 +0000 +++ b/USBHost/TARGET_STM32F7/USBHALHost_F746NG.cpp Fri Jun 17 09:00:35 2016 +0000 @@ -1,13 +1,10 @@ #if defined(TARGET_DISCO_F746NG) -#include "mbed.h" -#include "stm32f7xx_hal.h" #include "USBHALHost.h" #include <algorithm> -#include "USB_Helper.h" +#include "mydebug.h" + + #ifdef _USB_DBG -//extern RawSerial pc; -//RawSerial pc(USBTX,USBRX); -#include "mydebug.h" #define USB_DBG(...) do{printf("[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);printf(__VA_ARGS__);printf("\n");} while(0); #define USB_DBG_HEX(A,B) debug_hex<RawSerial>(pc,A,B) @@ -34,7 +31,10 @@ // usbh_conf.c //__attribute__((section(".dmatransfer"))); - +HCD_HandleTypeDef hhcd_USB_FS; +HCD_HandleTypeDef hhcd_USB_HS; +HCD_URBStateTypeDef URB_FS; +HCD_URBStateTypeDef URB_HS; void HAL_HCD_MspInit(HCD_HandleTypeDef* hhcd) { @@ -139,7 +139,6 @@ USBHALHost::USBHALHost(int InterfaceNumber) { IF_N=InterfaceNumber; - hhcd=&hhcd_USB; instHost = this; } @@ -147,56 +146,56 @@ { if(IF_N==USB_FastSpeed) { - hhcd_USB.Instance = USB_OTG_FS; - hhcd_USB.Init.Host_channels = 11; - hhcd_USB.Init.dma_enable = 0; - hhcd_USB.Init.low_power_enable = 0; - hhcd_USB.Init.phy_itface = HCD_PHY_EMBEDDED; - hhcd_USB.Init.Sof_enable = 1; - hhcd_USB.Init.speed = HCD_SPEED_FULL; - hhcd_USB.Init.vbus_sensing_enable = 0; - hhcd_USB.Init.lpm_enable = 0; + hhcd_USB_FS.Instance = USB_OTG_FS; + hhcd_USB_FS.Init.Host_channels = 11; + hhcd_USB_FS.Init.dma_enable = 0; + hhcd_USB_FS.Init.low_power_enable = 0; + hhcd_USB_FS.Init.phy_itface = HCD_PHY_EMBEDDED; + hhcd_USB_FS.Init.Sof_enable = 1; + hhcd_USB_FS.Init.speed = HCD_SPEED_FULL; + hhcd_USB_FS.Init.vbus_sensing_enable = 0; + hhcd_USB_FS.Init.lpm_enable = 0; - HAL_HCD_Init(&hhcd_USB); + HAL_HCD_Init(&hhcd_USB_FS); /* Peripheral interrupt init*/ /* Sets the priority grouping field */ - NVIC_SetVector(OTG_FS_IRQn, (uint32_t)&_usbisr); + NVIC_SetVector(OTG_FS_IRQn, (uint32_t)&_usbisr_FS); HAL_NVIC_SetPriority(OTG_FS_IRQn, 6, 0); HAL_NVIC_EnableIRQ(OTG_FS_IRQn); - HAL_HCD_Start(&hhcd_USB); + HAL_HCD_Start(&hhcd_USB_FS); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_RESET); bool lowSpeed = wait_attach(); delay_ms(200); - HAL_HCD_ResetPort(&hhcd_USB); + HAL_HCD_ResetPort(&hhcd_USB_FS); delay_ms(100); // Wait for 100 ms after Reset addDevice(NULL, 0, lowSpeed); } if(IF_N==USB_HighSpeed) { - hhcd_USB.Instance = USB_OTG_HS; - hhcd_USB.Init.Host_channels = 8; - hhcd_USB.Init.dma_enable = 1; - hhcd_USB.Init.low_power_enable = 0; - hhcd_USB.Init.phy_itface = HCD_PHY_ULPI; - hhcd_USB.Init.Sof_enable = 1; - hhcd_USB.Init.speed = HCD_SPEED_HIGH; - hhcd_USB.Init.vbus_sensing_enable = 0; - hhcd_USB.Init.use_external_vbus = 1; - hhcd_USB.Init.lpm_enable = 0; + hhcd_USB_HS.Instance = USB_OTG_HS; + hhcd_USB_HS.Init.Host_channels = 8; + hhcd_USB_HS.Init.dma_enable = 0; + hhcd_USB_HS.Init.low_power_enable = 0; + hhcd_USB_HS.Init.phy_itface = HCD_PHY_ULPI; + hhcd_USB_HS.Init.Sof_enable = 1; + hhcd_USB_HS.Init.speed = HCD_SPEED_HIGH; + hhcd_USB_HS.Init.vbus_sensing_enable = 0; + hhcd_USB_HS.Init.use_external_vbus = 1; + hhcd_USB_HS.Init.lpm_enable = 0; - HAL_HCD_Init(&hhcd_USB); - NVIC_SetVector(OTG_HS_IRQn, (uint32_t)&_usbisr); + HAL_HCD_Init(&hhcd_USB_HS); + NVIC_SetVector(OTG_HS_IRQn, (uint32_t)&_usbisr_HS); HAL_NVIC_SetPriority(OTG_HS_IRQn, 6, 0); HAL_NVIC_EnableIRQ(OTG_HS_IRQn); - HAL_HCD_Start(&hhcd_USB); + HAL_HCD_Start(&hhcd_USB_HS); bool lowSpeed = wait_attach(); delay_ms(200); - HAL_HCD_ResetPort(&hhcd_USB); + HAL_HCD_ResetPort(&hhcd_USB_HS); delay_ms(100); // Wait for 100 ms after Reset addDevice(NULL, 0, lowSpeed); } @@ -216,7 +215,14 @@ } } wait_ms(100); - return HAL_HCD_GetCurrentSpeed(&hhcd_USB) == USB_OTG_SPEED_LOW; + if(IF_N==USB_FastSpeed) + { + return HAL_HCD_GetCurrentSpeed(&hhcd_USB_FS) == USB_OTG_SPEED_LOW; + } + else + { + return HAL_HCD_GetCurrentSpeed(&hhcd_USB_HS) == USB_OTG_SPEED_LOW; + } } @@ -224,45 +230,63 @@ int USBHALHost::token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength) { const uint8_t ep_addr = 0x00; - HC hc(&hhcd_USB); - HCD_URBStateTypeDef st; - USBDeviceConnected* dev = ep->getDevice(); if(IF_N==USB_FastSpeed) { - hc.Init(IF_N, ep_addr, + HC_FS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep_addr, dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL, EP_TYPE_CTRL, ep->getSize()); + setup->wLength = wLength; + + hc.SubmitRequest((uint8_t*)setup, 8, true); // PID_SETUP + while(hc.GetURBState() == URB_IDLE); + + switch(hc.GetURBState()) + { + case URB_DONE: + LastStatus = ACK; + break; + default: + LastStatus = 0xff; + break; + } + ep->setData01(DATA1); + return 8; } else { - hc.Init(IF_N,ep_addr, + HC_HS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep_addr, dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH , EP_TYPE_CTRL, ep->getSize()); - } + setup->wLength = wLength; - setup->wLength = wLength; - st=hc.GetURBState(); - hc.SubmitRequest(IF_N,(uint8_t*)setup, 8, true); // PID_SETUP - while(hc.GetURBState() == URB_IDLE); + hc.SubmitRequest((uint8_t*)setup, 8, true); // PID_SETUP + while(hc.GetURBState() == URB_IDLE); - switch(hc.GetURBState()) - { - case URB_DONE: - LastStatus = ACK; - break; - default: - LastStatus = 0xff; - break; + switch(hc.GetURBState()) + { + case URB_DONE: + LastStatus = ACK; + break; + default: + LastStatus = 0xff; + break; + } + ep->setData01(DATA1); + return 8; } - ep->setData01(DATA1); - return 8; } int USBHALHost::token_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) { - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); + switch(ep->getType()) { case CONTROL_ENDPOINT: @@ -278,124 +302,117 @@ int USBHALHost::token_ctl_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) { const uint8_t ep_addr = 0x80; - HC hc(&hhcd_USB); - HCD_URBStateTypeDef st; - USBDeviceConnected* dev = ep->getDevice(); if(IF_N==USB_FastSpeed) { - hc.Init(IF_N,ep_addr, + HC_FS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep_addr, dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL, EP_TYPE_CTRL, ep->getSize()); + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + + wait_ms(1); + hc.SubmitRequest(data, size); + while(hc.GetURBState() == URB_IDLE); + + + switch(hc.GetURBState()) + { + case URB_DONE: + LastStatus = ACK; + break; + default: + LastStatus = 0xff; + return -1; + } + ep->toggleData01(); + return hc.GetXferCount(); } else { - hc.Init(IF_N,ep_addr, + HC_HS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep_addr, dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH , EP_TYPE_CTRL, ep->getSize()); - } + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + + wait_ms(1); + hc.SubmitRequest(data, size); + while(hc.GetURBState() == URB_IDLE); + - hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); - st=hc.GetURBState(); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); - wait_ms(1); - hc.SubmitRequest(IF_N,data, size); - while(hc.GetURBState() == URB_IDLE); - CPU_CACHE_Flush((uint32_t *)data,size); - st=hc.GetURBState(); - switch(hc.GetURBState()) - { - case URB_DONE: - LastStatus = ACK; - break; - default: - LastStatus = 0xff; - return -1; + switch(hc.GetURBState()) + { + case URB_DONE: + LastStatus = ACK; + break; + default: + LastStatus = 0xff; + return -1; + } + ep->toggleData01(); + return hc.GetXferCount(); } - ep->toggleData01(); - return hc.GetXferCount(); } int USBHALHost::token_int_in(USBEndpoint* ep, uint8_t* data, int size) { - HC hc(&hhcd_USB); - HCD_URBStateTypeDef st; - USBDeviceConnected* dev = ep->getDevice(); if(IF_N==USB_FastSpeed) { - hc.Init(IF_N,ep->getAddress(), + HC_FS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep->getAddress(), dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL, EP_TYPE_INTR, ep->getSize()); + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + + hc.SubmitRequest(data, size); + while(hc.GetURBState() == URB_IDLE); + + switch(hc.GetURBState()) + { + case URB_DONE: + switch(hc.GetState()) + { + case HC_XFRC: + LastStatus = ep->getData01(); + ep->toggleData01(); + return hc.GetXferCount(); + case HC_NAK: + LastStatus = NAK; + return -1; + } + break; + } + LastStatus = STALL; + return -1; } else { - hc.Init(IF_N,ep->getAddress(), + HC_HS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep->getAddress(), dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH , EP_TYPE_INTR, ep->getSize()); - } + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); - hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); - st=hc.GetURBState(); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); - hc.SubmitRequest(IF_N,data, size); - while(hc.GetURBState() == URB_IDLE); - CPU_CACHE_Flush((uint32_t *)data,size); - switch(hc.GetURBState()) - { - case URB_DONE: - switch(hc.GetState()) - { - case HC_XFRC: - LastStatus = ep->getData01(); - ep->toggleData01(); - return hc.GetXferCount(); - case HC_NAK: - LastStatus = NAK; - return -1; - } - break; - } - LastStatus = STALL; - return -1; -} -int USBHALHost::token_blk_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) -{ - HC hc(&hhcd_USB); - HCD_URBStateTypeDef st; - USBDeviceConnected* dev = ep->getDevice(); - if(IF_N==USB_FastSpeed) - { - hc.Init(IF_N, - ep->getAddress(), - dev->getAddress(), - HCD_SPEED_FULL, - EP_TYPE_BULK, ep->getSize()); - } - else - { - hc.Init(IF_N, - ep->getAddress(), - dev->getAddress(), - HCD_SPEED_HIGH, - EP_TYPE_BULK, ep->getSize()); - } + hc.SubmitRequest(data, size); + while(hc.GetURBState() == URB_IDLE); - hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); - - int retry = 0; - do - { - st=hc.GetURBState(); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); - hc.SubmitRequest(IF_N,data, size); - while(hc.GetURBState() == URB_IDLE); - CPU_CACHE_Flush((uint32_t *)data,size); - st=hc.GetURBState(); - switch(st) + switch(hc.GetURBState()) { case URB_DONE: switch(hc.GetState()) @@ -406,32 +423,129 @@ return hc.GetXferCount(); case HC_NAK: LastStatus = NAK; - if (retryLimit > 0) - { - delay_ms(1 + 10 * retry); - } - break; - default: - break; + return -1; } break; - case URB_STALL: - LastStatus = STALL; - return -1; - default: - LastStatus = STALL; - delay_ms(500 + 100 * retry); - break; } + LastStatus = STALL; + return -1; } - while(retry++ < retryLimit); - return -1; +} + +int USBHALHost::token_blk_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) +{ + if(IF_N==USB_FastSpeed) + { + HC_FS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init( + ep->getAddress(), + dev->getAddress(), + HCD_SPEED_FULL, + EP_TYPE_BULK, ep->getSize()); + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + int retry = 0; + do + { + + + hc.SubmitRequest(data, size); + while(hc.GetURBState() == URB_IDLE); + + + switch(hc.GetURBState()) + { + case URB_DONE: + switch(hc.GetState()) + { + case HC_XFRC: + LastStatus = ep->getData01(); + ep->toggleData01(); + return hc.GetXferCount(); + case HC_NAK: + LastStatus = NAK; + if (retryLimit > 0) + { + delay_ms(1 + 10 * retry); + } + break; + default: + break; + } + break; + case URB_STALL: + LastStatus = STALL; + return -1; + default: + LastStatus = STALL; + delay_ms(500 + 100 * retry); + break; + } + } + while(retry++ < retryLimit); + return -1; + } + else + { + HC_HS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init( + ep->getAddress(), + dev->getAddress(), + HCD_SPEED_HIGH, + EP_TYPE_BULK, ep->getSize()); + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + int retry = 0; + do + { + + + + hc.SubmitRequest(data, size); + while(hc.GetURBState() == URB_IDLE); + + + switch(hc.GetURBState()) + { + case URB_DONE: + switch(hc.GetState()) + { + case HC_XFRC: + LastStatus = ep->getData01(); + ep->toggleData01(); + return hc.GetXferCount(); + case HC_NAK: + LastStatus = NAK; + if (retryLimit > 0) + { + delay_ms(1 + 10 * retry); + } + break; + default: + break; + } + break; + case URB_STALL: + LastStatus = STALL; + return -1; + default: + LastStatus = STALL; + delay_ms(500 + 100 * retry); + break; + } + } + while(retry++ < retryLimit); + return -1; + } } int USBHALHost::token_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) { - CPU_CACHE_Flush((uint32_t *)data,size); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); + + switch(ep->getType()) { case CONTROL_ENDPOINT: @@ -447,171 +561,258 @@ int USBHALHost::token_ctl_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) { const uint8_t ep_addr = 0x00; - HC hc(&hhcd_USB); - HCD_URBStateTypeDef st; - USBDeviceConnected* dev = ep->getDevice(); if(IF_N==USB_FastSpeed) { - hc.Init(IF_N,ep_addr, + HC_FS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep_addr, dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL, EP_TYPE_CTRL, ep->getSize()); + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + do + { + + + wait_ms(1); + hc.SubmitRequest((uint8_t*)data, size); + while(hc.GetURBState() == URB_IDLE); + + switch(hc.GetURBState()) + { + case URB_DONE: + LastStatus = ACK; + ep->toggleData01(); + return size; + + default: + break; + } + delay_ms(1); + } + while(retryLimit-- > 0); + return -1; } else { - hc.Init(IF_N,ep_addr, + HC_HS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init(ep_addr, dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH , EP_TYPE_CTRL, ep->getSize()); - } + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + do + { - hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + wait_ms(1); + hc.SubmitRequest((uint8_t*)data, size); + while(hc.GetURBState() == URB_IDLE); - do - { - st=hc.GetURBState(); - CPU_CACHE_Flush((uint32_t *)data,size); - wait_ms(1); - hc.SubmitRequest(IF_N,(uint8_t*)data, size); - while(hc.GetURBState() == URB_IDLE); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); - switch(hc.GetURBState()) - { - case URB_DONE: - LastStatus = ACK; - ep->toggleData01(); - return size; + switch(hc.GetURBState()) + { + case URB_DONE: + LastStatus = ACK; + ep->toggleData01(); + return size; - default: - break; + default: + break; + } + delay_ms(1); } - delay_ms(1); + while(retryLimit-- > 0); + return -1; } - while(retryLimit-- > 0); - return -1; } int USBHALHost::token_int_out(USBEndpoint* ep, const uint8_t* data, int size) { - HC hc(&hhcd_USB); - HCD_URBStateTypeDef st; - USBDeviceConnected* dev = ep->getDevice(); if(IF_N==USB_FastSpeed) { - hc.Init(IF_N, + HC_FS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init( ep->getAddress(), dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL, EP_TYPE_INTR, ep->getSize()); + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + + hc.SubmitRequest((uint8_t*)data, size); + while(hc.GetURBState() == URB_IDLE); + + if (hc.GetURBState() != URB_DONE) + { + return -1; + } + ep->toggleData01(); + return size; } else { - hc.Init(IF_N, + HC_HS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init( ep->getAddress(), dev->getAddress(), dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH , EP_TYPE_INTR, ep->getSize()); - } + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + + hc.SubmitRequest((uint8_t*)data, size); + while(hc.GetURBState() == URB_IDLE); - hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); - st=hc.GetURBState(); - CPU_CACHE_Flush((uint32_t *)data,size); - hc.SubmitRequest(IF_N,(uint8_t*)data, size); - while(hc.GetURBState() == URB_IDLE); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); - if (hc.GetURBState() != URB_DONE) - { - return -1; + if (hc.GetURBState() != URB_DONE) + { + return -1; + } + ep->toggleData01(); + return size; } - ep->toggleData01(); - return size; } int USBHALHost::token_blk_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) { - HC hc(&hhcd_USB); - HCD_URBStateTypeDef st; - USBDeviceConnected* dev = ep->getDevice(); if(IF_N==USB_FastSpeed) { + HC_FS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init( + ep->getAddress(), dev->getAddress(), + HCD_SPEED_FULL, EP_TYPE_BULK, ep->getSize()); + + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + int retry = 0; + do + { - hc.Init(IF_N, - ep->getAddress(), dev->getAddress(), - HCD_SPEED_FULL, EP_TYPE_BULK, ep->getSize()); + hc.SubmitRequest((uint8_t*)data, size); + while(hc.GetURBState() == URB_IDLE); + + switch(hc.GetURBState()) + { + case URB_DONE: + switch(hc.GetState()) + { + case HC_XFRC: // ACK + LastStatus = ep->getData01(); + ep->toggleData01(); + return size; + default: + break; + } + break; + case URB_NOTREADY: // HC_NAK + LastStatus = NAK; + delay_ms(100 * retry); + break; + default: + LastStatus = STALL; + return -1; + } + } + while(retry++ < retryLimit); + return -1; } else { - hc.Init(IF_N, + HC_HS hc; + + USBDeviceConnected* dev = ep->getDevice(); + hc.Init( ep->getAddress(), dev->getAddress(), HCD_SPEED_HIGH, EP_TYPE_BULK, ep->getSize()); - } - hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1); + + int retry = 0; + do + { - int retry = 0; - do - { - st=hc.GetURBState(); - CPU_CACHE_Flush((uint32_t *)data,size); - hc.SubmitRequest(IF_N,(uint8_t*)data, size); - while(hc.GetURBState() == URB_IDLE); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); - switch(hc.GetURBState()) - { - case URB_DONE: - switch(hc.GetState()) + + hc.SubmitRequest((uint8_t*)data, size); + while(hc.GetURBState() == URB_IDLE); + + switch(hc.GetURBState()) { - case HC_XFRC: // ACK - LastStatus = ep->getData01(); - ep->toggleData01(); - return size; + case URB_DONE: + switch(hc.GetState()) + { + case HC_XFRC: // ACK + LastStatus = ep->getData01(); + ep->toggleData01(); + return size; + default: + break; + } + break; + case URB_NOTREADY: // HC_NAK + LastStatus = NAK; + delay_ms(100 * retry); + break; default: - break; + LastStatus = STALL; + return -1; } - break; - case URB_NOTREADY: // HC_NAK - LastStatus = NAK; - delay_ms(100 * retry); - break; - default: - LastStatus = STALL; - return -1; } + while(retry++ < retryLimit); + return -1; } - while(retry++ < retryLimit); - return -1; } int USBHALHost::token_iso_in(USBEndpoint* ep, uint8_t* data, int size) { - HCD_URBStateTypeDef st; - HC* hc = ep->getHALData<HC*>(); - if (hc == NULL) + + if(IF_N==USB_FastSpeed) { - hc = new HC(&hhcd_USB); - ep->setHALData<HC*>(hc); - USBDeviceConnected* dev = ep->getDevice(); - if(IF_N==USB_FastSpeed) + HC_FS* hc = ep->getHALData<HC_FS*>(); + if (hc == NULL) { - - - hc->Init(IF_N, + hc = new HC_FS(); + ep->setHALData<HC_FS*>(hc); + USBDeviceConnected* dev = ep->getDevice(); + hc->Init( ep->getAddress(), dev->getAddress(), HCD_SPEED_FULL, EP_TYPE_ISOC, ep->getSize()); } - else - { - hc->Init(IF_N, - ep->getAddress(), dev->getAddress(), - HCD_SPEED_HIGH, EP_TYPE_ISOC, ep->getSize()); - } + + + hc->SubmitRequest(data, size); + while(hc->GetURBState() == URB_IDLE); + + return hc->GetXferCount(); } - st=hc->GetURBState(); - SCB_InvalidateDCache_by_Addr((uint32_t *)data,size); - hc->SubmitRequest(IF_N,data, size); - while(hc->GetURBState() == URB_IDLE); - CPU_CACHE_Flush((uint32_t *)data,size); - return hc->GetXferCount(); + else + { + HC_HS* hc = ep->getHALData<HC_HS*>(); + if (hc == NULL) + { + hc = new HC_HS(); + ep->setHALData<HC_HS*>(hc); + USBDeviceConnected* dev = ep->getDevice(); + hc->Init( + ep->getAddress(), dev->getAddress(), + HCD_SPEED_FULL, EP_TYPE_ISOC, ep->getSize()); + } + + + hc->SubmitRequest(data, size); + while(hc->GetURBState() == URB_IDLE); + + return hc->GetXferCount(); + } } int USBHALHost::multi_token_in(USBEndpoint* ep, uint8_t* data, size_t total, bool block) @@ -719,52 +920,56 @@ return 1; } -void USBHALHost::_usbisr(void) +void USBHALHost::_usbisr_FS(void) { - instHost->usbisr(); + instHost->usbisr_FS(); } +void USBHALHost::_usbisr_HS(void) +{ + instHost->usbisr_HS(); +} -void USBHALHost::usbisr(void) +void USBHALHost::usbisr_FS(void) { - USB_OTG_GlobalTypeDef *USBx = hhcd->Instance; + USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS; uint32_t i = 0 , interrupt = 0; /* ensure that we are in device mode */ - if (USB_GetMode(hhcd->Instance) == USB_OTG_MODE_HOST) + if (USB_GetMode(USB_OTG_FS) == USB_OTG_MODE_HOST) { /* avoid spurious interrupt */ - if(__HAL_HCD_IS_INVALID_INTERRUPT(hhcd)) + if(__HAL_HCD_IS_INVALID_INTERRUPT(&hhcd_USB_FS)) { return; } - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT)) { /* incorrect mode, acknowledge the interrupt */ - __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT); } - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_IISOIXFR)) { /* incorrect mode, acknowledge the interrupt */ - __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_IISOIXFR); } - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PTXFE)) { /* incorrect mode, acknowledge the interrupt */ - __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PTXFE); } - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_MMIS)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_MMIS)) { /* incorrect mode, acknowledge the interrupt */ - __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_MMIS); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_MMIS); } /* Handle Host Disconnect Interrupts */ - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_DISCINT)) { /* Cleanup HPRT */ @@ -772,16 +977,16 @@ USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG ); /* Handle Host Port Interrupts */ - HAL_HCD_Disconnect_Callback(hhcd); - USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_48_MHZ ); - __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT); + HAL_HCD_Disconnect_Callback(&hhcd_USB_FS); + USB_InitFSLSPClkSel(USB_OTG_FS ,HCFG_48_MHZ ); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_DISCINT); } /* Handle Host Port Interrupts */ - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HPRTINT)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_HPRTINT)) { // - USB_OTG_GlobalTypeDef *USBx = hhcd->Instance; + USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS; __IO uint32_t hprt0, hprt0_dup; /* Handle Host Port Interrupts */ @@ -796,8 +1001,8 @@ { if((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS) { - USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT); - HAL_HCD_Connect_Callback(hhcd); + USB_MASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_DISCINT); + HAL_HCD_Connect_Callback(&hhcd_USB_FS); } hprt0_dup |= USB_OTG_HPRT_PCDET; @@ -810,29 +1015,29 @@ if((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA) { - if(hhcd->Init.phy_itface == USB_OTG_EMBEDDED_PHY) + if(hhcd_USB_FS.Init.phy_itface == USB_OTG_EMBEDDED_PHY) { if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17)) { - USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_6_MHZ ); + USB_InitFSLSPClkSel(USB_OTG_FS ,HCFG_6_MHZ ); } else { - USB_InitFSLSPClkSel(hhcd->Instance ,HCFG_48_MHZ ); + USB_InitFSLSPClkSel(USB_OTG_FS ,HCFG_48_MHZ ); } } else { - if(hhcd->Init.speed == HCD_SPEED_FULL) + if(hhcd_USB_FS.Init.speed == HCD_SPEED_FULL) { USBx_HOST->HFIR = (uint32_t)60000; } } - HAL_HCD_Connect_Callback(hhcd); + HAL_HCD_Connect_Callback(&hhcd_USB_FS); - if(hhcd->Init.speed == HCD_SPEED_HIGH) + if(hhcd_USB_FS.Init.speed == HCD_SPEED_HIGH) { - USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT); + USB_UNMASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_DISCINT); } } else @@ -841,7 +1046,7 @@ USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\ USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG ); - USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_DISCINT); + USB_UNMASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_DISCINT); } } @@ -860,25 +1065,25 @@ /* Handle Host SOF Interrupts */ - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_SOF)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_SOF)) { - HAL_HCD_SOF_Callback(hhcd); - __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_SOF); + HAL_HCD_SOF_Callback(&hhcd_USB_FS); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_SOF); } /* Handle Host channel Interrupts */ - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HCINT)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_HCINT)) { - interrupt = USB_HC_ReadInterrupt(hhcd->Instance); - for (i = 0; i < hhcd->Init.Host_channels ; i++) + interrupt = USB_HC_ReadInterrupt(USB_OTG_FS); + for (i = 0; i < hhcd_USB_FS.Init.Host_channels ; i++) { if (interrupt & (1 << i)) { if ((USBx_HC(i)->HCCHAR) & USB_OTG_HCCHAR_EPDIR) { // - USB_OTG_GlobalTypeDef *USBx = hhcd->Instance; + USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS; uint8_t chnum = i; if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR) { @@ -893,112 +1098,112 @@ else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - hhcd->hc[chnum].state = HC_STALL; + hhcd_USB_FS.hc[chnum].state = HC_STALL; __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); - hhcd->hc[chnum].state = HC_DATATGLERR; + hhcd_USB_FS.hc[chnum].state = HC_DATATGLERR; __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR); } if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR); } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC) { - if (hhcd->Init.dma_enable) + if (hhcd_USB_FS.Init.dma_enable) { - hhcd->hc[chnum].xfer_count = hhcd->hc[chnum].xfer_len - \ - (USBx_HC(chnum)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ); + hhcd_USB_FS.hc[chnum].xfer_count = hhcd_USB_FS.hc[chnum].xfer_len - \ + (USBx_HC(chnum)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ); } - hhcd->hc[chnum].state = HC_XFRC; + hhcd_USB_FS.hc[chnum].state = HC_XFRC; __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC); - if ((hhcd->hc[chnum].ep_type == EP_TYPE_CTRL)|| - (hhcd->hc[chnum].ep_type == EP_TYPE_BULK)) + if ((hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_CTRL)|| + (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_BULK)) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); } - else if(hhcd->hc[chnum].ep_type == EP_TYPE_INTR) + else if(hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_INTR) { USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM; __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); } - else if(hhcd->hc[chnum].ep_type == EP_TYPE_ISOC) + else if(hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_ISOC) { USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM; - hhcd->hc[chnum].urb_state = URB_DONE; - HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state); + hhcd_USB_FS.hc[chnum].urb_state = URB_DONE; + HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_FS, chnum, hhcd_USB_FS.hc[chnum].urb_state); } - hhcd->hc[chnum].toggle_in ^= 1; + hhcd_USB_FS.hc[chnum].toggle_in ^= 1; } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH) { __HAL_HCD_MASK_HALT_HC_INT(chnum); - if(hhcd->hc[chnum].state == HC_XFRC) + if(hhcd_USB_FS.hc[chnum].state == HC_XFRC) { - hhcd->hc[chnum].urb_state = URB_DONE; + hhcd_USB_FS.hc[chnum].urb_state = URB_DONE; } - else if (hhcd->hc[chnum].state == HC_NAK) + else if (hhcd_USB_FS.hc[chnum].state == HC_NAK) { - hhcd->hc[chnum].urb_state = URB_DONE; + hhcd_USB_FS.hc[chnum].urb_state = URB_DONE; } - else if (hhcd->hc[chnum].state == HC_STALL) + else if (hhcd_USB_FS.hc[chnum].state == HC_STALL) { - hhcd->hc[chnum].urb_state = URB_STALL; + hhcd_USB_FS.hc[chnum].urb_state = URB_STALL; } - else if (hhcd->hc[chnum].state == HC_XACTERR) + else if (hhcd_USB_FS.hc[chnum].state == HC_XACTERR) { - hhcd->hc[chnum].urb_state = URB_NOTREADY; + hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY; } - else if (hhcd->hc[chnum].state == HC_DATATGLERR) + else if (hhcd_USB_FS.hc[chnum].state == HC_DATATGLERR) { - hhcd->hc[chnum].urb_state = URB_ERROR; + hhcd_USB_FS.hc[chnum].urb_state = URB_ERROR; } __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH); - HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state); + HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_FS, chnum, hhcd_USB_FS.hc[chnum].urb_state); } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - hhcd->hc[chnum].state = HC_XACTERR; - USB_HC_Halt(hhcd->Instance, chnum); + hhcd_USB_FS.hc[chnum].state = HC_XACTERR; + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR); } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK) { - if(hhcd->hc[chnum].ep_type == EP_TYPE_INTR) + if(hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_INTR) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); } - else if (hhcd->hc[chnum].ep_type == EP_TYPE_CTRL) + else if (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_CTRL) { /* re-activate the channel */ USBx_HC(chnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS; @@ -1006,13 +1211,13 @@ } - else if (hhcd->hc[chnum].ep_type == EP_TYPE_BULK) + else if (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_BULK) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); } - hhcd->hc[chnum].state = HC_NAK; + hhcd_USB_FS.hc[chnum].state = HC_NAK; __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); } // @@ -1021,7 +1226,7 @@ { // uint8_t chnum=i; - USB_OTG_GlobalTypeDef *USBx = hhcd->Instance; + USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS; if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR) { @@ -1032,20 +1237,20 @@ { __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK); - if( hhcd->hc[chnum].do_ping == 1) + if( hhcd_USB_FS.hc[chnum].do_ping == 1) { - hhcd->hc[chnum].state = HC_NYET; + hhcd_USB_FS.hc[chnum].state = HC_NYET; __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); - hhcd->hc[chnum].urb_state = URB_NOTREADY; + USB_HC_Halt(USB_OTG_FS, chnum); + hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY; } } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NYET) { - hhcd->hc[chnum].state = HC_NYET; + hhcd_USB_FS.hc[chnum].state = HC_NYET; __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NYET); } @@ -1053,16 +1258,16 @@ else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR); } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC); - hhcd->hc[chnum].state = HC_XFRC; + hhcd_USB_FS.hc[chnum].state = HC_XFRC; } @@ -1070,33 +1275,33 @@ { __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL); __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); - hhcd->hc[chnum].state = HC_STALL; + USB_HC_Halt(USB_OTG_FS, chnum); + hhcd_USB_FS.hc[chnum].state = HC_STALL; } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); - hhcd->hc[chnum].state = HC_NAK; + USB_HC_Halt(USB_OTG_FS, chnum); + hhcd_USB_FS.hc[chnum].state = HC_NAK; __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); - hhcd->hc[chnum].state = HC_XACTERR; + USB_HC_Halt(USB_OTG_FS, chnum); + hhcd_USB_FS.hc[chnum].state = HC_XACTERR; __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR); } else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR) { __HAL_HCD_UNMASK_HALT_HC_INT(chnum); - USB_HC_Halt(hhcd->Instance, chnum); + USB_HC_Halt(USB_OTG_FS, chnum); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR); - hhcd->hc[chnum].state = HC_DATATGLERR; + hhcd_USB_FS.hc[chnum].state = HC_DATATGLERR; } @@ -1104,64 +1309,64 @@ { __HAL_HCD_MASK_HALT_HC_INT(chnum); - if(hhcd->hc[chnum].state == HC_XFRC) + if(hhcd_USB_FS.hc[chnum].state == HC_XFRC) { - hhcd->hc[chnum].urb_state = URB_DONE; - if (hhcd->hc[chnum].ep_type == EP_TYPE_BULK) + hhcd_USB_FS.hc[chnum].urb_state = URB_DONE; + if (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_BULK) { - hhcd->hc[chnum].toggle_out ^= 1; + hhcd_USB_FS.hc[chnum].toggle_out ^= 1; } } - else if (hhcd->hc[chnum].state == HC_NAK) + else if (hhcd_USB_FS.hc[chnum].state == HC_NAK) { - hhcd->hc[chnum].urb_state = URB_NOTREADY; + hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY; } - else if (hhcd->hc[chnum].state == HC_NYET) + else if (hhcd_USB_FS.hc[chnum].state == HC_NYET) { - hhcd->hc[chnum].urb_state = URB_NOTREADY; - hhcd->hc[chnum].do_ping = 0; + hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY; + hhcd_USB_FS.hc[chnum].do_ping = 0; } - else if (hhcd->hc[chnum].state == HC_STALL) + else if (hhcd_USB_FS.hc[chnum].state == HC_STALL) { - hhcd->hc[chnum].urb_state = URB_STALL; + hhcd_USB_FS.hc[chnum].urb_state = URB_STALL; } - else if(hhcd->hc[chnum].state == HC_XACTERR) + else if(hhcd_USB_FS.hc[chnum].state == HC_XACTERR) { - hhcd->hc[chnum].urb_state = URB_NOTREADY; + hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY; } - else if (hhcd->hc[chnum].state == HC_DATATGLERR) + else if (hhcd_USB_FS.hc[chnum].state == HC_DATATGLERR) { - hhcd->hc[chnum].urb_state = URB_ERROR; + hhcd_USB_FS.hc[chnum].urb_state = URB_ERROR; } __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH); - HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state); + HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_FS, chnum, hhcd_USB_FS.hc[chnum].urb_state); } // } } } - __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_HCINT); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_HCINT); } /* Handle Rx Queue Level Interrupts */ - if(__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_RXFLVL)) + if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_RXFLVL)) { - USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL); + USB_MASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_RXFLVL); // - USB_OTG_GlobalTypeDef *USBx = hhcd->Instance; + USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS; uint8_t channelnum =0; uint32_t pktsts; uint32_t pktcnt; uint32_t temp = 0; - temp = hhcd->Instance->GRXSTSP ; + temp = USB_OTG_FS->GRXSTSP ; channelnum = temp & USB_OTG_GRXSTSP_EPNUM; pktsts = (temp & USB_OTG_GRXSTSP_PKTSTS) >> 17; pktcnt = (temp & USB_OTG_GRXSTSP_BCNT) >> 4; @@ -1170,21 +1375,21 @@ { case GRXSTS_PKTSTS_IN: /* Read the data into the host buffer. */ - if ((pktcnt > 0) && (hhcd->hc[channelnum].xfer_buff != (void *)0)) + if ((pktcnt > 0) && (hhcd_USB_FS.hc[channelnum].xfer_buff != (void *)0)) { - USB_ReadPacket(hhcd->Instance, hhcd->hc[channelnum].xfer_buff, pktcnt); + USB_ReadPacket(USB_OTG_FS, hhcd_USB_FS.hc[channelnum].xfer_buff, pktcnt); /*manage multiple Xfer */ - hhcd->hc[channelnum].xfer_buff += pktcnt; - hhcd->hc[channelnum].xfer_count += pktcnt; + hhcd_USB_FS.hc[channelnum].xfer_buff += pktcnt; + hhcd_USB_FS.hc[channelnum].xfer_count += pktcnt; if((USBx_HC(channelnum)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) > 0) { /* re-activate the channel when more packets are expected */ USBx_HC(channelnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS; USBx_HC(channelnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA; - hhcd->hc[channelnum].toggle_in ^= 1; + hhcd_USB_FS.hc[channelnum].toggle_in ^= 1; } } break; @@ -1198,20 +1403,495 @@ } // - USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL); + USB_UNMASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_RXFLVL); } } } +void USBHALHost::usbisr_HS(void) +{ + USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS; + uint32_t i = 0 , interrupt = 0; + + /* ensure that we are in device mode */ + if (USB_GetMode(USB_OTG_HS) == USB_OTG_MODE_HOST) + { + /* avoid spurious interrupt */ + if(__HAL_HCD_IS_INVALID_INTERRUPT(&hhcd_USB_HS)) + { + return; + } + + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT)) + { + /* incorrect mode, acknowledge the interrupt */ + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT); + } + + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_IISOIXFR)) + { + /* incorrect mode, acknowledge the interrupt */ + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_IISOIXFR); + } + + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PTXFE)) + { + /* incorrect mode, acknowledge the interrupt */ + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PTXFE); + } + + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_MMIS)) + { + /* incorrect mode, acknowledge the interrupt */ + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_MMIS); + } + + /* Handle Host Disconnect Interrupts */ + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_DISCINT)) + { + + /* Cleanup HPRT */ + USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\ + USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG ); + + /* Handle Host Port Interrupts */ + HAL_HCD_Disconnect_Callback(&hhcd_USB_HS); + USB_InitFSLSPClkSel(USB_OTG_HS ,HCFG_48_MHZ ); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_DISCINT); + } + + /* Handle Host Port Interrupts */ + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_HPRTINT)) + { + // + USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS; + __IO uint32_t hprt0, hprt0_dup; + + /* Handle Host Port Interrupts */ + hprt0 = USBx_HPRT0; + hprt0_dup = USBx_HPRT0; + + hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\ + USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG ); + + /* Check wether Port Connect Detected */ + if((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET) + { + if((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS) + { + USB_MASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_DISCINT); + HAL_HCD_Connect_Callback(&hhcd_USB_HS); + } + hprt0_dup |= USB_OTG_HPRT_PCDET; + + } + + /* Check whether Port Enable Changed */ + if((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG) + { + hprt0_dup |= USB_OTG_HPRT_PENCHNG; + + if((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA) + { + if(hhcd_USB_HS.Init.phy_itface == USB_OTG_EMBEDDED_PHY) + { + if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17)) + { + USB_InitFSLSPClkSel(USB_OTG_HS ,HCFG_6_MHZ ); + } + else + { + USB_InitFSLSPClkSel(USB_OTG_HS ,HCFG_48_MHZ ); + } + } + else + { + if(hhcd_USB_HS.Init.speed == HCD_SPEED_FULL) + { + USBx_HOST->HFIR = (uint32_t)60000; + } + } + HAL_HCD_Connect_Callback(&hhcd_USB_HS); + + if(hhcd_USB_HS.Init.speed == HCD_SPEED_HIGH) + { + USB_UNMASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_DISCINT); + } + } + else + { + /* Cleanup HPRT */ + USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\ + USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG ); + + USB_UNMASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_DISCINT); + } + } + + /* Check For an overcurrent */ + if((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG) + { + hprt0_dup |= USB_OTG_HPRT_POCCHNG; + } + + /* Clear Port Interrupts */ + USBx_HPRT0 = hprt0_dup; + } + + + // + + + /* Handle Host SOF Interrupts */ + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_SOF)) + { + HAL_HCD_SOF_Callback(&hhcd_USB_HS); + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_SOF); + } + + /* Handle Host channel Interrupts */ + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_HCINT)) + { + + interrupt = USB_HC_ReadInterrupt(USB_OTG_HS); + for (i = 0; i < hhcd_USB_HS.Init.Host_channels ; i++) + { + if (interrupt & (1 << i)) + { + if ((USBx_HC(i)->HCCHAR) & USB_OTG_HCCHAR_EPDIR) + { + // + USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS; + uint8_t chnum = i; + if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR) + { + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR); + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + } + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK) + { + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK); + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + hhcd_USB_HS.hc[chnum].state = HC_STALL; + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL); + USB_HC_Halt(USB_OTG_HS, chnum); + } + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); + hhcd_USB_HS.hc[chnum].state = HC_DATATGLERR; + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR); + } + + if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR); + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC) + { + + if (hhcd_USB_HS.Init.dma_enable) + { + hhcd_USB_HS.hc[chnum].xfer_count = hhcd_USB_HS.hc[chnum].xfer_len - \ + (USBx_HC(chnum)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ); + } + + hhcd_USB_HS.hc[chnum].state = HC_XFRC; + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC); -uint8_t HC::slot = 0x00; + if ((hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_CTRL)|| + (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_BULK)) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); + + } + else if(hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_INTR) + { + USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM; + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); + } + else if(hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_ISOC) + { + USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM; + hhcd_USB_HS.hc[chnum].urb_state = URB_DONE; + HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_HS, chnum, hhcd_USB_HS.hc[chnum].urb_state); + } + hhcd_USB_HS.hc[chnum].toggle_in ^= 1; + + } + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH) + { + __HAL_HCD_MASK_HALT_HC_INT(chnum); + + if(hhcd_USB_HS.hc[chnum].state == HC_XFRC) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_DONE; + } + + else if (hhcd_USB_HS.hc[chnum].state == HC_NAK) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_DONE; + } + + else if (hhcd_USB_HS.hc[chnum].state == HC_STALL) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_STALL; + } + + else if (hhcd_USB_HS.hc[chnum].state == HC_XACTERR) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY; + } + + else if (hhcd_USB_HS.hc[chnum].state == HC_DATATGLERR) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_ERROR; + } + + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH); + HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_HS, chnum, hhcd_USB_HS.hc[chnum].urb_state); + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + hhcd_USB_HS.hc[chnum].state = HC_XACTERR; + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR); + } + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK) + { + if(hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_INTR) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + } + else if (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_CTRL) + { + /* re-activate the channel */ + USBx_HC(chnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS; + USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA; + + } + + else if (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_BULK) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + } + + hhcd_USB_HS.hc[chnum].state = HC_NAK; + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); + } + // + } + else + { + // + uint8_t chnum=i; + USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS; + + if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR) + { + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR); + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + } + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK) + { + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK); + + if( hhcd_USB_HS.hc[chnum].do_ping == 1) + { + hhcd_USB_HS.hc[chnum].state = HC_NYET; + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY; + } + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NYET) + { + hhcd_USB_HS.hc[chnum].state = HC_NYET; + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NYET); + + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR); + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC); + hhcd_USB_HS.hc[chnum].state = HC_XFRC; + + } -HC::HC(HCD_HandleTypeDef* hhcd_USB) + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL) + { + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL); + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + hhcd_USB_HS.hc[chnum].state = HC_STALL; + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + hhcd_USB_HS.hc[chnum].state = HC_NAK; + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + hhcd_USB_HS.hc[chnum].state = HC_XACTERR; + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR); + } + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR) + { + __HAL_HCD_UNMASK_HALT_HC_INT(chnum); + USB_HC_Halt(USB_OTG_HS, chnum); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK); + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR); + hhcd_USB_HS.hc[chnum].state = HC_DATATGLERR; + } + + + else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH) + { + __HAL_HCD_MASK_HALT_HC_INT(chnum); + + if(hhcd_USB_HS.hc[chnum].state == HC_XFRC) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_DONE; + if (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_BULK) + { + hhcd_USB_HS.hc[chnum].toggle_out ^= 1; + } + } + else if (hhcd_USB_HS.hc[chnum].state == HC_NAK) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY; + } + + else if (hhcd_USB_HS.hc[chnum].state == HC_NYET) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY; + hhcd_USB_HS.hc[chnum].do_ping = 0; + } + + else if (hhcd_USB_HS.hc[chnum].state == HC_STALL) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_STALL; + } + + else if(hhcd_USB_HS.hc[chnum].state == HC_XACTERR) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY; + } + + else if (hhcd_USB_HS.hc[chnum].state == HC_DATATGLERR) + { + hhcd_USB_HS.hc[chnum].urb_state = URB_ERROR; + } + + __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH); + HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_HS, chnum, hhcd_USB_HS.hc[chnum].urb_state); + } + // + } + } + } + + __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_HCINT); + } + + /* Handle Rx Queue Level Interrupts */ + if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_RXFLVL)) + { + USB_MASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_RXFLVL); + +// + USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS; + uint8_t channelnum =0; + uint32_t pktsts; + uint32_t pktcnt; + uint32_t temp = 0; + + temp = USB_OTG_HS->GRXSTSP ; + channelnum = temp & USB_OTG_GRXSTSP_EPNUM; + pktsts = (temp & USB_OTG_GRXSTSP_PKTSTS) >> 17; + pktcnt = (temp & USB_OTG_GRXSTSP_BCNT) >> 4; + + switch (pktsts) + { + case GRXSTS_PKTSTS_IN: + /* Read the data into the host buffer. */ + if ((pktcnt > 0) && (hhcd_USB_HS.hc[channelnum].xfer_buff != (void *)0)) + { + + USB_ReadPacket(USB_OTG_HS, hhcd_USB_HS.hc[channelnum].xfer_buff, pktcnt); + + /*manage multiple Xfer */ + hhcd_USB_HS.hc[channelnum].xfer_buff += pktcnt; + hhcd_USB_HS.hc[channelnum].xfer_count += pktcnt; + + if((USBx_HC(channelnum)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) > 0) + { + /* re-activate the channel when more packets are expected */ + USBx_HC(channelnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS; + USBx_HC(channelnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA; + hhcd_USB_HS.hc[channelnum].toggle_in ^= 1; + } + } + break; + + case GRXSTS_PKTSTS_DATA_TOGGLE_ERR: + break; + case GRXSTS_PKTSTS_IN_XFER_COMP: + case GRXSTS_PKTSTS_CH_HALTED: + default: + break; + } +// + + USB_UNMASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_RXFLVL); + } + } +} + +uint8_t HC_FS::slot = 0x00; + +HC_FS::HC_FS() { static const int start = 1; uint8_t mask = (1<<start); - hc_USB=hhcd_USB; for(int i = start; i < 8; i++, mask <<= 1) { if (!(slot & mask)) @@ -1224,44 +1904,31 @@ _ch = 0; // ERROR!!! } -HC::HC(HCD_HandleTypeDef* hhcd_USB,int ch) +HC_FS::HC_FS(int ch) { - hc_USB=hhcd_USB; _ch = ch; slot |= (1<<_ch); } -HC::~HC() +HC_FS::~HC_FS() { slot &= ~(1<<_ch); } -HAL_StatusTypeDef HC::Init(int IF_N, uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps) +HAL_StatusTypeDef HC_FS::Init(uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps) { _ep_addr = epnum; _ep_type = ep_type; - if(IF_N!=USB_FastSpeed) - { - while(HAL_HCD_GetState(hc_USB)!=HAL_HCD_STATE_READY); - } - return HAL_HCD_HC_Init(hc_USB, _ch, + return HAL_HCD_HC_Init(&hhcd_USB_FS, _ch, epnum, dev_address, speed, ep_type, mps); } -HAL_StatusTypeDef HC::SubmitRequest(int IF_N,uint8_t* pbuff, uint16_t length, bool setup) +HAL_StatusTypeDef HC_FS::SubmitRequest(uint8_t* pbuff, uint16_t length, bool setup) { - uint32_t tick; - tick=HAL_GetTick(); - while(HAL_GetTick()==tick); - if(IF_N!=USB_FastSpeed) - { - while((hc_USB->Instance->GRXSTSR&0x1E0000)==0x40000); - while(((hc_USB->Instance->HNPTXSTS& 0xFFFF)*((hc_USB->Instance->HNPTXSTS& 0x000F0000)>>16))<length); - } uint8_t direction = (_ep_addr & 0x80) ? DIR_IN : DIR_OUT; if (_ep_type == EP_TYPE_CTRL) { - HCD_HCTypeDef* hc = &hc_USB->hc[_ch]; + HCD_HCTypeDef* hc = &hhcd_USB_FS.hc[_ch]; if (setup) { hc->data_pid = HC_PID_SETUP; @@ -1298,48 +1965,156 @@ hc->xfer_count = 0; hc->ch_num = _ch; hc->state = HC_IDLE; - if(IF_N==USB_FastSpeed) - { - return USB_HC_StartXfer(hc_USB->Instance, hc, 0); - } - else - { - return USB_HC_StartXfer(hc_USB->Instance, hc, 1); - } + + return USB_HC_StartXfer(hhcd_USB_FS.Instance, hc, 0); } - return HAL_HCD_HC_SubmitRequest(hc_USB, _ch, + return HAL_HCD_HC_SubmitRequest(&hhcd_USB_FS, _ch, direction, _ep_type, 0, pbuff, length, 0); } -HCD_URBStateTypeDef HC::GetURBState() +HCD_URBStateTypeDef HC_FS::GetURBState() +{ + return HAL_HCD_HC_GetURBState(&hhcd_USB_FS, _ch); +} + +HCD_HCStateTypeDef HC_FS::GetState() +{ + return HAL_HCD_HC_GetState(&hhcd_USB_FS, _ch); +} + +uint32_t HC_FS::GetXferCount() { - return HAL_HCD_HC_GetURBState(hc_USB, _ch); + return HAL_HCD_HC_GetXferCount(&hhcd_USB_FS, _ch); +} + +void HC_FS::SetToggle(uint8_t toggle) +{ + if (_ep_addr & 0x80) // IN + { + hhcd_USB_FS.hc[_ch].toggle_in = toggle; + } + else // OUT + { + hhcd_USB_FS.hc[_ch].toggle_out = toggle; + } } +uint8_t HC_HS::slot = 0x00; -HCD_HCStateTypeDef HC::GetState() +HC_HS::HC_HS() { - return HAL_HCD_HC_GetState(hc_USB, _ch); + static const int start = 1; + uint8_t mask = (1<<start); + for(int i = start; i < 8; i++, mask <<= 1) + { + if (!(slot & mask)) + { + slot |= mask; + _ch = i; + return; + } + } + _ch = 0; // ERROR!!! +} + +HC_HS::HC_HS(int ch) +{ + _ch = ch; + slot |= (1<<_ch); +} + +HC_HS::~HC_HS() +{ + slot &= ~(1<<_ch); +} + +HAL_StatusTypeDef HC_HS::Init(uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps) +{ + _ep_addr = epnum; + _ep_type = ep_type; + return HAL_HCD_HC_Init(&hhcd_USB_HS, _ch, + epnum, dev_address, speed, ep_type, mps); } -uint32_t HC::GetXferCount() +HAL_StatusTypeDef HC_HS::SubmitRequest(uint8_t* pbuff, uint16_t length, bool setup) { - return HAL_HCD_HC_GetXferCount(hc_USB, _ch); + uint8_t direction = (_ep_addr & 0x80) ? DIR_IN : DIR_OUT; + if (_ep_type == EP_TYPE_CTRL) + { + HCD_HCTypeDef* hc = &hhcd_USB_HS.hc[_ch]; + if (setup) + { + hc->data_pid = HC_PID_SETUP; + hc->toggle_out = 0; + } + else + { + if (direction == DIR_IN) + { + if (hc->toggle_in == 0) + { + hc->data_pid = HC_PID_DATA0; + } + else + { + hc->data_pid = HC_PID_DATA1; + } + } + else // OUT + { + if (hc->toggle_out == 0) + { + hc->data_pid = HC_PID_DATA0; + } + else + { + hc->data_pid = HC_PID_DATA1; + } + } + } + hc->xfer_buff = pbuff; + hc->xfer_len = length; + hc->urb_state = URB_IDLE; + hc->xfer_count = 0; + hc->ch_num = _ch; + hc->state = HC_IDLE; + + return USB_HC_StartXfer(hhcd_USB_HS.Instance, hc, 0); + } + return HAL_HCD_HC_SubmitRequest(&hhcd_USB_HS, _ch, + direction, _ep_type, 0, pbuff, length, 0); } -void HC::SetToggle(uint8_t toggle) +HCD_URBStateTypeDef HC_HS::GetURBState() +{ + return HAL_HCD_HC_GetURBState(&hhcd_USB_HS, _ch); +} + +HCD_HCStateTypeDef HC_HS::GetState() +{ + return HAL_HCD_HC_GetState(&hhcd_USB_HS, _ch); +} + +uint32_t HC_HS::GetXferCount() +{ + return HAL_HCD_HC_GetXferCount(&hhcd_USB_HS, _ch); +} + +void HC_HS::SetToggle(uint8_t toggle) { if (_ep_addr & 0x80) // IN { - hc_USB->hc[_ch].toggle_in = toggle; + hhcd_USB_HS.hc[_ch].toggle_in = toggle; } else // OUT { - hc_USB->hc[_ch].toggle_out = toggle; + hhcd_USB_HS.hc[_ch].toggle_out = toggle; } } + #endif +
--- a/USBHost/TARGET_STM32F7/USBHALHost_F746NG.h Mon Jun 13 17:21:07 2016 +0000 +++ b/USBHost/TARGET_STM32F7/USBHALHost_F746NG.h Fri Jun 17 09:00:35 2016 +0000 @@ -1,4 +1,3 @@ - #pragma once #include "mbed.h" #include "USBHostTypes.h" @@ -6,29 +5,53 @@ #define USB_FastSpeed 0 #define USB_HighSpeed 1 -class HC { +class HC_FS { static const uint8_t DIR_IN = 1; static const uint8_t DIR_OUT = 0; public: - HC(HCD_HandleTypeDef* hhcd_USB); - HC(HCD_HandleTypeDef* hhcd_USB,int ch); - ~HC(); - HAL_StatusTypeDef Init(int IF_N,uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps); - HAL_StatusTypeDef SubmitRequest(int IF_N,uint8_t* pbuff, uint16_t length, bool setup = false); + HC_FS(); + HC_FS(int ch); + ~HC_FS(); + HAL_StatusTypeDef Init(uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps); + HAL_StatusTypeDef SubmitRequest(uint8_t* pbuff, uint16_t length, bool setup = false); HCD_URBStateTypeDef GetURBState(); HCD_HCStateTypeDef GetState(); uint32_t GetXferCount(); void SetToggle(uint8_t toggle); static uint8_t slot; - HCD_HandleTypeDef* hc_USB; + private: int _ch; uint8_t _ep_addr; uint8_t _ep_type; }; +class HC_HS { + static const uint8_t DIR_IN = 1; + static const uint8_t DIR_OUT = 0; + +public: + HC_HS(); + HC_HS(int ch); + ~HC_HS(); + HAL_StatusTypeDef Init(uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps); + HAL_StatusTypeDef SubmitRequest(uint8_t* pbuff, uint16_t length, bool setup = false); + HCD_URBStateTypeDef GetURBState(); + HCD_HCStateTypeDef GetState(); + uint32_t GetXferCount(); + void SetToggle(uint8_t toggle); + + static uint8_t slot; + +private: + int _ch; + uint8_t _ep_addr; + uint8_t _ep_type; +}; + + class USBHALHost { public: uint8_t LastStatus; @@ -63,8 +86,9 @@ bool wait_attach(); static USBHALHost * instHost; HCD_HandleTypeDef* hhcd; - void usbisr(void); - static void _usbisr(void); + void usbisr_FS(void); + static void _usbisr_FS(void); + void usbisr_HS(void); + static void _usbisr_HS(void); }; -
--- a/USBHost/USBHost.cpp Mon Jun 13 17:21:07 2016 +0000 +++ b/USBHost/USBHost.cpp Fri Jun 17 09:00:35 2016 +0000 @@ -21,24 +21,36 @@ void usb_test_assert_internal(const char *expr, const char *file, int line); #define USB_TEST_ASSERT(EXPR) while(!(EXPR)){usb_test_assert_internal(#EXPR,__FILE__,__LINE__);} -USBHost* USBHost::inst = NULL; +USBHost* USBHost::inst_0 = NULL; +USBHost* USBHost::inst_1 = NULL; USBHost* USBHost::getHostInst(int IF_Number) { - if (inst == NULL) { - inst = new USBHost(IF_Number); - inst->init(); + if (IF_Number==0) + { + if (inst_0 == NULL) { + inst_0 = new USBHost(IF_Number); + inst_0->init(); + } + return inst_0; } - if(inst->IF_N!=IF_Number) { - inst = new USBHost(IF_Number); - inst->init(); + else + { + if (inst_1 == NULL) { + inst_1 = new USBHost(IF_Number); + inst_1->init(); } - return inst; + return inst_1; + } + } void USBHost::poll() { - if (inst) { - inst->task(); + if (inst_0) { + inst_0->task(); + } + if (inst_1) { + inst_1->task(); } } @@ -202,7 +214,7 @@ USB_TYPE USBHost::controlRead(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { USBEndpoint* ep = dev->getEpCtl(); SETUP_PACKET setup(requestType, request, value, index, len); - + wait_ms(100); int result = token_setup(ep, &setup, len); // setup stage USB_TRACE1(result); if (result < 0) {
--- a/USBHost/USBHost.h Mon Jun 13 17:21:07 2016 +0000 +++ b/USBHost/USBHost.h Fri Jun 17 09:00:35 2016 +0000 @@ -173,7 +173,8 @@ private: USBHost(int InterfaceNumber); - static USBHost* inst; + static USBHost* inst_0; + static USBHost* inst_1; virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed); void root_enumeration(USBDeviceConnected* dev); void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator);
--- a/USBHostMSD/USBHostMSD.cpp Mon Jun 13 17:21:07 2016 +0000 +++ b/USBHostMSD/USBHostMSD.cpp Fri Jun 17 09:00:35 2016 +0000 @@ -23,13 +23,14 @@ #define CBW_SIGNATURE 0x43425355 #define CSW_SIGNATURE 0x53425355 + #define DEVICE_TO_HOST 0x80 #define HOST_TO_DEVICE 0x00 #define GET_MAX_LUN (0xFE) #define BO_MASS_STORAGE_RESET (0xFF) -USBHostMSD::USBHostMSD(int Interface,const char * rootdir) : FATFileSystem(rootdir) +USBHostMSD::USBHostMSD(const char * rootdir,int Interface) : FATFileSystem(rootdir) { host = USBHost::getHostInst(Interface); init(); @@ -65,19 +66,19 @@ for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { if ((dev = host->getDevice(i)) != NULL) { - + USB_DBG("Trying to connect MSD device\r\n"); - + if(host->enumerate(dev, this)) break; if (msd_device_found) { bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); - + if (!bulk_in || !bulk_out) continue; - + USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); dev->setName("MSD", msd_intf); host->registerDriver(dev, msd_intf, this, &USBHostMSD::init); @@ -122,9 +123,74 @@ } -int USBHostMSD::testUnitReady() { +int USBHostMSD::testUnitReady(int i) { USB_DBG("Test unit ready"); - return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); + uint8_t cmd[6] = {0,0,0,0,0,0}; + //SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) + //return SCSITransfer(cmd, 6, DEVICE_TO_HOST, 0, 0); + int res = 0; + + cbw.Signature = CBW_SIGNATURE; + cbw.Tag = 0; + cbw.DataLength = 0; + cbw.Flags = DEVICE_TO_HOST; + cbw.LUN = 0; + cbw.CBLength = 6; + memset(cbw.CB,0,sizeof(cbw.CB)); + if (cmd) { + memcpy(cbw.CB,cmd,6); + } + + // send the cbw + USB_DBG("Send CBW"); + res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); + if (checkResult(res, bulk_out)) + return -1; + + wait_ms(6000*i); + + // status stage + csw.Signature = 0; + USB_DBG("Read CSW"); + res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); + if (checkResult(res, bulk_in)) + return -1; + + if (csw.Signature != CSW_SIGNATURE) { + return -1; + } + + USB_DBG("recv csw: status: %d", csw.Status); + + // ModeSense? + if ((csw.Status == 1) && (cmd[0] != 0x03)) { + USB_DBG("request mode sense"); + return SCSIRequestSense(); + } + + // perform reset recovery + if ((csw.Status == 2) && (cmd[0] != 0x03)) { + + // send Bulk-Only Mass Storage Reset request + res = host->controlWrite( dev, + USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, + BO_MASS_STORAGE_RESET, + 0, msd_intf, NULL, 0); + + // unstall both endpoints + res = host->controlWrite( dev, + USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, + CLEAR_FEATURE, + 0, bulk_in->getAddress(), NULL, 0); + + res = host->controlWrite( dev, + USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, + CLEAR_FEATURE, + 0, bulk_out->getAddress(), NULL, 0); + + } + + return csw.Status; } @@ -219,7 +285,7 @@ if (data) { USB_DBG("data stage"); if (flags == HOST_TO_DEVICE) { - + res = host->bulkWrite(dev, bulk_out, data, transfer_len); if (checkResult(res, bulk_out)) return -1; @@ -242,7 +308,7 @@ if (csw.Signature != CSW_SIGNATURE) { return -1; } - + USB_DBG("recv csw: status: %d", csw.Status); // ModeSense? @@ -250,34 +316,34 @@ USB_DBG("request mode sense"); return SCSIRequestSense(); } - + // perform reset recovery if ((csw.Status == 2) && (cmd[0] != 0x03)) { - + // send Bulk-Only Mass Storage Reset request res = host->controlWrite( dev, USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, BO_MASS_STORAGE_RESET, 0, msd_intf, NULL, 0); - + // unstall both endpoints res = host->controlWrite( dev, USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, CLEAR_FEATURE, 0, bulk_in->getAddress(), NULL, 0); - + res = host->controlWrite( dev, USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, CLEAR_FEATURE, 0, bulk_out->getAddress(), NULL, 0); - + } return csw.Status; } -int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { +int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint32_t nbBlock, int direction) { uint8_t cmd[10]; memset(cmd,0,10); cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; @@ -304,20 +370,29 @@ int USBHostMSD::disk_initialize() { USB_DBG("FILESYSTEM: init"); /* U16 */int i, timeout = 10; - + getMaxLun(); - + for (i = 0; i < timeout; i++) { wait_ms(100); //Thread::wait(100); - if (!testUnitReady()) + if (!testUnitReady(i+1)) break; + printf("TestUnitReady %d\r\n",i); + if(i>1) + { + host->controlWrite( dev, + USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, + BO_MASS_STORAGE_RESET, + 0, msd_intf, NULL, 0); + + }; } - + if (i == timeout) { disk_init = false; return -1; } - + inquiry(0, 0); disk_init = 1; return readCapacity(); @@ -330,7 +405,9 @@ } if (!disk_init) return -1; - return dataTransfer((uint8_t *)buffer, sector, count, HOST_TO_DEVICE); + if (count>0x800000) + return -1; + return dataTransfer((uint8_t *)buffer, sector,(uint32_t) count, HOST_TO_DEVICE); } int USBHostMSD::disk_read(uint8_t * buffer, uint64_t sector, uint8_t count) { @@ -340,9 +417,12 @@ } if (!disk_init) return -1; - return dataTransfer((uint8_t *)buffer, sector, count, DEVICE_TO_HOST); + if (count>0x800000) + return -1; + return dataTransfer((uint8_t *)buffer, sector,(uint32_t) count, DEVICE_TO_HOST); } + uint64_t USBHostMSD::disk_sectors() { USB_DBG("FILESYSTEM: sectors"); if (!disk_init) {
--- a/USBHostMSD/USBHostMSD.h Mon Jun 13 17:21:07 2016 +0000 +++ b/USBHostMSD/USBHostMSD.h Fri Jun 17 09:00:35 2016 +0000 @@ -24,7 +24,7 @@ #include "USBHost.h" #include "FATFileSystem.h" -/** +/** * A class to communicate a USB flash disk */ class USBHostMSD : public IUSBEnumerator, public FATFileSystem { @@ -34,7 +34,7 @@ * * @param rootdir mount name */ - USBHostMSD(int Interface,const char * rootdir); + USBHostMSD(const char * rootdir,int Interface); /** * Check if a MSD device is connected @@ -95,11 +95,11 @@ CSW csw; int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len); - int testUnitReady(); + int testUnitReady(int i); int readCapacity(); int inquiry(uint8_t lun, uint8_t page_code); int SCSIRequestSense(); - int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction); + int dataTransfer(uint8_t * buf, uint32_t block, uint32_t nbBlock, int direction); int checkResult(uint8_t res, USBEndpoint * ep); int getMaxLun(); @@ -109,7 +109,7 @@ int msd_intf; bool msd_device_found; bool disk_init; - + void init(); };