1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-02 10:24:11 +01:00
LibrePilot/flight/pios/stm32f4xx/pios_usb.c

319 lines
8.1 KiB
C
Raw Normal View History

2012-02-07 08:12:55 +01:00
/**
******************************************************************************
* @addtogroup PIOS PIOS Core hardware abstraction layer
* @{
* @addtogroup PIOS_USB USB Setup Functions
* @brief PIOS USB device implementation
* @{
*
* @file pios_usb.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief USB device functions (STM32 dependent code)
* @see The GNU Public License (GPL) Version 3
*
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "pios.h"
2013-03-15 19:42:54 +01:00
#ifdef PIOS_INCLUDE_USB
2012-02-07 08:12:55 +01:00
#include "usb_core.h"
#include "pios_usb_board_data.h"
#include <pios_usb_priv.h>
#include <pios_helpers.h>
2012-02-07 08:12:55 +01:00
/* Rx/Tx status */
static uint8_t transfer_possible = 0;
2013-06-05 01:02:16 +02:00
static void(*disconnection_cb_list[3]) (void);
2012-02-07 08:12:55 +01:00
enum pios_usb_dev_magic {
PIOS_USB_DEV_MAGIC = 0x17365904,
2012-02-07 08:12:55 +01:00
};
struct pios_usb_dev {
enum pios_usb_dev_magic magic;
const struct pios_usb_cfg *cfg;
#ifdef PIOS_INCLUDE_FREERTOS
xSemaphoreHandle statusCheckSemaphore;
#endif
2012-02-07 08:12:55 +01:00
};
static void raiseDisconnectionCallbacks(void);
2012-02-07 08:12:55 +01:00
/**
* @brief Validate the usb device structure
* @returns true if valid device or false otherwise
*/
static bool PIOS_USB_validate(struct pios_usb_dev *usb_dev)
2012-02-07 08:12:55 +01:00
{
return usb_dev && (usb_dev->magic == PIOS_USB_DEV_MAGIC);
2012-02-07 08:12:55 +01:00
}
#if defined(PIOS_INCLUDE_FREERTOS)
static struct pios_usb_dev *PIOS_USB_alloc(void)
2012-02-07 08:12:55 +01:00
{
struct pios_usb_dev *usb_dev;
2012-02-07 08:12:55 +01:00
usb_dev = (struct pios_usb_dev *)pvPortMalloc(sizeof(*usb_dev));
if (!usb_dev) {
return NULL;
}
vSemaphoreCreateBinary(usb_dev->statusCheckSemaphore);
xSemaphoreGive(usb_dev->statusCheckSemaphore);
usb_dev->magic = PIOS_USB_DEV_MAGIC;
return usb_dev;
2012-02-07 08:12:55 +01:00
}
#else
static struct pios_usb_dev pios_usb_devs[PIOS_USB_MAX_DEVS];
static uint8_t pios_usb_num_devs;
static struct pios_usb_dev *PIOS_USB_alloc(void)
2012-02-07 08:12:55 +01:00
{
struct pios_usb_dev *usb_dev;
2012-02-07 08:12:55 +01:00
if (pios_usb_num_devs >= PIOS_USB_MAX_DEVS) {
return NULL;
}
2012-02-07 08:12:55 +01:00
usb_dev = &pios_usb_devs[pios_usb_num_devs++];
usb_dev->magic = PIOS_USB_DEV_MAGIC;
2012-02-07 08:12:55 +01:00
return usb_dev;
2012-02-07 08:12:55 +01:00
}
#endif /* if defined(PIOS_INCLUDE_FREERTOS) */
2012-02-07 08:12:55 +01:00
/**
* Bind configuration to USB BSP layer
* \return < 0 if initialisation failed
*/
static uint32_t pios_usb_id;
int32_t PIOS_USB_Init(uint32_t *usb_id, const struct pios_usb_cfg *cfg)
2012-02-07 08:12:55 +01:00
{
PIOS_Assert(usb_id);
PIOS_Assert(cfg);
2012-02-07 08:12:55 +01:00
struct pios_usb_dev *usb_dev;
2012-02-07 08:12:55 +01:00
usb_dev = (struct pios_usb_dev *)PIOS_USB_alloc();
if (!usb_dev) {
goto out_fail;
}
2012-02-07 08:12:55 +01:00
/* Bind the configuration to the device instance */
usb_dev->cfg = cfg;
2012-02-07 08:12:55 +01:00
/*
* This is a horrible hack to make this available to
* the interrupt callbacks. This should go away ASAP.
*/
pios_usb_id = (uint32_t)usb_dev;
2012-02-07 08:12:55 +01:00
*usb_id = (uint32_t)usb_dev;
2012-02-07 08:12:55 +01:00
return 0; /* No error */
2012-02-07 08:12:55 +01:00
out_fail:
return -1;
2012-02-07 08:12:55 +01:00
}
/**
* This function is called by the USB driver on cable connection/disconnection
* \param[in] connected connection status (1 if connected)
* \return < 0 on errors
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
*/
int32_t PIOS_USB_ChangeConnectionState(bool connected)
{
// In all cases: re-initialise USB HID driver
if (connected) {
transfer_possible = 1;
2012-02-07 08:12:55 +01:00
// TODO: Check SetEPRxValid(ENDP1);
2012-02-07 08:12:55 +01:00
#if defined(USB_LED_ON)
USB_LED_ON; // turn the USB led on
2012-02-07 08:12:55 +01:00
#endif
} else {
// Cable disconnected: disable transfers
transfer_possible = 0;
2012-02-07 08:12:55 +01:00
#if defined(USB_LED_OFF)
USB_LED_OFF; // turn the USB led off
2012-02-07 08:12:55 +01:00
#endif
}
2012-02-07 08:12:55 +01:00
return 0;
2012-02-07 08:12:55 +01:00
}
/**
* This function returns the connection status of the USB interface
* \return 1: interface available
* \return 0: interface not available
*/
uint32_t usb_found;
bool PIOS_USB_CheckAvailable(__attribute__((unused)) uint32_t id)
2012-02-07 08:12:55 +01:00
{
struct pios_usb_dev *usb_dev = (struct pios_usb_dev *)pios_usb_id;
static bool lastStatus = false;
2013-06-05 01:02:16 +02:00
if (!PIOS_USB_validate(usb_dev)) {
return false;
}
2012-02-07 08:12:55 +01:00
usb_found = ((usb_dev->cfg->vsense.gpio->IDR & usb_dev->cfg->vsense.init.GPIO_Pin) != 0) ^ usb_dev->cfg->vsense_active_low;
2012-02-07 08:12:55 +01:00
bool status = usb_found != 0 && transfer_possible ? 1 : 0;
#ifdef PIOS_INCLUDE_FREERTOS
while (xSemaphoreTakeFromISR(usb_dev->statusCheckSemaphore, NULL) != pdTRUE) {
;
}
#endif
bool reconnect = (lastStatus && !status);
lastStatus = status;
#ifdef PIOS_INCLUDE_FREERTOS
xSemaphoreGiveFromISR(usb_dev->statusCheckSemaphore, NULL);
#endif
if (reconnect) {
raiseDisconnectionCallbacks();
}
return status;
}
/*
*
* Register a physical disconnection callback
*
*/
2013-06-05 01:02:16 +02:00
void PIOS_USB_RegisterDisconnectionCallback(void (*disconnectionCB)(void))
{
PIOS_Assert(disconnectionCB);
2013-06-05 01:02:16 +02:00
for (uint32_t i = 0; i < NELEMENTS(disconnection_cb_list); i++) {
if (disconnection_cb_list[i] == NULL) {
disconnection_cb_list[i] = disconnectionCB;
return;
}
}
PIOS_Assert(0);
}
2013-06-05 01:02:16 +02:00
static void raiseDisconnectionCallbacks(void)
{
uint32_t i = 0;
2013-06-05 01:02:16 +02:00
while (i < NELEMENTS(disconnection_cb_list) && disconnection_cb_list[i] != NULL) {
(disconnection_cb_list[i++])();
}
2012-02-07 08:12:55 +01:00
}
/*
*
* Provide STM32 USB OTG BSP layer API
*
*/
#include "usb_bsp.h"
void USB_OTG_BSP_Init(__attribute__((unused)) USB_OTG_CORE_HANDLE *pdev)
2012-02-07 08:12:55 +01:00
{
struct pios_usb_dev *usb_dev = (struct pios_usb_dev *)pios_usb_id;
2012-02-07 08:12:55 +01:00
bool valid = PIOS_USB_validate(usb_dev);
PIOS_Assert(valid);
2012-02-07 08:12:55 +01:00
#define FORCE_DISABLE_USB_IRQ 1
#if FORCE_DISABLE_USB_IRQ
/* Make sure we disable the USB interrupt since it may be left on by bootloader */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure = usb_dev->cfg->irq.init;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure);
2012-02-07 08:12:55 +01:00
#endif
/* Configure USB D-/D+ (DM/DP) pins */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
2012-02-07 08:12:55 +01:00
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS);
2012-02-07 08:12:55 +01:00
/* Configure VBUS sense pin */
GPIO_Init(usb_dev->cfg->vsense.gpio, &usb_dev->cfg->vsense.init);
2012-02-07 08:12:55 +01:00
/* Enable USB OTG Clock */
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE);
2012-02-07 08:12:55 +01:00
}
void USB_OTG_BSP_EnableInterrupt(__attribute__((unused)) USB_OTG_CORE_HANDLE *pdev)
2012-02-07 08:12:55 +01:00
{
struct pios_usb_dev *usb_dev = (struct pios_usb_dev *)pios_usb_id;
bool valid = PIOS_USB_validate(usb_dev);
2012-02-07 08:12:55 +01:00
PIOS_Assert(valid);
2012-02-07 08:12:55 +01:00
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
2012-02-07 08:12:55 +01:00
NVIC_Init(&usb_dev->cfg->irq.init);
2012-02-07 08:12:55 +01:00
}
#ifdef USE_HOST_MODE
void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
{}
2012-02-07 08:12:55 +01:00
void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
{}
#endif /* USE_HOST_MODE */
2012-02-07 08:12:55 +01:00
void USB_OTG_BSP_TimeInit(void)
{}
2012-02-07 08:12:55 +01:00
void USB_OTG_BSP_uDelay(const uint32_t usec)
2012-02-07 08:12:55 +01:00
{
uint32_t count = 0;
const uint32_t utime = (120 * usec / 7);
do {
if (++count > utime) {
return;
}
} while (1);
2012-02-07 08:12:55 +01:00
}
void USB_OTG_BSP_mDelay(const uint32_t msec)
2012-02-07 08:12:55 +01:00
{
USB_OTG_BSP_uDelay(msec * 1000);
2012-02-07 08:12:55 +01:00
}
void USB_OTG_BSP_TimerIRQ(void)
{}
2012-02-07 08:12:55 +01:00
2013-03-15 19:42:54 +01:00
#endif /* PIOS_INCLUDE_USB */
2012-02-07 08:12:55 +01:00
/**
* @}
* @}
*/