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.

UCTXIFG how does it work?

Other Parts Discussed in Thread: MSP430F5171

I'm speaking by SPI to a SRAM. With serial clock=1MHz reading function is:

unsigned char RAM_read_byte(void)
{
volatile unsigned char data;
volatile unsigned int i;

//read the byte
data=UCB0RXBUF;                     //to flush buffer, without does not work
while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB0TXBUF=0;
while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
for(i=0; i<10; i++){}
while (!(UCB0IFG&UCRXIFG)); // USCI_B0 RX buffer ready?
data=UCB0RXBUF;

return data;
}

and for writing:

void RAM_write_byte(unsigned char data)
{
volatile unsigned int i;
//send the data
while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB0TXBUF=data;
while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
for(i=0; i<10; i++){}
}

If i omit the delay loop for(i=0; i<10; i++){} nothing works. If serial clock rises to 10MHz i can exclude the delay loop.

UCTXIFG sets when transmit buffer is empty, so this  should mean that data has been transmitted, so no delay should be needed.

Decreasing further the clock, delay must be increased. UCTXIFG works only if interrupt enabled?

Thanks for help, Enzo

  • Hi Enzo!

    A set TX IFG does not mean that a transmission has finished. There is the transmit buffer and the transmit shift register. If no byte is currently transmitted and you place a byte into the transmit buffer, it will be forwarded to the transmit shift register and the buffer is empty again and ready to take the next byte which will be forwarded to the transmit shift register after the currently transmitted byte is done. TX IFG is set when the transmit buffer is empty, so if there is no actual transmission and you place a byte into the transmit buffer that is then forwarded to the transmit shift register, TX IFG will be set while the byte will be transmitted in the backround. TX IFG only signals an empty transmit buffer. For a done transmission check for the RX IFG (every transmission means receiving a byte) or use the BUSY flag.

    Dennis
  • Which specific MSP chip are you using?

    Also, when you say "nothing works", what exactly happens? Does the loop testing UCRXIFG in RAM_read_byte never complete? What happens when you omit the loop in RAM_write_byte?
  • Thanks Dennis, i will verify but now it is clear.

    Empty buffer does not mean data transmitted, as i believed it was.

    Microprocessor is MFP430F5171.

    Enzo

  • Hi Robert,
    chip is MSP430F5171, nothing works menas that data transmitted are read back wrong; loop testing UCRXIFG in reading RAM always complete.
  • That read routine is certainly wrong.


    My usual approach is to write a routine to both write and read a byte. If this is the only routine that accesses the hardware, then the checking of flags is simplified.

    unsigned char spirw(unsigned char c)
    {
      UCB0TXBUF=c;
      while (!(UCB0IFG&UCRXIFG)); // USCI_B0 RX buffer ready?
    
      return UCB0RXBUF;
    }

  • Thanks all,

    these are the functions that work:

    void RAM_write_byte(unsigned char data)
    {
    while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
    UCB0TXBUF=data;
    while(UCB0STAT&UCBUSY); //USCI inactive?
    }

    unsigned char RAM_read_byte(void)
    {
    volatile unsigned char data;
    data=UCB0RXBUF; //to flush buffer
    while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
    UCB0TXBUF=0;
    while(UCB0STAT&UCBBUSY); //USCI inactive?
    while (!(UCB0IFG&UCRXIFG)); // USCI_B0 RX buffer ready?
    data=UCB0RXBUF;
    return data;
    }

    i have verified at 10MHz and 1 MHz clock. Thanks for your precious help, Enzo

**Attention** This is a public forum