x9pro

Constants

  1. const (
  2. LED Pin = 4 // HR LED pin
  3. UART_TX_PIN Pin = NoPin
  4. UART_RX_PIN Pin = NoPin
  5. SCL_PIN Pin = NoPin
  6. SDA_PIN Pin = NoPin
  7. SPI0_SCK_PIN Pin = 18
  8. SPI0_MISO_PIN Pin = 19
  9. SPI0_MOSI_PIN Pin = 20
  10. )

https://hackaday.io/project/144350-hacking-wearables-for-mental-health-and-more/details

  1. const (
  2. OLED_CS Pin = 15 // chip select
  3. OLED_RES Pin = 14 // reset pin
  4. OLED_DC Pin = 13 // data/command
  5. OLED_SCK Pin = 12 // SPI clock
  6. OLED_MOSI Pin = 11 // SPI MOSI (master-out, slave-in)
  7. OLED_LED_POW Pin = 16
  8. OLED_IC_POW Pin = 17
  9. )

LCD pins.

  1. const HasLowFrequencyCrystal = true
  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 = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos)
  3. PinInputPullup PinMode = PinInput | (nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos)
  4. PinInputPulldown PinMode = PinOutput | (nrf.GPIO_PIN_CNF_PULL_Pulldown << nrf.GPIO_PIN_CNF_PULL_Pos)
  5. PinOutput PinMode = (nrf.GPIO_PIN_CNF_DIR_Output << nrf.GPIO_PIN_CNF_DIR_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Disconnect << nrf.GPIO_PIN_CNF_INPUT_Pos)
  6. )

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 (
  2. ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
  3. )
  1. var (
  2. // UART0 is the hardware serial port on the NRF.
  3. UART0 = UART{Buffer: NewRingBuffer()}
  4. )

UART

  1. var (
  2. I2C0 = I2C{Bus: nrf.TWI0}
  3. I2C1 = I2C{Bus: nrf.TWI1}
  4. )

There are 2 I2C interfaces on the NRF.

  1. var (
  2. SPI0 = SPI{Bus: nrf.SPI0}
  3. SPI1 = SPI{Bus: nrf.SPI1}
  4. )

There are 2 SPI interfaces on the NRF5x.

func CPUFrequency

  1. func CPUFrequency() uint32

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 an ADC pin to be able to read analog data.

func (ADC) Get

  1. func (a ADC) Get() uint16

Get returns the current value of a ADC pin in the range 0..0xffff.

type I2C

  1. type I2C struct {
  2. Bus *nrf.TWI_Type
  3. }

I2C on the NRF.

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. SCL Pin
  4. SDA Pin
  5. }

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.

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 this pin with the given configuration.

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() (*uint32, uint32)

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

func (Pin) PortMaskSet

  1. func (p Pin) PortMaskSet() (*uint32, uint32)

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

func (Pin) Set

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

Set the pin to high or low.Warning: only use this on an output pin!

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 SPI

  1. type SPI struct {
  2. Bus *nrf.SPI_Type
  3. }

SPI on the NRF.

func (SPI) Configure

  1. func (spi SPI) Configure(config SPIConfig)

Configure is intended to setup the SPI interface.

func (SPI) Transfer

  1. func (spi SPI) Transfer(w byte) (byte, error)

Transfer writes/reads a single byte using the SPI interface.

func (SPI) Tx

  1. func (spi SPI) Tx(w, r []byte) error

Tx handles read/write operation for SPI interface. Since SPI is a syncronous write/readinterface, there must always be the same number of bytes written as bytes read.The Tx method knows about this, and offers a few different ways of calling it.

This form sends the bytes in tx buffer, putting the resulting bytes read into the rx buffer.Note that the tx and rx buffers must be the same size:

  1. spi.Tx(tx, rx)

This form sends the tx buffer, ignoring the result. Useful for sending “commands” that return zerosuntil all the bytes in the command packet have been received:

  1. spi.Tx(tx, nil)

This form sends zeros, putting the result into the rx buffer. Good for reading a “result packet”:

  1. spi.Tx(nil, rx)

type SPIConfig

  1. type SPIConfig struct {
  2. Frequency uint32
  3. SCK Pin
  4. MOSI Pin
  5. MISO Pin
  6. LSBFirst bool
  7. Mode uint8
  8. }

SPIConfig is used to store config info for SPI.

type UART

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

UART on the NRF.

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.

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

  1. func (uart UART) SetBaudRate(br uint32)

SetBaudRate sets the communication speed for the UART.

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