Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 8.41 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:             Tek15 Solutions
  3. -- Engineer:            Matt Lewis
  4. --
  5. -- Create Date:     14:22:35 01/20/2018
  6. -- Design Name:         V2FPGA Serial Module
  7. -- Module Name:     Serial - Behavioral
  8. -- Project Name:        Vermonster DRSSTC Controller
  9. -- Target Devices:  -undetermined-
  10. -- Description:         FPGA-based dual-resonant solid-state Tesla coil
  11. --                          controller designed for the Vermonster DRSSTC
  12. -- Revision:            2
  13. ----------------------------------------------------------------------------------
  14. library IEEE;
  15. use IEEE.STD_LOGIC_1164.ALL;
  16. use IEEE.NUMERIC_STD.ALL;
  17.  
  18. entity Serial is
  19.     port (
  20.         CLK_50MHz: IN STD_LOGIC;
  21.         RX: IN STD_LOGIC;
  22.         TX: OUT STD_LOGIC
  23.     );
  24. end Serial;
  25.  
  26. architecture Behavioral of Serial is
  27.  
  28. begin
  29.  
  30. -- 9600 baud = 9600 bits/sec
  31. -- 1 bit time = 1000ms/9600 = 104.2us
  32. -- 1/2 bit time = 52.1us
  33. -- 1 tick = 1/50000000Hz = 20ns
  34. -- Ticks per bit: 104.2us/20ns = 5210 ticks
  35. -- Ticks per half bit: 52.1us/20ns = 2605 ticks
  36.  
  37. -- Data Stream:
  38. -- BYTE0
  39. -- |BIT7|BIT6|  BIT5|   BIT4|   BIT3|   BIT2|   BIT1|   BIT0|
  40. -- |STRT|R/W ||_____________FEATURE______________|
  41. -- BYTE1
  42. -- |BIT7|BIT5| BIT5| BIT4|  BIT3|   BIT2|   BIT1|   BIT0|
  43. --  |________________VALUE (WRITE)________________|
  44.  
  45. RxBytes: process(CLK_50MHz)
  46.  
  47.     variable RxByte0: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');  -- Received byte 0
  48.     variable RxByte1: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');  --  Received byte 1
  49.     variable RxBegun: STD_LOGIC := '0';                 --  Receive sequence started
  50.     variable RxFinished: STD_LOGIC := '1';              --  Receive sequence finished
  51.     variable CurrentByte: INTEGER RANGE 0 TO 1;             --  Current byte being received
  52.     variable Counter: INTEGER RANGE 0 TO 7815;      --  Counter for baud generator (1.5 bits max @ 50MHz)
  53.     variable BitCount: INTEGER RANGE 0 TO 128;      --  Counter for number of bits received. Should not exceed 16 per cmd string
  54.    
  55. begin
  56.     if (rising_edge(CLK_50MHz)) then                        -- If CLK_50MHz is rising
  57.         if (RX = '0') then                                  --  If RX input is low (start bit detected)
  58.             if (RxBegun = '0') then                         --          If receive has not yet begun
  59.                 RxBegun := '1';                             --              Begin receive
  60.                 RxFinished := '0';                          --              Receive not yet finished
  61.                
  62.                 -- Byte0 --
  63.                
  64.                 BitCount := BitCount + 1;                   --              Increment number of bits received
  65.                 RxByte0(7) := '0';                          --              Load '0' into Byte0 BIT7 (start bit)
  66.                 if (Counter < 7815) then                    --              Count 1.5 bit times to sample center of BIT6
  67.                     Counter := Counter + 1;
  68.                 else                                                --              If ready for BIT6
  69.                     Counter := 0;                               --                  Reset time counter
  70.                     RxByte0(6) := RX;                           --                  Load value on RX pin into Byte0 BIT6 (R/W)
  71.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  72.                 end if;
  73.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT5
  74.                     Counter := Counter + 1;
  75.                 else                                                --              If ready for BIT5
  76.                     Counter := 0;                               --                  Reset time counter
  77.                     RxByte0(5) := RX;                           --                  Load value on RX pin into Byte0 BIT5
  78.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  79.                 end if;
  80.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT4
  81.                     Counter := Counter + 1;
  82.                 else                                                --              If ready for BIT4
  83.                     Counter := 0;                               --                  Reset time counter
  84.                     RxByte0(4) := RX;                           --                  Load value on RX pin into Byte0 BIT4
  85.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  86.                 end if;
  87.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT3
  88.                     Counter := Counter + 1;
  89.                 else                                                --              If ready for BIT3
  90.                     Counter := 0;                               --                  Reset time counter
  91.                     RxByte0(3) := RX;                           --                  Load value on RX pin into Byte0 BIT3
  92.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  93.                 end if;
  94.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT2
  95.                     Counter := Counter + 1;
  96.                 else                                                --              If ready for BIT2
  97.                     Counter := 0;                               --                  Reset time counter
  98.                     RxByte0(2) := RX;                           --                  Load value on RX pin into Byte0 BIT2
  99.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  100.                 end if;
  101.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT1
  102.                     Counter := Counter + 1;
  103.                 else                                                --              If ready for BIT1
  104.                     Counter := 0;                               --                  Reset time counter
  105.                     RxByte0(1) := RX;                           --                  Load value on RX pin into Byte0 BIT1
  106.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  107.                 end if;
  108.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT0
  109.                     Counter := Counter + 1;
  110.                 else                                                --              If ready for BIT0
  111.                     Counter := 0;                               --                  Reset time counter
  112.                     RxByte0(0) := RX;                           --                  Load value on RX pin into Byte0 BIT0
  113.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  114.                 end if;
  115.                
  116.                 -- Byte1 --
  117.                
  118.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT7
  119.                     Counter := Counter + 1;
  120.                 else                                                --              If ready for BIT7
  121.                     Counter := 0;                               --                  Reset time counter
  122.                     RxByte1(7) := RX;                           --                  Load value on RX pin into Byte1 BIT7
  123.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  124.                 end if;
  125.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT6
  126.                     Counter := Counter + 1;
  127.                 else                                                --              If ready for BIT6
  128.                     Counter := 0;                               --                  Reset time counter
  129.                     RxByte1(6) := RX;                           --                  Load value on RX pin into Byte1 BIT6
  130.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  131.                 end if;
  132.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT5
  133.                     Counter := Counter + 1;
  134.                 else                                                --              If ready for BIT5
  135.                     Counter := 0;                               --                  Reset time counter
  136.                     RxByte1(5) := RX;                           --                  Load value on RX pin into Byte1 BIT5
  137.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  138.                 end if;
  139.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT4
  140.                     Counter := Counter + 1;
  141.                 else                                                --              If ready for BIT4
  142.                     Counter := 0;                               --                  Reset time counter
  143.                     RxByte1(4) := RX;                           --                  Load value on RX pin into Byte1 BIT4
  144.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  145.                 end if;
  146.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT3
  147.                     Counter := Counter + 1;
  148.                 else                                                --              If ready for BIT3
  149.                     Counter := 0;                               --                  Reset time counter
  150.                     RxByte1(3) := RX;                           --                  Load value on RX pin into Byte1 BIT3
  151.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  152.                 end if;
  153.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT2
  154.                     Counter := Counter + 1;
  155.                 else                                                --              If ready for BIT2
  156.                     Counter := 0;                               --                  Reset time counter
  157.                     RxByte1(2) := RX;                           --                  Load value on RX pin into Byte1 BIT2
  158.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  159.                 end if;
  160.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT1
  161.                     Counter := Counter + 1;
  162.                 else                                                --              If ready for BIT1
  163.                     Counter := 0;                               --                  Reset time counter
  164.                     RxByte1(1) := RX;                           --                  Load value on RX pin into Byte1 BIT1
  165.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  166.                 end if;
  167.                 if (Counter < 5210) then                    --              Count 1 bit time to sample center of BIT0
  168.                     Counter := Counter + 1;
  169.                 else                                                --              If ready for BIT0
  170.                     Counter := 0;                               --                  Reset time counter
  171.                     RxByte1(0) := RX;                           --                  Load value on RX pin into Byte1 BIT0 (Stop bit)
  172.                     BitCount := BitCount + 1;               --                  Increment number of bits received
  173.                     RxBegun := '0';                         --                  Receive finished
  174.                 end if;
  175.                
  176.                 -- Error check --
  177.                 if (BitCount /= 16) then                    --              If wrong number of bits detected
  178.                     RxByte0 := (others => '0');         --                  Clear RxByte0
  179.                     RxByte1 := (others => '0');         --                  Clear RxByte1
  180.                 else                                                --              If data valid
  181.                     BitCount := 0;                              --                  Reset bit counter
  182.                     RxFinished := '1';                      --                  Receive is finished    
  183.                 end if;
  184.             end if;
  185.         end if;
  186.     end if;        
  187. end process RxBytes;
  188.  
  189. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement