Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
ardnew
2021-08-02 13:37:21 -05:00
170 changed files with 1515 additions and 229 deletions
-2
View File
@@ -47,8 +47,6 @@ const (
LED = D6
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN Pin = PA24
-2
View File
@@ -35,8 +35,6 @@ const (
LED3 Pin = PB03 // RX LED
)
var Serial = USB
// ADC pins
const (
AREF Pin = PA03
-2
View File
@@ -74,5 +74,3 @@ const (
PB30 Pin = 62
PB31 Pin = 63
)
var Serial = USB
-2
View File
@@ -15,8 +15,6 @@ const (
BUTTON = PB31
)
var Serial = USB
const (
// https://ww1.microchip.com/downloads/en/DeviceDoc/70005321A.pdf
+1 -1
View File
@@ -17,7 +17,7 @@ const (
BUTTON = PA0
)
var Serial = UART1
var DefaultUART = UART1
// UART pins
const (
@@ -57,8 +57,6 @@ const (
UART_RX_PIN = P0_30 // PORTB
)
var Serial = USB
// I2C pins
const (
SDA_PIN = P0_05 // I2C0 external
-5
View File
@@ -104,11 +104,6 @@ const (
UART_TX_PIN = D1
)
// Serial is the USB device
var (
Serial = USB
)
// I2C pins
const (
SDA_PIN = D20 // I2C0 external
-2
View File
@@ -68,8 +68,6 @@ const (
ADC3 Pin = IO39
)
var Serial = UART0
// UART0 pins
const (
UART_TX_PIN = IO1
-2
View File
@@ -47,8 +47,6 @@ const (
WS2812 = D8
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PA24
-2
View File
@@ -40,8 +40,6 @@ const (
WS2812 = D8
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PA24
+105
View File
@@ -0,0 +1,105 @@
// +build feather_nrf52840_sense
package machine
const HasLowFrequencyCrystal = true
// GPIO Pins
const (
D0 = P0_25 // UART TX
D1 = P0_24 // UART RX
D2 = P0_10 // NFC2
D3 = P1_11
D4 = P1_10 // LED2
D5 = P1_08
D6 = P0_07
D7 = P1_02 // Button
D8 = P0_16 // NeoPixel
D9 = P0_26
D10 = P0_27
D11 = P0_06
D12 = P0_08
D13 = P1_09 // LED1
D14 = P0_04 // A0
D15 = P0_05 // A1
D16 = P0_30 // A2
D17 = P0_28 // A3
D18 = P0_02 // A4
D19 = P0_03 // A5
D20 = P0_29 // Battery
D21 = P0_31 // AREF
D22 = P0_12 // I2C SDA
D23 = P0_11 // I2C SCL
D24 = P0_15 // SPI MISO
D25 = P0_13 // SPI MOSI
D26 = P0_14 // SPI SCK
D27 = P0_19 // QSPI CLK
D28 = P0_20 // QSPI CS
D29 = P0_17 // QSPI Data 0
D30 = P0_22 // QSPI Data 1
D31 = P0_23 // QSPI Data 2
D32 = P0_21 // QSPI Data 3
D33 = P0_09 // NFC1 (test point on bottom of board)
)
// Analog Pins
const (
A0 = D14
A1 = D15
A2 = D16
A3 = D17
A4 = D18
A5 = D19
A6 = D20 // Battery
A7 = D21 // ARef
)
const (
LED = D13
LED1 = LED
LED2 = D4
NEOPIXEL = D8
WS2812 = D8
BUTTON = D7
QSPI_SCK = D27
QSPI_CS = D28
QSPI_DATA0 = D29
QSPI_DATA1 = D30
QSPI_DATA2 = D31
QSPI_DATA3 = D32
)
// UART0 pins (logical UART1)
const (
UART_RX_PIN = D1
UART_TX_PIN = D0
)
// I2C pins
const (
SDA_PIN = D22 // I2C0 external
SCL_PIN = D23 // I2C0 external
)
// SPI pins
const (
SPI0_SCK_PIN = D26 // SCK
SPI0_SDO_PIN = D25 // SDO
SPI0_SDI_PIN = D24 // SDI
)
// USB CDC identifiers
const (
usb_STRING_PRODUCT = "Feather nRF52840 Express"
usb_STRING_MANUFACTURER = "Adafruit Industries LLC"
)
var (
usb_VID uint16 = 0x239A
usb_PID uint16 = 0x8088
)
var (
DefaultUART = UART0
)
+6 -7
View File
@@ -72,13 +72,8 @@ const (
// UART0 pins (logical UART1)
const (
UART_RX_PIN = D0
UART_TX_PIN = D1
)
// Serial is the USB device
var (
Serial = USB
UART_RX_PIN = D1
UART_TX_PIN = D0
)
// I2C pins
@@ -104,3 +99,7 @@ var (
usb_VID uint16 = 0x239A
usb_PID uint16 = 0x802A
)
var (
DefaultUART = UART0
)
+1 -1
View File
@@ -141,7 +141,7 @@ var (
TxAltFuncSelector: AF7_USART1_2_3,
RxAltFuncSelector: AF7_USART1_2_3,
}
Serial = UART1
DefaultUART = UART1
)
func initUART() {
+17
View File
@@ -8,3 +8,20 @@ const (
// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)
// SPI default pins
const (
// Default Serial Clock Bus 0 for SPI communications
SPI0_SCK_PIN = GPIO18
// Default Serial Out Bus 0 for SPI communications
SPI0_SDO_PIN = GPIO19 // Tx
// Default Serial In Bus 0 for SPI communications
SPI0_SDI_PIN = GPIO20 // Rx
// Default Serial Clock Bus 1 for SPI communications
SPI1_SCK_PIN = GPIO10
// Default Serial Out Bus 1 for SPI communications
SPI1_SDO_PIN = GPIO11 // Tx
// Default Serial In Bus 1 for SPI communications
SPI1_SDI_PIN = GPIO12 // Rx
)
-2
View File
@@ -142,8 +142,6 @@ const (
WS2812 = NEOPIXEL_PIN
)
var Serial = USB
// UART pins
const (
UART1_RX_PIN = D0 // (PB25)
+1 -1
View File
@@ -35,7 +35,7 @@ const (
LED_BLUE = P21
)
var Serial = UART0
var DefaultUART = UART0
const (
// TODO: figure out the pin numbers for these.
-2
View File
@@ -37,8 +37,6 @@ const (
LED = D13
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PA24
-5
View File
@@ -70,11 +70,6 @@ const (
UART_TX_PIN = D1
)
// Serial is the USB device
var (
Serial = USB
)
// I2C pins
const (
SDA_PIN = D21 // I2C0 external
+1 -1
View File
@@ -54,7 +54,7 @@ const (
I2C0_SDA_PIN = PA10
)
var Serial = UART0
var DefaultUART = UART0
var (
+1 -1
View File
@@ -52,7 +52,7 @@ const (
LED_BLUE = D14
)
var Serial = UART0
var DefaultUART = UART0
// Default pins for UARTHS.
const (
-2
View File
@@ -41,8 +41,6 @@ const (
WS2812 = D40
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PA24
+1 -1
View File
@@ -12,7 +12,7 @@ const (
BUTTONB Pin = P11
)
var Serial = UART0
var DefaultUART = UART0
// UART pins
const (
+1 -1
View File
@@ -12,7 +12,7 @@ const (
BUTTONB Pin = 26
)
var Serial = UART0
var DefaultUART = UART0
// UART pins
const (
-5
View File
@@ -70,11 +70,6 @@ const (
UART_TX_PIN = P1_03
)
// Serial is the USB device
var (
Serial = USB
)
// I2C pins
const (
SDA_PIN = P0_31
+6 -1
View File
@@ -53,11 +53,16 @@ const (
SCL_PIN Pin = GPIO13
)
// SPI pins
// SPI pins. SPI1 not available on Nano RP2040 Connect.
const (
SPI0_SCK_PIN Pin = GPIO6
SPI0_SDO_PIN Pin = GPIO7
SPI0_SDI_PIN Pin = GPIO4
// GPIO22 does not have SPI functionality so we set it to avoid interfering with NINA.
SPI1_SCK_PIN Pin = GPIO22
SPI1_SDO_PIN Pin = GPIO22
SPI1_SDI_PIN Pin = GPIO22
)
// NINA-W102 Pins
-5
View File
@@ -54,11 +54,6 @@ const (
UART_TX_PIN = P0_08
)
// Serial is the USB device
var (
Serial = USB
)
// I2C pins
const (
SDA_PIN = P0_17 // I2C0 external
-2
View File
@@ -20,8 +20,6 @@ const (
// Onboard blue LED (on the AI-Thinker module).
const LED = D4
var Serial = UART0
// SPI pins
const (
SPI0_SCK_PIN = D5
@@ -23,9 +23,6 @@ const (
UART_RX_PIN Pin = NoPin
)
// Serial is the USB device
var Serial = USB
// I2C pins (unused)
const (
SDA_PIN = NoPin
-3
View File
@@ -18,9 +18,6 @@ const (
UART_RX_PIN Pin = 19
)
// Serial is the USB device
var Serial = USB
// I2C pins (unused)
const (
SDA_PIN = NoPin
+1 -1
View File
@@ -34,7 +34,7 @@ var (
Buffer: NewRingBuffer(),
Bus: stm32.USART2,
}
Serial = UART2
DefaultUART = UART2
)
func init() {
+1 -1
View File
@@ -38,7 +38,7 @@ var (
TxAltFuncSelector: UART_ALT_FN,
RxAltFuncSelector: UART_ALT_FN,
}
Serial = UART1
DefaultUART = UART1
)
func init() {
+1 -1
View File
@@ -76,7 +76,7 @@ var (
TxAltFuncSelector: 4,
RxAltFuncSelector: 4,
}
Serial = UART1
DefaultUART = UART1
// I2C1 is documented, alias to I2C0 as well
I2C1 = &I2C{
+1 -1
View File
@@ -78,7 +78,7 @@ var (
TxAltFuncSelector: 7,
RxAltFuncSelector: 3,
}
Serial = UART1
DefaultUART = UART1
// I2C1 is documented, alias to I2C0 as well
I2C1 = &I2C{
+1 -1
View File
@@ -38,7 +38,7 @@ var (
TxAltFuncSelector: UART_ALT_FN,
RxAltFuncSelector: UART_ALT_FN,
}
Serial = UART1
DefaultUART = UART1
)
const (
+1 -1
View File
@@ -41,7 +41,7 @@ const (
// UART
var (
Serial = UART0
DefaultUART = UART0
)
const (
+1 -1
View File
@@ -41,7 +41,7 @@ const (
// UART
var (
Serial = UART0
DefaultUART = UART0
)
const (
+1 -1
View File
@@ -41,7 +41,7 @@ const (
// UART
var (
Serial = UART0
DefaultUART = UART0
)
const (
+1 -1
View File
@@ -19,7 +19,7 @@ const (
LED_BLUE Pin = 23
)
var Serial = UART0
var DefaultUART = UART0
// UART pins
const (
+1 -1
View File
@@ -23,7 +23,7 @@ const (
BUTTON4 Pin = 16
)
var Serial = UART0
var DefaultUART = UART0
// UART pins for NRF52840-DK
const (
+1 -1
View File
@@ -22,7 +22,7 @@ const (
BUTTON4 Pin = 25
)
var Serial = UART0
var DefaultUART = UART0
// UART pins
const (
-5
View File
@@ -34,11 +34,6 @@ const (
UART_RX_PIN Pin = NoPin
)
// Serial is the USB device
var (
Serial = USB
)
// I2C pins (unused)
const (
SDA_PIN = NoPin
+17
View File
@@ -37,3 +37,20 @@ const (
// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)
// SPI default pins
const (
// Default Serial Clock Bus 0 for SPI communications
SPI0_SCK_PIN = GPIO18
// Default Serial Out Bus 0 for SPI communications
SPI0_SDO_PIN = GPIO19 // Tx
// Default Serial In Bus 0 for SPI communications
SPI0_SDI_PIN = GPIO16 // Rx
// Default Serial Clock Bus 1 for SPI communications
SPI1_SCK_PIN = GPIO10
// Default Serial Out Bus 1 for SPI communications
SPI1_SDO_PIN = GPIO11 // Tx
// Default Serial In Bus 1 for SPI communications
SPI1_SDI_PIN = GPIO12 // Rx
)
+1 -1
View File
@@ -17,7 +17,7 @@ const (
LED3 = LCD_BACKLIGHT_LOW
)
var Serial = UART0
var DefaultUART = UART0
// UART pins for PineTime. Note that RX is set to NoPin as RXD is not listed in
// the PineTime schematic 1.0:
-2
View File
@@ -67,8 +67,6 @@ const (
BUTTON_B_MASK = 128
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PA24
-2
View File
@@ -70,8 +70,6 @@ const (
BUTTON_B_MASK = 128
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PA24
-2
View File
@@ -95,8 +95,6 @@ const (
LED = D13
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PA24
+1 -1
View File
@@ -29,7 +29,7 @@ const (
BUTTON Pin = 7
)
var Serial = UART0
var DefaultUART = UART0
// UART pins
const (
+1 -1
View File
@@ -38,7 +38,7 @@ var (
TxAltFuncSelector: AF7_USART1_2_3,
RxAltFuncSelector: AF7_USART1_2_3,
}
Serial = UART1
DefaultUART = UART1
)
// set up RX IRQ handler. Follow similar pattern for other UARTx instances
+2
View File
@@ -87,6 +87,8 @@ var (
TeensyUART5 = UART4
)
var DefaultUART = UART0
const (
defaultUART0RX = D00
defaultUART0TX = D01
+3 -3
View File
@@ -136,9 +136,9 @@ const (
)
var (
Serial = UART1
UART1 = &_UART1
_UART1 = UART{
DefaultUART = UART1
UART1 = &_UART1
_UART1 = UART{
Bus: nxp.LPUART6,
Buffer: NewRingBuffer(),
txBuffer: NewRingBuffer(),
-2
View File
@@ -325,8 +325,6 @@ const (
OUTPUT_CTR_3V3 = PC15
)
var Serial = USB
// USBCDC pins
const (
USBCDC_DM_PIN = PIN_USB_DM
+1 -1
View File
@@ -27,4 +27,4 @@ const (
const HasLowFrequencyCrystal = true
var Serial = UART0
var DefaultUART = UART0
+1 -1
View File
@@ -122,7 +122,7 @@ func (i2c *I2C) readByte() byte {
}
// Always use UART0 as the serial output.
var Serial = UART0
var DefaultUART = UART0
// UART
var (
+2
View File
@@ -251,6 +251,8 @@ func (p Pin) mux() *volatile.Register32 {
}
}
var DefaultUART = UART0
var (
UART0 = &_UART0
_UART0 = UART{Bus: esp.UART0, Buffer: NewRingBuffer()}
+2
View File
@@ -139,6 +139,8 @@ func (p Pin) PortMaskClear() (*uint32, uint32) {
return &esp.GPIO.GPIO_OUT_W1TC.Reg, 1 << p
}
var DefaultUART = UART0
// UART0 is a hardware UART that supports both TX and RX.
var UART0 = &_UART0
var _UART0 = UART{Buffer: NewRingBuffer()}
+6 -6
View File
@@ -11,6 +11,12 @@ var (
USB = &UART{100}
)
// The Serial port always points to the default UART in a simulated environment.
//
// TODO: perhaps this should be a special serial object that outputs via WASI
// stdout calls.
var Serial = UART0
const (
PinInput PinMode = iota
PinOutput
@@ -118,12 +124,6 @@ type UART struct {
Bus uint8
}
type UARTConfig struct {
BaudRate uint32
TX Pin
RX Pin
}
// Configure the UART.
func (uart *UART) Configure(config UARTConfig) {
uartConfigure(uart.Bus, config.TX, config.RX)
+1 -1
View File
@@ -108,7 +108,7 @@ var (
}
)
var Serial = UART0
var DefaultUART = UART0
func init() {
UART0.Interrupt = interrupt.New(rp.IRQ_UART0_IRQ, _UART0.handleInterrupt)
+3
View File
@@ -68,6 +68,7 @@ const (
PinInputPullup
PinAnalog
PinUART
PinSPI
)
// set drives the pin high
@@ -155,6 +156,8 @@ func (p Pin) Configure(config PinConfig) {
p.pulloff()
case PinUART:
p.setFunc(fnUART)
case PinSPI:
p.setFunc(fnSPI)
}
}
+361
View File
@@ -0,0 +1,361 @@
// +build rp2040
package machine
import (
"device/rp"
"errors"
)
// SPI on the RP2040
var (
SPI0 = &_SPI0
_SPI0 = SPI{
Bus: rp.SPI0,
}
SPI1 = &_SPI1
_SPI1 = SPI{
Bus: rp.SPI1,
}
)
// SPIConfig is used to store config info for SPI.
type SPIConfig struct {
Frequency uint32
// LSB not supported on rp2040.
LSBFirst bool
// Mode's two most LSB are CPOL and CPHA. i.e. Mode==2 (0b10) is CPOL=1, CPHA=0
Mode uint8
// Number of data bits per transfer. Valid values 4..16. Default and recommended is 8.
DataBits uint8
// Serial clock pin
SCK Pin
// TX or Serial Data Out (MOSI if rp2040 is master)
SDO Pin
// RX or Serial Data In (MISO if rp2040 is master)
SDI Pin
}
var (
ErrLSBNotSupported = errors.New("SPI LSB unsupported on PL022")
ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
ErrSPITimeout = errors.New("SPI timeout")
ErrSPIBaud = errors.New("SPI baud too low or above 66.5Mhz")
)
type SPI struct {
Bus *rp.SPI0_Type
}
// time to wait on a transaction before dropping. Unit in Microseconds for compatibility with ticks().
const _SPITimeout = 10 * 1000 // 10 ms
// Tx handles read/write operation for SPI interface. Since SPI is a syncronous write/read
// interface, 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:
//
// spi.Tx(tx, rx)
//
// This form sends the tx buffer, ignoring the result. Useful for sending "commands" that return zeros
// until all the bytes in the command packet have been received:
//
// spi.Tx(tx, nil)
//
// This form sends zeros, putting the result into the rx buffer. Good for reading a "result packet":
//
// spi.Tx(nil, rx)
//
// Remark: This implementation (RP2040) allows reading into buffer with a custom repeated
// value on tx.
//
// spi.Tx([]byte{0xff}, rx) // may cause unwanted heap allocations.
//
// This form sends 0xff and puts the result into rx buffer. Useful for reading from SD cards
// which require 0xff input on SI.
func (spi SPI) Tx(w, r []byte) (err error) {
switch {
case w == nil:
// read only, so write zero and read a result.
err = spi.rx(r, 0)
case r == nil:
// write only
err = spi.tx(w)
case len(w) == 1 && len(r) > 1:
// Read with custom repeated value.
err = spi.rx(r, w[0])
default:
// write/read
err = spi.txrx(w, r)
}
return err
}
// Write a single byte and read a single byte from TX/RX FIFO.
func (spi SPI) Transfer(w byte) (byte, error) {
var deadline = ticks() + _SPITimeout
for !spi.isWritable() {
if ticks() > deadline {
return 0, ErrSPITimeout
}
}
spi.Bus.SSPDR.Set(uint32(w))
for !spi.isReadable() {
if ticks() > deadline {
return 0, ErrSPITimeout
}
}
return uint8(spi.Bus.SSPDR.Get()), nil
}
func (spi SPI) SetBaudRate(br uint32) error {
const freqin uint32 = 125 * MHz
const maxBaud uint32 = 66.5 * MHz // max output frequency is 66.5MHz on rp2040. see Note page 527.
// Find smallest prescale value which puts output frequency in range of
// post-divide. Prescale is an even number from 2 to 254 inclusive.
var prescale, postdiv uint32
for prescale = 2; prescale < 255; prescale += 2 {
if freqin < (prescale+2)*256*br {
break
}
}
if prescale > 254 || br > maxBaud {
return ErrSPIBaud
}
// Find largest post-divide which makes output <= baudrate. Post-divide is
// an integer in the range 1 to 256 inclusive.
for postdiv = 256; postdiv > 1; postdiv-- {
if freqin/(prescale*(postdiv-1)) > br {
break
}
}
spi.Bus.SSPCPSR.Set(prescale)
spi.Bus.SSPCR0.ReplaceBits((postdiv-1)<<rp.SPI0_SSPCR0_SCR_Pos, rp.SPI0_SSPCR0_SCR_Msk, 0)
return nil
}
func (spi SPI) GetBaudRate() uint32 {
const freqin uint32 = 125 * MHz
prescale := spi.Bus.SSPCPSR.Get()
postdiv := ((spi.Bus.SSPCR0.Get() & rp.SPI0_SSPCR0_SCR_Msk) >> rp.SPI0_SSPCR0_SCR_Pos) + 1
return freqin / (prescale * postdiv)
}
// Configure is intended to setup/initialize the SPI interface.
// Default baudrate of 115200 is used if Frequency == 0. Default
// word length (data bits) is 8.
// Below is a list of GPIO pins corresponding to SPI0 bus on the rp2040:
// SI : 0, 4, 17 a.k.a RX and MISO (if rp2040 is master)
// SO : 3, 7, 19 a.k.a TX and MOSI (if rp2040 is master)
// SCK: 2, 6, 18
// SPI1 bus GPIO pins:
// SI : 8, 12
// SO : 11, 15
// SCK: 10, 14
// No pin configuration is needed of SCK, SDO and SDI needed after calling Configure.
func (spi SPI) Configure(config SPIConfig) error {
const defaultBaud uint32 = 115200
if config.SCK == 0 {
// set default pins if config zero valued or invalid clock pin supplied.
switch spi.Bus {
case rp.SPI0:
config.SCK = SPI0_SCK_PIN
config.SDO = SPI0_SDO_PIN
config.SDI = SPI0_SDI_PIN
case rp.SPI1:
config.SCK = SPI1_SCK_PIN
config.SDO = SPI1_SDO_PIN
config.SDI = SPI1_SDI_PIN
}
}
if config.DataBits < 4 || config.DataBits > 16 {
config.DataBits = 8
}
if config.Frequency == 0 {
config.Frequency = defaultBaud
}
// SPI pin configuration
config.SCK.setFunc(fnSPI)
config.SDO.setFunc(fnSPI)
config.SDI.setFunc(fnSPI)
return spi.initSPI(config)
}
func (spi SPI) initSPI(config SPIConfig) (err error) {
spi.reset()
// LSB-first not supported on PL022:
if config.LSBFirst {
return ErrLSBNotSupported
}
err = spi.SetBaudRate(config.Frequency)
// Set SPI Format (CPHA and CPOL) and frame format (default is Motorola)
spi.setFormat(config.DataBits, config.Mode, rp.XIP_SSI_CTRLR0_SPI_FRF_STD)
// Always enable DREQ signals -- harmless if DMA is not listening
spi.Bus.SSPDMACR.SetBits(rp.SPI0_SSPDMACR_TXDMAE | rp.SPI0_SSPDMACR_RXDMAE)
// Finally enable the SPI
spi.Bus.SSPCR1.SetBits(rp.SPI0_SSPCR1_SSE)
return err
}
//go:inline
func (spi SPI) setFormat(databits, mode uint8, frameFormat uint32) {
cpha := uint32(mode) & 1
cpol := uint32(mode>>1) & 1
spi.Bus.SSPCR0.ReplaceBits(
(cpha<<rp.SPI0_SSPCR0_SPH_Pos)|
(cpol<<rp.SPI0_SSPCR0_SPO_Pos)|
(uint32(databits-1)<<rp.SPI0_SSPCR0_DSS_Pos)| // Set databits (SPI word length). Valid inputs are 4-16.
(frameFormat&0b11)<<rp.SPI0_SSPCR0_FRF_Pos, // Frame format bits 4:5
rp.SPI0_SSPCR0_SPH_Msk|rp.SPI0_SSPCR0_SPO_Msk|rp.SPI0_SSPCR0_DSS_Msk|rp.SPI0_SSPCR0_FRF_Msk, 0)
}
// reset resets SPI and waits until reset is done.
//go:inline
func (spi SPI) reset() {
resetVal := spi.deinit()
rp.RESETS.RESET.ClearBits(resetVal)
// Wait until reset is done.
for !rp.RESETS.RESET_DONE.HasBits(resetVal) {
}
}
//go:inline
func (spi SPI) deinit() (resetVal uint32) {
switch spi.Bus {
case rp.SPI0:
resetVal = rp.RESETS_RESET_SPI0
case rp.SPI1:
resetVal = rp.RESETS_RESET_SPI1
}
// Perform SPI reset.
rp.RESETS.RESET.SetBits(resetVal)
return resetVal
}
// isWritable returns false if no space is available to write. True if a write is possible
//go:inline
func (spi SPI) isWritable() bool {
return spi.Bus.SSPSR.HasBits(rp.SPI0_SSPSR_TNF)
}
// isReadable returns true if a read is possible i.e. data is present
//go:inline
func (spi SPI) isReadable() bool {
return spi.Bus.SSPSR.HasBits(rp.SPI0_SSPSR_RNE)
}
// PrintRegs prints SPI's peripheral common registries current values
func (spi SPI) PrintRegs() {
cr0 := spi.Bus.SSPCR0.Get()
cr1 := spi.Bus.SSPCR1.Get()
dmacr := spi.Bus.SSPDMACR.Get()
cpsr := spi.Bus.SSPCPSR.Get()
dr := spi.Bus.SSPDR.Get()
ris := spi.Bus.SSPRIS.Get()
println("CR0:", cr0)
println("CR1:", cr1)
println("DMACR:", dmacr)
println("CPSR:", cpsr)
println("DR:", dr)
println("RIS:", ris)
}
//go:inline
func (spi SPI) isBusy() bool {
return spi.Bus.SSPSR.HasBits(rp.SPI0_SSPSR_BSY)
}
// tx writes buffer to SPI ignoring Rx.
func (spi SPI) tx(tx []byte) error {
var deadline = ticks() + _SPITimeout
// Write to TX FIFO whilst ignoring RX, then clean up afterward. When RX
// is full, PL022 inhibits RX pushes, and sets a sticky flag on
// push-on-full, but continues shifting. Safe if SSPIMSC_RORIM is not set.
for i := range tx {
for !spi.isWritable() {
if ticks() > deadline {
return ErrSPITimeout
}
}
spi.Bus.SSPDR.Set(uint32(tx[i]))
}
// Drain RX FIFO, then wait for shifting to finish (which may be *after*
// TX FIFO drains), then drain RX FIFO again
for spi.isReadable() {
spi.Bus.SSPDR.Get()
}
for spi.isBusy() {
if ticks() > deadline {
return ErrSPITimeout
}
}
for spi.isReadable() {
spi.Bus.SSPDR.Get()
}
// Don't leave overrun flag set
spi.Bus.SSPICR.Set(rp.SPI0_SSPICR_RORIC)
return nil
}
// rx reads buffer to SPI ignoring x.
// txrepeat is output repeatedly on SO as data is read in from SI.
// Generally this can be 0, but some devices require a specific value here,
// e.g. SD cards expect 0xff
func (spi SPI) rx(rx []byte, txrepeat byte) error {
var deadline = ticks() + _SPITimeout
plen := len(rx)
const fifoDepth = 8 // see txrx
var rxleft, txleft = plen, plen
for txleft != 0 || rxleft != 0 {
if txleft != 0 && spi.isWritable() && rxleft < txleft+fifoDepth {
spi.Bus.SSPDR.Set(uint32(txrepeat))
txleft--
}
if rxleft != 0 && spi.isReadable() {
rx[plen-rxleft] = uint8(spi.Bus.SSPDR.Get())
rxleft--
continue // if reading succesfully in rx there is no need to check deadline.
}
if ticks() > deadline {
return ErrSPITimeout
}
}
return nil
}
// Write len bytes from src to SPI. Simultaneously read len bytes from SPI to dst.
// Note this function is guaranteed to exit in a known amount of time (bits sent * time per bit)
func (spi SPI) txrx(tx, rx []byte) error {
var deadline = ticks() + _SPITimeout
plen := len(tx)
if plen != len(rx) {
return ErrTxInvalidSliceSize
}
// Never have more transfers in flight than will fit into the RX FIFO,
// else FIFO will overflow if this code is heavily interrupted.
const fifoDepth = 8
var rxleft, txleft = plen, plen
for (txleft != 0 || rxleft != 0) && ticks() <= deadline {
if txleft != 0 && spi.isWritable() && rxleft < txleft+fifoDepth {
spi.Bus.SSPDR.Set(uint32(tx[plen-txleft]))
txleft--
}
if rxleft != 0 && spi.isReadable() {
rx[plen-rxleft] = uint8(spi.Bus.SSPDR.Get())
rxleft--
}
}
if txleft != 0 || rxleft != 0 {
// Transaction ended early due to timeout
return ErrSPITimeout
}
return nil
}
+6
View File
@@ -0,0 +1,6 @@
// +build baremetal,serial.none
package machine
// Serial is a null device: writes to it are ignored.
var Serial = NullSerial{}
+6
View File
@@ -0,0 +1,6 @@
// +build baremetal,serial.uart
package machine
// Serial is implemented via the default (usually the first) UART on the chip.
var Serial = DefaultUART
+6
View File
@@ -0,0 +1,6 @@
// +build baremetal,serial.usb
package machine
// Serial is implemented via USB (USB-CDC).
var Serial = USB
+46
View File
@@ -0,0 +1,46 @@
package machine
import "errors"
var errNoByte = errors.New("machine: no byte read")
// UARTConfig is a struct with which a UART (or similar object) can be
// configured. The baud rate is usually respected, but TX and RX may be ignored
// depending on the chip and the type of object.
type UARTConfig struct {
BaudRate uint32
TX Pin
RX Pin
}
// NullSerial is a serial version of /dev/null (or null router): it drops
// everything that is written to it.
type NullSerial struct {
}
// Configure does nothing: the null serial has no configuration.
func (ns NullSerial) Configure(config UARTConfig) error {
return nil
}
// WriteByte is a no-op: the null serial doesn't write bytes.
func (ns NullSerial) WriteByte(b byte) error {
return nil
}
// ReadByte always returns an error because there aren't any bytes to read.
func (ns NullSerial) ReadByte() (byte, error) {
return 0, errNoByte
}
// Buffered returns how many bytes are buffered in the UART. It always returns 0
// as there are no bytes to read.
func (ns NullSerial) Buffered() int {
return 0
}
// Write is a no-op: none of the data is being written and it will not return an
// error.
func (ns NullSerial) Write(p []byte) (n int, err error) {
return len(p), nil
}
-6
View File
@@ -23,12 +23,6 @@ const (
ParityOdd UARTParity = 2
)
type UARTConfig struct {
BaudRate uint32
TX Pin
RX Pin
}
// To implement the UART interface for a board, you must declare a concrete type as follows:
//
// type UART struct {