Skip to content

Commit 41b22bf

Browse files
authored
Merge pull request #3432 from jamike/TARGET_STM_USBHOST_FS
Target STM USBHOST support
2 parents 6af7b29 + 25c0d90 commit 41b22bf

26 files changed

+1237
-92
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/* mbed USBHost Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#if defined(TARGET_STM) && defined(USBHOST_OTHER)
17+
18+
#include "dbg.h"
19+
#include "USBEndpoint.h"
20+
extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
21+
extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
22+
23+
24+
void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
25+
{
26+
hced = hced_;
27+
type = type_;
28+
dir = dir_;
29+
setup = (type == CONTROL_ENDPOINT) ? true : false;
30+
31+
//TDs have been allocated by the host
32+
memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
33+
memset(td_list_[0], 0, sizeof(HCTD));
34+
memset(td_list_[1], 0, sizeof(HCTD));
35+
36+
td_list[0]->ep = this;
37+
td_list[1]->ep = this;
38+
39+
address = (ep_number & 0x7F) | ((dir - 1) << 7);
40+
this->size = size;
41+
this->ep_number = ep_number;
42+
transfer_len = 0;
43+
transferred = 0;
44+
buf_start = 0;
45+
nextEp = NULL;
46+
47+
td_current = td_list[0];
48+
td_next = td_list[1];
49+
50+
intf_nb = 0;
51+
52+
state = USB_TYPE_IDLE;
53+
}
54+
void USBEndpoint::setSize(uint32_t size)
55+
{
56+
this->size = size;
57+
}
58+
59+
60+
void USBEndpoint::setDeviceAddress(uint8_t addr)
61+
{
62+
this->device_address = addr;
63+
HAL_HCD_HC_Init((HCD_HandleTypeDef*)hced->hhcd,hced->ch_num, address, addr, HCD_SPEED_FULL, type, size);
64+
}
65+
66+
void USBEndpoint::setSpeed(uint8_t speed)
67+
{
68+
this->speed = speed;
69+
}
70+
71+
72+
73+
void USBEndpoint::setState(uint8_t st) {
74+
if (st > 18)
75+
return;
76+
if (state == USB_TYPE_FREE) HAL_HCD_HC_Halt((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num);
77+
78+
state = (USB_TYPE)st;
79+
}
80+
81+
82+
extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
83+
extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
84+
85+
86+
USB_TYPE USBEndpoint::queueTransfer()
87+
{
88+
HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd;
89+
uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
90+
uint32_t type = HAL_HCD_HC_GetType(hhcd, hced->ch_num);
91+
uint32_t max_size = HAL_HCD_HC_GetMaxPacket(hhcd, hced->ch_num);
92+
/* if a packet is queue on disconnected ; no solution for now */
93+
if(*addr == (uint32_t) -1) {
94+
/* set td as disconnected */
95+
td_current->state = USB_TYPE_DISCONNECTED;
96+
return USB_TYPE_DISCONNECTED;
97+
}
98+
MBED_ASSERT(*addr ==0);
99+
if ((type == EP_TYPE_BULK) || (type== EP_TYPE_CTRL)) {
100+
transfer_len = td_current->size <= max_size ? td_current->size : max_size;
101+
transferred = td_current->size;
102+
} else {
103+
transfer_len = td_current->size;
104+
transferred = td_current->size;
105+
MBED_ASSERT(transferred <= (int)max_size);
106+
}
107+
buf_start = (uint8_t *)td_current->currBufPtr;
108+
109+
//Now add this free TD at this end of the queue
110+
state = USB_TYPE_PROCESSING;
111+
/* one request */
112+
td_current->nextTD = (hcTd*)0;
113+
114+
*addr = (uint32_t)td_current;
115+
/* dir /setup is inverted for ST */
116+
/* token is useful only ctrl endpoint */
117+
/* last parameter is ping ? */
118+
MBED_ASSERT(HAL_HCD_HC_SubmitRequest((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num, dir-1, type,!setup,(uint8_t*) td_current->currBufPtr, transfer_len, 0)==HAL_OK);
119+
return USB_TYPE_PROCESSING;
120+
}
121+
122+
void USBEndpoint::unqueueTransfer(volatile HCTD * td)
123+
{
124+
125+
uint32_t *addr = &((uint32_t *)((HCD_HandleTypeDef*)hced->hhcd)->pData)[hced->ch_num];
126+
td->state=0;
127+
td->currBufPtr=0;
128+
td->size=0;
129+
td->nextTD=0;
130+
*addr = 0;
131+
td_current = td_next;
132+
td_next = td;
133+
}
134+
135+
void USBEndpoint::queueEndpoint(USBEndpoint * ed)
136+
{
137+
nextEp = ed;
138+
}
139+
#endif
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* mbed USBHost Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef USBHALHOST_DISCOF429ZI
17+
#define USBHALHOST_DISCOF429ZI
18+
19+
#define USBHAL_IRQn OTG_HS_IRQn
20+
21+
#define HCCA_SIZE sizeof(HCD_HandleTypeDef)
22+
#define ED_SIZE sizeof(HCED)
23+
#define TD_SIZE sizeof(HCTD)
24+
25+
#define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT*ED_SIZE) + (MAX_TD*TD_SIZE))
26+
/* STM device FS have 11 channels (definition is for 60 channels) */
27+
static volatile uint8_t usb_buf[TOTAL_SIZE];
28+
typedef struct
29+
{
30+
/* store the request ongoing on each endpoit */
31+
/* 1st field of structure avoid giving knowledge of all structure to
32+
* endpoint */
33+
volatile uint32_t addr[MAX_ENDPOINT];
34+
USBHALHost *inst;
35+
void (USBHALHost::*deviceConnected)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent);
36+
void (USBHALHost::*deviceDisconnected)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr);
37+
void (USBHALHost::*transferCompleted)(volatile uint32_t addr);
38+
}USBHALHost_Private_t;
39+
/* CONFIGURATION for USB_VBUS
40+
* on 64 bits board PC_0 is used (0 VBUS on, 1 VBUS off)
41+
* on 144 pins board PG_6 is used ( 1 VBUS on, 0 VBUS on)
42+
*/
43+
static gpio_t gpio_vbus;
44+
45+
#define VBUS_OFF 1
46+
#define VBUS_ON 0
47+
#define USB_VBUS_CONFIG \
48+
do {__HAL_RCC_GPIOC_CLK_ENABLE();\
49+
gpio_init_out_ex(&gpio_vbus, PC_4, VBUS_OFF);\
50+
}while(0);
51+
52+
53+
void usb_vbus( uint8_t state)
54+
{
55+
if(state == 0)
56+
{
57+
gpio_write(&gpio_vbus, VBUS_OFF);
58+
}
59+
else
60+
{
61+
gpio_write(&gpio_vbus, VBUS_ON);
62+
}
63+
wait(0.2);
64+
}
65+
66+
67+
USBHALHost::USBHALHost() {
68+
gpio_t pin_vbus;
69+
instHost = this;
70+
HCD_HandleTypeDef *hhcd;
71+
USBHALHost_Private_t *HALPriv = new(USBHALHost_Private_t);
72+
memset(HALPriv, 0, sizeof(USBHALHost_Private_t));
73+
memInit();
74+
memset((void*)usb_hcca, 0, HCCA_SIZE);
75+
hhcd = (HCD_HandleTypeDef *)usb_hcca;
76+
hhcd->Instance = USB_OTG_HS;
77+
hhcd->pData = (void*)HALPriv;
78+
hhcd->Init.Host_channels = 11;
79+
/* for now failed with dma */
80+
hhcd->Init.dma_enable = 0;
81+
hhcd->Init.speed = HCD_SPEED_HIGH;
82+
hhcd->Init.phy_itface = HCD_PHY_EMBEDDED;
83+
hhcd->Init.use_external_vbus = 1;
84+
HALPriv->inst = this;
85+
HALPriv->deviceConnected = &USBHALHost::deviceConnected;
86+
HALPriv->deviceDisconnected = &USBHALHost::deviceDisconnected;
87+
HALPriv->transferCompleted = &USBHALHost::transferCompleted;
88+
for (int i = 0; i < MAX_ENDPOINT; i++) {
89+
edBufAlloc[i] = false;
90+
HALPriv->addr[i]=(uint32_t)-1;
91+
}
92+
for (int i = 0; i < MAX_TD; i++) {
93+
tdBufAlloc[i] = false;
94+
}
95+
/* Configure USB HS GPIOs */
96+
__HAL_RCC_GPIOB_CLK_ENABLE();
97+
98+
/*USB DM and DP */
99+
pin_function(PB_14, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS));
100+
pin_function(PB_15, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS));
101+
/* Configure VBUS Pin */
102+
gpio_init_in(&pin_vbus, PB_13);
103+
/* Configure POWER_SWITCH IO pin */
104+
USB_VBUS_CONFIG;
105+
/* Enable USB HS Clocks */
106+
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
107+
108+
/* Set USBFS Interrupt priority */
109+
HAL_NVIC_SetPriority(USBHAL_IRQn, 5, 0);
110+
NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
111+
}
112+
#endif
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* Copyright (c) 2016 mbed.org, MIT License
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4+
* and associated documentation files (the "Software"), to deal in the Software without
5+
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
6+
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7+
* Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or
10+
* substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
#ifndef USBHALHOST_DISCOL476VG
19+
#define USBHALHOST_DISCOL476VG
20+
21+
#define USBHAL_IRQn OTG_FS_IRQn
22+
23+
#define HCCA_SIZE sizeof(HCD_HandleTypeDef)
24+
#define ED_SIZE sizeof(HCED)
25+
#define TD_SIZE sizeof(HCTD)
26+
27+
#define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT*ED_SIZE) + (MAX_TD*TD_SIZE))
28+
/* STM device FS have 11 channels (definition is for 60 channels) */
29+
static volatile uint8_t usb_buf[TOTAL_SIZE];
30+
typedef struct
31+
{
32+
/* store the request ongoing on each endpoit */
33+
/* 1st field of structure avoid giving knowledge of all structure to
34+
* endpoint */
35+
volatile uint32_t addr[MAX_ENDPOINT];
36+
USBHALHost *inst;
37+
void (USBHALHost::*deviceConnected)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent);
38+
void (USBHALHost::*deviceDisconnected)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr);
39+
void (USBHALHost::*transferCompleted)(volatile uint32_t addr);
40+
}USBHALHost_Private_t;
41+
42+
static gpio_t gpio_vbus;
43+
44+
#define VBUS_OFF 1
45+
#define VBUS_ON 0
46+
#define USB_VBUS_CONFIG \
47+
do {\
48+
gpio_init_out_ex(&gpio_vbus, PC_9, VBUS_OFF);\
49+
}while(0);
50+
51+
52+
void usb_vbus( uint8_t state)
53+
{
54+
if(state == 0)
55+
{
56+
gpio_write(&gpio_vbus, VBUS_OFF);
57+
}
58+
else
59+
{
60+
gpio_write(&gpio_vbus, VBUS_ON);
61+
}
62+
wait(0.2);
63+
}
64+
65+
66+
USBHALHost::USBHALHost() {
67+
instHost = this;
68+
HCD_HandleTypeDef *hhcd;
69+
USBHALHost_Private_t *HALPriv = new(USBHALHost_Private_t);
70+
memset(HALPriv, 0, sizeof(USBHALHost_Private_t));
71+
memInit();
72+
memset((void*)usb_hcca, 0, HCCA_SIZE);
73+
hhcd = (HCD_HandleTypeDef *)usb_hcca;
74+
hhcd->Instance = USB_OTG_FS;
75+
hhcd->pData = (void*)HALPriv;
76+
hhcd->Init.Host_channels = 11;
77+
/* for now failed with dma */
78+
hhcd->Init.dma_enable = 0;
79+
hhcd->Init.speed = HCD_SPEED_FULL;
80+
hhcd->Init.phy_itface = HCD_PHY_EMBEDDED;
81+
hhcd->Init.use_external_vbus = 1;
82+
HALPriv->inst = this;
83+
HALPriv->deviceConnected = &USBHALHost::deviceConnected;
84+
HALPriv->deviceDisconnected = &USBHALHost::deviceDisconnected;
85+
HALPriv->transferCompleted = &USBHALHost::transferCompleted;
86+
for (int i = 0; i < MAX_ENDPOINT; i++) {
87+
edBufAlloc[i] = false;
88+
HALPriv->addr[i]=(uint32_t)-1;
89+
}
90+
for (int i = 0; i < MAX_TD; i++) {
91+
tdBufAlloc[i] = false;
92+
}
93+
__HAL_RCC_PWR_CLK_ENABLE();
94+
#ifdef TARGET_STM32L4
95+
HAL_PWREx_EnableVddUSB();
96+
#endif
97+
98+
/* Configure USB HS GPIOs */
99+
__HAL_RCC_GPIOA_CLK_ENABLE();
100+
101+
/*USB DM and DP */
102+
pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
103+
pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
104+
105+
/* Configure VBUS Pin */
106+
__HAL_RCC_GPIOC_CLK_ENABLE();
107+
pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
108+
/* Configure POWER_SWITCH IO pin */
109+
USB_VBUS_CONFIG;
110+
__HAL_RCC_SYSCFG_CLK_ENABLE();
111+
112+
/* Enable USB FS Clocks */
113+
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
114+
/* Set USBFS Interrupt priority */
115+
HAL_NVIC_SetPriority(USBHAL_IRQn, 5, 0);
116+
NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
117+
}
118+
#endif

0 commit comments

Comments
 (0)