title: VHDL Attempting to implement an SPI interface
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is what I have at the moment.</p>
  
  <pre><code>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 =&gt; '0');
  
  -- SPI interface
  process(SPI_CLK)
  
  begin
  
      if rising_edge(SPI_CLK) then
  
          SPI_MISO &lt;= SPI_REG(7);
          SPI_REG &lt;= SPI_REG(6 downto 0) &amp; SPI_MOSI; -- Concatenate bits
  
      end if;
  
  end process;
  
  end Behavioral;
  </code></pre>
  
  <p>I also have a main (cpp) program running on a Raspberry Pi.</p>
  
  <p>It's pretty basic again, this is what it does.</p>
  
  <pre><code>#include &lt;wiringPiSPI.h&gt;
  #include &lt;iostream&gt;
  
  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
  }
  </code></pre>
  
  <p>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.)</p>
  
  <p>This is a typical input:</p>
  
  <p><code>0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...</code></p>
  
  <p>And here is the typical output:</p>
  
  <p><code>31 128 0 129 1 130 2 131 3 132 4 135 5 134 6  ...</code></p>
  
  <p>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.)</p>
  
  <p>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...</p>
  
  <p>I am unsure if this is because I have misunderstood the SPI hardware definition or whether my VHDL code is not correct.</p>