arduino

Constants

const CPU_FREQUENCY = 16000000
const LED Pin = 13

LED on the Arduino

const (
	ADC0	Pin	= 0
	ADC1	Pin	= 1
	ADC2	Pin	= 2
	ADC3	Pin	= 3
	ADC4	Pin	= 4	// Used by TWI for SDA
	ADC5	Pin	= 5	// Used by TWI for SCL
)

ADC on the Arduino

const (
	UART_TX_PIN	Pin	= 1
	UART_RX_PIN	Pin	= 0
)

UART pins

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(-1)

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
	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")
)
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 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()

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 int8

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

It has some limitations currently due to how “volatile” variables that are members of a struct are not compiled correctly by TinyGo. See https://github.com/tinygo-org/tinygo/issues/151 for details.

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
}