SPI Hifive 1 Rev B


(DARKNEMESIS) #1

I am working with the Hifive 1 rev B, the main aim is to communicate with the ESP32 Chip on the Board using SPI. The example code that is available with the Freedom e SDK, is not working for me. I tried to debug the code and it seems stuck in a loop. How do i setup a working demo of SPI on Hifive 1B?
I just started working with the Borad, this is completely new for me. so please advice.


(Krists) #2

I am also experiencing this issue. Program hangs on “metal_spi_transfer(spi, &config, 3, tx_buf, rx_buf);”.

If someone reading this is working in Sifive, please hire a human to maintain and make sample programs. The included samples have problems. At least finaly thanks for led blinking demo fix. And please, restore Arduino IDE support or at least software support like in Hifive1. My Hifive revb board is currently an expensive paper weight. I can’t do anything meaningful without software samples and libraries.


#3

I’m not from SiFive, but in Rust community we do have working examples for this board: https://github.com/riscv-rust/riscv-rust-quickstart/tree/master/examples
Everyone is welcome to try this out :slight_smile:
If you are new to Rust, you can start here or here.


(Penguin) #4

I barely got any of the sifive examples to work. I use the rust stuff because it actually works and its pretty easy to get started.


(Akshai M) #5

Can’t believe one has to learn a new programming language altogether to use a Microcontroller Board !!!


#6

You do not have to. Rust is not forced or even officially supported by SiFive, it’s just a nice alternative.


#7

Hi I am also facing the same problem. Please provide some help.


#8

Hi, I’m also trying to figure things out and can only find these threads. One step I figured out is that the default freedom-e-sdk repo clone ends up in an ‘old’ branch from May. In this branch only SPI(0) is available! You need to go to the bleeding edge such that you can spi = metal_spi_get_device(1);
From there I haven’t been able to get a sensible response yet from the ESP32, but I believe you need to send the characters ‘AT\r\n’ as an initial message.


#9

I don’t think it’s mandatory. At the same time, I had some observations of WiFi module behavior and noticed that two things are mandatory:

  1. CS to packet time should be small. This means that it’s better to use hardware-controlled CS)
  2. Delay between bytes in packet should be avoided. This means that you should prepare your packet in advance and use SPI FIFO to prevent underflows.

Also remember that SPI communication with ESP32 uses its own protocol (not just sending character bytes back and force).


#10

Interesting indeed. Have you found the actual protocol description anywhere? I’ve tried to read the ESP slave driver code but I haven’t been able to figure it out. Somewhere I saw some code where first a send or revc byte is sent, then the size, and then the message. But that didn’t work for me.


#11

I haven’t seen any protocol description, so I had to reverse-engineer it. In this protocol each send or recv is a 3-packet sequence (“I want to send/recv”, “data length is y”, “actual data”). ESP32 also uses “handshake” pin to signal its readiness.


#12

Thanks, did you figure out in more detail? Are they first two packets indeed 4 bytes? And are they LSB?

I set gpio 10 to input (I believe that is the handshake pin), but I also believe pgio 9 is a chip enable pin? Do I need to turn that on as well?

Btw, I saw in the slave driver somewhere that only 512 byte packages were possible, which might result in the small packets you observed.


#13

I used a logic analyzer to analyze packet exchange during the boot stage (bootloader talks to ESP32 4 times or so). First two packets contain exactly 4 bytes. First packet is 01 00 00 00 (for recv) or 02 00 00 00 (for send). The second packet contains data size to transfer (in little endian): XX 00 00 41 for send and XX 00 00 42 for recv. The third packet contains actual bytes and nothing else.
Note that SPI performs data transfer in both directions at the same time, so, for example, if you want to receive data size packet from ESP32, you also have to send 4 bytes of garbage. The same for the last packet.


#14

Thanks so much for the info. I’m trying to apply it but still not sure about some things. In the read response, who writes the values? Is the slave supposed to reply with 02 … etc? I tried to use this approach but no success yet: https://gist.github.com/marijnfs/61af97c2ecdd9b1076486bcaa513051a


#15

I believe this printf can break timings, which are important here:

  1. for reading data you should send 01 00 00 00 command, not 02 00 00 00
  2. In the second packet you will receive the length of the data available. You should use it for the third packet (send N zero bytes, receive N data bytes).

#16

Thanks, good idea. However removing printf doesn’t seem to help. There seems to be some more disccusion here: HiFive1 Rev. B - How to use Wifi? How to get/set CPU clock speed?


#17

So this handshake protocol carries the AT command strings as its payload? I suppose that it is not guaranteed that each data packet contains exactly one AT commnd…