Skip to content

Commit 956d4b3

Browse files
committed
[USB] Generic Device config
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent f10c645 commit 956d4b3

File tree

11 files changed

+152
-2778
lines changed

11 files changed

+152
-2778
lines changed

variants/ARMED_V1/usb/usbd_conf.c renamed to cores/arduino/stm32/usbd_conf.c

+52-65
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,31 @@
11
/**
22
******************************************************************************
3-
* @file USB_Device/HID_Standalone/Src/usbd_conf.c
3+
* @file usbd_conf.c
44
* @author MCD Application Team
5-
* @version V1.0.2
6-
* @date 06-May-2016
7-
* @brief This file implements the USB Device library callbacks and MSP
5+
* @brief USB Device configuration and interface file
86
******************************************************************************
97
* @attention
108
*
11-
* <h2><center>&copy; Copyright � 2016 STMicroelectronics International N.V.
9+
* <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
1210
* All rights reserved.</center></h2>
1311
*
14-
* Redistribution and use in source and binary forms, with or without
15-
* modification, are permitted, provided that the following conditions are met:
16-
*
17-
* 1. Redistribution of source code must retain the above copyright notice,
18-
* this list of conditions and the following disclaimer.
19-
* 2. Redistributions in binary form must reproduce the above copyright notice,
20-
* this list of conditions and the following disclaimer in the documentation
21-
* and/or other materials provided with the distribution.
22-
* 3. Neither the name of STMicroelectronics nor the names of other
23-
* contributors to this software may be used to endorse or promote products
24-
* derived from this software without specific written permission.
25-
* 4. This software, including modifications and/or derivative works of this
26-
* software, must execute solely and exclusively on microcontroller or
27-
* microprocessor devices manufactured by or for STMicroelectronics.
28-
* 5. Redistribution and use of this software other than as permitted under
29-
* this license is void and will automatically terminate your rights under
30-
* this license.
31-
*
32-
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
33-
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
34-
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
35-
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
36-
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
37-
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38-
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
40-
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41-
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43-
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12+
* This software component is licensed by ST under Ultimate Liberty license
13+
* SLA0044, the "License"; You may not use this file except in compliance with
14+
* the License. You may obtain a copy of the License at:
15+
* http://www.st.com/SLA0044
4416
*
4517
******************************************************************************
4618
*/
4719
#ifdef USBCON
4820
/* Includes ------------------------------------------------------------------*/
49-
#include "usbd_conf.h"
5021
#include "usbd_core.h"
51-
#include "hw_config.h"
52-
5322
/* Private typedef -----------------------------------------------------------*/
5423
/* Private define ------------------------------------------------------------*/
5524
/* Private macro -------------------------------------------------------------*/
5625
/* Private variables ---------------------------------------------------------*/
5726
PCD_HandleTypeDef g_hpcd;
58-
5927
/* Private function prototypes -----------------------------------------------*/
60-
//static void SystemClockConfig_STOP(void);
61-
6228
/* Private functions ---------------------------------------------------------*/
63-
6429
/*******************************************************************************
6530
PCD BSP Routines
6631
*******************************************************************************/
@@ -74,6 +39,9 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
7439
{
7540
GPIO_InitTypeDef GPIO_InitStruct;
7641

42+
/* Enable USB power on Pwrctrl CR2 register */
43+
HAL_PWREx_EnableVddUSB();
44+
7745
/* Configure USB FS GPIOs */
7846
__HAL_RCC_GPIOA_CLK_ENABLE();
7947

@@ -85,27 +53,44 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
8553
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
8654
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
8755

88-
/* Enable USB FS Clocks */
56+
/* Configure VBUS Pin */
57+
GPIO_InitStruct.Pin = GPIO_PIN_9;
58+
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
59+
GPIO_InitStruct.Pull = GPIO_NOPULL;
60+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
61+
62+
/* Configure ID pin */
63+
GPIO_InitStruct.Pin = GPIO_PIN_10;
64+
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
65+
GPIO_InitStruct.Pull = GPIO_PULLUP;
66+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
67+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
68+
69+
/* Configure power enable pin (USB_OTG_FS_PWR_EN) */
70+
__HAL_RCC_GPIOD_CLK_ENABLE();
71+
GPIO_InitStruct.Pin = GPIO_PIN_12;
72+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
73+
GPIO_InitStruct.Pull = GPIO_PULLUP;
74+
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
75+
76+
/* USB power output is disabled in device mode */
77+
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
78+
79+
/* Enable USB FS Clock */
8980
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
9081

91-
/* Set USBFS Interrupt priority */
82+
/* Set USB FS Interrupt priority */
9283
HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
9384

94-
/* Enable USBFS Interrupt */
85+
/* Enable USB FS Interrupt */
9586
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
9687

9788
if(hpcd->Init.low_power_enable == 1)
9889
{
99-
/* Enable EXTI Line 18 for USB wakeup*/
90+
/* Enable EXTI Line 18 for USB wakeup */
10091
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
10192
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_RISING_EDGE();
10293
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_IT();
103-
104-
/* Set EXTI Wakeup Interrupt priority*/
105-
HAL_NVIC_SetPriority(OTG_FS_WKUP_IRQn, 0, 0);
106-
107-
/* Enable EXTI Interrupt */
108-
HAL_NVIC_EnableIRQ(OTG_FS_WKUP_IRQn);
10994
}
11095
}
11196

@@ -117,7 +102,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
117102
void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
118103
{
119104
UNUSED(hpcd);
120-
105+
121106
/* Disable USB FS Clock */
122107
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
123108
__HAL_RCC_SYSCFG_CLK_DISABLE();
@@ -178,6 +163,7 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
178163
{
179164
USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
180165

166+
#if defined (USB_OTG_HS)
181167
/* Set USB Current Speed */
182168
switch(hpcd->Init.speed)
183169
{
@@ -193,7 +179,7 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
193179
speed = USBD_SPEED_FULL;
194180
break;
195181
}
196-
182+
#endif
197183
/* Reset Device */
198184
USBD_LL_Reset(hpcd->pData);
199185

@@ -210,7 +196,7 @@ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
210196
__HAL_PCD_GATE_PHYCLOCK(hpcd);
211197
USBD_LL_Suspend(hpcd->pData);
212198

213-
/*Enter in STOP mode */
199+
/*Enter in STOP mode */
214200
if (hpcd->Init.low_power_enable)
215201
{
216202
/* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */
@@ -225,6 +211,7 @@ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
225211
*/
226212
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
227213
{
214+
__HAL_PCD_UNGATE_PHYCLOCK(hpcd);
228215
USBD_LL_Resume(hpcd->pData);
229216
}
230217

@@ -306,11 +293,9 @@ void OTG_FS_WKUP_IRQHandler(void)
306293
/* Clear EXTI pending Bit*/
307294
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
308295
}
309-
310296
/*******************************************************************************
311297
LL Driver Interface (USB Device Library --> PCD)
312298
*******************************************************************************/
313-
314299
/**
315300
* @brief Initializes the Low Level portion of the Device driver.
316301
* @param pdev: Device handle
@@ -320,27 +305,30 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
320305
{
321306
/* Set LL Driver parameters */
322307
g_hpcd.Instance = USB_OTG_FS;
323-
g_hpcd.Init.dev_endpoints = 4;
308+
g_hpcd.Init.dev_endpoints = 3;
324309
g_hpcd.Init.use_dedicated_ep1 = 0;
325-
g_hpcd.Init.ep0_mps = DEP0CTL_MPS_64; //0x40;
310+
g_hpcd.Init.ep0_mps = DEP0CTL_MPS_64;
326311
g_hpcd.Init.dma_enable = 0;
327312
g_hpcd.Init.low_power_enable = 0;
313+
g_hpcd.Init.lpm_enable = 0;
314+
g_hpcd.Init.battery_charging_enable = 0;
328315
g_hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
329316
g_hpcd.Init.Sof_enable = 0;
330317
g_hpcd.Init.speed = PCD_SPEED_FULL;
331-
g_hpcd.Init.vbus_sensing_enable = 0;
318+
g_hpcd.Init.vbus_sensing_enable = 1;
332319
g_hpcd.Init.lpm_enable = 0;
320+
g_hpcd.Init.use_external_vbus = 0;
333321
/* Link The driver to the stack */
334322
g_hpcd.pData = pdev;
335323
pdev->pData = &g_hpcd;
336-
337324
/* Initialize LL Driver */
338325
HAL_PCD_Init(&g_hpcd);
339326

327+
/* configure EPs FIFOs */
340328
HAL_PCDEx_SetRxFiFo(&g_hpcd, 0x80);
341329
HAL_PCDEx_SetTxFiFo(&g_hpcd, 0, 0x40);
342330
HAL_PCDEx_SetTxFiFo(&g_hpcd, 1, 0x80);
343-
331+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 2, 0x80);
344332
return USBD_OK;
345333
}
346334

@@ -394,7 +382,6 @@ USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev,
394382
ep_addr,
395383
ep_mps,
396384
ep_type);
397-
398385
return USBD_OK;
399386
}
400387

@@ -532,6 +519,6 @@ void USBD_LL_Delay(uint32_t Delay)
532519
{
533520
HAL_Delay(Delay);
534521
}
535-
536-
#endif // USBCON
522+
#endif /* USBCON */
537523
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
524+

cores/arduino/stm32/usbd_conf.h

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
******************************************************************************
3+
* @file usbd_conf.h
4+
* @author MCD Application Team
5+
* @brief Header file for the usbd_conf.c file
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
10+
* All rights reserved.</center></h2>
11+
*
12+
* This software component is licensed by ST under Ultimate Liberty license
13+
* SLA0044, the "License"; You may not use this file except in compliance with
14+
* the License. You may obtain a copy of the License at:
15+
* http://www.st.com/SLA0044
16+
*
17+
******************************************************************************
18+
*/
19+
20+
/* Define to prevent recursive inclusion -------------------------------------*/
21+
#ifndef __USBD_CONF_H
22+
#define __USBD_CONF_H
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
#ifdef USBCON
29+
/* Includes ------------------------------------------------------------------*/
30+
#include "stm32_def.h"
31+
#include <stdio.h>
32+
#include <stdlib.h>
33+
#include <string.h>
34+
35+
#define USBD_MAX_NUM_INTERFACES 2U
36+
#define USBD_MAX_NUM_CONFIGURATION 1U
37+
#define USBD_MAX_STR_DESC_SIZ 0x100U
38+
#define USBD_SUPPORT_USER_STRING 0U
39+
#define USBD_SELF_POWERED 1U
40+
#define USBD_DEBUG_LEVEL 0U
41+
42+
/* MSC Class Config */
43+
#define MSC_MEDIA_PACKET 8192U
44+
45+
/* CDC Class Config */
46+
#define USBD_CDC_INTERVAL 2000U
47+
48+
/* DFU Class Config */
49+
#define USBD_DFU_MAX_ITF_NUM 1U
50+
#define USBD_DFU_XFERS_IZE 1024U
51+
52+
/* AUDIO Class Config */
53+
#define USBD_AUDIO_FREQ 22100U
54+
55+
/* Memory management macros */
56+
#define USBD_malloc malloc
57+
#define USBD_free free
58+
#define USBD_memset memset
59+
#define USBD_memcpy memcpy
60+
61+
/* DEBUG macros */
62+
#if (USBD_DEBUG_LEVEL > 0U)
63+
#define USBD_UsrLog(...) do { \
64+
printf(__VA_ARGS__); \
65+
printf("\n"); \
66+
} while (0)
67+
#else
68+
#define USBD_UsrLog(...) do {} while (0)
69+
#endif
70+
71+
#if (USBD_DEBUG_LEVEL > 1U)
72+
73+
#define USBD_ErrLog(...) do { \
74+
printf("ERROR: ") ; \
75+
printf(__VA_ARGS__); \
76+
printf("\n"); \
77+
} while (0)
78+
#else
79+
#define USBD_ErrLog(...) do {} while (0)
80+
#endif
81+
82+
#if (USBD_DEBUG_LEVEL > 2U)
83+
#define USBD_DbgLog(...) do { \
84+
printf("DEBUG : ") ; \
85+
printf(__VA_ARGS__); \
86+
printf("\n"); \
87+
} while (0)
88+
#else
89+
#define USBD_DbgLog(...) do {} while (0)
90+
#endif
91+
92+
#endif /* USBCON */
93+
94+
#ifdef __cplusplus
95+
}
96+
#endif
97+
98+
#endif /* __USBD_CONF_H */
99+
100+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)