Skip to content

Handson 5

Rotary Encoder

For this handson you get a Rotary Encoder.

Lookup how such an encoder works.

Create verilog code to track the value of the Rotary Encoder.

Generate an interrupt when the button is pressed. This interrupt should go high for 1 clockcycle.

Create a testbench for your rotary encoder module.

Test your module quickly to make sure it works on the hardware.

Debouncing is an issue here. Do not forget to add proper debouncing to the inputs of the encoders.

Create and Package a new AXI IP

Create an AXI IP from the verilog code.

With the Vivado project opened, from the menu select Tools/Create and Package IP...

  • Click Next
  • Select Create a new AXI4 peripheral
  • Now you can give the peripheral an appropriate name, description and location. Click Next.
  • On the next page, we can configure the AXI bus interface. For the Rotary Encoder we'll use AXI Lite, ant it'll be a slave to the PS, so we'll stick with the default values.
  • On the last page select "Edit IP" and click Finish. Another Vivado window will open which will allow you to modify the peripheral that we just created.
  • At this point, the peripheral that has been generated by Vivado is an AXI lite slave that contains 4 x 32 bit read/write registers. We want to add our Rotary Encoder code to the IP and modify it so that one of the registers connects to the Rotary Encoder output.
  • Add the Rotary Encoder code to the project using "Add Sources", add the verilog file as a Design Source. Select the checkbox to copy the sources into the IP directory.

Next we will have to modify the peripheral. When we have a look at the sources in the project manager, we see that there are two files from the AXI IP and our freshly added rotary encoder file.

The two files are:

  • top file for the AXI IP
  • the actual implementation of the AXI Lite signals, which is the AXI Bus Interface S00_AXI

Have a look at the source code of both files.

Next, in the top file add the following user ports:

        // Users to add ports here
        input wire encoder_A,
        input wire encoder_B,
        // User ports ends

Also add two lines to the instiation of the AXI Bus Interface S00_AXI:

// Instantiation of Axi Bus Interface S00_AXI
    axi_rotary_encoder_slave_lite_v1_0_S00_AXI # ( 
        .C_S_AXI_DATA_WIDTH(C_S00_AXI_DATA_WIDTH),
        .C_S_AXI_ADDR_WIDTH(C_S00_AXI_ADDR_WIDTH)
    ) axi_rotary_encoder_slave_lite_v1_0_S00_AXI_inst (
        .encoder_A(encoder_A),
        .encoder_B(encoder_B),
        .S_AXI_ACLK(s00_axi_aclk),
        .S_AXI_ARESETN(s00_axi_aresetn),
        .S_AXI_AWADDR(s00_axi_awaddr),
        .S_AXI_AWPROT(s00_axi_awprot),
        .S_AXI_AWVALID(s00_axi_awvalid),
        .S_AXI_AWREADY(s00_axi_awready),
        .S_AXI_WDATA(s00_axi_wdata),
        .S_AXI_WSTRB(s00_axi_wstrb),
        .S_AXI_WVALID(s00_axi_wvalid),
        .S_AXI_WREADY(s00_axi_wready),
        .S_AXI_BRESP(s00_axi_bresp),
        .S_AXI_BVALID(s00_axi_bvalid),
        .S_AXI_BREADY(s00_axi_bready),
        .S_AXI_ARADDR(s00_axi_araddr),
        .S_AXI_ARPROT(s00_axi_arprot),
        .S_AXI_ARVALID(s00_axi_arvalid),
        .S_AXI_ARREADY(s00_axi_arready),
        .S_AXI_RDATA(s00_axi_rdata),
        .S_AXI_RRESP(s00_axi_rresp),
        .S_AXI_RVALID(s00_axi_rvalid),
        .S_AXI_RREADY(s00_axi_rready)
    );

Next, in the AXI Bus Interface S00_AXI source file, add two inputs:

        // Rotary encoder signals
        input wire encoder_A,
        input wire encoder_B,
        // User ports ends

Warning

There was an error here. Ofcourse you should not change the reset code, that should be set to 0.

Update the code to store the value (w_position) of the Rotary Encoder (line 262):

              default : begin
                          slv_reg0 <= w_position;
                          slv_reg1 <= slv_reg1;
                          slv_reg2 <= slv_reg2;
                          slv_reg3 <= slv_reg3;
                        end

At the bottom of the module add user logic that instatiates our Rotary Encoder module:

    // Add user logic here
    // Internal signals
    wire [7:0] w_position;
    wire direction;

    rotary_encoder rotary_encoder_inst(
        .clk(S_AXI_ACLK),
        .reset(~S_AXI_ARESETN),
        .encoder_A(encoder_A),
        .encoder_B(encoder_B),
        .position(w_position),
        .direction(direction)
    );
    // User logic ends

Next, go back to the Package IP tab and go through all packaging steps, make sure you update the files and update everything. In compatibilty you can check Package for IPI and Ignore Freq_Hz.

When everything is ok, click Review and Package and "Re-Package IP" This will add your IP to the repository of your project and close the Vivado window of the IP Packager.

Create the HW Design

Create a block design with a Zynq IP and add the previously created AXI IP, connect the interrupt to the Zynq IP. Since the IP Packager added the AXI IP to a local repo and this repo was added to your project, you can add the AXI Rotary Encoder IP by clicking on the '+' sign and by typing 'rot' you should find your new IP. Do not forget to enable the interrupt.

Build the bitstream, export the xsa (include the bitstream).

Choose if you want to work with Bare Metal (Vitis IDE) or in Petalinux.

Vitis IDE

Create a platform. Create an application that reads the value of the rotary encoder. This is an AXI IP, so it is similar to previous hands-on.

Petalinux

Create a petalinux project with the previous xsa as hw-description. Startup your Virtual Box Ubuntu 22.04 which has Petalinux 24.01 installed. The following are the commands you will need:

source /tools/petalinux/settings.sh
cd ~
mkdir petalinuxprojects
cd petalinuxprojects
petalinux-create --type project --template zynq --name HO5_ROTARY_ENCODER
cd HO5_ROTARY_ENCODER/
petalinux-config -c rootfs
ls project-spec/hw-description/drivers/axi_rotary_encoder_v1_0/
petalinux-config -c kernel
petalinux-build
petalinux-package boot --fsbl images/linux/zynq_fsbl.elf --u-boot images/linux/u-boot.elf --fpga images/linux/system.bit --force
petalinux-package wic
cp images/linux/petalinux-sdimage.wic /data/HO5_ROTARY_ENCODER/petalinux/petalinux-sdimage1.wic

The above steps will create the project, configure the project and build it. The WIC-file is a file that can be written to an SD card. Create an SD card and boot the system.

Check using devmem if you can read the value of the Rotary Encoder.

Write a program in C to process the interrupt.

Have a look at these git repositories:

Devmem

UIO

UIO interrupt