nrf52840-mdk

Constants

  1. const HasLowFrequencyCrystal = true
  1. const (
  2. LED Pin = LED_GREEN
  3. LED_GREEN Pin = 22
  4. LED_RED Pin = 23
  5. LED_BLUE Pin = 24
  6. )

LEDs on the nrf52840-mdk (nRF52840 dev board)

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

UART pins

  1. const (
  2. SDA_PIN = NoPin
  3. SCL_PIN = NoPin
  4. )

I2C pins (unused)

  1. const (
  2. SPI0_SCK_PIN = NoPin
  3. SPI0_MOSI_PIN = NoPin
  4. SPI0_MISO_PIN = NoPin
  5. )

SPI pins (unused)

  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. )
  1. const (
  2. P0_00 Pin = 0
  3. P0_01 Pin = 1
  4. P0_02 Pin = 2
  5. P0_03 Pin = 3
  6. P0_04 Pin = 4
  7. P0_05 Pin = 5
  8. P0_06 Pin = 6
  9. P0_07 Pin = 7
  10. P0_08 Pin = 8
  11. P0_09 Pin = 9
  12. P0_10 Pin = 10
  13. P0_11 Pin = 11
  14. P0_12 Pin = 12
  15. P0_13 Pin = 13
  16. P0_14 Pin = 14
  17. P0_15 Pin = 15
  18. P0_16 Pin = 16
  19. P0_17 Pin = 17
  20. P0_18 Pin = 18
  21. P0_19 Pin = 19
  22. P0_20 Pin = 20
  23. P0_21 Pin = 21
  24. P0_22 Pin = 22
  25. P0_23 Pin = 23
  26. P0_24 Pin = 24
  27. P0_25 Pin = 25
  28. P0_26 Pin = 26
  29. P0_27 Pin = 27
  30. P0_28 Pin = 28
  31. P0_29 Pin = 29
  32. P0_30 Pin = 30
  33. P0_31 Pin = 31
  34. P1_00 Pin = 32
  35. P1_01 Pin = 33
  36. P1_02 Pin = 34
  37. P1_03 Pin = 35
  38. P1_04 Pin = 36
  39. P1_05 Pin = 37
  40. P1_06 Pin = 38
  41. P1_07 Pin = 39
  42. P1_08 Pin = 40
  43. P1_09 Pin = 41
  44. P1_10 Pin = 42
  45. P1_11 Pin = 43
  46. P1_12 Pin = 44
  47. P1_13 Pin = 45
  48. P1_14 Pin = 46
  49. P1_15 Pin = 47
  50. )

Hardware pins

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