Friday, August 26, 2022

SPI slave as a Wishbone master for offloading Pi Zero Flight controller (BaseFlight)

(This is still work in progress but wanted to publish it and keep working on it)

 The Raspberry PI zero has a good price performance ratio, the 2W even better.  I have been working on porting BaseFlight (Flight controller software for STM32 micro-controller) to Linux running on the PI.  The code is working with a MPU9250.  

The hardware will be a PI Zero or a PI Zero 2W (Still need one),  SPI0.0 to the IMU, the SPI0.1 connected to the FPGA.   The FPGA will create the signals needed for OneShot, decode 6 channels of PWM  (I have an old Transmitter that supports 6 channels of PWM), and LED controller.  

Will be using the CS7Mod, its small, has 4 LEDs and 1 RGB.  LEDS will be used for status indication for BaseFlight.

So, what will be the communication protocol over SPI?  Simple Wishbone bus instructions, Read/Write using address and data.  

The WishBone master is interface to the SPI Slave, so the PI Zero will be the master.  Here is commands for a read and write

  • Command string
    • 1 byte command, either Read/Write
    • 4 byte address Highest address first  (Big Endian)
    • 2 byte length: first byte is the highest followed by the lowest
    • 4 bytes of data, first byte is the lowest byte.  (Little endian)
  • Read: example using command line: 
    • printf '\xA1\x1\x2\x3\x0\x0\x4\xf\x00\x00\x0\x0' | spi-pipe -m 0 -s 1000000 -d /dev/spidev0.1 | hexdump -C
  • Wite: 
    • printf '\xA2\x1\x2\x3\x0\x0\x4\xf\xff\xff\x11\0' | spi-pipe -m 0 -s 1000000 -d /dev/spidev0.1 | hexdump -C
The reason for using a Wishbone bus:
  1. Simple
  2. Use existing code for Wishbone Master 
  3. Easy to test
  4. Extendable (More on that later)

Right now, there are 3 wishbone peripherals
  1. LED controller
    1. address 0: set 32 bit register
    2. address 4: xor 32 bit register (blink)
    3. address 8: clear 32 bit register
  2. PWM decoder 
    1. 6 32 bit registers.  
  3. One Shot generator
    1. 4 32 bit registers
Here is screen shot of vivado, this is only for LED controller: