1
\$\begingroup\$

Setup: There are two MCUs, communicating with each other over UART. Let's call the transmitting MCU as A, and receiving MCU as B. MCU A and B are different. A is SAML22 (using IAR Compiler) and B is EFR32 (using GCC compiler).

Problem: For the MCU B to properly interprete a command, the command must have a line feed character (\n) at the end of the string. MCU A is transmiting the string using interrupt and the physical TX buffer is of 1 byte. Development and testing is done on a Windows machine.

char txCmd[] = "someCommand\n";

But, when the MCU A sends the command to the MCU B, the MCU B is not able to understand the \n at the end of the string.

What have we done so far: We used the dock light software to send the "someCommand\n" and the MCU B detects the line feed character very well and executes the command. We even sent the hex values of the command string with 0x0A at the end, and still the MCU B could not detect it.

What we will be trying: Use the logic analyzer and see what is being transmitted. Append \r\n to the string. Append 0x0D, 0x0A to the string.

Can someone help us to understand and solve this problem? Thanks!

\$\endgroup\$
9
  • 2
    \$\begingroup\$ Are you sure you're actually sending the last byte from MCU A? Have you tried sending some dummy data after the final "\n"? If you stop the UART on the last byte interrupt, it's not necessarily Tx complete interrupt, but FIFO empty interrupt. EDIT: I seee in fact no you have not. How can you form the question "detecting line feed" if you do not know if line feed is sent at all? \$\endgroup\$
    – MrGerber
    Commented Jul 10 at 15:36
  • \$\begingroup\$ I am transmitting the \n character. To verify this, I increased the size of the tx local buffer by 1. However, I will try adding characters after \n and see if they are being trasnmitted. \$\endgroup\$ Commented Jul 10 at 15:44
  • 1
    \$\begingroup\$ I suspect that you are not waiting for the tx shift register to complete. The tx buffer is empty, so you can write another character but the last character will still be in the shift register. \$\endgroup\$
    – RussellH
    Commented Jul 10 at 15:52
  • 2
    \$\begingroup\$ send the data to your computer instead ... you already proved that MCU B has no problem with detecting the line feed character \$\endgroup\$
    – jsotola
    Commented Jul 10 at 16:05
  • 1
    \$\begingroup\$ Great! Correct first step when debugging communication is observability, observability, observability. Sniff your communication, anything else is assumptions. | FYI, you're allowed to post the resolution as an answer and accept your own answer. Actually that would be the preferred way to do it! \$\endgroup\$
    – MrGerber
    Commented Jul 11 at 11:45

1 Answer 1

1
\$\begingroup\$

I did not mention in the question about what method I was using to create the string which I am sending on UART. Please find the code below:

char sourceString[] = "Hi\n"; 
char targetString[100];
int sourceStringLength = strlen(sourceString); 

snprintf(targetString, targetStringLength, sourceString);

The length of the string is 3. The snprintf function works only for n-1 (2 here) characters because it uses the nth position to add a NULL character (\0) to the targetString. The resultant string was targetString[] = "Hi\0". Simply extending the second parameter by 1 worked (targetStringLength+1).

I should have spent good time to understand the snprintf function before using it. This is the lesson #1. And the lesson #2 is, I should have written about the method that I am using to fill my array with the characters.

I am sorry for the incovenience caused and I really appreciate everyone who spent their times for giving the suggestions.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for typing out your answer. Now please tick the checkmark so that your question can be flagged as solved. I'm removing the "solved" paragraph from your question, because that is what your answer is for. \$\endgroup\$
    – Velvet
    Commented Jul 11 at 17:12
  • \$\begingroup\$ Sure. I will be able to do that after 22 hours. Thank you for taking the effort to make necessary edits. \$\endgroup\$ Commented Jul 11 at 17:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.