const (
D0 Pin = 16
D1 Pin = 5
D2 Pin = 4
D3 Pin = 0
D4 Pin = 2
D5 Pin = 14
D6 Pin = 12
D7 Pin = 13
D8 Pin = 15
)
GPIO pins on the NodeMCU board.
const LED = D4
Onboard blue LED (on the AI-Thinker module).
const (
SPI0_SCK_PIN = D5
SPI0_SDO_PIN = D7
SPI0_SDI_PIN = D6
SPI0_CS0_PIN = D8
)
SPI pins
const (
SDA_PIN = D2
SCL_PIN = D1
)
I2C pins
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 (
PinOutput PinMode = iota
PinInput
)
const (
UART_TX_PIN Pin = 1
UART_RX_PIN Pin = 3
)
Pins that are fixed by the chip.
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 UART0 = UART{Buffer: NewRingBuffer()}
UART0 is a hardware UART that supports both TX and RX.
func CPUFrequency() uint32
func NewRingBuffer() *RingBuffer
NewRingBuffer returns a new ring buffer.
type ADC struct {
Pin Pin
}
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 given pin as output or input pin.
func (p Pin) Get() bool
Get returns the current value of a GPIO pin when the pin is configured as an input.
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() (*uint32, uint32)
Return the register and mask to disable a given GPIO pin. This can be used to implement bit-banged drivers.
Warning: only use this on an output pin!
func (p Pin) PortMaskSet() (*uint32, uint32)
Return the register and mask to enable a given GPIO pin. This can be used to implement bit-banged drivers.
Warning: only use this on an output pin!
func (p Pin) Set(value bool)
Set sets the output value of this pin to high (true) or low (false).
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
}
func (uart UART) Buffered() int
Buffered returns the number of bytes currently stored in the RX buffer.
func (uart UART) Configure(config UARTConfig)
Configure the UART baud rate. TX and RX pins are fixed by the hardware so cannot be modified and will be ignored.
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 writes a single byte to the output buffer. Note that the hardware includes a buffer of 128 bytes which will be used first.
type UARTConfig struct {
BaudRate uint32
TX Pin
RX Pin
}