This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28020: Using SCI for receiving messages of lengths from 1-20 bytes

Part Number: TMS320F28020
Other Parts Discussed in Thread: C2000WARE

Hi All,

I'm looking for some advice on a good way to implement the SCI module to receive messages ranging from 10 to 20 bytes.  I acknowledge the need for a parsing routine to discern each message, but that doesn't seem challenging since each message ends with a carriage return and line feed.  I'm just looking for help in receiving the data into an array.

I've imported the Driverlib example code, "Example_F2802xSci_FFDLB.c" from TIREX, and it is talking to external devices.  However, that code uses a four level fifo, which doesn't seem optimal for receiving varying length messages.  Here's the code for reference:

//#############################################################################
//
//  File:   Example_F2802xSci_FFDLB.c
//
//  Title:  F2802x Device SCI FIFO Digital Loop Back Test.
//
//! \addtogroup example_list
//!  <h1>SCI FIFO Digital Loop Back</h1>
//!
//!   This test uses the loopback test mode of the SCI module to send
//!   characters starting with 0x00 through 0xFF.  The test will send
//!   a character and then check the receive buffer for a correct match.
//!
//!   Watch Variables:
//!   - LoopCount - Number of characters sent
//!   - ErrorCount - Number of errors detected
//!   - SendChar - Character sent
//!   - ReceivedChar - Character received
//
//#############################################################################
// $TI Release: F2802x Support Library v3.05.00.00 $
// $Release Date: 10-19-2021 $
// $Copyright:
// Copyright (C) 2009-2021 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
//   Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// 
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the   
//   distribution.
// 
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################

//
// Included Files
//
#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File
#include <stdio.h>
#include <file.h>

#include "common/include/adc.h"
#include "common/include/clk.h"
#include "common/include/flash.h"
#include "common/include/gpio.h"
#include "common/include/pie.h"
#include "common/include/pll.h"
#include "common/include/sci.h"
#include "common/include/wdog.h"

//
// Function Prototypes
//
void scia_init(void);
void scia_fifo_init(void);
void scia_xmit(int a);
void error(void);

//
// Globals
//
uint16_t LoopCount;
uint16_t ErrorCount;

ADC_Handle myAdc;
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
SCI_Handle mySci;

//
// Main
//
void main(void)
{
    uint16_t SendChar;
    uint16_t ReceivedChar;

    CPU_Handle myCpu;
    PLL_Handle myPll;
    WDOG_Handle myWDog;

    //
    // Initialize all the handles needed for this application
    //
    myAdc = ADC_init((void *)ADC_BASE_ADDR, sizeof(ADC_Obj));
    myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
    myCpu = CPU_init((void *)NULL, sizeof(CPU_Obj));
    myFlash = FLASH_init((void *)FLASH_BASE_ADDR, sizeof(FLASH_Obj));
    myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));
    myPie = PIE_init((void *)PIE_BASE_ADDR, sizeof(PIE_Obj));
    myPll = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));
    mySci = SCI_init((void *)SCIA_BASE_ADDR, sizeof(SCI_Obj));
    myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));

    //
    // Perform basic system initialization
    //
    WDOG_disable(myWDog);
    CLK_enableAdcClock(myClk);
    (*Device_cal)();

    //
    // Select the internal oscillator 1 as the clock source
    //
    CLK_setOscSrc(myClk, CLK_OscSrc_Internal);

    //
    // Setup the PLL for x10 /2 which will yield 50Mhz = 10Mhz * 10 / 2
    //
    PLL_setup(myPll, PLL_Multiplier_10, PLL_DivideSelect_ClkIn_by_2);

    //
    // Disable the PIE and all interrupts
    //
    PIE_disable(myPie);
    PIE_disableAllInts(myPie);
    CPU_disableGlobalInts(myCpu);
    CPU_clearIntFlags(myCpu);

    //
    // If running from flash copy RAM only functions to RAM
    //
#ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif

    //
    // Setup GPIO
    //
    GPIO_setPullUp(myGpio, GPIO_Number_28, GPIO_PullUp_Enable);
    GPIO_setPullUp(myGpio, GPIO_Number_29, GPIO_PullUp_Disable);
    GPIO_setQualification(myGpio, GPIO_Number_28, GPIO_Qual_ASync);
    GPIO_setMode(myGpio, GPIO_Number_28, GPIO_28_Mode_SCIRXDA);
    GPIO_setMode(myGpio, GPIO_Number_29, GPIO_29_Mode_SCITXDA);

    //
    // Setup a debug vector table and enable the PIE
    //
    PIE_setDebugIntVectorTable(myPie);
    PIE_enable(myPie);

    LoopCount = 0;
    ErrorCount = 0;

    scia_init();                // Initialize SCI for digital loop back
    scia_fifo_init();           // Initialize the SCI FIFO

    //
    // Send a character starting with 0
    //
    SendChar = 0;

    //
    // Send Characters forever starting with 0x00 and going through 0xFF.
    // After sending each, check the receive buffer for the correct value
    //
    for(;;)
    {
        SCI_putDataBlocking(mySci, SendChar);

        while(SCI_getRxFifoStatus(mySci) == SCI_FifoStatus_Empty)
        {
            
        }

        //
        // Check received character
        //
        ReceivedChar = SCI_getData(mySci);
        if(ReceivedChar != SendChar)
        {
            error();
        }

        //
        // Move to the next character and repeat the test
        //
        SendChar++;
        
        //
        // Limit the character to 8-bits
        //
        SendChar &= 0x00FF;
        LoopCount++;
    }
}

//
// Step 7. Insert all local Interrupt Service Routines (ISRs) and 
// functions here:
//

//
// error - 
//
void
error(void)
{
    ErrorCount++;
    __asm(" ESTOP0");       // Uncomment to stop the test here
    for (;;)
    {
        
    }

}

//
// scia_init - 
//
void
scia_init()
{
    CLK_enableSciaClock(myClk);

    //
    // 1 stop bit,  No loopback, No parity,8 char bits, async mode,
    // idle-line protocol
    //
    SCI_disableParity(mySci);
    SCI_setNumStopBits(mySci, SCI_NumStopBits_One);
    SCI_setCharLength(mySci, SCI_CharLength_8_Bits);

    //
    // enable TX, RX, internal SCICLK, Disable RX ERR, SLEEP, TXWAKE
    //
    SCI_enableTx(mySci);
    SCI_enableRx(mySci);
    SCI_enableTxInt(mySci);
    SCI_enableRxInt(mySci);

    SCI_enableLoopBack(mySci);

    //SCI BRR = LSPCLK/(SCI BAUDx8) - 1
#if (CPU_FRQ_50MHZ)
    SCI_setBaudRate(mySci, SCI_BaudRate_9_6_kBaud);
#elif (CPU_FRQ_40MHZ)
    SCI_setBaudRate(mySci, (SCI_BaudRate_e)129);
#endif

    SCI_enable(mySci);

    return;
}

//
// scia_fifo_init - Initialize the SCI FIFO
//
void
scia_fifo_init()
{
    SCI_enableFifoEnh(mySci);
    SCI_resetTxFifo(mySci);
    SCI_clearTxFifoInt(mySci);
    SCI_resetChannels(mySci);
    SCI_setTxFifoIntLevel(mySci, SCI_FifoLevel_Empty);

    SCI_resetRxFifo(mySci);
    SCI_clearRxFifoInt(mySci);
    SCI_setRxFifoIntLevel(mySci, SCI_FifoLevel_4_Words);

    return;
}

//
// End of File
//

Here's a logic analyzer image of one of the longer messages:

And here's a short one:

Questions:

1. I don't need to use an isr, but should I?

2. Should I abandon the fifo in favor of a streaming approach?

3. Can you point me to a TI app note that would address these questions?

4. Can you point me to some example code?

Thanks in advance!
robin

  • Hi Robin,

    1. I don't need to use an isr, but should I?

    This depends on what you have going on in your overall application. If you are only using this device for the serial communication then you could get way with not using an ISR. If there are other components involved that the CPU needs to handle then I would encourage the use of an ISR. 

    2. Should I abandon the fifo in favor of a streaming approach?

    Since you receiving messaging between 10 and 20 bytes, I do think the streaming approach would be the easier option. Our FIFO level only goes up to 4 on this device. 

    3. Can you point me to a TI app note that would address these questions?

    Unfortunately, we do not have any app notes that would cover these topics.

    4. Can you point me to some example code?

    The only example code that we do have is within C2000Ware.

    Best Regards,

    Marlyn

  • Thanks for the quick response, Marlyn.

    To switch from a fifo to a streaming approach, I performed the following:

    1. Commented out the fifo initialization calls:

    //
    // scia_fifo_init - Initialize the SCI FIFO
    //
    void scia_fifo_init()
    {
    //    SCI_enableFifoEnh(mySci);
    //    SCI_resetTxFifo(mySci);
    //    SCI_clearTxFifoInt(mySci);
    //    SCI_resetChannels(mySci);
    //    SCI_setTxFifoIntLevel(mySci, SCI_FifoLevel_Empty);
    
    //    SCI_resetRxFifo(mySci);
    //    SCI_clearRxFifoInt(mySci);
    //    SCI_setRxFifoIntLevel(mySci, SCI_FifoLevel_4_Words);
    
        return;
    }
    

    2. Left the non-fifo SCI initialization as it was:

    void scia_init()
    {
        CLK_enableSciaClock(myClk);
    
        //
        // 1 stop bit,  No loopback, No parity,8 char bits, async mode,
        // idle-line protocol
        //
        SCI_disableParity(mySci);
        SCI_setNumStopBits(mySci, SCI_NumStopBits_One);
        SCI_setCharLength(mySci, SCI_CharLength_8_Bits);
    
        //
        // enable TX, RX, internal SCICLK, Disable RX ERR, SLEEP, TXWAKE
        //
        SCI_enableTx(mySci);
        SCI_enableRx(mySci);
        SCI_enableTxInt(mySci);
        SCI_enableRxInt(mySci);
    
    //    SCI_enableLoopBack(mySci);
    
        SCI_setBaudRate(mySci, (SCI_BaudRate_e)162);	//rdv 9615 BAUD
    
        SCI_enable(mySci);
    
        return;
    }
    

    3. Changed the bit of code that waits for a byte to be received FROM THIS:

    	    while(SCI_getRxFifoStatus(mySci) == SCI_FifoStatus_Empty)
    	    {
                greenSwPressed = (GPIO_getData(myGpio, GRN_SW)) ? 0 : 1;
                redSwPressed   = (GPIO_getData(myGpio, RED_SW)) ? 0 : 1;
    
                if(redSwPressed & !redLED)
                {
                    GPIO_setHigh(myGpio, RED_LED);
                } else if(!redSwPressed & !redLED)
                {
                    GPIO_setLow(myGpio, RED_LED);
                }
    
                if(greenSwPressed & !greenLED)
                {
                    GPIO_setHigh(myGpio, GREEN_LED);
                } else if(!greenSwPressed & !greenLED)
                {
                    GPIO_setLow(myGpio, GREEN_LED);
                }
    
    	        if(i2cSelVar)
                {
    
                }
    
                if(detectVar)
                {
    
                }
    
    	        if(redLED & greenLED & blueLED)
    	        {
                    GPIO_setHigh(myGpio, I2C_SEL);
                    GPIO_setHigh(myGpio, DETECT);
    	            detectVar = GPIO_getData(myGpio, DETECT);
    	        } else
    	        {
                    GPIO_setLow(myGpio, I2C_SEL);
                    GPIO_setLow(myGpio, DETECT);
                    detectVar = GPIO_getData(myGpio, DETECT);
    	        }
            }
    

    4. TO THIS:

    	    while(SCI_isRxDataReady(mySci) != true)
    	    {
                greenSwPressed = (GPIO_getData(myGpio, GRN_SW)) ? 0 : 1;
                redSwPressed   = (GPIO_getData(myGpio, RED_SW)) ? 0 : 1;
    
                if(redSwPressed & !redLED)
                {
                    GPIO_setHigh(myGpio, RED_LED);
                } else if(!redSwPressed & !redLED)
                {
                    GPIO_setLow(myGpio, RED_LED);
                }
    
                if(greenSwPressed & !greenLED)
                {
                    GPIO_setHigh(myGpio, GREEN_LED);
                } else if(!greenSwPressed & !greenLED)
                {
                    GPIO_setLow(myGpio, GREEN_LED);
                }
    
    	        if(i2cSelVar)
                {
    
                }
    
                if(detectVar)
                {
    
                }
    
    	        if(redLED & greenLED & blueLED)
    	        {
                    GPIO_setHigh(myGpio, I2C_SEL);
                    GPIO_setHigh(myGpio, DETECT);
    	            detectVar = GPIO_getData(myGpio, DETECT);
    	        } else
    	        {
                    GPIO_setLow(myGpio, I2C_SEL);
                    GPIO_setLow(myGpio, DETECT);
                    detectVar = GPIO_getData(myGpio, DETECT);
    	        }
            }
    

    5. Upon receipt of a byte in the SCIRXBUF, here's the code that pulls in a message:

     	   byteReceived = SCI_getData(mySci);
           if(byteReceived != 10 && byteReceived != 13)
           {
               receivedByteFlag = 1;
               message[rdvWordCount++] = (char)byteReceived;
               rdvWordCount &= 0x7F;    //rdv Limit buffer range to 0-127
           }
    

    And it works splendidly.

    I may opt for using an isr for the receive function.  It works without it, but that just seems a cleaner approach.  I'm sure there'll be more refinements along the way, but this provided the functionality I was looking for.

    Thank you,
    robin