I am attempting to implement an SPI interface. I have 2 questions about this, this is the first. (I decided to ask each question individually to simplify things.
Nothing seems to be working, so I have stripped my design right down to just a shift register, where the data which is clocked in should be returned to the SPI master device.
Here is what I have at the moment.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity interface is
port
(
-- SPI
SPI_MOSI: in std_logic;
SPI_MISO: out std_logic;
SPI_CLK: in std_logic;
);
end interface;
architecture Behavioral of interface is
-- SPI
signal SPI_REG: std_logic_vector(7 downto 0) := (others => '0');
-- SPI interface
process(SPI_CLK)
begin
if rising_edge(SPI_CLK) then
SPI_MISO <= SPI_REG(7);
SPI_REG <= SPI_REG(6 downto 0) & SPI_MOSI; -- Concatenate bits
end if;
end process;
end Behavioral;
I also have a main (cpp) program running on a Raspberry Pi.
It's pretty basic again, this is what it does.
#include <wiringPiSPI.h>
#include <iostream>
int main()
{
wiringPiSPISetup(0, 500000); // Device 0, slowest speed available
// Create array of 64 bytes (unsigned char)
// Print this array
wiringPiSPIDataRW(0, data, 64);
// Print array again and compare data by eye
}
Currently I have set up the array creation so that it contains the numbers 0 to 63 in sequential order. They are printed out, after each one is converted to an int. (I have omitted this code for simplicity.)
This is a typical input:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
And here is the typical output:
31 128 0 129 1 130 2 131 3 132 4 135 5 134 6 ...
So the first 2 bytes are junk. I would have expected 1 junk byte to be returned... One which was set to contain zero! (As in the VHDL code.)
Then following this, every other byte is junk, I have no clue why... And the actual data which is sent appears to be returned, although it comes back with junk bytes in between...
I am unsure if this is because I have misunderstood the SPI hardware definition or whether my VHDL code is not correct.