dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_usb.c
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 /* Project Includes */
32 #include "pios.h"
33 #include "usb_core.h"
34 #include "pios_usb_board_data.h"
35 #include "pios_usb.h"
36 #include "pios_usb_priv.h"
37 
38 #if defined(PIOS_INCLUDE_USB)
39 
40 /* Rx/Tx status */
41 static uint8_t transfer_possible = 0;
42 
43 enum pios_usb_dev_magic {
44  PIOS_USB_DEV_MAGIC = 0x17365904,
45 };
46 
47 struct pios_usb_dev {
48  enum pios_usb_dev_magic magic;
49  const struct pios_usb_cfg * cfg;
50 };
51 
56 static bool PIOS_USB_validate(struct pios_usb_dev * usb_dev)
57 {
58  return (usb_dev && (usb_dev->magic == PIOS_USB_DEV_MAGIC));
59 }
60 
61 static struct pios_usb_dev * PIOS_USB_alloc(void)
62 {
63  struct pios_usb_dev * usb_dev;
64 
65  usb_dev = (struct pios_usb_dev *)PIOS_malloc(sizeof(*usb_dev));
66  if (!usb_dev) return(NULL);
67 
68  usb_dev->magic = PIOS_USB_DEV_MAGIC;
69  return(usb_dev);
70 }
71 
76 static uintptr_t pios_usb_id;
77 int32_t PIOS_USB_Init(uintptr_t * usb_id, const struct pios_usb_cfg * cfg)
78 {
79  PIOS_Assert(usb_id);
80  PIOS_Assert(cfg);
81 
82  struct pios_usb_dev * usb_dev;
83 
84  usb_dev = (struct pios_usb_dev *) PIOS_USB_alloc();
85  if (!usb_dev) goto out_fail;
86 
87  /* Bind the configuration to the device instance */
88  usb_dev->cfg = cfg;
89 
90  /*
91  * This is a horrible hack to make this available to
92  * the interrupt callbacks. This should go away ASAP.
93  */
94  pios_usb_id = (uintptr_t) usb_dev;
95 
96  *usb_id = (uintptr_t) usb_dev;
97 
98  return 0; /* No error */
99 
100 out_fail:
101  return(-1);
102 }
103 
110 int32_t PIOS_USB_ChangeConnectionState(bool connected)
111 {
112  // In all cases: re-initialise USB HID driver
113  if (connected) {
114  transfer_possible = 1;
115 
116  //TODO: Check SetEPRxValid(ENDP1);
117 
118 #if defined(USB_LED_ON)
119  USB_LED_ON; // turn the USB led on
120 #endif
121  } else {
122  // Cable disconnected: disable transfers
123  transfer_possible = 0;
124 
125 #if defined(USB_LED_OFF)
126  USB_LED_OFF; // turn the USB led off
127 #endif
128  }
129 
130  return 0;
131 }
132 
138 bool PIOS_USB_CheckAvailable(uintptr_t id)
139 {
140  struct pios_usb_dev * usb_dev = (struct pios_usb_dev *) pios_usb_id;
141 
142  if (!PIOS_USB_validate(usb_dev))
143  return false;
144 
145  if (usb_dev->cfg->vsense.gpio != NULL)
146  return GPIO_ReadInputDataBit(usb_dev->cfg->vsense.gpio, usb_dev->cfg->vsense.init.GPIO_Pin) == Bit_SET;
147 
148  return transfer_possible == 1;
149 }
150 
155 bool PIOS_USB_CableConnected(uintptr_t id)
156 {
157  return PIOS_USB_CheckAvailable(id);
158 }
159 
160 bool PIOS_USB_HaveVSense(uintptr_t id)
161 {
162  struct pios_usb_dev * usb_dev = (struct pios_usb_dev *) pios_usb_id;
163 
164  if (PIOS_USB_validate(usb_dev) != 0)
165  return false;
166 
167  return usb_dev->cfg->vsense.gpio != NULL;
168 }
169 
170 /*
171  *
172  * Provide STM32 USB OTG BSP layer API
173  *
174  */
175 
176 #include "usb_bsp.h"
177 
178 void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
179 {
180  struct pios_usb_dev * usb_dev = (struct pios_usb_dev *) pios_usb_id;
181 
182  bool valid = PIOS_USB_validate(usb_dev);
183  PIOS_Assert(valid);
184 
185 #define FORCE_DISABLE_USB_IRQ 1
186 #if FORCE_DISABLE_USB_IRQ
187  /* Make sure we disable the USB interrupt since it may be left on by bootloader */
188  NVIC_InitTypeDef NVIC_InitStructure;
189  NVIC_InitStructure = usb_dev->cfg->irq.init;
190  NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
191  NVIC_Init(&NVIC_InitStructure);
192 #endif
193 
194  /* Configure USB D-/D+ (DM/DP) pins */
195  GPIO_InitTypeDef GPIO_InitStructure;
196  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
197  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
198  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
199  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
200  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
201  GPIO_Init(GPIOA, &GPIO_InitStructure);
202 
203  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS);
204  GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS);
205 
206  /* Configure VBUS sense pin */
207  if (usb_dev->cfg->vsense.gpio)
208  GPIO_Init(usb_dev->cfg->vsense.gpio, (GPIO_InitTypeDef*)&usb_dev->cfg->vsense.init);
209 
210  /* Enable USB OTG Clock */
211  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE);
212 }
213 
214 void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
215 {
216  struct pios_usb_dev * usb_dev = (struct pios_usb_dev *) pios_usb_id;
217 
218  bool valid = PIOS_USB_validate(usb_dev);
219  PIOS_Assert(valid);
220 
221  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
222 
223  NVIC_Init((NVIC_InitTypeDef*)&usb_dev->cfg->irq.init);
224 }
225 
226 #ifdef USE_HOST_MODE
227 void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
228 {
229 
230 }
231 
232 void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
233 {
234 
235 }
236 #endif /* USE_HOST_MODE */
237 
238 void USB_OTG_BSP_TimeInit ( void )
239 {
240 
241 }
242 
243 void USB_OTG_BSP_uDelay (const uint32_t usec)
244 {
245  uint32_t count = 0;
246  const uint32_t utime = (120 * usec / 7);
247  do {
248  if (++count > utime) {
249  return ;
250  }
251  }
252  while (1);
253 }
254 
255 void USB_OTG_BSP_mDelay (const uint32_t msec)
256 {
257  USB_OTG_BSP_uDelay(msec * 1000);
258 }
259 
260 void USB_OTG_BSP_TimerIRQ (void)
261 {
262 
263 }
264 
265 #endif /* PIOS_INCLUDE_USB */
266 
Main PiOS header to include all the compiled in PiOS options.
const uint8_t count
Definition: panel.c:515
void * PIOS_malloc(size_t size)
Definition: pios_heap.c:125
bool PIOS_USB_CheckAvailable(uintptr_t id)
static struct flyingpicmd_cfg_fa cfg
Definition: main.c:49
#define USB_LED_OFF
Definition: pios_board.h:103
USB private definitions.
int32_t PIOS_USB_ChangeConnectionState(bool connected)
USB HID layer functions header.
uint32_t magic
bool PIOS_USB_HaveVSense(uintptr_t id)
int32_t PIOS_USB_Init(uintptr_t *usb_id, const struct pios_usb_cfg *cfg)
#define USB_LED_ON
Definition: pios_board.h:102
bool PIOS_USB_CableConnected(uintptr_t id)
#define PIOS_Assert(test)
Definition: pios_debug.h:52
enum arena_state state