fix WhatsNew for release
[librepilot.git] / flight / pios / stm32f10x / pios_usb.c
blobf79b08c53daca1513648a759270867fa03a16f1c
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @addtogroup PIOS_USB USB Setup Functions
6 * @brief PIOS USB device implementation
7 * @{
9 * @file pios_usb.c
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
24 * for more details.
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
31 #include "pios.h"
33 #ifdef PIOS_INCLUDE_USB
35 #include "usb_lib.h"
36 #include "pios_usb_board_data.h"
37 #include "stm32f10x.h"
39 #include "pios_usb_priv.h"
41 #ifdef PIOS_INCLUDE_USB_HID
43 /* Rx/Tx status */
44 static bool transfer_possible = false;
46 enum pios_usb_dev_magic {
47 PIOS_USB_DEV_MAGIC = 0x17365904,
50 struct pios_usb_dev {
51 enum pios_usb_dev_magic magic;
52 const struct pios_usb_cfg *cfg;
55 /**
56 * @brief Validate the usb device structure
57 * @returns 0 if valid device or -1 otherwise
59 static int32_t PIOS_USB_validate(struct pios_usb_dev *usb_dev)
61 if (usb_dev == NULL) {
62 return -1;
65 if (usb_dev->magic != PIOS_USB_DEV_MAGIC) {
66 return -1;
69 return 0;
72 #ifdef PIOS_INCLUDE_FREERTOS
73 static struct pios_usb_dev *PIOS_USB_alloc(void)
75 struct pios_usb_dev *usb_dev;
77 usb_dev = (struct pios_usb_dev *)pios_malloc(sizeof(*usb_dev));
78 if (!usb_dev) {
79 return NULL;
82 usb_dev->magic = PIOS_USB_DEV_MAGIC;
83 return usb_dev;
85 #else
86 static struct pios_usb_dev pios_usb_devs[PIOS_USB_MAX_DEVS];
87 static uint8_t pios_usb_num_devs;
88 static struct pios_usb_dev *PIOS_USB_alloc(void)
90 struct pios_usb_dev *usb_dev;
92 if (pios_usb_num_devs >= PIOS_USB_MAX_DEVS) {
93 return NULL;
96 usb_dev = &pios_usb_devs[pios_usb_num_devs++];
97 usb_dev->magic = PIOS_USB_DEV_MAGIC;
99 return usb_dev;
101 #endif /* ifdef PIOS_INCLUDE_FREERTOS */
105 * Initialises USB COM layer
106 * \return < 0 if initialisation failed
107 * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
109 static uint32_t pios_usb_com_id;
110 int32_t PIOS_USB_Init(uint32_t *usb_id, const struct pios_usb_cfg *cfg)
112 PIOS_Assert(usb_id);
113 PIOS_Assert(cfg);
115 struct pios_usb_dev *usb_dev;
117 usb_dev = (struct pios_usb_dev *)PIOS_USB_alloc();
118 if (!usb_dev) {
119 goto out_fail;
122 /* Bind the configuration to the device instance */
123 usb_dev->cfg = cfg;
125 PIOS_USB_Reenumerate();
128 * This is a horrible hack to make this available to
129 * the interrupt callbacks. This should go away ASAP.
131 pios_usb_com_id = (uint32_t)usb_dev;
133 /* Enable the USB Interrupts */
134 NVIC_Init(&usb_dev->cfg->irq.init);
136 /* Select USBCLK source */
137 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
138 /* Enable the USB clock */
139 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
141 USB_Init();
142 USB_SIL_Init();
144 *usb_id = (uint32_t)usb_dev;
146 return 0; /* No error */
148 out_fail:
149 return -1;
153 * This function is called by the USB driver on cable connection/disconnection
154 * \param[in] connected connection status (1 if connected)
155 * \return < 0 on errors
156 * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
158 int32_t PIOS_USB_ChangeConnectionState(bool Connected)
160 // In all cases: re-initialise USB HID driver
161 if (Connected) {
162 transfer_possible = true;
164 // TODO: Check SetEPRxValid(ENDP1);
166 #ifdef USB_LED_ON
167 USB_LED_ON; // turn the USB led on
168 #endif
169 } else {
170 // Cable disconnected: disable transfers
171 transfer_possible = false;
173 #ifdef USB_LED_OFF
174 USB_LED_OFF; // turn the USB led off
175 #endif
178 return 0;
181 int32_t PIOS_USB_Reenumerate()
183 /* Force USB reset and power-down (this will also release the USB pins for direct GPIO control) */
184 _SetCNTR(CNTR_FRES | CNTR_PDWN);
186 /* Using a "dirty" method to force a re-enumeration: */
187 /* Force DPM (Pin PA12) low for ca. 10 mS before USB Tranceiver will be enabled */
188 /* This overrules the external Pull-Up at PA12, and at least Windows & MacOS will enumerate again */
189 GPIO_InitTypeDef GPIO_InitStructure;
190 GPIO_StructInit(&GPIO_InitStructure);
191 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
192 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
193 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
194 GPIO_Init(GPIOA, &GPIO_InitStructure);
196 PIOS_DELAY_WaitmS(50);
198 /* Release power-down, still hold reset */
199 _SetCNTR(CNTR_PDWN);
200 PIOS_DELAY_WaituS(5);
202 /* CNTR_FRES = 0 */
203 _SetCNTR(0);
205 /* Clear pending interrupts */
206 _SetISTR(0);
208 /* Configure USB clock */
209 /* USBCLK = PLLCLK / 1.5 */
210 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
211 /* Enable USB clock */
212 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
214 return 0;
217 bool PIOS_USB_CableConnected(__attribute__((unused)) uint8_t id)
219 struct pios_usb_dev *usb_dev = (struct pios_usb_dev *)pios_usb_com_id;
221 if (PIOS_USB_validate(usb_dev) != 0) {
222 return false;
225 return ((usb_dev->cfg->vsense.gpio->IDR & usb_dev->cfg->vsense.init.GPIO_Pin) != 0) ^ usb_dev->cfg->vsense_active_low;
229 * This function returns the connection status of the USB HID interface
230 * \return 1: interface available
231 * \return 0: interface not available
232 * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
234 bool PIOS_USB_CheckAvailable(uint32_t id)
236 struct pios_usb_dev *usb_dev = (struct pios_usb_dev *)pios_usb_com_id;
238 if (PIOS_USB_validate(usb_dev) != 0) {
239 return false;
242 return PIOS_USB_CableConnected(id) && transfer_possible;
245 #endif /* PIOS_INCLUDE_USB_HID */
247 #endif /* PIOS_INCLUDE_USB */
250 * @}
251 * @}