Hello! 

I was trying to make the RF522 (RFID reader) to initialize properly. 

After some research (involving Logic analyzer and hardware protocol dumps) 
I realized that either I do something wrong with SPI, or it fails in 
general somewhere at the interface/extra side.

I tried https://github.com/ecc1/spi and that worked, but still I wondwe 
what may go wrong.

Please take a look at the code dumps:

*Using ECC1 library:*

package main

import (
   "fmt"
   "log"
   "github.com/ecc1/spi"
)

func main() {

   spiDev, err := spi.Open("/dev/spidev0.0", 1000000, 0)

   spiDev.SetMode(0)
   spiDev.SetBitsPerWord(8)
   spiDev.SetLSBFirst(false)
   spiDev.SetMaxSpeed(1000000)

   if err != nil {
      log.Fatal(err)
   }

   writeSpiData := func(dataIn []byte) (err error) {
      err = spiDev.Transfer(dataIn)
      return
   }

   devWrite := func(address int, data byte) (err error) {
      newData := [2]byte{(byte(address) << 1) & 0x7E, data}
      fmt.Print("<< ", newData, " ")
      err = writeSpiData(newData[0:])
      fmt.Println(">>", newData)
      return
   }

   if err != nil {
      log.Fatal(err)
   }

   devWrite(0x01, 0x0F)

   if err != nil {
      log.Fatal(err)
   }

}


The corresponging protocol dump is:

<https://lh3.googleusercontent.com/-diHR8oVbhKs/WeQtn46DXBI/AAAAAAAAG_s/X1Ff22h7N3spLyM0MhfFyjOgIfpJEiJYQCLcBGAs/s1600/screenshot-go-ecc1.png>


>From the code dump, I sent *0x02 0x0f *and received *0x0f 0x0 *- which is 
correct, according to the specs. And the SPI analyzer was able to capture 
that.


*Now with golang extra spi driver*


package main

import (
   "fmt"
   "golang.org/x/exp/io/spi"
   "log"
)

func main() {

   spiDev, err := spi.Open(&spi.Devfs{
      Dev:      "/dev/spidev0.0",
      MaxSpeed: int64(1000000),
   })

   spiDev.SetMode(spi.Mode0)
   spiDev.SetBitOrder(spi.MSBFirst)
   spiDev.SetBitsPerWord(8)

   if err != nil {
      log.Fatal(err)
   }

   writeSpiData := func(dataIn []byte) (out []byte, err error) {
      out = make([]byte, len(dataIn))
      err = spiDev.Tx(dataIn, out)
      return
   }

   devWrite := func(address int, data byte) (err error) {
      newData := [2]byte{(byte(address) << 1) & 0x7E, data}
      readBuf, err := writeSpiData(newData[0:])
      fmt.Println(">>", newData, readBuf)
      return
   }

   if err != nil {
      log.Fatal(err)
   }

   devWrite(0x01, 0x0F)

   fmt.Println("Done")

}


The resulting protocol analysis is weird:

<https://lh3.googleusercontent.com/-qXpBwSqEiQI/WeQuUr_pBcI/AAAAAAAAG_0/FlYaJYo5gywep35Gra2_-9921RegDTA7gCLcBGAs/s1600/screenshot-go-x.png>


Now the question is - am I doing anything wrong with 
*golang.org/x/exp/io/spi*, or it is not working for me for another reason?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to