Author Topic: UART on FPGA  (Read 6932 times)

0 Members and 2 Guests are viewing this topic.

Offline gauravmpTopic starter

  • Regular Contributor
  • *
  • Posts: 77
  • Country: us
UART on FPGA
« on: June 28, 2016, 02:33:05 pm »
Hello,

I've been working on trying to get UART running on an FPGA(MAX10) using my own code and other code from online sources like opencores and fpga4fun.com

My own code resembles that from fpga4fun.com ( http://www.fpga4fun.com/SerialInterface.html). However, whatever I try, I keep getting junk values when I read this data on my laptop. For converting to USB, I'm using an FTDI which I've used before for several microcontrollers and it has worked always.

Could someone pls let me know what I should be looking for?

Thanks
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 30439
  • Country: nl
    • NCT Developments
Re: UART on FPGA
« Reply #1 on: June 28, 2016, 02:45:11 pm »
I'd check the signals using an oscilloscope.
Also: did you run the FPGA code through a simulator? That would be a wise thing to do.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 2340
Re: UART on FPGA
« Reply #2 on: June 28, 2016, 02:59:57 pm »
I would be looking for an oscilloscope!
Don't try to make the whole lot work at once - break it down into blocks or modules that you can easily debug. This may entail writing extra code and/or make extra hardware.
How are you implementing the "physical layer" - have you accounted for the inversion of the logic?
 
The following users thanked this post: gauravmp

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 9942
  • Country: nl
  • Current job: ATEX product design
Re: UART on FPGA
« Reply #3 on: June 28, 2016, 03:01:13 pm »
Could be that your baud rate setting is wrong. Or it is off a few percent. Or you put it to the wrong pins. Or the data gremlin. Or voltage level. Or interference. Or or or...
Measure power rails with 2GHz bandwidt: https://eleshop.eu/prp1-power-rail-probe.html
 
The following users thanked this post: gauravmp

Offline SteveyG

  • Supporter
  • ****
  • Posts: 999
  • Country: gb
  • Soldering Equipment Guru
Re: UART on FPGA
« Reply #4 on: June 28, 2016, 03:38:59 pm »
How do the simulations look? Chances of you writing HDL that works first time in hardware are nil.

If you've copied most of the design, I'd be checking your clock generation.
YouTube Channel: https://www.youtube.com/user/sdgelectronics/
Use code: “SDG5” to get 5% off JBC Equipment at Kaisertech
 
The following users thanked this post: gauravmp

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 14860
  • Country: gb
    • Mike's Electric Stuff
Re: UART on FPGA
« Reply #5 on: June 28, 2016, 03:48:55 pm »
I keep getting junk values when I read this data on my laptop.
Why are you looking at it on your laptop and not a scope?

If the answer is that you don't have a scope, then get one before you spend another second on this. Doing FPGA stuff without a scope is a hopeless waste of time. 
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
The following users thanked this post: gauravmp

Offline gauravmpTopic starter

  • Regular Contributor
  • *
  • Posts: 77
  • Country: us
Re: UART on FPGA
« Reply #6 on: June 28, 2016, 05:33:57 pm »
Thanks for the suggestions guys! Taking your suggestions, I am using my scope now. The problem seems to be that my state machine is screwed up. So it's going to a state when it should be. Pls see this and tell me if you know what is wrong:


always @(posedge clk)
begin
   
   if(switch==0)
      begin
      transmit_flag <= 1;
      led <= 0;
      end
   else
   begin
      transmit_flag <= 0;
      led <= 1;
   end
end   
// baud ticks

always @(posedge clk_baud)
begin
   baud_count <= baud_count+1;
   if(baud_count==16)
      baud_count <= 0;
end

assign baud_tick = (baud_count==15);

//state machine
reg start;
always @(posedge clk)
case(state)
  4'b0000: if(transmit_flag) begin state <= 4'b0100; start <= 1; end
  4'b0100: if(baud_tick) begin state <= 4'b1000; start <= 0; end // start
  4'b1000: if(baud_tick) state <= 4'b1001; // bit 0
  4'b1001: if(baud_tick) state <= 4'b1010; // bit 1
  4'b1010: if(baud_tick) state <= 4'b1011; // bit 2
  4'b1011: if(baud_tick) state <= 4'b1100; // bit 3
  4'b1100: if(baud_tick) state <= 4'b1101; // bit 4
  4'b1101: if(baud_tick) state <= 4'b1110; // bit 5
  4'b1110: if(baud_tick) state <= 4'b1111; // bit 6
  4'b1111: if(baud_tick) state <= 4'b0001; // bit 7
  4'b0001: if(baud_tick) state <= 4'b0000; // stop1
  //4'b0010: if(baud_tick) state <= 4'b0000; // stop2
  default: if(baud_tick) state <= 4'b0000;
endcase

reg muxbit;

always @(state[2:0])
case(state[2:0])
  0: muxbit <= 0;//tx_data[0];
  1: muxbit <= 1;//tx_data[1];
  2: muxbit <= 0;//tx_data[2];
  3: muxbit <= 0;//tx_data[3];
  4: muxbit <= 0;//tx_data[4];
  5: muxbit <= 0;//tx_data[5];
  6: muxbit <= 1;//tx_data[6];
  7: muxbit <= 0;//tx_data[7];
endcase

// combine start, data, and stop bits together
assign tx = (state<4) | (state[3] & muxbit);




THanks
 

Offline gauravmpTopic starter

  • Regular Contributor
  • *
  • Posts: 77
  • Country: us
Re: UART on FPGA
« Reply #7 on: June 28, 2016, 06:09:15 pm »
I figured out the problem guys!!!! thanks a lot!! it works now!!!!
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: UART on FPGA
« Reply #8 on: June 28, 2016, 07:04:13 pm »
I figured out the problem guys!!!! thanks a lot!! it works now!!!!

Don't leave us hanging (and broken code sitting the the thread).... what was it that tripped you up? was it anything that might trip somebody else up?
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 
The following users thanked this post: gauravmp

Online nctnico

  • Super Contributor
  • ***
  • Posts: 30439
  • Country: nl
    • NCT Developments
Re: UART on FPGA
« Reply #9 on: June 28, 2016, 07:09:32 pm »
I hate that code BTW. I think I can do the same in a couple of lines of VHDL.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: UART on FPGA
« Reply #10 on: June 28, 2016, 08:52:15 pm »
I hate that code BTW. I think I can do the same in a couple of lines of VHDL.

To the original poster...

Don't take the above comment as a criticism, it is a very subtle joke - you can do nothing in a couple of lines of VHDL!

It takes at least two lines to include the standard libraries...
and another couple for the entity declarations...
and then another couple for the architecture declarations...
and then another three to start and end the process...
and then you might start to write your first line of code.
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 30439
  • Country: nl
    • NCT Developments
Re: UART on FPGA
« Reply #11 on: June 28, 2016, 08:55:30 pm »
I hate that code BTW. I think I can do the same in a couple of lines of VHDL.

To the original poster...

Don't take the above comment as a criticism, it is a very subtle joke - you can do nothing in a couple of lines of VHDL!
It is not a joke. True you'll need some entity declarations but the UART itself can be a lot less lines of code and easier to maintain. There are way too much repetitions in the code shown above and a very typical example of how not to write HDL.

Something like this (off the cuff):
Code: [Select]
signal uart_tx_data: std_logic(7 downto 0);  --input
signal uart_tx_start: std_logic; -- input

signal uart_tx_shift: std_logic_vector(9 downto 0);
signal uart_tx_count: unsigned(3 downto 0);
signal baud_tick : std_logic;
signal uart_tx : std_logic;
signal uart_transmit_busy : std_logic;

process uart(clk)
if rising_edge(clk) then
  if uart_tx_start=1' and  uart_transmit_busy='0'; then
    uart_transmit_busy<='1';
    uart_tx_shift <= "0" & uart_tx_data & "1";
    uart_tx_count <= 9;
  end if;

  if baud_tick='1' then
   if uart_transmit_busy='1' then;
     uart_tx<=uart_tx_shift(to_integer(uart_tx_count));
     uart_tx_count<= uart_tx_count-1;
     if uart_tx_count=0 then
        uart_transmit_busy<='0'
     end if;
   else
    uart_tx<='1';
  end if;
end if;
end process;

edit: typo
« Last Edit: June 28, 2016, 09:36:13 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3845
  • Country: us
Re: UART on FPGA
« Reply #12 on: June 28, 2016, 09:32:28 pm »
Code: [Select]
always @(posedge clk_baud)
begin
   baud_count <= baud_count+1;
   if(baud_count==16)
      baud_count <= 0;
end

Think carefully about what this does. In particular, what the value of baud_count is at each point in the code.
This looks like a bug.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: UART on FPGA
« Reply #13 on: June 28, 2016, 09:52:24 pm »
Quote
It is not a joke. True you'll need some entity declarations but the UART itself can be a lot less lines of code and easier to maintain. There are way too much repetitions in the code shown above and a very typical example of how not to write HDL.

Something like this (off the cuff):
....

You are still overthinking it a little (off the cuff too, so most likely has a bug or two):

Code: [Select]
...
  port (
    clk           : in  std_logic(7 downto 0); 
    uart_tx_data  : in  std_logic(7 downto 0); 
    uart_tx_start : in  std_logic;
    uart_busy     : out std_logic;
    uart_tx       : out std_logic);
....
   signal uart_tx_shift : std_logic_vector(9 downto 0);
   signal uart_busy     : std_logic_vector(9 downto 0);
   signal baud_count    : unsigned(??? downto 0);
   signal baud_tick     : std_logic;

....
  uart_tx   <= uart_tx_shift(0);
  uart_busy <= uart_busy_shift(0);
...
process uart(clk)
  if rising_edge(clk) then
    if baud_tick='1' then
     uart_tx_shift   <= '1' & uart_tx_shift(9 downto 1);
     uart_busy_shift <= '0' & uart_busy_shift(9 downto 1);
    end if;

    if uart_tx_start='1' and uart_busy(0) ='0'; then
      uart_tx_shift <= "1" & uart_tx_data & "0";
      uart_busy_shift <= (others => '1');
    end if;
 
    -- nasty baud tick generator
    if baud_count = clock_rate/baud_rate-1 then
        baud_count <= (others => '0');
        baud_tick  <= '1';
    else
        baud_count <= baud_count+1;
        baud_tick  <= '0';
    end if;
  end if;
end process;
....
« Last Edit: June 29, 2016, 11:01:52 am by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 30439
  • Country: nl
    • NCT Developments
Re: UART on FPGA
« Reply #14 on: June 29, 2016, 10:42:24 pm »
I wanted to stay closer to the original and show how easy it is to make a mux by using a counter as an index. BTW your method may look shorter but a mux can use the outputs of a memory like a FIFO directly and use fixed values for the start & stop bits so all in all my method (when more cleverly applied) could end up using less flipflops.
« Last Edit: June 30, 2016, 12:29:49 am by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: UART on FPGA
« Reply #15 on: June 30, 2016, 01:36:24 pm »
How about implementing the UART using a simple shift register. Just load the shift register MSB with the start bit with the value "1", followed by the data bits, optional parity, the stop bit with the value "0" and finally the end token with the value "1". Then keep shifting out until all other bits are "0" except the MSB (the MSB is always "1", but it can be "0" after RESET for example, so excluding the MSB from the comparison will make the design robust). For 8 data bits and even/odd parity the total required shift register length is 12 bits.

Edit: My suggestion is partly wrong as I assumed (remembered) wrongly that the data bits are sent MSB first. As hamster_nz writes below, the data will be sent LSB first which will also make the "end token" bit unnecessary.
« Last Edit: July 02, 2016, 01:37:27 pm by Kalvin »
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: UART on FPGA
« Reply #16 on: July 02, 2016, 09:56:22 am »
I wanted to stay closer to the original and show how easy it is to make a mux by using a counter as an index. BTW your method may look shorter but a mux can use the outputs of a memory like a FIFO directly and use fixed values for the start & stop bits so all in all my method (when more cleverly applied) could end up using less flipflops.

I implemented the design suggested by Kalvin and tested it on a board sending characters from the switches and buttons - 12 LUTs and 11 FFs, for the UART, another 3 LUTs and 13 FFs to generate the bit_tick signal.

Code: [Select]
+-------------------+---------------+------------+------------+---------+------+-----+--------+--------+--------------+
|      Instance     |     Module    | Total LUTs | Logic LUTs | LUTRAMs | SRLs | FFs | RAMB36 | RAMB18 | DSP48 Blocks |
+-------------------+---------------+------------+------------+---------+------+-----+--------+--------+--------------+
| basys3            |         (top) |         15 |         15 |       0 |    0 |  24 |      0 |      0 |            0 |
|   (basys3)        |         (top) |          3 |          3 |       0 |    0 |  13 |      0 |      0 |            0 |
|   i_tiny_rs232_tx | tiny_rs232_tx |         12 |         12 |       0 |    0 |  11 |      0 |      0 |            0 |
+-------------------+---------------+------------+------------+---------+------+-----+--------+--------+--------------+


Code: [Select]
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity tiny_rs232_tx is
    Port ( clk         : in  STD_LOGIC;
           bit_tick    : in  STD_LOGIC;
           data        : in  STD_LOGIC_VECTOR(7 downto 0);
           data_enable : in  STD_LOGIC;
           tx          : out STD_LOGIC := '1';
           busy        : out STD_LOGIC );
end tiny_rs232_tx;

architecture Behavioral of tiny_rs232_tx is
    signal shift_reg  : std_logic_vector(9 downto 0) := (others => '1');
    signal i_busy     : std_logic;
begin
    busy <= i_busy;
    with shift_reg select i_busy <= '0' when "0000000000", '1' when others;

clk_proc: process(clk)
    begin
        if rising_edge(clk) then
            if i_busy = '0' and data_enable = '1' then
                shift_reg <= '1' & data & '0';
            end if;             
       
            if bit_tick = '1' then
                if i_busy = '1' then
                    tx <= shift_reg(0);
                    shift_reg <= '0' & shift_reg(9 downto 1);
                end if;
            end if;
        end if;
    end process;
end Behavioral;
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline gauravmpTopic starter

  • Regular Contributor
  • *
  • Posts: 77
  • Country: us
Re: UART on FPGA
« Reply #17 on: July 20, 2016, 03:00:27 am »
I figured out the problem guys!!!! thanks a lot!! it works now!!!!

Don't leave us hanging (and broken code sitting the the thread).... what was it that tripped you up? was it anything that might trip somebody else up?

So, if you did notice the driver of the state machine I used was the baud tick. This baud tick is 1 for a number of clock cycles. So, I was skipping states. All I had to do was have the posedge of the baud tick instead of the clk be in the always block and it works.
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1961
  • Country: gb
Re: UART on FPGA
« Reply #18 on: July 20, 2016, 09:34:18 am »
In my limited experience of programmable logic, I have always used, and seen people using pulses generated form the main clock.  This isn't about clocking state machines form a derived clock - everything in the module should be clocked by the same clock, but using 'enables' instead.  So say you have your main clock at 16x the baud, you would generate a single pulse, one clock width wide, that drives the state machine.   The first thing the state machine does is check if this pulse line is high, if not, it does nothing.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->