Skip to content

Commit 865a592

Browse files
bcostmadbridge
authored andcommitted
DISCO_L475VG_IOT: Add USB Device files
1 parent 8b297a4 commit 865a592

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 USBHAL_STM32L475VG
19+
#define USBHAL_STM32L475VG
20+
21+
#define USBHAL_IRQn OTG_FS_IRQn
22+
23+
24+
#define NB_ENDPOINT 4
25+
/* must be multiple of 4 bytes */
26+
#define MAXTRANSFER_SIZE 0x200
27+
#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
28+
#if (FIFO_USB_RAM_SIZE > 0x500)
29+
#error "FIFO dimensioning incorrect"
30+
#endif
31+
32+
typedef struct
33+
{
34+
USBHAL *inst;
35+
void (USBHAL::*bus_reset)(void);
36+
void (USBHAL::*sof)(int frame);
37+
void (USBHAL::*connect_change)(unsigned int connected);
38+
void (USBHAL::*suspend_change)(unsigned int suspended);
39+
void (USBHAL::*ep0_setup)(void);
40+
void (USBHAL::*ep0_in)(void);
41+
void (USBHAL::*ep0_out)(void);
42+
void (USBHAL::*ep0_read)(void);
43+
bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
44+
bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
45+
uint8_t epComplete[8];
46+
/* memorize dummy buffer used for reception */
47+
uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
48+
uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
49+
}USBHAL_Private_t;
50+
51+
uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
52+
{
53+
uint32_t len;
54+
if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16;
55+
else
56+
len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16;
57+
return len*4;
58+
}
59+
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
60+
USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
61+
USBHAL *obj= priv->inst;
62+
USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
63+
uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8;
64+
void (USBHAL::*func)(int frame) = priv->sof;
65+
/* fix me call with same frame number */
66+
(obj->*func)(sofnum);
67+
}
68+
69+
USBHAL * USBHAL::instance;
70+
71+
USBHAL::USBHAL(void) {
72+
/* init parameter */
73+
USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
74+
/* initialized all field of init including 0 field */
75+
/* constructor does not fill with zero */
76+
hpcd.Instance = USB_OTG_FS;
77+
/* initialized all field of init including 0 field */
78+
/* constructor does not fill with zero */
79+
memset(&hpcd.Init, 0, sizeof(hpcd.Init));
80+
hpcd.Init.dev_endpoints = NB_ENDPOINT;
81+
hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
82+
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
83+
hpcd.Init.Sof_enable = 1;
84+
hpcd.Init.speed = PCD_SPEED_FULL;
85+
/* pass instance for usage inside call back */
86+
HALPriv->inst = this;
87+
HALPriv->bus_reset = &USBHAL::busReset;
88+
HALPriv->suspend_change = &USBHAL::suspendStateChanged;
89+
HALPriv->connect_change = &USBHAL::connectStateChanged;
90+
HALPriv->sof = &USBHAL::SOF;
91+
HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
92+
HALPriv->ep_realise = &USBHAL::realiseEndpoint;
93+
HALPriv->ep0_in = &USBHAL::EP0in;
94+
HALPriv->ep0_out = &USBHAL::EP0out;
95+
HALPriv->ep0_read = &USBHAL::EP0read;
96+
hpcd.pData = (void*)HALPriv;
97+
HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
98+
HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
99+
HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
100+
HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
101+
HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
102+
HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
103+
instance = this;
104+
105+
__HAL_RCC_PWR_CLK_ENABLE();
106+
107+
HAL_PWREx_EnableVddUSB();
108+
/* Configure USB VBUS GPIO */
109+
__HAL_RCC_GPIOC_CLK_ENABLE();
110+
111+
/* Configure USB FS GPIOs */
112+
__HAL_RCC_GPIOA_CLK_ENABLE();
113+
114+
/* Configure DM DP Pins */
115+
pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
116+
pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
117+
118+
/* Configure VBUS Pin */
119+
pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
120+
121+
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
122+
123+
hpcd.State = HAL_PCD_STATE_RESET;
124+
125+
HAL_PCD_Init(&hpcd);
126+
/* 1.25kbytes */
127+
/* min value 16 (= 16 x 4 bytes) */
128+
/* max value 256 (= 1K bytes ) */
129+
/* maximum sum is 0x140 */
130+
HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4));
131+
/* bulk/int 64 bytes in FS */
132+
HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1);
133+
/* bulk/int bytes in FS */
134+
HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1);
135+
HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4));
136+
/* ISOchronous */
137+
HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4));
138+
139+
NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
140+
NVIC_SetPriority( USBHAL_IRQn, 1);
141+
142+
HAL_PCD_Start(&hpcd);
143+
}
144+
145+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
#include "USBHAL_STM32L475VG.h"

0 commit comments

Comments
 (0)