bluepill

Constants

  1. const (
  2. PA0 = portA + 0
  3. PA1 = portA + 1
  4. PA2 = portA + 2
  5. PA3 = portA + 3
  6. PA4 = portA + 4
  7. PA5 = portA + 5
  8. PA6 = portA + 6
  9. PA7 = portA + 7
  10. PA8 = portA + 8
  11. PA9 = portA + 9
  12. PA10 = portA + 10
  13. PA11 = portA + 11
  14. PA12 = portA + 12
  15. PA13 = portA + 13
  16. PA14 = portA + 14
  17. PA15 = portA + 15
  18. PB0 = portB + 0
  19. PB1 = portB + 1
  20. PB2 = portB + 2
  21. PB3 = portB + 3
  22. PB4 = portB + 4
  23. PB5 = portB + 5
  24. PB6 = portB + 6
  25. PB7 = portB + 7
  26. PB8 = portB + 8
  27. PB9 = portB + 9
  28. PB10 = portB + 10
  29. PB11 = portB + 11
  30. PB12 = portB + 12
  31. PB13 = portB + 13
  32. PB14 = portB + 14
  33. PB15 = portB + 15
  34. PC13 = portC + 13
  35. PC14 = portC + 14
  36. PC15 = portC + 15
  37. )

https://wiki.stm32duino.com/index.php?title=File:Bluepillpinout.gif

  1. const (
  2. LED = PC13
  3. )
  1. const (
  2. UART_TX_PIN = PA9
  3. UART_RX_PIN = PA10
  4. UART_ALT_TX_PIN = PB6
  5. UART_ALT_RX_PIN = PB7
  6. )

UART pins

  1. const (
  2. SPI0_SCK_PIN = PA5
  3. SPI0_MOSI_PIN = PA7
  4. SPI0_MISO_PIN = PA6
  5. )

SPI pins

  1. const (
  2. SDA_PIN = PB7
  3. SCL_PIN = PB6
  4. )

I2C 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 = 0 // Input mode
  3. PinOutput10MHz PinMode = 1 // Output mode, max speed 10MHz
  4. PinOutput2MHz PinMode = 2 // Output mode, max speed 2MHz
  5. PinOutput50MHz PinMode = 3 // Output mode, max speed 50MHz
  6. PinOutput PinMode = PinOutput2MHz
  7. PinInputModeAnalog PinMode = 0 // Input analog mode
  8. PinInputModeFloating PinMode = 4 // Input floating mode
  9. PinInputModePullUpDown PinMode = 8 // Input pull up/down mode
  10. PinInputModeReserved PinMode = 12 // Input mode (reserved)
  11. PinOutputModeGPPushPull PinMode = 0 // Output mode general purpose push/pull
  12. PinOutputModeGPOpenDrain PinMode = 4 // Output mode general purpose open drain
  13. PinOutputModeAltPushPull PinMode = 8 // Output mode alt. purpose push/pull
  14. PinOutputModeAltOpenDrain PinMode = 12 // Output mode alt. purpose open drain
  15. )

Variables

  1. var (
  2. // USART1 is the first hardware serial port on the STM32.
  3. // Both UART0 and UART1 refer to USART1.
  4. UART0 = UART{
  5. Buffer: NewRingBuffer(),
  6. Bus: stm32.USART1,
  7. }
  8. UART1 = &UART0
  9. )
  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. SPI1 = SPI{Bus: stm32.SPI1}
  3. SPI0 = SPI1
  4. )

There are 3 SPI interfaces on the STM32F103xx.Since the first interface is named SPI1, both SPI0 and SPI1 refer to SPI1.TODO: implement SPI2 and SPI3.

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

There are 2 I2C interfaces on the STM32F103xx.Since the first interface is named I2C1, both I2C0 and I2C1 refer to I2C1.TODO: implement I2C2.

  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 I2C

  1. type I2C struct {
  2. Bus *stm32.I2C_Type
  3. }

I2C on the STM32F103xx.

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

  1. func (i2c I2C) WriteByte(data byte) error

WriteByte writes a single byte to the I2C 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. }

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.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 *stm32.SPI_Type
  3. }

SPI on the STM32.

func (SPI) Configure

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

Configure is intended to setup the STM32 SPI1 interface.Features still TODO:- support SPI2 and SPI3- allow setting data size to 16 bits?- allow setting direction in HW for additional optimization?- hardware SS pin?

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. Bus *stm32.USART_Type
  4. Interrupt interrupt.Interrupt
  5. }

UART

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