Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.42 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.NUMERIC_STD.ALL;
  4. USE work.all;
  5.  
  6. entity FlashWriter is
  7.     generic(
  8.         clock_frequency: natural);
  9.     port(
  10.         -- I2C signals
  11.         data_out            : out std_logic_vector(7 downto 0);
  12.         data_in             : in std_logic_vector(7 downto 0);
  13.         stop_detected       : in boolean;
  14.         transfer_started    : in boolean;
  15.         data_out_requested  : in boolean;
  16.         data_in_valid       : in boolean;
  17.  
  18.         -- other signals
  19.         clock               : in std_logic;
  20.         reset               : in std_logic;
  21.         asdo_in             : out std_logic;
  22.         dclk_in             : out std_logic;
  23.         ncso_in             : out std_logic;
  24.         data0_out           : in std_logic);
  25. end entity FlashWriter;
  26.  
  27. architecture rtl of FlashWriter is
  28.  
  29.     -- use 5 MHz for SPI clock (max. allowed: 20 MHz)
  30.     constant SPI_DELAY: natural := clock_frequency / 5e6;
  31.     signal spiDelay: natural range 0 to SPI_DELAY := 0;
  32.     signal spiBitCounter: natural range 0 to 7 := 0;
  33.     signal lastCommandWasRead: boolean := false;
  34.     signal firstByte: boolean := false;
  35.     signal triggerWrite: boolean := false;
  36.     signal triggerRead: boolean := false;
  37.     signal lastReadSpiByte: std_logic_vector(7 downto 0);
  38.     signal spiByteToWrite: std_logic_vector(7 downto 0);
  39.     signal spiShiftRegister: std_logic_vector(7 downto 0);
  40.  
  41.     type stateType is (
  42.         idle,
  43.         waitForReadWrite,
  44.         waitForNext);
  45.     signal state: stateType := idle;
  46.  
  47.     type spiStateType is (
  48.         spiIdle,
  49.         initialDelay,
  50.         setBit,
  51.         clearBit,
  52.         copyByte);
  53.     signal spiState: spiStateType := spiIdle;
  54.  
  55. begin
  56.  
  57.     i2cProcess: process(clock, reset)
  58.     begin
  59.         if reset = '1' then
  60.             state <= idle;
  61.         else
  62.             if rising_edge(clock) then
  63.                 -- I2C send/receive
  64.                 triggerWrite <= false;
  65.                 triggerRead <= false;
  66.                 case state is
  67.                     when idle =>
  68.                         firstByte <= true;
  69.                         lastCommandWasRead <= false;
  70.                         if transfer_started then
  71.                             state <= waitForReadWrite;
  72.                         end if;
  73.                     when waitForReadWrite =>
  74.                         if data_in_valid then
  75.                             -- write command from host, select flash
  76.                             ncso_in <= '0';
  77.                             spiByteToWrite <= data_in;
  78.                             triggerWrite <= true;
  79.                             state <= waitForNext;
  80.  
  81.                             -- test for 0 for first byte
  82.                             if firstByte then
  83.                                 if data_in = x"00" then
  84.                                     -- deselect flash without read
  85.                                     ncso_in <= '1';
  86.                                     triggerWrite <= false;
  87.                                     state <= idle;
  88.                                 end if;
  89.                             end if;
  90.                             firstByte <= false;
  91.                         end if;
  92.                         if data_out_requested then
  93.                             lastCommandWasRead <= true;
  94.                             data_out <= lastReadSpiByte;
  95.                             triggerRead <= true;
  96.                             state <= waitForNext;
  97.                         end if;
  98.                     when waitForNext =>
  99.                         if not data_in_valid and not data_out_requested then
  100.                             state <= waitForReadWrite;
  101.                         end if;
  102.                     when others =>
  103.                         state <= idle;
  104.                 end case;
  105.  
  106.                 if stop_detected then
  107.                     if lastCommandWasRead then
  108.                         -- read end from host, deselect flash
  109.                         lastCommandWasRead <= false;
  110.                         ncso_in <= '1';
  111.                     end if;
  112.                     state <= idle;
  113.                 end if;
  114.             end if;
  115.         end if;
  116.     end process;
  117.  
  118.     spiProcess: process(clock, reset)
  119.     begin
  120.         if reset = '1' then
  121.             spiState <= spiIdle;
  122.         else
  123.             if rising_edge(clock) then
  124.                 case spiState is
  125.                     when spiIdle =>
  126.                         spiBitCounter <= 7;
  127.                         spiDelay <= SPI_DELAY;
  128.                         if triggerWrite then
  129.                             spiShiftRegister <= spiByteToWrite;
  130.                             spiState <= initialDelay;
  131.                         end if;
  132.                         if triggerRead then
  133.                             spiShiftRegister <= x"00";
  134.                             spiState <= initialDelay;
  135.                         end if;
  136.                     when initialDelay =>
  137.                         if spiDelay = 0 then
  138.                             spiDelay <= SPI_DELAY;
  139.                             spiState <= setBit;
  140.                         else
  141.                             spiDelay <= spiDelay - 1;
  142.                         end if;
  143.                     when setBit =>
  144.                         if spiDelay = 0 then
  145.                             asdo_in <= spiShiftRegister(7);
  146.                             dclk_in <= '0';
  147.                             spiDelay <= SPI_DELAY;
  148.                             spiState <= clearBit;
  149.                         else
  150.                             spiDelay <= spiDelay - 1;
  151.                         end if;
  152.                     when clearBit =>
  153.                         if spiDelay = 0 then
  154.                             dclk_in <= '1';
  155.                             spiDelay <= SPI_DELAY;
  156.                             spiShiftRegister <= spiShiftRegister(6 downto 0) & data0_out;
  157.                             if spiBitCounter = 0 then
  158.                                 spiState <= copyByte;
  159.                             else
  160.                                 spiBitCounter <= spiBitCounter - 1;
  161.                                 spiState <= setBit;
  162.                             end if;
  163.                         else
  164.                             spiDelay <= spiDelay - 1;
  165.                         end if;
  166.                     when copyByte =>
  167.                         lastReadSpiByte <= spiShiftRegister;
  168.                         spiState <= spiIdle;
  169.                     when others =>
  170.                         spiState <= spiIdle;
  171.                 end case;
  172.             end if;
  173.         end if;
  174.     end process;
  175.  
  176. end architecture rtl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement