Library guide · API index and notation
This page is generated from the bundled library in this checkout. Signatures and adjacent source comments are reproduced below; the linked guide explains usage, storage, failures, and platform support.
import "std/embedded/chip/attiny85"
Names used in signatures
Unqualified names denote this package’s types (including other source files in the same package), language built-ins, or generic parameters such as T. Qualified names use the import aliases below. These aliases belong to the library source; import a dependency yourself to use its alias in your program.
| Alias | Package | Source file |
|---|---|---|
hal |
std/embedded/hal |
attiny85.dodo |
io |
std/io |
attiny85.dodo |
mmio |
core/mmio |
attiny85.dodo |
serial |
std/embedded/hal/serial |
attiny85.dodo |
PIN_COUNT
Constant · Source
pub const PIN_COUNT: u32 = 6
PB0
Constant · Source
pub const PB0: u32 = 0
PB1
Constant · Source
pub const PB1: u32 = 1
PB2
Constant · Source
pub const PB2: u32 = 2
PB3
Constant · Source
pub const PB3: u32 = 3
PB4
Constant · Source
pub const PB4: u32 = 4
PB5
Constant · Source
pub const PB5: u32 = 5
I2C_SDA_PIN
Constant · Source
The USI’s pins, for I2C and SPI.
pub const I2C_SDA_PIN: u32 = PB0
I2C_SCL_PIN
Constant · Source
pub const I2C_SCL_PIN: u32 = PB2
SPI_RX_PIN
Constant · Source
pub const SPI_RX_PIN: u32 = PB0
SPI_TX_PIN
Constant · Source
pub const SPI_TX_PIN: u32 = PB1
SPI_CLOCK_PIN
Constant · Source
pub const SPI_CLOCK_PIN: u32 = PB2
EEPROM_BYTES
Constant · Source
EEPROM size and the erase block of its hal Flash protocol.
pub const EEPROM_BYTES: u32 = 512
EEPROM_BLOCK
Constant · Source
pub const EEPROM_BLOCK: u32 = 64
ANALOG_MAX
Constant · Source
Analog readings are 10 bits.
pub const ANALOG_MAX: u16 = 1023
UART_MIN_BAUD
Constant · Source
The lowest and highest serial speeds
uarttakes.
pub const UART_MIN_BAUD: u32 = 9600
UART_MAX_BAUD
Constant · Source
pub const UART_MAX_BAUD: u32 = 115_200
Config
Struct · Source
What the circuit board decides. Board packages pass theirs to
take.
pub struct Config {
// The CPU clock in hertz, 1 to 20 MHz: 16.5 MHz on the Digispark,
// whose bootloader tunes the internal oscillator to the computer's USB
// clock, 16 MHz from its PLL otherwise, 8 or 1 MHz from the internal
// oscillator as the chip ships.
pub cpu_hz: u32
}
Peripherals
Struct · Source
The chip’s peripherals, from
take.
pub struct Peripherals {
pub pins: Pins
pub timer: Timer
pub watchdog: Watchdog
pub eeprom: Eeprom
}
take
Function · Source
Start the time base and return the peripherals. A second call returns Error.Busy, so no two owners exist; a clock outside 1 to 20 MHz returns Error.InvalidInput. Inlined, so that a board’s constant clock folds the divisions below away.
@inline
pub fn take(config: Config) -> Peripherals!hal.Error
reset_flags
Function · Source
Why the chip last started, from MCUSR at reset: bit 0 power-on, 1 the reset pin, 2 brown-out, 3 the watchdog. Bootloaders that run first, such as the Digispark’s, may clear it.
pub fn reset_flags() -> u8
restart
Function · Source
Restart the chip through the watchdog: it resets 16 ms later. Does not return.
pub fn restart()
jump_to_start
Function · Source
Stop the peripherals and interrupts, release every pin, and jump to address 0 without a reset: into the bootloader, where one owns the reset vector, as micronucleus on the Digispark does, otherwise to the start of the program. Does not return.
pub fn jump_to_start()
flash_byte
Function · Source
One byte of program memory at
address, such as a panic message.
pub fn flash_byte(address: u16) -> u8
Pins
Struct · Source
The GPIO pins and the peripherals on them. Each pin and peripheral is handed out once until released; a second claim returns Error.Busy, and a pin number from PIN_COUNT on returns Error.InvalidInput.
pub struct Pins {
}
Pins.output
Function · Source
A push-pull output, low until set. Claims the pin.
pub fn output(&mut self, number: u32) -> Pin!hal.Error
Pins.input
Function · Source
An input without pull-up, for a line another device drives.
pub fn input(&mut self, number: u32) -> Pin!hal.Error
Pins.input_pull_up
Function · Source
An input with the internal pull-up (20 to 50 kOhm), for a button to ground: high while released.
pub fn input_pull_up(&mut self, number: u32) -> Pin!hal.Error
Pins.release
Function · Source
Give back a pin: it becomes an input without pull-up.
pub fn release(&mut self, pin: Pin)
Pins.uart
Function · Source
A transmit-only serial port on
tx, any pin, atbaud(9600 to 115200), 8 data bits, no parity, 1 stop bit. The chip has no UART, so the runtime times each bit by counting cycles, with interrupts masked for the frame.rxmust be none: InvalidInput otherwise, and for a baud rate outside the range or one the clock cannot make within 2 %.
pub fn uart(&mut self, tx: u32, rx: Option<u32>, baud: u32) -> Serial!hal.Error
Pins.release_uart
Function · Source
Give back the port’s pin.
pub fn release_uart(&mut self, port: Serial)
Pins.i2c
Function · Source
An I2C controller on the USI, with SDA on PB0 and SCL on PB2, at about
frequency_hz(up to 400 kHz). The USI drives the lines low only, so they need pull-up resistors, as most modules have. InvalidInput for other pins or frequencies; Busy if the USI or a pin is out.
pub fn i2c(&mut self, sda: u32, scl: u32, frequency_hz: u32) -> I2c!hal.Error
Pins.release_i2c
Function · Source
Give back the controller and its pins.
pub fn release_i2c(&mut self, bus: I2c)
Pins.spi
Function · Source
An SPI controller on the USI: clock on PB2, data out on PB1, data in on PB0 (or none, leaving PB0 free), most significant bit first, at about
frequency_hz, which the program’s own clocking limits to a few hundred kHz. Use any output pin as chip select. InvalidInput for other pins or a frequency of 0; Busy if the USI or a pin is out.
pub fn spi(&mut self, clock: u32, tx: u32, rx: Option<u32>, mode: hal.SpiMode, frequency_hz: u32) -> Spi!hal.Error
Pins.release_spi
Function · Source
Give back the controller and its pins.
pub fn release_spi(&mut self, bus: Spi)
Pins.pwm
Function · Source
A PWM output. PB1 and PB4 share Timer 1, which takes
frequency_hzfrom the first of them (4 Hz up at 16.5 MHz, with less resolution as it rises: max_duty() is the count per period, up to 255); the second must ask for the same frequency. PB0 runs on the time base’s Timer 0, at its 1007 Hz (at 16.5 MHz) whateverfrequency_hzis, with max_duty() 255. InvalidInput for another pin, a frequency of 0, or one Timer 1 cannot make.
pub fn pwm(&mut self, pin: u32, frequency_hz: u32) -> Pwm!hal.Error
Pins.release_pwm
Function · Source
pub fn release_pwm(&mut self, output: Pwm)
Pins.analog
Function · Source
An analog input on PB2 (ADC1), PB3 (ADC3), PB4 (ADC2) or PB5 (ADC0): 0 at ground to ANALOG_MAX at the supply voltage. Disables the pin’s digital input buffer, which saves power. InvalidInput for another pin.
pub fn analog(&mut self, pin: u32) -> Analog!hal.Error
Pins.temperature
Function · Source
The temperature sensor, an analog input against the internal 1.1 V reference: about 300 at 25 C, rising about 1.08 per degree;
millicelsiusconverts a reading. Busy if it is out already.
pub fn temperature(&mut self) -> Analog!hal.Error
Pins.release_analog
Function · Source
pub fn release_analog(&mut self, input: Analog)
Pin
Struct · Source
One GPIO pin, from Pins.output or Pins.input. Implements the hal OutputPin and InputPin protocols.
pub struct Pin: hal.OutputPin + hal.InputPin {
// Private implementation fields omitted.
}
Pin.set_high
Function · Source
pub fn set_high(&mut self)
Pin.set_low
Function · Source
pub fn set_low(&mut self)
Pin.toggle
Function · Source
Writing a one to PINB toggles the output in one step.
pub fn toggle(&mut self)
Pin.is_high
Function · Source
pub fn is_high(&self) -> bool
Pin.set_as_output
Function · Source
Drive the pin at its output level, for a line that changes direction.
pub fn set_as_output(&mut self)
Pin.set_as_input
Function · Source
Stop driving the pin; is_high reads it.
pub fn set_as_input(&mut self)
Pin.set_pull_up
Function · Source
The internal pull-up of an input pin (the PORTB bit while it is an input).
pub fn set_pull_up(&mut self, on: bool)
Pin.number
Function · Source
The pin’s number, as given to Pins.
pub fn number(&self) -> u32
Timer
Struct · Source
Microseconds since
take, from Timer 0, which ticks every 64 cycles (3.88 us at 16.5 MHz), and busy-waiting delays. The runtime adds up the microseconds of each overflow of the timer, so reading the time takes no 64-bit arithmetic. Implements the hal Delay and Counter protocols.
pub struct Timer: hal.Delay + hal.Counter {
// Private implementation fields omitted.
}
Timer.now_us
Function · Source
Microseconds since take(), in steps of 64 cycles.
pub fn now_us(&self) -> u64
Timer.delay_us
Function · Source
Busy-wait
microseconds, counting cycles, so it also works with interrupts masked; interrupt handlers that run meanwhile lengthen it, and a call takes a few microseconds of its own.
pub fn delay_us(&mut self, microseconds: u32)
Timer.delay_ms
Function · Source
pub fn delay_ms(&mut self, milliseconds: u32)
Serial
Struct · Source
A transmit-only serial port, from Pins.uart. Implements std/io’s Writer and the serial Port and PollPort protocols, so std/fmt writes to it; reading reports io.ErrorKind.InvalidInput. Each byte goes out before write returns, so flush has nothing to wait for.
pub struct Serial: serial.Port + serial.PollPort {
// Private implementation fields omitted.
}
Serial.baud
Function · Source
pub fn baud(&self) -> u32
Serial.set_crlf
Function · Source
Send each “\n” as “\r\n”, as terminals expect.
pub fn set_crlf(&mut self, on: bool)
Serial.write
Function · Source
pub fn write(&mut self, bytes: &[u8]) -> usize!io.Error
Serial.poll_write
Function · Source
Send one byte, which never has to wait for room.
pub fn poll_write(&mut self, bytes: &[u8]) -> io.Poll!io.Error
Serial.flush
Function · Source
pub fn flush(&mut self) -> void!io.Error
Serial.poll_flush
Function · Source
pub fn poll_flush(&mut self) -> bool!io.Error
Serial.read
Function · Source
pub fn read(&mut self, buffer: &mut[u8]) -> usize!io.Error
Serial.poll_read
Function · Source
pub fn poll_read(&mut self, buffer: &mut[u8]) -> io.Poll!io.Error
I2c
Struct · Source
An I2C controller on the USI, from Pins.i2c, after Atmel’s application note AVR310: the program clocks each bit, and waits while a target holds SCL low. Implements hal.I2c.
pub struct I2c: hal.I2c {
// Private implementation fields omitted.
}
I2c.frequency_hz
Function · Source
The SCL frequency the delays give, roughly.
pub fn frequency_hz(&self) -> u32
I2c.write
Function · Source
pub fn write(&mut self, address: u8, bytes: &[u8]) -> void!hal.Error
I2c.read
Function · Source
pub fn read(&mut self, address: u8, buffer: &mut[u8]) -> void!hal.Error
I2c.write_read
Function · Source
pub fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut[u8]) -> void!hal.Error
Spi
Struct · Source
An SPI controller on the USI, from Pins.spi: the program clocks each edge. Implements hal.Spi.
pub struct Spi: hal.Spi {
// Private implementation fields omitted.
}
Spi.frequency_hz
Function · Source
The clock frequency the delays give, roughly.
pub fn frequency_hz(&self) -> u32
Spi.transfer
Function · Source
pub fn transfer(&mut self, bytes: &mut[u8]) -> void!hal.Error
Spi.write
Function · Source
pub fn write(&mut self, bytes: &[u8]) -> void!hal.Error
Pwm
Struct · Source
A PWM output, from Pins.pwm. Implements hal.PwmOutput.
pub struct Pwm: hal.PwmOutput {
// Private implementation fields omitted.
}
Pwm.max_duty
Function · Source
pub fn max_duty(&self) -> u16
Pwm.set_duty
Function · Source
High for
dutyof every max_duty() counts; 0 disconnects the output, which holds the pin low.
pub fn set_duty(&mut self, duty: u16) -> void!hal.Error
Pwm.pin
Function · Source
pub fn pin(&self) -> u32
Analog
Struct · Source
An analog input, from Pins.analog or Pins.temperature. Implements hal.AnalogInput. A conversion takes 13 converter clocks, 101 us at 16.5 MHz.
pub struct Analog: hal.AnalogInput {
// Private implementation fields omitted.
}
Analog.max_value
Function · Source
pub fn max_value(&self) -> u16
Analog.read
Function · Source
Convert once. After a switch to another reference, the first conversions are discarded while it settles.
pub fn read(&mut self) -> u16!hal.Error
Analog.pin
Function · Source
pub fn pin(&self) -> u32
millicelsius
Function · Source
The temperature in thousandths of a degree Celsius that a reading of the temperature sensor shows, from the datasheet’s typical values (230 at -40 C, 300 at 25 C, 370 at 85 C). Chips differ by up to about 10 degrees; calibrate with an offset of your own where that matters.
pub fn millicelsius(value: u16) -> i32
Watchdog
Struct · Source
The watchdog: stopped until
start, it then resets the chip unlessfeedis called within the timeout. Implements hal.Watchdog.
pub struct Watchdog: hal.Watchdog {
// Private implementation fields omitted.
}
Watchdog.max_timeout_ms
Function · Source
The longest timeout, 8 s.
pub fn max_timeout_ms(&self) -> u32
Watchdog.start
Function · Source
Start (or restart) with the longest of the watchdog’s timeouts, 16, 32 or 64 ms, 0.125 to 8 s in powers of two, that does not exceed
timeout_ms. InvalidInput below 16 ms or above max_timeout_ms().
pub fn start(&mut self, timeout_ms: u32) -> void!hal.Error
Watchdog.feed
Function · Source
pub fn feed(&mut self)
Watchdog.stop
Function · Source
pub fn stop(&mut self)
Watchdog.reset_reason
Function · Source
Whether the watchdog reset the chip last time, from the reset flags.
pub fn reset_reason(&self) -> hal.ResetReason
Eeprom
Struct · Source
The 512-byte EEPROM, which keeps its bytes without power for about 100,000 writes each.
readandupdateare the usual way to keep settings:saved: [4]u8 = [0u8; 4] b.eeprom.read(0, &mut saved)! b.eeprom.update(0, &saved)!
It also implements hal.Flash, with EEPROM_BLOCK-byte erase blocks and single-byte writes, for drivers written against that protocol. A byte takes 3.4 ms to erase and write, 1.8 ms for each alone. Flashing through the Digispark’s bootloader leaves the EEPROM as it was.
pub struct Eeprom: hal.Flash {
}
Eeprom.capacity
Function · Source
pub fn capacity(&self) -> u32
Eeprom.erase_size
Function · Source
pub fn erase_size(&self) -> u32
Eeprom.write_size
Function · Source
pub fn write_size(&self) -> u32
Eeprom.read
Function · Source
pub fn read(&mut self, offset: u32, buffer: &mut[u8]) -> void!hal.Error
Eeprom.erase
Function · Source
Set the range to 0xFF, skipping bytes that already are.
pub fn erase(&mut self, offset: u32, length: u32) -> void!hal.Error
Eeprom.write
Function · Source
Program
bytes, which clears bits only, as the Flash protocol writes to erased bytes.
pub fn write(&mut self, offset: u32, bytes: &[u8]) -> void!hal.Error
Eeprom.update
Function · Source
Replace the bytes at
offsetwithbytes, whatever they held, erasing and writing only the bytes that change: the usual way to keep a setting.
pub fn update(&mut self, offset: u32, bytes: &[u8]) -> void!hal.Error
report_panic
Function · Source
Write a failed check, as the compiler passes it to the panic hook, to the serial port if it is open:
dodo: arithmetic overflow check failed at /home/me/blink/main.dodo:12:5
the line a desktop program prints. Only the first call reports. SAFETY:
checkandfilemust be the hook’s NUL-terminated strings, which the compiler places in program memory, and the caller must halt or reset afterwards, as this takes over the serial port.
pub unsafe fn report_panic(check: *const u8, file: *const u8, line: u32, column: u32)
Requires an unsafe context. Follow the source safety preconditions and the linked guide before calling this API.