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.

CCS/MSP430F5342: Multiple bytes reception UART problem

Part Number: MSP430F5342


Tool/software: Code Composer Studio

Hi,

I am using the below code for UART. I am trying to send 4 bytes of data from terminal on my PC to P4.5 of MSP430F5342 . It is sent being back to terminal from P4.4 of MSP430. The below code is being used to loop back. I am receiving only 2 bytes back on the terminal(1st and last bytes only).


#include <msp430.h>
unsigned char bytescount=0;
unsigned char received_value=0;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT

P4SEL |= BIT4+BIT5; // P4.4,5
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL_2; // SMCLK
UCA1BR0 = 9; // 1MHz 115200 (see User's Guide)
UCA1BR1 = 0; // 1MHz 115200
UCA1MCTL |= UCBRS_1 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt

__bis_SR_register( GIE); // Enter LPM0, interrupts enabled
// __no_operation(); // For debugger


// P3.3,4 = USCI_A0 TXD/RXD
while(1);
}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_A1_VECTOR))) USCI_A1_ISR (void)
#else
#error Compiler not supported!
#endif
{
volatile unsigned int i;

switch(__even_in_range(UCA1IV,4))
{
case 0: break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
while (!(UCA1IFG&UCTXIFG)); // USCI_A0 RX buffer ready?
// Send next value
received_value=UCA1RXBUF;
bytescount++;
UCA1TXBUF=received_value;

break;
case 4: break; // Vector 4 - TXIFG
default: break;
}
}

Eg: When I send 0x01 0x02 0x03 0x04 I am receiving only 0x01 and 0x04 back on the terminal

If I send only 0x01 and 0x02 I receive them both correctly.

The count "bytescount" present in ISR increments only twice every time I send the data  IRRESPECTIVE of the length of the data. i tried sent 2 3 4 5 bytes. Whatever may be the number of bytes sent from terminal I am receiving only 1st and last bytes back and ISR is entered only twice every time. Receive ISR is supposed to be entered for every byte received but it is not happening so. Kindly suggest corrections.

  • Hi Vaishravana,

    You are most likely spending too much time in your ISR, therefore missing all of the "in-between" bytes as you prepare to return the first byte. You can partly test this theory by sending several bytes (10+) or only putting while (!(UCA1IFG&UCTXIFG)); & UCA1TXBUF = UCA1RXBUF; into the ISR. You should spend as little time in the ISR as possible, increase your MCLK speed, and/or lower the baud rate.

    Regards,
    Ryan
  • Thanks Ryan.

    Oops. I had enabled breakpoint in ISR and set its "breakpoint properties" to "refresh all windows". Probably refreshing all the windows is taking more time.I removed the breakpoint and the code just works fine. Cursing myself for not having tried this earlier and wasting time. I have to look into JTAG speed now.

  • A breakpoint halts MCU operation whereas the PC continues to send data, since there is only one RX buffer available to store incoming bytes it follows that the intermediate data is overwritten while you are servicing the breakpoint from the first byte. Once you continue operation, the next breakpoint services the last byte that was stored in the RX buffer, causing you to miss the rest. You need to store your results in an array and only set a breakpoint once the array has been adequately filled.

    Regards,
    Ryan

**Attention** This is a public forum