1
\$\begingroup\$

Through the C# interface i want to change the baudrate, to do that ofcurse i have to change the baudrate in the other side which is MSP430 using CodeCompserStudio and to send an Ack to PC then I implement the changes also in C#, but when i change the baudrate in CCS i cant then send the Ack message properly because the baudrate does not match! and because of that I get a non-sense message instead of the "Ack" which prevents changing the baudrate in the other side (PC) because it was waiting for the "Ack". I'v tried to send the "Ack" message before changing the baudrate in CCS, but it did not help. This is my pseudo code: Initially in C# i just hit a button which sends a proper command to CCS to change the baudrate. Then in CCS after or before changing the baudrate we have to send "Ack":

while(!(UCA1IFG & UCTXIFG));    // in this case we send the Ack msg before changing the cnfgs.
UCA1TXBUF='*';           // '*' indicates "Ack"
UCA1BR0 = 3;                 // 9600 baudrate (32k ACLK)
UCA1BR1 = 0;
UCA1MCTL = UCBRS_3+UCBRF_0;

in C#:

private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   string receivedData = SerialPort.ReadExisting();
   if (receivedData == "*") // BaudRate Acknowledge
       SerialPort.BaudRate = Convert.ToInt16(BaudRate.SelectedItem);
}

any ideas?

\$\endgroup\$
2
\$\begingroup\$
  1. Send command to change baudrate from PC to microcontroller on the old baudrate.
  2. Send ACK for the command on old baudrate from microcontroller to the PC.
  3. Change baudrate on PC and have the microcontroller execute the baudrate change.
  4. Wait x ms (to be sure both sides are done with the baudrate change).
  5. Send some data from microcontroller to PC on the new baudrate to check baudrate change is succesfull.
  6. PC doesn't receive data within x+y ms, go back to old baudrate.
  7. If the microcontroller doesn't get an ACK to the data from step 5 within z ms change back to old baudrate

Waiting times x, y and z will be need to be determined based on how fast both sides can switch baudrate and any possible delay caused by the whole chain (Program, OS, hardware). Here i would say it's better to wait a bit too long than too short, unless there is some reason you need to change quickly.

Step 5 to 7 are there to ensure you'll be pretty sure it will be possible to communicate after x + y + z ms, be it on the new baudrate or the old one.

Also from code you post i understand the ACK message is a single "*"? If so it might be an good idea to expand the ACK message so that the other side knows what message is getting acknowledged.

edit:

In the code for you microcontroller you put the * in the send buffer and directly after that you start fiddling with the baudrate registers. Meaning you're changing the baudrate while its sending the ACK. The easiest solution is to have the microcontroller wait untill it's done sending the ACK with a: while(UCA1STAT1 & 0x01);. UCA1STAT is the status register of the UART (Userguide for the MSP430 family page 914). And bit 0 will be 1 if the UART is busy sending or receiving.

A word of warning for the solution above, if for some reason the UART busy bit stays high the microcontroller will hang. Plus the microcontroller can't do anything else in the meantime. The term for this is blocking code/function.

\$\endgroup\$
  • \$\begingroup\$ but isn't this exactly what I did? I sent the command to microcont., sent back an Ack char (at this point the PC start changing the baudrate right?) and eventually change it in microcont. and yes its a single '*' , I really can sent "Ack" for instance but there is no need for that, sending a single char is faster.. \$\endgroup\$ – Renya Karasuma Jul 23 '14 at 0:20
  • 1
    \$\begingroup\$ Ah misunderstood you're question. Let me get the userguide for the msp430. What you're missing in you code for the microcontroller is waiting for the UART to be done with sending the ACK message. "while(UCA1STAT & 0x01);" should fix that. It's not the most beautyfull way to do it. Because that will 'hang' the microcontroller untill it's done sending the ACK \$\endgroup\$ – user17645 Jul 23 '14 at 0:38
  • \$\begingroup\$ why not adding: while(!(UCA1IFG & UCTXIFG)); before changing the baudrate? (although i tried and it didnt help!)! \$\endgroup\$ – Renya Karasuma Jul 23 '14 at 0:46
  • \$\begingroup\$ Thank you very much! it's working! but why it's not working with: while(!(UCA1IFG & UCTXIFG)); ? and it is working with your command!? what is the difference? \$\endgroup\$ – Renya Karasuma Jul 23 '14 at 0:51
  • \$\begingroup\$ Because with that line you stay in the while loop until there is no TX interrupt pending. And the UART gives an interrupt on a empty buffer. so remove the "!" and it should work. Unless i'm missing something, I haven't worked with MSP430 microcontrollers that much. \$\endgroup\$ – user17645 Jul 23 '14 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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