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.

MSP430F67791A: UART with esp8266

Part Number: MSP430F67791A

I am using msp430f67791a to communicate with the UART. 

I have set the baud rate of the ESP8266 to be 9600 have disabled the parity bit using the AT commands in the putty terminal.

According to that, the modulation control and baud rate control registers have been set.

The registers have been set according to table 39-5 in the document: www.ti.com/.../slau208q.pdf


#include <msp430f67791a.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

void uart_init();
unsigned char senddata(unsigned char tx);
unsigned char sendstring[2] = {'A','T'};
unsigned char receivestring[50];

unsigned int i;
unsigned int j=0;

int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;

uart_init();

while(1)
{
for(i=0;i<2;i++)
{
while (!(UCA1IFG & UCTXIFG))
UCA1TXBUF = sendstring[i];
}
j=0;
while (!(UCA1IFG & UCRXIFG))
{
receivestring[j] += UCA1RXBUF;
j++;
}
return 0;
}
}
void uart_init(void)
{
P3SEL0 |= (BIT4 + BIT5);

UCA1CTLW0 = 0;

UCA1CTLW0 |= UCSWRST;

UCA1CTLW0 |= (UCSSEL_2 + UCRXEIE + UCMODE_0);

UCA1CTLW0 &= ~(UCPEN + UCMSB + UC7BIT + UCSPB + UCSYNC + UCDORM);

UCA1CTLW1 |= UCGLIT_1;

UCA1BRW = 0x06;

UCA1MCTLW |= (UCBRS5 + UCBRF3 + UCOS16);

UCA1ABCTL &= ~(UCABDEN);

UCA1IRCTL &= ~(UCIREN);

UCA1CTLW0 &= ~(UCSWRST);
}

 


The ESP8266 does not seem to communicate with the MSP430. Please guide me through this.

  • UCA1MCTLW |= (UCBRS5 + UCBRF3 + UCOS16);

    Based on User Guide (SLAU208Q) Table 36-5, I would expect something more like:

    UCA1MCTLW |= ((0*UCBRS) + (UCBRF3|UCBRF2|UCBRF0) + UCOS16); // BRS=0, BRF=13 from UG table 36-5

    -----

    UCA1CTLW0 |= (UCSSEL_2 + UCRXEIE + UCMODE_0);

    This enables Rx-error interrupts, but I don't see an ISR for it. I suggest you remove UCRXEIE, at least for now.

    ----

    How is the MSP430 connected to the ESP? Check (again) to make sure RX and TX aren't reversed.

  • I think the device comprises e-USCI module instead of USCI module. 

    Which table should I refer 39-5 or 36-5?

    I have removed the UCRXEIE.

    Rx of the ESP8266 is connected to Tx of MSP430. Tx of ESP8266 is connected to RX of MSP430.

  • You are correct -- you should use Table 39-5. So that would be:

    UCA1MCTLW |= ((0x22*UCBRS0) + (UCBRF3|UCBRF2|UCBRF0) + UCOS16); // BRS=0x22, BRF=13 from UG table 39-5 

    Ordinarily the modulation details wouldn't cause a complete loss of communication, rather a few glitchy characters. I was guessing that the UCRXEIE was the real problem (isr_trap). Did that change anything?

    Also, did you use AT+UART_CUR= or AT+UART_DEF= to set the UART speed? You might consider first trying to connect at 115.2k (default out of the box), to remove one variable from your experiment.

    -----

    >while (!(UCA1IFG & UCRXIFG))
    >{
    >receivestring[j] += UCA1RXBUF;
    >j++;
    >}

    This looks backwards -- it will read Rx bytes only when there aren't any to read, and will (quickly) overrun receivestring[]. Try something like:

    for (j=0; j<sizeof(receivestring)-1; ++j) {
        while (!(UCA1IFG & UCRXIFG)) /*EMPTY*/;  // Spin until something arrives
        receivestring[j] = UCA1RXBUF;
        if (receivestring[j] == '\r') break;   // quit if we see carriage return
    }
    receivestring[j] = '\0';  // overwrite CR with string terminator (NUL)
    
    

  • I have updated the modulation control registers as you said, It seemed to have worked the trick. Whenever I reset the module, receivestring variable can be read as "ready".  But the Tx does not seem to send the data to the Rx of ESP8266. Is there any trick or should I try the interrupts?

    #include <msp430f67791a.h>
    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    #include <string.h>

    void uart_init();
    unsigned char senddata(unsigned char tx);
    unsigned char sendstring[3] = {'A','T','\0'};
    unsigned char receivestring[255];

    unsigned int i;
    unsigned int j=0;

    int main( void )
    {
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;

    uart_init();

    while(1)
    {
    for(i=0;i<sizeof(sendstring);i++)
    {
    while (!(UCA1IFG & UCTXIFG))
    UCA1TXBUF = sendstring[i];
    }
    delay(50);
    for (j=0; j<sizeof(receivestring); ++j)
    {
    while (!(UCA1IFG & UCRXIFG)) /*EMPTY*/; // Spin until something arrives
    receivestring[j] = UCA1RXBUF;
    if (receivestring[j] == '\r')
    break; // quit if we see carriage return
    }
    receivestring[j] = '\0';

    //printf("%s",&receivestring);
    //return 0;
    }
    }
    void uart_init(void)
    {
    P3SEL0 |= (BIT4 + BIT5);

    UCA1CTLW0 = 0;

    UCA1CTLW0 |= UCSWRST;

    UCA1CTLW0 |= (UCSSEL_2 + UCMODE_0);

    UCA1CTLW0 &= ~(UCPEN + UCMSB + UC7BIT + UCSPB + UCSYNC + UCDORM);

    UCA1CTLW1 |= UCGLIT_1;

    UCA1BRW = 0x06;

    UCA1MCTLW |= ((0x22*UCBRS0) + (UCBRF3|UCBRF2|UCBRF0) + UCOS16);

    UCA1ABCTL &= ~(UCABDEN);

    UCA1IRCTL &= ~(UCIREN);

    UCA1CTLW0 &= ~(UCSWRST);
    }

  • > while (!(UCA1IFG & UCTXIFG))

    I missed the missing semicolon here. Try:

    > while (!(UCA1IFG & UCTXIFG)) /*EMPTY*/; // Spin until TXBUF is available

  • When I include the semicolon to it, it seems to stuck in the line forever.

  • How do you tell that it is stuck there?

    The only ways I know of for TXIFG to stay 0 for a "long time" are (a) UCSWRST=1 or (b) the clock (UCSSEL_2=SMCLK) is stopped. Neither of these seem probable here, particularly since you reported that your receive side is working.

    So my first guesses are (1) a typo (2) a debugger illusion.

    Can you post the updated code fragment?

  • #include <msp430f67791a.h>
    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    #include <string.h>

    void uart_init();
    //unsigned char senddata(unsigned char tx);
    unsigned char* sendstring;
    unsigned char receivestring[25] = "0";

    unsigned char* sendstring1;
    unsigned char receivestring1[25] = "0";

    unsigned int i=0;
    unsigned int j=0;
    //unsigned int k=0;


    int main( void )
    {
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;

    sendstring = "AT+CWMODE=2";
    sendstring1 = "AT+CWJAP=\"PocoF1\",\"PocoF1@wifi\",0,0";

    uart_init();

    //_BIS_SR(LPM0_bits + GIE);

    for(j=0;j<sizeof(sendstring);j++)
    {
    while (!(UCA1IFG & UCTXIFG));
    UCA1TXBUF = sendstring[j];
    }
    for (j=0; j<sizeof(receivestring); ++j)
    {
    while (!(UCA1IFG & UCRXIFG)); // Spin until something arrives
    receivestring[j] = UCA1RXBUF;
    if (receivestring[j] == '\r')
    break; // quit if we see carriage return
    }
    receivestring[j] = '\0';


    for(j=0;j<sizeof(sendstring1);j++)
    {
    while (!(UCA1IFG & UCTXIFG));
    UCA1TXBUF = sendstring1[j];
    }
    for (j=0; j<sizeof(receivestring1); ++j)
    {
    while (!(UCA1IFG & UCRXIFG)); // Spin until something arrives
    receivestring1[j] = UCA1RXBUF;
    if (receivestring1[j] == '\r')
    break; // quit if we see carriage return
    }
    receivestring1[j] = '\0';
    }

    void uart_init(void)
    {

    P3SEL0 |= (BIT4 + BIT5);

    UCA1CTLW0 = 0;

    UCA1CTLW0 |= UCSWRST;

    UCA1CTLW0 |= (UCSSEL_2 + UCMODE_0);

    UCA1CTLW0 &= ~(UCPEN + UCMSB + UC7BIT + UCSPB + UCSYNC + UCDORM);

    UCA1CTLW1 |= UCGLIT_1;

    UCA1BRW = 0x06;

    UCA1MCTLW |= ((0x22*UCBRS0) + (UCBRF3|UCBRF2|UCBRF0) + UCOS16);

    UCA1ABCTL &= ~(UCABDEN);

    UCA1IRCTL &= ~(UCIREN);

    UCA1CTLW0 &= ~(UCSWRST);

    }

     


    I have given the snippet above.

    while (!(UCA1IFG & UCRXIFG)); 


    The code seems to stuck here all the way. Even after multiple runs, the code seems to stuck there all the way.


    Does the START bit (or byte) has to be sent from the MSP430 to initiate the transmission from the MSP430.

    I suspect that, something is wrong with the start bit.

    Does the direction of the word be changed to MSB first. Does that influence in any way?

     

  • What do you see in receivestring[] when it hangs?

    ----------------

    >for(j=0;j<sizeof(sendstring);j++)


    You've changed sendstring into a pointer, so sizeof() is now 2 (or maybe 4). As a result, you're only sending the first 2 (or 4) bytes of the command. If you want to send by pointers, you need a different strategy, probably by looking for a NUL ('\0') terminator in the string.

    ----------------

    You're not sending any end-of-line indicators as required by the ESP8266 AT document ["4A" Version 3.0.3 Section 2 ("Notice" at the bottom of the page)]. Both the ESP8266 and your receive loop rely on this, which is probably why your code is hanging.

    ----------------

    More generally, this thread seems to have morphed into "How do I use AT commands with the ESP8266?". Two minutes with Google ("esp8266 at commands library") turned up some potentially useful results.

  • The end of line characters was the issue all along.

**Attention** This is a public forum