From 82ce660932e781e24b673e727a4fd9c977b38ca9 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Thu, 19 Sep 2013 19:51:32 -0700 Subject: [PATCH] Broke some application-specific USB code into separate files within hackrf_usb/. Hoping I can use them as necessary in other projects, or push them into a library eventually. --- firmware/common/usb_queue.h | 2 + firmware/hackrf_usb/Makefile | 2 + firmware/hackrf_usb/hackrf_usb.c | 91 ++---------------------------- firmware/hackrf_usb/usb_device.c | 67 ++++++++++++++++++++++ firmware/hackrf_usb/usb_device.h | 30 ++++++++++ firmware/hackrf_usb/usb_endpoint.c | 73 ++++++++++++++++++++++++ firmware/hackrf_usb/usb_endpoint.h | 41 ++++++++++++++ 7 files changed, 219 insertions(+), 87 deletions(-) create mode 100644 firmware/hackrf_usb/usb_device.c create mode 100644 firmware/hackrf_usb/usb_device.h create mode 100644 firmware/hackrf_usb/usb_endpoint.c create mode 100644 firmware/hackrf_usb/usb_endpoint.h diff --git a/firmware/common/usb_queue.h b/firmware/common/usb_queue.h index 2332022c..36663427 100644 --- a/firmware/common/usb_queue.h +++ b/firmware/common/usb_queue.h @@ -49,6 +49,8 @@ struct _usb_queue_t { usb_transfer_t* volatile active; }; +#define USB_DECLARE_QUEUE(endpoint_name) \ + struct _usb_queue_t endpoint_name##_queue; #define USB_DEFINE_QUEUE(endpoint_name, _pool_size) \ struct _usb_transfer_t endpoint_name##_transfers[_pool_size]; \ struct _usb_queue_t endpoint_name##_queue = { \ diff --git a/firmware/hackrf_usb/Makefile b/firmware/hackrf_usb/Makefile index e614197b..69d31197 100644 --- a/firmware/hackrf_usb/Makefile +++ b/firmware/hackrf_usb/Makefile @@ -31,6 +31,8 @@ SRC = $(BINARY).c \ ../common/usb_request.c \ ../common/usb_standard_request.c \ usb_descriptor.c \ + usb_device.c \ + usb_endpoint.c \ ../common/usb_queue.c \ ../common/fault_handler.c \ ../common/hackrf_core.c \ diff --git a/firmware/hackrf_usb/hackrf_usb.c b/firmware/hackrf_usb/hackrf_usb.c index 6f4d1fdf..25c23866 100644 --- a/firmware/hackrf_usb/hackrf_usb.c +++ b/firmware/hackrf_usb/hackrf_usb.c @@ -46,6 +46,10 @@ #include "usb_descriptor.h" #include "usb_standard_request.h" +#include "usb_device.h" +#include "usb_endpoint.h" +#include "usb_api_cpld.h" + #include "rf_path.h" #include "tuning.h" #include "sgpio_isr.h" @@ -74,93 +78,6 @@ typedef struct { set_sample_r_params_t set_sample_r_params; -usb_configuration_t usb_configuration_high_speed = { - .number = 1, - .speed = USB_SPEED_HIGH, - .descriptor = usb_descriptor_configuration_high_speed, -}; - -usb_configuration_t usb_configuration_full_speed = { - .number = 1, - .speed = USB_SPEED_FULL, - .descriptor = usb_descriptor_configuration_full_speed, -}; - -usb_configuration_t usb_configuration_cpld_update_full_speed = { - .number = 2, - .speed = USB_SPEED_FULL, - .descriptor = usb_descriptor_configuration_cpld_update_full_speed, -}; - -usb_configuration_t usb_configuration_cpld_update_high_speed = { - .number = 2, - .speed = USB_SPEED_HIGH, - .descriptor = usb_descriptor_configuration_cpld_update_high_speed, -}; - -usb_configuration_t* usb_configurations[] = { - &usb_configuration_high_speed, - &usb_configuration_full_speed, - &usb_configuration_cpld_update_full_speed, - &usb_configuration_cpld_update_high_speed, - 0, -}; - -usb_device_t usb_device = { - .descriptor = usb_descriptor_device, - .descriptor_strings = usb_descriptor_strings, - .qualifier_descriptor = usb_descriptor_device_qualifier, - .configurations = &usb_configurations, - .configuration = 0, -}; - -usb_endpoint_t usb_endpoint_control_out; -usb_endpoint_t usb_endpoint_control_in; - -usb_endpoint_t usb_endpoint_control_out = { - .address = 0x00, - .device = &usb_device, - .in = &usb_endpoint_control_in, - .out = &usb_endpoint_control_out, - .setup_complete = usb_setup_complete, - .transfer_complete = usb_control_out_complete, -}; -USB_DEFINE_QUEUE(usb_endpoint_control_out, 4); - -usb_endpoint_t usb_endpoint_control_in = { - .address = 0x80, - .device = &usb_device, - .in = &usb_endpoint_control_in, - .out = &usb_endpoint_control_out, - .setup_complete = 0, - .transfer_complete = usb_control_in_complete, -}; -static USB_DEFINE_QUEUE(usb_endpoint_control_in, 4); - -// NOTE: Endpoint number for IN and OUT are different. I wish I had some -// evidence that having BULK IN and OUT on separate endpoint numbers was -// actually a good idea. Seems like everybody does it that way, but why? - -usb_endpoint_t usb_endpoint_bulk_in = { - .address = 0x81, - .device = &usb_device, - .in = &usb_endpoint_bulk_in, - .out = 0, - .setup_complete = 0, - .transfer_complete = usb_queue_transfer_complete -}; -static USB_DEFINE_QUEUE(usb_endpoint_bulk_in, 4); - -usb_endpoint_t usb_endpoint_bulk_out = { - .address = 0x02, - .device = &usb_device, - .in = 0, - .out = &usb_endpoint_bulk_out, - .setup_complete = 0, - .transfer_complete = usb_queue_transfer_complete -}; -static USB_DEFINE_QUEUE(usb_endpoint_bulk_out, 4); - void baseband_streaming_enable() { nvic_set_priority(NVIC_SGPIO_IRQ, 0); nvic_enable_irq(NVIC_SGPIO_IRQ); diff --git a/firmware/hackrf_usb/usb_device.c b/firmware/hackrf_usb/usb_device.c new file mode 100644 index 00000000..6df358ed --- /dev/null +++ b/firmware/hackrf_usb/usb_device.c @@ -0,0 +1,67 @@ +/* + * Copyright 2012 Jared Boone + * Copyright 2013 Benjamin Vernoux + * + * This file is part of HackRF. + * + * 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 2, 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; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "usb_device.h" + +#include + +#include "usb_descriptor.h" + +usb_configuration_t usb_configuration_high_speed = { + .number = 1, + .speed = USB_SPEED_HIGH, + .descriptor = usb_descriptor_configuration_high_speed, +}; + +usb_configuration_t usb_configuration_full_speed = { + .number = 1, + .speed = USB_SPEED_FULL, + .descriptor = usb_descriptor_configuration_full_speed, +}; + +usb_configuration_t usb_configuration_cpld_update_full_speed = { + .number = 2, + .speed = USB_SPEED_FULL, + .descriptor = usb_descriptor_configuration_cpld_update_full_speed, +}; + +usb_configuration_t usb_configuration_cpld_update_high_speed = { + .number = 2, + .speed = USB_SPEED_HIGH, + .descriptor = usb_descriptor_configuration_cpld_update_high_speed, +}; + +usb_configuration_t* usb_configurations[] = { + &usb_configuration_high_speed, + &usb_configuration_full_speed, + &usb_configuration_cpld_update_full_speed, + &usb_configuration_cpld_update_high_speed, + 0, +}; + +usb_device_t usb_device = { + .descriptor = usb_descriptor_device, + .descriptor_strings = usb_descriptor_strings, + .qualifier_descriptor = usb_descriptor_device_qualifier, + .configurations = &usb_configurations, + .configuration = 0, +}; diff --git a/firmware/hackrf_usb/usb_device.h b/firmware/hackrf_usb/usb_device.h new file mode 100644 index 00000000..8efada7d --- /dev/null +++ b/firmware/hackrf_usb/usb_device.h @@ -0,0 +1,30 @@ +/* + * Copyright 2012 Jared Boone + * Copyright 2013 Benjamin Vernoux + * + * This file is part of HackRF. + * + * 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 2, 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; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __USB_DEVICE_H__ +#define __USB_DEVICE_H__ + +#include + +extern usb_device_t usb_device; + +#endif /* end of include guard: __USB_DEVICE_H__ */ diff --git a/firmware/hackrf_usb/usb_endpoint.c b/firmware/hackrf_usb/usb_endpoint.c new file mode 100644 index 00000000..c965287f --- /dev/null +++ b/firmware/hackrf_usb/usb_endpoint.c @@ -0,0 +1,73 @@ +/* + * Copyright 2012 Jared Boone + * Copyright 2013 Benjamin Vernoux + * + * This file is part of HackRF. + * + * 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 2, 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; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "usb_endpoint.h" + +#include + +#include "usb_device.h" + +usb_endpoint_t usb_endpoint_control_out = { + .address = 0x00, + .device = &usb_device, + .in = &usb_endpoint_control_in, + .out = &usb_endpoint_control_out, + .setup_complete = usb_setup_complete, + .transfer_complete = usb_control_out_complete, +}; +USB_DEFINE_QUEUE(usb_endpoint_control_out, 4); + +usb_endpoint_t usb_endpoint_control_in = { + .address = 0x80, + .device = &usb_device, + .in = &usb_endpoint_control_in, + .out = &usb_endpoint_control_out, + .setup_complete = 0, + .transfer_complete = usb_control_in_complete, +}; +static USB_DEFINE_QUEUE(usb_endpoint_control_in, 4); + +// NOTE: Endpoint number for IN and OUT are different. I wish I had some +// evidence that having BULK IN and OUT on separate endpoint numbers was +// actually a good idea. Seems like everybody does it that way, but why? + +usb_endpoint_t usb_endpoint_bulk_in = { + .address = 0x81, + .device = &usb_device, + .in = &usb_endpoint_bulk_in, + .out = 0, + .setup_complete = 0, + .transfer_complete = usb_queue_transfer_complete +}; +static USB_DEFINE_QUEUE(usb_endpoint_bulk_in, 4); + +usb_endpoint_t usb_endpoint_bulk_out = { + .address = 0x02, + .device = &usb_device, + .in = 0, + .out = &usb_endpoint_bulk_out, + .setup_complete = 0, + .transfer_complete = usb_queue_transfer_complete +}; +static USB_DEFINE_QUEUE(usb_endpoint_bulk_out, 4); + + diff --git a/firmware/hackrf_usb/usb_endpoint.h b/firmware/hackrf_usb/usb_endpoint.h new file mode 100644 index 00000000..bed1d3ce --- /dev/null +++ b/firmware/hackrf_usb/usb_endpoint.h @@ -0,0 +1,41 @@ +/* + * Copyright 2012 Jared Boone + * Copyright 2013 Benjamin Vernoux + * + * This file is part of HackRF. + * + * 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 2, 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; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __USB_ENDPOINT_H__ +#define __USB_ENDPOINT_H__ + +#include +#include + +extern usb_endpoint_t usb_endpoint_control_out; +extern USB_DECLARE_QUEUE(usb_endpoint_control_out); + +extern usb_endpoint_t usb_endpoint_control_in; +extern USB_DECLARE_QUEUE(usb_endpoint_control_in); + +extern usb_endpoint_t usb_endpoint_bulk_in; +extern USB_DECLARE_QUEUE(usb_endpoint_bulk_in); + +extern usb_endpoint_t usb_endpoint_bulk_out; +extern USB_DECLARE_QUEUE(usb_endpoint_bulk_out); + +#endif /* end of include guard: __USB_ENDPOINT_H__ */