EE265 Lab 2: Working with Hardware

Winter 2008-2009
Instructor: Teresa Meng


I. Purpose:

This lab introduces real-time programming on the C5509 EVM board. Hardware-related issues, real-time programming concepts, constraints, and limitations will be discussed. We will take a closer look at the EVM board by generating a waveform and observing the output. The waveform that is generated will also be fed back to the input of the EVM board to be digitized and analyzed. This effectively measures the distortion introduced by the Analog Interface Circuitry (AIC) on the EVM board. This lab will attempt to relate these experiments to what you learned from the lecture on analog interface and signal reconstruction. Finally, this lab will introduce some experiments on sampling theory and quantization to give you an idea of how they affect the signals.

II. Introduction:

A. Real-Time Processing

The main purpose of this lab is to introduce real-time programming on the TI C5509 evaluation module (EVM) board. The method we introduce here will be used in the rest of this lab course. In order to do real-time programming, we must be able to acquire real-time inputs and generate real-time outputs. In the lab exercises we will do, we want to use analog signals (such as an audio source) as our input and we would like to generate analog signals (audio signal) as our output. The C5509 itself does not have any analog inputs or outputs, so it can only deal with digital input and outputs. This means that any inputs must be converted to digital form before being passed to the C5509. Also, any output from the C5509 can only be digital, so some external hardware must be needed to convert the digital output to analog form. This means that the analog interfacing is actually handled by hardware on the EVM board.

 

This process ensures that the application is not notified until all required conditions are met, so the input and output pipes are effectively synchronized.

 

B. Analog Interface

The C5509 EVM board uses the AIC23B. Detailed information on the AIC is given in TLV320 AIC23B Data Sheet. You will not need most of the information given in the data sheet as the data sheet is intended for the board designers.

 

III. Readings:

Section 2.2, "Pipe or PIP Module"

Section 3.1, "About the Example"

Section 3.3.2 "Audio Sampling Rate"

IV. Lab Exercises:

This lab will take you step by step through setting up a real-time DSP code capable of processing audio-band signals. Once the framework for handling real-time data is setup, you will then implement a waveform generator to generate a sine wave to the output of the DSP. You will also write a waveform recorder to record signals and see the effect of the sampling/quantization process on their spectrum. Finally, you will play with different sampling rates and quantization schemes to see for yourself what kind of effects they introduce.

A. Setup

Although the DSP/BIOS can assist you in configuring the processor, the interrupts, and the serial ports, the DSP/BIOS still requires a fair amount of setup work in order to get it working correctly. This section goes through the setup step by step. We could have provided you an entire project template with all of this done for you, but it is important for you to understand what goes on behind the scene since you will be repeating the same procedure in later labs. The following sections describe how to set up the DSP/BIOS to read data to/from the analog interface chip, and how to configure the various program elements that are used for the rest of the lab.

 

Setting up the Project

Follow the procedure given in lab 1 to start CCS.

Create a new project in the lab2/audio directory and call it audio.

The assembly template is provided in audio_tmpl.zip. First, download and decompress these files to your lab2/audio directory. Do not put the files in an "audio_tmpl" subfolder.

Add the following source files to the project. Select Project->Add Files to Project... to add source files. If you don't see the source files listed, change the field Files of type: to the appropriate type. For example, for .s55 (linear assembly) codes, set the type as Asm Source Files (*.a*,*.s*).

Notice that the automatically generated audiocfg* files are added to your source code when you add audio.cdb.

Select Project->Scan All Dependencies. What this does is it updates the include folder to reflect any header files that you have included in your source codes. If you expand the include folder by clicking on the plus next to the Include folder, you will see a very long list of files. Most of this is from DSP/BIOS. Remark: From here on for the sake of conciseness, the term "expand" will be used to denote the action of clicking on the "plus" symbol next to the item to be expanded.

Completing the DSP/BIOS Configuration

We need to finish configuring the DSP/BIOS. Double click on the audio.cdb file (located under the DSP/BIOS Config folder).

 

 

 

 

B. Digital Audio Loop-back

This exercise will be the first to use IO in this lab, so we will merely verify their use. An audio source is generated on the host computer and this is connected to the input of the EVM board. The loop-back is done digitally by copying the data from the receive serial port to the transmit serial port. You can check the functionality of this loop-back by listening to the output of the EVM board. A better way to check the functionality is to verify numerically that the input samples are copied to the output (the source (src) buffer frame gets copied to the destination (dst) buffer frame).

C. Sine Wave Generator

In this portion of the lab, you will write a sine wave generator to output a pure digital sine wave from the EVM board. This section is intended to exercise your assembly programming skills with a simple exercise of copying data from the data memory into the data transmit buffer, pointed to by dst. A template for the assembly routine is provided for you but you must complete the code and generate a sine wave at the output of the EVM.

1. Setup

extern Void sinegen_proc_init(Void);
extern Void sinegen_proc(Void);
sinegen_proc_init();

Note that there is no underscore, "_", preceding the function name, sinegen_proc_init, but in sinegen_proc.s55, this function does have the underscore. This is simply the convention used to call C functions from assembly, but the underscore is not needed in C.

In the processBuffer () function, replace the for loop with the following statement:

sinegen_proc();

So rather than copying the data from the src buffer to the dst buffer in the C code, you are calling a function (which in this case is written in assembly), sinegen_proc(), to process the src and dst buffers.

2. Coding

3. Debugging with the CCS Graphing Feature

·         One of the most helpful debugging tools that we have available is the graphing feature of Code Composer Studio. This allows us to graphically plot out the values in region of memory (which can be very helpful for sinewave generation). Here is a brief description of how to graph the dst buffer:

o        Select View->Graph->Time/Frequency. A Graph Property dialog box will pop up. Change the following settings:

§         Graph Title - dst buffer. This is the label you have chosen to identify the graph window.

§         Start Address - dst. This is the label you have chosen for starting address of your data buffer.

§         Acquisition Buffer Size - 192. This is the size of the data buffer. 

§         Display Data Size - 96. This specifies the portion of the data buffer you would like to have plotted. The number specified here must be smaller than the acquisition buffer size.

§         DSP Data Type - 16-bit signed integer. This is the default type for digitized samples.

§         Click on OK to close this dialog box. The data in your dst pipe frame will be plotted with respect to time.

o        This will allow you to view the frame pointed to by dst. Keep in mind that dst switches between input frames, so the graph will actually show you different frames at different times.

o        The receive pipe can be viewed by graphing src.

 

4. Profiling

 


D. Audio Recorder

Once you have completed and tested your sine wave generator, you will implement an audio recorder. You will then analyze the audio samples using the CCS graphing tool to view the time and frequency spectrum of the samples. You will also perform some simple experiments to analyze the physical signals.

1. Coding Specifications

 

2. Debugging using CCS Features

·         Graphing:
As described above, the graphing feature of CCS can be one of the most helpful debugging tools that we have available. You can use the graphing feature to plot the values directly, or you can also make other plots (such as the FFT of the values). The following steps will guide you through the process of plotting the recorded signal directly and also plotting its FFT magnitude:

o        To test your code, connect an audio source and start the music, as described previously.

o        Compile, reset DSP, load the program onto the EVM, and run the program.

o        Stop the program shortly (after few seconds - to be sure that the buffer is filled with data samples).

o        Select View->Graph->Time/Frequency. A Graph Property dialog box will pop up. Change the following settings:

§         Graph Title - Amplitude vs. Time

§         Start Address - recordData. This is the label you have chosen for starting address of your data buffer.

§         Acquisition Buffer Size - 1024. This is the size of the data buffer.

§         Index Increment - 2. This is because the buffer format is stereo, L/R interleaved format.

§         Display Data Size - 512. This specifies the portion of the data buffer you would like to have plotted. The number specified here must be smaller than the acquisition buffer size.

§         DSP Data Type - 16-bit signed integer. This is the default type for digitized samples.

o        Click on OK to close this dialog box. The data in your record buffer will be plotted with respect to time.

 

 

3. Verification using MATLAB

Although CCS has many useful debugging features, it cannot help you with all types of analysis that you may wish to perform. For example, in Lab 1 you were asked to write code that implemented an FIR filter. The CCS debugging features were helpful in verifying individual operations, but ultimately CCS could not verify that the computed result was correct since it has no way of computing the FIR result on its own. Most of you probably used MATLAB to determine what the FIR output should have been and then used this to compare to the output that your code generated. Indeed, MATLAB is almost ideal for verifying code, since it has plenty of computational power to perform all sorts of analyses on data. In this lab we will introduce a method for using MATLAB to verify your DSP code to ensure that it is working correctly. The verification can be used to determine with great certainty whether or not your algorithms are computationally correct. This method may be useful for debugging, but since it is less interactive than working in CCS, it may be most useful for verification only after you are pretty sure that the code is working correctly.

MATLAB/CCS Link:
You will be performing the verification of your DSP code by using the MATLAB Link for Code Composer Studio. The Link for Code Composer Studio is a toolbox available for MATLAB that lets you link to your DSP processor so that you can run and test your programs from within MATLAB. The link allows you to do many CCS actions: you can run CCS, load projects, build executables, load executables, execute code, set breakpoints, read memory and write to memory on the DSP. These capabilities allow us to write MATLAB scripts that perform verification of our CCS projects. A typical script will perform the following actions:

CCS Tutorial:
There are several tutorials for the Link to CCS that are built in to MATLAB. The simplest of which is a script named ccstutorial.m and it can be run from the MATLAB command prompt by typing ccstutorial. The tutorial covers the basic operations that are outlined above, but it also goes into detail about how to access data from C language constructs such as structs and strings. Since this is beyond the scope of this lab, you will run a modified version of this script, ccstutorial_brief.m. Here are instructions on how to run the script:

MATLAB/CCS Help:
Now that you’ve seen some of the MATLAB involved in verification, you may want to take a look at the documentation for the functions. There is a fair amount of documentation available for the CCS Link functions (and any MATLAB function in general). We do not suggest that you look through the documentation first, because you will probably learn faster by going through the tutorial above and the two examples that will follow below. However, as you go through the next two examples, you may have some questions about the details of a function or you might want to know what other capabilities are available in the CCS Link. Here are some suggestions of where to look for help in MATLAB:

Lab 1 Example Script:
The tutorial script should familiarize you with the basic operations for verifying DSP code. To give you better example of how to apply this to your lab assignments, we have provided a script that verifies the operations of a particular solution to the Lab 1 assignment. You will be expected to execute this example script and also to step through it in the MATLAB debugger so that you can see the commands used for the verification. Here are instructions for this:

Note that the board selection process in this example is much simpler than the process demonstrated in the tutorial. This is because we only have one board and one processor, so we can choose them as board 0 and processor 0 without checking what is available on our system. In order to fully understand the verification script, there are three places to keep your eye on:

Lab 2 Loopback Example Script:
The Lab 1 example script should give you a pretty good idea of how to use the CCS Link to verify your code. However, it did not use the data pipes so it is not a great example of how this can be applied to real-time code. To help you with verifying real-time code, we have also provided another example script that uses the pipes. This script will verify a basic loopback example which copies the input frame directly to the output frame (the lab2b code). Here are instructions to use the script:

Verifying the Recorder code:
By now, you should have a pretty good idea of how the MATLAB/CCS Link can be used to verify code. You now must create your own script (or modify one we’ve provided to you) to verify your recorder code. Here are the requirements of the script:

Hints and possible problems:

 

 

4. Profiling

There are a number of metrics for determining the performance of DSP codes. First, the code must meet real-time requirements meaning that it has to work without missing real-time data. Another metric is code size. Code size is important in large systems where program memory is limited. Code size is also closely related to execution cycles in programmable processors. Smaller code size requires less cycles to execute. But the execution time is also determined by other factors such as the choice of algorithm. And the choice of algorithm is also closely related to the output precision of your code (as we will see in later labs). There is no clear-cut boundary between these metrics and writing good DSP codes require taking everything into consideration.

This section goes through how you would profile your code to determine its execution time. You will use the profiling tools to analyze your code and report the results in your write-up.

Profiling has been discussed in the CCS tutorial. CCS supports many ways of performance evaluation but most of them cannot be used in real-time since they would interfere with real-time data transfer. In addition, the profiling mechanism introduces significant amount of overhead that makes it difficult to determine an accurate cycle count between the profiling points.

For complex systems, profiling will be done on a non-real-time code first (as discussed in the CCS tutorial) and the code will then be rewritten to incorporate real-time data transfer. In this course, the labs will be simple enough that you will not need to write different codes and profiling will be done primarily on the critical sections. You will do profiling one block at a time to report the execution time.

To accurately profile a section of your code, you need to subtract out the overhead associated with extracting profiling statistics from the EVM board and transferring them to the host computer. A procedure for accurate profiling is described in the CCS User's Guide accessible through the EE265 Navigation Toolbar - On-Line Line Programming Reference. Read the section Profiler->Profile Clock Accuracy.

When you measure the cycles of a function, there may be possibility of interrupt during the execution of your function. Since what you need to profile is just the execution cycles of your own function, it isn’t a good situation. In C55x.h ( it is in C:\CCStudio_v3.1\C5500\cgtools\include ), the following two functions are defined.

¡        void            _enable_interrupts(void);

¡        unsigned int  _disable_interrupts(void);

_disable_interrupts() function disables all the interrupts, while _enable_interrupts() re-enable the disabled interrupts. For the profiling purpose only, you could disable interrupts, measure the cycles from your function call to the _enable_interrupts() call, and enable interrupts again. This will give you more accurate cycles. (without this, the cycles measured may vary significantly from time to time). Don’t forget to remove these interrupts things after measuring cycles.

 

E. Sampling and Quantization

At this point, there is a lot of things you can do with the DSP board. Here, you will experiment with the different sampling rates and implement a couple of quantization schemes to see how these affect the signals.

1. Changing the Sampling Rate

In this exercise you will get experience with changing the sampling rate of the DSP processor. The procedure for changing the sampling rate of the AD50C AIC is provided below.

2. Quantization

Here, you will look at the effects of quantization on a signal. You will use MATLAB to code a uniform quantizer and compare it to a non-uniform quantizer. You will then examine the frequency contents of the output of both types of quantizers and discuss the results.

·          

o        Create a new directory called lab2/u-quan.

o        Download the file uniform.m to your lab2/u-quan directory.

§         This is a starter script for your quantizer. (Read the comments in the code for more info.)

§         This script reads in a signal from a wavefile and also writes the quantized data to a wavefile so that the quantized signal can be compared to the original.

o        Download the files sample1.wav, sample2.wav , and sample3.wav to your lab2/u-quan directory.

o        Modify the uniform.m script to implement a uniform quantizer with the specified number of bits (see the starter file).

§         The number of bits used for quantization limits the number of discrete levels in the quantized signal.  Your quantizer must ensure that the signal cannot assume any more than 2^(quantBits) discrete values.

§         The starter script creates an input signal (un-quantized) within the range of -1.0 to +1.0. Your quantizer should convert this to some range of integer values. For example, an 8-bit quantizer should have quantized samples in the range of -128 to +127.

§         CAUTION: MATLAB does all calculations in floating point, so you will need to make sure that your values are properly quantized as integers (using the floor, ceil, or round functions).

§         The quantized signal must be converted back to the range of -1.0 to +1.0 before writing the signal out as a wavefile (wavwrite).

o        Quantize the audio files, sample1.wav, sample2.wav , and sample3.wav.

o        For each audio file, vary the number of bits used for encoding and report the number of bits used with which you can distinctly hear artifacts of quantization.

o        Write a MATLAB script to perform non-uniform quantization (mu-law companding) as specified below. Name this file, lab2/u-quan/mulaw.m (i.e. same directory as the uniform quantizer MATLAB code). Refer to TI's appnote for more information on the details of the non-uniform quantizer (mu-Law compression and expanding).

§         Compression:

1.      The input/output relation is given by the following expression (for -1 <= x <= 1):
c(x) = sign(x)*ln(1 + µ · abs(x)) / ln(1 + µ)

2.      Set µ = 255.

3.      The output c(x) will be a normalized, compressed value, taking on values from -1.0 to 1.0.

§         Quantization:
Now that the signal has been compressed, it can be quantized linearly to a fixed number of levels. By linearly quantizing the compressed input, we effectively perform non-uniform quantization on the original input. This is the critical step in which we force our input values (which are still continuous) to take on a fixed number of output values.

1.      We will assume an 8-bit output (i.e. there are a total of 256 output levels), which means that c(x) should be linearly quantized to take on 256 values.

1.      One way to do the quantization is to assume that 1 bit is used for the sign of c(x) and the remaining 7 bits are used to represent and integer between 0 and 127. This is the method used in the TI's appnote.

2.      Alternatively, this could be done using something equivalent to the uniform quantizer.

2.      Scale and linearly quantize the normalized c(x) so that it takes on the range of -128 to 127 (in integer form).

3.      This gives us the input compressed into an 8-bit encoded form.

§         Expansion:
In order to retrieve the original waveform, we will need to reverse the above processes. This will restore the waveform to its natural (uncompressed) format so that we can compare the resulting signal to the un-quantized signal as well as the uniformly-quantized signal.

1.      First, convert the quantized c(x) back to the range from -1.0 to 1.0

2.      Now apply the inverse of the compression formula shown above to restore the signal. Make sure you handle the case for negative x correctly!

o        Use the non-uniform quantizer to quantize the audio test samples, sample1.wav, sample2.wav , and sample3.wav with your MATLAB script. Note: you only need to do 8-bit mu-law quantization.

o        For each of the audio examples, report the effective number of bits the uniform quantizer would require to achieve the same (subjective) quality output as the 8-bit non-uniform quantizer.

o        Side note: You can do a lot of interesting analysis with the MATLAB codes. For example, you can analyze the error introduced by quantization (using the mean-square error of the quantized results, for example). Note that the error introduced is itself a function of the signal statistics.

·          


Last modified: 9:00 am  02/24/2007