hifive1b

Constants

  1. const (
  2. P00 Pin = 0
  3. P01 Pin = 1
  4. P02 Pin = 2
  5. P03 Pin = 3
  6. P04 Pin = 4
  7. P05 Pin = 5
  8. P06 Pin = 6
  9. P07 Pin = 7
  10. P08 Pin = 8
  11. P09 Pin = 9
  12. P10 Pin = 10
  13. P11 Pin = 11
  14. P12 Pin = 12
  15. P13 Pin = 13
  16. P14 Pin = 14
  17. P15 Pin = 15
  18. P16 Pin = 16
  19. P17 Pin = 17
  20. P18 Pin = 18
  21. P19 Pin = 19
  22. P20 Pin = 20
  23. P21 Pin = 21
  24. P22 Pin = 22
  25. P23 Pin = 23
  26. P24 Pin = 24
  27. P25 Pin = 25
  28. P26 Pin = 26
  29. P27 Pin = 27
  30. P28 Pin = 28
  31. P29 Pin = 29
  32. P30 Pin = 30
  33. P31 Pin = 31
  34. )
  1. const (
  2. D0 = P16
  3. D1 = P17
  4. D2 = P18
  5. D3 = P19 // Green LED/PWM (PWM1_PWM1)
  6. D4 = P20 // PWM (PWM1_PWM0)
  7. D5 = P21 // Blue LED/PWM (PWM1_PWM2)
  8. D6 = P22 // Red LED/PWM (PWM1_PWM3)
  9. D7 = P16
  10. D8 = NoPin // PWM?
  11. D9 = P01
  12. D10 = P02 // SPI1_CS0
  13. D11 = P03 // SPI1_DQ0
  14. D12 = P04 // SPI1_DQ1
  15. D13 = P05 // SPI1_SCK
  16. D14 = NoPin // not connected
  17. D15 = P09 // does not seem to work?
  18. D16 = P10 // PWM (PWM2_PWM0)
  19. D17 = P11 // PWM (PWM2_PWM1)
  20. D18 = P12 // SDA (I2C0_SDA)/PWM (PWM2_PWM2)
  21. D19 = P13 // SDL (I2C0_SCL)/PWM (PWM2_PWM3)
  22. )
  1. const (
  2. LED = LED1
  3. LED1 = LED_RED
  4. LED2 = LED_GREEN
  5. LED3 = LED_BLUE
  6. LED_RED = P22
  7. LED_GREEN = P19
  8. LED_BLUE = P21
  9. )
  1. const (
  2. // TODO: figure out the pin numbers for these.
  3. UART_TX_PIN = NoPin
  4. UART_RX_PIN = NoPin
  5. )
  1. const (
  2. SPI0_SCK_PIN = NoPin
  3. SPI0_MOSI_PIN = NoPin
  4. SPI0_MISO_PIN = NoPin
  5. SPI1_SCK_PIN = D13
  6. SPI1_MOSI_PIN = D11
  7. SPI1_MISO_PIN = D12
  8. )

SPI pins

  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. PinPWM
  5. PinSPI
  6. PinI2C = PinSPI
  7. )

Variables

  1. var (
  2. SPI1 = SPI{
  3. Bus: sifive.QSPI1,
  4. }
  5. )

SPI on the HiFive1.

  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. UART0 = UART{Bus: sifive.UART0, Buffer: NewRingBuffer()}
  3. )
  1. var (
  2. ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
  3. )

func CPUFrequency

  1. func CPUFrequency() uint32

func NewRingBuffer

  1. func NewRingBuffer() *RingBuffer

NewRingBuffer returns a new ring buffer.

type ADC

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

type PWM

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

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

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

Set the pin to high or low.

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 *sifive.QSPI_Type
  3. }

SPI on the FE310. The normal SPI0 is actually a quad-SPI meant for flash, so it is bestto use SPI1 or SPI2 port for most applications.

func (SPI) Configure

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

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. Bus *sifive.UART_Type
  3. Buffer *RingBuffer
  4. }

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)

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)

type UARTConfig

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