Beginning FPGA Programming - Partie67
Beginning FPGA Programming - Partie67
328
Chapter 14 ■ How Fast Can You Run? Ask the Accelerometer!
-----------------------------------------------------------------------------
-- Data sending out to the MOSI is using shift register
-----------------------------------------------------------------------------
data_out_p : process (clk, reset_n)
begin
if (reset_n = '0') then
reg_shift_out <= (others => '0');
elsif(rising_edge(clk)) then
if(ena = '1') then
reg_shift_out <= '0'& write_byte;
else
if(running = '1' and count = 0) then
reg_shift_out <= reg_shift_out(7 downto 0)& '0';
end if;
end if;
end if;
end process;
-------------------------------------------------------------------------------
-- Data receiving from SPI slave is shifted into shift register
-------------------------------------------------------------------------------
data_in_p : process (clk, reset_n)
begin
if (reset_n = '0') then
data_clk_dly <= '0';
reg_shift_in <= (others => '0');
elsif(rising_edge(clk)) then
data_clk_dly <= data_clk;
if(ena = '1') then
reg_shift_in <= (others => '0');
else
if(running = '1' and data_clk_dly = '0' and data_clk = '1') then
-- only shift when SPI clock is rising edge
reg_shift_in <= reg_shift_in(6 downto 0)& spi_miso;
end if;
end if;
end if;
end process;
end rtl;
329
Chapter 14 ■ How Fast Can You Run? Ask the Accelerometer!
14.3.3.2 uartTOi2cspi.vhd code
This module is copied from Chapter 13's uartTOi2c.vhd file with modifications to support the SPI master. It
added some commands and status registers which are described in the section “Add New Command and Status
Registers.” This new updated uartTOi2cspi code in Listing 14-2 also includes the new spi_master.vhd file
entity uartTOi2cspi is
port(
clk_uart_29MHz_i : in std_logic;
uart_rst_i : in std_logic;
uart_leds_o : out std_logic_vector(7 downto 0);
clk_uart_monitor_o : out std_logic;
------------UART TX & RX---------------------------
uart_dout_o : out std_logic;
uart_din_i : in std_logic;
-----------I2C interface---------------------------
i2c_scl : inout std_logic; -- serial clock
i2c_dat : inout std_logic; -- serial data
-----------SPI Master interface--------------------
spi_sclk : out std_logic;
spi_csn : out std_logic;
spi_mosi : out std_logic;
spi_miso : in std_logic;
spi_int1 : in std_logic;
spi_int2 : in std_logic
);
end uartTOi2cspi;
330
Chapter 14 ■ How Fast Can You Run? Ask the Accelerometer!
----------------------------------
-- signals for SPI Master block --
----------------------------------
signal spi_done : std_logic;
signal spi_done_dly : std_logic;
signal spi_firstbyte : std_logic;
signal spi_read : std_logic_vector(7 downto 0);
--------------------------
-- State Machine states --
--------------------------
type t_tx_reg_map is (IDLE, WAIT_A_BYTE, LATCH, TRANSMIT);
signal s_tx_fsm : t_tx_reg_map;
begin
331
Chapter 14 ■ How Fast Can You Run? Ask the Accelerometer!
332