<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="Module.2004-05-17.5721">
  <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Using RTDX with a MATLAB GUI</name>
  <metadata xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <md:version xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">1.5</md:version>
  <md:created xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">2007/02/19 17:44:26 US/Central</md:created>
  <md:revised xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">2008/03/26 15:56:06.492 GMT-5</md:revised>
  <md:authorlist xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
      <md:author xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="tbshen">
      <md:firstname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Thomas</md:firstname>
      
      <md:surname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Shen</md:surname>
      <md:email xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">tbshen@uiuc.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
    <md:maintainer xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="tbshen">
      <md:firstname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Thomas</md:firstname>
      
      <md:surname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Shen</md:surname>
      <md:email xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">tbshen@uiuc.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">55x</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">DSP</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">GUI</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Matlab</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX</md:keyword>
  </md:keywordlist>

  <md:abstract xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Explains how to send and receive data with the DSP through the USB port using RTDX. Example code is shown in C as well as MATLAB for interfacing on the PC. Some example code for creating a MATLAB GUI is also shown.</md:abstract>
</metadata>

  <!--Here is where all of the code for the page goes!!!-->
  <content xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
      <!---Introduction-->
      <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="Intro">
        <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Introduction</name>
        <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="IntroPar">The USB port on the DSP box can also be used to transmit data between the DSP and the PC during real-time operation. Texas Instruments came up with Real Time Data Exchange (RTDX) to allow users real-time bidirectional exchange of data between the target and host. This allows the simulation of data input and output which can be used as feedback from the DSP for a variety of applications.  Both input and output data are buffered until read, allowing transmission of larger amounts of data.
        </para>
      </section>
      <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="Setup">
        <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Setting Up Input/Output Channels</name>
          <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="setupChannels">The RTDX works by setting up input and output channels to the DSP. RTDX functionality is supplied through the include file <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">rtdx.h</code> so be sure it is included in your <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">main.c</code> and any other files that use rtdx functions.
          </para><para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="element-50">Depending on whether there will be input and/or output from the computer in your project, add input and output channels in the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">main.c</code> file using the commands. These are declared like global variables near the top of the code after the include files. They do NOT go in the main() function or  any other function. </para><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block"><![CDATA[     RTDX_CreateInputChannel(ichan);
     RTDX_CreateOutputChannel(ochan);]]></code><para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="element-678">By default, these channels are disabled on the DSP. You may enable them in the main loop so that they will be enabled for the duration of the program. This can be accomplished with the following instructions:</para><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block"><![CDATA[     RTDX_enableInput(&ichan);
     RTDX_enableOutput(&ochan);
]]></code><para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="element-226">In other C files that utilize the declared input and output channels, you will need to declare the input and output channels again with an extern so that your files know what the variables are. </para><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block"><![CDATA[     extern RTDX_input_channel ichan;
     extern RTDX_output_channel ochan;]]></code><para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="element-515">Lastly, RTDX <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">MUST</emphasis> be manually enabled on the DSP boards. Go to <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Tools-&gt;RTDX-&gt;Configuration Control</code> and select 'Enable RTDX' in the window that opens. Another helpful window for debugging is the 'Channel Viewer Control' accessed through <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Tools-&gt;RTDX-&gt;Configuration Control</code>. This window displays the number of completed and outstanding transfers between computer and board.</para>
      </section>
      <!--DSP Side of things-->
      <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="DSPSide">
        <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Using the DSP to Access the USB Port</name>
        <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="TwoMeth">The data buffer can be written to in C, but requires the block method of input/output used in <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="http://cnx.org/content/m13809/latest/">Lab 4</link> and <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="http://cnx.org/content/m13805/latest/">Lab 5</link>. The sample-by-sample method used in Labs 1 through 3 will not work with RTDX.
            RTDX functionality is supplied through the include file <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">rtdx.h</code> so be sure it is included in your <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">main.c</code> file and any other files that use rtdx functions.</para>
        <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="CUsage">
            <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Using C to Send/Receive</name>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="CPar">
                There are several functions for transmitting and receiving data within the C environment: 
            </para>
            <list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="element-506" type="bulleted"><item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_readNB()</code> takes three arguments: the first is a pointer to the input channel, the second is a pointer to the variable in which to store read data, and the third is the number of bytes to read. This is a non-blocking read, meaning if no data is available to be read, it will merely return. However, there will then be an outstanding read and the variable will be updated when some data is finally read. It returns 'RTDX_OK' on success, '0' on failure (the target buffer is full), and 'RTDX_READ_ERROR' when the channel is currently busy reading.</item>
	<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_read()</code> also takes three inputs like <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_readNB()</code> and but on successful read, it returns the number of bytes of data actually in the buffer. The difference from <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_readNB()</code> is it's a blocking read, meaning <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_read()</code> won't return until something is read. If the channel is busy or not enabled, 'RTDX_READ_ERROR' will be returned.</item>
	<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_write()</code> takes three arguments: the first is the pointer to the output channel, the second is a pointer to the buffer containing the data to write, and the third is the size of the buffer in bytes. It returns an integer, non-zero on success and '0' on failure. </item>
        <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_sizeofInput()</code> takes a pointer to an input channel and returns the number of bytes of data actually read from the buffer. It is used in conjunction with <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_readNB()</code> after a read operation is completed.</item>
        <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_channelBusy()</code> takes a pointer to an input channel and returns an int indicating the status of the channel. A return of '0' means the channel is not busy while non-zero means the channel is busy. This is usually used in conjunction with <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">RTDX_readNB()</code> to check if another read request needs to be issued.</item></list><para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="element-128">More information about the RTDX module and the commands that can be used with it are in the <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="http://focus.ti.com/lit/ug/spru423f/spru423f.pdf">TMS320 DSP/BIOS User's Guide (spru423)</link> and the <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="http://focus.ti.com/lit/ug/spru404g/spru404g.pdf">TMS320C5000 DSP/BIOS API Reference Guide (spru404)</link>.</para>
            
            <example xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="exC1">
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SingleC">The following example shows a simple C program that will echo received data back through the output channel. This files assumes that the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">main.c</code> file has declared and enabled the input and output channels. The project file and all the necessary files are available from <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">v:/ece420/55x/block_rtdx</code>. MATLAB GUI files that are made to interface with this project are <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="rtdx_text.m">rtdx_text.m</link> and <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="rtdx_echotext.m">rtdx_echotext.m</link>.</para>
            <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block"><![CDATA[#include "dsk5510_dual3006cfg.h"
#include "dsk5510.h"
#include "swi_process.h"
#include "dsplib.h"
#include "rtdx.h"                         // Include file for rtdx functionality

extern RTDX_input_channel ichan;          //ichan has been declared in main.c
extern RTDX_output_channel ochan;         //ochan has been declared in main.c

int recvd;
int sentNew = 0 ;

// all data processing should be done in SWI_ProcessBuffer

void SWI_ProcessBuffer()
{
	static unsigned int mbox_value = 0;
	short *psrc, *pdest;

	mbox_value |= SWI_getmbox();

	// buffers are only processed when both transmit and receive are ready
	if((mbox_value & DMA_RECEIVE_DONE) && (mbox_value & DMA_TRANSMIT_DONE)) {
 		mbox_value = 0;

		// get buffer pointers
		psrc = receive_buffer[receive_buffer_to_process_index];
		pdest = transmit_buffer[transmit_buffer_to_fill_index];
		
		if (!RTDX_channelBusy(&ichan)) {     // read only when not busy
	    		RTDX_readNB(&ichan, &recvd, sizeof(recvd));
	    		sentNew = 1;
		}
		
		if (sentNew == 1) {  // echo back when data has been received
			RTDX_write(&ochan, &recvd, sizeof(recvd));
			sentNew = 0;
		}
		
		
		receive_buffer_processed = 1; // flag receive buffer as processed
		transmit_buffer_filled = 1; // flag output buffer as full
	}
}]]></code>
            
            </example>
        </section>
      </section>
      
      <!--PC Side of things-->
      <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="PCSide">
        <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Using MATLAB to Access the DSP Board (PC)</name>
        <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="MATPar">MATLAB can be used to access the data coming from the DSP board. A simple typing/echo GUI for the block_rtdx project has been provided. An interface can also be programmed in Visual Basic. The setup and transfer/receive commands for Matlab will be described below.</para>
        <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="MATWrite">
            <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Sending Data</name>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="MATWritePar">Before accessing the DSP board, it must be initialized through MATLAB.  This is done
                with this code:
                <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">h = actxserver('RTDX');</code>
                which sets the port with all necessary parameters.  The port is still not open for writing. To open the port, specify the name of the channel you would like to open in the command:
                <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">invoke(h,'Open','ichan','W');</code>
                To write to
                the buffer, you invoke h with a 'Write' parameter and the data to send.
                <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">invoke(h,'Write',int16(v));</code>
                In this case, v was a char and we wanted to send the ASCII value. (There is a limitation to the ASCII converter in Matlab: it does not take all possible key presses.) Multiple channels can be opened in this manner. When there are multiple channels open, the write will be to the most recently opened channel.
            </para>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="MATWRITE2">Before finishing a function, or before executing a read, the port should be closed.
The port is closed with the command:
                <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">invoke(h,'Close');</code></para>
        </section>

        <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="MATRead">
            <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Receiving Data</name>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="MATReadPar">To read data from the DSP, a read channel must be set up. This is done using the invoke open command with an 'R' parameter.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">invoke(h,'Open','ochan','R');</code>
Reading data from the DSP board is a little more complicated. It seems that the DSP board buffers all the data. To get the latest piece of data, you must first 'Seek' to the current message.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">[status,nummsgs] = invoke(h,'GetNumMsgs');
status = invoke(h,'Seek',nummsgs);</code>
Once at the correct message, the actual reading can be done.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">[status, values] = invoke(h,'ReadI2');</code>
As with writing, the port should be closed after reading.</para>
        </section>
        <!--
        <section id="ASMUsage">
            <name>Using Assembly to Send/Receive</name>
            <para id="ASMPar">
                Accessing the serial port in assembly comes in the form of macros (READSER and WRITSER).  
                These macros allow for multiple bytes of data to be written to the serial port
                buffer at a time.  
            </para>
            <list id="macros" type="bulleted">
            <item>
                <code type="inline">READSER</code>
                 : READSER accepts one parameter, an integer number(n).  The macro will read n bytes
                into memory and place them starting at the address <code type="inline">*AR3</code>.  The macro modifies <code type="inline">AR3</code> and it is left
                pointing to one byte past the last memory location written.  The actual number of data bytes read is
                put into <code type="inline">AR1</code>.  If <code type="inline">AR1</code> is zero, there were no available data byte sin the buffer. The calling
                format is: <code type="inline">READSER n</code>
            </item>
            <item>
                <code type="inline">WRITSER</code>
                 : Similar to the previous macro, WRITSER takes a single integer parameter n.  This macro will add
                n bytes starting at <code type="inline">*AR3</code> to the serial port buffer.  <code type="inline">*AR3</code>
                is left pointing one location after the last memory read.  This data is queued in the buffer and will
                remain there until the PC retrieves the data. The calling format is: <code type="inline">WRITSER n</code>
            </item>
            </list>
            <note type="Warning">
                READSER and WRITSER modify registers AR0, AR1, AR2, AR3, and BK as well as the flag TC. 
            </note>
            <para id="BufNote1">
                The core file allows up to 126 characters to be stored in the input and output buffers. 
                No checks to protect against buffer overflows are made, so do not allow the input and output 
                buffers to overflow. (The length of the buffers can be changed by changing <code type="inline">ser_rxlen</code> and <code type="inline">ser_txlen</code>
                values in the core.asm file.) The buffers are 127 characters long; however, 
                the code cannot distinguish between a completely-full and completely-empty buffer. 
                Therefore, only 126 characters can be stored in the buffers. 
            </para>
            <para id="BufNote2">
                It is easy to check if the input or output buffers in memory are empty. 
                The input buffer can be checked by comparing the values stored in the memory 
                locations <code type="inline">srx_head</code> and <code type="inline">srx_tail</code>; if both memory locations hold the same value, 
                the input buffer is empty. Likewise, the output buffer can be checked by comparing 
                the values stored in memory locations <code type="inline">stx_head</code> and <code type="inline">stx_tail</code>. The number of characters 
                in the buffer can be computed by subtracting the head pointer from the tail pointer; 
                add the length of the buffer (normally 127) if the resulting distance is negative. 
            </para>
            <example id="ExASm1">
            <para id="ExampleASM">
                Download the code here <link src="ser_echo.asm">ser_echo</link>
            </para>
            <code type="block">
1 .copy "v:\54x\dsplib\core.asm" 
2 
3       .sect ".data" 
4 hold  .word 0 
5 
6       .sect ".text" 
7 main 
8       stm #hold,AR3       ; Read to hold location  
9 
10      READSER 1          ; Read one byte from serial port 
11          
12      cmpm AR1,#1        ; Did we get a character? 
13      bc main,NTC        ; if not, branch back to start 
14 
15      stm #hold,AR3      ; Write from hold location 
16      WRITSER 1          ; ... one byte 
17 
18      b main 
            </code>
            <para id="CodeEx">
                On Line 8, we tell <code type="inline">READSER</code> to receive into the location hold by setting <code type="inline">AR3</code> to point at it. 
                On Line 9, we call <code type="inline">READSER 1</code> to read one serial byte into hold; the byte is placed in the 
                low-order bits of the word and the high-order bits are zeroed. If a byte was read, 
                <code type="inline">AR1</code> will be set to 1. <code type="inline">AR1</code> is checked in Line 12; Line 13 branches back to the top if no 
                byte was read. Otherwise, we tell reset <code type="inline">AR3</code> to hold (since <code type="inline">READSER</code> moved it), then call 
                <code type="inline">WRITSER</code> to send the word we received on Line 16. On Line 18, we branch back to the start to 
                receive another character. 
            </para>
            </example>
            <section id="Alter">
            <name>Alternative Method in Assembly</name>
            <para id="Alternate">
                Many students have found that there are issues with the <code type="inline">READSER</code> and <code type="inline">WRITSER</code> macros.  Performance
                of these macros is often "flaky" if they even work at all.  Two ECE 320 students 
                I-Ju Liao and Jung-Gyu Lee from the Fall 2002 semester created this alternative method which
                provides much better assembly support for serial port access.  The following is a skeleton
                for reading data from the serial port onto the DSP:
            </para>
            <example id="BetterCode">
            <para id="codexmpl">
                Skeleton of a program for receiving data over the serial port. The function 
                of interest is get_data. In this function, we first recieve one 8 bit value 
                and store it at <code type="inline">value_1</code>. Then, we receive one 16 bit value and store it at 
                <code type="inline">value_2</code>. 
            </para>
            <code type="block">
.copy "v:\ece320\54x\dsplib\core.asm" 
                                    
                                    
        .sect ".data" 
value_1 .word 0 
value_2 .word 0 
                    
                    
        .sect ".text" 
main: 
                    
loop: 
        WAITDATA 
                
        call #get_data      ; call function to get serial port data 
                    
        stm #BlockLen-1, BRC 
        rptb endblock-1 
                
        ;******your code goes here
                    
endblock: 
        b loop 


                    
get_data: 
        pshm AR0            ; we save all registers that are used in 
        pshm AR2            ; this function - note that accumulator 
        pshm AR3            ; B IS OVERWRITTEN! 
        pshm BK 
                    
        mvdm #srx_head, AR2 ; srx_head, defined in core.asm, points 
                            ; to one element past the last value 
                            ; recieved in the serial recieve buffer 
                                            
        stm #ser_rxlen, BK  ; set BK to length of receive buffer 
                        
        mar *+AR2(-4)%      ; AR2 now points to the most recently 
                            ; received block of 24 bits, i.e. one 8 
                            ; bit value and one 16 bit value 
                   
        stm #1, AR0         ; set increment 
                        
        stm #value_1, AR3   ; get first value 
        mvdd *AR2+0%, *AR3  ; save at value_1 
                        
        stm #value_2, AR3   ; get second value 
        ld *AR2+%, 8, B     ; place first 8 bits in high part of B 
        or *AR2+%, B        ; combine last 8 bits in low part of B 
        stl B, *AR3         ; save at value_2 
                    
        popm BK 
        popm AR3 
        popm AR2 
        popm AR0 
        ret
                </code>
                </example>
                <note type="Note">
                    The above program does not describe an alternative means for transmitting data 
                    from the DSP board. Some simple sleuthing in the core.asm file starting at stx 
                    head should shed some light on the subject. 
                </note>
                </section>
        </section>
        -->
      </section>
      
      <!--GUI Tutorial-->
      <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="Interface">
        <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Using MATLAB GUI Features</name>
        <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="GUIPar">MATLAB has some nice Graphical User Interface (GUI) features which can be used to control the flow of data to and from the 
            RTDX port.  The basic implementation consists of a blank window (figure) which can have different
            interface elements placed on it.  These elements can be sliders, buttons, text boxes, etc...
        </para>
        <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="moreExpl">
            When an element is accessed (for instance, a slider is moved, or a button is pushed), MATLAB will
            execute a "callback routine" which is a MATLAB function defined by the user.  Desgining these interfaces
            is simple.
        </para>
        <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="slideS">
            <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Creating a User Interface with Sliders</name>
            <list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="files" type="bulleted"><name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Download These Files</name>
                <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="rtdx_sliders.m">rtdx_sliders.m</link> - User Interface</item>
                <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="rtdx_wrt_sliders.m">rtdx_wrt_sliders.m</link> - Callback File</item>
            </list>
            <example xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="interfEx">
                <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">1   % rtdx_sliders - initializes RTDX port and sets up three sliders
2
3   h = actxserver('RTDX');
4
5   % open a blank figure for the slider 
6   Fig = figure(1); 
7   % open sliders 
8   % first slider 
9   sld1 = uicontrol(Fig,'units','normal','pos',[.2,.7,.5,.05],... 
10       'style','slider','value',4,'max',254,'min',0,'callback','rtdx_wrt_sliders'); 
11
12   % second slider 
13   sld2 = uicontrol(Fig,'units','normal','pos',[.2,.5,.5,.05],... 
14       'style','slider','value',4,'max',254,'min',0,'callback','rtdx_wrt_sliders'); 
15
16   % third slider 
17   sld3 = uicontrol(Fig,'units','normal','pos',[.2,.3,.5,.05],... 
18       'style','slider','value',4,'max',254,'min',0,'callback','rtdx_wrt_sliders');                </code>
            </example>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="explEx">Lines 9 through the end create the three sliders for the user interface. Several 
                parameters are used to specify the behavior of each slider. The first parameter, 
                Fig, tells the slider to create itself in the window we created in Line 6. The rest 
                of the parameters are property/value pairs: 
                <list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="params" type="bulleted"><item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">units</code>: Normal tells Matlab to use positioning relative to the window boundaries. </item>
                    <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">pos</code>: Tells Matlab where to place the control.</item>
                    <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">style</code>: Tells Matlab what type of control to place. slider creates a slider control.</item>
                    <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">value</code>: Tells Matlab the default value for the control. </item>
                    <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">max</code>: Tells Matlab the maximum value for the control.</item>
                    <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">min</code>: Tells Matlab the maximum value for the control.</item>
                    <item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">callback</code>: Tells Matlab what script to call when the control is manipulated. rtdx_wrt_sliders is a Matlab file that writes the values of the controls to the RTDX port. </item>
                </list>
                Every time a slider is moved, the rtdx_wrt_sliders.m file is called: 
            </para>
            <example xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="cllbk">
                <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">1   % rtdx_wrt_sliders : writes values of sliders out to rtdx
2   
3   % open rtdx port for data transfer
4   status = invoke(h,'Open','ichan','W');
5   
6   % send value from sld1
7   v1 = round(get(sld1,'value')); 
8   status = invoke(h,'Write',int16(v1));
9   
10   % send value from sld2
11   v2 = round(get(sld2,'value')); 
12   status = invoke(h,'Write',int16(v2));
13   
14   % send value from sld3
15   v3 = round(get(sld3,'value')); 
16   status = invoke(h,'Write',int16(v3));
17
18   % send reset pulse
19   status = invoke(h,'Write',int16(2989));
20
21   % close rtdx port
22   status = invoke(h,'Close');</code>
            </example>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="secondexExpl">Line 7 retrieves the value from the slider using the get function to retrieve 
                the value property. The value is then rounded off to create an integer, and the integer 
                is sent as an 16-bit quantity to the DSP in Line 8. The other 
                two sliders are sent in the same way. Line 19 sends 2989 to the DSP, which can be used to 
                indicate that the three previously-transmitted values represent a complete set of data points. ( You can use whatever value you want. )
                This can be used to prevent the DSP and Matlab from losing synchronization if a transmitted 
                character is not received by the DSP and provides some error detection. 
                <note xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="note">Line 22 closes the RTDX port. Make sure you close the port after sending a data block to the DSP. 
                </note>
            </para>
        </section>
        <section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="advanced">
            <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Advanced features</name>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="adv">The slider example shows some basic features of the gui tools.  The handle for the RTDX server is generated
                into the workspace so that it can be used for writing.  But other elements, such as text boxes
                cannot be dealt with as easily.  The Parameters from these can be accessed through their returned
                handles.  Some examples:
            </para>
            <example xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="buttons">
            <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">
%GUI.m
%****Sample GUI, Text and a Button***

%open a blank figure
Fig = figure(1);
set(Fig,'Name','Test GUI');

%Space to enter text
ed2 = uicontrol(Fig,'backgroundcolor','white','units','Normalized','pos',[.1,.6,.4,.05],...
                'string','Default Text','style','edit');

%Button
but1 = uicontrol(Fig,'foregroundcolor','blue','units','Normalized','pos',[.1,.4,.5,.1],...
                'string','Press Me!','style','pushbutton','callback','SampleGUI');
            </code>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="explainCode">
                A Text box is created with default text in it that says: "Defaul Text".  A button is also created, 
                which when pressed, will execute the callback function SampleGUI.m
            </para>
            <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">
%SampleGUI.m

%Get Text
testText = get(ed2,'string')
            </code>
            <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="text">Now testText holds whatever string was entered into the text box.  The function <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">get()</code>
                is used to retrieve the data from the 'string; parameter in the ed2 handle.  MATLAB <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="inline">help uicontrol</code>
                gives the full list of options for interface elements.
            </para>
            </example>
        </section>
      </section>
  </content>
  
</document>
