arduino

Constants

const (
	D0	= PD0	// RX
	D1	= PD1	// TX
	D2	= PD2
	D3	= PD3
	D4	= PD4
	D5	= PD5
	D6	= PD6
	D7	= PD7
	D8	= PB0
	D9	= PB1
	D10	= PB2
	D11	= PB3
	D12	= PB4
	D13	= PB5
)

Digital pins, marked as plain numbers on the board.

const LED Pin = D13

LED on the Arduino

const (
	ADC0	Pin	= PC0
	ADC1	Pin	= PC1
	ADC2	Pin	= PC2
	ADC3	Pin	= PC3
	ADC4	Pin	= PC4	// Used by TWI for SDA
	ADC5	Pin	= PC5	// Used by TWI for SCL
)

ADC on the Arduino

const (
	UART_TX_PIN	Pin	= PD1
	UART_RX_PIN	Pin	= PD0
)

UART pins

const (
	PB0	= portB + 0
	PB1	= portB + 1
	PB2	= portB + 2
	PB3	= portB + 3
	PB4	= portB + 4
	PB5	= portB + 5
	PB6	= portB + 6
	PB7	= portB + 7
	PC0	= portC + 0
	PC1	= portC + 1
	PC2	= portC + 2
	PC3	= portC + 3
	PC4	= portC + 4
	PC5	= portC + 5
	PC6	= portC + 6
	PC7	= portC + 7
	PD0	= portD + 0
	PD1	= portD + 1
	PD2	= portD + 2
	PD3	= portD + 3
	PD4	= portD + 4
	PD5	= portD + 5
	PD6	= portD + 6
	PD7	= portD + 7
)
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 (
	PinInput	PinMode	= iota
	PinInputPullup
	PinOutput
)

Variables

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

func CPUFrequency() uint32

Return the current CPU frequency in hertz.

func InitADC

func InitADC()

InitADC initializes the registers needed for ADC.

func InitPWM

func InitPWM()

InitPWM initializes the registers needed for PWM.

func NewRingBuffer

func NewRingBuffer() *RingBuffer

NewRingBuffer returns a new ring buffer.

type ADC

type ADC struct {
	Pin Pin
}

func (ADC) Configure

func (a ADC) Configure()

Configure configures a ADCPin to be able to be used to read data.

func (ADC) Get

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

type I2C struct {
}

I2C on AVR.

func (I2C) Configure

func (i2c I2C) Configure(config I2CConfig)

Configure is intended to setup the I2C interface.

func (I2C) ReadRegister

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) Tx

func (i2c I2C) Tx(addr uint16, w, r []byte) error

Tx does a single I2C transaction at the specified address. It clocks out the given address, writes the bytes in w, reads back len® bytes and stores them in r, and generates a stop condition on the bus.

func (I2C) WriteRegister

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 I2CConfig

type I2CConfig struct {
	Frequency uint32
}

I2CConfig is used to store config info for I2C.

type PWM

type PWM struct {
	Pin Pin
}

func (PWM) Configure

func (pwm PWM) Configure() error

Configure configures a PWM pin for output.

func (PWM) Set

func (pwm PWM) Set(value uint16)

Set turns on the duty cycle for a PWM pin using the provided value. On the AVR this is normally a 8-bit value ranging from 0 to 255.

type 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 (Pin) Configure

func (p Pin) Configure(config PinConfig)

Configure sets the pin to input or output.

func (Pin) Get

func (p Pin) Get() bool

Get returns the current value of a GPIO pin.

func (Pin) High

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 (Pin) Low

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 (Pin) PortMaskClear

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 (Pin) PortMaskSet

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 (Pin) Set

func (p Pin) Set(value bool)

Set changes the value of the GPIO pin. The pin must be configured as output.

type PinConfig

type PinConfig struct {
	Mode PinMode
}

type PinMode

type PinMode uint8

type RingBuffer

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 (*RingBuffer) Clear

func (rb *RingBuffer) Clear()

Clear resets the head and tail pointer to zero.

func (*RingBuffer) Get

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 (*RingBuffer) Put

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 (*RingBuffer) Used

func (rb *RingBuffer) Used() uint8

Used returns how many bytes in buffer have been used.

type UART

type UART struct {
	Buffer *RingBuffer
}

UART on the AVR.

func (UART) Buffered

func (uart UART) Buffered() int

Buffered returns the number of bytes currently stored in the RX buffer.

func (UART) Configure

func (uart UART) Configure(config UARTConfig)

Configure the UART on the AVR. Defaults to 9600 baud on Arduino.

func (UART) Read

func (uart UART) Read(data []byte) (n int, err error)

Read from the RX buffer.

func (UART) ReadByte

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) Receive

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) Write

func (uart UART) Write(data []byte) (n int, err error)

Write data to the UART.

func (UART) WriteByte

func (uart UART) WriteByte(c byte) error

WriteByte writes a byte of data to the UART.

type UARTConfig

type UARTConfig struct {
	BaudRate	uint32
	TX		Pin
	RX		Pin
}