diff --git a/Makefile.common b/Makefile.common index 4fb582d1f..103005afe 100644 --- a/Makefile.common +++ b/Makefile.common @@ -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 diff --git a/doc/Intro.html b/doc/Intro.html index a5e38ed0d..31c2b1fd1 100644 --- a/doc/Intro.html +++ b/doc/Intro.html @@ -680,6 +680,12 @@
Other peripherals.
and the omission should only be visible if the pins used are also linked to unrelated external circuitry. +
USB.
+The USB device controller is like the other communication peripherals: +the firmware sees its registers, and the program using libsimavr +plays the USB host, moving packets in and out of the endpoints with IOCTLs. +See USB in simavr. +
Simavr Timers.
The implementation of peripherals such as serial communications and the ADC (and the AVR's own timers!) requires timed events that are independent diff --git a/doc/USB.html b/doc/USB.html new file mode 100644 index 000000000..d57c70cd0 --- /dev/null +++ b/doc/USB.html @@ -0,0 +1,66 @@ + + +USB in simavr. + + + +

USB in simavr.

+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 libsimavr acts as the USB host, +moving packets in and out of the endpoints with the IOCTLs in + + avr_usb.h +. +

+AVR_IOCTL_USB_SETUP, AVR_IOCTL_USB_WRITE and AVR_IOCTL_USB_READ +take a struct avr_io_usb 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. +

+	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 ...
+
+tests/test_usb_host.h is a complete example that enumerates a device +and exchanges bulk data. +

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

+examples/extra_board_usb 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. + + + diff --git a/simavr/cores/sim_mega16u2.c b/simavr/cores/sim_mega16u2.c new file mode 100644 index 000000000..bdd21b500 --- /dev/null +++ b/simavr/cores/sim_mega16u2.c @@ -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 . + */ + +#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 +}; diff --git a/simavr/cores/sim_mega16u4.c b/simavr/cores/sim_mega16u4.c new file mode 100644 index 000000000..e7889ff4d --- /dev/null +++ b/simavr/cores/sim_mega16u4.c @@ -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 . + */ + +#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 +}; diff --git a/simavr/cores/sim_mega32u2.c b/simavr/cores/sim_mega32u2.c new file mode 100644 index 000000000..980a73718 --- /dev/null +++ b/simavr/cores/sim_mega32u2.c @@ -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 . + */ + +#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 +}; diff --git a/simavr/cores/sim_mega32u4.c b/simavr/cores/sim_mega32u4.c index dc3102dbd..1f91e66ae 100644 --- a/simavr/cores/sim_mega32u4.c +++ b/simavr/cores/sim_mega32u4.c @@ -3,7 +3,7 @@ Copyright 2008, 2009 Michel Pollet - This file is part of simavr. + 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 @@ -19,38 +19,10 @@ along with simavr. If not, see . */ -#define SIM_VECTOR_SIZE 4 -#define SIM_MMCU "atmega32u4" -#define SIM_CORENAME mcu_mega32u4 -#define USBRF 5 // missing in avr/iom32u4.h - -#define PD7 7 -#define PD6 6 -#define PD5 5 -#define PD4 4 -#define PD3 3 -#define PD2 2 -#define PD1 1 -#define PD0 0 - -#define PE6 6 - #include "sim_avr.h" -#include "avr_eeprom.h" -#include "avr_flash.h" -#include "avr_watchdog.h" -#include "avr_extint.h" -#include "avr_ioport.h" -#include "avr_uart.h" -#include "avr_adc.h" -#include "avr_timer.h" -#include "avr_spi.h" -#include "avr_twi.h" -#include "avr_acomp.h" -#include "avr_usb.h" -void m32u4_init(struct avr_t * avr); -void m32u4_reset(struct avr_t * avr); +#define SIM_MMCU "atmega32u4" +#define SIM_CORENAME mcu_mega32u4 #define _AVR_IO_H_ #define __ASSEMBLER__ @@ -59,411 +31,14 @@ void m32u4_reset(struct avr_t * avr); #endif #include "avr/iom32u4.h" -#include "sim_core_declare.h" - -/* - * ATmega32u4 definitions - */ -const struct mcu_t { - avr_t core; - avr_ioport_t portb, portc, portd, porte, portf; - avr_eeprom_t eeprom; - avr_flash_t selfprog; - avr_watchdog_t watchdog; - avr_extint_t extint; - avr_uart_t uart1; - avr_acomp_t acomp; - avr_adc_t adc; - avr_timer_t timer0, timer1, timer3; - avr_spi_t spi; - avr_twi_t twi; - avr_usb_t usb; -} mcu_mega32u4 = { - .core = { - .mmcu = "atmega32u4", - DEFAULT_CORE(4), - - .init = m32u4_init, - .reset = m32u4_reset, - - .rampz = RAMPZ, - }, - .portb = { - .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB, - .pcint = { - .enable = AVR_IO_REGBIT(PCICR, PCIE0), - .raised = AVR_IO_REGBIT(PCIFR, PCIF0), - .vector = PCINT0_vect, - }, - .r_pcint = PCMSK0, - }, - AVR_IOPORT_DECLARE(c, 'C', C), - AVR_IOPORT_DECLARE(d, 'D', D), - AVR_IOPORT_DECLARE(e, 'E', E), - AVR_IOPORT_DECLARE(f, 'F', F), - - AVR_EEPROM_DECLARE(EE_READY_vect), - AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect), - AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect), - .extint = { - AVR_EXTINT_MEGA_DECLARE(0, 'D', PD0, A), - AVR_EXTINT_MEGA_DECLARE(1, 'D', PD1, A), - AVR_EXTINT_MEGA_DECLARE(2, 'D', PD2, A), - AVR_EXTINT_MEGA_DECLARE(3, 'D', PD3, A), - AVR_EXTINT_MEGA_DECLARE(6, 'E', PE6, B), - }, - AVR_UARTX_DECLARE(1, PRR1, PRUSART1), - - .acomp = { - .mux_inputs = 8, - .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1), - AVR_IO_REGBIT(ADMUX, MUX2) }, - .pradc = AVR_IO_REGBIT(PRR0, PRADC), - .aden = AVR_IO_REGBIT(ADCSRA, ADEN), - .acme = AVR_IO_REGBIT(ADCSRB, ACME), - - .r_acsr = ACSR, - .acis = { AVR_IO_REGBIT(ACSR, ACIS0), AVR_IO_REGBIT(ACSR, ACIS1) }, - .acic = AVR_IO_REGBIT(ACSR, ACIC), - .aco = AVR_IO_REGBIT(ACSR, ACO), - .acbg = AVR_IO_REGBIT(ACSR, ACBG), - .disabled = AVR_IO_REGBIT(ACSR, ACD), - - .timer_name = '1', - - .ac = { - .enable = AVR_IO_REGBIT(ACSR, ACIE), - .raised = AVR_IO_REGBIT(ACSR, ACI), - .vector = ANALOG_COMP_vect, - } - }, - .adc = { - .r_admux = ADMUX, - .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1), - AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3), - AVR_IO_REGBIT(ADMUX, MUX4),AVR_IO_REGBIT(ADCSRB, MUX5),}, - .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)}, - .ref_values = { [0] = ADC_VREF_AREF, [1] = ADC_VREF_AVCC, - //[2] = Reserved: any constant to mark this? - [3] = ADC_VREF_V256 }, - - .adlar = AVR_IO_REGBIT(ADMUX, ADLAR), - .r_adcsra = ADCSRA, - .aden = AVR_IO_REGBIT(ADCSRA, ADEN), - .adsc = AVR_IO_REGBIT(ADCSRA, ADSC), - .adate = AVR_IO_REGBIT(ADCSRA, ADATE), - .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),}, - - .r_adch = ADCH, - .r_adcl = ADCL, - - .r_adcsrb = ADCSRB, - .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), - AVR_IO_REGBIT(ADCSRB, ADTS2), AVR_IO_REGBIT(ADCSRB, ADTS3),}, - .adts_op = { - [0] = avr_adts_free_running, - [1] = avr_adts_analog_comparator_0, - [2] = avr_adts_external_interrupt_0, - [3] = avr_adts_timer_0_compare_match_a, - [4] = avr_adts_timer_0_overflow, - [5] = avr_adts_timer_1_compare_match_b, - [6] = avr_adts_timer_1_overflow, - [7] = avr_adts_timer_1_capture_event, - // follows 4 sources for unsupported fast timer4 - }, - - .muxmode = { - [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1), - - // Not available: 2 items - [2] = AVR_ADC_DIFF(0, 0, 1), [3] = AVR_ADC_DIFF(0, 0, 1), - - [4] = AVR_ADC_SINGLE(4), [5] = AVR_ADC_SINGLE(5), - [6] = AVR_ADC_SINGLE(6), [7] = AVR_ADC_SINGLE(7), - - // Not available: 1 item - [ 8] = AVR_ADC_DIFF(0, 0, 10), - - [ 9] = AVR_ADC_DIFF(1, 0, 10), - - // Not available: 1 item - [10] = AVR_ADC_DIFF(0, 0, 200), - - [11] = AVR_ADC_DIFF(1, 0, 200), - - // Not available: 4 items - [12] = AVR_ADC_DIFF(0, 0, 1), [13] = AVR_ADC_DIFF(0, 0, 1), - [14] = AVR_ADC_DIFF(0, 0, 1), [15] = AVR_ADC_DIFF(0, 0, 1), - - [16] = AVR_ADC_DIFF(0, 1, 1), - - // Not available: 3 items - [17] = AVR_ADC_DIFF(0, 0, 1), - [18] = AVR_ADC_DIFF(0, 0, 1), - [19] = AVR_ADC_DIFF(0, 0, 1), - - [20] = AVR_ADC_DIFF(4, 1, 1), [21] = AVR_ADC_DIFF(5, 1, 1), - [22] = AVR_ADC_DIFF(6, 1, 1), [23] = AVR_ADC_DIFF(7, 1, 1), - - // Not available: 6 items - [24] = AVR_ADC_DIFF(0, 0, 1), [25] = AVR_ADC_DIFF(0, 0, 1), - [26] = AVR_ADC_DIFF(0, 0, 1), [27] = AVR_ADC_DIFF(0, 0, 1), - [28] = AVR_ADC_DIFF(0, 0, 1), [29] = AVR_ADC_DIFF(0, 0, 1), - - [30] = AVR_ADC_REF(1100), // 1.1V - [31] = AVR_ADC_REF(0), // GND - - [32] = AVR_ADC_SINGLE( 8), [33] = AVR_ADC_SINGLE( 9), - [34] = AVR_ADC_SINGLE(10), [35] = AVR_ADC_SINGLE(11), - [36] = AVR_ADC_SINGLE(12), [37] = AVR_ADC_SINGLE(13), - - [38] = AVR_ADC_DIFF(1, 0, 40), - [39] = AVR_ADC_TEMP(), - - [40] = AVR_ADC_DIFF(4, 0, 10), [41] = AVR_ADC_DIFF(5, 0, 10), - [42] = AVR_ADC_DIFF(6, 0, 10), [43] = AVR_ADC_DIFF(7, 0, 10), - [44] = AVR_ADC_DIFF(4, 1, 10), [45] = AVR_ADC_DIFF(5, 1, 10), - [46] = AVR_ADC_DIFF(6, 1, 10), [47] = AVR_ADC_DIFF(7, 1, 10), - - [48] = AVR_ADC_DIFF(4, 0, 40), [49] = AVR_ADC_DIFF(5, 0, 40), - [50] = AVR_ADC_DIFF(6, 0, 40), [51] = AVR_ADC_DIFF(7, 0, 40), - [52] = AVR_ADC_DIFF(4, 1, 40), [53] = AVR_ADC_DIFF(5, 1, 40), - [54] = AVR_ADC_DIFF(6, 1, 40), [55] = AVR_ADC_DIFF(7, 1, 40), - - [56] = AVR_ADC_DIFF(4, 0, 200), [57] = AVR_ADC_DIFF(5, 0, 200), - [58] = AVR_ADC_DIFF(6, 0, 200), [59] = AVR_ADC_DIFF(7, 0, 200), - [60] = AVR_ADC_DIFF(4, 1, 200), [61] = AVR_ADC_DIFF(5, 1, 200), - [62] = AVR_ADC_DIFF(6, 1, 200), [63] = AVR_ADC_DIFF(7, 1, 200), - }, - - .adc = { - .enable = AVR_IO_REGBIT(ADCSRA, ADIE), - .raised = AVR_IO_REGBIT(ADCSRA, ADIF), - .vector = ADC_vect, - }, - }, - - .timer0 = { - .name = '0', - .disabled = AVR_IO_REGBIT(PRR0, PRTIM0), - .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) }, - WGM_OPS_8, - .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) }, - .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ }, - - .r_tcnt = TCNT0, - - .overflow = { - .enable = AVR_IO_REGBIT(TIMSK0, TOIE0), - .raised = AVR_IO_REGBIT(TIFR0, TOV0), - .vector = TIMER0_OVF_vect, - }, - .comp = { - [AVR_TIMER_COMPA] = { - .r_ocr = OCR0A, - .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTB, 7), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A), - .raised = AVR_IO_REGBIT(TIFR0, OCF0A), - .vector = TIMER0_COMPA_vect, - }, - }, - [AVR_TIMER_COMPB] = { - .r_ocr = OCR0B, - .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTD, 0), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B), - .raised = AVR_IO_REGBIT(TIFR0, OCF0B), - .vector = TIMER0_COMPB_vect, - } - } - } - }, - .timer1 = { - .name = '1', - .disabled = AVR_IO_REGBIT(PRR0, PRTIM1), - .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11), - AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) }, - WGM_OPS_16, - .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) }, - .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */}, - - .r_tcnt = TCNT1L, - .r_tcnth = TCNT1H, - .r_icr = ICR1L, - .r_icrh = ICR1H, - - .ices = AVR_IO_REGBIT(TCCR1B, ICES1), - .icp = AVR_IO_REGBIT(PORTD, 4), - - .overflow = { - .enable = AVR_IO_REGBIT(TIMSK1, TOIE1), - .raised = AVR_IO_REGBIT(TIFR1, TOV1), - .vector = TIMER1_OVF_vect, - }, - .icr = { - .enable = AVR_IO_REGBIT(TIMSK1, ICIE1), - .raised = AVR_IO_REGBIT(TIFR1, ICF1), - .vector = TIMER1_CAPT_vect, - }, - .comp = { - [AVR_TIMER_COMPA] = { - .r_ocr = OCR1AL, - .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it - .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTB, 5), - .foc = AVR_IO_REGBIT(TCCR1C, FOC1A), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A), - .raised = AVR_IO_REGBIT(TIFR1, OCF1A), - .vector = TIMER1_COMPA_vect, - } - }, - [AVR_TIMER_COMPB] = { - .r_ocr = OCR1BL, - .r_ocrh = OCR1BH, - .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTB, 6), - .foc = AVR_IO_REGBIT(TCCR1C, FOC1B), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B), - .raised = AVR_IO_REGBIT(TIFR1, OCF1B), - .vector = TIMER1_COMPB_vect, - } - } - } - }, - .timer3 = { - .name = '3', - .disabled = AVR_IO_REGBIT(PRR1, PRTIM3), - .wgm = { AVR_IO_REGBIT(TCCR3A, WGM30), AVR_IO_REGBIT(TCCR3A, WGM31), - AVR_IO_REGBIT(TCCR3B, WGM32), AVR_IO_REGBIT(TCCR3B, WGM33) }, - WGM_OPS_16, - .cs = { AVR_IO_REGBIT(TCCR3B, CS30), AVR_IO_REGBIT(TCCR3B, CS31), AVR_IO_REGBIT(TCCR3B, CS32) }, - .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */}, - - .r_tcnt = TCNT3L, - .r_tcnth = TCNT3H, - .r_icr = ICR3L, - .r_icrh = ICR3H, - - .ices = AVR_IO_REGBIT(TCCR3B, ICES3), - .icp = AVR_IO_REGBIT(PORTC, 7), - - .overflow = { - .enable = AVR_IO_REGBIT(TIMSK3, TOIE3), - .raised = AVR_IO_REGBIT(TIFR3, TOV3), - .vector = TIMER3_OVF_vect, - }, - .icr = { - .enable = AVR_IO_REGBIT(TIMSK3, ICIE3), - .raised = AVR_IO_REGBIT(TIFR3, ICF3), - .vector = TIMER3_CAPT_vect, - }, - .comp = { - [AVR_TIMER_COMPA] = { - .r_ocr = OCR3AL, - .r_ocrh = OCR3AH, // 16 bits timers have two bytes of it - .com = AVR_IO_REGBITS(TCCR3A, COM3A0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTC, 6), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK3, OCIE3A), - .raised = AVR_IO_REGBIT(TIFR3, OCF3A), - .vector = TIMER3_COMPA_vect, - } - }, - [AVR_TIMER_COMPB] = { - .r_ocr = OCR3BL, - .r_ocrh = OCR3BH, - .com = AVR_IO_REGBITS(TCCR3A, COM3B0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTC, 6), // WTF nothing in doc about this - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK3, OCIE3B), - .raised = AVR_IO_REGBIT(TIFR3, OCF3B), - .vector = TIMER3_COMPB_vect, - } - } - } - }, - // .timer4 = { /* TODO 10 bits realtime timer */ }, - AVR_SPI_DECLARE(PRR0, PRSPI), - .twi = { - - .r_twcr = TWCR, - .r_twsr = TWSR, - .r_twbr = TWBR, - .r_twdr = TWDR, - .r_twar = TWAR, - .r_twamr = TWAMR, - - .twen = AVR_IO_REGBIT(TWCR, TWEN), - .twea = AVR_IO_REGBIT(TWCR, TWEA), - .twsta = AVR_IO_REGBIT(TWCR, TWSTA), - .twsto = AVR_IO_REGBIT(TWCR, TWSTO), - .twwc = AVR_IO_REGBIT(TWCR, TWWC), - - .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f), // 5 bits - .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3), // 2 bits - - .twi = { - .enable = AVR_IO_REGBIT(TWCR, TWIE), - .raised = AVR_IO_REGBIT(TWCR, TWINT), - .raise_sticky = 1, - .vector = TWI_vect, - }, - }, - .usb = { - .name='1', - .disabled = AVR_IO_REGBIT(PRR1, PRUSB), // bit in the PRR - - .usbrf = AVR_IO_REGBIT(MCUSR, USBRF), // bit in the MCUSR - - .r_usbcon = USBCON, - .r_pllcsr = PLLCSR, - - .usb_com_vect = USB_COM_vect, - .usb_gen_vect = USB_GEN_vect, - }, -}; - +#include "sim_megaxu4.h" static avr_t * make() { - return avr_core_allocate(&mcu_mega32u4.core, sizeof(struct mcu_t)); + return avr_core_allocate(&SIM_CORENAME.core, sizeof(struct mcu_t)); } avr_kind_t mega32u4 = { .names = { "atmega32u4", }, .make = make }; - -void m32u4_init(struct avr_t * avr) -{ - struct mcu_t * mcu = (struct mcu_t*)avr; - - avr_ioport_init(avr, &mcu->portb); - avr_ioport_init(avr, &mcu->portc); - avr_ioport_init(avr, &mcu->portd); - avr_ioport_init(avr, &mcu->porte); - avr_ioport_init(avr, &mcu->portf); - avr_eeprom_init(avr, &mcu->eeprom); - avr_flash_init(avr, &mcu->selfprog); - avr_extint_init(avr, &mcu->extint); - avr_watchdog_init(avr, &mcu->watchdog); - avr_uart_init(avr, &mcu->uart1); - avr_acomp_init(avr, &mcu->acomp); - avr_adc_init(avr, &mcu->adc); - avr_timer_init(avr, &mcu->timer0); - avr_timer_init(avr, &mcu->timer1); - avr_timer_init(avr, &mcu->timer3); - avr_spi_init(avr, &mcu->spi); - avr_twi_init(avr, &mcu->twi); - avr_usb_init(avr, &mcu->usb); -} - -void m32u4_reset(struct avr_t * avr) -{ -// struct mcu_t * mcu = (struct mcu_t*)avr; -} diff --git a/simavr/cores/sim_mega8u2.c b/simavr/cores/sim_mega8u2.c new file mode 100644 index 000000000..2bcb5bd83 --- /dev/null +++ b/simavr/cores/sim_mega8u2.c @@ -0,0 +1,44 @@ +/* + sim_mega8u2.c + + ATmega8U2, 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 . + */ + +#include "sim_avr.h" + +#define SIM_MMCU "atmega8u2" +#define SIM_CORENAME mcu_mega8u2 + +#define _AVR_IO_H_ +#define __ASSEMBLER__ +#ifndef __AVR_ATmega8U2__ +#define __AVR_ATmega8U2__ +#endif +#include "avr/iom8u2.h" + +#include "sim_megaxu2.h" + +static avr_t * make() +{ + return avr_core_allocate(&SIM_CORENAME.core, sizeof(struct mcu_t)); +} + +avr_kind_t mega8u2 = { + .names = { "atmega8u2" }, + .make = make +}; diff --git a/simavr/cores/sim_megaxu2.c b/simavr/cores/sim_megaxu2.c new file mode 100644 index 000000000..6c85e5c2d --- /dev/null +++ b/simavr/cores/sim_megaxu2.c @@ -0,0 +1,46 @@ +/* + sim_megaxu2.c + + Common code for the AT90USB82/162 and ATmega8U2/16U2/32U2 cores. + + 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 . + */ + +#include "sim_megaxu2.h" + +void mxu2_init(struct avr_t * avr) +{ + struct mcu_t * mcu = (struct mcu_t*)avr; + + avr_ioport_init(avr, &mcu->portb); + avr_ioport_init(avr, &mcu->portc); + avr_ioport_init(avr, &mcu->portd); + avr_eeprom_init(avr, &mcu->eeprom); + avr_flash_init(avr, &mcu->selfprog); + avr_extint_init(avr, &mcu->extint); + avr_watchdog_init(avr, &mcu->watchdog); + avr_uart_init(avr, &mcu->uart1); + avr_timer_init(avr, &mcu->timer0); + avr_timer_init(avr, &mcu->timer1); + avr_spi_init(avr, &mcu->spi); + avr_usb_init(avr, &mcu->usb); + avr_acomp_init(avr, &mcu->acomp); +} + +void mxu2_reset(struct avr_t * avr) +{ +// struct mcu_t * mcu = (struct mcu_t*)avr; +} diff --git a/simavr/cores/sim_megaxu2.h b/simavr/cores/sim_megaxu2.h new file mode 100644 index 000000000..cdad6e721 --- /dev/null +++ b/simavr/cores/sim_megaxu2.h @@ -0,0 +1,245 @@ +/* + sim_megaxu2.h + + Template for the AT90USB82/162 and ATmega8U2/16U2/32U2, which have the + same USB controller. The ATmega8U2/16U2 are an improved version of the + AT90USB82/162 (Atmel AVR530, "Migrating from AT90USB162/82 to + ATmega16U2/8U2"). Besides electrical characteristics they add two + registers that were reserved on the AT90USB parts, neither of which + simavr models on either: + - WDTCKD (0x62), watchdog clock divider and early warning interrupt + - ACMUX (0x7D), selects AIN1..AIN6 as the comparator's negative input; + here it is always AIN1, as on the AT90USB parts + so the cores built from this template only differ in memory sizes. + Derived from sim_usb162.c, Copyright 2012 Torbjorn Tyridal + + 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 . + */ + +#ifndef __SIM_MEGAXU2_H__ +#define __SIM_MEGAXU2_H__ + +#include "sim_avr.h" +#include "sim_core_declare.h" +#include "avr_eeprom.h" +#include "avr_flash.h" +#include "avr_watchdog.h" +#include "avr_extint.h" +#include "avr_ioport.h" +#include "avr_uart.h" +#include "avr_timer.h" +#include "avr_spi.h" +#include "avr_usb.h" +#include "avr_acomp.h" + +void mxu2_init(struct avr_t * avr); +void mxu2_reset(struct avr_t * avr); + +struct mcu_t { + avr_t core; + avr_ioport_t portb, portc, portd; + avr_eeprom_t eeprom; + avr_flash_t selfprog; + avr_watchdog_t watchdog; + avr_extint_t extint; + avr_uart_t uart1; + avr_timer_t timer0,timer1; + avr_spi_t spi; + avr_usb_t usb; + avr_acomp_t acomp; +}; + +#ifdef SIM_CORENAME + +#ifndef SIM_MMCU +#error SIM_MMCU is not declared +#endif + +const struct mcu_t SIM_CORENAME = { + .core = { + .mmcu = SIM_MMCU, + DEFAULT_CORE(4), + + .init = mxu2_init, + .reset = mxu2_reset, + }, + .portb = { + .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB, + .pcint = { + .enable = AVR_IO_REGBIT(PCICR, PCIE0), + .raised = AVR_IO_REGBIT(PCIFR, PCIF0), + .vector = PCINT0_vect, + }, + .r_pcint = PCMSK0, + }, + .portc = { + .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC, + .pcint = { + .enable = AVR_IO_REGBIT(PCICR, PCIE1), + .raised = AVR_IO_REGBIT(PCIFR, PCIF1), + .vector = PCINT1_vect, + }, + .r_pcint = PCMSK1, + }, + AVR_IOPORT_DECLARE(d, 'D', D), + + AVR_EEPROM_DECLARE(EE_READY_vect), + AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect), + AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect), + .extint = { + AVR_EXTINT_MEGA_DECLARE(0, 'D', PIND0, A), + AVR_EXTINT_MEGA_DECLARE(1, 'D', PIND1, A), + AVR_EXTINT_MEGA_DECLARE(2, 'D', PIND2, A), + AVR_EXTINT_MEGA_DECLARE(3, 'D', PIND3, A), + AVR_EXTINT_MEGA_DECLARE(4, 'C', PINC7, B), + AVR_EXTINT_MEGA_DECLARE(5, 'D', PIND4, B), + AVR_EXTINT_MEGA_DECLARE(6, 'D', PIND6, B), + AVR_EXTINT_MEGA_DECLARE(7, 'D', PIND7, B), + }, + AVR_UARTX_DECLARE(1, PRR1, PRUSART1), + + .timer0 = { + .name = '0', + .disabled = AVR_IO_REGBIT(PRR0,PRTIM0), + .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) }, + WGM_OPS_8, + .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) }, + .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */, AVR_TIMER_EXTCLK_CHOOSE, AVR_TIMER_EXTCLK_CHOOSE }, + .ext_clock_pin = AVR_IO_REGBIT(PORTD, 7), /* External clock pin */ + + .r_tcnt = TCNT0, + + .overflow = { + .enable = AVR_IO_REGBIT(TIMSK0, TOIE0), + .raised = AVR_IO_REGBIT(TIFR0, TOV0), + .vector = TIMER0_OVF_vect, + }, + .comp = { + [AVR_TIMER_COMPA] = { + .r_ocr = OCR0A, + .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTD, 6), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A), + .raised = AVR_IO_REGBIT(TIFR0, OCF0A), + .vector = TIMER0_COMPA_vect, + }, + }, + [AVR_TIMER_COMPB] = { + .r_ocr = OCR0B, + .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTD, 5), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B), + .raised = AVR_IO_REGBIT(TIFR0, OCF0B), + .vector = TIMER0_COMPB_vect, + } + } + } + }, + .timer1 = { + .name = '1', + .disabled = AVR_IO_REGBIT(PRR0,PRTIM1), + .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11), + AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) }, + WGM_OPS_16, + .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) }, + .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */, AVR_TIMER_EXTCLK_CHOOSE, AVR_TIMER_EXTCLK_CHOOSE }, + .ext_clock_pin = AVR_IO_REGBIT(PORTB, 4), /* External clock pin */ + + .r_tcnt = TCNT1L, + .r_tcnth = TCNT1H, + .r_icr = ICR1L, + .r_icrh = ICR1H, + + .ices = AVR_IO_REGBIT(TCCR1B, ICES1), + .icp = AVR_IO_REGBIT(PORTB, 0), + + .overflow = { + .enable = AVR_IO_REGBIT(TIMSK1, TOIE1), + .raised = AVR_IO_REGBIT(TIFR1, TOV1), + .vector = TIMER1_OVF_vect, + }, + .icr = { + .enable = AVR_IO_REGBIT(TIMSK1, ICIE1), + .raised = AVR_IO_REGBIT(TIFR1, ICF1), + .vector = TIMER1_CAPT_vect, + }, + .comp = { + [AVR_TIMER_COMPA] = { + .r_ocr = OCR1AL, + .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it + .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTB, 1), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A), + .raised = AVR_IO_REGBIT(TIFR1, OCF1A), + .vector = TIMER1_COMPA_vect, + }, + }, + [AVR_TIMER_COMPB] = { + .r_ocr = OCR1BL, + .r_ocrh = OCR1BH, + .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTB, 2), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B), + .raised = AVR_IO_REGBIT(TIFR1, OCF1B), + .vector = TIMER1_COMPB_vect, + }, + }, + }, + }, + AVR_SPI_DECLARE(PRR0, PRSPI), + .usb = { + .name='1', + .disabled=AVR_IO_REGBIT(PRR1, PRUSB),// bit in the PRR + + .usbrf=AVR_IO_REGBIT(MCUSR,USBRF), // bit in the MCUSR + + .r_usbcon = USBCON, + .r_pllcsr=PLLCSR, + + .usb_com_vect=USB_COM_vect, + .usb_gen_vect=USB_GEN_vect, + + // 176 bytes DPRAM, EP0 64, EP1-2 64 single bank, EP3-4 64 dual bank + .ep_count = 5, + .dpram_size = 176, + .ep_size_max = { 64, 64, 64, 64, 64 }, + .ep_banks_max = { 1, 1, 1, 2, 2 }, + }, + .acomp = { + .r_acsr = ACSR, + .acis = { AVR_IO_REGBIT(ACSR, ACIS0), AVR_IO_REGBIT(ACSR, ACIS1) }, + .acic = AVR_IO_REGBIT(ACSR, ACIC), + .aco = AVR_IO_REGBIT(ACSR, ACO), + .acbg = AVR_IO_REGBIT(ACSR, ACBG), + .disabled = AVR_IO_REGBIT(ACSR, ACD), + + .timer_name = '1', + + .ac = { + .enable = AVR_IO_REGBIT(ACSR, ACIE), + .raised = AVR_IO_REGBIT(ACSR, ACI), + .vector = ANALOG_COMP_vect, + } + } +}; + +#endif /* SIM_CORENAME */ + +#endif /* __SIM_MEGAXU2_H__ */ diff --git a/simavr/cores/sim_megaxu4.c b/simavr/cores/sim_megaxu4.c new file mode 100644 index 000000000..2a438a80a --- /dev/null +++ b/simavr/cores/sim_megaxu4.c @@ -0,0 +1,51 @@ +/* + sim_megaxu4.c + + Common code for the ATmega16U4/32U4 cores. + + 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 . + */ + +#include "sim_megaxu4.h" + +void mxu4_init(struct avr_t * avr) +{ + struct mcu_t * mcu = (struct mcu_t*)avr; + + avr_ioport_init(avr, &mcu->portb); + avr_ioport_init(avr, &mcu->portc); + avr_ioport_init(avr, &mcu->portd); + avr_ioport_init(avr, &mcu->porte); + avr_ioport_init(avr, &mcu->portf); + avr_eeprom_init(avr, &mcu->eeprom); + avr_flash_init(avr, &mcu->selfprog); + avr_extint_init(avr, &mcu->extint); + avr_watchdog_init(avr, &mcu->watchdog); + avr_uart_init(avr, &mcu->uart1); + avr_acomp_init(avr, &mcu->acomp); + avr_adc_init(avr, &mcu->adc); + avr_timer_init(avr, &mcu->timer0); + avr_timer_init(avr, &mcu->timer1); + avr_timer_init(avr, &mcu->timer3); + avr_spi_init(avr, &mcu->spi); + avr_twi_init(avr, &mcu->twi); + avr_usb_init(avr, &mcu->usb); +} + +void mxu4_reset(struct avr_t * avr) +{ +// struct mcu_t * mcu = (struct mcu_t*)avr; +} diff --git a/simavr/cores/sim_megaxu4.h b/simavr/cores/sim_megaxu4.h new file mode 100644 index 000000000..d7f90c3f3 --- /dev/null +++ b/simavr/cores/sim_megaxu4.h @@ -0,0 +1,448 @@ +/* + sim_megaxu4.h + + Template for the ATmega16U4/32U4, they only differ in memory sizes. + Derived from sim_mega32u4.c, Copyright 2008, 2009 Michel Pollet + + 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 . + */ + +#ifndef __SIM_MEGAXU4_H__ +#define __SIM_MEGAXU4_H__ + +#ifndef USBRF +#define USBRF 5 // missing in avr/iomXXu4.h +#endif + +#ifndef PD7 +#define PD7 7 +#define PD6 6 +#define PD5 5 +#define PD4 4 +#define PD3 3 +#define PD2 2 +#define PD1 1 +#define PD0 0 +#endif +#ifndef PE6 +#define PE6 6 +#endif + +#include "sim_avr.h" +#include "sim_core_declare.h" +#include "avr_eeprom.h" +#include "avr_flash.h" +#include "avr_watchdog.h" +#include "avr_extint.h" +#include "avr_ioport.h" +#include "avr_uart.h" +#include "avr_adc.h" +#include "avr_timer.h" +#include "avr_spi.h" +#include "avr_twi.h" +#include "avr_acomp.h" +#include "avr_usb.h" + +void mxu4_init(struct avr_t * avr); +void mxu4_reset(struct avr_t * avr); + +struct mcu_t { + avr_t core; + avr_ioport_t portb, portc, portd, porte, portf; + avr_eeprom_t eeprom; + avr_flash_t selfprog; + avr_watchdog_t watchdog; + avr_extint_t extint; + avr_uart_t uart1; + avr_acomp_t acomp; + avr_adc_t adc; + avr_timer_t timer0, timer1, timer3; + avr_spi_t spi; + avr_twi_t twi; + avr_usb_t usb; +}; + +#ifdef SIM_CORENAME + +#ifndef SIM_MMCU +#error SIM_MMCU is not declared +#endif + +const struct mcu_t SIM_CORENAME = { + .core = { + .mmcu = SIM_MMCU, + DEFAULT_CORE(4), + + .init = mxu4_init, + .reset = mxu4_reset, + + .rampz = RAMPZ, + }, + .portb = { + .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB, + .pcint = { + .enable = AVR_IO_REGBIT(PCICR, PCIE0), + .raised = AVR_IO_REGBIT(PCIFR, PCIF0), + .vector = PCINT0_vect, + }, + .r_pcint = PCMSK0, + }, + AVR_IOPORT_DECLARE(c, 'C', C), + AVR_IOPORT_DECLARE(d, 'D', D), + AVR_IOPORT_DECLARE(e, 'E', E), + AVR_IOPORT_DECLARE(f, 'F', F), + + AVR_EEPROM_DECLARE(EE_READY_vect), + AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect), + AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect), + .extint = { + AVR_EXTINT_MEGA_DECLARE(0, 'D', PD0, A), + AVR_EXTINT_MEGA_DECLARE(1, 'D', PD1, A), + AVR_EXTINT_MEGA_DECLARE(2, 'D', PD2, A), + AVR_EXTINT_MEGA_DECLARE(3, 'D', PD3, A), + AVR_EXTINT_MEGA_DECLARE(6, 'E', PE6, B), + }, + AVR_UARTX_DECLARE(1, PRR1, PRUSART1), + + .acomp = { + .mux_inputs = 8, + .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1), + AVR_IO_REGBIT(ADMUX, MUX2) }, + .pradc = AVR_IO_REGBIT(PRR0, PRADC), + .aden = AVR_IO_REGBIT(ADCSRA, ADEN), + .acme = AVR_IO_REGBIT(ADCSRB, ACME), + + .r_acsr = ACSR, + .acis = { AVR_IO_REGBIT(ACSR, ACIS0), AVR_IO_REGBIT(ACSR, ACIS1) }, + .acic = AVR_IO_REGBIT(ACSR, ACIC), + .aco = AVR_IO_REGBIT(ACSR, ACO), + .acbg = AVR_IO_REGBIT(ACSR, ACBG), + .disabled = AVR_IO_REGBIT(ACSR, ACD), + + .timer_name = '1', + + .ac = { + .enable = AVR_IO_REGBIT(ACSR, ACIE), + .raised = AVR_IO_REGBIT(ACSR, ACI), + .vector = ANALOG_COMP_vect, + } + }, + .adc = { + .r_admux = ADMUX, + .mux = { AVR_IO_REGBIT(ADMUX, MUX0), AVR_IO_REGBIT(ADMUX, MUX1), + AVR_IO_REGBIT(ADMUX, MUX2), AVR_IO_REGBIT(ADMUX, MUX3), + AVR_IO_REGBIT(ADMUX, MUX4),AVR_IO_REGBIT(ADCSRB, MUX5),}, + .ref = { AVR_IO_REGBIT(ADMUX, REFS0), AVR_IO_REGBIT(ADMUX, REFS1)}, + .ref_values = { [0] = ADC_VREF_AREF, [1] = ADC_VREF_AVCC, + //[2] = Reserved: any constant to mark this? + [3] = ADC_VREF_V256 }, + + .adlar = AVR_IO_REGBIT(ADMUX, ADLAR), + .r_adcsra = ADCSRA, + .aden = AVR_IO_REGBIT(ADCSRA, ADEN), + .adsc = AVR_IO_REGBIT(ADCSRA, ADSC), + .adate = AVR_IO_REGBIT(ADCSRA, ADATE), + .adps = { AVR_IO_REGBIT(ADCSRA, ADPS0), AVR_IO_REGBIT(ADCSRA, ADPS1), AVR_IO_REGBIT(ADCSRA, ADPS2),}, + + .r_adch = ADCH, + .r_adcl = ADCL, + + .r_adcsrb = ADCSRB, + .adts = { AVR_IO_REGBIT(ADCSRB, ADTS0), AVR_IO_REGBIT(ADCSRB, ADTS1), + AVR_IO_REGBIT(ADCSRB, ADTS2), AVR_IO_REGBIT(ADCSRB, ADTS3),}, + .adts_op = { + [0] = avr_adts_free_running, + [1] = avr_adts_analog_comparator_0, + [2] = avr_adts_external_interrupt_0, + [3] = avr_adts_timer_0_compare_match_a, + [4] = avr_adts_timer_0_overflow, + [5] = avr_adts_timer_1_compare_match_b, + [6] = avr_adts_timer_1_overflow, + [7] = avr_adts_timer_1_capture_event, + // follows 4 sources for unsupported fast timer4 + }, + + .muxmode = { + [0] = AVR_ADC_SINGLE(0), [1] = AVR_ADC_SINGLE(1), + + // Not available: 2 items + [2] = AVR_ADC_DIFF(0, 0, 1), [3] = AVR_ADC_DIFF(0, 0, 1), + + [4] = AVR_ADC_SINGLE(4), [5] = AVR_ADC_SINGLE(5), + [6] = AVR_ADC_SINGLE(6), [7] = AVR_ADC_SINGLE(7), + + // Not available: 1 item + [ 8] = AVR_ADC_DIFF(0, 0, 10), + + [ 9] = AVR_ADC_DIFF(1, 0, 10), + + // Not available: 1 item + [10] = AVR_ADC_DIFF(0, 0, 200), + + [11] = AVR_ADC_DIFF(1, 0, 200), + + // Not available: 4 items + [12] = AVR_ADC_DIFF(0, 0, 1), [13] = AVR_ADC_DIFF(0, 0, 1), + [14] = AVR_ADC_DIFF(0, 0, 1), [15] = AVR_ADC_DIFF(0, 0, 1), + + [16] = AVR_ADC_DIFF(0, 1, 1), + + // Not available: 3 items + [17] = AVR_ADC_DIFF(0, 0, 1), + [18] = AVR_ADC_DIFF(0, 0, 1), + [19] = AVR_ADC_DIFF(0, 0, 1), + + [20] = AVR_ADC_DIFF(4, 1, 1), [21] = AVR_ADC_DIFF(5, 1, 1), + [22] = AVR_ADC_DIFF(6, 1, 1), [23] = AVR_ADC_DIFF(7, 1, 1), + + // Not available: 6 items + [24] = AVR_ADC_DIFF(0, 0, 1), [25] = AVR_ADC_DIFF(0, 0, 1), + [26] = AVR_ADC_DIFF(0, 0, 1), [27] = AVR_ADC_DIFF(0, 0, 1), + [28] = AVR_ADC_DIFF(0, 0, 1), [29] = AVR_ADC_DIFF(0, 0, 1), + + [30] = AVR_ADC_REF(1100), // 1.1V + [31] = AVR_ADC_REF(0), // GND + + [32] = AVR_ADC_SINGLE( 8), [33] = AVR_ADC_SINGLE( 9), + [34] = AVR_ADC_SINGLE(10), [35] = AVR_ADC_SINGLE(11), + [36] = AVR_ADC_SINGLE(12), [37] = AVR_ADC_SINGLE(13), + + [38] = AVR_ADC_DIFF(1, 0, 40), + [39] = AVR_ADC_TEMP(), + + [40] = AVR_ADC_DIFF(4, 0, 10), [41] = AVR_ADC_DIFF(5, 0, 10), + [42] = AVR_ADC_DIFF(6, 0, 10), [43] = AVR_ADC_DIFF(7, 0, 10), + [44] = AVR_ADC_DIFF(4, 1, 10), [45] = AVR_ADC_DIFF(5, 1, 10), + [46] = AVR_ADC_DIFF(6, 1, 10), [47] = AVR_ADC_DIFF(7, 1, 10), + + [48] = AVR_ADC_DIFF(4, 0, 40), [49] = AVR_ADC_DIFF(5, 0, 40), + [50] = AVR_ADC_DIFF(6, 0, 40), [51] = AVR_ADC_DIFF(7, 0, 40), + [52] = AVR_ADC_DIFF(4, 1, 40), [53] = AVR_ADC_DIFF(5, 1, 40), + [54] = AVR_ADC_DIFF(6, 1, 40), [55] = AVR_ADC_DIFF(7, 1, 40), + + [56] = AVR_ADC_DIFF(4, 0, 200), [57] = AVR_ADC_DIFF(5, 0, 200), + [58] = AVR_ADC_DIFF(6, 0, 200), [59] = AVR_ADC_DIFF(7, 0, 200), + [60] = AVR_ADC_DIFF(4, 1, 200), [61] = AVR_ADC_DIFF(5, 1, 200), + [62] = AVR_ADC_DIFF(6, 1, 200), [63] = AVR_ADC_DIFF(7, 1, 200), + }, + + .adc = { + .enable = AVR_IO_REGBIT(ADCSRA, ADIE), + .raised = AVR_IO_REGBIT(ADCSRA, ADIF), + .vector = ADC_vect, + }, + }, + + .timer0 = { + .name = '0', + .disabled = AVR_IO_REGBIT(PRR0, PRTIM0), + .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) }, + WGM_OPS_8, + .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) }, + .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ }, + + .r_tcnt = TCNT0, + + .overflow = { + .enable = AVR_IO_REGBIT(TIMSK0, TOIE0), + .raised = AVR_IO_REGBIT(TIFR0, TOV0), + .vector = TIMER0_OVF_vect, + }, + .comp = { + [AVR_TIMER_COMPA] = { + .r_ocr = OCR0A, + .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTB, 7), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A), + .raised = AVR_IO_REGBIT(TIFR0, OCF0A), + .vector = TIMER0_COMPA_vect, + }, + }, + [AVR_TIMER_COMPB] = { + .r_ocr = OCR0B, + .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTD, 0), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B), + .raised = AVR_IO_REGBIT(TIFR0, OCF0B), + .vector = TIMER0_COMPB_vect, + } + } + } + }, + .timer1 = { + .name = '1', + .disabled = AVR_IO_REGBIT(PRR0, PRTIM1), + .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11), + AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) }, + WGM_OPS_16, + .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) }, + .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */}, + + .r_tcnt = TCNT1L, + .r_tcnth = TCNT1H, + .r_icr = ICR1L, + .r_icrh = ICR1H, + + .ices = AVR_IO_REGBIT(TCCR1B, ICES1), + .icp = AVR_IO_REGBIT(PORTD, 4), + + .overflow = { + .enable = AVR_IO_REGBIT(TIMSK1, TOIE1), + .raised = AVR_IO_REGBIT(TIFR1, TOV1), + .vector = TIMER1_OVF_vect, + }, + .icr = { + .enable = AVR_IO_REGBIT(TIMSK1, ICIE1), + .raised = AVR_IO_REGBIT(TIFR1, ICF1), + .vector = TIMER1_CAPT_vect, + }, + .comp = { + [AVR_TIMER_COMPA] = { + .r_ocr = OCR1AL, + .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it + .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTB, 5), + .foc = AVR_IO_REGBIT(TCCR1C, FOC1A), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A), + .raised = AVR_IO_REGBIT(TIFR1, OCF1A), + .vector = TIMER1_COMPA_vect, + } + }, + [AVR_TIMER_COMPB] = { + .r_ocr = OCR1BL, + .r_ocrh = OCR1BH, + .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTB, 6), + .foc = AVR_IO_REGBIT(TCCR1C, FOC1B), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B), + .raised = AVR_IO_REGBIT(TIFR1, OCF1B), + .vector = TIMER1_COMPB_vect, + } + } + } + }, + .timer3 = { + .name = '3', + .disabled = AVR_IO_REGBIT(PRR1, PRTIM3), + .wgm = { AVR_IO_REGBIT(TCCR3A, WGM30), AVR_IO_REGBIT(TCCR3A, WGM31), + AVR_IO_REGBIT(TCCR3B, WGM32), AVR_IO_REGBIT(TCCR3B, WGM33) }, + WGM_OPS_16, + .cs = { AVR_IO_REGBIT(TCCR3B, CS30), AVR_IO_REGBIT(TCCR3B, CS31), AVR_IO_REGBIT(TCCR3B, CS32) }, + .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */}, + + .r_tcnt = TCNT3L, + .r_tcnth = TCNT3H, + .r_icr = ICR3L, + .r_icrh = ICR3H, + + .ices = AVR_IO_REGBIT(TCCR3B, ICES3), + .icp = AVR_IO_REGBIT(PORTC, 7), + + .overflow = { + .enable = AVR_IO_REGBIT(TIMSK3, TOIE3), + .raised = AVR_IO_REGBIT(TIFR3, TOV3), + .vector = TIMER3_OVF_vect, + }, + .icr = { + .enable = AVR_IO_REGBIT(TIMSK3, ICIE3), + .raised = AVR_IO_REGBIT(TIFR3, ICF3), + .vector = TIMER3_CAPT_vect, + }, + .comp = { + [AVR_TIMER_COMPA] = { + .r_ocr = OCR3AL, + .r_ocrh = OCR3AH, // 16 bits timers have two bytes of it + .com = AVR_IO_REGBITS(TCCR3A, COM3A0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTC, 6), + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK3, OCIE3A), + .raised = AVR_IO_REGBIT(TIFR3, OCF3A), + .vector = TIMER3_COMPA_vect, + } + }, + [AVR_TIMER_COMPB] = { + .r_ocr = OCR3BL, + .r_ocrh = OCR3BH, + .com = AVR_IO_REGBITS(TCCR3A, COM3B0, 0x3), + .com_pin = AVR_IO_REGBIT(PORTC, 6), // WTF nothing in doc about this + .interrupt = { + .enable = AVR_IO_REGBIT(TIMSK3, OCIE3B), + .raised = AVR_IO_REGBIT(TIFR3, OCF3B), + .vector = TIMER3_COMPB_vect, + } + } + } + }, + // .timer4 = { /* TODO 10 bits realtime timer */ }, + AVR_SPI_DECLARE(PRR0, PRSPI), + .twi = { + + .r_twcr = TWCR, + .r_twsr = TWSR, + .r_twbr = TWBR, + .r_twdr = TWDR, + .r_twar = TWAR, + .r_twamr = TWAMR, + + .twen = AVR_IO_REGBIT(TWCR, TWEN), + .twea = AVR_IO_REGBIT(TWCR, TWEA), + .twsta = AVR_IO_REGBIT(TWCR, TWSTA), + .twsto = AVR_IO_REGBIT(TWCR, TWSTO), + .twwc = AVR_IO_REGBIT(TWCR, TWWC), + + .twsr = AVR_IO_REGBITS(TWSR, TWS3, 0x1f), // 5 bits + .twps = AVR_IO_REGBITS(TWSR, TWPS0, 0x3), // 2 bits + + .twi = { + .enable = AVR_IO_REGBIT(TWCR, TWIE), + .raised = AVR_IO_REGBIT(TWCR, TWINT), + .raise_sticky = 1, + .vector = TWI_vect, + }, + }, + .usb = { + .name='1', + .disabled = AVR_IO_REGBIT(PRR1, PRUSB), // bit in the PRR + + .usbrf = AVR_IO_REGBIT(MCUSR, USBRF), // bit in the MCUSR + + .r_usbcon = USBCON, + .r_pllcsr = PLLCSR, + + .usb_com_vect = USB_COM_vect, + .usb_gen_vect = USB_GEN_vect, + + // 832 bytes DPRAM, EP0 64, EP1 256, EP2-6 64, EP1-6 dual bank + .ep_count = 7, + .dpram_size = 832, + .ep_size_max = { 64, 256, 64, 64, 64, 64, 64 }, + .ep_banks_max = { 1, 2, 2, 2, 2, 2, 2 }, + + .vbus = AVR_IO_REGBIT(USBSTA, VBUS), + .id = AVR_IO_REGBIT(USBSTA, 1), // ID, missing in avr/iomXXu4.h + .vbusti = AVR_IO_REGBIT(USBINT, VBUSTI), + .vbuste = AVR_IO_REGBIT(USBCON, VBUSTE), + .otgpade = AVR_IO_REGBIT(USBCON, OTGPADE), + }, +}; + +#endif /* SIM_CORENAME */ + +#endif /* __SIM_MEGAXU4_H__ */ diff --git a/simavr/cores/sim_usb162.c b/simavr/cores/sim_usb162.c index bbd00b71c..41ee5b370 100644 --- a/simavr/cores/sim_usb162.c +++ b/simavr/cores/sim_usb162.c @@ -1,9 +1,11 @@ /* - sim_90usb162.c + sim_usb162.c + + AT90USB162, see sim_megaxu2.h Copyright 2012 Torbjorn Tyridal - This file is part of simavr. + 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 @@ -20,235 +22,25 @@ */ #include "sim_avr.h" -#include "avr_eeprom.h" -#include "avr_flash.h" -#include "avr_watchdog.h" -#include "avr_extint.h" -#include "avr_ioport.h" -#include "avr_uart.h" -#include "avr_adc.h" -#include "avr_timer.h" -#include "avr_spi.h" -#include "avr_usb.h" -#include "avr_acomp.h" -void usb162_init(struct avr_t * avr); -void usb162_reset(struct avr_t * avr); +#define SIM_MMCU "at90usb162" +#define SIM_CORENAME mcu_usb162 #define _AVR_IO_H_ #define __ASSEMBLER__ +#ifndef __AVR_AT90USB162__ +#define __AVR_AT90USB162__ +#endif #include "avr/iousb162.h" -#include "sim_core_declare.h" - -const struct mcu_t { - avr_t core; - avr_ioport_t portb, portc, portd; - avr_eeprom_t eeprom; - avr_flash_t selfprog; - avr_watchdog_t watchdog; - avr_extint_t extint; - avr_uart_t uart1; - avr_timer_t timer0,timer1; - avr_spi_t spi; - avr_usb_t usb; - avr_acomp_t acomp; -} mcu_usb162 = { - .core = { - .mmcu = "at90usb162", - DEFAULT_CORE(4), - - .init = usb162_init, - .reset = usb162_reset, - }, - .portb = { - .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB, - .pcint = { - .enable = AVR_IO_REGBIT(PCICR, PCIE0), - .raised = AVR_IO_REGBIT(PCIFR, PCIF0), - .vector = PCINT0_vect, - }, - .r_pcint = PCMSK0, - }, - .portc = { - .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC, - .pcint = { - .enable = AVR_IO_REGBIT(PCICR, PCIE1), - .raised = AVR_IO_REGBIT(PCIFR, PCIF1), - .vector = PCINT1_vect, - }, - .r_pcint = PCMSK1, - }, - AVR_IOPORT_DECLARE(d, 'D', D), - - AVR_EEPROM_DECLARE(EE_READY_vect), - AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect), - AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect), - .extint = { - AVR_EXTINT_MEGA_DECLARE(0, 'D', PD0, A), - AVR_EXTINT_MEGA_DECLARE(1, 'D', PD1, A), - AVR_EXTINT_MEGA_DECLARE(2, 'D', PD2, A), - AVR_EXTINT_MEGA_DECLARE(3, 'D', PD3, A), - AVR_EXTINT_MEGA_DECLARE(4, 'C', PC7, B), - AVR_EXTINT_MEGA_DECLARE(5, 'D', PD4, B), - AVR_EXTINT_MEGA_DECLARE(6, 'D', PD6, B), - AVR_EXTINT_MEGA_DECLARE(7, 'D', PD7, B), - }, - AVR_UARTX_DECLARE(1, PRR1, PRUSART1), - - .timer0 = { - .name = '0', - .disabled = AVR_IO_REGBIT(PRR0,PRTIM0), - .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) }, - WGM_OPS_8, - .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) }, - .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */, AVR_TIMER_EXTCLK_CHOOSE, AVR_TIMER_EXTCLK_CHOOSE }, - .ext_clock_pin = AVR_IO_REGBIT(PORTD, 7), /* External clock pin */ - - .r_tcnt = TCNT0, - - .overflow = { - .enable = AVR_IO_REGBIT(TIMSK0, TOIE0), - .raised = AVR_IO_REGBIT(TIFR0, TOV0), - .vector = TIMER0_OVF_vect, - }, - .comp = { - [AVR_TIMER_COMPA] = { - .r_ocr = OCR0A, - .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTD, 6), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A), - .raised = AVR_IO_REGBIT(TIFR0, OCF0A), - .vector = TIMER0_COMPA_vect, - }, - }, - [AVR_TIMER_COMPB] = { - .r_ocr = OCR0B, - .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTD, 5), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B), - .raised = AVR_IO_REGBIT(TIFR0, OCF0B), - .vector = TIMER0_COMPB_vect, - } - } - } - }, - .timer1 = { - .name = '1', - .disabled = AVR_IO_REGBIT(PRR0,PRTIM1), - .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11), - AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) }, - WGM_OPS_16, - .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) }, - .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */, AVR_TIMER_EXTCLK_CHOOSE, AVR_TIMER_EXTCLK_CHOOSE }, - .ext_clock_pin = AVR_IO_REGBIT(PORTB, 4), /* External clock pin */ - - .r_tcnt = TCNT1L, - .r_tcnth = TCNT1H, - .r_icr = ICR1L, - .r_icrh = ICR1H, - - .ices = AVR_IO_REGBIT(TCCR1B, ICES1), - .icp = AVR_IO_REGBIT(PORTB, 0), - - .overflow = { - .enable = AVR_IO_REGBIT(TIMSK1, TOIE1), - .raised = AVR_IO_REGBIT(TIFR1, TOV1), - .vector = TIMER1_OVF_vect, - }, - .icr = { - .enable = AVR_IO_REGBIT(TIMSK1, ICIE1), - .raised = AVR_IO_REGBIT(TIFR1, ICF1), - .vector = TIMER1_CAPT_vect, - }, - .comp = { - [AVR_TIMER_COMPA] = { - .r_ocr = OCR1AL, - .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it - .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTB, 1), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A), - .raised = AVR_IO_REGBIT(TIFR1, OCF1A), - .vector = TIMER1_COMPA_vect, - }, - }, - [AVR_TIMER_COMPB] = { - .r_ocr = OCR1BL, - .r_ocrh = OCR1BH, - .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3), - .com_pin = AVR_IO_REGBIT(PORTB, 2), - .interrupt = { - .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B), - .raised = AVR_IO_REGBIT(TIFR1, OCF1B), - .vector = TIMER1_COMPB_vect, - }, - }, - }, - }, - AVR_SPI_DECLARE(0, 0), - .usb = { - .name='1', - .disabled=AVR_IO_REGBIT(PRR1, PRUSB),// bit in the PRR - - .usbrf=AVR_IO_REGBIT(MCUSR,USBRF), // bit in the MCUSR - - .r_usbcon = USBCON, - .r_pllcsr=PLLCSR, - - .usb_com_vect=USB_COM_vect, - .usb_gen_vect=USB_GEN_vect, - }, - .acomp = { - .r_acsr = ACSR, - .acis = { AVR_IO_REGBIT(ACSR, ACIS0), AVR_IO_REGBIT(ACSR, ACIS1) }, - .acic = AVR_IO_REGBIT(ACSR, ACIC), - .aco = AVR_IO_REGBIT(ACSR, ACO), - .acbg = AVR_IO_REGBIT(ACSR, ACBG), - .disabled = AVR_IO_REGBIT(ACSR, ACD), - - .timer_name = '1', - - .ac = { - .enable = AVR_IO_REGBIT(ACSR, ACIE), - .raised = AVR_IO_REGBIT(ACSR, ACI), - .vector = ANALOG_COMP_vect, - } - } -}; +#include "sim_megaxu2.h" static avr_t * make() { - return avr_core_allocate(&mcu_usb162.core, sizeof(struct mcu_t)); + return avr_core_allocate(&SIM_CORENAME.core, sizeof(struct mcu_t)); } avr_kind_t usb162 = { .names = { "at90usb162" }, .make = make }; - -void usb162_init(struct avr_t * avr) -{ - struct mcu_t * mcu = (struct mcu_t*)avr; - - avr_ioport_init(avr, &mcu->portb); - avr_ioport_init(avr, &mcu->portc); - avr_ioport_init(avr, &mcu->portd); - avr_eeprom_init(avr, &mcu->eeprom); - avr_flash_init(avr, &mcu->selfprog); - avr_extint_init(avr, &mcu->extint); - avr_watchdog_init(avr, &mcu->watchdog); - avr_uart_init(avr, &mcu->uart1); - avr_timer_init(avr, &mcu->timer0); - avr_timer_init(avr, &mcu->timer1); - avr_spi_init(avr, &mcu->spi); - avr_usb_init(avr, &mcu->usb); - avr_acomp_init(avr, &mcu->acomp); -} - -void usb162_reset(struct avr_t * avr) -{ -// struct mcu_t * mcu = (struct mcu_t*)avr; -} diff --git a/simavr/cores/sim_usb82.c b/simavr/cores/sim_usb82.c new file mode 100644 index 000000000..820360f5d --- /dev/null +++ b/simavr/cores/sim_usb82.c @@ -0,0 +1,51 @@ +/* + sim_usb82.c + + AT90USB82, 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 . + */ + +#include "sim_avr.h" + +#define SIM_MMCU "at90usb82" +#define SIM_CORENAME mcu_usb82 + +#define _AVR_IO_H_ +#define __ASSEMBLER__ +#ifndef __AVR_AT90USB82__ +#define __AVR_AT90USB82__ +#endif +#include "avr/iousb82.h" + +// missing in avr/iousb82.h +#ifndef SIGNATURE_0 +#define SIGNATURE_0 0x1E +#define SIGNATURE_1 0x93 +#define SIGNATURE_2 0x82 +#endif + +#include "sim_megaxu2.h" + +static avr_t * make() +{ + return avr_core_allocate(&SIM_CORENAME.core, sizeof(struct mcu_t)); +} + +avr_kind_t usb82 = { + .names = { "at90usb82" }, + .make = make +}; diff --git a/simavr/sim/avr_usb.c b/simavr/sim/avr_usb.c index 88a83b818..f76e5134d 100644 --- a/simavr/sim/avr_usb.c +++ b/simavr/sim/avr_usb.c @@ -19,23 +19,34 @@ along with simavr. If not, see . */ -/* TODO correct reset values */ -/* TODO generate sofi every 1ms (when connected) */ +/* + * USB device controller of the AT90USB82/162, ATmega8U2/16U2/32U2 and + * ATmega16U4/32U4. The register layout is common, the cores describe the + * endpoint geometry and the optional VBUS pad (see avr_usb_t). + * + * Interrupts are level driven like on the real part: the USB_COM and + * USB_GEN conditions are re-evaluated whenever a flag or an enable bit + * changes, so enabling an interrupt whose flag is already set fires it. + */ + +/* TODO suspend/resume, SOFs never stop while attached */ /* TODO otg support? */ /* TODO drop bitfields? */ /* TODO thread safe ioctls */ -/* TODO dual-bank endpoint buffers */ -/* TODO actually pay attention to endpoint memory allocation ? buggy endpoint configuration doesn't matter in the simulator now. */ +/* TODO dual-bank endpoint buffers, banks are allocated but one is used */ +/* TODO data toggle sequencing, DTSEQ only follows the resets */ -#include #include #include #include #include "avr_usb.h" +#include "sim_time.h" enum usb_regs { usbcon = 0, + usbsta = 1, + usbint = 2, udcon = 8, udint = 9, udien = 10, @@ -55,11 +66,28 @@ enum usb_regs ueienx = 24, uedatx = 25, uebclx = 26, -// _res2=27, + uebchx = 27, // ATmega16U4/32U4 only, reserved (reads 0) elsewhere ueint = 28, otgtcon = 29, }; +// register bits +#define USBCON_USBE (1 << 7) +#define USBCON_FRZCLK (1 << 5) +#define UDCON_DETACH (1 << 0) +#define UDADDR_ADDEN (1 << 7) +#define UECONX_EPEN (1 << 0) +#define UECONX_RSTDT (1 << 3) +#define UECONX_STALLRQC (1 << 4) +#define UECONX_STALLRQ (1 << 5) + +// UEINTX flags that can raise an interrupt (all but RWAL and FIFOCON) +#define UEINTX_INT_MASK 0x5f +// UESTA0X OVERFI/UNDERFI, enabled by UEIENX.FLERRE +#define UESTA0X_ERR_MASK 0x60 +// UDINT flags, UDIEN has the same layout +#define UDINT_INT_MASK 0x7f + union _ueintx { struct { uint8_t txini :1; @@ -76,8 +104,6 @@ union _ueintx { struct _epstate { union _ueintx ueintx; - uint8_t dummy1; - uint8_t dummy2; union { struct { uint8_t epen :1; @@ -100,7 +126,7 @@ struct _epstate { struct { uint8_t res :1; uint8_t alloc :1; - uint8_t epbk1 :2; + uint8_t epbk :2; uint8_t epsize :3; uint8_t res2 :1; }; @@ -140,37 +166,169 @@ struct _epstate { } ueienx; struct { - uint8_t bytes[64]; - uint8_t tail; + uint8_t bytes[256]; + uint16_t tail; } bank[2]; uint8_t current_bank; - int setup_is_read; }; struct usb_internal_state { - struct _epstate ep_state[5]; + struct _epstate ep_state[AVR_USB_MAX_ENDPOINTS]; avr_int_vector_t com_vect; avr_int_vector_t gen_vect; + uint8_t vbus_level; // VBUS as driven by the host + uint8_t attached; // last value sent on USB_IRQ_ATTACH + uint8_t bad_epnum_warned; }; -const uint8_t num_endpoints = 5;//sizeof (struct usb_internal_state.ep_state) / sizeof (struct usb_internal_state.ep_state[0]); +/* + * Part description helpers, zero fields fall back to the AT90USB162 + * layout the module was written for. + */ +static uint8_t +ep_count( + avr_usb_t * p) +{ + return p->ep_count ? p->ep_count : 5; +} + +static uint16_t +ep_size_max( + avr_usb_t * p, + uint8_t ep) +{ + return p->ep_size_max[ep] ? p->ep_size_max[ep] : 64; +} + +static uint8_t +ep_banks_max( + avr_usb_t * p, + uint8_t ep) +{ + if (p->ep_banks_max[ep]) + return p->ep_banks_max[ep]; + return ep ? 2 : 1; +} static uint8_t current_ep_to_cpu( avr_usb_t * p) { - return p->io.avr->data[p->r_usbcon + uenum]; + return p->io.avr->data[p->r_usbcon + uenum] & (AVR_USB_MAX_ENDPOINTS - 1); } +/* + * EPNUM values past the last endpoint select a dummy state that is never + * enabled, so a bad UENUM write does not stop the simulation. + */ static struct _epstate * get_epstate( avr_usb_t * p, uint8_t ep) { - assert(ep < num_endpoints); + assert(ep < AVR_USB_MAX_ENDPOINTS); return &p->state->ep_state[ep]; } +static uint8_t +ep_int_pending( + struct _epstate * epstate) +{ + if (!epstate->ueconx.epen) + return 0; + if (epstate->ueintx.v & epstate->ueienx.v & UEINTX_INT_MASK) + return 1; + return epstate->ueienx.flerre && (epstate->uesta0x.v & UESTA0X_ERR_MASK); +} + +// UEINT: one EPINTx bit per endpoint with an enabled, raised flag +static uint8_t +ueint_value( + avr_usb_t * p) +{ + uint8_t v = 0; + + for (int i = 0; i < ep_count(p); i++) + if (ep_int_pending(get_epstate(p, i))) + v |= 1 << i; + return v; +} + +/* + * USB_COM and USB_GEN have no flag of their own: they trigger for as long + * as an enabled flag is set (datasheet "Reset and Interrupt Handling", + * second type of interrupt), and the flags are only cleared by software. + * The vectors are sticky, raised while the condition holds and cleared + * when it goes away. Called whenever a flag or an enable bit changes. + */ +static void +update_com_irq( + avr_usb_t * p) +{ + if (ueint_value(p)) + avr_raise_interrupt(p->io.avr, &p->state->com_vect); + else + avr_clear_sticky_interrupt(p->io.avr, &p->state->com_vect); +} + +static void +update_gen_irq( + avr_usb_t * p) +{ + avr_t * avr = p->io.avr; + uint8_t pending = avr->data[p->r_usbcon + udint] & + avr->data[p->r_usbcon + udien] & UDINT_INT_MASK; + + if (p->vbusti.reg && + avr_regbit_get(avr, p->vbusti) && avr_regbit_get(avr, p->vbuste)) + pending = 1; + if (pending) + avr_raise_interrupt(avr, &p->state->gen_vect); + else + avr_clear_sticky_interrupt(avr, &p->state->gen_vect); +} + +/* + * USB_IRQ_ATTACH follows the D+ pull-up: USBE set, DETACH clear and, on + * parts with a VBUS pad, VBUS detected. + */ +static void +update_attach( + avr_usb_t * p) +{ + avr_t * avr = p->io.avr; + uint8_t attached = (avr->data[p->r_usbcon + usbcon] & USBCON_USBE) && + !(avr->data[p->r_usbcon + udcon] & UDCON_DETACH); + + if (p->vbus.reg && !avr_regbit_get(avr, p->vbus)) + attached = 0; + if (attached != p->state->attached) { + p->state->attached = attached; + AVR_LOG(avr, LOG_TRACE, "USB: %s\n", attached ? "attach" : "detach"); + avr_raise_irq(p->io.irq + USB_IRQ_ATTACH, attached); + } +} + +/* + * USBSTA.VBUS tracks the host VBUS level while the pad is enabled + * (OTGPADE), every change of it sets VBUSTI. + */ +static void +update_vbus( + avr_usb_t * p) +{ + avr_t * avr = p->io.avr; + + if (!p->vbus.reg) + return; + + uint8_t level = avr_regbit_get(avr, p->otgpade) && p->state->vbus_level; + if (level != avr_regbit_get(avr, p->vbus)) { + avr_regbit_setto(avr, p->vbus, level); + avr_regbit_set(avr, p->vbusti); + AVR_LOG(avr, LOG_TRACE, "USB: VBUS %d\n", level); + } +} enum epints { txini = 0, @@ -191,47 +349,38 @@ raise_ep_interrupt( enum epints irq) { struct _epstate * epstate = get_epstate(p, ep); - assert(ep < num_endpoints); - avr->data[p->r_usbcon + ueint] |= 1 << ep; + switch (irq) { case txini: case stalledi: case rxouti: case nakouti: case nakini: - epstate->ueintx.v |= 1 << irq; - if (epstate->ueienx.v & (1 << irq)) - avr_raise_interrupt(avr, &p->state->com_vect); - break; case rxstpi: epstate->ueintx.v |= 1 << irq; - if (epstate->ueienx.v & (1 << irq)) - avr_raise_interrupt(avr, &p->state->com_vect); break; case overfi: epstate->uesta0x.overfi = 1; - if (epstate->ueienx.flerre) - avr_raise_interrupt(avr, &p->state->com_vect); break; case underfi: epstate->uesta0x.underfi = 1; - if (epstate->ueienx.flerre) - avr_raise_interrupt(avr, &p->state->com_vect); break; default: assert(0); } + update_com_irq(p); } enum usbints { - suspi = 0, sofi = 2, eorsti = 3, wakeupi = 4, eorsmi = 5, uprsmi = 6 + suspi = 0, msofi = 1, sofi = 2, eorsti = 3, wakeupi = 4, eorsmi = 5, + uprsmi = 6 }; + static void raise_usb_interrupt( avr_usb_t * p, enum usbints irq) { - uint8_t * Rudien = &p->io.avr->data[p->r_usbcon + udien]; uint8_t * Rudint = &p->io.avr->data[p->r_usbcon + udint]; switch (irq) { @@ -240,24 +389,21 @@ raise_usb_interrupt( case wakeupi: case eorsti: case sofi: + case msofi: case suspi: *Rudint |= 1 << irq; - if (*Rudien & (1 << irq)) - avr_raise_interrupt(p->io.avr, &p->state->gen_vect); break; default: assert(0); } - + update_gen_irq(p); } -static void -reset_endpoints( - struct avr_t * avr, - avr_usb_t * p) +static uint16_t +ep_fifo_size( + struct _epstate * epstate) { - memset(&p->state->ep_state[1], 0, - sizeof p->state->ep_state - sizeof p->state->ep_state[0]); + return 8 << epstate->uecfg1x.epsize; } static int @@ -271,34 +417,93 @@ static int ep_fifo_full( struct _epstate * epstate) { - return epstate->bank[epstate->current_bank].tail >= - (8 << epstate->uecfg1x.epsize); + return epstate->bank[epstate->current_bank].tail >= ep_fifo_size(epstate); } -static uint8_t -ep_fifo_size( +static uint16_t +ep_fifo_count( struct _epstate * epstate) { - assert(epstate->ueconx.epen); - return (8 << epstate->uecfg1x.epsize); + return epstate->bank[epstate->current_bank].tail; } -static uint8_t -ep_fifo_count( +/* + * CFGOK check: size and bank count within what the endpoint supports and, + * when the part declares its DPRAM size, the endpoints allocated so far + * (EP0 up to this one, the allocation order) fitting in it. + */ +static int +ep_config_valid( + avr_usb_t * p, + uint8_t ep) +{ + struct _epstate * epstate = get_epstate(p, ep); + uint32_t used = 0; + + if (ep >= ep_count(p)) + return 0; + if (epstate->uecfg1x.epsize > 5 || epstate->uecfg1x.epbk > 1) + return 0; + if (ep_fifo_size(epstate) > ep_size_max(p, ep) || + epstate->uecfg1x.epbk + 1 > ep_banks_max(p, ep)) + return 0; + if (!p->dpram_size) + return 1; + for (int i = 0; i <= ep; i++) { + struct _epstate * e = get_epstate(p, i); + if (e->uecfg1x.alloc && e->uecfg1x.epsize <= 5 && e->uecfg1x.epbk <= 1) + used += ep_fifo_size(e) * (e->uecfg1x.epbk + 1); + } + return used <= p->dpram_size; +} + +// UEINTX state of an empty, configured endpoint +static void +ep_init_flags( struct _epstate * epstate) { - return epstate->bank[epstate->current_bank].tail; + if (epstate->uecfg0x.eptype == 0) + epstate->ueintx.txini = 1; + else if (epstate->uecfg0x.epdir) { + epstate->ueintx.txini = 1; + epstate->ueintx.rwal = 1; + epstate->ueintx.fifocon = 1; + } else + epstate->ueintx.rxouti = 0; +} + +/* + * Endpoint reset (UERST, EPEN cleared, USB reset of EP0): banks emptied, + * UEINTX/UESTA0X/UESTA1X back to their reset values. The configuration + * (and so CFGOK) and the data toggle are kept. + */ +static void +ep_reset( + struct _epstate * epstate) +{ + uint8_t keep = epstate->uesta0x.v & 0x8c; // CFGOK, DTSEQ + + epstate->bank[0].tail = 0; + epstate->bank[1].tail = 0; + epstate->current_bank = 0; + epstate->ueintx.v = 0; + epstate->uesta0x.v = keep; + epstate->uesta1x.v = 0; + if (epstate->ueconx.epen && epstate->uecfg1x.alloc) + ep_init_flags(epstate); } static int ep_fifo_cpu_readbyte( + avr_usb_t * p, struct _epstate * epstate) { - uint8_t i, j; + uint16_t i, j; uint8_t v = epstate->bank[epstate->current_bank].bytes[0]; if (!epstate->ueconx.epen) { - printf("WARNING! Adding bytes to non configured endpoint\n"); + AVR_LOG(p->io.avr, LOG_WARNING, + "USB: Reading bytes from non configured endpoint\n"); return -1; } @@ -314,11 +519,13 @@ ep_fifo_cpu_readbyte( static int ep_fifo_cpu_writebyte( + avr_usb_t * p, struct _epstate * epstate, uint8_t v) { if (!epstate->ueconx.epen) { - printf("WARNING! Adding bytes to non configured endpoint\n"); + AVR_LOG(p->io.avr, LOG_WARNING, + "USB: Adding bytes to non configured endpoint\n"); return -1; } if (ep_fifo_full(epstate)) @@ -330,13 +537,11 @@ ep_fifo_cpu_writebyte( static int ep_fifo_usb_read( + avr_usb_t * p, struct _epstate * epstate, - uint8_t * buf) + uint8_t * buf, + uint32_t max) { - if (!epstate->ueconx.epen) { - printf("WARNING! Reading from non configured endpoint\n"); - return -1; - } if (epstate->ueintx.txini) { return AVR_IOCTL_USB_NAK; } @@ -345,22 +550,25 @@ ep_fifo_usb_read( } int ret = epstate->bank[epstate->current_bank].tail; - memcpy(buf, epstate->bank[epstate->current_bank].bytes, - epstate->bank[epstate->current_bank].tail); + if ((uint32_t)ret > max) { + AVR_LOG(p->io.avr, LOG_WARNING, + "USB: IN packet of %d bytes, host buffer is %u, truncated\n", + ret, max); + ret = max; + } + memcpy(buf, epstate->bank[epstate->current_bank].bytes, ret); epstate->bank[epstate->current_bank].tail = 0; return ret; } static int ep_fifo_usb_write( - struct _epstate * epstate, - uint8_t * buf, - uint8_t len) + avr_usb_t * p, + uint8_t ep, + uint8_t * buf, + uint32_t len) { - if (!epstate->ueconx.epen) { - printf("WARNING! Adding bytes to non configured endpoint\n"); - return -1; - } + struct _epstate * epstate = get_epstate(p, ep); if (epstate->ueintx.rxouti) { return AVR_IOCTL_USB_NAK; @@ -370,8 +578,11 @@ ep_fifo_usb_write( } if (len > ep_fifo_size(epstate)) { - printf("EP OVERFI\n"); - len = sizeof epstate->bank[epstate->current_bank].bytes; + AVR_LOG(p->io.avr, LOG_WARNING, + "USB: EP%d OUT packet of %u bytes, endpoint size is %d\n", + ep, len, ep_fifo_size(epstate)); + len = ep_fifo_size(epstate); + raise_ep_interrupt(p->io.avr, p, ep, overfi); } memcpy(epstate->bank[epstate->current_bank].bytes, buf, len); epstate->bank[epstate->current_bank].tail = len; @@ -386,9 +597,27 @@ avr_usb_ep_read_bytecount( void * param) { avr_usb_t * p = (avr_usb_t *) param; - return ep_fifo_count(get_epstate(p, current_ep_to_cpu(p))); + uint16_t count = ep_fifo_count(get_epstate(p, current_ep_to_cpu(p))); + + return addr == p->r_usbcon + uebclx ? count & 0xff : count >> 8; +} + +static uint8_t +avr_usb_ueint_read( + struct avr_t * avr, + avr_io_addr_t addr, + void * param) +{ + return ueint_value((avr_usb_t *) param); } +static void +avr_usb_usbcon_write( + struct avr_t * avr, + avr_io_addr_t addr, + uint8_t v, + void * param); + static void avr_usb_udaddr_write( struct avr_t * avr, @@ -396,7 +625,7 @@ avr_usb_udaddr_write( uint8_t v, void * param) { - if (v & 0x80) + if (v & UDADDR_ADDEN) AVR_LOG(avr, LOG_TRACE, "USB: Activate address %d\n", v & 0x7f); avr_core_watch_write(avr, addr, v); } @@ -406,9 +635,43 @@ avr_usb_udcon_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * pa { avr_usb_t * p = (avr_usb_t *)param; - if(avr->data[addr]&1 && !(v&1)) - avr_raise_irq(p->io.irq + USB_IRQ_ATTACH, !(v&1)); avr_core_watch_write(avr, addr, v); + update_attach(p); +} + +// UDINT flags are cleared by writing 0, writing 1 has no effect +static void +avr_usb_udint_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param) +{ + avr_usb_t * p = (avr_usb_t *)param; + + avr_core_watch_write(avr, addr, avr->data[addr] & v); + update_gen_irq(p); +} + +static void +avr_usb_udien_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param) +{ + avr_usb_t * p = (avr_usb_t *)param; + + avr_core_watch_write(avr, addr, v); + update_gen_irq(p); +} + +// USBSTA is read only +static void +avr_usb_usbsta_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param) +{ +} + +// USBINT.VBUSTI is cleared by writing 0 +static void +avr_usb_usbint_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param) +{ + avr_usb_t * p = (avr_usb_t *)param; + + avr_core_watch_write(avr, addr, avr->data[addr] & v); + update_gen_irq(p); } static void @@ -418,7 +681,31 @@ avr_usb_uenum_write( uint8_t v, void * param) { - assert(v < num_endpoints); + avr_usb_t * p = (avr_usb_t *) param; + + v &= AVR_USB_MAX_ENDPOINTS - 1; + if (v >= ep_count(p) && !p->state->bad_epnum_warned) { + AVR_LOG(avr, LOG_WARNING, + "USB: UENUM=%d, this part has endpoints 0..%d\n", + v, ep_count(p) - 1); + p->state->bad_epnum_warned = 1; + } + avr_core_watch_write(avr, addr, v); +} + +static void +avr_usb_uerst_write( + struct avr_t * avr, + avr_io_addr_t addr, + uint8_t v, + void * param) +{ + avr_usb_t * p = (avr_usb_t *) param; + + v &= (1 << ep_count(p)) - 1; + for (int i = 0; i < ep_count(p); i++) + if (v & (1 << i)) + ep_reset(get_epstate(p, i)); avr_core_watch_write(avr, addr, v); } @@ -429,14 +716,14 @@ avr_usb_ep_read_ueintx( void * param) { avr_usb_t * p = (avr_usb_t *) param; - uint8_t ep = current_ep_to_cpu(p); + struct _epstate * epstate = get_epstate(p, current_ep_to_cpu(p)); - if (p->state->ep_state[ep].uecfg0x.epdir) - p->state->ep_state[ep].ueintx.rwal = !ep_fifo_full(get_epstate(p, ep)); + if (epstate->uecfg0x.epdir) + epstate->ueintx.rwal = !ep_fifo_full(epstate); else - p->state->ep_state[ep].ueintx.rwal = !ep_fifo_empty(get_epstate(p, ep)); + epstate->ueintx.rwal = !ep_fifo_empty(epstate); - return p->state->ep_state[ep].ueintx.v; + return epstate->ueintx.v; } static void @@ -450,7 +737,7 @@ avr_usb_ep_write_ueintx( uint8_t ep = current_ep_to_cpu(p); union _ueintx * newstate = (union _ueintx*) &v; - union _ueintx * curstate = &p->state->ep_state[ep].ueintx; + union _ueintx * curstate = &get_epstate(p, ep)->ueintx; if (curstate->rxouti & !newstate->rxouti) curstate->rxouti = 0; @@ -472,8 +759,7 @@ avr_usb_ep_write_ueintx( if (curstate->rwal & !newstate->rwal) AVR_LOG(avr, LOG_WARNING, "USB: Pointless change of ueintx.rwal\n"); - if ((curstate->v & 0xdf) == 0) - avr->data[p->r_usbcon + ueint] &= 0xff ^ (1 << ep); // mark ep0 interrupt + update_com_irq(p); } static uint8_t @@ -507,45 +793,59 @@ avr_usb_ep_write( void * param) { avr_usb_t * p = (avr_usb_t *) param; - struct _epstate * epstate = get_epstate(p, current_ep_to_cpu(p)); + uint8_t ep = current_ep_to_cpu(p); + struct _epstate * epstate = get_epstate(p, ep); uint8_t laddr = addr - p->r_usbcon; + if (ep >= ep_count(p)) + return; + switch (laddr) { - case ueconx: - if (v & 1 << 4) - epstate->ueconx.stallrq = 0; - if (v & 1 << 5) + case ueconx: { + uint8_t was_enabled = epstate->ueconx.epen; + // STALLRQ and STALLRQC are strobes, a read-modify-write + // "UECONX |= STALLRQC" carries STALLRQ too, so clear wins. + if (v & UECONX_STALLRQ) epstate->ueconx.stallrq = 1; - epstate->ueconx.epen = (v & 1) != 0; - break; + if (v & UECONX_STALLRQC) + epstate->ueconx.stallrq = 0; + if (v & UECONX_RSTDT) + epstate->uesta0x.dtseq = 0; + epstate->ueconx.epen = (v & UECONX_EPEN) != 0; + if (was_enabled && !epstate->ueconx.epen) { + ep_reset(epstate); + epstate->uesta0x.dtseq = 0; + } + } break; case uecfg0x: epstate->uecfg0x.v = v; epstate->uesta0x.cfgok = 0; break; case uecfg1x: epstate->uecfg1x.v = v; - epstate->uesta0x.cfgok = epstate->uecfg1x.alloc; - if (epstate->uecfg0x.eptype == 0) - epstate->ueintx.txini = 1; - else if (epstate->uecfg0x.epdir) { - epstate->ueintx.txini = 1; - epstate->ueintx.rwal = 1; - epstate->ueintx.fifocon = 1; - } else - epstate->ueintx.rxouti = 0; - avr_core_watch_write(avr, p->r_usbcon + uesta0x, - epstate->uesta0x.v); + epstate->uesta0x.cfgok = epstate->uecfg1x.alloc && + ep_config_valid(p, ep); + if (epstate->uecfg1x.alloc && !epstate->uesta0x.cfgok) + AVR_LOG(avr, LOG_WARNING, + "USB: EP%d configuration rejected (UECFG1X=%02x)\n", + ep, v); + ep_init_flags(epstate); break; case uesta0x: + // only OVERFI and UNDERFI can be cleared v = (epstate->uesta0x.v & 0x9f) + (v & (0x60 & epstate->uesta0x.v)); epstate->uesta0x.v = v; break; + case uesta1x: + // read only + break; case ueienx: epstate->ueienx.v = v; break; default: assert(0); } + update_com_irq(p); } static uint8_t @@ -555,11 +855,12 @@ avr_usb_ep_read_data( void * param) { avr_usb_t * p = (avr_usb_t *) param; - int ret = ep_fifo_cpu_readbyte(get_epstate(p, current_ep_to_cpu(p))); + uint8_t ep = current_ep_to_cpu(p); + int ret = ep_fifo_cpu_readbyte(p, get_epstate(p, ep)); if (ret < 0) { if (ret == -2) - raise_ep_interrupt(avr, p, current_ep_to_cpu(p), underfi); + raise_ep_interrupt(avr, p, ep, underfi); return 0; } else return (uint8_t) ret; @@ -573,14 +874,16 @@ avr_usb_ep_write_data( void * param) { avr_usb_t * p = (avr_usb_t *) param; - int ret = ep_fifo_cpu_writebyte(get_epstate(p, current_ep_to_cpu(p)), v); + uint8_t ep = current_ep_to_cpu(p); + int ret = ep_fifo_cpu_writebyte(p, get_epstate(p, ep), v); if (ret == 0) return; if (ret == -2) - raise_ep_interrupt(avr, p, current_ep_to_cpu(p), overfi); + raise_ep_interrupt(avr, p, ep, overfi); } +// PLOCK (bit 0) follows PLLE (bit 1), the PLL locks instantly static void avr_usb_pll_write( struct avr_t * avr, @@ -588,25 +891,69 @@ avr_usb_pll_write( uint8_t v, void * param) { - v |= (v >> 1) & 1; + v = (v & ~1) | ((v >> 1) & 1); avr_core_watch_write(avr, addr, v); } -avr_cycle_count_t +/* + * Full speed Start Of Frame, every 1ms from the first bus reset until the + * device detaches. Advances the 11 bits frame number in UDFNUMH:UDFNUML. + */ +static avr_cycle_count_t sof_generator( struct avr_t * avr, avr_cycle_count_t when, void * param) { avr_usb_t * p = (avr_usb_t *) param; - //stop sof generation if detached - if (avr->data[p->r_usbcon + udcon] & 1) + uint16_t fnum; + + if (!p->state->attached) return 0; - else { - raise_usb_interrupt(p, sofi); - return when; + fnum = (avr->data[p->r_usbcon + udfnuml] | + (avr->data[p->r_usbcon + udfnumh] << 8)) + 1; + avr->data[p->r_usbcon + udfnuml] = fnum; + avr->data[p->r_usbcon + udfnumh] = (fnum >> 8) & 0x07; + raise_usb_interrupt(p, sofi); + return when + avr_usec_to_cycles(avr, 1000); +} + +/* + * Device side reset, on CPU reset and when USBE is cleared: all the + * device and endpoint registers back to their reset values, DETACH set. + */ +static void +reset_device( + avr_usb_t * p) +{ + avr_t * avr = p->io.avr; + + memset(p->state->ep_state, 0, sizeof p->state->ep_state); + for (int i = udcon; i < otgtcon; i++) + avr->data[p->r_usbcon + i] = 0; + avr->data[p->r_usbcon + udcon] = UDCON_DETACH; +} + +static void +avr_usb_usbcon_write( + struct avr_t * avr, + avr_io_addr_t addr, + uint8_t v, + void * param) +{ + avr_usb_t * p = (avr_usb_t *) param; + + if ((avr->data[addr] & USBCON_USBE) && !(v & USBCON_USBE)) { + AVR_LOG(avr, LOG_TRACE, "USB: controller reset (USBE cleared)\n"); + reset_device(p); + v |= USBCON_FRZCLK; } + avr_core_watch_write(avr, addr, v); + update_vbus(p); + update_attach(p); + update_gen_irq(p); + update_com_irq(p); } static int @@ -623,24 +970,42 @@ avr_usb_ioctl( switch (ctl) { case AVR_IOCTL_USB_READ: + case AVR_IOCTL_USB_WRITE: + case AVR_IOCTL_USB_SETUP: ep = d->pipe & 0x7f; - if (ep >= ARRAY_SIZE(p->state->ep_state)) { - AVR_LOG(io->avr, LOG_ERROR, - "USB: endpoint out of range (%d/ >= %d\n", - ep, ARRAY_SIZE(p->state->ep_state)); - return -1; - } + if (ep >= ep_count(p)) { + AVR_LOG(io->avr, LOG_WARNING, + "USB: host access to EP%d, part has %d endpoints\n", + ep, ep_count(p)); + return -1; + } + // no pull-up, or endpoint not usable: no handshake + if (!p->state->attached) + return AVR_IOCTL_USB_NAK; epstate = get_epstate(p, ep); + if (!epstate->ueconx.epen || !epstate->uesta0x.cfgok) + return AVR_IOCTL_USB_NAK; + break; + case AVR_IOCTL_USB_RESET: + if (!p->state->attached) { + AVR_LOG(io->avr, LOG_WARNING, "USB: bus reset while detached\n"); + return -1; + } + break; + } + switch (ctl) { + case AVR_IOCTL_USB_READ: if (epstate->ueconx.stallrq) { - raise_ep_interrupt(io->avr, p, 0, stalledi); + raise_ep_interrupt(io->avr, p, ep, stalledi); return AVR_IOCTL_USB_STALL; } if (ep && !epstate->uecfg0x.epdir) AVR_LOG(io->avr, LOG_WARNING, "USB: Reading from IN endpoint from host??\n"); - ret = ep_fifo_usb_read(epstate, d->buf); + ret = ep_fifo_usb_read(p, epstate, d->buf, d->sz); if (ret < 0) { + raise_ep_interrupt(io->avr, p, ep, nakini); // is this correct? It makes the cdc example work. // Linux stops polling the data ep if we send naks,but // according to usb spec nak'ing should be ok. @@ -655,35 +1020,32 @@ avr_usb_ioctl( raise_ep_interrupt(io->avr, p, ep, txini); return 0; case AVR_IOCTL_USB_WRITE: - ep = d->pipe & 0x7f; - epstate = get_epstate(p, ep); - if (ep && epstate->uecfg0x.epdir) AVR_LOG(io->avr, LOG_WARNING, "USB: Writing to IN endpoint from host??\n"); if (epstate->ueconx.stallrq) { - raise_ep_interrupt(io->avr, p, 0, stalledi); + raise_ep_interrupt(io->avr, p, ep, stalledi); return AVR_IOCTL_USB_STALL; } - ret = ep_fifo_usb_write(epstate, d->buf, d->sz); - if (ret < 0) + ret = ep_fifo_usb_write(p, ep, d->buf, d->sz); + if (ret < 0) { + if (ret == AVR_IOCTL_USB_NAK) + raise_ep_interrupt(io->avr, p, ep, nakouti); return ret; + } epstate->ueintx.fifocon = 1; raise_ep_interrupt(io->avr, p, ep, rxouti); return 0; case AVR_IOCTL_USB_SETUP: - ep = d->pipe & 0x7f; - epstate = get_epstate(p, ep); - epstate->ueconx.stallrq = 0; // teensy actually depends on this (fails to ack rxouti on usb // control read status stage) even if the datasheet clearly states // that one should do so. epstate->ueintx.rxouti = 0; - ret = ep_fifo_usb_write(epstate, d->buf, d->sz); + ret = ep_fifo_usb_write(p, ep, d->buf, d->sz); if (ret < 0) return ret; raise_ep_interrupt(io->avr, p, ep, rxstpi); @@ -691,10 +1053,20 @@ avr_usb_ioctl( return 0; case AVR_IOCTL_USB_RESET: AVR_LOG(io->avr, LOG_TRACE, "USB: __USB_RESET__\n"); - reset_endpoints(io->avr, p); + // all endpoints disabled, EP0 stays configured, address 0 + memset(&p->state->ep_state[1], 0, + sizeof p->state->ep_state - sizeof p->state->ep_state[0]); + ep_reset(get_epstate(p, 0)); + io->avr->data[p->r_usbcon + udaddr] = 0; raise_usb_interrupt(p, eorsti); - if (0) - avr_cycle_timer_register_usec(io->avr, 1000, sof_generator, p); + update_com_irq(p); + avr_cycle_timer_register_usec(io->avr, 1000, sof_generator, p); + return 0; + case AVR_IOCTL_USB_VBUS: + p->state->vbus_level = io_param != NULL; + update_vbus(p); + update_attach(p); + update_gen_irq(p); return 0; default: return -1; @@ -706,15 +1078,17 @@ avr_usb_reset( struct avr_io_t *io) { avr_usb_t * p = (avr_usb_t *) io; - uint8_t i; - - memset(p->state->ep_state, 0, sizeof p->state->ep_state); - - for (i = 0; i < otgtcon; i++) - p->io.avr->data[p->r_usbcon + i] = 0; - - p->io.avr->data[p->r_usbcon] = 0x20; - p->io.avr->data[p->r_usbcon + udcon] = 1; + avr_t * avr = io->avr; + + reset_device(p); + avr->data[p->r_usbcon + usbcon] = USBCON_FRZCLK; + avr->data[p->r_usbcon + usbsta] = 0; + avr->data[p->r_usbcon + usbint] = 0; + for (int i = usbint + 1; i < udcon; i++) + avr->data[p->r_usbcon + i] = 0; + if (p->id.reg) + avr_regbit_set(avr, p->id); + update_attach(p); AVR_LOG(io->avr, LOG_TRACE, "USB: %s\n", __FUNCTION__); } @@ -767,6 +1141,8 @@ register_vectors( p->state->com_vect.vector = p->usb_com_vect; p->state->gen_vect.vector = p->usb_gen_vect; + p->state->com_vect.raise_sticky = 1; + p->state->gen_vect.raise_sticky = 1; avr_register_vector(avr, &p->state->com_vect); avr_register_vector(avr, &p->state->gen_vect); @@ -776,6 +1152,7 @@ void avr_usb_init(avr_t * avr, avr_usb_t * p) { p->io = _io; + assert(ep_count(p) <= AVR_USB_MAX_ENDPOINTS - 1); p->state = calloc(1, sizeof *p->state); avr_register_io(avr, &p->io); @@ -783,13 +1160,24 @@ void avr_usb_init(avr_t * avr, avr_usb_t * p) // allocate this module's IRQ avr_io_setirqs(&p->io, AVR_IOCTL_USB_GETIRQ(), USB_IRQ_COUNT, NULL); + avr_register_io_write(avr, p->r_usbcon + usbcon, avr_usb_usbcon_write, p); + if (p->vbus.reg) { + avr_register_io_write(avr, p->r_usbcon + usbsta, avr_usb_usbsta_write, p); + avr_register_io_write(avr, p->r_usbcon + usbint, avr_usb_usbint_write, p); + } + avr_register_io_write(avr, p->r_usbcon + udaddr, avr_usb_udaddr_write, p); avr_register_io_write(avr, p->r_usbcon + udcon, avr_usb_udcon_write, p); + avr_register_io_write(avr, p->r_usbcon + udint, avr_usb_udint_write, p); + avr_register_io_write(avr, p->r_usbcon + udien, avr_usb_udien_write, p); avr_register_io_write(avr, p->r_usbcon + uenum, avr_usb_uenum_write, p); + avr_register_io_write(avr, p->r_usbcon + uerst, avr_usb_uerst_write, p); avr_register_io_read(avr, p->r_usbcon + uedatx, avr_usb_ep_read_data, p); avr_register_io_write(avr, p->r_usbcon + uedatx, avr_usb_ep_write_data, p); avr_register_io_read(avr, p->r_usbcon + uebclx, avr_usb_ep_read_bytecount, p); //ro + avr_register_io_read(avr, p->r_usbcon + uebchx, avr_usb_ep_read_bytecount, p); //ro + avr_register_io_read(avr, p->r_usbcon + ueint, avr_usb_ueint_read, p); //ro avr_register_io_read(avr, p->r_usbcon + ueintx, avr_usb_ep_read_ueintx, p); avr_register_io_write(avr, p->r_usbcon + ueintx, avr_usb_ep_write_ueintx, p); @@ -803,4 +1191,3 @@ void avr_usb_init(avr_t * avr, avr_usb_t * p) avr_register_io_write(avr, p->r_pllcsr, avr_usb_pll_write, p); } - diff --git a/simavr/sim/avr_usb.h b/simavr/sim/avr_usb.h index 17e5c0dfc..c0880ece9 100644 --- a/simavr/sim/avr_usb.h +++ b/simavr/sim/avr_usb.h @@ -33,7 +33,28 @@ enum { USB_IRQ_COUNT }; -// add port number to get the real IRQ +/* + * Host side interface. The code calling these plays the USB host, + * see doc/Intro.html and tests/test_usb_host.h. + * + * WRITE, READ and SETUP take a struct avr_io_usb and return + * AVR_IOCTL_USB_OK, AVR_IOCTL_USB_NAK (firmware not ready, run it and + * retry), AVR_IOCTL_USB_STALL, or -1 for an endpoint the part does not + * have. They NAK while the device is detached or the endpoint is not + * configured. + * SETUP: 8 byte SETUP packet to a control endpoint. + * WRITE: OUT data, sz bytes. More than the endpoint size is + * truncated and flagged as an overflow to the firmware. + * READ: IN data, sz is the buffer size in, the byte count out. + * A bulk endpoint with no data returns OK with sz 0, not NAK. + * A zero length READ or WRITE on EP0 is a control status stage. + * RESET: bus reset, NULL parameter. -1 while detached. + * VBUS: VBUS level as the parameter, (void*)1 when the host powers the + * port, (void*)0 when it does not. It only changes registers on + * parts with a VBUS pad (see 'vbus' below). + * GETIRQ: base of the USB_IRQ_* IRQs. USB_IRQ_ATTACH is raised with 1 + * when the firmware connects to the bus, 0 when it disconnects. + */ #define AVR_IOCTL_USB_WRITE AVR_IOCTL_DEF('u','s','b','w') #define AVR_IOCTL_USB_READ AVR_IOCTL_DEF('u','s','b','r') #define AVR_IOCTL_USB_SETUP AVR_IOCTL_DEF('u','s','b','s') @@ -42,7 +63,7 @@ enum { #define AVR_IOCTL_USB_GETIRQ() AVR_IOCTL_DEF('u','s','b',' ') struct avr_io_usb { - uint8_t pipe; //[in] + uint8_t pipe; //[in] endpoint number, the direction bit is ignored uint32_t sz; //[in/out] uint8_t * buf; //[in/out] }; @@ -50,6 +71,9 @@ struct avr_io_usb { #define AVR_IOCTL_USB_STALL -3 #define AVR_IOCTL_USB_OK 0 +// EPNUM is 3 bits wide, parts implement up to 7 of these +#define AVR_USB_MAX_ENDPOINTS 8 + typedef struct avr_usb_t { avr_io_t io; char name; @@ -62,6 +86,25 @@ typedef struct avr_usb_t { uint8_t usb_com_vect; uint8_t usb_gen_vect; + /* + * Endpoint geometry. Zero values select the AT90USB82/162 layout: + * 5 endpoints of up to 64 bytes and no DPRAM allocation check. + */ + uint8_t ep_count; // implemented endpoints, EP0..ep_count-1 + uint16_t dpram_size; // endpoint DPRAM bytes, 0 = not checked + uint16_t ep_size_max[AVR_USB_MAX_ENDPOINTS]; // bytes per bank + uint8_t ep_banks_max[AVR_USB_MAX_ENDPOINTS]; // 1 or 2 + + /* + * VBUS pad (ATmega16U4/32U4). Leave reg 0 on parts without one, + * they attach as soon as DETACH is cleared. + */ + avr_regbit_t vbus; // USBSTA.VBUS + avr_regbit_t id; // USBSTA.ID, reads as 1 + avr_regbit_t vbusti; // USBINT.VBUSTI + avr_regbit_t vbuste; // USBCON.VBUSTE + avr_regbit_t otgpade; // USBCON.OTGPADE + struct usb_internal_state * state; } avr_usb_t; diff --git a/tests/Makefile b/tests/Makefile index 4bc6c9069..3e27043fa 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -37,6 +37,8 @@ axf: ${sources:.c=.axf} hex: atmega88_example.hex +at90usb162_usb.axf atmega32u2_usb.axf atmega32u4_usb.axf: usb_test_fw.h + ${OBJ}/%.tst: tests.c %.c ifneq ($(E),) @echo TST $@ diff --git a/tests/at90usb162_usb.c b/tests/at90usb162_usb.c new file mode 100644 index 000000000..1a0e28ad2 --- /dev/null +++ b/tests/at90usb162_usb.c @@ -0,0 +1,25 @@ +/* + at90usb162_usb.c + + USB device test firmware, see usb_test_fw.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 . + */ + +#include "avr_mcu_section.h" +AVR_MCU(F_CPU, "at90usb162"); + +#include "usb_test_fw.h" diff --git a/tests/atmega32u2_usb.c b/tests/atmega32u2_usb.c new file mode 100644 index 000000000..edd8a51d7 --- /dev/null +++ b/tests/atmega32u2_usb.c @@ -0,0 +1,25 @@ +/* + atmega32u2_usb.c + + USB device test firmware, see usb_test_fw.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 . + */ + +#include "avr_mcu_section.h" +AVR_MCU(F_CPU, "atmega32u2"); + +#include "usb_test_fw.h" diff --git a/tests/atmega32u4_usb.c b/tests/atmega32u4_usb.c new file mode 100644 index 000000000..42d182947 --- /dev/null +++ b/tests/atmega32u4_usb.c @@ -0,0 +1,25 @@ +/* + atmega32u4_usb.c + + USB device test firmware, see usb_test_fw.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 . + */ + +#include "avr_mcu_section.h" +AVR_MCU(F_CPU, "atmega32u4"); + +#include "usb_test_fw.h" diff --git a/tests/test_atmegaxu2_usb.c b/tests/test_atmegaxu2_usb.c new file mode 100644 index 000000000..7f02d653d --- /dev/null +++ b/tests/test_atmegaxu2_usb.c @@ -0,0 +1,28 @@ +#include "test_usb_host.h" + +/* + * AT90USB82/162 and ATmega8U2/16U2/32U2: same USB controller, 176 bytes + * of DPRAM, no VBUS pad. Only the memory sizes differ, so only one part of + * each line is tested. The other entries need an atmegaXXX_usb.c firmware + * file like atmega32u2_usb.c. + */ +static const struct usb_test parts[] = { +// { "at90usb82_usb.axf", "at90usb82", 0x1fff, 0x2ff, 0x1ff, +// { 0x1e, 0x93, 0x82 }, 0, 64, 0xc8 }, + { "at90usb162_usb.axf", "at90usb162", 0x3fff, 0x2ff, 0x1ff, + { 0x1e, 0x94, 0x82 }, 0, 64, 0xc8 }, +// { "atmega8u2_usb.axf", "atmega8u2", 0x1fff, 0x2ff, 0x1ff, +// { 0x1e, 0x93, 0x89 }, 0, 64, 0xc8 }, +// { "atmega16u2_usb.axf", "atmega16u2", 0x3fff, 0x2ff, 0x1ff, +// { 0x1e, 0x94, 0x89 }, 0, 64, 0xc8 }, + { "atmega32u2_usb.axf", "atmega32u2", 0x7fff, 0x4ff, 0x3ff, + { 0x1e, 0x95, 0x8a }, 0, 64, 0xc8 }, +}; + +int main(int argc, char **argv) { + tests_init(argc, argv); + for (unsigned i = 0; i < sizeof(parts) / sizeof(parts[0]); i++) + run_usb_test(&parts[i]); + tests_success(); + return 0; +} diff --git a/tests/test_atmegaxu4_usb.c b/tests/test_atmegaxu4_usb.c new file mode 100644 index 000000000..4b7ab5440 --- /dev/null +++ b/tests/test_atmegaxu4_usb.c @@ -0,0 +1,21 @@ +#include "test_usb_host.h" + +/* + * ATmega16U4/32U4: same USB controller, 832 bytes of DPRAM, VBUS pad. + * Only the memory sizes differ, so only the 32U4 is tested. The 16U4 + * entry needs an atmega16u4_usb.c firmware file like atmega32u4_usb.c. + */ +static const struct usb_test parts[] = { +// { "atmega16u4_usb.axf", "atmega16u4", 0x3fff, 0x5ff, 0x1ff, +// { 0x1e, 0x94, 0x88 }, 1, 256, 0xfd }, + { "atmega32u4_usb.axf", "atmega32u4", 0x7fff, 0xaff, 0x3ff, + { 0x1e, 0x95, 0x87 }, 1, 256, 0xfd }, +}; + +int main(int argc, char **argv) { + tests_init(argc, argv); + for (unsigned i = 0; i < sizeof(parts) / sizeof(parts[0]); i++) + run_usb_test(&parts[i]); + tests_success(); + return 0; +} diff --git a/tests/test_usb_host.h b/tests/test_usb_host.h new file mode 100644 index 000000000..3fe886a7d --- /dev/null +++ b/tests/test_usb_host.h @@ -0,0 +1,218 @@ +/* + test_usb_host.h + + USB host side of the atmegaXXuX_usb tests, drives usb_test_fw.h + through the avr_usb ioctls. + + 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 . + */ + +#include +#include "tests.h" +#include "sim_time.h" +#include "avr_usb.h" + +#define UDADDR_ADDR 0xe3 +#define RETRIES 200 + +struct usb_test { + const char * elf; + const char * mmcu; + uint16_t flashend, ramend, e2end; + uint8_t signature[3]; + int has_vbus; + uint16_t ep1_size; + uint8_t probe; +}; + +static int attached = -1; +static const char * part; + +#define part_fail(s, ...) fail("%s: " s, part, ## __VA_ARGS__) + +static void +attach_hook(struct avr_irq_t * irq, uint32_t value, void * param) +{ + attached = value; +} + +static void +run_us(avr_t * avr, uint32_t us) +{ + avr_cycle_count_t end = avr->cycle + avr_usec_to_cycles(avr, us); + + while (avr->cycle < end) { + int state = avr_run(avr); + if (state == cpu_Done || state == cpu_Crashed) + part_fail("firmware stopped, cpu state %d", state); + } +} + +// ioctl retried while the device NAKs, gives the firmware time in between +static int +usb_io(avr_t * avr, uint32_t ctl, uint8_t pipe, uint8_t * buf, uint32_t * sz) +{ + struct avr_io_usb pkt = { pipe, *sz, buf }; + int ret; + + for (int i = 0; i < RETRIES; i++) { + pkt.sz = *sz; + ret = avr_ioctl(avr, ctl, &pkt); + if (ret != AVR_IOCTL_USB_NAK) + break; + run_us(avr, 50); + } + *sz = pkt.sz; + run_us(avr, 50); + return ret; +} + +static int +control_in(avr_t * avr, uint8_t type, uint8_t req, uint16_t value, + uint8_t * data, uint16_t len) +{ + uint8_t setup[8] = { type, req, value, value >> 8, 0, 0, len, len >> 8 }; + uint32_t sz = 8; + int ret; + + if ((ret = usb_io(avr, AVR_IOCTL_USB_SETUP, 0, setup, &sz))) + part_fail("SETUP %02x/%02x: %d", type, req, ret); + sz = len; + ret = usb_io(avr, AVR_IOCTL_USB_READ, 0, data, &sz); + if (ret) + return ret; + uint32_t got = sz; + sz = 0; + if ((ret = usb_io(avr, AVR_IOCTL_USB_WRITE, 0, NULL, &sz))) + part_fail("status OUT %02x/%02x: %d", type, req, ret); + return got; +} + +static void +control_out(avr_t * avr, uint8_t type, uint8_t req, uint16_t value) +{ + uint8_t setup[8] = { type, req, value, value >> 8, 0, 0, 0, 0 }; + uint32_t sz = 8; + int ret; + + if ((ret = usb_io(avr, AVR_IOCTL_USB_SETUP, 0, setup, &sz))) + part_fail("SETUP %02x/%02x: %d", type, req, ret); + sz = 0; + if ((ret = usb_io(avr, AVR_IOCTL_USB_READ, 0, NULL, &sz)) || sz) + part_fail("status IN %02x/%02x: %d, %u bytes", type, req, ret, sz); +} + +static void +wait_attached(avr_t * avr, int want) +{ + for (int i = 0; i < 1000 && attached != want; i++) + run_us(avr, 100); + if (attached != want) + part_fail("attach state %d, expected %d", attached, want); +} + +static void +run_usb_test(const struct usb_test * t) +{ + avr_t * avr; + uint8_t data[256], echo[256]; + uint32_t sz; + int ret; + + part = t->mmcu; + avr = tests_init_avr(t->elf); + if (!avr) + part_fail("cannot load"); + if (strcmp(avr->mmcu, t->mmcu)) + part_fail("core %s", avr->mmcu); + if (avr->flashend != t->flashend || avr->ramend != t->ramend || + avr->e2end != t->e2end) + part_fail("flashend %x ramend %x e2end %x", + avr->flashend, avr->ramend, avr->e2end); + if (memcmp(avr->signature, t->signature, 3)) + part_fail("signature %02x %02x %02x", avr->signature[0], + avr->signature[1], avr->signature[2]); + + attached = -1; + avr_irq_register_notify( + avr_io_getirq(avr, AVR_IOCTL_USB_GETIRQ(), USB_IRQ_ATTACH), + attach_hook, NULL); + + // No VBUS yet: a part with a VBUS pad must stay off the bus + run_us(avr, 2000); + if (t->has_vbus && attached == 1) + part_fail("attached without VBUS"); + avr_ioctl(avr, AVR_IOCTL_USB_VBUS, (void*)1); + wait_attached(avr, 1); + + sz = 8; + if (avr_ioctl(avr, AVR_IOCTL_USB_RESET, NULL)) + part_fail("bus reset refused"); + run_us(avr, 200); + + ret = control_in(avr, 0x80, 6, 0x0100, data, 18); + if (ret != 18 || data[0] != 18 || data[1] != 1 || data[7] != 64) + part_fail("device descriptor: %d bytes, %02x %02x %02x", ret, + data[0], data[1], data[7]); + + control_out(avr, 0x00, 5, 9); + if (avr->data[UDADDR_ADDR] != 0x89) + part_fail("UDADDR %02x, expected 89", avr->data[UDADDR_ADDR]); + + ret = control_in(avr, 0xc0, 1, 0, data, 1); + if (ret != 1 || data[0] != t->probe) + part_fail("endpoint probe %02x, expected %02x (%d)", data[0], t->probe, ret); + + // unknown request, the data stage must STALL + ret = control_in(avr, 0x80, 6, 0x0300, data, 4); + if (ret != AVR_IOCTL_USB_STALL) + part_fail("GET_DESCRIPTOR(string) not stalled: %d", ret); + + control_out(avr, 0x00, 9, 1); + + for (int i = 0; i < t->ep1_size; i++) + data[i] = i * 7 + 1; + sz = t->ep1_size; + if ((ret = usb_io(avr, AVR_IOCTL_USB_WRITE, 1, data, &sz))) + part_fail("EP1 OUT: %d", ret); + + uint32_t got = 0; + for (int i = 0; i < RETRIES && got < t->ep1_size; i++) { + sz = 64; + ret = usb_io(avr, AVR_IOCTL_USB_READ, 0x82, echo + got, &sz); + if (ret) + part_fail("EP2 IN: %d", ret); + got += sz; + } + if (got != t->ep1_size) + part_fail("EP2 IN: %u bytes, expected %u", got, t->ep1_size); + for (int i = 0; i < t->ep1_size; i++) + if (echo[i] != (data[i] ^ 0x5a)) + part_fail("echo byte %d: %02x, expected %02x", i, echo[i], + data[i] ^ 0x5a); + + // endpoints past the last one are refused + sz = 0; + if (avr_ioctl(avr, AVR_IOCTL_USB_READ, + &(struct avr_io_usb){ t->has_vbus ? 7 : 5, 0, data }) != -1) + part_fail("access to a missing endpoint accepted"); + + if (t->has_vbus) { + avr_ioctl(avr, AVR_IOCTL_USB_VBUS, (void*)0); + wait_attached(avr, 0); + } + avr_terminate(avr); +} diff --git a/tests/usb_test_fw.h b/tests/usb_test_fw.h new file mode 100644 index 000000000..77a188ea7 --- /dev/null +++ b/tests/usb_test_fw.h @@ -0,0 +1,279 @@ +/* + usb_test_fw.h + + USB device firmware shared by atmega32u2_usb.c and atmega32u4_usb.c, + driven by test_usb_host.h. + + - Probes endpoint configurations before attaching and reports the CFGOK + results through a vendor request. + - Attaches when VBUS is detected (parts with a VBUS pad) or right away. + VBUSTE is enabled after VBUSTI is already set, so the USB_GEN + interrupt has to fire on the level, not on the edge. + - Answers GET_DESCRIPTOR(device), SET_ADDRESS, SET_CONFIGURATION and + vendor request 1 on EP0, stalls everything else. + - Echoes EP1 OUT packets, XORed with 0x5a, on EP2 IN. + + 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 . + */ + +#include +#include +#include + +#ifdef UEBCHX +#define EP1_SIZE 256 +#define EP1_SIZE_BITS (5 << EPSIZE0) +#define EP2_SIZE 64 +#define EP2_SIZE_BITS (3 << EPSIZE0) +#else +// 176 bytes DPRAM: EP0 64 + EP1 64 + EP2 32 +#define EP1_SIZE 64 +#define EP1_SIZE_BITS (3 << EPSIZE0) +#define EP2_SIZE 32 +#define EP2_SIZE_BITS (2 << EPSIZE0) +#endif + +// Probe result bits, see probe_endpoints() +#define PROBE_EP1_256 (1 << 0) +#define PROBE_EP2_128 (1 << 1) +#define PROBE_EP1_DUAL (1 << 2) +#define PROBE_EP3_DUAL (1 << 3) +#define PROBE_DPRAM_192 (1 << 4) +#define PROBE_EP5 (1 << 5) +#define PROBE_STALL_RMW (1 << 6) +#define PROBE_UERST (1 << 7) + +static const uint8_t device_desc[18] = { + 18, 1, 0x00, 0x02, 0xff, 0x00, 0x00, 64, + 0xeb, 0x03, 0x34, 0x12, 0x00, 0x01, 0, 0, 0, 1 +}; + +static volatile uint8_t probe; +static volatile uint8_t configured; +static uint8_t buf[EP1_SIZE]; + +static void +ep_free(uint8_t ep) +{ + UENUM = ep; + UECFG1X = 0; + UECONX = 0; +} + +// Configures an endpoint, returns CFGOK +static uint8_t +ep_config(uint8_t ep, uint8_t cfg0, uint8_t cfg1) +{ + UENUM = ep; + UECONX = (1 << EPEN); + UECFG0X = cfg0; + UECFG1X = cfg1 | (1 << ALLOC); + return (UESTA0X & (1 << CFGOK)) != 0; +} + +static void +probe_endpoints(void) +{ + const uint8_t bulk_out = (2 << EPTYPE0); + const uint8_t bulk_in = (2 << EPTYPE0) | (1 << EPDIR); + uint8_t r = 0; + + if (ep_config(1, bulk_out, 5 << EPSIZE0)) + r |= PROBE_EP1_256; + ep_free(1); + if (ep_config(2, bulk_out, 4 << EPSIZE0)) + r |= PROBE_EP2_128; + ep_free(2); + if (ep_config(1, bulk_out, (3 << EPSIZE0) | (1 << EPBK0))) + r |= PROBE_EP1_DUAL; + ep_free(1); + if (ep_config(3, bulk_out, (3 << EPSIZE0) | (1 << EPBK0))) + r |= PROBE_EP3_DUAL; + ep_free(3); + + // 3 x 64 bytes, more than the 176 bytes of the xxU2 DPRAM + ep_config(0, 0, 3 << EPSIZE0); + ep_config(1, bulk_out, 3 << EPSIZE0); + if (ep_config(2, bulk_out, 3 << EPSIZE0)) + r |= PROBE_DPRAM_192; + ep_free(2); + ep_free(1); + ep_free(0); + + if (ep_config(5, bulk_out, 3 << EPSIZE0)) + r |= PROBE_EP5; + ep_free(5); + + // read-modify-write STALLRQC must clear a pending STALLRQ + ep_config(3, bulk_in, 3 << EPSIZE0); + UECONX |= (1 << STALLRQ); + if (UECONX & (1 << STALLRQ)) { + UECONX |= (1 << STALLRQC); + if (!(UECONX & (1 << STALLRQ))) + r |= PROBE_STALL_RMW; + } + + // endpoint reset empties the bank, keeps the configuration + UEDATX = 1; UEDATX = 2; UEDATX = 3; + if (UEBCLX == 3) { + UERST = (1 << 3); + UERST = 0; + UENUM = 3; + if (UEBCLX == 0 && (UESTA0X & (1 << CFGOK)) && (UECONX & (1 << EPEN))) + r |= PROBE_UERST; + } + ep_free(3); + + probe = r; +} + +ISR(USB_GEN_vect) +{ +#ifdef VBUSTI + if (USBINT & (1 << VBUSTI)) { + USBINT &= ~(1 << VBUSTI); + if (USBSTA & (1 << VBUS)) + UDCON &= ~(1 << DETACH); + else + UDCON |= (1 << DETACH); + } +#endif + if (UDINT & (1 << EORSTI)) { + UDINT &= ~(1 << EORSTI); + configured = 0; + ep_config(0, 0, 3 << EPSIZE0); + UEIENX = (1 << RXSTPE); + } +} + +static void +ep0_send(const uint8_t * data, uint8_t len) +{ + while (len--) + UEDATX = *data++; + UEINTX &= ~(1 << TXINI); + while (!(UEINTX & (1 << RXOUTI))) + ; + UEINTX &= ~(1 << RXOUTI); +} + +static void +ep0_status_in(void) +{ + UEINTX &= ~(1 << TXINI); + while (!(UEINTX & (1 << TXINI))) + ; +} + +ISR(USB_COM_vect) +{ + uint8_t setup[8]; + + UENUM = 0; + if (!(UEINTX & (1 << RXSTPI))) + return; + for (uint8_t i = 0; i < 8; i++) + setup[i] = UEDATX; + UEINTX &= ~(1 << RXSTPI); + + uint16_t wLength = setup[6] | (setup[7] << 8); + switch (setup[1] | (setup[0] << 8)) { + case 0x8006: // GET_DESCRIPTOR + if (setup[3] != 1) + goto stall; + ep0_send(device_desc, wLength < 18 ? wLength : 18); + break; + case 0x0005: // SET_ADDRESS + UDADDR = setup[2] & 0x7f; + ep0_status_in(); + UDADDR |= (1 << ADDEN); + break; + case 0x0009: // SET_CONFIGURATION + ep_config(1, (2 << EPTYPE0), EP1_SIZE_BITS); + ep_config(2, (2 << EPTYPE0) | (1 << EPDIR), EP2_SIZE_BITS); + UENUM = 0; + ep0_status_in(); + configured = 1; + break; + case 0xc001: { // vendor: probe results + uint8_t v = probe; + ep0_send(&v, 1); + } break; + default: + stall: + UECONX |= (1 << STALLRQ); + break; + } +} + +static void +echo(void) +{ + uint16_t n, i, j; + + UENUM = 1; + if (!(UEINTX & (1 << RXOUTI))) + return; +#ifdef UEBCHX + n = UEBCLX | (UEBCHX << 8); +#else + n = UEBCLX; +#endif + for (i = 0; i < n; i++) + buf[i] = UEDATX ^ 0x5a; + UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON)); + + UENUM = 2; + for (i = 0; i < n; i += EP2_SIZE) { + while (!(UEINTX & (1 << TXINI))) + ; + UEINTX &= ~(1 << TXINI); + for (j = i; j < n && j < i + EP2_SIZE; j++) + UEDATX = buf[j]; + UEINTX &= ~(1 << FIFOCON); + } +} + +int +main(void) +{ + PLLCSR = (1 << PLLE); + while (!(PLLCSR & (1 << PLOCK))) + ; +#ifdef UHWCON + UHWCON = (1 << UVREGE); +#endif + USBCON = (1 << USBE); // clears FRZCLK + probe_endpoints(); + + UDIEN = (1 << EORSTE); +#ifdef VBUSTI + USBCON |= (1 << OTGPADE); + while (!(USBINT & (1 << VBUSTI))) + ; + sei(); + USBCON |= (1 << VBUSTE); // flag already set, must interrupt now +#else + UDCON &= ~(1 << DETACH); + sei(); +#endif + for (;;) { + cli(); + if (configured) + echo(); + sei(); + } +}