Hi,
I am struggling making one hifive1 be the SPI master (MHF below), and the other one be the slave (SHF below).
After connecting both of them from pin 10 to 13 (SS, MOSI, MISO, SCK), I tried this SPI example. In the MHF part, no modification is required. The linking is automatically done by arduino IDE and the hifive1 package. In the SHF part, however, the scenario is very different from arduino family boards.
In practice, a SPI slave often works in a interrupt-driven model. This cause two difficulties when applying the example code to the SHF:
- SPI registers
Arduino uses things likeSPDR
(data reg.) andSPCR
(control reg.). I wonder if there was similar design in hifive1, so I dig into the related files but found no trivial mappings for this. Even if I try to replaceSPDR
bySPI_REG_TXFIFO/SPI_REG_RXFIFO
, I have no idea how to reassembleSPCR
andSPSR
from all theSPI_REG_XXX
s. - ISR design
The author useISR (SPI_STC_vect)
to declare the interrupt handler, and they don’t even need to doattachInterrupt()
insetup()
. But theISR
here is a AVR-specific macro. I did try to design a ISR for SHF, but the problem became: what interrupt number to attach?INT_SPI1_BASE
? but it did not seem to work either.
Therefore for SHF, I currently have no idea how to utilize the hardware support part, so I implemented software-emulated SPI slave. (But of course I want to know the native way to do SPI on hifive1 later )
There are two ISRs,
- on the
CHANGING
ofSS
After theFALLING
ofSS
, set anENABLE
flag, and disable it onceRISING
is observed. - on the
RISING
ofSCK
TheSPI.begin()
shows that hifive1 boards do SPI under mode 0. So whenSCK
rises andSS
is low (ENABLE
flag is set), one bit of the data should be transmitted inMOSI
. In theory I can read the bit and do the shift-and-accumulate on SHF.
I don’t know if I understood anything wrong, but surprisingly at least to me, this emulation did not work either. I always imagine the SPI communication process as the upper part of figure 76 in this website, so I expected when the MHF send a byte of data, there should be 8 rises on SCK
pin. However, the ISR on SCK
does not respond like that; rather, it activate only once as MHF do a SPI.transfer()
.
So I don’t know where to go from here now.
Quick summary: I am working on using hifive1 as an SPI slave. I try to find an easy mapping to work for an arduino example code but in vain. I then try to do it in a software-emulated way, but nothing works out either.
Sorry for the messy description above. If any of you need more information, please just ask me. Any help would be appreciated. Thanks!