const (
P0 Pin = PB0
P1 Pin = PB1
P2 Pin = PB2
P3 Pin = PB3
P4 Pin = PB4
P5 Pin = PB5
LED = P1
)
const (
TWI_FREQ_100KHZ = 100000
TWI_FREQ_400KHZ = 400000
)
TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
const NoPin = Pin(0xff)
NoPin explicitly indicates “not a pin”. Use this pin if you want to leave one of the pins in a peripheral unconfigured (if supported by the hardware).
const (
PB0 Pin = iota
PB1
PB2
PB3
PB4
PB5
)
const (
PinInput PinMode = iota
PinInputPullup
PinOutput
)
var (
ErrInvalidInputPin = errors.New("machine: invalid input pin")
ErrInvalidOutputPin = errors.New("machine: invalid output pin")
ErrInvalidClockPin = errors.New("machine: invalid clock pin")
ErrInvalidDataPin = errors.New("machine: invalid data pin")
ErrNoPinChangeChannel = errors.New("machine: no channel available for pin interrupt")
)
var I2C0 = I2C{}
I2C0 is the only I2C interface on most AVRs.
var (
// UART0 is the hardware serial port on the AVR.
UART0 = UART{Buffer: NewRingBuffer()}
)
UART
func CPUFrequency() uint32
Return the current CPU frequency in hertz.
func InitADC()
InitADC initializes the registers needed for ADC.
func NewRingBuffer() *RingBuffer
NewRingBuffer returns a new ring buffer.
type ADC struct {
Pin Pin
}
func (a ADC) Configure()
Configure configures a ADCPin to be able to be used to read data.
func (a ADC) Get() uint16
Get returns the current value of a ADC pin, in the range 0..0xffff. The AVR has an ADC of 10 bits precision so the lower 6 bits will be zero.
type I2C struct {
}
I2C on AVR.
func (i2c I2C) ReadRegister(address uint8, register uint8, data []byte) error
ReadRegister transmits the register, restarts the connection as a read operation, and reads the response.
Many I2C-compatible devices are organized in terms of registers. This method is a shortcut to easily read such registers. Also, it only works for devices with 7-bit addresses, which is the vast majority.
func (i2c I2C) Tx(addr uint16, w, r []byte) error
Tx is a dummy implementation. I2C has not been implemented for ATtiny devices.
func (i2c I2C) WriteRegister(address uint8, register uint8, data []byte) error
WriteRegister transmits first the register and then the data to the peripheral device.
Many I2C-compatible devices are organized in terms of registers. This method is a shortcut to easily write to such registers. Also, it only works for devices with 7-bit addresses, which is the vast majority.
type PWM struct {
Pin Pin
}
type Pin uint8
Pin is a single pin on a chip, which may be connected to other hardware devices. It can either be used directly as GPIO pin or it can be used in other peripherals like ADC, I2C, etc.
func (p Pin) Configure(config PinConfig)
Configure sets the pin to input or output.
func (p Pin) Get() bool
Get returns the current value of a GPIO pin.
func (p Pin) High()
High sets this GPIO pin to high, assuming it has been configured as an output pin. It is hardware dependent (and often undefined) what happens if you set a pin to high that is not configured as an output pin.
func (p Pin) Low()
Low sets this GPIO pin to low, assuming it has been configured as an output pin. It is hardware dependent (and often undefined) what happens if you set a pin to low that is not configured as an output pin.
func (p Pin) PortMaskClear() (*volatile.Register8, uint8)
Return the register and mask to disable a given port. This can be used to implement bit-banged drivers.
Warning: there are no separate pin set/clear registers on the AVR. The returned mask is only valid as long as no other pin in the same port has been changed.
func (p Pin) PortMaskSet() (*volatile.Register8, uint8)
Return the register and mask to enable a given GPIO pin. This can be used to implement bit-banged drivers.
Warning: there are no separate pin set/clear registers on the AVR. The returned mask is only valid as long as no other pin in the same port has been changed.
func (p Pin) Set(value bool)
Set changes the value of the GPIO pin. The pin must be configured as output.
type PinConfig struct {
Mode PinMode
}
type PinMode uint8
type RingBuffer struct {
rxbuffer [bufferSize]volatile.Register8
head volatile.Register8
tail volatile.Register8
}
RingBuffer is ring buffer implementation inspired by post at https://www.embeddedrelated.com/showthread/comp.arch.embedded/77084-1.php
func (rb *RingBuffer) Clear()
Clear resets the head and tail pointer to zero.
func (rb *RingBuffer) Get() (byte, bool)
Get returns a byte from the buffer. If the buffer is empty, the method will return a false as the second value.
func (rb *RingBuffer) Put(val byte) bool
Put stores a byte in the buffer. If the buffer is already full, the method will return false.
func (rb *RingBuffer) Used() uint8
Used returns how many bytes in buffer have been used.
type UART struct {
Buffer *RingBuffer
}
UART on the AVR is a dummy implementation. UART has not been implemented for ATtiny devices.
func (uart UART) Buffered() int
Buffered returns the number of bytes currently stored in the RX buffer.
func (uart UART) Configure(config UARTConfig)
Configure is a dummy implementation. UART has not been implemented for ATtiny devices.
func (uart UART) Read(data []byte) (n int, err error)
Read from the RX buffer.
func (uart UART) ReadByte() (byte, error)
ReadByte reads a single byte from the RX buffer. If there is no data in the buffer, returns an error.
func (uart UART) Receive(data byte)
Receive handles adding data to the UART’s data buffer. Usually called by the IRQ handler for a machine.
func (uart UART) Write(data []byte) (n int, err error)
Write data to the UART.
func (uart UART) WriteByte(c byte) error
WriteByte is a dummy implementation. UART has not been implemented for ATtiny devices.
type UARTConfig struct {
BaudRate uint32
TX Pin
RX Pin
}