mirror of https://github.com/ARMmbed/mbed-os.git
USBHOST STM support :
NUCLEO : F429ZI, F446ZE, F207ZG, F767ZI, F401RE, DISCO : F429ZIpull/3432/head
parent
8af69dcbd6
commit
14153dcbc3
|
@ -0,0 +1,139 @@
|
|||
/* mbed USBHost Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#if defined(TARGET_STM) && defined(USBHOST_OTHER)
|
||||
|
||||
#include "dbg.h"
|
||||
#include "USBEndpoint.h"
|
||||
extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
|
||||
extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
|
||||
|
||||
|
||||
void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
|
||||
{
|
||||
hced = hced_;
|
||||
type = type_;
|
||||
dir = dir_;
|
||||
setup = (type == CONTROL_ENDPOINT) ? true : false;
|
||||
|
||||
//TDs have been allocated by the host
|
||||
memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
|
||||
memset(td_list_[0], 0, sizeof(HCTD));
|
||||
memset(td_list_[1], 0, sizeof(HCTD));
|
||||
|
||||
td_list[0]->ep = this;
|
||||
td_list[1]->ep = this;
|
||||
|
||||
address = (ep_number & 0x7F) | ((dir - 1) << 7);
|
||||
this->size = size;
|
||||
this->ep_number = ep_number;
|
||||
transfer_len = 0;
|
||||
transferred = 0;
|
||||
buf_start = 0;
|
||||
nextEp = NULL;
|
||||
|
||||
td_current = td_list[0];
|
||||
td_next = td_list[1];
|
||||
|
||||
intf_nb = 0;
|
||||
|
||||
state = USB_TYPE_IDLE;
|
||||
}
|
||||
void USBEndpoint::setSize(uint32_t size)
|
||||
{
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
|
||||
void USBEndpoint::setDeviceAddress(uint8_t addr)
|
||||
{
|
||||
this->device_address = addr;
|
||||
HAL_HCD_HC_Init((HCD_HandleTypeDef*)hced->hhcd,hced->ch_num, address, addr, HCD_SPEED_FULL, type, size);
|
||||
}
|
||||
|
||||
void USBEndpoint::setSpeed(uint8_t speed)
|
||||
{
|
||||
this->speed = speed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void USBEndpoint::setState(uint8_t st) {
|
||||
if (st > 18)
|
||||
return;
|
||||
if (state == USB_TYPE_FREE) HAL_HCD_HC_Halt((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num);
|
||||
|
||||
state = (USB_TYPE)st;
|
||||
}
|
||||
|
||||
|
||||
extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
|
||||
extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
|
||||
|
||||
|
||||
USB_TYPE USBEndpoint::queueTransfer()
|
||||
{
|
||||
HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd;
|
||||
uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
|
||||
uint32_t type = HAL_HCD_HC_GetType(hhcd, hced->ch_num);
|
||||
uint32_t max_size = HAL_HCD_HC_GetMaxPacket(hhcd, hced->ch_num);
|
||||
/* if a packet is queue on disconnected ; no solution for now */
|
||||
if(*addr == (uint32_t) -1) {
|
||||
/* set td as disconnected */
|
||||
td_current->state = USB_TYPE_DISCONNECTED;
|
||||
return USB_TYPE_DISCONNECTED;
|
||||
}
|
||||
MBED_ASSERT(*addr ==0);
|
||||
if ((type == EP_TYPE_BULK) || (type== EP_TYPE_CTRL)) {
|
||||
transfer_len = td_current->size <= max_size ? td_current->size : max_size;
|
||||
transferred = td_current->size;
|
||||
} else {
|
||||
transfer_len = td_current->size;
|
||||
transferred = td_current->size;
|
||||
MBED_ASSERT(transferred <= (int)max_size);
|
||||
}
|
||||
buf_start = (uint8_t *)td_current->currBufPtr;
|
||||
|
||||
//Now add this free TD at this end of the queue
|
||||
state = USB_TYPE_PROCESSING;
|
||||
/* one request */
|
||||
td_current->nextTD = (hcTd*)0;
|
||||
|
||||
*addr = (uint32_t)td_current;
|
||||
/* dir /setup is inverted for ST */
|
||||
/* token is useful only ctrl endpoint */
|
||||
/* last parameter is ping ? */
|
||||
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);
|
||||
return USB_TYPE_PROCESSING;
|
||||
}
|
||||
|
||||
void USBEndpoint::unqueueTransfer(volatile HCTD * td)
|
||||
{
|
||||
|
||||
uint32_t *addr = &((uint32_t *)((HCD_HandleTypeDef*)hced->hhcd)->pData)[hced->ch_num];
|
||||
td->state=0;
|
||||
td->currBufPtr=0;
|
||||
td->size=0;
|
||||
td->nextTD=0;
|
||||
*addr = 0;
|
||||
td_current = td_next;
|
||||
td_next = td;
|
||||
}
|
||||
|
||||
void USBEndpoint::queueEndpoint(USBEndpoint * ed)
|
||||
{
|
||||
nextEp = ed;
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,112 @@
|
|||
/* mbed USBHost Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef USBHALHOST_DISCOF429ZI
|
||||
#define USBHALHOST_DISCOF429ZI
|
||||
|
||||
#define USBHAL_IRQn OTG_HS_IRQn
|
||||
|
||||
#define HCCA_SIZE sizeof(HCD_HandleTypeDef)
|
||||
#define ED_SIZE sizeof(HCED)
|
||||
#define TD_SIZE sizeof(HCTD)
|
||||
|
||||
#define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT*ED_SIZE) + (MAX_TD*TD_SIZE))
|
||||
/* STM device FS have 11 channels (definition is for 60 channels) */
|
||||
static volatile uint8_t usb_buf[TOTAL_SIZE];
|
||||
typedef struct
|
||||
{
|
||||
/* store the request ongoing on each endpoit */
|
||||
/* 1st field of structure avoid giving knowledge of all structure to
|
||||
* endpoint */
|
||||
volatile uint32_t addr[MAX_ENDPOINT];
|
||||
USBHALHost *inst;
|
||||
void (USBHALHost::*deviceConnected)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent);
|
||||
void (USBHALHost::*deviceDisconnected)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr);
|
||||
void (USBHALHost::*transferCompleted)(volatile uint32_t addr);
|
||||
}USBHALHost_Private_t;
|
||||
/* CONFIGURATION for USB_VBUS
|
||||
* on 64 bits board PC_0 is used (0 VBUS on, 1 VBUS off)
|
||||
* on 144 pins board PG_6 is used ( 1 VBUS on, 0 VBUS on)
|
||||
*/
|
||||
static gpio_t gpio_vbus;
|
||||
|
||||
#define VBUS_OFF 1
|
||||
#define VBUS_ON 0
|
||||
#define USB_VBUS_CONFIG \
|
||||
do {__HAL_RCC_GPIOC_CLK_ENABLE();\
|
||||
gpio_init_out_ex(&gpio_vbus, PC_4, VBUS_OFF);\
|
||||
}while(0);
|
||||
|
||||
|
||||
void usb_vbus( uint8_t state)
|
||||
{
|
||||
if(state == 0)
|
||||
{
|
||||
gpio_write(&gpio_vbus, VBUS_OFF);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_write(&gpio_vbus, VBUS_ON);
|
||||
}
|
||||
wait(0.2);
|
||||
}
|
||||
|
||||
|
||||
USBHALHost::USBHALHost() {
|
||||
gpio_t pin_vbus;
|
||||
instHost = this;
|
||||
HCD_HandleTypeDef *hhcd;
|
||||
USBHALHost_Private_t *HALPriv = new(USBHALHost_Private_t);
|
||||
memset(HALPriv, 0, sizeof(USBHALHost_Private_t));
|
||||
memInit();
|
||||
memset((void*)usb_hcca, 0, HCCA_SIZE);
|
||||
hhcd = (HCD_HandleTypeDef *)usb_hcca;
|
||||
hhcd->Instance = USB_OTG_HS;
|
||||
hhcd->pData = (void*)HALPriv;
|
||||
hhcd->Init.Host_channels = 11;
|
||||
/* for now failed with dma */
|
||||
hhcd->Init.dma_enable = 0;
|
||||
hhcd->Init.speed = HCD_SPEED_HIGH;
|
||||
hhcd->Init.phy_itface = HCD_PHY_EMBEDDED;
|
||||
hhcd->Init.use_external_vbus = 1;
|
||||
HALPriv->inst = this;
|
||||
HALPriv->deviceConnected = &USBHALHost::deviceConnected;
|
||||
HALPriv->deviceDisconnected = &USBHALHost::deviceDisconnected;
|
||||
HALPriv->transferCompleted = &USBHALHost::transferCompleted;
|
||||
for (int i = 0; i < MAX_ENDPOINT; i++) {
|
||||
edBufAlloc[i] = false;
|
||||
HALPriv->addr[i]=(uint32_t)-1;
|
||||
}
|
||||
for (int i = 0; i < MAX_TD; i++) {
|
||||
tdBufAlloc[i] = false;
|
||||
}
|
||||
/* Configure USB HS GPIOs */
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
/*USB DM and DP */
|
||||
pin_function(PB_14, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS));
|
||||
pin_function(PB_15, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS));
|
||||
/* Configure VBUS Pin */
|
||||
gpio_init_in(&pin_vbus, PB_13);
|
||||
/* Configure POWER_SWITCH IO pin */
|
||||
USB_VBUS_CONFIG;
|
||||
/* Enable USB HS Clocks */
|
||||
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
|
||||
|
||||
/* Set USBFS Interrupt priority */
|
||||
HAL_NVIC_SetPriority(USBHAL_IRQn, 5, 0);
|
||||
NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,233 @@
|
|||
/* mbed USBHost Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifdef TARGET_STM
|
||||
#include "mbed.h"
|
||||
#include "USBHALHost.h"
|
||||
#include "dbg.h"
|
||||
#include "pinmap.h"
|
||||
|
||||
#include "USBHALHost_STM_TARGET.h"
|
||||
|
||||
void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
|
||||
{
|
||||
USBHALHost_Private_t *priv=(USBHALHost_Private_t *)(hhcd->pData);
|
||||
USBHALHost *obj= priv->inst;
|
||||
int i;
|
||||
void (USBHALHost::*func)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent ) = priv->deviceConnected;
|
||||
for (i=0; i<MAX_ENDPOINT; i++)
|
||||
if (priv->addr[i]==(uint32_t)-1)priv->addr[i]=0;
|
||||
(obj->*func)(0,1,1,NULL);
|
||||
}
|
||||
void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
|
||||
{
|
||||
USBHALHost_Private_t *priv=(USBHALHost_Private_t *)(hhcd->pData);
|
||||
USBHALHost *obj= priv->inst;
|
||||
void (USBHALHost::*func1)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr)= priv->deviceDisconnected;
|
||||
void (USBHALHost::*func2)(volatile uint32_t addr)= priv->transferCompleted;
|
||||
int i;
|
||||
(obj->*func1)(0,1,(USBHostHub *)NULL,0);
|
||||
|
||||
/* fix me call with same frame number */
|
||||
/* all on going transaction must end and any new one must be rejected */
|
||||
for (i=0; i<MAX_ENDPOINT; i++) {
|
||||
uint32_t addr = priv->addr[i];
|
||||
priv->addr[i]=(uint32_t)-1;
|
||||
if ((addr!=(uint32_t)-1)&& (addr!=0)){
|
||||
HCTD *td = (HCTD *)addr;
|
||||
td->currBufPtr +=HAL_HCD_HC_GetXferCount(hhcd, i);
|
||||
td->state = USB_TYPE_DISCONNECTED;
|
||||
(obj->*func2)(addr);
|
||||
}
|
||||
}
|
||||
for (i=1; i< MAX_ENDPOINT;i++)HAL_HCD_HC_Halt(hhcd,i);
|
||||
}
|
||||
int HAL_HCD_HC_GetDirection(HCD_HandleTypeDef *hhcd,uint8_t chnum)
|
||||
{
|
||||
/* useful for transmission */
|
||||
return hhcd->hc[chnum].ep_is_in;
|
||||
}
|
||||
|
||||
uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd,uint8_t chnum)
|
||||
{
|
||||
/* useful for transmission */
|
||||
return hhcd->hc[chnum].max_packet;
|
||||
}
|
||||
|
||||
|
||||
uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd,uint8_t chnum)
|
||||
{
|
||||
/* useful for transmission */
|
||||
return hhcd->hc[chnum].ep_type;
|
||||
}
|
||||
|
||||
void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd,uint8_t chnum, HCD_URBStateTypeDef urb_state)
|
||||
{
|
||||
USBHALHost_Private_t *priv=(USBHALHost_Private_t *)(hhcd->pData);
|
||||
USBHALHost *obj= priv->inst;
|
||||
void (USBHALHost::*func)(volatile uint32_t addr)= priv->transferCompleted;
|
||||
uint32_t addr = priv->addr[chnum];
|
||||
uint32_t max_size = HAL_HCD_HC_GetMaxPacket(hhcd, chnum);
|
||||
uint32_t type = HAL_HCD_HC_GetType(hhcd, chnum);
|
||||
uint32_t dir = HAL_HCD_HC_GetDirection(hhcd,chnum);
|
||||
uint32_t length;
|
||||
if ((addr!=(uint32_t)-1) && (addr!=0)) {
|
||||
HCTD *td = (HCTD *)addr;
|
||||
/* put the state */
|
||||
if ((urb_state == URB_IDLE) && (type == EP_TYPE_INTR) ) {
|
||||
length = td->size;
|
||||
MBED_ASSERT(HAL_HCD_HC_SubmitRequest(hhcd, chnum, dir ,type , 1,(uint8_t*) td->currBufPtr, length, 0)==HAL_OK);
|
||||
return;
|
||||
}
|
||||
td->state = (urb_state == URB_DONE) ? USB_TYPE_IDLE : USB_TYPE_ERROR;
|
||||
if (urb_state == URB_NOTREADY)
|
||||
USB_ERR("urb_state != URB_NOTREADY");
|
||||
/* move buffer pointer , for size */
|
||||
if ((type != EP_TYPE_BULK) && (type != EP_TYPE_CTRL )) {
|
||||
/* in packet */
|
||||
} else {
|
||||
if (urb_state == URB_DONE) {
|
||||
if (td->size > max_size) {
|
||||
/* enqueue another request */
|
||||
td->currBufPtr += max_size;
|
||||
td->size -= max_size;
|
||||
length = td->size <= max_size ? td->size : max_size;
|
||||
MBED_ASSERT(HAL_HCD_HC_SubmitRequest(hhcd, chnum, dir ,type , 1,(uint8_t*) td->currBufPtr, length, 0)==HAL_OK);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
td->state = (urb_state == URB_DONE) ? USB_TYPE_IDLE : USB_TYPE_ERROR;
|
||||
td->currBufPtr +=HAL_HCD_HC_GetXferCount(hhcd, chnum);
|
||||
priv->addr[chnum]=0;
|
||||
(obj->*func)(addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
USB_DBG_EVENT("spurious %d %d",chnum, urb_state);
|
||||
}
|
||||
}
|
||||
USBHALHost * USBHALHost::instHost;
|
||||
|
||||
|
||||
void USBHALHost::init() {
|
||||
|
||||
NVIC_DisableIRQ(USBHAL_IRQn);
|
||||
NVIC_SetVector(USBHAL_IRQn, (uint32_t)(_usbisr));
|
||||
HAL_HCD_Init((HCD_HandleTypeDef *) usb_hcca);
|
||||
NVIC_EnableIRQ(USBHAL_IRQn);
|
||||
HAL_HCD_Start((HCD_HandleTypeDef *) usb_hcca);
|
||||
usb_vbus(1);
|
||||
}
|
||||
|
||||
uint32_t USBHALHost::controlHeadED() {
|
||||
return 0xffffffff;
|
||||
}
|
||||
|
||||
uint32_t USBHALHost::bulkHeadED() {
|
||||
return 0xffffffff;
|
||||
}
|
||||
|
||||
uint32_t USBHALHost::interruptHeadED() {
|
||||
return 0xffffffff;
|
||||
}
|
||||
|
||||
void USBHALHost::updateBulkHeadED(uint32_t addr) {
|
||||
}
|
||||
|
||||
|
||||
void USBHALHost::updateControlHeadED(uint32_t addr) {
|
||||
}
|
||||
|
||||
void USBHALHost::updateInterruptHeadED(uint32_t addr) {
|
||||
}
|
||||
|
||||
|
||||
void USBHALHost::enableList(ENDPOINT_TYPE type) {
|
||||
}
|
||||
|
||||
|
||||
bool USBHALHost::disableList(ENDPOINT_TYPE type) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void USBHALHost::memInit() {
|
||||
usb_hcca = (volatile HCD_HandleTypeDef *)usb_buf;
|
||||
usb_edBuf = usb_buf + HCCA_SIZE;
|
||||
usb_tdBuf = usb_buf + HCCA_SIZE +(MAX_ENDPOINT*ED_SIZE);
|
||||
/* init channel */
|
||||
for (int i=0; i < MAX_ENDPOINT; i++) {
|
||||
HCED *hced = (HCED*)(usb_edBuf + i*ED_SIZE);
|
||||
hced->ch_num = i;
|
||||
hced->hhcd = (HCCA *) usb_hcca;
|
||||
}
|
||||
}
|
||||
|
||||
volatile uint8_t * USBHALHost::getED() {
|
||||
for (int i = 0; i < MAX_ENDPOINT; i++) {
|
||||
if ( !edBufAlloc[i] ) {
|
||||
edBufAlloc[i] = true;
|
||||
return (volatile uint8_t *)(usb_edBuf + i*ED_SIZE);
|
||||
}
|
||||
}
|
||||
perror("Could not allocate ED\r\n");
|
||||
return NULL; //Could not alloc ED
|
||||
}
|
||||
|
||||
volatile uint8_t * USBHALHost::getTD() {
|
||||
int i;
|
||||
for (i = 0; i < MAX_TD; i++) {
|
||||
if ( !tdBufAlloc[i] ) {
|
||||
tdBufAlloc[i] = true;
|
||||
return (volatile uint8_t *)(usb_tdBuf + i*TD_SIZE);
|
||||
}
|
||||
}
|
||||
perror("Could not allocate TD\r\n");
|
||||
return NULL; //Could not alloc TD
|
||||
}
|
||||
|
||||
|
||||
void USBHALHost::freeED(volatile uint8_t * ed) {
|
||||
int i;
|
||||
i = (ed - usb_edBuf) / ED_SIZE;
|
||||
edBufAlloc[i] = false;
|
||||
}
|
||||
|
||||
void USBHALHost::freeTD(volatile uint8_t * td) {
|
||||
int i;
|
||||
i = (td - usb_tdBuf) / TD_SIZE;
|
||||
tdBufAlloc[i] = false;
|
||||
}
|
||||
|
||||
|
||||
void USBHALHost::resetRootHub() {
|
||||
// Initiate port reset
|
||||
wait(0.2);
|
||||
HAL_HCD_ResetPort((HCD_HandleTypeDef *)usb_hcca);
|
||||
}
|
||||
|
||||
|
||||
void USBHALHost::_usbisr(void) {
|
||||
if (instHost) {
|
||||
instHost->UsbIrqhandler();
|
||||
}
|
||||
}
|
||||
|
||||
void USBHALHost::UsbIrqhandler() {
|
||||
HAL_HCD_IRQHandler((HCD_HandleTypeDef *)usb_hcca);
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,116 @@
|
|||
/* mbed USBHost Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef USBHALHOST_STM32_144_64
|
||||
#define USBHALHOST_STM32_144_64
|
||||
|
||||
#define USBHAL_IRQn OTG_FS_IRQn
|
||||
|
||||
#define HCCA_SIZE sizeof(HCD_HandleTypeDef)
|
||||
#define ED_SIZE sizeof(HCED)
|
||||
#define TD_SIZE sizeof(HCTD)
|
||||
|
||||
#define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT*ED_SIZE) + (MAX_TD*TD_SIZE))
|
||||
/* STM device FS have 11 channels (definition is for 60 channels) */
|
||||
static volatile uint8_t usb_buf[TOTAL_SIZE];
|
||||
typedef struct
|
||||
{
|
||||
/* store the request ongoing on each endpoit */
|
||||
/* 1st field of structure avoid giving knowledge of all structure to
|
||||
* endpoint */
|
||||
volatile uint32_t addr[MAX_ENDPOINT];
|
||||
USBHALHost *inst;
|
||||
void (USBHALHost::*deviceConnected)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent);
|
||||
void (USBHALHost::*deviceDisconnected)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr);
|
||||
void (USBHALHost::*transferCompleted)(volatile uint32_t addr);
|
||||
}USBHALHost_Private_t;
|
||||
|
||||
/* CONFIGURATION for USB_VBUS
|
||||
* on 64 bits board PC_0 is used (0 VBUS on, 1 VBUS off)
|
||||
* on 144 pins board PG_6 is used ( 1 VBUS on, 0 VBUS on)
|
||||
*/
|
||||
static gpio_t gpio_vbus;
|
||||
|
||||
#if defined(USBHALHOST_64pins)
|
||||
#define VBUS_OFF 1
|
||||
#define VBUS_ON 0
|
||||
#define USB_VBUS_CONFIG \
|
||||
do {__HAL_RCC_GPIOC_CLK_ENABLE();\
|
||||
gpio_init_out_ex(&gpio_vbus, PC_0, VBUS_OFF);\
|
||||
}while(0);
|
||||
#else
|
||||
#define VBUS_OFF 0
|
||||
#define VBUS_ON 1
|
||||
#define USB_VBUS_CONFIG \
|
||||
do {__HAL_RCC_GPIOG_CLK_ENABLE();\
|
||||
gpio_init_out_ex(&gpio_vbus, PG_6, VBUS_OFF);\
|
||||
}while(0);
|
||||
#endif
|
||||
|
||||
void usb_vbus( uint8_t state)
|
||||
{
|
||||
if(state == 0)
|
||||
{
|
||||
gpio_write(&gpio_vbus, VBUS_OFF);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_write(&gpio_vbus, VBUS_ON);
|
||||
}
|
||||
wait(0.2);
|
||||
}
|
||||
|
||||
USBHALHost::USBHALHost() {
|
||||
instHost = this;
|
||||
HCD_HandleTypeDef *hhcd;
|
||||
USBHALHost_Private_t *HALPriv = new(USBHALHost_Private_t);
|
||||
memset(HALPriv, 0, sizeof(USBHALHost_Private_t));
|
||||
memInit();
|
||||
memset((void*)usb_hcca, 0, HCCA_SIZE);
|
||||
hhcd = (HCD_HandleTypeDef *)usb_hcca;
|
||||
hhcd->Instance = USB_OTG_FS;
|
||||
hhcd->pData = (void*)HALPriv;
|
||||
hhcd->Init.Host_channels = 11;
|
||||
hhcd->Init.speed = HCD_SPEED_FULL;
|
||||
hhcd->Init.phy_itface = HCD_PHY_EMBEDDED;
|
||||
HALPriv->inst = this;
|
||||
HALPriv->deviceConnected = &USBHALHost::deviceConnected;
|
||||
HALPriv->deviceDisconnected = &USBHALHost::deviceDisconnected;
|
||||
HALPriv->transferCompleted = &USBHALHost::transferCompleted;
|
||||
for (int i = 0; i < MAX_ENDPOINT; i++) {
|
||||
edBufAlloc[i] = false;
|
||||
HALPriv->addr[i]=(uint32_t)-1;
|
||||
}
|
||||
for (int i = 0; i < MAX_TD; i++) {
|
||||
tdBufAlloc[i] = false;
|
||||
}
|
||||
/* Configure USB FS GPIOs */
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
/*USB DM and DP */
|
||||
pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
|
||||
pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
|
||||
/*USB ID */
|
||||
pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS));
|
||||
|
||||
/* Configure POWER_SWITCH IO pin */
|
||||
USB_VBUS_CONFIG;
|
||||
/* Enable USB FS Clocks */
|
||||
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
|
||||
|
||||
/* Set USBFS Interrupt priority */
|
||||
HAL_NVIC_SetPriority(OTG_FS_IRQn, 6, 0);
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,35 @@
|
|||
/* Copyright (c) 2016 mbed.org, MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/* 144 pins boards */
|
||||
#if defined(TARGET_NUCLEO_F429ZI) || defined(TARGET_NUCLEO_F446ZE) || defined(TARGET_NUCLEO_F207ZG) \
|
||||
|| defined(TARGET_NUCLEO_F767ZI)
|
||||
#include "USBHALHost_STM_144_64pins.h"
|
||||
#endif
|
||||
|
||||
/* 64 pins boards */
|
||||
#if defined(TARGET_NUCLEO_F401RE)
|
||||
#define USBHALHOST_64pins
|
||||
#include "USBHALHost_STM_144_64pins.h"
|
||||
#endif
|
||||
|
||||
/* DISCO board */
|
||||
|
||||
#ifdef TARGET_DISCO_F429ZI
|
||||
#include "USBHALHost_DISCOF429ZI.h"
|
||||
#endif
|
|
@ -16,7 +16,70 @@
|
|||
|
||||
#ifndef USBHOST_CONF_H
|
||||
#define USBHOST_CONF_H
|
||||
#if defined(TARGET_STM)
|
||||
/*
|
||||
* Maximum number of devices that can be connected
|
||||
* to the usb host
|
||||
*/
|
||||
/* hub + 2 devices */
|
||||
#define MAX_DEVICE_CONNECTED 3
|
||||
|
||||
/*
|
||||
* Maximum of Hub connected to the usb host
|
||||
*/
|
||||
#define MAX_HUB_NB 0
|
||||
|
||||
/*
|
||||
* Maximum number of ports on a USB hub
|
||||
*/
|
||||
#define MAX_HUB_PORT 2
|
||||
|
||||
/*
|
||||
* Enable USBHostMSD
|
||||
*/
|
||||
#define USBHOST_MSD 1
|
||||
|
||||
/*
|
||||
* Enable USBHostKeyboard
|
||||
*/
|
||||
#define USBHOST_KEYBOARD 1
|
||||
|
||||
/*
|
||||
* Enable USBHostMouse
|
||||
*/
|
||||
#define USBHOST_MOUSE 1
|
||||
|
||||
/*
|
||||
* Enable USBHostSerial or USBHostMultiSerial (if set > 1)
|
||||
*/
|
||||
#define USBHOST_SERIAL 1
|
||||
|
||||
/*
|
||||
* Enable USB3Gmodule
|
||||
*/
|
||||
#define USBHOST_3GMODULE 1
|
||||
|
||||
/*
|
||||
* Enable USB MIDI
|
||||
*/
|
||||
#define USBHOST_MIDI 1
|
||||
|
||||
/*
|
||||
* Maximum number of interfaces of a usb device
|
||||
*/
|
||||
#define MAX_INTF 2
|
||||
|
||||
/*
|
||||
* Maximum number of endpoints on each interface
|
||||
*/
|
||||
#define MAX_ENDPOINT_PER_INTERFACE 2
|
||||
|
||||
/*
|
||||
* Maximum number of endpoint descriptors that can be allocated
|
||||
*/
|
||||
#define MAX_ENDPOINT 11 /* USB FS 11 channel */
|
||||
|
||||
#else
|
||||
/*
|
||||
* Maximum number of devices that can be connected
|
||||
* to the usb host
|
||||
|
@ -77,7 +140,7 @@
|
|||
* Maximum number of endpoint descriptors that can be allocated
|
||||
*/
|
||||
#define MAX_ENDPOINT (MAX_DEVICE_CONNECTED * MAX_INTF * MAX_ENDPOINT_PER_INTERFACE)
|
||||
|
||||
#endif
|
||||
/*
|
||||
* Maximum number of transfer descriptors that can be allocated
|
||||
*/
|
||||
|
@ -86,6 +149,6 @@
|
|||
/*
|
||||
* usb_thread stack size
|
||||
*/
|
||||
#define USB_THREAD_STACK (256*4 + MAX_HUB_NB*256*4)
|
||||
#define USB_THREAD_STACK (256*4 + 2*256*4)
|
||||
|
||||
#endif
|
||||
|
|
|
@ -734,7 +734,7 @@
|
|||
"supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"],
|
||||
"inherits": ["Target"],
|
||||
"detect_code": ["0835"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USBHOST_OTHER"],
|
||||
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
|
||||
"features": ["LWIP"],
|
||||
"release_versions": ["2", "5"],
|
||||
|
@ -816,7 +816,7 @@
|
|||
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
|
||||
"inherits": ["Target"],
|
||||
"detect_code": ["0720"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USB_STM_HAL"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USB_STM_HAL", "USBHOST_OTHER"],
|
||||
"device_has": ["ANALOGIN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
|
||||
"release_versions": ["2", "5"],
|
||||
"device_name": "STM32F401RE"
|
||||
|
@ -882,7 +882,7 @@
|
|||
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xx", "F429_F439"],
|
||||
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
|
||||
"progen": {"target": "nucleo-f429zi"},
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USB_STM_HAL"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USB_STM_HAL", "USBHOST_OTHER"],
|
||||
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
|
||||
"detect_code": ["0796"],
|
||||
"features": ["LWIP"],
|
||||
|
@ -925,7 +925,7 @@
|
|||
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
|
||||
"inherits": ["Target"],
|
||||
"detect_code": ["0778"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USB_STM_HAL"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USB_STM_HAL", "USBHOST_OTHER"],
|
||||
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
|
||||
"release_versions": ["2", "5"],
|
||||
"device_name" : "STM32F446ZE"
|
||||
|
@ -977,7 +977,7 @@
|
|||
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
|
||||
"default_toolchain": "ARM",
|
||||
"supported_form_factors": ["ARDUINO"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2"],
|
||||
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2", "USBHOST_OTHER"],
|
||||
"detect_code": ["0818"],
|
||||
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
|
||||
"features": ["LWIP"],
|
||||
|
@ -1168,7 +1168,7 @@
|
|||
"core": "Cortex-M4F",
|
||||
"default_toolchain": "ARM",
|
||||
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xx"],
|
||||
"macros": ["RTC_LSI=1","TRANSACTION_QUEUE_SIZE_SPI=2"],
|
||||
"macros": ["RTC_LSI=1","TRANSACTION_QUEUE_SIZE_SPI=2", "USBHOST_OTHER"],
|
||||
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
|
||||
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
|
||||
"release_versions": ["2", "5"],
|
||||
|
|
Loading…
Reference in New Issue