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 Norimasa Okamoto

Committer:
DieterGraef
Date:
Fri Jun 17 09:00:35 2016 +0000
Revision:
25:7d6d9fc471bf
Parent:
24:5396b6a93262
USB Host now works with both Interfaces even in parallel. Some changes in the USB MSD driver to make it operable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 24:5396b6a93262 1 #if defined(TARGET_DISCO_F746NG)
DieterGraef 24:5396b6a93262 2 #include "USBHALHost.h"
DieterGraef 24:5396b6a93262 3 #include <algorithm>
DieterGraef 25:7d6d9fc471bf 4 #include "mydebug.h"
DieterGraef 25:7d6d9fc471bf 5
DieterGraef 25:7d6d9fc471bf 6
DieterGraef 24:5396b6a93262 7 #ifdef _USB_DBG
DieterGraef 24:5396b6a93262 8 #define USB_DBG(...) do{printf("[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);printf(__VA_ARGS__);printf("\n");} while(0);
DieterGraef 24:5396b6a93262 9 #define USB_DBG_HEX(A,B) debug_hex<RawSerial>(pc,A,B)
DieterGraef 24:5396b6a93262 10
DieterGraef 24:5396b6a93262 11 #else
DieterGraef 24:5396b6a93262 12 #define USB_DBG(...) while(0)
DieterGraef 24:5396b6a93262 13 #define USB_DBG_HEX(A,B) while(0)
DieterGraef 24:5396b6a93262 14 #endif
DieterGraef 24:5396b6a93262 15
DieterGraef 24:5396b6a93262 16 #undef USB_TEST_ASSERT
DieterGraef 24:5396b6a93262 17 void usb_test_assert_internal(const char *expr, const char *file, int line);
DieterGraef 24:5396b6a93262 18 #define USB_TEST_ASSERT(EXPR) while(!(EXPR)){usb_test_assert_internal(#EXPR,__FILE__,__LINE__);}
DieterGraef 24:5396b6a93262 19
DieterGraef 24:5396b6a93262 20 #define USB_TRACE1(A) while(0)
DieterGraef 24:5396b6a93262 21
DieterGraef 24:5396b6a93262 22 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");}while(0);
DieterGraef 24:5396b6a93262 23
DieterGraef 24:5396b6a93262 24 __IO bool attach_done = false;
DieterGraef 24:5396b6a93262 25
DieterGraef 24:5396b6a93262 26 void delay_ms(uint32_t t)
DieterGraef 24:5396b6a93262 27 {
DieterGraef 24:5396b6a93262 28 wait_ms(t);
DieterGraef 24:5396b6a93262 29 }
DieterGraef 24:5396b6a93262 30
DieterGraef 24:5396b6a93262 31 // usbh_conf.c
DieterGraef 24:5396b6a93262 32
DieterGraef 24:5396b6a93262 33 //__attribute__((section(".dmatransfer")));
DieterGraef 25:7d6d9fc471bf 34 HCD_HandleTypeDef hhcd_USB_FS;
DieterGraef 25:7d6d9fc471bf 35 HCD_HandleTypeDef hhcd_USB_HS;
DieterGraef 25:7d6d9fc471bf 36 HCD_URBStateTypeDef URB_FS;
DieterGraef 25:7d6d9fc471bf 37 HCD_URBStateTypeDef URB_HS;
DieterGraef 24:5396b6a93262 38
DieterGraef 24:5396b6a93262 39 void HAL_HCD_MspInit(HCD_HandleTypeDef* hhcd)
DieterGraef 24:5396b6a93262 40 {
DieterGraef 24:5396b6a93262 41 GPIO_InitTypeDef GPIO_InitStruct;
DieterGraef 24:5396b6a93262 42 if(hhcd->Instance==USB_OTG_FS)
DieterGraef 24:5396b6a93262 43 {
DieterGraef 24:5396b6a93262 44 /* Configure USB FS GPIOs */
DieterGraef 24:5396b6a93262 45 __HAL_RCC_GPIOA_CLK_ENABLE();
DieterGraef 24:5396b6a93262 46 __HAL_RCC_GPIOD_CLK_ENABLE();
DieterGraef 24:5396b6a93262 47 GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
DieterGraef 24:5396b6a93262 48 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
DieterGraef 24:5396b6a93262 49 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 50 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
DieterGraef 24:5396b6a93262 51 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
DieterGraef 24:5396b6a93262 52 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 53
DieterGraef 24:5396b6a93262 54 GPIO_InitStruct.Pin = GPIO_PIN_10;
DieterGraef 24:5396b6a93262 55 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
DieterGraef 24:5396b6a93262 56 GPIO_InitStruct.Pull = GPIO_PULLUP;
DieterGraef 24:5396b6a93262 57 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
DieterGraef 24:5396b6a93262 58 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 59
DieterGraef 24:5396b6a93262 60 /* Configure POWER_SWITCH IO pin */
DieterGraef 24:5396b6a93262 61 GPIO_InitStruct.Pin = GPIO_PIN_5;
DieterGraef 24:5396b6a93262 62 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
DieterGraef 24:5396b6a93262 63 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 64 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 65
DieterGraef 24:5396b6a93262 66 /* Enable USB FS Clocks */
DieterGraef 24:5396b6a93262 67 __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
DieterGraef 24:5396b6a93262 68 }
DieterGraef 24:5396b6a93262 69 if(hhcd->Instance==USB_OTG_HS)
DieterGraef 24:5396b6a93262 70 {
DieterGraef 24:5396b6a93262 71 /* Peripheral clock enable */
DieterGraef 24:5396b6a93262 72 __HAL_RCC_GPIOA_CLK_ENABLE();
DieterGraef 24:5396b6a93262 73 __HAL_RCC_GPIOB_CLK_ENABLE();
DieterGraef 24:5396b6a93262 74 __HAL_RCC_GPIOC_CLK_ENABLE();
DieterGraef 24:5396b6a93262 75 __HAL_RCC_GPIOH_CLK_ENABLE();
DieterGraef 24:5396b6a93262 76 /* CLK */
DieterGraef 24:5396b6a93262 77 GPIO_InitStruct.Pin = GPIO_PIN_5;
DieterGraef 24:5396b6a93262 78 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
DieterGraef 24:5396b6a93262 79 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 80 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
DieterGraef 24:5396b6a93262 81 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
DieterGraef 24:5396b6a93262 82 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 83
DieterGraef 24:5396b6a93262 84 /* D0 */
DieterGraef 24:5396b6a93262 85 GPIO_InitStruct.Pin = GPIO_PIN_3;
DieterGraef 24:5396b6a93262 86 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
DieterGraef 24:5396b6a93262 87 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 88 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
DieterGraef 24:5396b6a93262 89 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
DieterGraef 24:5396b6a93262 90 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 91
DieterGraef 24:5396b6a93262 92 /* D1 D2 D3 D4 D5 D6 D7 */
DieterGraef 24:5396b6a93262 93 GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
DieterGraef 24:5396b6a93262 94 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
DieterGraef 24:5396b6a93262 95 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 96 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
DieterGraef 24:5396b6a93262 97 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 98
DieterGraef 24:5396b6a93262 99 /* STP */
DieterGraef 24:5396b6a93262 100 GPIO_InitStruct.Pin = GPIO_PIN_0;
DieterGraef 24:5396b6a93262 101 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
DieterGraef 24:5396b6a93262 102 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 103 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
DieterGraef 24:5396b6a93262 104 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 105
DieterGraef 24:5396b6a93262 106 /* NXT */
DieterGraef 24:5396b6a93262 107 GPIO_InitStruct.Pin = GPIO_PIN_4;
DieterGraef 24:5396b6a93262 108 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
DieterGraef 24:5396b6a93262 109 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 110 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
DieterGraef 24:5396b6a93262 111 HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 112
DieterGraef 24:5396b6a93262 113 /* DIR */
DieterGraef 24:5396b6a93262 114 GPIO_InitStruct.Pin = GPIO_PIN_2;
DieterGraef 24:5396b6a93262 115 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
DieterGraef 24:5396b6a93262 116 GPIO_InitStruct.Pull = GPIO_NOPULL;
DieterGraef 24:5396b6a93262 117 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
DieterGraef 24:5396b6a93262 118 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
DieterGraef 24:5396b6a93262 119
DieterGraef 24:5396b6a93262 120 __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
DieterGraef 24:5396b6a93262 121
DieterGraef 24:5396b6a93262 122 /* Enable USB HS Clocks */
DieterGraef 24:5396b6a93262 123 __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
DieterGraef 24:5396b6a93262 124 }
DieterGraef 24:5396b6a93262 125 }
DieterGraef 24:5396b6a93262 126
DieterGraef 24:5396b6a93262 127 // stm32f4xx_it.c
DieterGraef 24:5396b6a93262 128 extern "C" {
DieterGraef 24:5396b6a93262 129 void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
DieterGraef 24:5396b6a93262 130 {
DieterGraef 24:5396b6a93262 131 //USB_TRACE1(hhcd);
DieterGraef 24:5396b6a93262 132 attach_done = true;
DieterGraef 24:5396b6a93262 133 }
DieterGraef 24:5396b6a93262 134
DieterGraef 24:5396b6a93262 135 } // extern "C"
DieterGraef 24:5396b6a93262 136
DieterGraef 24:5396b6a93262 137 USBHALHost* USBHALHost::instHost;
DieterGraef 24:5396b6a93262 138
DieterGraef 24:5396b6a93262 139 USBHALHost::USBHALHost(int InterfaceNumber)
DieterGraef 24:5396b6a93262 140 {
DieterGraef 24:5396b6a93262 141 IF_N=InterfaceNumber;
DieterGraef 24:5396b6a93262 142 instHost = this;
DieterGraef 24:5396b6a93262 143 }
DieterGraef 24:5396b6a93262 144
DieterGraef 24:5396b6a93262 145 void USBHALHost::init()
DieterGraef 24:5396b6a93262 146 {
DieterGraef 24:5396b6a93262 147 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 148 {
DieterGraef 25:7d6d9fc471bf 149 hhcd_USB_FS.Instance = USB_OTG_FS;
DieterGraef 25:7d6d9fc471bf 150 hhcd_USB_FS.Init.Host_channels = 11;
DieterGraef 25:7d6d9fc471bf 151 hhcd_USB_FS.Init.dma_enable = 0;
DieterGraef 25:7d6d9fc471bf 152 hhcd_USB_FS.Init.low_power_enable = 0;
DieterGraef 25:7d6d9fc471bf 153 hhcd_USB_FS.Init.phy_itface = HCD_PHY_EMBEDDED;
DieterGraef 25:7d6d9fc471bf 154 hhcd_USB_FS.Init.Sof_enable = 1;
DieterGraef 25:7d6d9fc471bf 155 hhcd_USB_FS.Init.speed = HCD_SPEED_FULL;
DieterGraef 25:7d6d9fc471bf 156 hhcd_USB_FS.Init.vbus_sensing_enable = 0;
DieterGraef 25:7d6d9fc471bf 157 hhcd_USB_FS.Init.lpm_enable = 0;
DieterGraef 24:5396b6a93262 158
DieterGraef 25:7d6d9fc471bf 159 HAL_HCD_Init(&hhcd_USB_FS);
DieterGraef 24:5396b6a93262 160 /* Peripheral interrupt init*/
DieterGraef 24:5396b6a93262 161 /* Sets the priority grouping field */
DieterGraef 25:7d6d9fc471bf 162 NVIC_SetVector(OTG_FS_IRQn, (uint32_t)&_usbisr_FS);
DieterGraef 24:5396b6a93262 163 HAL_NVIC_SetPriority(OTG_FS_IRQn, 6, 0);
DieterGraef 24:5396b6a93262 164 HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
DieterGraef 24:5396b6a93262 165
DieterGraef 25:7d6d9fc471bf 166 HAL_HCD_Start(&hhcd_USB_FS);
DieterGraef 24:5396b6a93262 167
DieterGraef 24:5396b6a93262 168 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_RESET);
DieterGraef 24:5396b6a93262 169
DieterGraef 24:5396b6a93262 170 bool lowSpeed = wait_attach();
DieterGraef 24:5396b6a93262 171 delay_ms(200);
DieterGraef 25:7d6d9fc471bf 172 HAL_HCD_ResetPort(&hhcd_USB_FS);
DieterGraef 24:5396b6a93262 173 delay_ms(100); // Wait for 100 ms after Reset
DieterGraef 24:5396b6a93262 174 addDevice(NULL, 0, lowSpeed);
DieterGraef 24:5396b6a93262 175 }
DieterGraef 24:5396b6a93262 176 if(IF_N==USB_HighSpeed)
DieterGraef 24:5396b6a93262 177 {
DieterGraef 25:7d6d9fc471bf 178 hhcd_USB_HS.Instance = USB_OTG_HS;
DieterGraef 25:7d6d9fc471bf 179 hhcd_USB_HS.Init.Host_channels = 8;
DieterGraef 25:7d6d9fc471bf 180 hhcd_USB_HS.Init.dma_enable = 0;
DieterGraef 25:7d6d9fc471bf 181 hhcd_USB_HS.Init.low_power_enable = 0;
DieterGraef 25:7d6d9fc471bf 182 hhcd_USB_HS.Init.phy_itface = HCD_PHY_ULPI;
DieterGraef 25:7d6d9fc471bf 183 hhcd_USB_HS.Init.Sof_enable = 1;
DieterGraef 25:7d6d9fc471bf 184 hhcd_USB_HS.Init.speed = HCD_SPEED_HIGH;
DieterGraef 25:7d6d9fc471bf 185 hhcd_USB_HS.Init.vbus_sensing_enable = 0;
DieterGraef 25:7d6d9fc471bf 186 hhcd_USB_HS.Init.use_external_vbus = 1;
DieterGraef 25:7d6d9fc471bf 187 hhcd_USB_HS.Init.lpm_enable = 0;
DieterGraef 24:5396b6a93262 188
DieterGraef 25:7d6d9fc471bf 189 HAL_HCD_Init(&hhcd_USB_HS);
DieterGraef 25:7d6d9fc471bf 190 NVIC_SetVector(OTG_HS_IRQn, (uint32_t)&_usbisr_HS);
DieterGraef 24:5396b6a93262 191 HAL_NVIC_SetPriority(OTG_HS_IRQn, 6, 0);
DieterGraef 24:5396b6a93262 192 HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
DieterGraef 24:5396b6a93262 193
DieterGraef 25:7d6d9fc471bf 194 HAL_HCD_Start(&hhcd_USB_HS);
DieterGraef 24:5396b6a93262 195
DieterGraef 24:5396b6a93262 196 bool lowSpeed = wait_attach();
DieterGraef 24:5396b6a93262 197 delay_ms(200);
DieterGraef 25:7d6d9fc471bf 198 HAL_HCD_ResetPort(&hhcd_USB_HS);
DieterGraef 24:5396b6a93262 199 delay_ms(100); // Wait for 100 ms after Reset
DieterGraef 24:5396b6a93262 200 addDevice(NULL, 0, lowSpeed);
DieterGraef 24:5396b6a93262 201 }
DieterGraef 24:5396b6a93262 202 }
DieterGraef 24:5396b6a93262 203
DieterGraef 24:5396b6a93262 204 bool USBHALHost::wait_attach()
DieterGraef 24:5396b6a93262 205 {
DieterGraef 24:5396b6a93262 206 Timer t;
DieterGraef 24:5396b6a93262 207 t.reset();
DieterGraef 24:5396b6a93262 208 t.start();
DieterGraef 24:5396b6a93262 209 while(!attach_done)
DieterGraef 24:5396b6a93262 210 {
DieterGraef 24:5396b6a93262 211 if (t.read_ms() > 5*1000)
DieterGraef 24:5396b6a93262 212 {
DieterGraef 24:5396b6a93262 213 t.reset();
DieterGraef 24:5396b6a93262 214 USB_INFO("Please attach USB device.");
DieterGraef 24:5396b6a93262 215 }
DieterGraef 24:5396b6a93262 216 }
DieterGraef 24:5396b6a93262 217 wait_ms(100);
DieterGraef 25:7d6d9fc471bf 218 if(IF_N==USB_FastSpeed)
DieterGraef 25:7d6d9fc471bf 219 {
DieterGraef 25:7d6d9fc471bf 220 return HAL_HCD_GetCurrentSpeed(&hhcd_USB_FS) == USB_OTG_SPEED_LOW;
DieterGraef 25:7d6d9fc471bf 221 }
DieterGraef 25:7d6d9fc471bf 222 else
DieterGraef 25:7d6d9fc471bf 223 {
DieterGraef 25:7d6d9fc471bf 224 return HAL_HCD_GetCurrentSpeed(&hhcd_USB_HS) == USB_OTG_SPEED_LOW;
DieterGraef 25:7d6d9fc471bf 225 }
DieterGraef 24:5396b6a93262 226 }
DieterGraef 24:5396b6a93262 227
DieterGraef 24:5396b6a93262 228
DieterGraef 24:5396b6a93262 229
DieterGraef 24:5396b6a93262 230 int USBHALHost::token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength)
DieterGraef 24:5396b6a93262 231 {
DieterGraef 24:5396b6a93262 232 const uint8_t ep_addr = 0x00;
DieterGraef 24:5396b6a93262 233 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 234 {
DieterGraef 25:7d6d9fc471bf 235 HC_FS hc;
DieterGraef 25:7d6d9fc471bf 236
DieterGraef 25:7d6d9fc471bf 237 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 238 hc.Init(ep_addr,
DieterGraef 24:5396b6a93262 239 dev->getAddress(),
DieterGraef 24:5396b6a93262 240 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
DieterGraef 24:5396b6a93262 241 EP_TYPE_CTRL, ep->getSize());
DieterGraef 25:7d6d9fc471bf 242 setup->wLength = wLength;
DieterGraef 25:7d6d9fc471bf 243
DieterGraef 25:7d6d9fc471bf 244 hc.SubmitRequest((uint8_t*)setup, 8, true); // PID_SETUP
DieterGraef 25:7d6d9fc471bf 245 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 246
DieterGraef 25:7d6d9fc471bf 247 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 248 {
DieterGraef 25:7d6d9fc471bf 249 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 250 LastStatus = ACK;
DieterGraef 25:7d6d9fc471bf 251 break;
DieterGraef 25:7d6d9fc471bf 252 default:
DieterGraef 25:7d6d9fc471bf 253 LastStatus = 0xff;
DieterGraef 25:7d6d9fc471bf 254 break;
DieterGraef 25:7d6d9fc471bf 255 }
DieterGraef 25:7d6d9fc471bf 256 ep->setData01(DATA1);
DieterGraef 25:7d6d9fc471bf 257 return 8;
DieterGraef 24:5396b6a93262 258 }
DieterGraef 24:5396b6a93262 259 else
DieterGraef 24:5396b6a93262 260 {
DieterGraef 25:7d6d9fc471bf 261 HC_HS hc;
DieterGraef 25:7d6d9fc471bf 262
DieterGraef 25:7d6d9fc471bf 263 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 264 hc.Init(ep_addr,
DieterGraef 24:5396b6a93262 265 dev->getAddress(),
DieterGraef 24:5396b6a93262 266 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH ,
DieterGraef 24:5396b6a93262 267 EP_TYPE_CTRL, ep->getSize());
DieterGraef 25:7d6d9fc471bf 268 setup->wLength = wLength;
DieterGraef 24:5396b6a93262 269
DieterGraef 25:7d6d9fc471bf 270 hc.SubmitRequest((uint8_t*)setup, 8, true); // PID_SETUP
DieterGraef 25:7d6d9fc471bf 271 while(hc.GetURBState() == URB_IDLE);
DieterGraef 24:5396b6a93262 272
DieterGraef 25:7d6d9fc471bf 273 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 274 {
DieterGraef 25:7d6d9fc471bf 275 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 276 LastStatus = ACK;
DieterGraef 25:7d6d9fc471bf 277 break;
DieterGraef 25:7d6d9fc471bf 278 default:
DieterGraef 25:7d6d9fc471bf 279 LastStatus = 0xff;
DieterGraef 25:7d6d9fc471bf 280 break;
DieterGraef 25:7d6d9fc471bf 281 }
DieterGraef 25:7d6d9fc471bf 282 ep->setData01(DATA1);
DieterGraef 25:7d6d9fc471bf 283 return 8;
DieterGraef 24:5396b6a93262 284 }
DieterGraef 24:5396b6a93262 285 }
DieterGraef 24:5396b6a93262 286
DieterGraef 24:5396b6a93262 287 int USBHALHost::token_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit)
DieterGraef 24:5396b6a93262 288 {
DieterGraef 25:7d6d9fc471bf 289
DieterGraef 24:5396b6a93262 290 switch(ep->getType())
DieterGraef 24:5396b6a93262 291 {
DieterGraef 24:5396b6a93262 292 case CONTROL_ENDPOINT:
DieterGraef 24:5396b6a93262 293 return token_ctl_in(ep, data, size, retryLimit);
DieterGraef 24:5396b6a93262 294 case INTERRUPT_ENDPOINT:
DieterGraef 24:5396b6a93262 295 return token_int_in(ep, data, size);
DieterGraef 24:5396b6a93262 296 case BULK_ENDPOINT:
DieterGraef 24:5396b6a93262 297 return token_blk_in(ep, data, size, retryLimit);
DieterGraef 24:5396b6a93262 298 }
DieterGraef 24:5396b6a93262 299 return -1;
DieterGraef 24:5396b6a93262 300 }
DieterGraef 24:5396b6a93262 301
DieterGraef 24:5396b6a93262 302 int USBHALHost::token_ctl_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit)
DieterGraef 24:5396b6a93262 303 {
DieterGraef 24:5396b6a93262 304 const uint8_t ep_addr = 0x80;
DieterGraef 24:5396b6a93262 305 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 306 {
DieterGraef 25:7d6d9fc471bf 307 HC_FS hc;
DieterGraef 25:7d6d9fc471bf 308
DieterGraef 25:7d6d9fc471bf 309 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 310 hc.Init(ep_addr,
DieterGraef 24:5396b6a93262 311 dev->getAddress(),
DieterGraef 24:5396b6a93262 312 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
DieterGraef 24:5396b6a93262 313 EP_TYPE_CTRL, ep->getSize());
DieterGraef 25:7d6d9fc471bf 314 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 315
DieterGraef 25:7d6d9fc471bf 316
DieterGraef 25:7d6d9fc471bf 317 wait_ms(1);
DieterGraef 25:7d6d9fc471bf 318 hc.SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 319 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 320
DieterGraef 25:7d6d9fc471bf 321
DieterGraef 25:7d6d9fc471bf 322 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 323 {
DieterGraef 25:7d6d9fc471bf 324 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 325 LastStatus = ACK;
DieterGraef 25:7d6d9fc471bf 326 break;
DieterGraef 25:7d6d9fc471bf 327 default:
DieterGraef 25:7d6d9fc471bf 328 LastStatus = 0xff;
DieterGraef 25:7d6d9fc471bf 329 return -1;
DieterGraef 25:7d6d9fc471bf 330 }
DieterGraef 25:7d6d9fc471bf 331 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 332 return hc.GetXferCount();
DieterGraef 24:5396b6a93262 333 }
DieterGraef 24:5396b6a93262 334 else
DieterGraef 24:5396b6a93262 335 {
DieterGraef 25:7d6d9fc471bf 336 HC_HS hc;
DieterGraef 25:7d6d9fc471bf 337
DieterGraef 25:7d6d9fc471bf 338 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 339 hc.Init(ep_addr,
DieterGraef 24:5396b6a93262 340 dev->getAddress(),
DieterGraef 24:5396b6a93262 341 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH ,
DieterGraef 24:5396b6a93262 342 EP_TYPE_CTRL, ep->getSize());
DieterGraef 25:7d6d9fc471bf 343 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 344
DieterGraef 25:7d6d9fc471bf 345
DieterGraef 25:7d6d9fc471bf 346 wait_ms(1);
DieterGraef 25:7d6d9fc471bf 347 hc.SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 348 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 349
DieterGraef 24:5396b6a93262 350
DieterGraef 25:7d6d9fc471bf 351 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 352 {
DieterGraef 25:7d6d9fc471bf 353 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 354 LastStatus = ACK;
DieterGraef 25:7d6d9fc471bf 355 break;
DieterGraef 25:7d6d9fc471bf 356 default:
DieterGraef 25:7d6d9fc471bf 357 LastStatus = 0xff;
DieterGraef 25:7d6d9fc471bf 358 return -1;
DieterGraef 25:7d6d9fc471bf 359 }
DieterGraef 25:7d6d9fc471bf 360 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 361 return hc.GetXferCount();
DieterGraef 24:5396b6a93262 362 }
DieterGraef 24:5396b6a93262 363 }
DieterGraef 24:5396b6a93262 364
DieterGraef 24:5396b6a93262 365 int USBHALHost::token_int_in(USBEndpoint* ep, uint8_t* data, int size)
DieterGraef 24:5396b6a93262 366 {
DieterGraef 24:5396b6a93262 367 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 368 {
DieterGraef 25:7d6d9fc471bf 369 HC_FS hc;
DieterGraef 25:7d6d9fc471bf 370
DieterGraef 25:7d6d9fc471bf 371 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 372 hc.Init(ep->getAddress(),
DieterGraef 24:5396b6a93262 373 dev->getAddress(),
DieterGraef 24:5396b6a93262 374 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
DieterGraef 24:5396b6a93262 375 EP_TYPE_INTR, ep->getSize());
DieterGraef 25:7d6d9fc471bf 376 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 377
DieterGraef 25:7d6d9fc471bf 378
DieterGraef 25:7d6d9fc471bf 379 hc.SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 380 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 381
DieterGraef 25:7d6d9fc471bf 382 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 383 {
DieterGraef 25:7d6d9fc471bf 384 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 385 switch(hc.GetState())
DieterGraef 25:7d6d9fc471bf 386 {
DieterGraef 25:7d6d9fc471bf 387 case HC_XFRC:
DieterGraef 25:7d6d9fc471bf 388 LastStatus = ep->getData01();
DieterGraef 25:7d6d9fc471bf 389 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 390 return hc.GetXferCount();
DieterGraef 25:7d6d9fc471bf 391 case HC_NAK:
DieterGraef 25:7d6d9fc471bf 392 LastStatus = NAK;
DieterGraef 25:7d6d9fc471bf 393 return -1;
DieterGraef 25:7d6d9fc471bf 394 }
DieterGraef 25:7d6d9fc471bf 395 break;
DieterGraef 25:7d6d9fc471bf 396 }
DieterGraef 25:7d6d9fc471bf 397 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 398 return -1;
DieterGraef 24:5396b6a93262 399 }
DieterGraef 24:5396b6a93262 400 else
DieterGraef 24:5396b6a93262 401 {
DieterGraef 25:7d6d9fc471bf 402 HC_HS hc;
DieterGraef 25:7d6d9fc471bf 403
DieterGraef 25:7d6d9fc471bf 404 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 405 hc.Init(ep->getAddress(),
DieterGraef 24:5396b6a93262 406 dev->getAddress(),
DieterGraef 24:5396b6a93262 407 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH ,
DieterGraef 24:5396b6a93262 408 EP_TYPE_INTR, ep->getSize());
DieterGraef 25:7d6d9fc471bf 409 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 24:5396b6a93262 410
DieterGraef 24:5396b6a93262 411
DieterGraef 25:7d6d9fc471bf 412 hc.SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 413 while(hc.GetURBState() == URB_IDLE);
DieterGraef 24:5396b6a93262 414
DieterGraef 25:7d6d9fc471bf 415 switch(hc.GetURBState())
DieterGraef 24:5396b6a93262 416 {
DieterGraef 24:5396b6a93262 417 case URB_DONE:
DieterGraef 24:5396b6a93262 418 switch(hc.GetState())
DieterGraef 24:5396b6a93262 419 {
DieterGraef 24:5396b6a93262 420 case HC_XFRC:
DieterGraef 24:5396b6a93262 421 LastStatus = ep->getData01();
DieterGraef 24:5396b6a93262 422 ep->toggleData01();
DieterGraef 24:5396b6a93262 423 return hc.GetXferCount();
DieterGraef 24:5396b6a93262 424 case HC_NAK:
DieterGraef 24:5396b6a93262 425 LastStatus = NAK;
DieterGraef 25:7d6d9fc471bf 426 return -1;
DieterGraef 24:5396b6a93262 427 }
DieterGraef 24:5396b6a93262 428 break;
DieterGraef 24:5396b6a93262 429 }
DieterGraef 25:7d6d9fc471bf 430 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 431 return -1;
DieterGraef 24:5396b6a93262 432 }
DieterGraef 25:7d6d9fc471bf 433 }
DieterGraef 25:7d6d9fc471bf 434
DieterGraef 25:7d6d9fc471bf 435 int USBHALHost::token_blk_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit)
DieterGraef 25:7d6d9fc471bf 436 {
DieterGraef 25:7d6d9fc471bf 437 if(IF_N==USB_FastSpeed)
DieterGraef 25:7d6d9fc471bf 438 {
DieterGraef 25:7d6d9fc471bf 439 HC_FS hc;
DieterGraef 25:7d6d9fc471bf 440
DieterGraef 25:7d6d9fc471bf 441 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 442 hc.Init(
DieterGraef 25:7d6d9fc471bf 443 ep->getAddress(),
DieterGraef 25:7d6d9fc471bf 444 dev->getAddress(),
DieterGraef 25:7d6d9fc471bf 445 HCD_SPEED_FULL,
DieterGraef 25:7d6d9fc471bf 446 EP_TYPE_BULK, ep->getSize());
DieterGraef 25:7d6d9fc471bf 447 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 448
DieterGraef 25:7d6d9fc471bf 449 int retry = 0;
DieterGraef 25:7d6d9fc471bf 450 do
DieterGraef 25:7d6d9fc471bf 451 {
DieterGraef 25:7d6d9fc471bf 452
DieterGraef 25:7d6d9fc471bf 453
DieterGraef 25:7d6d9fc471bf 454 hc.SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 455 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 456
DieterGraef 25:7d6d9fc471bf 457
DieterGraef 25:7d6d9fc471bf 458 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 459 {
DieterGraef 25:7d6d9fc471bf 460 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 461 switch(hc.GetState())
DieterGraef 25:7d6d9fc471bf 462 {
DieterGraef 25:7d6d9fc471bf 463 case HC_XFRC:
DieterGraef 25:7d6d9fc471bf 464 LastStatus = ep->getData01();
DieterGraef 25:7d6d9fc471bf 465 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 466 return hc.GetXferCount();
DieterGraef 25:7d6d9fc471bf 467 case HC_NAK:
DieterGraef 25:7d6d9fc471bf 468 LastStatus = NAK;
DieterGraef 25:7d6d9fc471bf 469 if (retryLimit > 0)
DieterGraef 25:7d6d9fc471bf 470 {
DieterGraef 25:7d6d9fc471bf 471 delay_ms(1 + 10 * retry);
DieterGraef 25:7d6d9fc471bf 472 }
DieterGraef 25:7d6d9fc471bf 473 break;
DieterGraef 25:7d6d9fc471bf 474 default:
DieterGraef 25:7d6d9fc471bf 475 break;
DieterGraef 25:7d6d9fc471bf 476 }
DieterGraef 25:7d6d9fc471bf 477 break;
DieterGraef 25:7d6d9fc471bf 478 case URB_STALL:
DieterGraef 25:7d6d9fc471bf 479 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 480 return -1;
DieterGraef 25:7d6d9fc471bf 481 default:
DieterGraef 25:7d6d9fc471bf 482 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 483 delay_ms(500 + 100 * retry);
DieterGraef 25:7d6d9fc471bf 484 break;
DieterGraef 25:7d6d9fc471bf 485 }
DieterGraef 25:7d6d9fc471bf 486 }
DieterGraef 25:7d6d9fc471bf 487 while(retry++ < retryLimit);
DieterGraef 25:7d6d9fc471bf 488 return -1;
DieterGraef 25:7d6d9fc471bf 489 }
DieterGraef 25:7d6d9fc471bf 490 else
DieterGraef 25:7d6d9fc471bf 491 {
DieterGraef 25:7d6d9fc471bf 492 HC_HS hc;
DieterGraef 25:7d6d9fc471bf 493
DieterGraef 25:7d6d9fc471bf 494 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 495 hc.Init(
DieterGraef 25:7d6d9fc471bf 496 ep->getAddress(),
DieterGraef 25:7d6d9fc471bf 497 dev->getAddress(),
DieterGraef 25:7d6d9fc471bf 498 HCD_SPEED_HIGH,
DieterGraef 25:7d6d9fc471bf 499 EP_TYPE_BULK, ep->getSize());
DieterGraef 25:7d6d9fc471bf 500 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 501 int retry = 0;
DieterGraef 25:7d6d9fc471bf 502 do
DieterGraef 25:7d6d9fc471bf 503 {
DieterGraef 25:7d6d9fc471bf 504
DieterGraef 25:7d6d9fc471bf 505
DieterGraef 25:7d6d9fc471bf 506
DieterGraef 25:7d6d9fc471bf 507 hc.SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 508 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 509
DieterGraef 25:7d6d9fc471bf 510
DieterGraef 25:7d6d9fc471bf 511 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 512 {
DieterGraef 25:7d6d9fc471bf 513 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 514 switch(hc.GetState())
DieterGraef 25:7d6d9fc471bf 515 {
DieterGraef 25:7d6d9fc471bf 516 case HC_XFRC:
DieterGraef 25:7d6d9fc471bf 517 LastStatus = ep->getData01();
DieterGraef 25:7d6d9fc471bf 518 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 519 return hc.GetXferCount();
DieterGraef 25:7d6d9fc471bf 520 case HC_NAK:
DieterGraef 25:7d6d9fc471bf 521 LastStatus = NAK;
DieterGraef 25:7d6d9fc471bf 522 if (retryLimit > 0)
DieterGraef 25:7d6d9fc471bf 523 {
DieterGraef 25:7d6d9fc471bf 524 delay_ms(1 + 10 * retry);
DieterGraef 25:7d6d9fc471bf 525 }
DieterGraef 25:7d6d9fc471bf 526 break;
DieterGraef 25:7d6d9fc471bf 527 default:
DieterGraef 25:7d6d9fc471bf 528 break;
DieterGraef 25:7d6d9fc471bf 529 }
DieterGraef 25:7d6d9fc471bf 530 break;
DieterGraef 25:7d6d9fc471bf 531 case URB_STALL:
DieterGraef 25:7d6d9fc471bf 532 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 533 return -1;
DieterGraef 25:7d6d9fc471bf 534 default:
DieterGraef 25:7d6d9fc471bf 535 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 536 delay_ms(500 + 100 * retry);
DieterGraef 25:7d6d9fc471bf 537 break;
DieterGraef 25:7d6d9fc471bf 538 }
DieterGraef 25:7d6d9fc471bf 539 }
DieterGraef 25:7d6d9fc471bf 540 while(retry++ < retryLimit);
DieterGraef 25:7d6d9fc471bf 541 return -1;
DieterGraef 25:7d6d9fc471bf 542 }
DieterGraef 24:5396b6a93262 543 }
DieterGraef 24:5396b6a93262 544
DieterGraef 24:5396b6a93262 545 int USBHALHost::token_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit)
DieterGraef 24:5396b6a93262 546 {
DieterGraef 25:7d6d9fc471bf 547
DieterGraef 25:7d6d9fc471bf 548
DieterGraef 24:5396b6a93262 549 switch(ep->getType())
DieterGraef 24:5396b6a93262 550 {
DieterGraef 24:5396b6a93262 551 case CONTROL_ENDPOINT:
DieterGraef 24:5396b6a93262 552 return token_ctl_out(ep, data, size, retryLimit);
DieterGraef 24:5396b6a93262 553 case INTERRUPT_ENDPOINT:
DieterGraef 24:5396b6a93262 554 return token_int_out(ep, data, size);
DieterGraef 24:5396b6a93262 555 case BULK_ENDPOINT:
DieterGraef 24:5396b6a93262 556 return token_blk_out(ep, data, size, retryLimit);
DieterGraef 24:5396b6a93262 557 }
DieterGraef 24:5396b6a93262 558 return -1;
DieterGraef 24:5396b6a93262 559 }
DieterGraef 24:5396b6a93262 560
DieterGraef 24:5396b6a93262 561 int USBHALHost::token_ctl_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit)
DieterGraef 24:5396b6a93262 562 {
DieterGraef 24:5396b6a93262 563 const uint8_t ep_addr = 0x00;
DieterGraef 24:5396b6a93262 564 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 565 {
DieterGraef 25:7d6d9fc471bf 566 HC_FS hc;
DieterGraef 25:7d6d9fc471bf 567
DieterGraef 25:7d6d9fc471bf 568 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 569 hc.Init(ep_addr,
DieterGraef 24:5396b6a93262 570 dev->getAddress(),
DieterGraef 24:5396b6a93262 571 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
DieterGraef 24:5396b6a93262 572 EP_TYPE_CTRL, ep->getSize());
DieterGraef 25:7d6d9fc471bf 573 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 574
DieterGraef 25:7d6d9fc471bf 575 do
DieterGraef 25:7d6d9fc471bf 576 {
DieterGraef 25:7d6d9fc471bf 577
DieterGraef 25:7d6d9fc471bf 578
DieterGraef 25:7d6d9fc471bf 579 wait_ms(1);
DieterGraef 25:7d6d9fc471bf 580 hc.SubmitRequest((uint8_t*)data, size);
DieterGraef 25:7d6d9fc471bf 581 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 582
DieterGraef 25:7d6d9fc471bf 583 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 584 {
DieterGraef 25:7d6d9fc471bf 585 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 586 LastStatus = ACK;
DieterGraef 25:7d6d9fc471bf 587 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 588 return size;
DieterGraef 25:7d6d9fc471bf 589
DieterGraef 25:7d6d9fc471bf 590 default:
DieterGraef 25:7d6d9fc471bf 591 break;
DieterGraef 25:7d6d9fc471bf 592 }
DieterGraef 25:7d6d9fc471bf 593 delay_ms(1);
DieterGraef 25:7d6d9fc471bf 594 }
DieterGraef 25:7d6d9fc471bf 595 while(retryLimit-- > 0);
DieterGraef 25:7d6d9fc471bf 596 return -1;
DieterGraef 24:5396b6a93262 597 }
DieterGraef 24:5396b6a93262 598 else
DieterGraef 24:5396b6a93262 599 {
DieterGraef 25:7d6d9fc471bf 600 HC_HS hc;
DieterGraef 25:7d6d9fc471bf 601
DieterGraef 25:7d6d9fc471bf 602 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 603 hc.Init(ep_addr,
DieterGraef 24:5396b6a93262 604 dev->getAddress(),
DieterGraef 24:5396b6a93262 605 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH ,
DieterGraef 24:5396b6a93262 606 EP_TYPE_CTRL, ep->getSize());
DieterGraef 25:7d6d9fc471bf 607 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 608
DieterGraef 25:7d6d9fc471bf 609 do
DieterGraef 25:7d6d9fc471bf 610 {
DieterGraef 24:5396b6a93262 611
DieterGraef 25:7d6d9fc471bf 612
DieterGraef 25:7d6d9fc471bf 613 wait_ms(1);
DieterGraef 25:7d6d9fc471bf 614 hc.SubmitRequest((uint8_t*)data, size);
DieterGraef 25:7d6d9fc471bf 615 while(hc.GetURBState() == URB_IDLE);
DieterGraef 24:5396b6a93262 616
DieterGraef 25:7d6d9fc471bf 617 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 618 {
DieterGraef 25:7d6d9fc471bf 619 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 620 LastStatus = ACK;
DieterGraef 25:7d6d9fc471bf 621 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 622 return size;
DieterGraef 24:5396b6a93262 623
DieterGraef 25:7d6d9fc471bf 624 default:
DieterGraef 25:7d6d9fc471bf 625 break;
DieterGraef 25:7d6d9fc471bf 626 }
DieterGraef 25:7d6d9fc471bf 627 delay_ms(1);
DieterGraef 24:5396b6a93262 628 }
DieterGraef 25:7d6d9fc471bf 629 while(retryLimit-- > 0);
DieterGraef 25:7d6d9fc471bf 630 return -1;
DieterGraef 24:5396b6a93262 631 }
DieterGraef 24:5396b6a93262 632 }
DieterGraef 24:5396b6a93262 633
DieterGraef 24:5396b6a93262 634 int USBHALHost::token_int_out(USBEndpoint* ep, const uint8_t* data, int size)
DieterGraef 24:5396b6a93262 635 {
DieterGraef 24:5396b6a93262 636 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 637 {
DieterGraef 25:7d6d9fc471bf 638 HC_FS hc;
DieterGraef 25:7d6d9fc471bf 639
DieterGraef 25:7d6d9fc471bf 640 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 641 hc.Init(
DieterGraef 24:5396b6a93262 642 ep->getAddress(),
DieterGraef 24:5396b6a93262 643 dev->getAddress(),
DieterGraef 24:5396b6a93262 644 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
DieterGraef 24:5396b6a93262 645 EP_TYPE_INTR, ep->getSize());
DieterGraef 25:7d6d9fc471bf 646 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 647
DieterGraef 25:7d6d9fc471bf 648
DieterGraef 25:7d6d9fc471bf 649 hc.SubmitRequest((uint8_t*)data, size);
DieterGraef 25:7d6d9fc471bf 650 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 651
DieterGraef 25:7d6d9fc471bf 652 if (hc.GetURBState() != URB_DONE)
DieterGraef 25:7d6d9fc471bf 653 {
DieterGraef 25:7d6d9fc471bf 654 return -1;
DieterGraef 25:7d6d9fc471bf 655 }
DieterGraef 25:7d6d9fc471bf 656 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 657 return size;
DieterGraef 24:5396b6a93262 658 }
DieterGraef 24:5396b6a93262 659 else
DieterGraef 24:5396b6a93262 660 {
DieterGraef 25:7d6d9fc471bf 661 HC_HS hc;
DieterGraef 25:7d6d9fc471bf 662
DieterGraef 25:7d6d9fc471bf 663 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 664 hc.Init(
DieterGraef 24:5396b6a93262 665 ep->getAddress(),
DieterGraef 24:5396b6a93262 666 dev->getAddress(),
DieterGraef 24:5396b6a93262 667 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_HIGH ,
DieterGraef 24:5396b6a93262 668 EP_TYPE_INTR, ep->getSize());
DieterGraef 25:7d6d9fc471bf 669 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 670
DieterGraef 25:7d6d9fc471bf 671
DieterGraef 25:7d6d9fc471bf 672 hc.SubmitRequest((uint8_t*)data, size);
DieterGraef 25:7d6d9fc471bf 673 while(hc.GetURBState() == URB_IDLE);
DieterGraef 24:5396b6a93262 674
DieterGraef 25:7d6d9fc471bf 675 if (hc.GetURBState() != URB_DONE)
DieterGraef 25:7d6d9fc471bf 676 {
DieterGraef 25:7d6d9fc471bf 677 return -1;
DieterGraef 25:7d6d9fc471bf 678 }
DieterGraef 25:7d6d9fc471bf 679 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 680 return size;
DieterGraef 24:5396b6a93262 681 }
DieterGraef 24:5396b6a93262 682 }
DieterGraef 24:5396b6a93262 683
DieterGraef 24:5396b6a93262 684 int USBHALHost::token_blk_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit)
DieterGraef 24:5396b6a93262 685 {
DieterGraef 24:5396b6a93262 686 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 687 {
DieterGraef 25:7d6d9fc471bf 688 HC_FS hc;
DieterGraef 25:7d6d9fc471bf 689
DieterGraef 25:7d6d9fc471bf 690 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 691 hc.Init(
DieterGraef 25:7d6d9fc471bf 692 ep->getAddress(), dev->getAddress(),
DieterGraef 25:7d6d9fc471bf 693 HCD_SPEED_FULL, EP_TYPE_BULK, ep->getSize());
DieterGraef 25:7d6d9fc471bf 694
DieterGraef 25:7d6d9fc471bf 695 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 696
DieterGraef 25:7d6d9fc471bf 697 int retry = 0;
DieterGraef 25:7d6d9fc471bf 698 do
DieterGraef 25:7d6d9fc471bf 699 {
DieterGraef 24:5396b6a93262 700
DieterGraef 24:5396b6a93262 701
DieterGraef 25:7d6d9fc471bf 702 hc.SubmitRequest((uint8_t*)data, size);
DieterGraef 25:7d6d9fc471bf 703 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 704
DieterGraef 25:7d6d9fc471bf 705 switch(hc.GetURBState())
DieterGraef 25:7d6d9fc471bf 706 {
DieterGraef 25:7d6d9fc471bf 707 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 708 switch(hc.GetState())
DieterGraef 25:7d6d9fc471bf 709 {
DieterGraef 25:7d6d9fc471bf 710 case HC_XFRC: // ACK
DieterGraef 25:7d6d9fc471bf 711 LastStatus = ep->getData01();
DieterGraef 25:7d6d9fc471bf 712 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 713 return size;
DieterGraef 25:7d6d9fc471bf 714 default:
DieterGraef 25:7d6d9fc471bf 715 break;
DieterGraef 25:7d6d9fc471bf 716 }
DieterGraef 25:7d6d9fc471bf 717 break;
DieterGraef 25:7d6d9fc471bf 718 case URB_NOTREADY: // HC_NAK
DieterGraef 25:7d6d9fc471bf 719 LastStatus = NAK;
DieterGraef 25:7d6d9fc471bf 720 delay_ms(100 * retry);
DieterGraef 25:7d6d9fc471bf 721 break;
DieterGraef 25:7d6d9fc471bf 722 default:
DieterGraef 25:7d6d9fc471bf 723 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 724 return -1;
DieterGraef 25:7d6d9fc471bf 725 }
DieterGraef 25:7d6d9fc471bf 726 }
DieterGraef 25:7d6d9fc471bf 727 while(retry++ < retryLimit);
DieterGraef 25:7d6d9fc471bf 728 return -1;
DieterGraef 24:5396b6a93262 729 }
DieterGraef 24:5396b6a93262 730 else
DieterGraef 24:5396b6a93262 731 {
DieterGraef 25:7d6d9fc471bf 732 HC_HS hc;
DieterGraef 25:7d6d9fc471bf 733
DieterGraef 25:7d6d9fc471bf 734 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 735 hc.Init(
DieterGraef 24:5396b6a93262 736 ep->getAddress(), dev->getAddress(),
DieterGraef 24:5396b6a93262 737 HCD_SPEED_HIGH, EP_TYPE_BULK, ep->getSize());
DieterGraef 25:7d6d9fc471bf 738 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
DieterGraef 25:7d6d9fc471bf 739
DieterGraef 25:7d6d9fc471bf 740 int retry = 0;
DieterGraef 25:7d6d9fc471bf 741 do
DieterGraef 25:7d6d9fc471bf 742 {
DieterGraef 24:5396b6a93262 743
DieterGraef 25:7d6d9fc471bf 744
DieterGraef 25:7d6d9fc471bf 745 hc.SubmitRequest((uint8_t*)data, size);
DieterGraef 25:7d6d9fc471bf 746 while(hc.GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 747
DieterGraef 25:7d6d9fc471bf 748 switch(hc.GetURBState())
DieterGraef 24:5396b6a93262 749 {
DieterGraef 25:7d6d9fc471bf 750 case URB_DONE:
DieterGraef 25:7d6d9fc471bf 751 switch(hc.GetState())
DieterGraef 25:7d6d9fc471bf 752 {
DieterGraef 25:7d6d9fc471bf 753 case HC_XFRC: // ACK
DieterGraef 25:7d6d9fc471bf 754 LastStatus = ep->getData01();
DieterGraef 25:7d6d9fc471bf 755 ep->toggleData01();
DieterGraef 25:7d6d9fc471bf 756 return size;
DieterGraef 25:7d6d9fc471bf 757 default:
DieterGraef 25:7d6d9fc471bf 758 break;
DieterGraef 25:7d6d9fc471bf 759 }
DieterGraef 25:7d6d9fc471bf 760 break;
DieterGraef 25:7d6d9fc471bf 761 case URB_NOTREADY: // HC_NAK
DieterGraef 25:7d6d9fc471bf 762 LastStatus = NAK;
DieterGraef 25:7d6d9fc471bf 763 delay_ms(100 * retry);
DieterGraef 25:7d6d9fc471bf 764 break;
DieterGraef 24:5396b6a93262 765 default:
DieterGraef 25:7d6d9fc471bf 766 LastStatus = STALL;
DieterGraef 25:7d6d9fc471bf 767 return -1;
DieterGraef 24:5396b6a93262 768 }
DieterGraef 24:5396b6a93262 769 }
DieterGraef 25:7d6d9fc471bf 770 while(retry++ < retryLimit);
DieterGraef 25:7d6d9fc471bf 771 return -1;
DieterGraef 24:5396b6a93262 772 }
DieterGraef 24:5396b6a93262 773 }
DieterGraef 24:5396b6a93262 774
DieterGraef 24:5396b6a93262 775 int USBHALHost::token_iso_in(USBEndpoint* ep, uint8_t* data, int size)
DieterGraef 24:5396b6a93262 776 {
DieterGraef 25:7d6d9fc471bf 777
DieterGraef 25:7d6d9fc471bf 778 if(IF_N==USB_FastSpeed)
DieterGraef 24:5396b6a93262 779 {
DieterGraef 25:7d6d9fc471bf 780 HC_FS* hc = ep->getHALData<HC_FS*>();
DieterGraef 25:7d6d9fc471bf 781 if (hc == NULL)
DieterGraef 24:5396b6a93262 782 {
DieterGraef 25:7d6d9fc471bf 783 hc = new HC_FS();
DieterGraef 25:7d6d9fc471bf 784 ep->setHALData<HC_FS*>(hc);
DieterGraef 25:7d6d9fc471bf 785 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 786 hc->Init(
DieterGraef 24:5396b6a93262 787 ep->getAddress(), dev->getAddress(),
DieterGraef 24:5396b6a93262 788 HCD_SPEED_FULL, EP_TYPE_ISOC, ep->getSize());
DieterGraef 24:5396b6a93262 789 }
DieterGraef 25:7d6d9fc471bf 790
DieterGraef 25:7d6d9fc471bf 791
DieterGraef 25:7d6d9fc471bf 792 hc->SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 793 while(hc->GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 794
DieterGraef 25:7d6d9fc471bf 795 return hc->GetXferCount();
DieterGraef 24:5396b6a93262 796 }
DieterGraef 25:7d6d9fc471bf 797 else
DieterGraef 25:7d6d9fc471bf 798 {
DieterGraef 25:7d6d9fc471bf 799 HC_HS* hc = ep->getHALData<HC_HS*>();
DieterGraef 25:7d6d9fc471bf 800 if (hc == NULL)
DieterGraef 25:7d6d9fc471bf 801 {
DieterGraef 25:7d6d9fc471bf 802 hc = new HC_HS();
DieterGraef 25:7d6d9fc471bf 803 ep->setHALData<HC_HS*>(hc);
DieterGraef 25:7d6d9fc471bf 804 USBDeviceConnected* dev = ep->getDevice();
DieterGraef 25:7d6d9fc471bf 805 hc->Init(
DieterGraef 25:7d6d9fc471bf 806 ep->getAddress(), dev->getAddress(),
DieterGraef 25:7d6d9fc471bf 807 HCD_SPEED_FULL, EP_TYPE_ISOC, ep->getSize());
DieterGraef 25:7d6d9fc471bf 808 }
DieterGraef 25:7d6d9fc471bf 809
DieterGraef 25:7d6d9fc471bf 810
DieterGraef 25:7d6d9fc471bf 811 hc->SubmitRequest(data, size);
DieterGraef 25:7d6d9fc471bf 812 while(hc->GetURBState() == URB_IDLE);
DieterGraef 25:7d6d9fc471bf 813
DieterGraef 25:7d6d9fc471bf 814 return hc->GetXferCount();
DieterGraef 25:7d6d9fc471bf 815 }
DieterGraef 24:5396b6a93262 816 }
DieterGraef 24:5396b6a93262 817
DieterGraef 24:5396b6a93262 818 int USBHALHost::multi_token_in(USBEndpoint* ep, uint8_t* data, size_t total, bool block)
DieterGraef 24:5396b6a93262 819 {
DieterGraef 24:5396b6a93262 820 if (total == 0)
DieterGraef 24:5396b6a93262 821 {
DieterGraef 24:5396b6a93262 822 return token_in(ep);
DieterGraef 24:5396b6a93262 823 }
DieterGraef 24:5396b6a93262 824 int retryLimit = block ? 10 : 0;
DieterGraef 24:5396b6a93262 825 int read_len = 0;
DieterGraef 24:5396b6a93262 826 for(int n = 0; read_len < total; n++)
DieterGraef 24:5396b6a93262 827 {
DieterGraef 24:5396b6a93262 828 int size = std::min((int)total-read_len, ep->getSize());
DieterGraef 24:5396b6a93262 829 int result = token_in(ep, data+read_len, size, retryLimit);
DieterGraef 24:5396b6a93262 830 if (result < 0)
DieterGraef 24:5396b6a93262 831 {
DieterGraef 24:5396b6a93262 832 if (block)
DieterGraef 24:5396b6a93262 833 {
DieterGraef 24:5396b6a93262 834 return -1;
DieterGraef 24:5396b6a93262 835 }
DieterGraef 24:5396b6a93262 836 if (LastStatus == NAK)
DieterGraef 24:5396b6a93262 837 {
DieterGraef 24:5396b6a93262 838 if (n == 0)
DieterGraef 24:5396b6a93262 839 {
DieterGraef 24:5396b6a93262 840 return -1;
DieterGraef 24:5396b6a93262 841 }
DieterGraef 24:5396b6a93262 842 break;
DieterGraef 24:5396b6a93262 843 }
DieterGraef 24:5396b6a93262 844 return result;
DieterGraef 24:5396b6a93262 845 }
DieterGraef 24:5396b6a93262 846 read_len += result;
DieterGraef 24:5396b6a93262 847 if (result < ep->getSize())
DieterGraef 24:5396b6a93262 848 {
DieterGraef 24:5396b6a93262 849 break;
DieterGraef 24:5396b6a93262 850 }
DieterGraef 24:5396b6a93262 851 }
DieterGraef 24:5396b6a93262 852 return read_len;
DieterGraef 24:5396b6a93262 853 }
DieterGraef 24:5396b6a93262 854
DieterGraef 24:5396b6a93262 855 int USBHALHost::multi_token_out(USBEndpoint* ep, const uint8_t* data, size_t total)
DieterGraef 24:5396b6a93262 856 {
DieterGraef 24:5396b6a93262 857 if (total == 0)
DieterGraef 24:5396b6a93262 858 {
DieterGraef 24:5396b6a93262 859 return token_out(ep);
DieterGraef 24:5396b6a93262 860 }
DieterGraef 24:5396b6a93262 861 int write_len = 0;
DieterGraef 24:5396b6a93262 862 for(int n = 0; write_len < total; n++)
DieterGraef 24:5396b6a93262 863 {
DieterGraef 24:5396b6a93262 864 int size = std::min((int)total-write_len, ep->getSize());
DieterGraef 24:5396b6a93262 865 int result = token_out(ep, data+write_len, size);
DieterGraef 24:5396b6a93262 866 if (result < 0)
DieterGraef 24:5396b6a93262 867 {
DieterGraef 24:5396b6a93262 868 if (LastStatus == NAK)
DieterGraef 24:5396b6a93262 869 {
DieterGraef 24:5396b6a93262 870 if (n == 0)
DieterGraef 24:5396b6a93262 871 {
DieterGraef 24:5396b6a93262 872 return -1;
DieterGraef 24:5396b6a93262 873 }
DieterGraef 24:5396b6a93262 874 break;
DieterGraef 24:5396b6a93262 875 }
DieterGraef 24:5396b6a93262 876 USB_DBG("token_out result=%d %02x", result, LastStatus);
DieterGraef 24:5396b6a93262 877 return result;
DieterGraef 24:5396b6a93262 878 }
DieterGraef 24:5396b6a93262 879 write_len += result;
DieterGraef 24:5396b6a93262 880 if (result < ep->getSize())
DieterGraef 24:5396b6a93262 881 {
DieterGraef 24:5396b6a93262 882 break;
DieterGraef 24:5396b6a93262 883 }
DieterGraef 24:5396b6a93262 884 }
DieterGraef 24:5396b6a93262 885 return write_len;
DieterGraef 24:5396b6a93262 886 }
DieterGraef 24:5396b6a93262 887 void USBHALHost::multi_token_inNB(USBEndpoint* ep, uint8_t* data, int size)
DieterGraef 24:5396b6a93262 888 {
DieterGraef 24:5396b6a93262 889 //USB_TRACE1(size);
DieterGraef 24:5396b6a93262 890 //USB_TEST_ASSERT(ep->getState() != USB_TYPE_PROCESSING);
DieterGraef 24:5396b6a93262 891 ep->setBuffer(data, size);
DieterGraef 24:5396b6a93262 892 ep->setState(USB_TYPE_PROCESSING);
DieterGraef 24:5396b6a93262 893 }
DieterGraef 24:5396b6a93262 894
DieterGraef 24:5396b6a93262 895 USB_TYPE USBHALHost::multi_token_inNB_result(USBEndpoint* ep)
DieterGraef 24:5396b6a93262 896 {
DieterGraef 24:5396b6a93262 897 //USB_TEST_ASSERT(ep->getState() == USB_TYPE_PROCESSING);
DieterGraef 24:5396b6a93262 898 uint8_t* buf = ep->getBufStart();
DieterGraef 24:5396b6a93262 899 int size = ep->getBufSize();
DieterGraef 24:5396b6a93262 900 int result = multi_token_in(ep, buf, size, false);
DieterGraef 24:5396b6a93262 901 //USB_TRACE1(result);
DieterGraef 24:5396b6a93262 902 if (result < 0)
DieterGraef 24:5396b6a93262 903 {
DieterGraef 24:5396b6a93262 904 return USB_TYPE_PROCESSING;
DieterGraef 24:5396b6a93262 905 }
DieterGraef 24:5396b6a93262 906 ep->setLengthTransferred(result);
DieterGraef 24:5396b6a93262 907 ep->setState(USB_TYPE_IDLE);
DieterGraef 24:5396b6a93262 908 return USB_TYPE_OK;
DieterGraef 24:5396b6a93262 909
DieterGraef 24:5396b6a93262 910 }
DieterGraef 24:5396b6a93262 911
DieterGraef 24:5396b6a93262 912 void USBHALHost::setToggle(USBEndpoint* ep, uint8_t toggle)
DieterGraef 24:5396b6a93262 913 {
DieterGraef 24:5396b6a93262 914 //USB_TEST_ASSERT(toggle == 1);
DieterGraef 24:5396b6a93262 915 ep->setData01(toggle == 0 ? DATA0 : DATA1);
DieterGraef 24:5396b6a93262 916 }
DieterGraef 24:5396b6a93262 917
DieterGraef 24:5396b6a93262 918 int USBHALHost::epint_setup(USBEndpoint* ep)
DieterGraef 24:5396b6a93262 919 {
DieterGraef 24:5396b6a93262 920 return 1;
DieterGraef 24:5396b6a93262 921 }
DieterGraef 24:5396b6a93262 922
DieterGraef 25:7d6d9fc471bf 923 void USBHALHost::_usbisr_FS(void)
DieterGraef 24:5396b6a93262 924 {
DieterGraef 25:7d6d9fc471bf 925 instHost->usbisr_FS();
DieterGraef 24:5396b6a93262 926 }
DieterGraef 24:5396b6a93262 927
DieterGraef 25:7d6d9fc471bf 928 void USBHALHost::_usbisr_HS(void)
DieterGraef 25:7d6d9fc471bf 929 {
DieterGraef 25:7d6d9fc471bf 930 instHost->usbisr_HS();
DieterGraef 25:7d6d9fc471bf 931 }
DieterGraef 24:5396b6a93262 932
DieterGraef 25:7d6d9fc471bf 933 void USBHALHost::usbisr_FS(void)
DieterGraef 24:5396b6a93262 934 {
DieterGraef 25:7d6d9fc471bf 935 USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS;
DieterGraef 24:5396b6a93262 936 uint32_t i = 0 , interrupt = 0;
DieterGraef 24:5396b6a93262 937
DieterGraef 24:5396b6a93262 938 /* ensure that we are in device mode */
DieterGraef 25:7d6d9fc471bf 939 if (USB_GetMode(USB_OTG_FS) == USB_OTG_MODE_HOST)
DieterGraef 24:5396b6a93262 940 {
DieterGraef 24:5396b6a93262 941 /* avoid spurious interrupt */
DieterGraef 25:7d6d9fc471bf 942 if(__HAL_HCD_IS_INVALID_INTERRUPT(&hhcd_USB_FS))
DieterGraef 24:5396b6a93262 943 {
DieterGraef 24:5396b6a93262 944 return;
DieterGraef 24:5396b6a93262 945 }
DieterGraef 24:5396b6a93262 946
DieterGraef 25:7d6d9fc471bf 947 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
DieterGraef 24:5396b6a93262 948 {
DieterGraef 24:5396b6a93262 949 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 950 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
DieterGraef 24:5396b6a93262 951 }
DieterGraef 24:5396b6a93262 952
DieterGraef 25:7d6d9fc471bf 953 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_IISOIXFR))
DieterGraef 24:5396b6a93262 954 {
DieterGraef 24:5396b6a93262 955 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 956 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_IISOIXFR);
DieterGraef 24:5396b6a93262 957 }
DieterGraef 24:5396b6a93262 958
DieterGraef 25:7d6d9fc471bf 959 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PTXFE))
DieterGraef 24:5396b6a93262 960 {
DieterGraef 24:5396b6a93262 961 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 962 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_PTXFE);
DieterGraef 24:5396b6a93262 963 }
DieterGraef 24:5396b6a93262 964
DieterGraef 25:7d6d9fc471bf 965 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_MMIS))
DieterGraef 24:5396b6a93262 966 {
DieterGraef 24:5396b6a93262 967 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 968 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_MMIS);
DieterGraef 24:5396b6a93262 969 }
DieterGraef 24:5396b6a93262 970
DieterGraef 24:5396b6a93262 971 /* Handle Host Disconnect Interrupts */
DieterGraef 25:7d6d9fc471bf 972 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_DISCINT))
DieterGraef 24:5396b6a93262 973 {
DieterGraef 24:5396b6a93262 974
DieterGraef 24:5396b6a93262 975 /* Cleanup HPRT */
DieterGraef 24:5396b6a93262 976 USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
DieterGraef 24:5396b6a93262 977 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
DieterGraef 24:5396b6a93262 978
DieterGraef 24:5396b6a93262 979 /* Handle Host Port Interrupts */
DieterGraef 25:7d6d9fc471bf 980 HAL_HCD_Disconnect_Callback(&hhcd_USB_FS);
DieterGraef 25:7d6d9fc471bf 981 USB_InitFSLSPClkSel(USB_OTG_FS ,HCFG_48_MHZ );
DieterGraef 25:7d6d9fc471bf 982 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 24:5396b6a93262 983 }
DieterGraef 24:5396b6a93262 984
DieterGraef 24:5396b6a93262 985 /* Handle Host Port Interrupts */
DieterGraef 25:7d6d9fc471bf 986 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_HPRTINT))
DieterGraef 24:5396b6a93262 987 {
DieterGraef 24:5396b6a93262 988 //
DieterGraef 25:7d6d9fc471bf 989 USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS;
DieterGraef 24:5396b6a93262 990 __IO uint32_t hprt0, hprt0_dup;
DieterGraef 24:5396b6a93262 991
DieterGraef 24:5396b6a93262 992 /* Handle Host Port Interrupts */
DieterGraef 24:5396b6a93262 993 hprt0 = USBx_HPRT0;
DieterGraef 24:5396b6a93262 994 hprt0_dup = USBx_HPRT0;
DieterGraef 24:5396b6a93262 995
DieterGraef 24:5396b6a93262 996 hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
DieterGraef 24:5396b6a93262 997 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
DieterGraef 24:5396b6a93262 998
DieterGraef 24:5396b6a93262 999 /* Check wether Port Connect Detected */
DieterGraef 24:5396b6a93262 1000 if((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET)
DieterGraef 24:5396b6a93262 1001 {
DieterGraef 24:5396b6a93262 1002 if((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS)
DieterGraef 24:5396b6a93262 1003 {
DieterGraef 25:7d6d9fc471bf 1004 USB_MASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 25:7d6d9fc471bf 1005 HAL_HCD_Connect_Callback(&hhcd_USB_FS);
DieterGraef 24:5396b6a93262 1006 }
DieterGraef 24:5396b6a93262 1007 hprt0_dup |= USB_OTG_HPRT_PCDET;
DieterGraef 24:5396b6a93262 1008
DieterGraef 24:5396b6a93262 1009 }
DieterGraef 24:5396b6a93262 1010
DieterGraef 24:5396b6a93262 1011 /* Check whether Port Enable Changed */
DieterGraef 24:5396b6a93262 1012 if((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG)
DieterGraef 24:5396b6a93262 1013 {
DieterGraef 24:5396b6a93262 1014 hprt0_dup |= USB_OTG_HPRT_PENCHNG;
DieterGraef 24:5396b6a93262 1015
DieterGraef 24:5396b6a93262 1016 if((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA)
DieterGraef 24:5396b6a93262 1017 {
DieterGraef 25:7d6d9fc471bf 1018 if(hhcd_USB_FS.Init.phy_itface == USB_OTG_EMBEDDED_PHY)
DieterGraef 24:5396b6a93262 1019 {
DieterGraef 24:5396b6a93262 1020 if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17))
DieterGraef 24:5396b6a93262 1021 {
DieterGraef 25:7d6d9fc471bf 1022 USB_InitFSLSPClkSel(USB_OTG_FS ,HCFG_6_MHZ );
DieterGraef 24:5396b6a93262 1023 }
DieterGraef 24:5396b6a93262 1024 else
DieterGraef 24:5396b6a93262 1025 {
DieterGraef 25:7d6d9fc471bf 1026 USB_InitFSLSPClkSel(USB_OTG_FS ,HCFG_48_MHZ );
DieterGraef 24:5396b6a93262 1027 }
DieterGraef 24:5396b6a93262 1028 }
DieterGraef 24:5396b6a93262 1029 else
DieterGraef 24:5396b6a93262 1030 {
DieterGraef 25:7d6d9fc471bf 1031 if(hhcd_USB_FS.Init.speed == HCD_SPEED_FULL)
DieterGraef 24:5396b6a93262 1032 {
DieterGraef 24:5396b6a93262 1033 USBx_HOST->HFIR = (uint32_t)60000;
DieterGraef 24:5396b6a93262 1034 }
DieterGraef 24:5396b6a93262 1035 }
DieterGraef 25:7d6d9fc471bf 1036 HAL_HCD_Connect_Callback(&hhcd_USB_FS);
DieterGraef 24:5396b6a93262 1037
DieterGraef 25:7d6d9fc471bf 1038 if(hhcd_USB_FS.Init.speed == HCD_SPEED_HIGH)
DieterGraef 24:5396b6a93262 1039 {
DieterGraef 25:7d6d9fc471bf 1040 USB_UNMASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 24:5396b6a93262 1041 }
DieterGraef 24:5396b6a93262 1042 }
DieterGraef 24:5396b6a93262 1043 else
DieterGraef 24:5396b6a93262 1044 {
DieterGraef 24:5396b6a93262 1045 /* Cleanup HPRT */
DieterGraef 24:5396b6a93262 1046 USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
DieterGraef 24:5396b6a93262 1047 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
DieterGraef 24:5396b6a93262 1048
DieterGraef 25:7d6d9fc471bf 1049 USB_UNMASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 24:5396b6a93262 1050 }
DieterGraef 24:5396b6a93262 1051 }
DieterGraef 24:5396b6a93262 1052
DieterGraef 24:5396b6a93262 1053 /* Check For an overcurrent */
DieterGraef 24:5396b6a93262 1054 if((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG)
DieterGraef 24:5396b6a93262 1055 {
DieterGraef 24:5396b6a93262 1056 hprt0_dup |= USB_OTG_HPRT_POCCHNG;
DieterGraef 24:5396b6a93262 1057 }
DieterGraef 24:5396b6a93262 1058
DieterGraef 24:5396b6a93262 1059 /* Clear Port Interrupts */
DieterGraef 24:5396b6a93262 1060 USBx_HPRT0 = hprt0_dup;
DieterGraef 24:5396b6a93262 1061 }
DieterGraef 24:5396b6a93262 1062
DieterGraef 24:5396b6a93262 1063
DieterGraef 24:5396b6a93262 1064 //
DieterGraef 24:5396b6a93262 1065
DieterGraef 24:5396b6a93262 1066
DieterGraef 24:5396b6a93262 1067 /* Handle Host SOF Interrupts */
DieterGraef 25:7d6d9fc471bf 1068 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_SOF))
DieterGraef 24:5396b6a93262 1069 {
DieterGraef 25:7d6d9fc471bf 1070 HAL_HCD_SOF_Callback(&hhcd_USB_FS);
DieterGraef 25:7d6d9fc471bf 1071 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_SOF);
DieterGraef 24:5396b6a93262 1072 }
DieterGraef 24:5396b6a93262 1073
DieterGraef 24:5396b6a93262 1074 /* Handle Host channel Interrupts */
DieterGraef 25:7d6d9fc471bf 1075 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_HCINT))
DieterGraef 24:5396b6a93262 1076 {
DieterGraef 24:5396b6a93262 1077
DieterGraef 25:7d6d9fc471bf 1078 interrupt = USB_HC_ReadInterrupt(USB_OTG_FS);
DieterGraef 25:7d6d9fc471bf 1079 for (i = 0; i < hhcd_USB_FS.Init.Host_channels ; i++)
DieterGraef 24:5396b6a93262 1080 {
DieterGraef 24:5396b6a93262 1081 if (interrupt & (1 << i))
DieterGraef 24:5396b6a93262 1082 {
DieterGraef 24:5396b6a93262 1083 if ((USBx_HC(i)->HCCHAR) & USB_OTG_HCCHAR_EPDIR)
DieterGraef 24:5396b6a93262 1084 {
DieterGraef 24:5396b6a93262 1085 //
DieterGraef 25:7d6d9fc471bf 1086 USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS;
DieterGraef 24:5396b6a93262 1087 uint8_t chnum = i;
DieterGraef 24:5396b6a93262 1088 if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
DieterGraef 24:5396b6a93262 1089 {
DieterGraef 24:5396b6a93262 1090 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
DieterGraef 24:5396b6a93262 1091 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 24:5396b6a93262 1092 }
DieterGraef 24:5396b6a93262 1093 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
DieterGraef 24:5396b6a93262 1094 {
DieterGraef 24:5396b6a93262 1095 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
DieterGraef 24:5396b6a93262 1096 }
DieterGraef 24:5396b6a93262 1097
DieterGraef 24:5396b6a93262 1098 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
DieterGraef 24:5396b6a93262 1099 {
DieterGraef 24:5396b6a93262 1100 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1101 hhcd_USB_FS.hc[chnum].state = HC_STALL;
DieterGraef 24:5396b6a93262 1102 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 24:5396b6a93262 1103 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
DieterGraef 25:7d6d9fc471bf 1104 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1105 }
DieterGraef 24:5396b6a93262 1106 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
DieterGraef 24:5396b6a93262 1107 {
DieterGraef 24:5396b6a93262 1108 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1109 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1110 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1111 hhcd_USB_FS.hc[chnum].state = HC_DATATGLERR;
DieterGraef 24:5396b6a93262 1112 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
DieterGraef 24:5396b6a93262 1113 }
DieterGraef 24:5396b6a93262 1114
DieterGraef 24:5396b6a93262 1115 if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
DieterGraef 24:5396b6a93262 1116 {
DieterGraef 24:5396b6a93262 1117 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1118 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1119 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
DieterGraef 24:5396b6a93262 1120 }
DieterGraef 24:5396b6a93262 1121
DieterGraef 24:5396b6a93262 1122 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
DieterGraef 24:5396b6a93262 1123 {
DieterGraef 24:5396b6a93262 1124
DieterGraef 25:7d6d9fc471bf 1125 if (hhcd_USB_FS.Init.dma_enable)
DieterGraef 24:5396b6a93262 1126 {
DieterGraef 25:7d6d9fc471bf 1127 hhcd_USB_FS.hc[chnum].xfer_count = hhcd_USB_FS.hc[chnum].xfer_len - \
DieterGraef 25:7d6d9fc471bf 1128 (USBx_HC(chnum)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ);
DieterGraef 24:5396b6a93262 1129 }
DieterGraef 24:5396b6a93262 1130
DieterGraef 25:7d6d9fc471bf 1131 hhcd_USB_FS.hc[chnum].state = HC_XFRC;
DieterGraef 24:5396b6a93262 1132 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
DieterGraef 24:5396b6a93262 1133
DieterGraef 24:5396b6a93262 1134
DieterGraef 25:7d6d9fc471bf 1135 if ((hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_CTRL)||
DieterGraef 25:7d6d9fc471bf 1136 (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_BULK))
DieterGraef 24:5396b6a93262 1137 {
DieterGraef 24:5396b6a93262 1138 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1139 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1140 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 24:5396b6a93262 1141
DieterGraef 24:5396b6a93262 1142 }
DieterGraef 25:7d6d9fc471bf 1143 else if(hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_INTR)
DieterGraef 24:5396b6a93262 1144 {
DieterGraef 24:5396b6a93262 1145 USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
DieterGraef 24:5396b6a93262 1146 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1147 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1148 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 24:5396b6a93262 1149 }
DieterGraef 25:7d6d9fc471bf 1150 else if(hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_ISOC)
DieterGraef 24:5396b6a93262 1151 {
DieterGraef 24:5396b6a93262 1152 USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
DieterGraef 25:7d6d9fc471bf 1153 hhcd_USB_FS.hc[chnum].urb_state = URB_DONE;
DieterGraef 25:7d6d9fc471bf 1154 HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_FS, chnum, hhcd_USB_FS.hc[chnum].urb_state);
DieterGraef 24:5396b6a93262 1155 }
DieterGraef 25:7d6d9fc471bf 1156 hhcd_USB_FS.hc[chnum].toggle_in ^= 1;
DieterGraef 24:5396b6a93262 1157
DieterGraef 24:5396b6a93262 1158 }
DieterGraef 24:5396b6a93262 1159 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
DieterGraef 24:5396b6a93262 1160 {
DieterGraef 24:5396b6a93262 1161 __HAL_HCD_MASK_HALT_HC_INT(chnum);
DieterGraef 24:5396b6a93262 1162
DieterGraef 25:7d6d9fc471bf 1163 if(hhcd_USB_FS.hc[chnum].state == HC_XFRC)
DieterGraef 24:5396b6a93262 1164 {
DieterGraef 25:7d6d9fc471bf 1165 hhcd_USB_FS.hc[chnum].urb_state = URB_DONE;
DieterGraef 24:5396b6a93262 1166 }
DieterGraef 24:5396b6a93262 1167
DieterGraef 25:7d6d9fc471bf 1168 else if (hhcd_USB_FS.hc[chnum].state == HC_NAK)
DieterGraef 24:5396b6a93262 1169 {
DieterGraef 25:7d6d9fc471bf 1170 hhcd_USB_FS.hc[chnum].urb_state = URB_DONE;
DieterGraef 24:5396b6a93262 1171 }
DieterGraef 24:5396b6a93262 1172
DieterGraef 25:7d6d9fc471bf 1173 else if (hhcd_USB_FS.hc[chnum].state == HC_STALL)
DieterGraef 24:5396b6a93262 1174 {
DieterGraef 25:7d6d9fc471bf 1175 hhcd_USB_FS.hc[chnum].urb_state = URB_STALL;
DieterGraef 24:5396b6a93262 1176 }
DieterGraef 24:5396b6a93262 1177
DieterGraef 25:7d6d9fc471bf 1178 else if (hhcd_USB_FS.hc[chnum].state == HC_XACTERR)
DieterGraef 24:5396b6a93262 1179 {
DieterGraef 25:7d6d9fc471bf 1180 hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 24:5396b6a93262 1181 }
DieterGraef 24:5396b6a93262 1182
DieterGraef 25:7d6d9fc471bf 1183 else if (hhcd_USB_FS.hc[chnum].state == HC_DATATGLERR)
DieterGraef 24:5396b6a93262 1184 {
DieterGraef 25:7d6d9fc471bf 1185 hhcd_USB_FS.hc[chnum].urb_state = URB_ERROR;
DieterGraef 24:5396b6a93262 1186 }
DieterGraef 24:5396b6a93262 1187
DieterGraef 24:5396b6a93262 1188 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
DieterGraef 25:7d6d9fc471bf 1189 HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_FS, chnum, hhcd_USB_FS.hc[chnum].urb_state);
DieterGraef 24:5396b6a93262 1190 }
DieterGraef 24:5396b6a93262 1191
DieterGraef 24:5396b6a93262 1192 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
DieterGraef 24:5396b6a93262 1193 {
DieterGraef 24:5396b6a93262 1194 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1195 hhcd_USB_FS.hc[chnum].state = HC_XACTERR;
DieterGraef 25:7d6d9fc471bf 1196 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1197 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
DieterGraef 24:5396b6a93262 1198 }
DieterGraef 24:5396b6a93262 1199 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
DieterGraef 24:5396b6a93262 1200 {
DieterGraef 25:7d6d9fc471bf 1201 if(hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_INTR)
DieterGraef 24:5396b6a93262 1202 {
DieterGraef 24:5396b6a93262 1203 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1204 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1205 }
DieterGraef 25:7d6d9fc471bf 1206 else if (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_CTRL)
DieterGraef 24:5396b6a93262 1207 {
DieterGraef 24:5396b6a93262 1208 /* re-activate the channel */
DieterGraef 24:5396b6a93262 1209 USBx_HC(chnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
DieterGraef 24:5396b6a93262 1210 USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
DieterGraef 24:5396b6a93262 1211
DieterGraef 24:5396b6a93262 1212 }
DieterGraef 24:5396b6a93262 1213
DieterGraef 25:7d6d9fc471bf 1214 else if (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_BULK)
DieterGraef 24:5396b6a93262 1215 {
DieterGraef 24:5396b6a93262 1216 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1217 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1218 }
DieterGraef 24:5396b6a93262 1219
DieterGraef 25:7d6d9fc471bf 1220 hhcd_USB_FS.hc[chnum].state = HC_NAK;
DieterGraef 24:5396b6a93262 1221 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 24:5396b6a93262 1222 }
DieterGraef 24:5396b6a93262 1223 //
DieterGraef 24:5396b6a93262 1224 }
DieterGraef 24:5396b6a93262 1225 else
DieterGraef 24:5396b6a93262 1226 {
DieterGraef 24:5396b6a93262 1227 //
DieterGraef 24:5396b6a93262 1228 uint8_t chnum=i;
DieterGraef 25:7d6d9fc471bf 1229 USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS;
DieterGraef 24:5396b6a93262 1230
DieterGraef 24:5396b6a93262 1231 if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
DieterGraef 24:5396b6a93262 1232 {
DieterGraef 24:5396b6a93262 1233 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
DieterGraef 24:5396b6a93262 1234 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 24:5396b6a93262 1235 }
DieterGraef 24:5396b6a93262 1236 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
DieterGraef 24:5396b6a93262 1237 {
DieterGraef 24:5396b6a93262 1238 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
DieterGraef 24:5396b6a93262 1239
DieterGraef 25:7d6d9fc471bf 1240 if( hhcd_USB_FS.hc[chnum].do_ping == 1)
DieterGraef 24:5396b6a93262 1241 {
DieterGraef 25:7d6d9fc471bf 1242 hhcd_USB_FS.hc[chnum].state = HC_NYET;
DieterGraef 24:5396b6a93262 1243 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1244 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 25:7d6d9fc471bf 1245 hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 24:5396b6a93262 1246 }
DieterGraef 24:5396b6a93262 1247 }
DieterGraef 24:5396b6a93262 1248
DieterGraef 24:5396b6a93262 1249 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NYET)
DieterGraef 24:5396b6a93262 1250 {
DieterGraef 25:7d6d9fc471bf 1251 hhcd_USB_FS.hc[chnum].state = HC_NYET;
DieterGraef 24:5396b6a93262 1252 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1253 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1254 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NYET);
DieterGraef 24:5396b6a93262 1255
DieterGraef 24:5396b6a93262 1256 }
DieterGraef 24:5396b6a93262 1257
DieterGraef 24:5396b6a93262 1258 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
DieterGraef 24:5396b6a93262 1259 {
DieterGraef 24:5396b6a93262 1260 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1261 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1262 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
DieterGraef 24:5396b6a93262 1263 }
DieterGraef 24:5396b6a93262 1264
DieterGraef 24:5396b6a93262 1265 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
DieterGraef 24:5396b6a93262 1266 {
DieterGraef 24:5396b6a93262 1267 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1268 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1269 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
DieterGraef 25:7d6d9fc471bf 1270 hhcd_USB_FS.hc[chnum].state = HC_XFRC;
DieterGraef 24:5396b6a93262 1271
DieterGraef 24:5396b6a93262 1272 }
DieterGraef 24:5396b6a93262 1273
DieterGraef 24:5396b6a93262 1274 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
DieterGraef 24:5396b6a93262 1275 {
DieterGraef 24:5396b6a93262 1276 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
DieterGraef 24:5396b6a93262 1277 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1278 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 25:7d6d9fc471bf 1279 hhcd_USB_FS.hc[chnum].state = HC_STALL;
DieterGraef 24:5396b6a93262 1280 }
DieterGraef 24:5396b6a93262 1281
DieterGraef 24:5396b6a93262 1282 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
DieterGraef 24:5396b6a93262 1283 {
DieterGraef 24:5396b6a93262 1284 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1285 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 25:7d6d9fc471bf 1286 hhcd_USB_FS.hc[chnum].state = HC_NAK;
DieterGraef 24:5396b6a93262 1287 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 24:5396b6a93262 1288 }
DieterGraef 24:5396b6a93262 1289
DieterGraef 24:5396b6a93262 1290 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
DieterGraef 24:5396b6a93262 1291 {
DieterGraef 24:5396b6a93262 1292 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1293 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 25:7d6d9fc471bf 1294 hhcd_USB_FS.hc[chnum].state = HC_XACTERR;
DieterGraef 24:5396b6a93262 1295 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
DieterGraef 24:5396b6a93262 1296 }
DieterGraef 24:5396b6a93262 1297
DieterGraef 24:5396b6a93262 1298 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
DieterGraef 24:5396b6a93262 1299 {
DieterGraef 24:5396b6a93262 1300 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1301 USB_HC_Halt(USB_OTG_FS, chnum);
DieterGraef 24:5396b6a93262 1302 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 24:5396b6a93262 1303 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
DieterGraef 25:7d6d9fc471bf 1304 hhcd_USB_FS.hc[chnum].state = HC_DATATGLERR;
DieterGraef 24:5396b6a93262 1305 }
DieterGraef 24:5396b6a93262 1306
DieterGraef 24:5396b6a93262 1307
DieterGraef 24:5396b6a93262 1308 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
DieterGraef 24:5396b6a93262 1309 {
DieterGraef 24:5396b6a93262 1310 __HAL_HCD_MASK_HALT_HC_INT(chnum);
DieterGraef 24:5396b6a93262 1311
DieterGraef 25:7d6d9fc471bf 1312 if(hhcd_USB_FS.hc[chnum].state == HC_XFRC)
DieterGraef 24:5396b6a93262 1313 {
DieterGraef 25:7d6d9fc471bf 1314 hhcd_USB_FS.hc[chnum].urb_state = URB_DONE;
DieterGraef 25:7d6d9fc471bf 1315 if (hhcd_USB_FS.hc[chnum].ep_type == EP_TYPE_BULK)
DieterGraef 24:5396b6a93262 1316 {
DieterGraef 25:7d6d9fc471bf 1317 hhcd_USB_FS.hc[chnum].toggle_out ^= 1;
DieterGraef 24:5396b6a93262 1318 }
DieterGraef 24:5396b6a93262 1319 }
DieterGraef 25:7d6d9fc471bf 1320 else if (hhcd_USB_FS.hc[chnum].state == HC_NAK)
DieterGraef 24:5396b6a93262 1321 {
DieterGraef 25:7d6d9fc471bf 1322 hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 24:5396b6a93262 1323 }
DieterGraef 24:5396b6a93262 1324
DieterGraef 25:7d6d9fc471bf 1325 else if (hhcd_USB_FS.hc[chnum].state == HC_NYET)
DieterGraef 24:5396b6a93262 1326 {
DieterGraef 25:7d6d9fc471bf 1327 hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 25:7d6d9fc471bf 1328 hhcd_USB_FS.hc[chnum].do_ping = 0;
DieterGraef 24:5396b6a93262 1329 }
DieterGraef 24:5396b6a93262 1330
DieterGraef 25:7d6d9fc471bf 1331 else if (hhcd_USB_FS.hc[chnum].state == HC_STALL)
DieterGraef 24:5396b6a93262 1332 {
DieterGraef 25:7d6d9fc471bf 1333 hhcd_USB_FS.hc[chnum].urb_state = URB_STALL;
DieterGraef 24:5396b6a93262 1334 }
DieterGraef 24:5396b6a93262 1335
DieterGraef 25:7d6d9fc471bf 1336 else if(hhcd_USB_FS.hc[chnum].state == HC_XACTERR)
DieterGraef 24:5396b6a93262 1337 {
DieterGraef 25:7d6d9fc471bf 1338 hhcd_USB_FS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 24:5396b6a93262 1339 }
DieterGraef 24:5396b6a93262 1340
DieterGraef 25:7d6d9fc471bf 1341 else if (hhcd_USB_FS.hc[chnum].state == HC_DATATGLERR)
DieterGraef 24:5396b6a93262 1342 {
DieterGraef 25:7d6d9fc471bf 1343 hhcd_USB_FS.hc[chnum].urb_state = URB_ERROR;
DieterGraef 24:5396b6a93262 1344 }
DieterGraef 24:5396b6a93262 1345
DieterGraef 24:5396b6a93262 1346 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
DieterGraef 25:7d6d9fc471bf 1347 HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_FS, chnum, hhcd_USB_FS.hc[chnum].urb_state);
DieterGraef 24:5396b6a93262 1348 }
DieterGraef 24:5396b6a93262 1349 //
DieterGraef 24:5396b6a93262 1350 }
DieterGraef 24:5396b6a93262 1351 }
DieterGraef 24:5396b6a93262 1352 }
DieterGraef 24:5396b6a93262 1353
DieterGraef 25:7d6d9fc471bf 1354 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_HCINT);
DieterGraef 24:5396b6a93262 1355 }
DieterGraef 24:5396b6a93262 1356
DieterGraef 24:5396b6a93262 1357 /* Handle Rx Queue Level Interrupts */
DieterGraef 25:7d6d9fc471bf 1358 if(__HAL_HCD_GET_FLAG(&hhcd_USB_FS, USB_OTG_GINTSTS_RXFLVL))
DieterGraef 24:5396b6a93262 1359 {
DieterGraef 25:7d6d9fc471bf 1360 USB_MASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_RXFLVL);
DieterGraef 24:5396b6a93262 1361
DieterGraef 24:5396b6a93262 1362 //
DieterGraef 25:7d6d9fc471bf 1363 USB_OTG_GlobalTypeDef *USBx = USB_OTG_FS;
DieterGraef 24:5396b6a93262 1364 uint8_t channelnum =0;
DieterGraef 24:5396b6a93262 1365 uint32_t pktsts;
DieterGraef 24:5396b6a93262 1366 uint32_t pktcnt;
DieterGraef 24:5396b6a93262 1367 uint32_t temp = 0;
DieterGraef 24:5396b6a93262 1368
DieterGraef 25:7d6d9fc471bf 1369 temp = USB_OTG_FS->GRXSTSP ;
DieterGraef 24:5396b6a93262 1370 channelnum = temp & USB_OTG_GRXSTSP_EPNUM;
DieterGraef 24:5396b6a93262 1371 pktsts = (temp & USB_OTG_GRXSTSP_PKTSTS) >> 17;
DieterGraef 24:5396b6a93262 1372 pktcnt = (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
DieterGraef 24:5396b6a93262 1373
DieterGraef 24:5396b6a93262 1374 switch (pktsts)
DieterGraef 24:5396b6a93262 1375 {
DieterGraef 24:5396b6a93262 1376 case GRXSTS_PKTSTS_IN:
DieterGraef 24:5396b6a93262 1377 /* Read the data into the host buffer. */
DieterGraef 25:7d6d9fc471bf 1378 if ((pktcnt > 0) && (hhcd_USB_FS.hc[channelnum].xfer_buff != (void *)0))
DieterGraef 24:5396b6a93262 1379 {
DieterGraef 24:5396b6a93262 1380
DieterGraef 25:7d6d9fc471bf 1381 USB_ReadPacket(USB_OTG_FS, hhcd_USB_FS.hc[channelnum].xfer_buff, pktcnt);
DieterGraef 24:5396b6a93262 1382
DieterGraef 24:5396b6a93262 1383 /*manage multiple Xfer */
DieterGraef 25:7d6d9fc471bf 1384 hhcd_USB_FS.hc[channelnum].xfer_buff += pktcnt;
DieterGraef 25:7d6d9fc471bf 1385 hhcd_USB_FS.hc[channelnum].xfer_count += pktcnt;
DieterGraef 24:5396b6a93262 1386
DieterGraef 24:5396b6a93262 1387 if((USBx_HC(channelnum)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) > 0)
DieterGraef 24:5396b6a93262 1388 {
DieterGraef 24:5396b6a93262 1389 /* re-activate the channel when more packets are expected */
DieterGraef 24:5396b6a93262 1390 USBx_HC(channelnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
DieterGraef 24:5396b6a93262 1391 USBx_HC(channelnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
DieterGraef 25:7d6d9fc471bf 1392 hhcd_USB_FS.hc[channelnum].toggle_in ^= 1;
DieterGraef 24:5396b6a93262 1393 }
DieterGraef 24:5396b6a93262 1394 }
DieterGraef 24:5396b6a93262 1395 break;
DieterGraef 24:5396b6a93262 1396
DieterGraef 24:5396b6a93262 1397 case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
DieterGraef 24:5396b6a93262 1398 break;
DieterGraef 24:5396b6a93262 1399 case GRXSTS_PKTSTS_IN_XFER_COMP:
DieterGraef 24:5396b6a93262 1400 case GRXSTS_PKTSTS_CH_HALTED:
DieterGraef 24:5396b6a93262 1401 default:
DieterGraef 24:5396b6a93262 1402 break;
DieterGraef 24:5396b6a93262 1403 }
DieterGraef 24:5396b6a93262 1404 //
DieterGraef 24:5396b6a93262 1405
DieterGraef 25:7d6d9fc471bf 1406 USB_UNMASK_INTERRUPT(USB_OTG_FS, USB_OTG_GINTSTS_RXFLVL);
DieterGraef 24:5396b6a93262 1407 }
DieterGraef 24:5396b6a93262 1408 }
DieterGraef 24:5396b6a93262 1409 }
DieterGraef 24:5396b6a93262 1410
DieterGraef 25:7d6d9fc471bf 1411 void USBHALHost::usbisr_HS(void)
DieterGraef 25:7d6d9fc471bf 1412 {
DieterGraef 25:7d6d9fc471bf 1413 USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS;
DieterGraef 25:7d6d9fc471bf 1414 uint32_t i = 0 , interrupt = 0;
DieterGraef 25:7d6d9fc471bf 1415
DieterGraef 25:7d6d9fc471bf 1416 /* ensure that we are in device mode */
DieterGraef 25:7d6d9fc471bf 1417 if (USB_GetMode(USB_OTG_HS) == USB_OTG_MODE_HOST)
DieterGraef 25:7d6d9fc471bf 1418 {
DieterGraef 25:7d6d9fc471bf 1419 /* avoid spurious interrupt */
DieterGraef 25:7d6d9fc471bf 1420 if(__HAL_HCD_IS_INVALID_INTERRUPT(&hhcd_USB_HS))
DieterGraef 25:7d6d9fc471bf 1421 {
DieterGraef 25:7d6d9fc471bf 1422 return;
DieterGraef 25:7d6d9fc471bf 1423 }
DieterGraef 25:7d6d9fc471bf 1424
DieterGraef 25:7d6d9fc471bf 1425 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
DieterGraef 25:7d6d9fc471bf 1426 {
DieterGraef 25:7d6d9fc471bf 1427 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 1428 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
DieterGraef 25:7d6d9fc471bf 1429 }
DieterGraef 25:7d6d9fc471bf 1430
DieterGraef 25:7d6d9fc471bf 1431 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_IISOIXFR))
DieterGraef 25:7d6d9fc471bf 1432 {
DieterGraef 25:7d6d9fc471bf 1433 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 1434 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_IISOIXFR);
DieterGraef 25:7d6d9fc471bf 1435 }
DieterGraef 25:7d6d9fc471bf 1436
DieterGraef 25:7d6d9fc471bf 1437 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PTXFE))
DieterGraef 25:7d6d9fc471bf 1438 {
DieterGraef 25:7d6d9fc471bf 1439 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 1440 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_PTXFE);
DieterGraef 25:7d6d9fc471bf 1441 }
DieterGraef 25:7d6d9fc471bf 1442
DieterGraef 25:7d6d9fc471bf 1443 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_MMIS))
DieterGraef 25:7d6d9fc471bf 1444 {
DieterGraef 25:7d6d9fc471bf 1445 /* incorrect mode, acknowledge the interrupt */
DieterGraef 25:7d6d9fc471bf 1446 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_MMIS);
DieterGraef 25:7d6d9fc471bf 1447 }
DieterGraef 25:7d6d9fc471bf 1448
DieterGraef 25:7d6d9fc471bf 1449 /* Handle Host Disconnect Interrupts */
DieterGraef 25:7d6d9fc471bf 1450 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_DISCINT))
DieterGraef 25:7d6d9fc471bf 1451 {
DieterGraef 25:7d6d9fc471bf 1452
DieterGraef 25:7d6d9fc471bf 1453 /* Cleanup HPRT */
DieterGraef 25:7d6d9fc471bf 1454 USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
DieterGraef 25:7d6d9fc471bf 1455 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
DieterGraef 25:7d6d9fc471bf 1456
DieterGraef 25:7d6d9fc471bf 1457 /* Handle Host Port Interrupts */
DieterGraef 25:7d6d9fc471bf 1458 HAL_HCD_Disconnect_Callback(&hhcd_USB_HS);
DieterGraef 25:7d6d9fc471bf 1459 USB_InitFSLSPClkSel(USB_OTG_HS ,HCFG_48_MHZ );
DieterGraef 25:7d6d9fc471bf 1460 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 25:7d6d9fc471bf 1461 }
DieterGraef 25:7d6d9fc471bf 1462
DieterGraef 25:7d6d9fc471bf 1463 /* Handle Host Port Interrupts */
DieterGraef 25:7d6d9fc471bf 1464 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_HPRTINT))
DieterGraef 25:7d6d9fc471bf 1465 {
DieterGraef 25:7d6d9fc471bf 1466 //
DieterGraef 25:7d6d9fc471bf 1467 USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS;
DieterGraef 25:7d6d9fc471bf 1468 __IO uint32_t hprt0, hprt0_dup;
DieterGraef 25:7d6d9fc471bf 1469
DieterGraef 25:7d6d9fc471bf 1470 /* Handle Host Port Interrupts */
DieterGraef 25:7d6d9fc471bf 1471 hprt0 = USBx_HPRT0;
DieterGraef 25:7d6d9fc471bf 1472 hprt0_dup = USBx_HPRT0;
DieterGraef 25:7d6d9fc471bf 1473
DieterGraef 25:7d6d9fc471bf 1474 hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
DieterGraef 25:7d6d9fc471bf 1475 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
DieterGraef 25:7d6d9fc471bf 1476
DieterGraef 25:7d6d9fc471bf 1477 /* Check wether Port Connect Detected */
DieterGraef 25:7d6d9fc471bf 1478 if((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET)
DieterGraef 25:7d6d9fc471bf 1479 {
DieterGraef 25:7d6d9fc471bf 1480 if((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS)
DieterGraef 25:7d6d9fc471bf 1481 {
DieterGraef 25:7d6d9fc471bf 1482 USB_MASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 25:7d6d9fc471bf 1483 HAL_HCD_Connect_Callback(&hhcd_USB_HS);
DieterGraef 25:7d6d9fc471bf 1484 }
DieterGraef 25:7d6d9fc471bf 1485 hprt0_dup |= USB_OTG_HPRT_PCDET;
DieterGraef 25:7d6d9fc471bf 1486
DieterGraef 25:7d6d9fc471bf 1487 }
DieterGraef 25:7d6d9fc471bf 1488
DieterGraef 25:7d6d9fc471bf 1489 /* Check whether Port Enable Changed */
DieterGraef 25:7d6d9fc471bf 1490 if((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG)
DieterGraef 25:7d6d9fc471bf 1491 {
DieterGraef 25:7d6d9fc471bf 1492 hprt0_dup |= USB_OTG_HPRT_PENCHNG;
DieterGraef 25:7d6d9fc471bf 1493
DieterGraef 25:7d6d9fc471bf 1494 if((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA)
DieterGraef 25:7d6d9fc471bf 1495 {
DieterGraef 25:7d6d9fc471bf 1496 if(hhcd_USB_HS.Init.phy_itface == USB_OTG_EMBEDDED_PHY)
DieterGraef 25:7d6d9fc471bf 1497 {
DieterGraef 25:7d6d9fc471bf 1498 if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17))
DieterGraef 25:7d6d9fc471bf 1499 {
DieterGraef 25:7d6d9fc471bf 1500 USB_InitFSLSPClkSel(USB_OTG_HS ,HCFG_6_MHZ );
DieterGraef 25:7d6d9fc471bf 1501 }
DieterGraef 25:7d6d9fc471bf 1502 else
DieterGraef 25:7d6d9fc471bf 1503 {
DieterGraef 25:7d6d9fc471bf 1504 USB_InitFSLSPClkSel(USB_OTG_HS ,HCFG_48_MHZ );
DieterGraef 25:7d6d9fc471bf 1505 }
DieterGraef 25:7d6d9fc471bf 1506 }
DieterGraef 25:7d6d9fc471bf 1507 else
DieterGraef 25:7d6d9fc471bf 1508 {
DieterGraef 25:7d6d9fc471bf 1509 if(hhcd_USB_HS.Init.speed == HCD_SPEED_FULL)
DieterGraef 25:7d6d9fc471bf 1510 {
DieterGraef 25:7d6d9fc471bf 1511 USBx_HOST->HFIR = (uint32_t)60000;
DieterGraef 25:7d6d9fc471bf 1512 }
DieterGraef 25:7d6d9fc471bf 1513 }
DieterGraef 25:7d6d9fc471bf 1514 HAL_HCD_Connect_Callback(&hhcd_USB_HS);
DieterGraef 25:7d6d9fc471bf 1515
DieterGraef 25:7d6d9fc471bf 1516 if(hhcd_USB_HS.Init.speed == HCD_SPEED_HIGH)
DieterGraef 25:7d6d9fc471bf 1517 {
DieterGraef 25:7d6d9fc471bf 1518 USB_UNMASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 25:7d6d9fc471bf 1519 }
DieterGraef 25:7d6d9fc471bf 1520 }
DieterGraef 25:7d6d9fc471bf 1521 else
DieterGraef 25:7d6d9fc471bf 1522 {
DieterGraef 25:7d6d9fc471bf 1523 /* Cleanup HPRT */
DieterGraef 25:7d6d9fc471bf 1524 USBx_HPRT0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
DieterGraef 25:7d6d9fc471bf 1525 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
DieterGraef 25:7d6d9fc471bf 1526
DieterGraef 25:7d6d9fc471bf 1527 USB_UNMASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_DISCINT);
DieterGraef 25:7d6d9fc471bf 1528 }
DieterGraef 25:7d6d9fc471bf 1529 }
DieterGraef 25:7d6d9fc471bf 1530
DieterGraef 25:7d6d9fc471bf 1531 /* Check For an overcurrent */
DieterGraef 25:7d6d9fc471bf 1532 if((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG)
DieterGraef 25:7d6d9fc471bf 1533 {
DieterGraef 25:7d6d9fc471bf 1534 hprt0_dup |= USB_OTG_HPRT_POCCHNG;
DieterGraef 25:7d6d9fc471bf 1535 }
DieterGraef 25:7d6d9fc471bf 1536
DieterGraef 25:7d6d9fc471bf 1537 /* Clear Port Interrupts */
DieterGraef 25:7d6d9fc471bf 1538 USBx_HPRT0 = hprt0_dup;
DieterGraef 25:7d6d9fc471bf 1539 }
DieterGraef 25:7d6d9fc471bf 1540
DieterGraef 25:7d6d9fc471bf 1541
DieterGraef 25:7d6d9fc471bf 1542 //
DieterGraef 25:7d6d9fc471bf 1543
DieterGraef 25:7d6d9fc471bf 1544
DieterGraef 25:7d6d9fc471bf 1545 /* Handle Host SOF Interrupts */
DieterGraef 25:7d6d9fc471bf 1546 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_SOF))
DieterGraef 25:7d6d9fc471bf 1547 {
DieterGraef 25:7d6d9fc471bf 1548 HAL_HCD_SOF_Callback(&hhcd_USB_HS);
DieterGraef 25:7d6d9fc471bf 1549 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_SOF);
DieterGraef 25:7d6d9fc471bf 1550 }
DieterGraef 25:7d6d9fc471bf 1551
DieterGraef 25:7d6d9fc471bf 1552 /* Handle Host channel Interrupts */
DieterGraef 25:7d6d9fc471bf 1553 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_HCINT))
DieterGraef 25:7d6d9fc471bf 1554 {
DieterGraef 25:7d6d9fc471bf 1555
DieterGraef 25:7d6d9fc471bf 1556 interrupt = USB_HC_ReadInterrupt(USB_OTG_HS);
DieterGraef 25:7d6d9fc471bf 1557 for (i = 0; i < hhcd_USB_HS.Init.Host_channels ; i++)
DieterGraef 25:7d6d9fc471bf 1558 {
DieterGraef 25:7d6d9fc471bf 1559 if (interrupt & (1 << i))
DieterGraef 25:7d6d9fc471bf 1560 {
DieterGraef 25:7d6d9fc471bf 1561 if ((USBx_HC(i)->HCCHAR) & USB_OTG_HCCHAR_EPDIR)
DieterGraef 25:7d6d9fc471bf 1562 {
DieterGraef 25:7d6d9fc471bf 1563 //
DieterGraef 25:7d6d9fc471bf 1564 USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS;
DieterGraef 25:7d6d9fc471bf 1565 uint8_t chnum = i;
DieterGraef 25:7d6d9fc471bf 1566 if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
DieterGraef 25:7d6d9fc471bf 1567 {
DieterGraef 25:7d6d9fc471bf 1568 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
DieterGraef 25:7d6d9fc471bf 1569 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1570 }
DieterGraef 25:7d6d9fc471bf 1571 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
DieterGraef 25:7d6d9fc471bf 1572 {
DieterGraef 25:7d6d9fc471bf 1573 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
DieterGraef 25:7d6d9fc471bf 1574 }
DieterGraef 25:7d6d9fc471bf 1575
DieterGraef 25:7d6d9fc471bf 1576 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
DieterGraef 25:7d6d9fc471bf 1577 {
DieterGraef 25:7d6d9fc471bf 1578 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1579 hhcd_USB_HS.hc[chnum].state = HC_STALL;
DieterGraef 25:7d6d9fc471bf 1580 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1581 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
DieterGraef 25:7d6d9fc471bf 1582 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1583 }
DieterGraef 25:7d6d9fc471bf 1584 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
DieterGraef 25:7d6d9fc471bf 1585 {
DieterGraef 25:7d6d9fc471bf 1586 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1587 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1588 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1589 hhcd_USB_HS.hc[chnum].state = HC_DATATGLERR;
DieterGraef 25:7d6d9fc471bf 1590 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
DieterGraef 25:7d6d9fc471bf 1591 }
DieterGraef 25:7d6d9fc471bf 1592
DieterGraef 25:7d6d9fc471bf 1593 if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
DieterGraef 25:7d6d9fc471bf 1594 {
DieterGraef 25:7d6d9fc471bf 1595 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1596 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1597 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
DieterGraef 25:7d6d9fc471bf 1598 }
DieterGraef 25:7d6d9fc471bf 1599
DieterGraef 25:7d6d9fc471bf 1600 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
DieterGraef 25:7d6d9fc471bf 1601 {
DieterGraef 25:7d6d9fc471bf 1602
DieterGraef 25:7d6d9fc471bf 1603 if (hhcd_USB_HS.Init.dma_enable)
DieterGraef 25:7d6d9fc471bf 1604 {
DieterGraef 25:7d6d9fc471bf 1605 hhcd_USB_HS.hc[chnum].xfer_count = hhcd_USB_HS.hc[chnum].xfer_len - \
DieterGraef 25:7d6d9fc471bf 1606 (USBx_HC(chnum)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ);
DieterGraef 25:7d6d9fc471bf 1607 }
DieterGraef 25:7d6d9fc471bf 1608
DieterGraef 25:7d6d9fc471bf 1609 hhcd_USB_HS.hc[chnum].state = HC_XFRC;
DieterGraef 25:7d6d9fc471bf 1610 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
DieterGraef 24:5396b6a93262 1611
DieterGraef 24:5396b6a93262 1612
DieterGraef 25:7d6d9fc471bf 1613 if ((hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_CTRL)||
DieterGraef 25:7d6d9fc471bf 1614 (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_BULK))
DieterGraef 25:7d6d9fc471bf 1615 {
DieterGraef 25:7d6d9fc471bf 1616 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1617 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1618 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1619
DieterGraef 25:7d6d9fc471bf 1620 }
DieterGraef 25:7d6d9fc471bf 1621 else if(hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_INTR)
DieterGraef 25:7d6d9fc471bf 1622 {
DieterGraef 25:7d6d9fc471bf 1623 USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
DieterGraef 25:7d6d9fc471bf 1624 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1625 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1626 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1627 }
DieterGraef 25:7d6d9fc471bf 1628 else if(hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_ISOC)
DieterGraef 25:7d6d9fc471bf 1629 {
DieterGraef 25:7d6d9fc471bf 1630 USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
DieterGraef 25:7d6d9fc471bf 1631 hhcd_USB_HS.hc[chnum].urb_state = URB_DONE;
DieterGraef 25:7d6d9fc471bf 1632 HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_HS, chnum, hhcd_USB_HS.hc[chnum].urb_state);
DieterGraef 25:7d6d9fc471bf 1633 }
DieterGraef 25:7d6d9fc471bf 1634 hhcd_USB_HS.hc[chnum].toggle_in ^= 1;
DieterGraef 25:7d6d9fc471bf 1635
DieterGraef 25:7d6d9fc471bf 1636 }
DieterGraef 25:7d6d9fc471bf 1637 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
DieterGraef 25:7d6d9fc471bf 1638 {
DieterGraef 25:7d6d9fc471bf 1639 __HAL_HCD_MASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1640
DieterGraef 25:7d6d9fc471bf 1641 if(hhcd_USB_HS.hc[chnum].state == HC_XFRC)
DieterGraef 25:7d6d9fc471bf 1642 {
DieterGraef 25:7d6d9fc471bf 1643 hhcd_USB_HS.hc[chnum].urb_state = URB_DONE;
DieterGraef 25:7d6d9fc471bf 1644 }
DieterGraef 25:7d6d9fc471bf 1645
DieterGraef 25:7d6d9fc471bf 1646 else if (hhcd_USB_HS.hc[chnum].state == HC_NAK)
DieterGraef 25:7d6d9fc471bf 1647 {
DieterGraef 25:7d6d9fc471bf 1648 hhcd_USB_HS.hc[chnum].urb_state = URB_DONE;
DieterGraef 25:7d6d9fc471bf 1649 }
DieterGraef 25:7d6d9fc471bf 1650
DieterGraef 25:7d6d9fc471bf 1651 else if (hhcd_USB_HS.hc[chnum].state == HC_STALL)
DieterGraef 25:7d6d9fc471bf 1652 {
DieterGraef 25:7d6d9fc471bf 1653 hhcd_USB_HS.hc[chnum].urb_state = URB_STALL;
DieterGraef 25:7d6d9fc471bf 1654 }
DieterGraef 25:7d6d9fc471bf 1655
DieterGraef 25:7d6d9fc471bf 1656 else if (hhcd_USB_HS.hc[chnum].state == HC_XACTERR)
DieterGraef 25:7d6d9fc471bf 1657 {
DieterGraef 25:7d6d9fc471bf 1658 hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 25:7d6d9fc471bf 1659 }
DieterGraef 25:7d6d9fc471bf 1660
DieterGraef 25:7d6d9fc471bf 1661 else if (hhcd_USB_HS.hc[chnum].state == HC_DATATGLERR)
DieterGraef 25:7d6d9fc471bf 1662 {
DieterGraef 25:7d6d9fc471bf 1663 hhcd_USB_HS.hc[chnum].urb_state = URB_ERROR;
DieterGraef 25:7d6d9fc471bf 1664 }
DieterGraef 25:7d6d9fc471bf 1665
DieterGraef 25:7d6d9fc471bf 1666 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
DieterGraef 25:7d6d9fc471bf 1667 HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_HS, chnum, hhcd_USB_HS.hc[chnum].urb_state);
DieterGraef 25:7d6d9fc471bf 1668 }
DieterGraef 25:7d6d9fc471bf 1669
DieterGraef 25:7d6d9fc471bf 1670 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
DieterGraef 25:7d6d9fc471bf 1671 {
DieterGraef 25:7d6d9fc471bf 1672 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1673 hhcd_USB_HS.hc[chnum].state = HC_XACTERR;
DieterGraef 25:7d6d9fc471bf 1674 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1675 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
DieterGraef 25:7d6d9fc471bf 1676 }
DieterGraef 25:7d6d9fc471bf 1677 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
DieterGraef 25:7d6d9fc471bf 1678 {
DieterGraef 25:7d6d9fc471bf 1679 if(hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_INTR)
DieterGraef 25:7d6d9fc471bf 1680 {
DieterGraef 25:7d6d9fc471bf 1681 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1682 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1683 }
DieterGraef 25:7d6d9fc471bf 1684 else if (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_CTRL)
DieterGraef 25:7d6d9fc471bf 1685 {
DieterGraef 25:7d6d9fc471bf 1686 /* re-activate the channel */
DieterGraef 25:7d6d9fc471bf 1687 USBx_HC(chnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
DieterGraef 25:7d6d9fc471bf 1688 USBx_HC(chnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
DieterGraef 25:7d6d9fc471bf 1689
DieterGraef 25:7d6d9fc471bf 1690 }
DieterGraef 25:7d6d9fc471bf 1691
DieterGraef 25:7d6d9fc471bf 1692 else if (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_BULK)
DieterGraef 25:7d6d9fc471bf 1693 {
DieterGraef 25:7d6d9fc471bf 1694 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1695 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1696 }
DieterGraef 25:7d6d9fc471bf 1697
DieterGraef 25:7d6d9fc471bf 1698 hhcd_USB_HS.hc[chnum].state = HC_NAK;
DieterGraef 25:7d6d9fc471bf 1699 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1700 }
DieterGraef 25:7d6d9fc471bf 1701 //
DieterGraef 25:7d6d9fc471bf 1702 }
DieterGraef 25:7d6d9fc471bf 1703 else
DieterGraef 25:7d6d9fc471bf 1704 {
DieterGraef 25:7d6d9fc471bf 1705 //
DieterGraef 25:7d6d9fc471bf 1706 uint8_t chnum=i;
DieterGraef 25:7d6d9fc471bf 1707 USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS;
DieterGraef 25:7d6d9fc471bf 1708
DieterGraef 25:7d6d9fc471bf 1709 if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_AHBERR)
DieterGraef 25:7d6d9fc471bf 1710 {
DieterGraef 25:7d6d9fc471bf 1711 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_AHBERR);
DieterGraef 25:7d6d9fc471bf 1712 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1713 }
DieterGraef 25:7d6d9fc471bf 1714 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_ACK)
DieterGraef 25:7d6d9fc471bf 1715 {
DieterGraef 25:7d6d9fc471bf 1716 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_ACK);
DieterGraef 25:7d6d9fc471bf 1717
DieterGraef 25:7d6d9fc471bf 1718 if( hhcd_USB_HS.hc[chnum].do_ping == 1)
DieterGraef 25:7d6d9fc471bf 1719 {
DieterGraef 25:7d6d9fc471bf 1720 hhcd_USB_HS.hc[chnum].state = HC_NYET;
DieterGraef 25:7d6d9fc471bf 1721 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1722 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1723 hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 25:7d6d9fc471bf 1724 }
DieterGraef 25:7d6d9fc471bf 1725 }
DieterGraef 25:7d6d9fc471bf 1726
DieterGraef 25:7d6d9fc471bf 1727 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NYET)
DieterGraef 25:7d6d9fc471bf 1728 {
DieterGraef 25:7d6d9fc471bf 1729 hhcd_USB_HS.hc[chnum].state = HC_NYET;
DieterGraef 25:7d6d9fc471bf 1730 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1731 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1732 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NYET);
DieterGraef 25:7d6d9fc471bf 1733
DieterGraef 25:7d6d9fc471bf 1734 }
DieterGraef 25:7d6d9fc471bf 1735
DieterGraef 25:7d6d9fc471bf 1736 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_FRMOR)
DieterGraef 25:7d6d9fc471bf 1737 {
DieterGraef 25:7d6d9fc471bf 1738 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1739 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1740 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_FRMOR);
DieterGraef 25:7d6d9fc471bf 1741 }
DieterGraef 25:7d6d9fc471bf 1742
DieterGraef 25:7d6d9fc471bf 1743 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_XFRC)
DieterGraef 25:7d6d9fc471bf 1744 {
DieterGraef 25:7d6d9fc471bf 1745 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1746 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1747 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_XFRC);
DieterGraef 25:7d6d9fc471bf 1748 hhcd_USB_HS.hc[chnum].state = HC_XFRC;
DieterGraef 25:7d6d9fc471bf 1749
DieterGraef 25:7d6d9fc471bf 1750 }
DieterGraef 24:5396b6a93262 1751
DieterGraef 25:7d6d9fc471bf 1752 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_STALL)
DieterGraef 25:7d6d9fc471bf 1753 {
DieterGraef 25:7d6d9fc471bf 1754 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_STALL);
DieterGraef 25:7d6d9fc471bf 1755 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1756 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1757 hhcd_USB_HS.hc[chnum].state = HC_STALL;
DieterGraef 25:7d6d9fc471bf 1758 }
DieterGraef 25:7d6d9fc471bf 1759
DieterGraef 25:7d6d9fc471bf 1760 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_NAK)
DieterGraef 25:7d6d9fc471bf 1761 {
DieterGraef 25:7d6d9fc471bf 1762 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1763 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1764 hhcd_USB_HS.hc[chnum].state = HC_NAK;
DieterGraef 25:7d6d9fc471bf 1765 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1766 }
DieterGraef 25:7d6d9fc471bf 1767
DieterGraef 25:7d6d9fc471bf 1768 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)
DieterGraef 25:7d6d9fc471bf 1769 {
DieterGraef 25:7d6d9fc471bf 1770 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1771 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1772 hhcd_USB_HS.hc[chnum].state = HC_XACTERR;
DieterGraef 25:7d6d9fc471bf 1773 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_TXERR);
DieterGraef 25:7d6d9fc471bf 1774 }
DieterGraef 25:7d6d9fc471bf 1775
DieterGraef 25:7d6d9fc471bf 1776 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_DTERR)
DieterGraef 25:7d6d9fc471bf 1777 {
DieterGraef 25:7d6d9fc471bf 1778 __HAL_HCD_UNMASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1779 USB_HC_Halt(USB_OTG_HS, chnum);
DieterGraef 25:7d6d9fc471bf 1780 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_NAK);
DieterGraef 25:7d6d9fc471bf 1781 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_DTERR);
DieterGraef 25:7d6d9fc471bf 1782 hhcd_USB_HS.hc[chnum].state = HC_DATATGLERR;
DieterGraef 25:7d6d9fc471bf 1783 }
DieterGraef 25:7d6d9fc471bf 1784
DieterGraef 25:7d6d9fc471bf 1785
DieterGraef 25:7d6d9fc471bf 1786 else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
DieterGraef 25:7d6d9fc471bf 1787 {
DieterGraef 25:7d6d9fc471bf 1788 __HAL_HCD_MASK_HALT_HC_INT(chnum);
DieterGraef 25:7d6d9fc471bf 1789
DieterGraef 25:7d6d9fc471bf 1790 if(hhcd_USB_HS.hc[chnum].state == HC_XFRC)
DieterGraef 25:7d6d9fc471bf 1791 {
DieterGraef 25:7d6d9fc471bf 1792 hhcd_USB_HS.hc[chnum].urb_state = URB_DONE;
DieterGraef 25:7d6d9fc471bf 1793 if (hhcd_USB_HS.hc[chnum].ep_type == EP_TYPE_BULK)
DieterGraef 25:7d6d9fc471bf 1794 {
DieterGraef 25:7d6d9fc471bf 1795 hhcd_USB_HS.hc[chnum].toggle_out ^= 1;
DieterGraef 25:7d6d9fc471bf 1796 }
DieterGraef 25:7d6d9fc471bf 1797 }
DieterGraef 25:7d6d9fc471bf 1798 else if (hhcd_USB_HS.hc[chnum].state == HC_NAK)
DieterGraef 25:7d6d9fc471bf 1799 {
DieterGraef 25:7d6d9fc471bf 1800 hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 25:7d6d9fc471bf 1801 }
DieterGraef 25:7d6d9fc471bf 1802
DieterGraef 25:7d6d9fc471bf 1803 else if (hhcd_USB_HS.hc[chnum].state == HC_NYET)
DieterGraef 25:7d6d9fc471bf 1804 {
DieterGraef 25:7d6d9fc471bf 1805 hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 25:7d6d9fc471bf 1806 hhcd_USB_HS.hc[chnum].do_ping = 0;
DieterGraef 25:7d6d9fc471bf 1807 }
DieterGraef 25:7d6d9fc471bf 1808
DieterGraef 25:7d6d9fc471bf 1809 else if (hhcd_USB_HS.hc[chnum].state == HC_STALL)
DieterGraef 25:7d6d9fc471bf 1810 {
DieterGraef 25:7d6d9fc471bf 1811 hhcd_USB_HS.hc[chnum].urb_state = URB_STALL;
DieterGraef 25:7d6d9fc471bf 1812 }
DieterGraef 25:7d6d9fc471bf 1813
DieterGraef 25:7d6d9fc471bf 1814 else if(hhcd_USB_HS.hc[chnum].state == HC_XACTERR)
DieterGraef 25:7d6d9fc471bf 1815 {
DieterGraef 25:7d6d9fc471bf 1816 hhcd_USB_HS.hc[chnum].urb_state = URB_NOTREADY;
DieterGraef 25:7d6d9fc471bf 1817 }
DieterGraef 25:7d6d9fc471bf 1818
DieterGraef 25:7d6d9fc471bf 1819 else if (hhcd_USB_HS.hc[chnum].state == HC_DATATGLERR)
DieterGraef 25:7d6d9fc471bf 1820 {
DieterGraef 25:7d6d9fc471bf 1821 hhcd_USB_HS.hc[chnum].urb_state = URB_ERROR;
DieterGraef 25:7d6d9fc471bf 1822 }
DieterGraef 25:7d6d9fc471bf 1823
DieterGraef 25:7d6d9fc471bf 1824 __HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
DieterGraef 25:7d6d9fc471bf 1825 HAL_HCD_HC_NotifyURBChange_Callback(&hhcd_USB_HS, chnum, hhcd_USB_HS.hc[chnum].urb_state);
DieterGraef 25:7d6d9fc471bf 1826 }
DieterGraef 25:7d6d9fc471bf 1827 //
DieterGraef 25:7d6d9fc471bf 1828 }
DieterGraef 25:7d6d9fc471bf 1829 }
DieterGraef 25:7d6d9fc471bf 1830 }
DieterGraef 25:7d6d9fc471bf 1831
DieterGraef 25:7d6d9fc471bf 1832 __HAL_HCD_CLEAR_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_HCINT);
DieterGraef 25:7d6d9fc471bf 1833 }
DieterGraef 25:7d6d9fc471bf 1834
DieterGraef 25:7d6d9fc471bf 1835 /* Handle Rx Queue Level Interrupts */
DieterGraef 25:7d6d9fc471bf 1836 if(__HAL_HCD_GET_FLAG(&hhcd_USB_HS, USB_OTG_GINTSTS_RXFLVL))
DieterGraef 25:7d6d9fc471bf 1837 {
DieterGraef 25:7d6d9fc471bf 1838 USB_MASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_RXFLVL);
DieterGraef 25:7d6d9fc471bf 1839
DieterGraef 25:7d6d9fc471bf 1840 //
DieterGraef 25:7d6d9fc471bf 1841 USB_OTG_GlobalTypeDef *USBx = USB_OTG_HS;
DieterGraef 25:7d6d9fc471bf 1842 uint8_t channelnum =0;
DieterGraef 25:7d6d9fc471bf 1843 uint32_t pktsts;
DieterGraef 25:7d6d9fc471bf 1844 uint32_t pktcnt;
DieterGraef 25:7d6d9fc471bf 1845 uint32_t temp = 0;
DieterGraef 25:7d6d9fc471bf 1846
DieterGraef 25:7d6d9fc471bf 1847 temp = USB_OTG_HS->GRXSTSP ;
DieterGraef 25:7d6d9fc471bf 1848 channelnum = temp & USB_OTG_GRXSTSP_EPNUM;
DieterGraef 25:7d6d9fc471bf 1849 pktsts = (temp & USB_OTG_GRXSTSP_PKTSTS) >> 17;
DieterGraef 25:7d6d9fc471bf 1850 pktcnt = (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
DieterGraef 25:7d6d9fc471bf 1851
DieterGraef 25:7d6d9fc471bf 1852 switch (pktsts)
DieterGraef 25:7d6d9fc471bf 1853 {
DieterGraef 25:7d6d9fc471bf 1854 case GRXSTS_PKTSTS_IN:
DieterGraef 25:7d6d9fc471bf 1855 /* Read the data into the host buffer. */
DieterGraef 25:7d6d9fc471bf 1856 if ((pktcnt > 0) && (hhcd_USB_HS.hc[channelnum].xfer_buff != (void *)0))
DieterGraef 25:7d6d9fc471bf 1857 {
DieterGraef 25:7d6d9fc471bf 1858
DieterGraef 25:7d6d9fc471bf 1859 USB_ReadPacket(USB_OTG_HS, hhcd_USB_HS.hc[channelnum].xfer_buff, pktcnt);
DieterGraef 25:7d6d9fc471bf 1860
DieterGraef 25:7d6d9fc471bf 1861 /*manage multiple Xfer */
DieterGraef 25:7d6d9fc471bf 1862 hhcd_USB_HS.hc[channelnum].xfer_buff += pktcnt;
DieterGraef 25:7d6d9fc471bf 1863 hhcd_USB_HS.hc[channelnum].xfer_count += pktcnt;
DieterGraef 25:7d6d9fc471bf 1864
DieterGraef 25:7d6d9fc471bf 1865 if((USBx_HC(channelnum)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) > 0)
DieterGraef 25:7d6d9fc471bf 1866 {
DieterGraef 25:7d6d9fc471bf 1867 /* re-activate the channel when more packets are expected */
DieterGraef 25:7d6d9fc471bf 1868 USBx_HC(channelnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
DieterGraef 25:7d6d9fc471bf 1869 USBx_HC(channelnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
DieterGraef 25:7d6d9fc471bf 1870 hhcd_USB_HS.hc[channelnum].toggle_in ^= 1;
DieterGraef 25:7d6d9fc471bf 1871 }
DieterGraef 25:7d6d9fc471bf 1872 }
DieterGraef 25:7d6d9fc471bf 1873 break;
DieterGraef 25:7d6d9fc471bf 1874
DieterGraef 25:7d6d9fc471bf 1875 case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
DieterGraef 25:7d6d9fc471bf 1876 break;
DieterGraef 25:7d6d9fc471bf 1877 case GRXSTS_PKTSTS_IN_XFER_COMP:
DieterGraef 25:7d6d9fc471bf 1878 case GRXSTS_PKTSTS_CH_HALTED:
DieterGraef 25:7d6d9fc471bf 1879 default:
DieterGraef 25:7d6d9fc471bf 1880 break;
DieterGraef 25:7d6d9fc471bf 1881 }
DieterGraef 25:7d6d9fc471bf 1882 //
DieterGraef 25:7d6d9fc471bf 1883
DieterGraef 25:7d6d9fc471bf 1884 USB_UNMASK_INTERRUPT(USB_OTG_HS, USB_OTG_GINTSTS_RXFLVL);
DieterGraef 25:7d6d9fc471bf 1885 }
DieterGraef 25:7d6d9fc471bf 1886 }
DieterGraef 25:7d6d9fc471bf 1887 }
DieterGraef 25:7d6d9fc471bf 1888
DieterGraef 25:7d6d9fc471bf 1889 uint8_t HC_FS::slot = 0x00;
DieterGraef 25:7d6d9fc471bf 1890
DieterGraef 25:7d6d9fc471bf 1891 HC_FS::HC_FS()
DieterGraef 24:5396b6a93262 1892 {
DieterGraef 24:5396b6a93262 1893 static const int start = 1;
DieterGraef 24:5396b6a93262 1894 uint8_t mask = (1<<start);
DieterGraef 24:5396b6a93262 1895 for(int i = start; i < 8; i++, mask <<= 1)
DieterGraef 24:5396b6a93262 1896 {
DieterGraef 24:5396b6a93262 1897 if (!(slot & mask))
DieterGraef 24:5396b6a93262 1898 {
DieterGraef 24:5396b6a93262 1899 slot |= mask;
DieterGraef 24:5396b6a93262 1900 _ch = i;
DieterGraef 24:5396b6a93262 1901 return;
DieterGraef 24:5396b6a93262 1902 }
DieterGraef 24:5396b6a93262 1903 }
DieterGraef 24:5396b6a93262 1904 _ch = 0; // ERROR!!!
DieterGraef 24:5396b6a93262 1905 }
DieterGraef 24:5396b6a93262 1906
DieterGraef 25:7d6d9fc471bf 1907 HC_FS::HC_FS(int ch)
DieterGraef 24:5396b6a93262 1908 {
DieterGraef 24:5396b6a93262 1909 _ch = ch;
DieterGraef 24:5396b6a93262 1910 slot |= (1<<_ch);
DieterGraef 24:5396b6a93262 1911 }
DieterGraef 24:5396b6a93262 1912
DieterGraef 25:7d6d9fc471bf 1913 HC_FS::~HC_FS()
DieterGraef 24:5396b6a93262 1914 {
DieterGraef 24:5396b6a93262 1915 slot &= ~(1<<_ch);
DieterGraef 24:5396b6a93262 1916 }
DieterGraef 24:5396b6a93262 1917
DieterGraef 25:7d6d9fc471bf 1918 HAL_StatusTypeDef HC_FS::Init(uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps)
DieterGraef 24:5396b6a93262 1919 {
DieterGraef 24:5396b6a93262 1920 _ep_addr = epnum;
DieterGraef 24:5396b6a93262 1921 _ep_type = ep_type;
DieterGraef 25:7d6d9fc471bf 1922 return HAL_HCD_HC_Init(&hhcd_USB_FS, _ch,
DieterGraef 24:5396b6a93262 1923 epnum, dev_address, speed, ep_type, mps);
DieterGraef 24:5396b6a93262 1924 }
DieterGraef 24:5396b6a93262 1925
DieterGraef 25:7d6d9fc471bf 1926 HAL_StatusTypeDef HC_FS::SubmitRequest(uint8_t* pbuff, uint16_t length, bool setup)
DieterGraef 24:5396b6a93262 1927 {
DieterGraef 24:5396b6a93262 1928 uint8_t direction = (_ep_addr & 0x80) ? DIR_IN : DIR_OUT;
DieterGraef 24:5396b6a93262 1929 if (_ep_type == EP_TYPE_CTRL)
DieterGraef 24:5396b6a93262 1930 {
DieterGraef 25:7d6d9fc471bf 1931 HCD_HCTypeDef* hc = &hhcd_USB_FS.hc[_ch];
DieterGraef 24:5396b6a93262 1932 if (setup)
DieterGraef 24:5396b6a93262 1933 {
DieterGraef 24:5396b6a93262 1934 hc->data_pid = HC_PID_SETUP;
DieterGraef 24:5396b6a93262 1935 hc->toggle_out = 0;
DieterGraef 24:5396b6a93262 1936 }
DieterGraef 24:5396b6a93262 1937 else
DieterGraef 24:5396b6a93262 1938 {
DieterGraef 24:5396b6a93262 1939 if (direction == DIR_IN)
DieterGraef 24:5396b6a93262 1940 {
DieterGraef 24:5396b6a93262 1941 if (hc->toggle_in == 0)
DieterGraef 24:5396b6a93262 1942 {
DieterGraef 24:5396b6a93262 1943 hc->data_pid = HC_PID_DATA0;
DieterGraef 24:5396b6a93262 1944 }
DieterGraef 24:5396b6a93262 1945 else
DieterGraef 24:5396b6a93262 1946 {
DieterGraef 24:5396b6a93262 1947 hc->data_pid = HC_PID_DATA1;
DieterGraef 24:5396b6a93262 1948 }
DieterGraef 24:5396b6a93262 1949 }
DieterGraef 24:5396b6a93262 1950 else // OUT
DieterGraef 24:5396b6a93262 1951 {
DieterGraef 24:5396b6a93262 1952 if (hc->toggle_out == 0)
DieterGraef 24:5396b6a93262 1953 {
DieterGraef 24:5396b6a93262 1954 hc->data_pid = HC_PID_DATA0;
DieterGraef 24:5396b6a93262 1955 }
DieterGraef 24:5396b6a93262 1956 else
DieterGraef 24:5396b6a93262 1957 {
DieterGraef 24:5396b6a93262 1958 hc->data_pid = HC_PID_DATA1;
DieterGraef 24:5396b6a93262 1959 }
DieterGraef 24:5396b6a93262 1960 }
DieterGraef 24:5396b6a93262 1961 }
DieterGraef 24:5396b6a93262 1962 hc->xfer_buff = pbuff;
DieterGraef 24:5396b6a93262 1963 hc->xfer_len = length;
DieterGraef 24:5396b6a93262 1964 hc->urb_state = URB_IDLE;
DieterGraef 24:5396b6a93262 1965 hc->xfer_count = 0;
DieterGraef 24:5396b6a93262 1966 hc->ch_num = _ch;
DieterGraef 24:5396b6a93262 1967 hc->state = HC_IDLE;
DieterGraef 25:7d6d9fc471bf 1968
DieterGraef 25:7d6d9fc471bf 1969 return USB_HC_StartXfer(hhcd_USB_FS.Instance, hc, 0);
DieterGraef 24:5396b6a93262 1970 }
DieterGraef 25:7d6d9fc471bf 1971 return HAL_HCD_HC_SubmitRequest(&hhcd_USB_FS, _ch,
DieterGraef 24:5396b6a93262 1972 direction, _ep_type, 0, pbuff, length, 0);
DieterGraef 24:5396b6a93262 1973 }
DieterGraef 24:5396b6a93262 1974
DieterGraef 25:7d6d9fc471bf 1975 HCD_URBStateTypeDef HC_FS::GetURBState()
DieterGraef 25:7d6d9fc471bf 1976 {
DieterGraef 25:7d6d9fc471bf 1977 return HAL_HCD_HC_GetURBState(&hhcd_USB_FS, _ch);
DieterGraef 25:7d6d9fc471bf 1978 }
DieterGraef 25:7d6d9fc471bf 1979
DieterGraef 25:7d6d9fc471bf 1980 HCD_HCStateTypeDef HC_FS::GetState()
DieterGraef 25:7d6d9fc471bf 1981 {
DieterGraef 25:7d6d9fc471bf 1982 return HAL_HCD_HC_GetState(&hhcd_USB_FS, _ch);
DieterGraef 25:7d6d9fc471bf 1983 }
DieterGraef 25:7d6d9fc471bf 1984
DieterGraef 25:7d6d9fc471bf 1985 uint32_t HC_FS::GetXferCount()
DieterGraef 24:5396b6a93262 1986 {
DieterGraef 25:7d6d9fc471bf 1987 return HAL_HCD_HC_GetXferCount(&hhcd_USB_FS, _ch);
DieterGraef 25:7d6d9fc471bf 1988 }
DieterGraef 25:7d6d9fc471bf 1989
DieterGraef 25:7d6d9fc471bf 1990 void HC_FS::SetToggle(uint8_t toggle)
DieterGraef 25:7d6d9fc471bf 1991 {
DieterGraef 25:7d6d9fc471bf 1992 if (_ep_addr & 0x80) // IN
DieterGraef 25:7d6d9fc471bf 1993 {
DieterGraef 25:7d6d9fc471bf 1994 hhcd_USB_FS.hc[_ch].toggle_in = toggle;
DieterGraef 25:7d6d9fc471bf 1995 }
DieterGraef 25:7d6d9fc471bf 1996 else // OUT
DieterGraef 25:7d6d9fc471bf 1997 {
DieterGraef 25:7d6d9fc471bf 1998 hhcd_USB_FS.hc[_ch].toggle_out = toggle;
DieterGraef 25:7d6d9fc471bf 1999 }
DieterGraef 24:5396b6a93262 2000 }
DieterGraef 24:5396b6a93262 2001
DieterGraef 24:5396b6a93262 2002
DieterGraef 25:7d6d9fc471bf 2003 uint8_t HC_HS::slot = 0x00;
DieterGraef 24:5396b6a93262 2004
DieterGraef 25:7d6d9fc471bf 2005 HC_HS::HC_HS()
DieterGraef 24:5396b6a93262 2006 {
DieterGraef 25:7d6d9fc471bf 2007 static const int start = 1;
DieterGraef 25:7d6d9fc471bf 2008 uint8_t mask = (1<<start);
DieterGraef 25:7d6d9fc471bf 2009 for(int i = start; i < 8; i++, mask <<= 1)
DieterGraef 25:7d6d9fc471bf 2010 {
DieterGraef 25:7d6d9fc471bf 2011 if (!(slot & mask))
DieterGraef 25:7d6d9fc471bf 2012 {
DieterGraef 25:7d6d9fc471bf 2013 slot |= mask;
DieterGraef 25:7d6d9fc471bf 2014 _ch = i;
DieterGraef 25:7d6d9fc471bf 2015 return;
DieterGraef 25:7d6d9fc471bf 2016 }
DieterGraef 25:7d6d9fc471bf 2017 }
DieterGraef 25:7d6d9fc471bf 2018 _ch = 0; // ERROR!!!
DieterGraef 25:7d6d9fc471bf 2019 }
DieterGraef 25:7d6d9fc471bf 2020
DieterGraef 25:7d6d9fc471bf 2021 HC_HS::HC_HS(int ch)
DieterGraef 25:7d6d9fc471bf 2022 {
DieterGraef 25:7d6d9fc471bf 2023 _ch = ch;
DieterGraef 25:7d6d9fc471bf 2024 slot |= (1<<_ch);
DieterGraef 25:7d6d9fc471bf 2025 }
DieterGraef 25:7d6d9fc471bf 2026
DieterGraef 25:7d6d9fc471bf 2027 HC_HS::~HC_HS()
DieterGraef 25:7d6d9fc471bf 2028 {
DieterGraef 25:7d6d9fc471bf 2029 slot &= ~(1<<_ch);
DieterGraef 25:7d6d9fc471bf 2030 }
DieterGraef 25:7d6d9fc471bf 2031
DieterGraef 25:7d6d9fc471bf 2032 HAL_StatusTypeDef HC_HS::Init(uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps)
DieterGraef 25:7d6d9fc471bf 2033 {
DieterGraef 25:7d6d9fc471bf 2034 _ep_addr = epnum;
DieterGraef 25:7d6d9fc471bf 2035 _ep_type = ep_type;
DieterGraef 25:7d6d9fc471bf 2036 return HAL_HCD_HC_Init(&hhcd_USB_HS, _ch,
DieterGraef 25:7d6d9fc471bf 2037 epnum, dev_address, speed, ep_type, mps);
DieterGraef 24:5396b6a93262 2038 }
DieterGraef 24:5396b6a93262 2039
DieterGraef 25:7d6d9fc471bf 2040 HAL_StatusTypeDef HC_HS::SubmitRequest(uint8_t* pbuff, uint16_t length, bool setup)
DieterGraef 24:5396b6a93262 2041 {
DieterGraef 25:7d6d9fc471bf 2042 uint8_t direction = (_ep_addr & 0x80) ? DIR_IN : DIR_OUT;
DieterGraef 25:7d6d9fc471bf 2043 if (_ep_type == EP_TYPE_CTRL)
DieterGraef 25:7d6d9fc471bf 2044 {
DieterGraef 25:7d6d9fc471bf 2045 HCD_HCTypeDef* hc = &hhcd_USB_HS.hc[_ch];
DieterGraef 25:7d6d9fc471bf 2046 if (setup)
DieterGraef 25:7d6d9fc471bf 2047 {
DieterGraef 25:7d6d9fc471bf 2048 hc->data_pid = HC_PID_SETUP;
DieterGraef 25:7d6d9fc471bf 2049 hc->toggle_out = 0;
DieterGraef 25:7d6d9fc471bf 2050 }
DieterGraef 25:7d6d9fc471bf 2051 else
DieterGraef 25:7d6d9fc471bf 2052 {
DieterGraef 25:7d6d9fc471bf 2053 if (direction == DIR_IN)
DieterGraef 25:7d6d9fc471bf 2054 {
DieterGraef 25:7d6d9fc471bf 2055 if (hc->toggle_in == 0)
DieterGraef 25:7d6d9fc471bf 2056 {
DieterGraef 25:7d6d9fc471bf 2057 hc->data_pid = HC_PID_DATA0;
DieterGraef 25:7d6d9fc471bf 2058 }
DieterGraef 25:7d6d9fc471bf 2059 else
DieterGraef 25:7d6d9fc471bf 2060 {
DieterGraef 25:7d6d9fc471bf 2061 hc->data_pid = HC_PID_DATA1;
DieterGraef 25:7d6d9fc471bf 2062 }
DieterGraef 25:7d6d9fc471bf 2063 }
DieterGraef 25:7d6d9fc471bf 2064 else // OUT
DieterGraef 25:7d6d9fc471bf 2065 {
DieterGraef 25:7d6d9fc471bf 2066 if (hc->toggle_out == 0)
DieterGraef 25:7d6d9fc471bf 2067 {
DieterGraef 25:7d6d9fc471bf 2068 hc->data_pid = HC_PID_DATA0;
DieterGraef 25:7d6d9fc471bf 2069 }
DieterGraef 25:7d6d9fc471bf 2070 else
DieterGraef 25:7d6d9fc471bf 2071 {
DieterGraef 25:7d6d9fc471bf 2072 hc->data_pid = HC_PID_DATA1;
DieterGraef 25:7d6d9fc471bf 2073 }
DieterGraef 25:7d6d9fc471bf 2074 }
DieterGraef 25:7d6d9fc471bf 2075 }
DieterGraef 25:7d6d9fc471bf 2076 hc->xfer_buff = pbuff;
DieterGraef 25:7d6d9fc471bf 2077 hc->xfer_len = length;
DieterGraef 25:7d6d9fc471bf 2078 hc->urb_state = URB_IDLE;
DieterGraef 25:7d6d9fc471bf 2079 hc->xfer_count = 0;
DieterGraef 25:7d6d9fc471bf 2080 hc->ch_num = _ch;
DieterGraef 25:7d6d9fc471bf 2081 hc->state = HC_IDLE;
DieterGraef 25:7d6d9fc471bf 2082
DieterGraef 25:7d6d9fc471bf 2083 return USB_HC_StartXfer(hhcd_USB_HS.Instance, hc, 0);
DieterGraef 25:7d6d9fc471bf 2084 }
DieterGraef 25:7d6d9fc471bf 2085 return HAL_HCD_HC_SubmitRequest(&hhcd_USB_HS, _ch,
DieterGraef 25:7d6d9fc471bf 2086 direction, _ep_type, 0, pbuff, length, 0);
DieterGraef 24:5396b6a93262 2087 }
DieterGraef 24:5396b6a93262 2088
DieterGraef 25:7d6d9fc471bf 2089 HCD_URBStateTypeDef HC_HS::GetURBState()
DieterGraef 25:7d6d9fc471bf 2090 {
DieterGraef 25:7d6d9fc471bf 2091 return HAL_HCD_HC_GetURBState(&hhcd_USB_HS, _ch);
DieterGraef 25:7d6d9fc471bf 2092 }
DieterGraef 25:7d6d9fc471bf 2093
DieterGraef 25:7d6d9fc471bf 2094 HCD_HCStateTypeDef HC_HS::GetState()
DieterGraef 25:7d6d9fc471bf 2095 {
DieterGraef 25:7d6d9fc471bf 2096 return HAL_HCD_HC_GetState(&hhcd_USB_HS, _ch);
DieterGraef 25:7d6d9fc471bf 2097 }
DieterGraef 25:7d6d9fc471bf 2098
DieterGraef 25:7d6d9fc471bf 2099 uint32_t HC_HS::GetXferCount()
DieterGraef 25:7d6d9fc471bf 2100 {
DieterGraef 25:7d6d9fc471bf 2101 return HAL_HCD_HC_GetXferCount(&hhcd_USB_HS, _ch);
DieterGraef 25:7d6d9fc471bf 2102 }
DieterGraef 25:7d6d9fc471bf 2103
DieterGraef 25:7d6d9fc471bf 2104 void HC_HS::SetToggle(uint8_t toggle)
DieterGraef 24:5396b6a93262 2105 {
DieterGraef 24:5396b6a93262 2106 if (_ep_addr & 0x80) // IN
DieterGraef 24:5396b6a93262 2107 {
DieterGraef 25:7d6d9fc471bf 2108 hhcd_USB_HS.hc[_ch].toggle_in = toggle;
DieterGraef 24:5396b6a93262 2109 }
DieterGraef 24:5396b6a93262 2110 else // OUT
DieterGraef 24:5396b6a93262 2111 {
DieterGraef 25:7d6d9fc471bf 2112 hhcd_USB_HS.hc[_ch].toggle_out = toggle;
DieterGraef 24:5396b6a93262 2113 }
DieterGraef 24:5396b6a93262 2114 }
DieterGraef 24:5396b6a93262 2115
DieterGraef 25:7d6d9fc471bf 2116
DieterGraef 24:5396b6a93262 2117 #endif
DieterGraef 24:5396b6a93262 2118
DieterGraef 24:5396b6a93262 2119
DieterGraef 25:7d6d9fc471bf 2120