arduino

Constants

  1. const LED Pin = 13

LED on the Arduino

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

ADC on the Arduino

  1. const (
  2. UART_TX_PIN Pin = 1
  3. UART_RX_PIN Pin = 0
  4. )

UART pins

  1. const (
  2. TWI_FREQ_100KHZ = 100000
  3. TWI_FREQ_400KHZ = 400000
  4. )

TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.

  1. const NoPin = Pin(-1)

NoPin explicitly indicates “not a pin”. Use this pin if you want to leave oneof the pins in a peripheral unconfigured (if supported by the hardware).

  1. const (
  2. PinInput PinMode = iota
  3. PinOutput
  4. )

Variables

  1. var (
  2. ErrInvalidInputPin = errors.New("machine: invalid input pin")
  3. ErrInvalidOutputPin = errors.New("machine: invalid output pin")
  4. ErrInvalidClockPin = errors.New("machine: invalid clock pin")
  5. ErrInvalidDataPin = errors.New("machine: invalid data pin")
  6. )
  1. var I2C0 = I2C{}

I2C0 is the only I2C interface on most AVRs.

  1. var (
  2. // UART0 is the hardware serial port on the AVR.
  3. UART0 = UART{Buffer: NewRingBuffer()}
  4. )

UART

func CPUFrequency

  1. func CPUFrequency() uint32

Return the current CPU frequency in hertz.

func InitADC

  1. func InitADC()

InitADC initializes the registers needed for ADC.

func InitPWM

  1. func InitPWM()

InitPWM initializes the registers needed for PWM.

func NewRingBuffer

  1. func NewRingBuffer() *RingBuffer

NewRingBuffer returns a new ring buffer.

type ADC

  1. type ADC struct {
  2. Pin Pin
  3. }

func (ADC) Configure

  1. func (a ADC) Configure()

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

func (ADC) Get

  1. func (a ADC) Get() uint16

Get returns the current value of a ADC pin, in the range 0..0xffff. The AVRhas an ADC of 10 bits precision so the lower 6 bits will be zero.

type I2C

  1. type I2C struct {
  2. }

I2C on AVR.

func (I2C) Configure

  1. func (i2c I2C) Configure(config I2CConfig)

Configure is intended to setup the I2C interface.

func (I2C) ReadRegister

  1. func (i2c I2C) ReadRegister(address uint8, register uint8, data []byte) error

ReadRegister transmits the register, restarts the connection as a readoperation, and reads the response.

Many I2C-compatible devices are organized in terms of registers. This methodis a shortcut to easily read such registers. Also, it only works for deviceswith 7-bit addresses, which is the vast majority.

func (I2C) Tx

  1. 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

  1. func (i2c I2C) WriteRegister(address uint8, register uint8, data []byte) error

WriteRegister transmits first the register and then the data to theperipheral device.

Many I2C-compatible devices are organized in terms of registers. This methodis a shortcut to easily write to such registers. Also, it only works fordevices with 7-bit addresses, which is the vast majority.

type I2CConfig

  1. type I2CConfig struct {
  2. Frequency uint32
  3. }

I2CConfig is used to store config info for I2C.

type PWM

  1. type PWM struct {
  2. Pin Pin
  3. }

func (PWM) Configure

  1. func (pwm PWM) Configure()

Configure configures a PWM pin for output.

func (PWM) Set

  1. 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 a8-bit value ranging from 0 to 255.

type Pin

  1. type Pin int8

Pin is a single pin on a chip, which may be connected to other hardwaredevices. It can either be used directly as GPIO pin or it can be used inother peripherals like ADC, I2C, etc.

func (Pin) Configure

  1. func (p Pin) Configure(config PinConfig)

Configure sets the pin to input or output.

func (Pin) Get

  1. func (p Pin) Get() bool

Get returns the current value of a GPIO pin.

func (Pin) High

  1. func (p Pin) High()

High sets this GPIO pin to high, assuming it has been configured as an outputpin. It is hardware dependent (and often undefined) what happens if you set apin to high that is not configured as an output pin.

func (Pin) Low

  1. func (p Pin) Low()

Low sets this GPIO pin to low, assuming it has been configured as an outputpin. It is hardware dependent (and often undefined) what happens if you set apin to low that is not configured as an output pin.

func (Pin) PortMaskClear

  1. func (p Pin) PortMaskClear() (*volatile.Register8, uint8)

Return the register and mask to disable a given port. This can be used toimplement bit-banged drivers.

Warning: there are no separate pin set/clear registers on the AVR. Thereturned mask is only valid as long as no other pin in the same port has beenchanged.

func (Pin) PortMaskSet

  1. func (p Pin) PortMaskSet() (*volatile.Register8, uint8)

Return the register and mask to enable a given GPIO pin. This can be used toimplement bit-banged drivers.

Warning: there are no separate pin set/clear registers on the AVR. Thereturned mask is only valid as long as no other pin in the same port has beenchanged.

func (Pin) Set

  1. func (p Pin) Set(value bool)

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

type PinConfig

  1. type PinConfig struct {
  2. Mode PinMode
  3. }

type PinMode

  1. type PinMode uint8

type RingBuffer

  1. type RingBuffer struct {
  2. rxbuffer [bufferSize]volatile.Register8
  3. head volatile.Register8
  4. tail volatile.Register8
  5. }

RingBuffer is ring buffer implementation inspired by post athttps://www.embeddedrelated.com/showthread/comp.arch.embedded/77084-1.php

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

func (*RingBuffer) Get

  1. 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

  1. func (rb *RingBuffer) Put(val byte) bool

Put stores a byte in the buffer. If the buffer is alreadyfull, the method will return false.

func (*RingBuffer) Used

  1. func (rb *RingBuffer) Used() uint8

Used returns how many bytes in buffer have been used.

type UART

  1. type UART struct {
  2. Buffer *RingBuffer
  3. }

UART on the AVR.

func (UART) Buffered

  1. func (uart UART) Buffered() int

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

func (UART) Configure

  1. func (uart UART) Configure(config UARTConfig)

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

func (UART) Read

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

Read from the RX buffer.

func (UART) ReadByte

  1. 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

  1. 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

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

Write data to the UART.

func (UART) WriteByte

  1. func (uart UART) WriteByte(c byte) error

WriteByte writes a byte of data to the UART.

type UARTConfig

  1. type UARTConfig struct {
  2. BaudRate uint32
  3. TX Pin
  4. RX Pin
  5. }