Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions doc/USB.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ <H3>USB in simavr.</H3>
<I>tests/test_usb_host.h</I> is a complete example that enumerates a device
and exchanges bulk data.
<P>
When no data is ready, a read from a bulk IN endpoint returns 0 with a size
of zero rather than AVR_IOCTL_USB_NAK.
Suspend and resume, OTG and host mode, double-buffered endpoints
and data toggle checking are not simulated.
<P>
<I>examples/extra_board_usb</I> connects the simulated device to Linux
through the out-of-tree usb-vhci kernel module, so that it appears as a real
USB device.
That module is no longer maintained and does not build on current kernels
without patches.
<I>examples/parts/usbip_usb.c</I> plays the host for you and exports the
simulated device over USB/IP, so that a USB/IP client attaches it to a
real USB stack, where it appears as an ordinary USB device.
On Linux the client is the mainline vhci-hcd driver and the <I>usbip</I>
tool, on Windows usbip-win2.
<I>examples/board_simduino_leonardo</I> is an Arduino Leonardo this way,
an ATmega32U4 with Arduino's Caterina bootloader that avrdude uploads
sketches to.
<I>examples/board_usbserial</I> is the Arduino Uno R3's USB to
serial adapter, an ATmega16U2 with its USART looped back or on a pty.

</BODY>
</HTML>
4 changes: 0 additions & 4 deletions doc/manual/setupguide.tex
Original file line number Diff line number Diff line change
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
10 changes: 1 addition & 9 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
boards_base=${wildcard board_*}
# Remove simduino until FileMapping is implemented to work around missing mmap in Win32
ifeq (${shell uname -o}, Msys)
boards=${subst board_usb,,${subst board_simduino,,$(boards_base)}}
boards=$(filter-out board_simduino,$(boards_base))
else
boards=$(boards_base)
endif
Expand All @@ -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
128 changes: 128 additions & 0 deletions examples/board_simduino_leonardo/Caterina-Leonardo-bootloader.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
:2070000055C000006EC000006CC000006AC0000068C0000066C0000064C0000062C0000043
:2070200060C000005EC00000F2C400005AC0000058C0000056C0000054C0000052C00000EE
:2070400050C0000078C000004CC000004AC0000048C0000046C0000044C0000042C00000BE
:2070600040C000003EC000003CC000003AC0000038C0000036C0000034C0000032C0000048
:2070800030C000002EC000002CC000002AC0000028C0000026C0000024C0000022C00000A8
:2070A00020C000001EC000001CC0000011241FBECFEFDAE0DEBFCDBF11E0A0E0B1E0E2E3BC
:2070C000FFE702C005900D92A83AB107D9F711E0A8EAB1E001C01D92AE3BB107E1F78FD30B
:2070E00026C78ECFF89410926F0010928100109285001092840081E085BF15BE47985D9AEB
:20710000289A0C94000008952091B2013091B3012F5F3F4F3093B3012093B201932F37FFA6
:2071200003C08EEF831B982F990F921710F447980895479A08951F920F920FB60F9211246E
:207140002F938F939F93EF93FF9310928500109284008091A8019091A901009741F00197D3
:207160009093A9018093A801892B09F45D9A8091AA019091AB01009741F001979093AB0126
:207180008093AA01892B09F4289AE0E0F0E0859194918F5F9F4F49F08091AC019091AD0151
:2071A00001969093AD018093AC01FF91EF919F918F912F910F900FBE0F901F90189584E0BC
:2071C0008093E9000DC08091E8008B778093E80003C08EB3882351F08091E80082FFF9CFBE
:2071E0008091E80085FFEFCF8091F1000895982F83E08093E9008091E80085FD0DC0809136
:20720000E8008E778093E80003C08EB3882369F08091E80080FFF9CF9093F1005D9884E6CB
:2072200090E09093A9018093A80108954F925F926F927F928F929F92AF92BF92CF92DF921E
:20724000EF92FF920F931F93CF93DF9384E08093E9008091E80082FF57C2289884E690E067
:207260009093AB018093AA01AADF182F853481F48CE49DE19093AD018093AC0107B600FC4B
:20728000FDCFF999FECF81E180935700E89503C0843519F494DF8DE00DC28C34E1F38035F9
:2072A000D1F3843721F484E4A2DF80E003C2813611F489E5FFC18134B1F481DF182F7FDFE3
:2072C00090E0880F991FAA2797FDA095BA2F312F330F20E0442737FD4095542F822B932B68
:2072E000A42BB52BB8C1803711F483E5E3C1833549F4C0E0D1E089917ADF21E0C730D20714
:20730000D1F7D9C1863521F481E371DF80E3D2C1833731F487E86BDF85E969DF8EE1CAC149
:207320008536B9F4E0E0F0E093E085E090935700E89507B600FCFDCF80935700E89507B65D
:2073400000FCFDCFE058FF4FA0E7E030FA0771F7A2CF823739F4E1E0F0E089E08093570024
:207360008491A8C1863439F4E0E0F0E089E08093570084919FC18E3439F4E3E0F0E089E0E5
:2073800080935700849196C1813539F4E2E0F0E089E08093570084918DC1823631F489E521
:2073A00026DF80E024DF80E885C1823419F0873609F0E5C01092AD011092AC0100DF082FE8
:2073C000FEDEF82EFCDE682E8554823008F071C1902F80E0CF2DD0E0C82BD92B10926F00B3
:2073E000173609F04BC081E180935700E895DD24CC24C3943FC0E090AE01F090AF010091CC
:20740000B0011091B101B6E46B16D9F4ED2DF0E0EE29FF29E4918E2FEADEDD2081F082E08D
:2074200090E0A0E0B0E0E80EF91E0A1F1B1FE092AE01F092AF010093B0011093B101DC2470
:2074400018C0D801C701B695A7959795879559D5CEDE82E090E0A0E0B0E0E80EF91E0A1FF2
:207460001B1FE092AE01F092AF010093B0011093B1012197209709F0BECF7DC08090AE01F5
:207480009090AF01A090B001B090B10196E4691609F05DC083E0F40180935700E89507B63E
:2074A00000FCFDCF54C0F6E46F1661F5772031F1E090AE01F090AF010091B0011091B1019E
:2074C0007EDED82ECC24852D90E08C299D29F7010C0140925700E895112482E090E0A0E08B
:2074E000B0E0E80EF91E0A1F1B1FE092AE01F092AF010093B0011093B10102C060DE582E1A
:20750000742423C0E090AE01F090AF010091B0011091B10116950795F794E79450DE682FFA
:20752000C701F7D48091AE019091AF01A091B001B091B1010296A11DB11D8093AE01909349
:20754000AF01A093B001B093B101219704C05524772444244394209709F0A5CF96E46916B6
:2075600041F485E0F40180935700E89507B600FCFDCF8DE03CDE82E080936F009CC0833492
:2075800071F40091AE011091AF0119DE90E021E0F8010C0120935700E89511247CCE8336C8
:2075A00019F5E090AE01F090AF010091B0011091B10105DEF701E16090E021E00C0120938C
:2075C0005700E895112482E090E0A0E0B0E0E80EF91E0A1F1B1FE092AE01F092AF0100936A
:2075E000B0011093B10157CE8D3661F4E091AE01F091AF0185E080935700E89507B600FCF2
:20760000FDCF49CE823551F4E091AE01F091AF0105911491812FEBDD802F4CC0843421F5FE
:20762000E090AE01F090AF010091B0011091B10116950795F794E794C2DD682FC70169D4DE
:207640008091AE019091AF01A091B001B091B1010296A11DB11D8093AE019093AF01A093D8
:20766000B001B093B10117CE843609F5E090AE01F090AF010091B0011091B101D801C70142
:20768000B695A795979587953CD4B1DD82E090E0A0E0B0E0E80EF91E0A1F1B1FE092AE010A
:2076A000F092AF010093B0011093B10104C08B3111F08FE39CDD83E08093E9009091E8002B
:2076C0008091E8008E778093E80095FF04C010C08EB38823C9F08091E80080FFF9CF809193
:2076E000E8008E778093E80003C08EB3882361F08091E80080FFF9CF84E08093E9008091F1
:20770000E8008B778093E800DF91CF911F910F91FF90EF90DF90CF90BF90AF909F908F90AC
:207720007F906F905F904F9008959091B601892F8F77813249F58091B7018032A1F081328A
:2077400019F5913A09F58091E800877F8093E8008DE091E067E070E00BD28091E8008B770B
:207760008093E8000895913279F48091E800877F8093E8008DE091E067E070E05DD2809192
:20778000E8008E778093E800089582E061EC42E0B5D083E061E842E1B1D084E060E842E1EF
:2077A000ADC084B7877F84BF88E10FB6F89480936000109260000FBE20E880E090E00FB63F
:2077C000F89420936100809361000FBE81E085BF92E095BF3F9A209A559AE1E6F0E02083A1
:2077E000108247985D9A289A109289008AEF8093880090936F0083E080938100F0C0409116
:20780000000850910108109201081092000894B714BE88E10FB6F8948093600010926000D5
:207820000FBE292F30E0F901E270F07091FD18C090FF05C0859194918F5F9F4F81F423FFFF
:207840000FC08091090190910A014817590741F0E0E0F0E0859194918F5F9F4F09F042DC64
:20786000A0DF78941092AD011092AC010CC0DEDC36D38091AC019091AD0181549F4110F00D
:207880001092140141DC80911401882381F78091E00081608093E00025DC80E090E0089598
:2078A000FA01923049F0933061F09130F9F485E191E022E130E01EC087E291E02EE330E04D
:2078C00019C0882329F485E691E024E030E012C0813029F489E691E022E230E00BC0823006
:2078E00029F48DE891E028E130E004C080E090E020E030E091838083C90108958093E9004E
:207900008091EB0081608093EB001092ED006093EC004093ED008091EE00881F8827881F62
:2079200008958091B60188238CF403C08EB38823B1F08091E80082FFF9CF8091E8008B772A
:207940008093E80008958EB3882349F08091E80080FFF9CF8091E8008E778093E800089594
:20796000EF92FF920F931F9345D04CD008ED10E0F80180818F77808380818068808380819B
:207980008F7D808319BC1EBA1092B40180EEE82EF12CF70180818B7F8083F8018081816052
:2079A000808380E060E042E0A9DFE1EEF0E080818E7F8083E2EEF0E0808181608083808144
:2079C00088608083F70180818E7F8083F8018081806180831F910F91FF90EF900895E7ED06
:2079E000F0E08081816080838AE482BF81E08093B501B6CFE8EDF0E080818E7F808310921C
:207A0000E20008951092DA001092E10008951F920F920FB60F9211242F933F934F935F93F6
:207A20006F937F938F939F93AF93BF93EF93FF938091DA0080FF1BC08091D80080FF17C0B2
:207A40008091DA008E7F8093DA008091D90080FF0BC080E189BD82E189BD09B400FEFDCF36
:207A600081E08EBB3BD203C019BC1EBA37D28091E10080FF17C08091E20080FF13C0809138
:207A8000E2008E7F8093E2008091E20080618093E2008091D80080628093D80019BC85E049
:207AA0008EBB1CD28091E10084FF2CC08091E20084FF28C080E189BD82E189BD09B400FEC5
:207AC000FDCF8091D8008F7D8093D8008091E1008F7E8093E1008091E2008F7E8093E20012
:207AE0008091E20081608093E2008091B401882331F48091E30087FD02C081E001C084E067
:207B00008EBBECD18091E10083FF21C08091E20083FF1DC08091E100877F8093E10082E06A
:207B20008EBB1092B4018091E1008E7F8093E1008091E2008E7F8093E2008091E2008061E9
:207B40008093E20080E060E042E0D8DEC7D1FF91EF91BF91AF919F918F917F916F915F9130
:207B60004F913F912F910F900FBE0F901F9018959C014091BC015091BD014617570718F49D
:207B8000F90190E044C06115710511F0AB01F8CF8091E8008E778093E80040E050E0F0CF0F
:207BA0008EB3882309F444C0853009F443C08091E80083FF02C081E008958091E80082FD70
:207BC00031C08091E80080FF22C08091F3009091F200782F60E0292F30E0262B372B07C07A
:207BE00081918093F100415050402F5F3F4F4115510519F02830310598F390E02830310566
:207C000009F491E08091E8008E778093E8004115510531F6992321F605C08EB3882341F075
:207C2000853041F08091E80082FFF7CF80E0089582E0089583E008959C016115710529F47C
:207C40008091E8008B778093E800F90126C08EB3882391F1853091F18091E80083FF02C06C
:207C600081E008958091E80082FFF1CF06C08091F10081936150704059F02091F300809191
:207C8000F200322F20E090E0822B932B892B79F78091E8008B778093E80061157105B9F601
:207CA00005C08EB3882341F0853041F08091E80080FFF7CF80E0089582E0089583E00895C2
:207CC0000F931F93DF93CF9300D0CDB7DEB7E6EBF1E08091F100819381E0EE3BF807C9F792
:207CE00024DD8091E80083FFE4C08091B6019091B701953009F46DC0963040F4913081F1A7
:207D0000913070F0933009F0D4C02AC0983009F4A3C0993009F4B2C0963009F0CAC07CC023
:207D2000803809F4C6C0823809F0C3C08091BA0187708093E9008091EB001092E9002091DB
:207D4000E800277F2093E80090E025E0969587952A95E1F781708093F1001092F10087C0D8
:207D6000882319F0823009F0A4C08F71823009F0A0C08091B801882331F52091BA01277097
:207D800009F497C02093E9008091EB0080FF1BC0933021F48091EB00806213C08091EB0018
:207DA00080618093EB0081E090E002C0880F991F2A95E2F78093EA001092EA008091EB00E5
:207DC00088608093EB001092E9008091E800877F51C0882309F06DC01091B8011F770FB79B
:207DE000F8948091E800877F8093E8009ADD8091E80080FFFCCF8091E3008078812B809398
:207E0000E30080688093E300112311F482E001C083E08EBB0FBF4DC08058823008F049C033
:207E20008091B8019091B9016091BA01AE014F5F5F4F36DDBC01009709F43BC08091E8008E
:207E4000877F8093E80089819A8192DE8091E8008B778093E8002DC0803859F58091E800AA
:207E6000877F8093E8008091B4018093F1008091E8008E778093E80054DD1BC08823C9F4CA
:207E80009091B8019230A8F48091E800877F8093E8009093B40145DD8091B401882331F420
:207EA0008091E30087FD02C081E001C084E08EBB6CDC8091E80083FF0AC08091EB0080624E
:207EC0008093EB008091E800877F8093E8000F900F90CF91DF911F910F91089508951F9360
:207EE0008EB3882361F01091E9001092E9008091E80083FF01C0E4DE17701093E9001F916F
:207F00000895F999FECF92BD81BDF89A992780B50895262FF999FECF1FBA92BD81BD20BDBD
:207F20000FB6F894FA9AF99A0FBE01960895F894FFCF4341544552494E4100777700080031
:207F40000000000000080112011001020000084123360001000201000109023E00020100FF
:207F600080320904000001020201000524001001042402040524060001070582030800FF0C
:207F800009040100020A000000070504021000010705830210000104030904220341007216
:207FA000006400750069006E006F0020004C0065006F006E006100720064006F0000001836
:1A7FC00003410072006400750069006E006F0020004C004C004300000000D7
:00000001FF
40 changes: 40 additions & 0 deletions examples/board_simduino_leonardo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Caterina-Leonardo-bootloader.hex
================================

Arduino's Caterina bootloader, built for the Leonardo, is a modified
version of the CDC class bootloader of Dean Camera's LUFA library, built
against LUFA 111009. The image comes from Arduino's ArduinoCore-avr
repository, https://github.com/arduino/ArduinoCore-avr:

bootloaders/caterina/Caterina-Leonardo.hex
commit 11b9130371e8447920edb65a75706a6c951e51fc
git blob 47053bc531df86341eb31c361e1ac42f7ef8ad56

The records below address 0x7000 were removed. They hold a sketch built
with the Arduino core, which is not part of the bootloader and is not
covered by the notice below. The bootloader sources,
bootloaders/caterina/Caterina.c and the files next to it, carry LUFA's
license, which LUFA's documentation calls the MIT license. Its notice,
as it appears in those sources:

Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)

Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.

The USB vendor and product IDs in the image (2341:0036) are Arduino's.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012 Torbjorn Tyridal <[email protected]>
#
# Arduino Leonardo with the Caterina bootloader over USB/IP, see README.
#
# This file is part of simavr.
#
Expand All @@ -16,46 +16,28 @@
# You should have received a copy of the GNU General Public License
# along with simavr. If not, see <http://www.gnu.org/licenses/>.

target= simusb
firm_src = at90usb162_cdc_loopback.c
firmware = ${firm_src:.c=.hex}
target= simduino_leonardo
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

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

include ${simavr}/Makefile.common

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

${board} : ${OBJ}/vhci_usb.o
${board} : ${OBJ}/usbip_usb.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
rm -rf *.a *.axf *.hex ${target} *.vcd
clean: clean-${OBJ}
rm -rf *.a *.axf ${target} *.vcd
88 changes: 88 additions & 0 deletions examples/board_simduino_leonardo/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
simduino_leonardo is an Arduino Leonardo: an ATmega32U4 at 16MHz running
Arduino's Caterina bootloader, with the board's USB port exported over
USB/IP. Once a USB/IP client attaches it, it is a Leonardo plugged into
the host, and avrdude, the Arduino IDE or arduino-cli upload sketches to
it the same way.

make
./obj-*/simduino_leonardo.elf

Linux
-----
The client is the mainline vhci-hcd driver (USBIP_VHCI_HCD in the kernel
configuration) and the usbip tool, packaged as usbip, linux-tools or
net-misc/usbip depending on the distribution:

sudo modprobe vhci-hcd
usbip list -r 127.0.0.1
sudo usbip attach -r 127.0.0.1 -b 1-1

The bootloader shows up as 2341:0036 and a /dev/ttyACM device, which
avrdude programs:

avrdude -c avr109 -p m32u4 -P /dev/ttyACM0 -U flash:w:sketch.hex

"sudo usbip port" lists attached devices and "sudo usbip detach -p
<port>" removes one.

If opening the port fails with an I/O error, and the kernel log shows the
board being reset and reconnected under a new device number, USB
autosuspend is enabled (power/control is "auto", often set by a power
management tool). Caterina is built without the answer to GET_STATUS for
the device, which Linux asks for when it resumes a suspended device; it
then resets the board and takes it for a new one. A real Leonardo in its
bootloader does the same. Turn autosuspend off for devices that appear
from then on, until the next boot:

echo -1 | sudo tee /sys/module/usbcore/parameters/autosuspend

or for the attached board only, until it re-enumerates:

echo on | sudo tee /sys/bus/usb/devices/<port>/power/control

Windows
-------
Build simavr in MSYS2 as usual. The client is usbip-win2
(https://github.com/vadimgrn/usbip-win2, or "winget install
vadimgrn.usbip-win2"), Windows 10 1903 or later. From an administrator
prompt:

usbip list -r 127.0.0.1
usbip attach -r 127.0.0.1 -b 1-1

The board then has a COM port.

Resets and reattaching
----------------------
As on the board, the flash starts erased, and Caterina waits in the
bootloader until it is given a sketch. After a power-on with a sketch in
the flash, Caterina starts it at once. After the reset button, it waits
8 seconds for an upload first. -r starts the simulation as after the
reset button, and Ctrl-\ presses it while it runs (not on Windows). A
sketch built with the Arduino core resets into the bootloader when the
port is opened at 1200 baud and closed, the usual way to upload, through
a watchdog reset.

Each time the firmware disconnects from the bus, when the bootloader
starts the sketch, at a reset, or when a sketch is reset into the
bootloader, the USB/IP connection is closed and the board has to be
attached again, which is when a real board re-enumerates.

The flash and the EEPROM are kept in simduino_leonardo_memory.hex in the
current directory, an Intel HEX file with the EEPROM at simavr's 0x810000
offset. It is written back when simduino_leonardo stops (Ctrl-C included),
so the next run finds the sketch uploaded last and what it stored. -F
names another .hex file, -n starts erased and keeps nothing. A sketch
given on the command line, .hex or .elf, is programmed over it at start.

simduino_leonardo.elf -h lists the options. The server only listens on
127.0.0.1 unless given another address with -a; "-a 0.0.0.0" lets other
machines attach the board.

The bootloader
--------------
Caterina-Leonardo-bootloader.hex is Arduino's Caterina-Leonardo.hex with
the records below 0x7000 removed, which hold a demonstration sketch, so
that only the bootloader remains, at 0x7000 to 0x7FD9. See LICENSE.

USB/IP: https://docs.kernel.org/usb/usbip_protocol.html
Loading
Loading