Embedded Web Server Development using Python and Flask Framework

Introduction

In the previous tutorial, we have built the web page for GCD accelerator. In this tutorial, we are going to build the embedded web server in Python. It stores and delivers the web page to client (web browser) that request it. We are going to use the Flask framework. It is a micro web framework written in Python.

Prerequisites

In order to follow this tutorial, you should have Flask framework installed in your ZYBO. Tutorial part 6 covers the Flask framework installation.

Web Server

The following listing shows Python code of the web server. The server does several tasks. First, it initializes the hardware GCD. It calculates software GCD in C via Python C extension module. Then, it also receives HTTP request (GET and POST request), processes it, and sends the HTTP response.

Accessing Hardware GCD in Python

First, in line 7-8, we define the physical address of the AXI GCD. Then, in line 11-12, we map that address to the virtual address space. Unlike in bare-metal program, the web server program that runs on top of the Linux OS can’t access the physical memory directly. The web server program can only access the virtual address space. So, we need to map the physical address to the virtual address space by using mmap function.

In line 15-23, we define our hardware GCD function. It calculates GCD using the GCD core. It accesses the GCD core in FPGA via the mem variable. The mem variable is like a pointer in C program.

HTTP Request and Response

The web server code in line 29-30 handles the HTTP GET request from the client. The web server will send the web page (index.html) to the client. In line 34-57, the server handles the HTTP POST request from the client. The web server receives input A and B for GCD calculation. Then, it calls the hardware and software GCD functions. The server also calculates the execution time of hardware and software GCD. The number of clock cycles is multiplied by 10 ns (the FPGA clock period). The result will be sent as an HTTP response to the client.

Software GCD in Python C Extension Module

For the software GCD, we are going to implement it in C. Then, by using Python C extension library, we are going to make it as a Python library. So, we will be able to call the software GCD function that is implemented in C from the Python code in the web server. The implementation of software GCD function is the same as in the tutorial part 5. Then, we wrap the software GD function in Python extension module.

In line 20-22, we map the physical address of the AXI performance counter to the virtual address. It is similar to the Python code when we map the AXI GCD. We us the mmap function. The perf_cnt_p is a pointer to the AXI performance counter.

Finally, we should make a Python script as follows for installing our software GCD module. The module will be installed as a Python library. So, we will be able to import it within our Python code.

Putting It All Together

Now, we are ready to run our web server. Please make sure your that your project folder in your ZYBO is populated as in the following figure. After that, we are going to install the software GCD module.

To install the module, you should execute the following command in ZYBO shell prompt.

Then, don’t forget to program the FPGA by executing the following command.

Finally, you can run the web server by executing the following command.

The web server will be started on your ZYBO’s IP address on port 5000. So, to open the web page, you should open your web browser and type the URL, for example 192.168.1.90:5000. The following video shows the final result.

You can get the full source code from here.

Summary

In this tutorial, we have learned how to build an embedded web server using Python and Flask. We have learned how to access the GCD core in FPGA via the virtual address. We can map the physical address of the GCD core to the virtual address by using mmap function. It can be done both in Python or C program. We have also learned how to build a Python C extension module.

This is the final part of this tutorial series. We have learned how to build a complete embedded system on the ZYBO that employs FPGA, CPU, Linux OS, and embedded web applications. I hope this course will be beneficial one for beginners in Zynq FPGA and Linux. So, you can adapt this project to fulfill your needs.

What’s Next?

There are several resources for you about Zynq development that you may want to check out. Please follow this link.  I have also video course about AXI4 protocol (please follow this link to the course). In this course, you will also learn the AXI4-Stream protocol.

Donation

If you benefit from this page, please make a small donation. I suggest $5, but you can choose the amount. We need your help to pay for web hosting, buy new components and equipment for new tutorials, and buy a cup of coffee :). Alternately, you can make a donation by sending me Bitcoin, at address 1JN3zEw8NqCr8bn4UQa3TfZXaz7UZnb5bH

Leave a Reply

Your email address will not be published. Required fields are marked *