Skip to content

Enable support for this library Arduino Giga #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/USBHost/USBHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void USBHost::usb_process()
}
}

USBHost::USBHost() : usbThread(osPriorityNormal, USB_THREAD_STACK)
USBHost::USBHost() : usbThread(osPriorityHigh, USB_THREAD_STACK)
{
#ifndef USBHOST_OTHER
headControlEndpoint = NULL;
Expand Down Expand Up @@ -1202,6 +1202,7 @@ USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType,
{
osEvent event = control->ep_queue.get(TD_TIMEOUT_CTRL);
if (event.status == osEventTimeout) {
USB_DBG_TRANSFER("TIMEOUT");
disableList(CONTROL_ENDPOINT);
control->setState(USB_TYPE_ERROR);
control->ep_queue.get(0);
Expand Down
2 changes: 1 addition & 1 deletion src/USBHost/USBHostTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ enum ENDPOINT_TYPE {

#else

#define TD_TIMEOUT_CTRL 600
#define TD_TIMEOUT_CTRL 1000
#define TD_TIMEOUT 2000
#define TD_SETUP (uint32_t)(0) // Direction of Setup Packet
#define TD_IN (uint32_t)(0x00100000) // Direction In
Expand Down
12 changes: 11 additions & 1 deletion src/USBHostHID/USBHostKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,15 @@ bool USBHostKeyboard::connect()
for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if ((dev = host->getDevice(i)) != NULL) {

if (host->enumerate(dev, this)) {
printf("got device\n");
int ret = host->enumerate(dev, this);
printf("ret: %d\n", ret);
if (ret) {
break;
}

printf("after enumerate\n");

if (keyboard_device_found) {
{
/* As this is done in a specific thread
Expand Down Expand Up @@ -183,6 +188,10 @@ void USBHostKeyboard::rxHandler()

/*virtual*/ bool USBHostKeyboard::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
{
printf("intf_class: %d\n", intf_class);
printf("intf_subclass: %d\n", intf_subclass);
printf("intf_protocol: %d\n", intf_protocol);

if ((keyboard_intf == -1) &&
(intf_class == HID_CLASS) &&
(intf_subclass == 0x01) &&
Expand All @@ -195,6 +204,7 @@ void USBHostKeyboard::rxHandler()

/*virtual*/ bool USBHostKeyboard::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
{
printf("intf_nb: %d\n", intf_nb);
if (intf_nb == keyboard_intf) {
if (type == INTERRUPT_ENDPOINT && dir == IN) {
keyboard_device_found = true;
Expand Down
11 changes: 6 additions & 5 deletions src/targets/TARGET_STM/USBEndpoint_STM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void USBEndpoint::init(HCED *hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_
state = USB_TYPE_IDLE;
speed = false;
}

void USBEndpoint::setSize(uint32_t size)
{
this->size = size;
Expand All @@ -80,7 +81,7 @@ void USBEndpoint::setDeviceAddress(uint8_t addr)
if (this->speed) {
USB_WARN("small speed device on hub not supported");
}
MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, addr, hcd_speed, type, size) != HAL_BUSY);
HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, addr, hcd_speed, type, size);;
this->device_address = addr;

}
Expand All @@ -106,21 +107,21 @@ void USBEndpoint::setState(USB_TYPE st)
if ((*addr) && (type != INTERRUPT_ENDPOINT)) {
this->ep_queue.put((uint8_t *)1);
}
MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num) != HAL_BUSY);
HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);
HAL_HCD_DisableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);
*addr = 0;

}
if (st == USB_TYPE_ERROR) {
MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num) != HAL_BUSY);
HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);
HAL_HCD_DisableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);

}
if (st == USB_TYPE_ERROR) {
uint8_t hcd_speed = HCD_SPEED_FULL;
/* small speed device with hub not supported
if (this->speed) hcd_speed = HCD_SPEED_LOW;*/
MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, 0, hcd_speed, type, size) != HAL_BUSY);
HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, 0, hcd_speed, type, size);
}
}

Expand Down Expand Up @@ -157,7 +158,7 @@ USB_TYPE USBEndpoint::queueTransfer()
/* 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);
HAL_HCD_HC_SubmitRequest((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, dir - 1, type, !setup, (uint8_t *) td_current->currBufPtr, transfer_len, 1);
HAL_HCD_EnableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);

return USB_TYPE_PROCESSING;
Expand Down
17 changes: 12 additions & 5 deletions src/targets/TARGET_STM/USBHALHost_STM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@

#ifdef TARGET_STM

#if defined(TARGET_PORTENTA_H7)
#define USBx_BASE USB2_OTG_FS_PERIPH_BASE
#elif defined(TARGET_GIGA)
#define USBx_BASE USB1_OTG_HS_PERIPH_BASE
#else
#define USBx_BASE USB1_OTG_HS_PERIPH_BASE
#endif

#include "mbed.h"
#include "USBHost/USBHALHost.h"
#include "USBHost/dbg.h"
#include "pinmap.h"
#include "mbed_chrono.h"

#include "USBHALHost_STM.h"

Expand Down Expand Up @@ -94,7 +101,7 @@ void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum,
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, !td->setup, (uint8_t *) td->currBufPtr, length, 0) == HAL_OK);
HAL_HCD_HC_SubmitRequest(hhcd, chnum, dir, type, !td->setup, (uint8_t *) td->currBufPtr, length, 0);
HAL_HCD_EnableInt(hhcd, chnum);
return;
}
Expand All @@ -109,7 +116,7 @@ void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum,
td->retry++;
#endif
length = td->size <= max_size ? td->size : max_size;
MBED_ASSERT(HAL_HCD_HC_SubmitRequest(hhcd, chnum, dir, type, !td->setup, (uint8_t *) td->currBufPtr, length, 0) == HAL_OK);
HAL_HCD_HC_SubmitRequest(hhcd, chnum, dir, type, !td->setup, (uint8_t *) td->currBufPtr, length, 0);
HAL_HCD_EnableInt(hhcd, chnum);
return;
#if defined(MAX_NYET_RETRY)
Expand Down Expand Up @@ -152,13 +159,12 @@ 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);
control_disable = 0;
HAL_HCD_Start((HCD_HandleTypeDef *) usb_hcca);
NVIC_EnableIRQ(USBHAL_IRQn);
usb_vbus(1);
}

Expand Down Expand Up @@ -273,11 +279,12 @@ void USBHALHost::freeTD(volatile uint8_t *td)
tdBufAlloc[i] = false;
}

using namespace mbed::chrono_literals;

void USBHALHost::resetRootHub()
{
// Initiate port reset
rtos::ThisThread::sleep_for(0.2);
rtos::ThisThread::sleep_for(200);
HAL_HCD_ResetPort((HCD_HandleTypeDef *)usb_hcca);
}

Expand Down
51 changes: 49 additions & 2 deletions src/targets/TARGET_STM/USBHALHost_STM.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#endif
#endif

#if defined(TARGET_DISCO_F746NG_HS) || defined(TARGET_DISCO_F769NI)
#if defined(TARGET_DISCO_F746NG_HS) || defined(TARGET_DISCO_F769NI) || defined(TARGET_GIGA)
#define USBHAL_IRQn OTG_HS_IRQn
#else
#define USBHAL_IRQn OTG_FS_IRQn
Expand All @@ -38,6 +38,8 @@

#define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT * ED_SIZE) + (MAX_TD * TD_SIZE))

#define MAX_NYET_RETRY 5

/* STM device FS have 11 channels (definition is for 60 channels) */
static volatile uint8_t usb_buf[TOTAL_SIZE];

Expand Down Expand Up @@ -100,6 +102,11 @@ static gpio_t gpio_powerpin;
#define USB_POWER_OFF 1
#define USB_POWERPIN_CONFIG {}

#elif defined(TARGET_GIGA)
#define USB_POWER_ON 1
#define USB_POWER_OFF 0
#define USB_POWERPIN_CONFIG {__HAL_RCC_GPIOA_CLK_ENABLE();gpio_init_out_ex(&gpio_powerpin, PA_15, USB_POWER_OFF);}

#elif defined(TARGET_DISCO_F769NI)
#define USB_POWER_ON 0
#define USB_POWER_OFF 1
Expand Down Expand Up @@ -136,7 +143,7 @@ void usb_vbus(uint8_t state)
} else {
/* The board does not have GPIO pin to control usb supply */
}
rtos::ThisThread::sleep_for(0.2);
rtos::ThisThread::sleep_for(1.0f);
}


Expand All @@ -157,6 +164,34 @@ USBHALHost::USBHALHost()
hhcd->Instance = USB_OTG_HS;
hhcd->Init.speed = HCD_SPEED_HIGH;
hhcd->Init.phy_itface = HCD_PHY_ULPI;

#elif defined(TARGET_GIGA)
hhcd->Instance = USB_OTG_HS;
hhcd->Init.speed = HCD_SPEED_FULL;
hhcd->Init.phy_itface = USB_OTG_EMBEDDED_PHY;

#if 1
/* Configure the clock recovery system (CRS) ********************************/
/* Enable CRS Clock */
__CRS_CLK_ENABLE();

static RCC_CRSInitTypeDef RCC_CRSInitStruct;

/* Default Synchro Signal division factor (not divided) */
RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;
/* Set the SYNCSRC[1:0] bits according to CRS_Source value */
RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB2;
/* HSI48 is synchronized with USB SOF at 1KHz rate */
RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_CALCULATE_RELOADVALUE(48000000, 1000);
RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT;
/* Set the TRIM[5:0] to the default value */
RCC_CRSInitStruct.HSI48CalibrationValue = 0x1D;
/* Start automatic synchronization */
HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);

HAL_RCCEx_CRSWaitSynchronization(1000);
#endif

#else
hhcd->Instance = USB_OTG_FS;
hhcd->Init.speed = HCD_SPEED_FULL;
Expand All @@ -169,6 +204,7 @@ USBHALHost::USBHALHost()
hhcd->Init.Sof_enable = 0;
hhcd->Init.vbus_sensing_enable = 0;
hhcd->Init.use_external_vbus = 1;
hhcd->Init.battery_charging_enable = 0;
hhcd->Init.lpm_enable = 0;

HALPriv->inst = this;
Expand Down Expand Up @@ -224,6 +260,11 @@ USBHALHost::USBHALHost()
__HAL_RCC_GPIOJ_CLK_ENABLE();
pin_function(PJ_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // VBUS

#elif defined(TARGET_GIGA)
__HAL_RCC_GPIOB_CLK_ENABLE();
pin_function(PB_14, STM_PIN_DEFINE_SPEED(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG2_FS, GPIO_SPEED_FREQ_VERY_HIGH)); // DM
pin_function(PB_15, STM_PIN_DEFINE_SPEED(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG2_FS, GPIO_SPEED_FREQ_VERY_HIGH)); // DM

#elif defined(TARGET_DISCO_F746NG_HS)
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
Expand Down Expand Up @@ -288,6 +329,8 @@ USBHALHost::USBHALHost()
#error "USB pins are not configured !"
#endif

NVIC_DisableIRQ(USBHAL_IRQn);

// Configure USB POWER pin
USB_POWERPIN_CONFIG;

Expand All @@ -297,6 +340,10 @@ USBHALHost::USBHALHost()
#if defined(TARGET_DISCO_F746NG_HS) || defined(TARGET_DISCO_F769NI)
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
#elif defined(TARGET_GIGA)
__HAL_RCC_USB_OTG_HS_ULPI_CLK_SLEEP_DISABLE();
__HAL_RCC_USB_OTG_HS_CLK_SLEEP_ENABLE();
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
#else
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
#endif
Expand Down