File Exchange

image thumbnail

HDL Coder Self-Guided Tutorial

version 1.35 (1.97 MB) by Jack Erickson
Learn how to deploy an algorithm to an FPGA using MATLAB and Simulink.

108 Downloads

Updated 25 Jan 2021

From GitHub

View Version History

View license on GitHub

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This tutorial will guide you through the steps necessary to implement a MATLAB algorithm in FPGA hardware, including:
* Create a streaming version of the algorithm using Simulink
* Implement the hardware architecture
* Convert the design to fixed-point
* Generate and synthesize the HDL code

Cite As

Jack Erickson (2021). HDL Coder Self-Guided Tutorial (https://github.com/mathworks/HDL-Coder-Self-Guided-Tutorial), GitHub. Retrieved .

Comments and Ratings (27)

Jack Erickson

Hi Maya - is it possible that you running a version of MATLAB that's older than R2016a? That release was when the Dataset signal format was introduced, which uses the Values method to fetch values. The getLogged function (which is in the work directory so you can view/edit it) does error checking to make sure the signal got logged. So if it made it past the error checking and it still says that the Values method is not recognized, I think an old version is the cause. You could change that functionality yourself in the MATLAB script to use the old ModelDataLogs format, more on the differences here in the docs: https://www.mathworks.com/help/simulink/ug/enabling-signal-logging-for-a-model.html?s_tid=srchtitle#mw_b85e462f-81d4-47fa-a3e8-1257f9e53a01

Maya Jubran

Hi , This is a good tutorial to start implementing my algorithms in HDL.I have run into some problems in step 2.5 - compare architecture model to matlab reference. When I run the file pulse_detector_v2_tb.mlx on the test bench , I get the following error:
Error using getLogged (line 18) : Unrecognized method, property, or field Values for class 'Simulink.SimulationData.Dataset'
line 18 of the code - reading the magnitude squared output:
MagSqSL = getLogged(slout, 'mag_sq_out) .
I checked the connection of the compute power block and its in and out ports - they are all correct.
What can I do to correct this error??
Thank you
MJ

Jack Erickson

Hi Lok Yan - not sure what's going on there. The data types are set in the _tb.mlx script, where you set fxpt_mode. These types get used in the block parameters for the FIR Filter, Data Type Conversion block, and Data Type Duplicate block. You might want to double-check all that. As for "Numerator", that indicates there's a type mismatch in a division operation somewhere. The only division operation should be in the MATLAB Function block, WindowLen/2, but WindowLen is already set in pulse_detector_reference.mlx. Hope that helps you debug your design.

Lok Yan Cheng

Hi Jack, thank you so much for your help with my earlier query. I'm now stuck on step 3 of the tutorial - the line slout= sim('pulse_detector_v3') in pulse_detector_v3_tb.mlx shows the error "Expected Numerator to be one of these types: double, single, uint8, uint16, uint64,... etc Instead its type was Simulink.NumericType". My simulink models show all data types to be doubles, even with the Signal from Workspace using the input true(size(RxSignal)) which should give a boolean, I'm not sure if the error is related to this. I tried overriding control data types by executing the command
set_param('MyModel','DataTypeOverride','Off'), however all the data types are still doubles. Do you have any advice on how to go about debugging this, thanks again!

Jack Erickson

Hi Kyotani-san - pulse_detector_reference.mlx should be in the same folder as pulse_detector_v1.mlx. So make sure you run from that folder, or have that folder on your MATLAB path. If it is not there, then perhaps try downloading the kit again.

Takamasa Kyotani

The bench script "pulse_detector_v1.mlx " doesn't work. I got the error message 'The function or variable 'pulse_detector_reference' is not recognized'.

Jack Erickson

Hi Lok Yan - please make sure you are using the Signal from Workspace block from the DSP System Toolbox library, not the From Workspace block from the Simulink library.

Lok Yan Cheng

Hi, I keep getting the error in the beginning of the tutorial saying "Unsupported input format for From Workspace block" - is the data in meant to be complex? How can I get this to work with the For Workspace block? Thanks.

Brian Farmer

Hi Jack: Your location result for peak is "location" and "location_2", both from "MATLAB Golden Reference" "pulse_dector_reference.mlx" and within "MATLAB Golden Reference" "Hardware friendly implementation of peak finder", respectively. Wouldn't it make more in line with what you are demonstrating if the Simulink model, i.e. "pulse_detector_v1.slx" 1 thru 4; had an output of the peak location? That way when you compare locations at the end of your MATLAB testbench, it will make more sense with what you are trying to compare (Golden verse Simulink, not Golden versus a Golden add on hardware friendly script portion). Thank you for comparing the peaks.

Would be nice if you also guided us through synthesis and simulation, using HDL Coder - possibly Workflow Advisor.

John Abh

Hi, I am using MATLAB 2015b, is there any way to get the simulink model files for MATLAB 2015b (because later version model will not work on earlier version of MATLAB). Otherwise can anybody covert these files to 2015b version and upload the same in some where and will provide me the link.

Curie Chung

Hi Jose - You can now download the R2018b tutorial files from GitHub:
https://github.com/mathworks/HDL-Coder-Self-Guided-Tutorial/releases

Hi Gill - Are you using R2018b or R2019b? The error is not expected for R2019b, even though the instructions have not been updated. Please feel free to contact us if you need further assistance.

Curie Chung

Hi Gill - Are you using R2018b or R2019b? The error is not expected for R2019b, even though the instructions have not been updated. Please feel free to contact us if you need further assistance.

Gill Duykers

I am using R2019b and have the same error posted by Jose Caballero on 7 Jan 2020. I have downloaded the 14Feb2020 version from Github but still get the error reported about dot indexing not supported for this data type (line 11).
Was there a fix and I missed it? Thanks for any help on this.

Ryan Bosley

Excellent intro to using Simulink/HDL Coder for signal processing!
Thanks for posting!!

Jose Caballero

Hi, is there any way to easily adapt this tutorial to R2018b? I am trying to run the testbench at step 7 and get the error:

Warning: Unconnected output line found on 'pulse_detector_v1/Discrete FIR Filter' (output port: 1)
> In pulse_detector_v1_tb (line 8)
Dot indexing is not supported for variables of this type.

Error in pulse_detector_v1_tb (line 11)
FilterOutSL = squeeze(slout.logsout.getElement('filter_out').Values.Data);

Jing Ma

Jack Erickson

Hi Suranga - thanks for pointing that out. In 2019a Simulink changed the way it names the simulation workspace data. We have updated the kit for 2019a.

Suranga Handagala

The example doesn't work with 2019a. I get the error "Undefined variable "logsout" or class "logsout.getElement"" when trying to run the model.

Meabh Loughman

Figured out the answer to my own question - posting in case some one else makes the same mistake. I used the first simulink "from workspace" I saw. That block , from a comment in this link https://uk.mathworks.com/matlabcentral/answers/359452-error-input-uint8-simulink "From Workspace blocks are not able to import "just plain data". From Workspace blocks are intended to deliver data at a particular time: they are blocks for execution of a model, where as importing plain data is something that would be used in a procedural language.”".

As it turns out there are two simulink from workspace block. One specifies “signal from workspace” and can be used to import “just plain data”.

Summarily, make sure to use "signal from workspace". You'll get an error otherwise.

Raghu Sivakumar

Stephan van Beek

Pablo Romero

Werner Bachhuber

Eric Cigan

Steve Kuznicki

Eric Cigan

Very helpful step-by-step guide for users new to HDL Coder.

Stephan van Beek

MATLAB Release Compatibility
Created with R2020b
Compatible with R2018a to R2020b
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

pulse_detector/work