Skip to content

Commit 2212b34

Browse files
committed
DISCO_L053C8: add USBHAL_STM files
1 parent 1321dc8 commit 2212b34

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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_STM32L053C8_H
19+
#define USBHAL_STM32L053C8_H
20+
21+
#define USBHAL_IRQn USB_IRQn
22+
23+
/* must be multiple of 4 bytes */
24+
#define NB_ENDPOINT 8
25+
#define MAXTRANSFER_SIZE 0x200
26+
#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
27+
#if (FIFO_USB_RAM_SIZE > 0x500)
28+
#error "FIFO dimensioning incorrect"
29+
#endif
30+
31+
typedef struct
32+
{
33+
USBHAL *inst;
34+
void (USBHAL::*bus_reset)(void);
35+
void (USBHAL::*sof)(int frame);
36+
void (USBHAL::*connect_change)(unsigned int connected);
37+
void (USBHAL::*suspend_change)(unsigned int suspended);
38+
void (USBHAL::*ep0_setup)(void);
39+
void (USBHAL::*ep0_in)(void);
40+
void (USBHAL::*ep0_out)(void);
41+
void (USBHAL::*ep0_read)(void);
42+
bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
43+
bool (USBHAL::*epCallback[6])(void);
44+
uint8_t epComplete[2*NB_ENDPOINT];
45+
/* memorize dummy buffer used for reception */
46+
uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
47+
uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
48+
gpio_t usb_switch;
49+
}USBHAL_Private_t;
50+
51+
uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
52+
{
53+
return 1024;
54+
}
55+
56+
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state){
57+
USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
58+
gpio_write(&(priv->usb_switch),state);
59+
}
60+
61+
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
62+
USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
63+
USBHAL *obj= priv->inst;
64+
uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
65+
void (USBHAL::*func)(int frame) = priv->sof;
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+
hpcd.Instance = USB;
75+
/* initialized Init to zero (constructor does not zero initialized the
76+
* area */
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+
/* Configure USB DM pin. This is optional, and maintained only for user guidance. */
106+
__HAL_RCC_GPIOA_CLK_ENABLE();
107+
pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
108+
pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
109+
110+
/* Enable USB Clock */
111+
__HAL_RCC_USB_CLK_ENABLE();
112+
113+
/* Enable SYSCFG Clock */
114+
__HAL_RCC_SYSCFG_CLK_ENABLE();
115+
hpcd.State = HAL_PCD_STATE_RESET;
116+
HAL_PCD_Init(&hpcd);
117+
118+
/* hardcoded size of FIFO according definition*/
119+
HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
120+
HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
121+
#if 1
122+
HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0);
123+
#else
124+
HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_SNG_BUF, 0x180);
125+
#endif
126+
HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0);
127+
NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
128+
NVIC_SetPriority(USBHAL_IRQn, 1);
129+
HAL_PCD_Start(&hpcd);
130+
}
131+
#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_STM32L053C8.h"

0 commit comments

Comments
 (0)