UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Visitor meganadhan
Visitor
21,374 Views
Registered: ‎06-25-2014

Sending an Image file via UART

Hi All,

 

1. Is it possible to send an image file( .jpg -1080p) from PC to FPGA(ML605 evaluation board) via UART ?

2. .jpg to .hex to .txt then  through hyper terminal ? or any  other way ?

 

Meganadhan

 

0 Kudos
14 Replies
Explorer
Explorer
21,360 Views
Registered: ‎11-24-2013

Re: Sending an Image file via UART

Hi, Meganadhan,

 

every image file is just a bunch of bytes, so, of course, you can send them via UART. For doing this, you need to access these bytes and send them one by one through the serial port.

The majority of the terminal programs allow you to do this. They usually have an option called "send file" for this task. For example, in the free program "RealTerm" you have it here:

 

realtermcapture.PNG

 

 

Then, on the FPGA you could save the bytes received by your UART in a memory or whatever you wanted to do with them. All these bytes together will form the JPEG file.

 

Nevertheless, even though you can transmit a JPEG image via UART, this might not be the best way to do it. Let's do some calculations: Imagine that your UART is 9600 bauds (9600 bps in standard serial comunication) and you are using this speed for the transmission, and imagine your image size is 1MB. The time it would take if the transmission was perfet would be:

 

t_trans = 1 * 1024 * 1024 * 8 [bits] / 9600 [bits/sec] = 873.81 sec = 14.56 min

 

 

So, maybe, if you want to work with images, a better solution would be using the Ethernet port that you have on that board.

 

Regards,

Ignacio.

Tags (4)
0 Kudos
Visitor meganadhan
Visitor
21,349 Views
Registered: ‎06-25-2014

Re: Sending an Image file via UART

HI ignacio,

 

thanks for your comments

 

  1. Suppose i am planning  using a Microblaze processor with uart core as peripheral ,receiving each byte from uart and writing it in to a FIFO in c code.
  2. If i am directly sending a jpg(24 bit RGB) image, what woudl be the value of each byte received ?
  3. Is it a hex vale or ascii or binary value value i am getting inside the C code ?
  4. How can i convert this to binary RGB for writing it to a FIFO inside the FPGA ?

Meganadhan

0 Kudos
Highlighted
Explorer
Explorer
21,338 Views
Registered: ‎11-24-2013

Re: Sending an Image file via UART

Hello,

 

if you are using MicroBlaze, you will use the next function for receiving data:

 

unsigned int XUartLite_Recv ( XUartLite * InstancePtr,
                              u8 * DataBufferPtr,
                              unsigned int NumBytes
                                                    ) 	

 

The arg. DataBufferPtr is a pointer to a buffer to where you will save the received data for each time, which will be a 8 bits unsigned integer, that means, a byte of the JPEG file. That is the value that you are receiving in your C code, as you say, 1 byte.

 

Now, the bytes that you are receiving are not the bytes with the colour information. A JPEG file is a compressed file with a lot of information. You are receiving the bytes of the file, but then you will have to be able to read them properly to get the image information.

 

Extracting the RGB data from a JPG is not an easy task. You might find interesting this C library and its documentation for getting this:

 

http://libjpeg.sourceforge.net/

 

Regards,

Ignacio.

Tags (4)
0 Kudos
20,474 Views
Registered: ‎02-23-2015

Re: Sending an Image file via UART

how about if the image format is .bmp? and i'm not using microblaze, it is still possible?

0 Kudos
Explorer
Explorer
20,458 Views
Registered: ‎11-24-2013

Re: Sending an Image file via UART


@halimfutuwijaya wrote:

how about if the image format is .bmp? and i'm not using microblaze, it is still possible?


Hi!

 

The file format is not relevant. It doesn't matter if it is an image (of any kind), a sound or whatever. The important thing are the information bytes contained in the file.

 

Of course you can receive an image not using MicroBlaze. All you need is an UART module and some logic for controlling the data transmission, for instance a state machine. For the UART you can design your own or search over the Internet a pre-designed one. This serial communications are not difficult at all to implement.

 

Regards

Ignacio

 

 

Tags (2)
0 Kudos
20,446 Views
Registered: ‎02-23-2015

Re: Sending an Image file via UART

thanks for the help before 

i have another question

if i work with rgb image that consist of 24 bit each pixel, and i already made an algorithm that has  24 bit for input. but

when i  search uart program in internet, all the program that i get  just buffer 8 bit and send it to main program , do you have some advice for me to solve my problem?,maybe any additional program that i have to create?

 

has the uart standard just allow us to transfer 8 bit each cycle ?

 

if i change the code from that uart program for buffer 24 bit directly it is possible?, im afraid that it cause unmatched standard between pc and fpga

 

best regards

halim

0 Kudos
Advisor eilert
Advisor
20,432 Views
Registered: ‎08-14-2007

Re: Sending an Image file via UART

Hi Halim.

This is one of the oldest integrated UART devices:

http://www.intersil.com/content/dam/Intersil/documents/hd-6/hd-6402.pdf

 

On pg.2 you find a list of all the modes a UART should support.

 

However, out of practical reasons, 8 bit (= 1 Byte)  is the standard today.

(While PC standard UARTs do not support longer word sizes it is possible to do such things with FPGAs.

But there's a limit, due to the asynchronous behavior of the UART protocol.

For short connections between two devices todays clocks should be reliable enough, stil there's some risk to run out of synchronisation that needs to be considered)

 

If you are about to work with 24 bit words you have to pack them somehow into at least 3 bytes.

Keep in mind that synchronisation is important, otherwise R, G and B might be in the wrong order.

There are numerous approaches with different pros and cons.

It's up to you to work out a scheme that works for you.

 

Have a nice synthesis

  Eilert

0 Kudos
Explorer
Explorer
20,429 Views
Registered: ‎11-24-2013

Re: Sending an Image file via UART

Hi, Halim,

 

you have to stick to the serial 8 bits in one line. This means that your output will be just of 1 bit depth, and in pieces of 8 bits. So, you must program the logic for sending the data in this way. It is not going to be difficult and you will learn about how to handle serial communications, which is good :)

 

Of course you could implement a 24 bit parallel transfer between your devices, but a computer will not "understand" that. You don't even have a standard way to insert those bits in it.

 

But, you must be aware of something: the fact that you have the information of the pixels doesn't mean you have an image file. If you send this information to the computer, at the computer side you must have some kind of software that, from the information of the bits you are sending, generates an image file. Another approach will be to send all the file headers and all that stuff from the FPGA, but this can be a bit messy. It depends on what you have.

 

What I mean is: if you have, for instance, an image of 4 pixels, with a resolution of 2*2, and the information that you have is this: {0x4F, 0x03, 0x01, 0xCA}, that doesn't mean that if you write that into a file you will get an image file. You must create a propper file. In your case, with BMP, you must add some headers (more info. here).

 

Regards

Ignacio

Tags (3)
0 Kudos
Historian
Historian
20,414 Views
Registered: ‎02-25-2008

Re: Sending an Image file via UART


@halimfutuwijaya wrote:

if i work with rgb image that consist of 24 bit each pixel, and i already made an algorithm that has  24 bit for input. but when i search uart program in internet, all the program that i get  just buffer 8 bit and send it to main program , do you have some advice for me to solve my problem?,maybe any additional program that i have to create?


All you need to do is to take each 24 bit word and send it in three chunks. It's really not hard at all.


has the uart standard just allow us to transfer 8 bit each cycle ?


There is no such thing as "The UART standard."

----------------------------Yes, I do this for a living.
0 Kudos
9,870 Views
Registered: ‎02-23-2015

Re: Sending an Image file via UART

thanks for the information

 

so ,can you give me some advice to send 24 bit in 3 chunks? can i use shift register first for receive that chunks before my algorithm?

 

 

 

 

 

 

 

 

 

regards 

halim

0 Kudos
9,863 Views
Registered: ‎02-23-2015

Re: Sending an Image file via UART

hi imginagcio

 

can you tell me about some software that you mention before?

 

 

 

 

 

 

 

 

 

 

regards

halim

0 Kudos
Explorer
Explorer
9,848 Views
Registered: ‎11-24-2013

Re: Sending an Image file via UART


@halimfutuwijaya wrote:

hi imginagcio

 

can you tell me about some software that you mention before?

 


 

Sure! I meant to program a piece of software for the reception and the file creation from the raw pixels information. I will give you an example in C#:

 

For receiving the serial data, you must use something like this (taken from Microsoft Developer Network):

 

using System;
using System.IO.Ports;

class PortDataReceived
{
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM1");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceivedHandler(
                        object sender,
                        SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
}

 

Then you must put the chunks of information together to create the pixels, to get an array of bytes, and save into a file. It would be something like this:

 

using System.Drawing.Image;
System.IO.MemoryStream

//You must have somethin like this array:
byte [] pixels;

//You put them into a Memory Stream:
var pixelsMs = MemoryStream(pixels);

//You create an image:
System.Drawing.Image receivedImage= Image.FromStream(pixelsMs);

//You save it
image.Save("here you put the path")

 

Regards

Ignacio

0 Kudos
9,835 Views
Registered: ‎02-23-2015

Re: Sending an Image file via UART

thanks ignacio

 

i will try it letter

 

but for sending an image to fpga, can i use realterm?  actually i'm doing image encryption project and the size pixel is 1600X1600, it's very big , but it's ok if it will take a long time for transfer it via UART because i just want to make sure that my alghorithm was true.

 

 and i have some trouble with the uart rx code. i would like to simulate it with random bit that represent transfer bit from pc, but it's hard to sychronise the timing between uart rx and the random bit program , here the code

 

----------------------------------------------------------------------------------

RANDOM BIT
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;


entity random is
 generic (
    g_CLKS_PER_BIT : integer := 87    -- Needs to be set correctly-->  10 MHZ/115200(baud rate)
    );
    Port (  clk :in  STD_LOGIC;
	
	 output : out  STD_LOGIC);
end random;

architecture Behavioral of random is
signal counter: integer:=0;

 
begin
process (clk)
begin
 
	
if rising_edge(clk) then
	counter <= counter+1;
	if counter= (g_CLKS_PER_BIT-1) then
		output <= '1';		
		elsif counter=55 then
		output <= '0';
		elsif counter=65	then
		output <= '0';
		elsif counter=75 then
		output <= '0';
		elsif counter=85 then
		output <= '1';
		elsif counter=95 then
		output <= '0';
		elsif counter=105 then
		output <= '0';
		elsif counter=115 then
		output <= '0';
		--elsif counter =1 then
		counter <= 0;
		
	end if;
end if;
end process;

end Behavioral;

 

----------------------------------------------------------------------UART RX
-- File Downloaded from http://www.nandland.com
----------------------------------------------------------------------
-- This file contains the UART Receiver.  This receiver is able to
-- receive 8 bits of serial data, one start bit, one stop bit,
-- and no parity bit.  When receive is complete o_rx_dv will be
-- driven high for one clock cycle.
-- 
-- Set Generic g_CLKS_PER_BIT as follows:
-- g_CLKS_PER_BIT = (Frequency of i_clk)/(Frequency of UART)
-- Example: 10 MHz Clock, 115200 baud UART
-- (10000000)/(115200) = 87
--
library ieee;
use ieee.std_logic_1164.ALL;
use ieee.numeric_std.all;

entity receive is
  generic (
    g_CLKS_PER_BIT : integer := 87    -- Needs to be set correctly-->  10 MHZ/115200(baud rate)
    );
  port (
    i_clk       : in  std_logic;
    i_rx_serial : in  std_logic;
    o_rx_dv     : out std_logic;
    o_rx_byte   : out std_logic_vector(7 downto 0)
    );
end receive;


architecture rtl of receive is

  type t_SM_MAIN is (s_IDLE, s_RX_START_BIT, s_RX_DATA_BITS,
                     s_RX_STOP_BIT, s_CLEANUP);
  signal r_SM_MAIN : t_SM_MAIN := s_IDLE;

  signal r_RX_DATA_R : std_logic := '0';
  signal r_RX_DATA   &colon; std_logic := '0';
  
  signal r_CLK_COUNT : integer range 0 to g_CLKS_PER_BIT-1 := 0;
  signal r_BIT_INDEX : integer range 0 to 7 := 0;  -- 8 Bits Total
  signal r_RX_BYTE   : std_logic_vector(7 downto 0):= (others => '0');
  signal r_RX_DV     : std_logic := '0';
  
begin

  -- Purpose: Double-register the incoming data.
  -- This allows it to be used in the UART RX Clock Domain.
  -- (It removes problems caused by metastabiliy)
  p_SAMPLE : process (i_clk)
  begin
    if rising_edge(i_clk) then
      r_RX_DATA_R <= i_rx_serial;
      r_RX_DATA   <= r_RX_DATA_R;
    end if;
  end process p_SAMPLE;
  

  -- Purpose: Control RX state machine
  p_UART_RX : process (i_clk)
  begin
    if rising_edge(i_clk) then
        
      case r_SM_MAIN is

        when s_IDLE =>
          r_RX_DV     <= '0';
          r_CLK_COUNT <= 0;
          r_BIT_INDEX <= 0;

          if r_RX_DATA = '0' then       -- Start bit detected
            r_SM_MAIN <= s_RX_START_BIT;
          else
            r_SM_MAIN <= s_IDLE;
          end if;

          
        -- Check middle of start bit to make sure it's still low
        when s_RX_START_BIT =>
          if r_CLK_COUNT = (g_CLKS_PER_BIT-1)/2 then
            if r_RX_DATA = '0' then
              r_CLK_COUNT <= 0;  -- reset counter since we found the middle
              r_SM_MAIN   <= s_RX_DATA_BITS;
            else
              r_SM_MAIN   <= s_IDLE;
            end if;
          else
            r_CLK_COUNT <= r_CLK_COUNT + 1;
            r_SM_MAIN   <= s_RX_START_BIT;
          end if;

          
        -- Wait g_CLKS_PER_BIT-1 clock cycles to sample serial data
        when s_RX_DATA_BITS =>
          if r_CLK_COUNT < g_CLKS_PER_BIT-1 then
            r_CLK_COUNT <= r_CLK_COUNT + 1;
            r_SM_MAIN   <= s_RX_DATA_BITS;
          else
            r_CLK_COUNT            <= 0;
            r_RX_BYTE(r_BIT_INDEX) <= r_RX_DATA;
            
            -- Check if we have sent out all bits
            if r_BIT_INDEX < 7 then
              r_BIT_INDEX <= r_BIT_INDEX + 1;
              r_SM_MAIN   <= s_RX_DATA_BITS;
            else
              r_BIT_INDEX <= 0;
              r_SM_MAIN   <= s_RX_STOP_BIT;
            end if;
          end if;


        -- Receive Stop bit.  Stop bit = 1
        when s_RX_STOP_BIT =>
          -- Wait g_CLKS_PER_BIT-1 clock cycles for Stop bit to finish
          if r_CLK_COUNT < g_CLKS_PER_BIT-1 then
            r_CLK_COUNT <= r_CLK_COUNT + 1;
            r_SM_MAIN   <= s_RX_STOP_BIT;
          else
            r_RX_DV     <= '1';
            r_CLK_COUNT <= 0;
            r_SM_MAIN   <= s_CLEANUP;
          end if;

                  
        -- Stay here 1 clock
        when s_CLEANUP =>
          r_SM_MAIN <= s_IDLE;
          r_RX_DV   <= '0';

            
        when others =>
          r_SM_MAIN <= s_IDLE;

      end case;
    end if;
  end process p_UART_RX;

  o_rx_dv   <= r_RX_DV;
  o_rx_byte <= r_RX_BYTE;
  
end rtl;

 

----------------------------------------------------------------------------------
TOP
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;



entity top is
port(cl_k: in std_logic
	  );
end top;

architecture Behavioral of top is

-------------------------------------------------------------------------
--component random
-------------------------------------------------------------------------
component random is
 generic (
    g_CLKS_PER_BIT : integer := 87    -- Needs to be set correctly-->  10 MHZ/115200(baud rate)
    );
    Port ( clk 	:in  STD_LOGIC;
			  output : out  STD_LOGIC
			  );
end component;
-------------------------------------------------------------------------
--component uart rx
-------------------------------------------------------------------------
component receive is
  generic (
    g_CLKS_PER_BIT : integer := 87    -- Needs to be set correctly-->  10 MHZ/115200(baud rate)
    );
  port (
    i_clk       : in  std_logic;
    i_rx_serial : in  std_logic;
    o_rx_dv     : out std_logic;
    o_rx_byte   : out std_logic_vector(7 downto 0)
    );
end component;

-------------------------------------------------------------------------
--signal
-------------------------------------------------------------------------
signal mlebu:std_logic;
signal enabl:std_logic;
signal metu	:std_logic_vector (7 downto 0);
begin
a:random port map( output=>mlebu,
						 clk=>cl_k
);
b:receive port map( i_clk=>cl_k,
						  i_rx_serial=>mlebu,
						  o_rx_dv=>enabl,
						  o_rx_byte=>metu
);


end Behavioral;

 

 

regards

halim

 

 



0 Kudos
Explorer
Explorer
8,737 Views
Registered: ‎11-24-2013

Re: Sending an Image file via UART

Hello halim,

 

I apollogize for this huge delay. I forgot to answer... Now I gess that you found a solution, but just in case it helps more people...

 


but for sending an image to fpga, can i use realterm?  actually i'm doing image encryption project and the size pixel is 1600X1600, it's very big , but it's ok if it will take a long time for transfer it via UART because i just want to make sure that my alghorithm was true.


 

You can send via serial, with RealTerm or other programs, whatever you want. It does not matter how big it is. But you have to take into account that the receiver must have to have where to store the information and that the communication must be at a speed in which the receiver can handle it without saturating it's input buffer (if this exists). If the sender is too quick for the receiver, you will loose data unless you implement a flow control.

 


 and i have some trouble with the uart rx code. i would like to simulate it with random bit that represent transfer bit from pc, but it's hard to sychronise the timing between uart rx and the random bit program


 

Since you just want to simulate it, in my oppinion it is not worth it to create a component that it's not going to be implemented afterwards. I would just create an array with the random numbers and then I would extract such numbers out of the array syncrhonously in the test bench file where is your unit under test.

 

There are functions in the library ieee.math.real that allow you to generate a random number. This is fine for simulation. You could just generate a number from 0 to 1 in each clock cycle and then, according to if it's greater than 0.5 set '1', or if not set '0'. This would be synchronous to your simulation clock.

 

Regards,

 

Ignacio

 

Tags (1)
0 Kudos