Altera_Forum
Honored Contributor
11 years agoReading SPI data. The VHDL synchronize problem?
Hi, can anybody help me with this issue. I'm trying to read the SPI data from another device but it seems that I cannot make it synchronized. Briefly, I try to send a constant (16 bit), for instant and easy 1001101, but the signal sometimes is shifted one or two bit around (sometimes the output data is a range of 1001101, but after like 20ms, it's a range of 0011011, or 1100110, in other words, it's not stable).
That problem happens when I used this code: process(SPI_CS) begin if rising_edge(SPI_CS) then DATA_REG_O <= sdata_reg; end if; end process; process(SPI_SCLK) begin if rising_edge(SPI_SCLK) then sdata_reg <= sdata_reg(len_SPI-2 downto 0) & SPI_DIO; end if; end process; I can't think of any problem with the above code :( the simulation is perfect! Then I change to this code, which causes bad synchronization when the program start (as it may start in the middle of the 16-bit SPI clock). But the data is always stable (fixed at 11001100, or 1001101, in other words, I received the same signal although it's wrong, but it's always the same) process(SPI_SCLK) begin if rising_edge(SPI_SCLK) then if position_SPI = len_SPI-1 then position_SPI <= 0; DATA_REG_O <= sdata_reg; sdata_reg <= (others => '0'); else position_SPI <= position_SPI + 1; sdata_reg <= sdata_reg(len_SPI-2 downto 0) & SPI_DIO; end if; end if; end process; Please let me know if you don't understand what I'm trying to say. My English is not very good. Thanks Tan