Peripherals#
Avr8Sharp emulates the ATmega/ATtiny peripheral set with datasheet-accurate register
behaviour. All peripherals are optional and added through the AvrTestSimulation builder
API (or wired up automatically by the board presets).
Coverage#
Peripheral |
Status |
Notes |
|---|---|---|
GPIO (I/O ports) |
โ |
Pin-change and external interrupts |
USART |
โ |
Baud config, TX/RX, interrupts |
Timer 0/2 (8-bit) |
โ |
Normal, CTC, Fast PWM, Phase-correct PWM |
Timer 1/3/4/5 (16-bit) |
โ |
Normal, CTC, Fast PWM, Phase-correct PWM, Input Capture |
ATtiny85 TC1 (8-bit) |
๐ก |
Normal mode; CTC and PWM modes not emulated |
SPI |
โ |
Master mode, SPSR/SPCR/SPDR |
TWI (IยฒC) |
โ |
Master mode; slave state machine not emulated |
ADC |
โ |
8 channels, single-conversion mode |
EEPROM |
โ |
Read/write/erase, EE_RDY interrupt |
USI |
โ |
Three-wire and two-wire mode |
Watchdog |
โ |
Prescaler, reset, interrupt mode |
For the mapping between Arduino pin numbers and simulator port/pin properties, see Board pin maps.
Note
Per-peripheral reference pages are being filled in. For now, the API surface is documented via XML-doc comments in the source; see Reference.
GPIO#
All I/O ports expose AvrIoPort, which mirrors the hardware PIN/DDR/PORT triple:
// Drive PB5 high from the test host
uno.PortB.SetPin(5, true);
// Read firmware output
bool isHigh = uno.PortB.GetPinState(5);
// Register a listener for any pin change
uno.PortB.AddListener(pin => Console.WriteLine($"PB{pin} changed"));
USART#
// Capture all TX bytes in a SerialProbe (done automatically by board presets)
sim.AddUsart(AvrUsart.Usart0Config, out var serial);
serial.OnByte = b => Console.Write((char)b);
// Inject a byte into the RX FIFO (simulates host โ firmware)
serial.Usart.WriteByte(0x41); // send 'A'
Timers#
Timer peripherals fire output-compare events that toggle OC pins automatically. You can also hook the compare match callback:
sim.AddTimer(AvrTimer.Timer1Config, out var timer1);
timer1.OnOutputCompareMatch += channel => Console.WriteLine($"OC1{(char)('A' + channel)}");
EEPROM#
sim.AddEeprom(AvrEeprom.EepromConfig, out var eeprom, eepromSize: 1024);
// Pre-load EEPROM content before running firmware
eeprom.Backend.Data[0] = 0xCA;
eeprom.Backend.Data[1] = 0xFE;
ADC#
sim.AddAdc(AvrAdc.AdcConfig, out var adc);
// Set the voltage on channel 0 (in millivolts, 0โ5000)
adc.ChannelValues[0] = 2500; // 2.5 V โ mid-scale