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.

SCI FIFO TX problem with printf

Hi, All.

I am trying to send using SCI the string "Burst-Transmit/n/r" in a similar way that Frank Bormann wrote in example Lab9_3.c. Tha main difference is that I am trying to use an "sprintf" function to charge the string. I am also using SCI TX in FIFO mode.

 The following main routine and the SCI_TX interrupt routine are the core of my programm

//main Routine

void main(void)
{

//Here, I write all initialization routines

  while(1)
  { 

  printf("Burst-Transmit\n\r");
  //sprintf(message,"Burst-Transmit\n\r");

  //A Timer interrupt is also running

  while(CpuTimer0.InterruptCount < 40) // 40 * 50ms = 2 sec

  {

  }
  CpuTimer0.InterruptCount = 0;
  SciaRegs.SCIFFTX.bit.TXFFINTCLR = 1; // re-arm Tx - FIFO
}
}

/********************************************************************/

//SCI_TX_FIFO interrupt routine

interrupt void SCIA_TX_isr(void) // SCI-A Transmit Interrupt Service
{
unsigned int i;
sprintf(message,"Burst-Transmit\n\r");
// copy 16 character into SCI-A TX buffer
for(i=0;i<16;i++) SciaRegs.SCITXBUF= message[i];
// Acknowledge this interrupt to receive more interrupts from group 9
PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}

The printf is working OK and the "Burst-Transmit" is written in the console window

My problem is with the hyperterminal that I have connected to the SCI output.

Sometimes, the right string is written "Burst.Transmit", but most times a bad string is written (something similar to "#/€$3.....ansmit".

The same is happening if I put the sprintf sentence in the main function instead of the SCI_TX interrupt routine.

Some explanation for that?

  • Two changes to my previous version.

    In the main function:

    I write

    SciaRegs.SCIFFTX.bit.TXFIFOXRESET =1; // enable TXFIFO
    SciaRegs.SCIFFTX.bit.TXFFINTCLR = 1; // re-arm Tx - FIFO

    instead of

    SciaRegs.SCIFFTX.bit.TXFFINTCLR = 1; // re-arm Tx - FIFO

    The second one:

    I comment or delete the printf line

    //printf("Burst-Transmit\n\r");

    With these two changes, the programm runs OK. And moreover, I can put the sprintf line in the main loop or in the SCI_TX interrupt routine. The programm works right in both cases.

    The main difference is that "the printf line is not compiled". Some explanation for that?


  • The following file works quite fine. It shows a string in a hyperteminal. In the string, only a counter "intvar1" is changed between a print and the next print (the encoder does not move).

    It has only two problems:

    1. The first sentence with the intvar1=0 is printed two times.

    2. When I include a printf sentence (see commented line before the sprintf sentence), the print process in the hyperteminal fails. I begin to see a lot of bad characters printed in the hyperterminal screen.

    I am working with it two days, baut i can not found an answer . Some explanation for that?

    //
    //      Lab9_6: TMS320F28335
    //      (c) F.F. Linera
    //
    //###########################################################################
    //
    // FILE:	Lab9_6.c
    // 
    // TITLE:	DSP28 SCI - Communication to PC - Terminal
    //			DSP sends the string "float=23.456 Int=  1234 encoder=nn Hex=AB"   every 2 seconds
    //			CPU Timer0 ISR every 50 ms
    //			SCI-Setup: 38400 Baud, 8 Bit , No Parity , 1 Stopbit
    //			SCI - TX - Interrupt used in this Lab 
    //			TX SCI is used in FIFO mode
    //			Watchdog disabled , serviced in ISR and main-loop
    //###########################################################################
    //  Ver | dd mmm yyyy | Who  | Description of changes
    // =====|=============|======|===============================================
    //  1.0 | 18 Jan 2013 | F.F.L | 
    //###########################################################################
    #include "DSP2833x_Device.h"
    #include <stdlib.h>;
    #include <string.h>
    #include <stdio.h>
    
    // External Function prototypes
    extern void InitSysCtrl(void);
    extern void InitPieCtrl(void);
    extern void InitPieVectTable(void);
    extern void InitCpuTimers(void);
    extern void ConfigCpuTimer(struct CPUTIMER_VARS *, float, float);
    
    // Prototype statements for functions found within this file.
    void Gpio_select(void);
    void SCIA_init(void);
    interrupt void cpu_timer0_isr(void); // Prototype for Timer 0 Interrupt Service Routine
    interrupt void SCIA_TX_isr(void);	 // SCI-A Transmit Interrupt Service
    
    // Global Variables
    //char message[]={"The F28335 - UART ISR is fine 2!\n\r"};
    char message[35];
    char message2[]={"    "};
    int index =0;			// index variable into message
    
    float floatvar1 = 23.456;
    
    int intvar1 = 0;
    
    int	hexvar = 0xAB;
    
    //###########################################################################
    //						main code									
    //###########################################################################
    void main(void)
    {
    	long encoder=0;	// binary counter for digital output
    	InitSysCtrl();	// Basic Core Initialization
    					// SYSCLK=150MHz, HISPCLK=75MHz, LSPCLK=37.5MHz
    	EALLOW;
       	//SysCtrlRegs.WDCR= 0x00AF;		  // Re-enable the watchdog
       	SysCtrlRegs.WDCR= 0x00E8;		  // Disable the watchdog
       	EDIS;			// 0x00E8  to disable the Watchdog , Prescaler = 1
       					// 0x00AF  to NOT disable the Watchdog, Prescaler = 64
    	
    	Gpio_select();	// GPIO9, GPIO11, GPIO34 and GPIO49 as output
    					// to 4 LEDs at Peripheral Explorer	
    	
    	InitPieCtrl();		// default status of PIE; in DSP2833x_PieCtrl.c
    	
    	InitPieVectTable(); // init PIE vector table; in DSP2833x_PieVect.c
    	
    	// re-map PIE - entry for Timer 0 Interrupt and SCI-A-TX
    	EALLOW;  
       	PieVectTable.TINT0 = &cpu_timer0_isr;
    	PieVectTable.SCITXINTA = &SCIA_TX_isr;
       	EDIS;    
       		
    	InitCpuTimers();	// Function in: DSP2833x_CpuTimers.c	
    	
    	// Configure CPU-Timer 0 to interrupt every 50 ms:
    	// 150MHz CPU Freq, 50000 �seconds interrupt period
        ConfigCpuTimer(&CpuTimer0, 150, 50000);	// DSP2833x_CpuTimers.c
        
       	SCIA_init();  // Initalize SCI 
       
        PieCtrlRegs.PIEIER1.bit.INTx7 = 1;	// Cpu Timer 0 isr
    	PieCtrlRegs.PIEIER9.bit.INTx2 = 1;  // SCI-A-TX-isr
    
    
    	// Enable CPU INT1 (CPU-Timer 0) and INT9 (SCIA-TX):
        IER = 0x101;
        
    	// Enable global Interrupts and higher priority real-time debug events:
       	EINT;   // Enable Global interrupt INTM
       	ERTM;   // Enable Global realtime interrupt DBGM
       	
       	CpuTimer0Regs.TCR.bit.TSS = 0;		// Start T0
       	
    	while(1)
    	{    
    		encoder = (GpioDataRegs.GPADAT.all >> 12) &0x0000000F; //read 4-bit hex encoder
    		//SciaRegs.SCITXBUF=message[index++];			// send first character
    		//sprintf("Hello again, world\n");
    
    		//The following line works OK, but  some changes are necessary for that
    		//in the 28335_RAM_lnk2.cmd file:
    		//1.To compile the functions of the stdio.h: text section has been changed to
    		//  .text            : >> RAML1 | RAML2 | RAML3,     PAGE = 0
    		//2.To print float, the stack size for the project has ben changed to 0x800
    		// and due to that, the stacks section has also been changed to
    		//.stack           : > RAML5,     PAGE = 1
    		ltoa (encoder,message2);
    		//printf(message,"float=%6.3f Int=%6d Hex=%X\n\r",floatvar1, intvar1, hexvar);
    		//printf(message,"float=%6.3f Int=%6d encoder=%s Hex=%X\n\r",floatvar1, intvar1++, message2, hexvar);
    		sprintf(message,"float=%6.3f Int=%6d encoder=%s Hex=%X\n\r",floatvar1, intvar1++, message2, hexvar);
    		//GpioDataRegs.GPASET.bit.GPIO9 = 1;
    		GpioDataRegs.GPACLEAR.bit.GPIO11 = 1;
           	while(CpuTimer0.InterruptCount < 40)  	// 40 * 50ms = 2 sec
    	   	{
    			//EALLOW;
    			//SysCtrlRegs.WDKEY = 0xAA;			// Service watchdog #2
    			//EDIS;
    		}
    		//index =0;
    		CpuTimer0.InterruptCount = 0;
    		//GpioDataRegs.GPACLEAR.bit.GPIO9 = 1;
    		SciaRegs.SCIFFTX.bit.TXFIFOXRESET =1;  // enable TXFIFO
    		SciaRegs.SCIFFTX.bit.TXFFINTCLR = 1;	// re-arm Tx - FIFO
    	}
    } 	
    
    void Gpio_select(void)
    {
    	EALLOW;
    	GpioCtrlRegs.GPAMUX1.all = 0;			// GPIO15 ... GPIO0 = General Puropse I/O
    	GpioCtrlRegs.GPAMUX2.all = 0;			// GPIO31 ... GPIO16 = General Purpose I/O
    	
    	GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1;	// SCIRXDA
    	GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1;	// SCITXDA	
    	
    	GpioCtrlRegs.GPBMUX1.all = 0;			// GPIO47 ... GPIO32 = General Purpose I/O
    	GpioCtrlRegs.GPBMUX2.all = 0;			// GPIO63 ... GPIO48 = General Purpose I/O
    	GpioCtrlRegs.GPCMUX1.all = 0;			// GPIO79 ... GPIO64 = General Purpose I/O
    	GpioCtrlRegs.GPCMUX2.all = 0;			// GPIO87 ... GPIO80 = General Purpose I/O
    	 
    	GpioCtrlRegs.GPADIR.all = 0;
    	GpioCtrlRegs.GPADIR.bit.GPIO9 = 1;		// peripheral explorer: LED LD1 at GPIO9
    	GpioCtrlRegs.GPADIR.bit.GPIO11 = 1;		// peripheral explorer: LED LD2 at GPIO11
    	GpioDataRegs.GPACLEAR.bit.GPIO9 = 1;
    	GpioDataRegs.GPACLEAR.bit.GPIO11 = 1;
    
    	GpioCtrlRegs.GPBDIR.all = 0;			// GPIO63-32 as inputs
    	GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;	// peripheral explorer: LED LD3 at GPIO34
    	GpioCtrlRegs.GPBDIR.bit.GPIO49 = 1; // peripheral explorer: LED LD4 at GPIO49
    
    	GpioCtrlRegs.GPCDIR.all = 0;			// GPIO87-64 as inputs
    	EDIS;
    } 
    
    void SCIA_init()
    {    
      	SciaRegs.SCICCR.all =0x0007;   	// 1 stop bit,  No loopback
                                       	// No parity,8 char bits,
                                       	// async mode, idle-line protocol
    	SciaRegs.SCICTL1.all =0x0003;  	// enable TX, RX, internal SCICLK, 
                                       	// Disable RX ERR, SLEEP, TXWAKE
    	
    	// SYSCLOCKOUT = 150MHz; LSPCLK = 1/4 = 37.5 MHz
    	// BRR = (LSPCLK / (38400 x 8)) -1
    	// BRR = 121  gives 38422 Baud
    	SciaRegs.SCIHBAUD    = 121 >> 8;		// Highbyte
    	SciaRegs.SCILBAUD    = 121 & 0x00FF;	// Lowbyte
    
    	SciaRegs.SCICTL2.bit.TXINTENA = 1; 		// enable SCI-A Tx-ISR
    
    	SciaRegs.SCIFFTX.all = 0xC060;			// bit 15 = 1 : relinquish from Reset
    											// bit 14 = 1 : Enable FIFO
    											// bit 6 = 1 :  CLR TXFFINT-Flag
    											// bit 5 = 1 :  enable TX FIFO match
    											// bit 4-0 :  TX-ISR, if TX FIFO is 0(empty)
    	SciaRegs.SCIFFCT.all = 0x0000;			// Set FIFO transfer delay to 0
    	//SciaRegs.SCIFFTX.bit.TXFIFOXRESET = 1;  // re-enable transmit fifo operation
    
    	SciaRegs.SCICTL1.all = 0x0023;	// Relinquish SCI from Reset 
    }  
    
    interrupt void cpu_timer0_isr(void)
    {
        CpuTimer0.InterruptCount++;		// increment time counter
       	//GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1; // toggle LED at GPIO34
       	// Service the watchdog every Timer 0 interrupt
       	EALLOW;
    	SysCtrlRegs.WDKEY = 0x55;		// Service watchdog #1
    	EDIS;
       	// Acknowledge this interrupt to receive more interrupts from group 1
       	PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
    }
    
    interrupt void SCIA_TX_isr(void)	 // SCI-A Transmit Interrupt Service
    {
    	// test for end of string ('\0'); if not, send next character
    	//if (message[index]!= '\0'	) SciaRegs.SCITXBUF= message[index++];
    	unsigned int i;
    	//strcpy(message,"Burst-Transmit\n\r");
    	//sprintf(message,"Burst-Transmit\n\r");
    		// copy 16 character into SCI-A TX buffer
    	//for(i=0;i<16;i++) SciaRegs.SCITXBUF= message[i];
    	GpioDataRegs.GPACLEAR.bit.GPIO9 = 1;
    	do{
    		if (SciaRegs.SCIFFTX.bit.TXFFST!=15)
    			{
    			SciaRegs.SCITXBUF= message[index++];
    			GpioDataRegs.GPATOGGLE.bit.GPIO11 = 1;
    			}
    		else
    			{
    
    			GpioDataRegs.GPASET.bit.GPIO9 = 1;
    			}
    	}while (message[index]!= '\0');
    	//GpioDataRegs.GPASET.bit.GPIO9 = 1;
    	index =0;
    	// Acknowledge this interrupt to receive more interrupts from group 9
        PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;  
    }
    
    //===========================================================================
    // End of SourceCode.
    //===========================================================================