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/MSP432P401R: Sending data to serial port

Part Number: MSP432P401R


Tool/software: Code Composer Studio

Hey, I'm trying to learn how to send data to the serial port but I'm not very experienced with that part of coding or the MSP. I've looked at the MSP432 manual and it has a very dense section on using the UART but it doesn't seem to have any examples of how to use all the collective parts to accomplish the task.

What I am hoping to do is setup a base code that has a variable X that can increment, and then view that data being sent to the COM port on Teraterm  (or something), so I can see data on the COM and then have something else gather the published data. Overall I have a lot of data I want to send but finding a starting place with just one piece would be helpful. Is there any sample code I can use to help me get started?

Luke

  • Hi Luke,

    You can find simple code examples on how to use the UART in the TI resource explorer:

    BR,
    Leo

  • This seems like a good start. I am using the msp432p401x_euscia0_uart_01 example code. I understand that it sets up all the registers and clock settings for me. Now I'm still not sure how to send data though. Looking at the code and the commented areas my best guess is the following block:

    // UART interrupt service routine
    void EUSCIA0_IRQHandler(void)
    {
        if (EUSCI_A0->IFG & EUSCI_A_IFG_RXIFG)
        {
            // Check if the TX buffer is empty first
            while(!(EUSCI_A0->IFG & EUSCI_A_IFG_TXIFG));

            // Echo the received character back
            EUSCI_A0->TXBUF = EUSCI_A0->RXBUF;
      }

    My guess was that the interrupt is constantly sending data to the com port. So putting in instructions there could work. What I'm looking for is how to actually send that data and what that code looks like. Is the data just being printed so it's displayed on the port?

    printf("The value of X is %d .", x); kind of thing?

    Luke

  • Here is what I do in my ISR:

    I have TXData as a char buffer and TXIidx as a volatile int, initially set to -1.

    To send, I put a '\0' terminated data in TXBuf and set TXIdx to 0. In my main loop I have

            if (TXIdx == 0)

            {

                // Start the transmission

                MAP_UART_transmitData(EUSCI_A0_BASE, TXData[TXIdx++]);

            }

    to kick things off.

    The IRQ handles the rest.

    /* EUSCI A0 UART ISR */
    void EUSCIA0_IRQHandler(void)
    {
        uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
    
        MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);
    
        if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)
        {
            RXData = MAP_UART_receiveData(EUSCI_A0_BASE);
    
    
        } else if (status & EUSCI_A_UART_TRANSMIT_INTERRUPT_FLAG) {
            // Ready to transmit the next byte
            if (TXIdx == -1) return;
    
            if (TXData[TXIdx] == '\0')
            {
                // Done transmitting
                TXIdx = -1;
    
            } else {
                MAP_UART_transmitData(EUSCI_A0_BASE, TXData[TXIdx++]);
    
            }
        }
    
    }

  • Hi Luke,

    I also recommend looking at the other UART examples in resource explorer like:

    BR,
    Leo

  • Okay, so I have an example that seems to print the character x. It's not the whole code but the bottom half.

        MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
        MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
        MAP_Interrupt_enableSleepOnIsrExit();
        MAP_Interrupt_enableMaster();   
        //![Simple UART Example]

        while(1)
        {

            printf("Sending byte \n");
            MAP_UART_transmitData(EUSCI_A0_BASE, 'x');
          
        }
    }

    /* EUSCI A0 UART ISR - Echoes data back to PC host */
    void EUSCIA0_IRQHandler(void)
    {
        uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);

        if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)
        {
            MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
        }

    }

    Which is similar to your example. Is it the main that is sending the data or is that taking place in the ISR? In which case, if I want to send data to the port should all that be in the ISR MAP_UART portion? I'm trying to understand where the work takes place.

    Luke

  • In your case it is in main(). The irq will only transmit when it receives a byte.

  • So if I am understanding correctly, whatever I place in the

    MAP_UART_transmitData(EUSCI_A0_BASE, 'x');

    will be the data that is sent to the com port. So currently it should print the character x. Can this be replaced with a variable that is sent? I looked in the MSP handbook and it doesn't have anything about the transmitData code so I'm trying to learn what I can/can't send through this instruction. Is there resource aside from the MSP432 tech manual? Or knowledge someone knows?

    Luke

  • As you can see in my code I use a variable.

    The prototype is:

    void UART_transmitData (uint32_t moduleInstance, uint_fast8_t transmitData)

    So you can send any 8 bit value.

    Here is a link to the driverlib docs.

  • Oh, a few caveats, the command can only buffer one byte. When you execute it, it immediately moves the data to another buffer and starts sending, so you can send another byte. However, you must wait until the first byte is sent and the new data is put into the buffer before you send another byte. In your while loop, if you remove the printf(), your loop will execute much too fast and you will overrun bytes. That is why my code waits for the TX interrupt to know when it is ready for another byte.

  • Thanks for sending the link Leo. I have been using the example from the from v10 of CCS. It seems to send something to the com port but it isn't complete. The ultimate goal is to have data sent to the port so it can be read/displayed on Node Red.

**Attention** This is a public forum