Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace vhci with usbip #247

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.common
Expand Up @@ -112,7 +112,7 @@ SHELL := ${shell which bash}

OBJ := obj-${shell $(CC) -dumpmachine}
LIBDIR := ${shell pwd}/${SIMAVR}/${OBJ}
LDFLAGS += -L${LIBDIR} -lsimavr -lm
LDFLAGS += -L${LIBDIR} -lsimavr -lm -lpthread

LDFLAGS += -lelf

Expand Down
4 changes: 0 additions & 4 deletions doc/manual/setupguide.tex
Expand Up @@ -37,10 +37,6 @@ \subsection{Software Dependencies}
\item make 3.82
\end{itemize}

Furthermore, the board\_usb example depends on libusb\_vhci and vhci\_hcd. For
further details, see \emph{examples/board\_usb/README}. Note however that these
are not required for a fully working \simavr build.

\subsection{Compilation and Installation}

\simavr's build system relies on standard makefiles. The simplest compilation
Expand Down
8 changes: 0 additions & 8 deletions examples/Makefile
Expand Up @@ -12,11 +12,3 @@ all:

clean:
for bi in ${boards}; do $(MAKE) -C $$bi clean; done

#
# The USB example is not made by default, as it downloads stuff
# for the vhci library, it fails the debian policy on being able
# to build offline.
#
extra_board_usb:
make -C extra_board_usb
File renamed without changes.
24 changes: 6 additions & 18 deletions examples/extra_board_usb/Makefile → examples/board_usb/Makefile
Expand Up @@ -19,43 +19,31 @@
target= simusb
firm_src = at90usb162_cdc_loopback.c
firmware = ${firm_src:.c=.hex}
firmware2 = ${firm_src:.c=.axf}
simavr = ../../

IPATH = .
IPATH += ../parts
IPATH += ../vhci/include
IPATH += ../vhci
IPATH += ${simavr}/include
IPATH += ${simavr}/simavr/sim
#IPATH += /usr/local/include

VPATH = .
VPATH += ../parts

LDFLAGS += -lpthread -lusb_vhci -L../vhci/lib
LDFLAGS += -lpthread

all: obj ${firmware} ${target}
all: obj ${firmware} ${target} ${firmware2}

include ${simavr}/Makefile.common

board = ${OBJ}/${target}.elf

${board} : ${OBJ}/vhci_usb.o
${board} : ${OBJ}/usbip.o
${board} : ${OBJ}/${target}.o

../vhci/lib/libusb_vhci.a:
@echo -n "BUILDIN $@ "
@{ export CC=;export CFLAGS=; $(MAKE) -j -C ../vhci >/tmp/vhci.build.log 2>&1 || \
{ echo "ERROR check /tmp/vhci.build.log"; exit 1; }; } && \
echo " Done"

vhci : ../vhci/lib/libusb_vhci.a

clean-vhci:
$(MAKE) -C ../vhci clean

${target}: vhci ${board}
${target}: ${board}
@echo $@ done

clean: clean-${OBJ} clean-vhci
clean: clean-${OBJ}
rm -rf *.a *.axf *.hex ${target} *.vcd
23 changes: 14 additions & 9 deletions examples/extra_board_usb/simusb.c → examples/board_usb/simusb.c
Expand Up @@ -35,10 +35,10 @@
#include "sim_elf.h"
#include "sim_hex.h"
#include "sim_gdb.h"
#include "vhci_usb.h"
#include "usbip.h"
#include "sim_vcd_file.h"

struct vhci_usb_t vhci_usb;
struct usbip_t * usbip;
avr_t * avr = NULL;
avr_vcd_t vcd_file;

Expand All @@ -58,15 +58,15 @@ void avr_special_init( avr_t* avr, void *data)
if(avr->flash) free(avr->flash);
// open the file
flash_data->avr_flash_fd = open(flash_data->avr_flash_path,
O_RDWR|O_CREAT, 0644);
O_RDWR|O_CREAT, 0644);
if (flash_data->avr_flash_fd < 0) {
perror(flash_data->avr_flash_path);
exit(1);
}
// resize and map the file the file
(void)ftruncate(flash_data->avr_flash_fd, avr->flashend + 1);
avr->flash = (uint8_t*)mmap(NULL, avr->flashend + 1, // 32k is multiple of 4096
PROT_READ|PROT_WRITE, MAP_SHARED, flash_data->avr_flash_fd, 0);
PROT_READ|PROT_WRITE, MAP_SHARED, flash_data->avr_flash_fd, 0);
if (!avr->flash) {
fprintf(stderr, "unable to map memory\n");
perror(flash_data->avr_flash_path);
Expand All @@ -89,7 +89,6 @@ void avr_special_deinit( avr_t* avr, void *data)

int main(int argc, char *argv[])
{
// elf_firmware_t f;
const char * pwd = dirname(argv[0]);
struct avr_flash flash_data;

Expand All @@ -98,7 +97,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "%s: Error creating the AVR core\n", argv[0]);
exit(1);
}
strcpy(flash_data.avr_flash_path, "simusb_flash.bin");
strcpy(flash_data.avr_flash_path, "simusb_flash.bin");
flash_data.avr_flash_fd = 0;
// register our own functions
avr->custom.init = avr_special_init;
Expand Down Expand Up @@ -131,16 +130,22 @@ int main(int argc, char *argv[])

// even if not setup at startup, activate gdb if crashing
avr->gdb_port = 1234;
avr_gdb_init(avr);
if (0) {
//avr->state = cpu_Stopped;
avr_gdb_init(avr);
}

vhci_usb_init(avr, &vhci_usb);
vhci_usb_connect(&vhci_usb, '0');

usbip = usbip_create(avr);
if (!usbip) {
fprintf(stderr, "usbip_create failed\n");
exit(1);
}
pthread_t usb_thread;
pthread_create(&usb_thread, NULL, usbip_main, usbip);

while (1) {
avr_run(avr);
}
usbip_destroy(usbip);
}
45 changes: 0 additions & 45 deletions examples/extra_board_usb/README

This file was deleted.

91 changes: 91 additions & 0 deletions examples/parts/usb_types.h
@@ -0,0 +1,91 @@
/* vim: set sts=4:sw=4:ts=4:noexpandtab
usb_types.h

Copyright 2017 Torbjorn Tyridal <ttyridal@gmail.com>

This file is part of simavr.

simavr 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.

simavr 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 simavr. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>

#define byte uint8_t
#define word uint16_t

struct usb_setup_pkt {
byte reqtype;
byte req;
word wValue;
word wIndex;
word wLength;
} __attribute__((__packed__));

#define USB_REQTYPE_DIR_DEV_TO_HOST 0x80
#define USB_REQTYPE_STD 0
#define USB_REQTYPE_DEVICE 0

#define USB_REQUEST_GET_DESCRIPTOR 0x06


// USB Descriptors
#define USB_DESCRIPTOR_DEVICE 0x01 // Device Descriptor.
#define USB_DESCRIPTOR_CONFIGURATION 0x02 // Configuration Descriptor.
#define USB_DESCRIPTOR_STRING 0x03 // String Descriptor.
#define USB_DESCRIPTOR_INTERFACE 0x04 // Interface Descriptor.
#define USB_DESCRIPTOR_ENDPOINT 0x05 // Endpoint Descriptor.
#define USB_DESCRIPTOR_DEVICE_QUALIFIER 0x06 // Device Qualifier.

struct usb_device_descriptor {
byte bLength; // Length of this descriptor.
byte bDescriptorType; // DEVICE descriptor type (USB_DESCRIPTOR_DEVICE).
word bcdUSB; // USB Spec Release Number (BCD).
byte bDeviceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bDeviceSubClass; // Subclass code (assigned by the USB-IF).
byte bDeviceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bMaxPacketSize0; // Maximum packet size for endpoint 0.
word idVendor; // Vendor ID (assigned by the USB-IF).
word idProduct; // Product ID (assigned by the manufacturer).
word bcdDevice; // Device release number (BCD).
byte iManufacturer; // Index of String Descriptor describing the manufacturer.
byte iProduct; // Index of String Descriptor describing the product.
byte iSerialNumber; // Index of String Descriptor with the device's serial number.
byte bNumConfigurations; // Number of possible configurations.
} __attribute__ ((__packed__));

struct usb_configuration_descriptor
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // CONFIGURATION descriptor type (USB_DESCRIPTOR_CONFIGURATION).
word wTotalLength; // Total length of all descriptors for this configuration.
byte bNumInterfaces; // Number of interfaces in this configuration.
byte bConfigurationValue; // Value of this configuration (1 based).
byte iConfiguration; // Index of String Descriptor describing the configuration.
byte bmAttributes; // Configuration characteristics.
byte bMaxPower; // Maximum power consumed by this configuration.
} __attribute__ ((__packed__));


struct usb_interface_descriptor
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // INTERFACE descriptor type (USB_DESCRIPTOR_INTERFACE).
byte bInterfaceNumber; // Number of this interface (0 based).
byte bAlternateSetting; // Value of this alternate interface setting.
byte bNumEndpoints; // Number of endpoints in this interface.
byte bInterfaceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bInterfaceSubClass; // Subclass code (assigned by the USB-IF).
byte bInterfaceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
byte iInterface; // Index of String Descriptor describing the interface.
} __attribute__ ((__packed__));