Transmitting data from MATLAB to FPGA

dspfpgaimageprocessingMATLAB

This is about design of a JPEG compression/decompression scheme on an Altera FPGA. It is possible to have camera or SD card for image input and TV or SD card for image output. However, these interfaces introduce their own complexities.

To keep things simple in the early part of the project and to make it easier to thoroughly test the design, it is desired that it be possible to pass data of an image from MATLAB into the FPGA. MATLAB already has a lot of functions to manipulate data, so it is desired to send data to FPGA and read back the result after processing and compare the FPGA result with the result of identical processing done within MATLAB.

Basically data shall be read back from the FPGA from different stages of the JPEG compression and decompression processor to check how each part of the design is working.

So the question, how do I transmit data from MATLAB to the FPGA and read data back? Is there some tutorial on this somewhere?

Best Answer

I have done something very similar in the past. My aim was to transfer an image (*.bmp) from PC to FPGA (internal BRAM), and send it back to PC after the watermarking process. As previously mentioned, UART is your best bet. Implement a UART in FPGA or use an existing design. For Xilinx, look at this design provided with the Picoblaze.

It is well documented and can be used as standalone in your design. You can also find older versions of this design for older Xilinx FPGAs. I think you can find similar designs for Altera (or vendor independent) easily.

On the MATLAB, you can read and write data to a serial object using these functions. I have had problems with baud rates above the standard 115200 so if you need real-time performance, UART might not be sufficient. Otherwise start with the lowest baud rate and test it for errors and try to achieve maximum.

Though MATLAB will be fast and easy for this application; since it is not free and not everyone might have access to it, another option is to use Python. In my case, I have written a Python script to communicate with the FPGA. It has a nice and simple serial library called pyserial for serial communication. You can use PIL (Python Imaging Library) for image processing and numpy for computation process.

However; if you are only interested in testing the design for functionality, you can just read pixel data from a file to process it in simulation. Output image data to a text file using MATLAB and then read it into a memory array defined in your testbench file. You can simulate your design as if its running on the hardware (assuming the design is synthesizable) and test it. You can output processed data to a file at any stage of the process and read it from MATLAB for comparison. After you make sure the design works perfectly, you can start implementing the communication interface on actual hardware.