Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ endif
-Wl,--relax,--gc-sections \
-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
-I$(SIMAVR)/sim/avr -I../simavr/sim/avr -I../../simavr/sim/avr \
${^} -o ${@}
${filter-out %.h, ${^}} -o ${@}
@${AVR}size ${@}|sed '1d'

# this rule has precedence
Expand Down
6 changes: 6 additions & 0 deletions doc/Intro.html
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,12 @@ <H5>Other peripherals.</H5>
and the omission should only be visible if the pins used
are also linked to unrelated external circuitry.

<H5 id="usb">USB.</H5>
The USB device controller is like the other communication peripherals:
the firmware sees its registers, and the program using <I>libsimavr</I>
plays the USB host, moving packets in and out of the endpoints with IOCTLs.
See <A href="USB.html">USB in simavr</A>.

<H5>Simavr Timers.</H5>
The implementation of peripherals such as serial communications and the ADC
(and the AVR's own timers!) requires timed events that are independent
Expand Down
66 changes: 66 additions & 0 deletions doc/USB.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<HTML>
<TITLE>
USB in simavr.
</TITLE>
<BODY style="max-width:700px; margin:auto;">

<H3>USB in simavr.</H3>
The USB device controller is simulated on the AT90USB82/162,
ATmega8U2/16U2/32U2 and ATmega16U4/32U4, in device mode only.
There is no simulated cable or host controller.
Instead the program using <I>libsimavr</I> acts as the USB host,
moving packets in and out of the endpoints with the IOCTLs in
<I><A href="https://raw.githubusercontent.com/buserror/simavr/refs/heads/master/simavr/sim/avr_usb.h">
avr_usb.h
</A></I>.
<P>
AVR_IOCTL_USB_SETUP, AVR_IOCTL_USB_WRITE and AVR_IOCTL_USB_READ
take a <I>struct avr_io_usb</I> with an endpoint number, a size and a buffer.
They return 0, AVR_IOCTL_USB_NAK if the firmware is not ready
(try again after running it for a while) or AVR_IOCTL_USB_STALL.
After a read, the size field holds the number of bytes received.
A control transfer is a SETUP followed by its data and status stages,
which are ordinary reads and writes on endpoint 0,
the status stage being a transfer of zero bytes.
AVR_IOCTL_USB_RESET signals a bus reset and AVR_IOCTL_USB_VBUS,
with a parameter of 1 or 0, plugs or unplugs the cable.
That matters on the ATmega16U4/32U4: as on real hardware,
firmware that waits for VBUS before connecting needs it.
The USB_IRQ_ATTACH IRQ reports when the firmware connects to or
disconnects from the bus.
<PRE>
avr_irq_register_notify(
avr_io_getirq(avr, AVR_IOCTL_USB_GETIRQ(), USB_IRQ_ATTACH),
attach_hook, NULL);
avr_ioctl(avr, AVR_IOCTL_USB_VBUS, (void *)1);

... run until attach_hook() sees 1 ...

avr_ioctl(avr, AVR_IOCTL_USB_RESET, NULL);

uint8_t setup[8] = { 0x80, 6, 0, 1, 0, 0, 18, 0 }; // GET_DESCRIPTOR(device)
uint8_t desc[18];
struct avr_io_usb pkt = { 0, sizeof setup, setup };

avr_ioctl(avr, AVR_IOCTL_USB_SETUP, &pkt);
pkt.sz = sizeof desc;
pkt.buf = desc;
while (avr_ioctl(avr, AVR_IOCTL_USB_READ, &pkt) == AVR_IOCTL_USB_NAK)
... run the firmware for a little, restore pkt.sz ...
</PRE>
<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.

</BODY>
</HTML>
44 changes: 44 additions & 0 deletions simavr/cores/sim_mega16u2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
sim_mega16u2.c

ATmega16U2, see sim_megaxu2.h

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 "sim_avr.h"

#define SIM_MMCU "atmega16u2"
#define SIM_CORENAME mcu_mega16u2

#define _AVR_IO_H_
#define __ASSEMBLER__
#ifndef __AVR_ATmega16U2__
#define __AVR_ATmega16U2__
#endif
#include "avr/iom16u2.h"

#include "sim_megaxu2.h"

static avr_t * make()
{
return avr_core_allocate(&SIM_CORENAME.core, sizeof(struct mcu_t));
}

avr_kind_t mega16u2 = {
.names = { "atmega16u2" },
.make = make
};
44 changes: 44 additions & 0 deletions simavr/cores/sim_mega16u4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
sim_mega16u4.c

ATmega16U4, see sim_megaxu4.h

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 "sim_avr.h"

#define SIM_MMCU "atmega16u4"
#define SIM_CORENAME mcu_mega16u4

#define _AVR_IO_H_
#define __ASSEMBLER__
#ifndef __AVR_ATmega16U4__
#define __AVR_ATmega16U4__
#endif
#include "avr/iom16u4.h"

#include "sim_megaxu4.h"

static avr_t * make()
{
return avr_core_allocate(&SIM_CORENAME.core, sizeof(struct mcu_t));
}

avr_kind_t mega16u4 = {
.names = { "atmega16u4", },
.make = make
};
44 changes: 44 additions & 0 deletions simavr/cores/sim_mega32u2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
sim_mega32u2.c

ATmega32U2, see sim_megaxu2.h

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 "sim_avr.h"

#define SIM_MMCU "atmega32u2"
#define SIM_CORENAME mcu_mega32u2

#define _AVR_IO_H_
#define __ASSEMBLER__
#ifndef __AVR_ATmega32U2__
#define __AVR_ATmega32U2__
#endif
#include "avr/iom32u2.h"

#include "sim_megaxu2.h"

static avr_t * make()
{
return avr_core_allocate(&SIM_CORENAME.core, sizeof(struct mcu_t));
}

avr_kind_t mega32u2 = {
.names = { "atmega32u2" },
.make = make
};
Loading
Loading