12.07.2015 Views

VHDL - Practical Example - Designing an UART

VHDL - Practical Example - Designing an UART

VHDL - Practical Example - Designing an UART

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

© ALSE - Sept 2001<strong>VHDL</strong> - <strong>Practical</strong> <strong>Example</strong> -<strong>Designing</strong> <strong>an</strong> <strong>UART</strong>Bertr<strong>an</strong>d CUZEAUTechnical M<strong>an</strong>ager - ALSEASIC / FPGA Design ExpertDoulos HDL Instructor (Verilog-<strong>VHDL</strong>)info@ALSE-FR.COMhttp://www.alse-fr.com : 33.(0)1 45 82 64 01


IntroductionWe will demonstrate, on a “real-life” example, how asound HDL methodology c<strong>an</strong> be used in conjunctionwith modern synthesis <strong>an</strong>d simulation tools.Note : the source code we provide here if for teaching purpose only.This code belongs to ALSE.If you w<strong>an</strong>t to use it in your projects please contact us.© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


<strong>UART</strong> SpecificationWe w<strong>an</strong>t to address the following needs :• Tr<strong>an</strong>smit / Receive with h/w h<strong>an</strong>dshake• “N81” Format , but pl<strong>an</strong> for parity• Speed : 1200..115200 baud (Clock = 14.7456 MHz)• No internal Fifo (usually not needed in <strong>an</strong> FPGA !)• Limited frame timing checks© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


MethodologyWe adopt the following constraints :• St<strong>an</strong>dard & 100% portable <strong>VHDL</strong> Description :- Synthesis- Simulation- Target FPGA (or CPLD)• Complete functional Simulation with file I/O.• Should work “in vivo” on <strong>an</strong> existing ALSE demo board.© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


Application ArchitectureRXFLEXNoted on PCB = CTSFlexRTSFLEX**RS232 InputsI15I54RawRxCLKRSTDRawRTSI218CLKCI306DQQSDout[7:0]LD_SDoutRXBaud[2:0]CLKRSTRTSDin[7:0]LDRxBaud[2:0]CLKRST<strong>UART</strong>SI320<strong>UART</strong> moduleTxTxBusyDout[7:0]RxRDYRxErrTXTxBusySDin[7:0]RxRDYRxErr* TXoutI14(0=active)TxBusySDout[7:0] SDout[7:0] * CTSFLEXSDin[7:0]I202LD_SDout LD_SDoutNoted on PCB = RTSFlexRxRDYRxErrRTSApplicationRS 232 OutputInversion neededRSTCI307External Baud Rate SelectionBaud[2]DIPSW[2] *I319Baud[1]DIPSW[1] *I318Baud[0]DIPSW[0] *I317CLK CLKRST RST© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


Baud Rate Generator• Embedded in <strong>UART</strong>S.• Divides by 8, 16, 28, 48, 96, 192, 384 or 768<strong>an</strong>d builds Top16.• Generates two “ticks” by further dividing Top16 :- Tr<strong>an</strong>smit : TopTx, fixed rate- Receive : TopRx, mid-bit, resynchronized© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


-- ---------------------------- Baud rate selection-- --------------------------process (RST, CLK)beginif RST='1' thenDivisor Divisor Divisor Divisor Divisor Divisor Divisor Divisor Divisor Divisor


Tr<strong>an</strong>smitterWe use a very simple State Machine to control thetr<strong>an</strong>smit shift register. The FSM inputs are :– LD : Loads the character to tr<strong>an</strong>smit (Din)– TopTx : Bit shifting comm<strong>an</strong>dFor simplicity we code the FSM as a“re-synchronized Mealy”.© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


-- ---------------------------- Tr<strong>an</strong>smit State Machine-- --------------------------TX


ReceiverWe also use a State Machine :• Wait RX (Start bit) falling edge,• Synchronize the Half-bit counter• Sample RX at mid-bit <strong>an</strong>d verify the Start bit• Loop on the data bits (+ parity) :* Skip tr<strong>an</strong>sition* Sample at mid-bit• Sample <strong>an</strong>d Test Stop bit• Return to Idle state (waiting for a new Start condition)© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


-- ------------------------when Edge_Rx => -- should be near Rx edge-- RECEIVE State Machineif TopRx = '1' then-- ------------------------RxFSM


Receiver State Machine© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


Test ApplicationTo test our <strong>UART</strong>, we use a trivial “application” whichincrements the characters received <strong>an</strong>d resends them !(<strong>Example</strong> : “A”→“B”, “f”→“g”, “HAL”→”IBM”…)This way, it is easy to verify the reveive <strong>an</strong>d tr<strong>an</strong>smitoperations, both by simulation <strong>an</strong>d on the demo board.© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


-- APPLIC.vhd-- ------------------------------------------------------ Demo for <strong>UART</strong> module-- ------------------------------------------------------ Bertr<strong>an</strong>d Cuzeau / info@alsealse-frfr.com-- Receives a char, <strong>an</strong>d re-emits the same char + 1LIBRARY ieee;USE ieee.std_logic_1164.ALL;USE ieee.numeric_std.ALL;-- ----------------------------------------------------Entity APPLIC is-- ----------------------------------------------------Port ( CLK : In std_logic;RST : In std_logic;RTS : In std_logic;RxErr : In std_logic;RxRDY : In std_logic;SDin : In std_logic_vector (7 downto 0);TxBusy : In std_logic;LD_SDout: Out std_logic;SDout : Out std_logic_vector (7 downto 0));end APPLIC;-- ----------------------------------------------------Architecture RTL of APPLIC is-- ----------------------------------------------------type State_Type is (Idle, Get, Send);signal State : State_Type;signal RData : std_logic_vector (7 downto 0);signal SData : std_logic_vector (7 downto 0);beginSDout


Test BenchThe <strong>VHDL</strong> Test Bench simply sends the ASCII character ‘A’<strong>an</strong>d displays the character(s) sent back by the system.It is based on two behavioral <strong>UART</strong> routines (described in<strong>an</strong>other of our conferences).A much more sophisticated Test Bench (with file I/O <strong>an</strong>dconsole emulation with inter-character spacing) is provided byALSE in the commercial version.© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


-- ----------------------------------------------- Simple <strong>VHDL</strong> test bench for <strong>UART</strong> Top_Level-- ----------------------------------------------- (c) ALSE - Bertr<strong>an</strong>d Cuzeau-- info@alsealse-frfr.comUSE std.textiotextio.all;LIBRARY ieee;USE ieee.std_logic_1164.ALL;USE ieee.numeric_std.ALL;USE ieee.std_logic_.std_logic_textiotextio.ALL;entity testbench isend testbench;-- ---------------------------------------------Architecture TEST of testbench iscomponent ALSE_<strong>UART</strong>Port ( RST : In std_logic;CLK : In std_logic;RXFLEX : In std_logic;RTSFLEX : In std_logic;DIPSW : In std_logic_vector (2 downto 0);CTSFLEX : Out std_logic;TXout : Out std_logic );end component;const<strong>an</strong>t period : time := 68 ns;const<strong>an</strong>t BITperiod : time := 8680 ns; -- 115.200signal RSData : std_logic_vector (7 downto 0);signal CLK : std_logic := '0';signal RST : std_logic;signal RXFLEX : std_logic;signal RTSFLEX : std_logic;signal DIPSW : std_logic_vector (2 downto 0);signal CTSFLEX : std_logic;signal TXout : std_logic;begin-- UUT Inst<strong>an</strong>ciation :UUT : ALSE_<strong>UART</strong>Port Map (CLK=>CLK, CTSFLEX=>CTSFLEX, DIPSW=>DIPSW,RST=>RST, RTSFLEX=>RTSFLEX, RXFLEX=>RXFLEX,TXout=>=>TXout);-- Clock, Reset & DIP-SwitchesRST


Let’s make it work !After the theory, we are now going to follow the entire designflow, down to the Demo Board (~10 minutes) :1. Build the Project2. Syntactic Verification3. Unitary Functional Simulation4. Unitary Logic Synthesis5. System-Level Simulation6. Global Synthesis7. Place <strong>an</strong>d Route8. Download & tests on the demo board (using HyperTerminal !)© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com


ConclusionIt takes less th<strong>an</strong> a working day to design <strong>an</strong>d test a simple <strong>UART</strong>like this one. Powerful HDL l<strong>an</strong>guages, as well as capableSimulation <strong>an</strong>d Synthesis Tools are now widely available.With the right methodology <strong>an</strong>d some design practice, projects thatused to be considered as “complex” become almost trivial.Note : <strong>an</strong> enh<strong>an</strong>ced version of this <strong>UART</strong>, still simple <strong>an</strong>d efficient, isavailable at ALSE, at a very affordable cost.© Bertr<strong>an</strong>d CUZEAU - info@alse-fr.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!