aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorsago35 <[email protected]>2021-03-15 18:36:19 +0900
committerRon Evans <[email protected]>2021-03-19 17:34:49 +0100
commit1571b8fd34041a24655919296e967837cf11d53e (patch)
treee357f1b1f7ef509d0760d93414077d2416371956
parenta41b72578b7414ce728c9613700b22d10bdcabba (diff)
downloadtinygo-1571b8fd34041a24655919296e967837cf11d53e.tar.gz
tinygo-1571b8fd34041a24655919296e967837cf11d53e.zip
Add special handling when SPI Freq is 24Mhz
-rw-r--r--src/machine/machine_atsamd21.go89
1 files changed, 77 insertions, 12 deletions
diff --git a/src/machine/machine_atsamd21.go b/src/machine/machine_atsamd21.go
index 1ab400909..8db404eff 100644
--- a/src/machine/machine_atsamd21.go
+++ b/src/machine/machine_atsamd21.go
@@ -8,6 +8,7 @@
package machine
import (
+ "device"
"device/arm"
"device/sam"
"errors"
@@ -1289,21 +1290,43 @@ var (
// spi.Tx(nil, rx)
//
func (spi SPI) Tx(w, r []byte) error {
- switch {
- case w == nil:
- // read only, so write zero and read a result.
- spi.rx(r)
- case r == nil:
- // write only
- spi.tx(w)
+ if spi.Bus.BAUD.Get() == 0x00 {
+ // When the SPI Freq is 24MHz, special processing is performed to improve the speed.
- default:
- // write/read
- if len(w) != len(r) {
- return ErrTxInvalidSliceSize
+ switch {
+ case w == nil:
+ // read only, so write zero and read a result.
+ spi.rx(r)
+ case r == nil:
+ // write only
+ spi.tx24mhz(w)
+
+ default:
+ // write/read
+ if len(w) != len(r) {
+ return ErrTxInvalidSliceSize
+ }
+
+ spi.txrx24mhz(w, r)
}
- spi.txrx(w, r)
+ } else {
+ switch {
+ case w == nil:
+ // read only, so write zero and read a result.
+ spi.rx(r)
+ case r == nil:
+ // write only
+ spi.tx(w)
+
+ default:
+ // write/read
+ if len(w) != len(r) {
+ return ErrTxInvalidSliceSize
+ }
+
+ spi.txrx(w, r)
+ }
}
return nil
@@ -1356,6 +1379,48 @@ func (spi SPI) txrx(tx, rx []byte) {
rx[len(rx)-1] = byte(spi.Bus.DATA.Get())
}
+// tx24mhz is a special tx/rx function for CPU Clock 48 Mhz and SPI Freq 24 Mhz
+func (spi SPI) tx24mhz(tx []byte) {
+ spi.Bus.DATA.Set(uint32(tx[0]))
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+
+ for i := 1; i < len(tx); i++ {
+ spi.Bus.DATA.Set(uint32(tx[i]))
+ device.Asm("nop")
+ device.Asm("nop")
+ spi.Bus.DATA.Get()
+ }
+ for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_RXC) {
+ }
+ spi.Bus.DATA.Get()
+}
+
+// txrx24mhz is a special tx/rx function for CPU Clock 48 Mhz and SPI Freq 24 Mhz
+func (spi SPI) txrx24mhz(tx, rx []byte) {
+ spi.Bus.DATA.Set(uint32(tx[0]))
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+ device.Asm("nop")
+
+ for i := 1; i < len(rx); i++ {
+ spi.Bus.DATA.Set(uint32(tx[i]))
+ device.Asm("nop")
+ device.Asm("nop")
+ rx[i-1] = byte(spi.Bus.DATA.Get())
+ }
+ for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_RXC) {
+ }
+ rx[len(rx)-1] = byte(spi.Bus.DATA.Get())
+}
+
// PWM
const period = 0xFFFF