2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_USB USB Setup Functions
6 * @brief PIOS USB device implementation
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
11 * @brief USB device functions (STM32 dependent code)
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #ifdef PIOS_INCLUDE_USB
36 #include "pios_usb_board_data.h"
37 #include <pios_usb_priv.h>
38 #include <pios_helpers.h>
41 static volatile uint8_t transfer_possible
= 0;
43 #ifdef PIOS_INCLUDE_FREERTOS
44 static void(*disconnection_cb_list
[3]) (void);
47 void (*callback
)(bool connected
, uint32_t context
);
49 } connectionState_cb_list
[3];
52 enum pios_usb_dev_magic
{
53 PIOS_USB_DEV_MAGIC
= 0x17365904,
57 enum pios_usb_dev_magic magic
;
58 const struct pios_usb_cfg
*cfg
;
59 #ifdef PIOS_INCLUDE_FREERTOS
60 xSemaphoreHandle statusCheckSemaphore
;
63 #ifdef PIOS_INCLUDE_FREERTOS
64 static void raiseDisconnectionCallbacks(void);
65 static void raiseConnectionStateCallback(bool connected
);
68 * @brief Validate the usb device structure
69 * @returns true if valid device or false otherwise
71 static bool PIOS_USB_validate(struct pios_usb_dev
*usb_dev
)
73 return usb_dev
&& (usb_dev
->magic
== PIOS_USB_DEV_MAGIC
);
76 #if defined(PIOS_INCLUDE_FREERTOS)
77 static struct pios_usb_dev
*PIOS_USB_alloc(void)
79 struct pios_usb_dev
*usb_dev
;
81 usb_dev
= (struct pios_usb_dev
*)pios_malloc(sizeof(*usb_dev
));
85 vSemaphoreCreateBinary(usb_dev
->statusCheckSemaphore
);
86 xSemaphoreGive(usb_dev
->statusCheckSemaphore
);
87 usb_dev
->magic
= PIOS_USB_DEV_MAGIC
;
91 static struct pios_usb_dev pios_usb_devs
[PIOS_USB_MAX_DEVS
];
92 static uint8_t pios_usb_num_devs
;
93 static struct pios_usb_dev
*PIOS_USB_alloc(void)
95 struct pios_usb_dev
*usb_dev
;
97 if (pios_usb_num_devs
>= PIOS_USB_MAX_DEVS
) {
101 usb_dev
= &pios_usb_devs
[pios_usb_num_devs
++];
102 usb_dev
->magic
= PIOS_USB_DEV_MAGIC
;
106 #endif /* if defined(PIOS_INCLUDE_FREERTOS) */
110 * Bind configuration to USB BSP layer
111 * \return < 0 if initialisation failed
113 static uint32_t pios_usb_id
;
114 int32_t PIOS_USB_Init(uint32_t *usb_id
, const struct pios_usb_cfg
*cfg
)
119 struct pios_usb_dev
*usb_dev
;
121 usb_dev
= (struct pios_usb_dev
*)PIOS_USB_alloc();
126 /* Bind the configuration to the device instance */
130 * This is a horrible hack to make this available to
131 * the interrupt callbacks. This should go away ASAP.
133 pios_usb_id
= (uint32_t)usb_dev
;
135 *usb_id
= (uint32_t)usb_dev
;
137 return 0; /* No error */
144 * This function is called by the USB driver on cable connection/disconnection
145 * \param[in] connected connection status (1 if connected)
146 * \return < 0 on errors
147 * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
149 int32_t PIOS_USB_ChangeConnectionState(bool connected
)
151 #ifdef PIOS_INCLUDE_FREERTOS
152 static volatile uint8_t lastStatus
= 2; // 2 is "no last status"
154 // In all cases: re-initialise USB HID driver
156 transfer_possible
= 1;
158 // TODO: Check SetEPRxValid(ENDP1);
160 #if defined(USB_LED_ON)
161 USB_LED_ON
; // turn the USB led on
164 // Cable disconnected: disable transfers
165 transfer_possible
= 0;
167 #if defined(USB_LED_OFF)
168 USB_LED_OFF
; // turn the USB led off
172 #ifdef PIOS_INCLUDE_FREERTOS
173 raiseConnectionStateCallback(connected
);
174 if (lastStatus
!= transfer_possible
) {
175 if (lastStatus
== 1) {
176 raiseDisconnectionCallbacks();
178 lastStatus
= transfer_possible
;
186 * This function returns the connection status of the USB interface
187 * \return 1: interface available
188 * \return 0: interface not available
191 bool PIOS_USB_CheckAvailable(__attribute__((unused
)) uint32_t id
)
193 struct pios_usb_dev
*usb_dev
= (struct pios_usb_dev
*)pios_usb_id
;
195 if (!PIOS_USB_validate(usb_dev
)) {
199 return transfer_possible
;
204 * Register a physical disconnection callback
207 #ifdef PIOS_INCLUDE_FREERTOS
208 void PIOS_USB_RegisterDisconnectionCallback(void (*disconnectionCB
)(void))
210 PIOS_Assert(disconnectionCB
);
211 for (uint32_t i
= 0; i
< NELEMENTS(disconnection_cb_list
); i
++) {
212 if (disconnection_cb_list
[i
] == NULL
) {
213 disconnection_cb_list
[i
] = disconnectionCB
;
219 static void raiseDisconnectionCallbacks(void)
223 while (i
< NELEMENTS(disconnection_cb_list
) && disconnection_cb_list
[i
] != NULL
) {
224 (disconnection_cb_list
[i
++])();
228 void PIOS_USB_RegisterConnectionStateCallback(void (*connectionStateCallback
)(bool connected
, uint32_t context
), uint32_t context
)
230 PIOS_Assert(connectionStateCallback
);
232 for (uint32_t i
= 0; i
< NELEMENTS(connectionState_cb_list
); i
++) {
233 if (connectionState_cb_list
[i
].callback
== NULL
) {
234 connectionState_cb_list
[i
].callback
= connectionStateCallback
;
235 connectionState_cb_list
[i
].context
= context
;
243 static void raiseConnectionStateCallback(bool connected
)
247 while (i
< NELEMENTS(connectionState_cb_list
) && connectionState_cb_list
[i
].callback
!= NULL
) {
248 connectionState_cb_list
[i
].callback(connected
, connectionState_cb_list
[i
].context
);
252 #else /* PIOS_INCLUDE_FREERTOS */
253 void PIOS_USB_RegisterDisconnectionCallback(__attribute__((unused
)) void (*disconnectionCB
)(void))
255 void PIOS_USB_RegisterConnectionStateCallback(__attribute__((unused
)) void (*connectionStateCallback
)(bool connected
, uint32_t context
), __attribute__((unused
)) uint32_t context
)
257 #endif /* PIOS_INCLUDE_FREERTOS */
261 * Provide STM32 USB OTG BSP layer API
267 void USB_OTG_BSP_Init(__attribute__((unused
)) USB_OTG_CORE_HANDLE
*pdev
)
269 struct pios_usb_dev
*usb_dev
= (struct pios_usb_dev
*)pios_usb_id
;
271 bool valid
= PIOS_USB_validate(usb_dev
);
275 #define FORCE_DISABLE_USB_IRQ 1
276 #if FORCE_DISABLE_USB_IRQ
277 /* Make sure we disable the USB interrupt since it may be left on by bootloader */
278 NVIC_InitTypeDef NVIC_InitStructure
;
279 NVIC_InitStructure
= usb_dev
->cfg
->irq
.init
;
280 NVIC_InitStructure
.NVIC_IRQChannelCmd
= DISABLE
;
281 NVIC_Init(&NVIC_InitStructure
);
284 /* Configure USB D-/D+ (DM/DP) pins */
285 GPIO_InitTypeDef GPIO_InitStructure
;
286 GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_11
| GPIO_Pin_12
;
287 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_100MHz
;
288 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF
;
289 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
290 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
291 GPIO_Init(GPIOA
, &GPIO_InitStructure
);
293 GPIO_PinAFConfig(GPIOA
, GPIO_PinSource11
, GPIO_AF_OTG_FS
);
294 GPIO_PinAFConfig(GPIOA
, GPIO_PinSource12
, GPIO_AF_OTG_FS
);
296 /* Configure VBUS sense pin */
297 GPIO_Init(usb_dev
->cfg
->vsense
.gpio
, &usb_dev
->cfg
->vsense
.init
);
299 /* Enable USB OTG Clock */
300 RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS
, ENABLE
);
303 void USB_OTG_BSP_EnableInterrupt(__attribute__((unused
)) USB_OTG_CORE_HANDLE
*pdev
)
305 struct pios_usb_dev
*usb_dev
= (struct pios_usb_dev
*)pios_usb_id
;
307 bool valid
= PIOS_USB_validate(usb_dev
);
311 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4
);
313 NVIC_Init(&usb_dev
->cfg
->irq
.init
);
317 void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE
*pdev
, uint8_t state
)
320 void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE
*pdev
)
322 #endif /* USE_HOST_MODE */
324 void USB_OTG_BSP_TimeInit(void)
327 void USB_OTG_BSP_uDelay(const uint32_t usec
)
330 const uint32_t utime
= (120 * usec
/ 7);
333 if (++count
> utime
) {
339 void USB_OTG_BSP_mDelay(const uint32_t msec
)
341 USB_OTG_BSP_uDelay(msec
* 1000);
344 void USB_OTG_BSP_TimerIRQ(void)
347 #endif /* PIOS_INCLUDE_USB */