Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > can I change UART baud rate in run time?

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



can I change UART baud rate in run time?
Moderator:
RKRM

Post Reply
Follow this topic



can I change UART baud rate in run time?

boulder posted on 14 Mar 2012 3:27 PM PST
Top Contributor
35 Forum Posts

 

I read through API doc, but I can not find it. Is there a way I can configure the baud rate by software?

Thanks,




Re: can I change UART baud rate in run time?

Bob Marlowe posted on 15 Mar 2012 12:14 AM PST
Top Contributor
1768 Forum Posts

When you set the clock of the UART to external, you have to provide your own (with a pwm for instance) then you have programatically full control over the frequency/Baud-rate.

 

Bob



Re: can I change UART baud rate in run time?

boulder posted on 16 Mar 2012 09:10 AM PST
Top Contributor
35 Forum Posts

 I see, I will use a Clock then set divider to adjust the frequency. Hope it will work.

Thanks,



Re: can I change UART baud rate in run time?

Bob Marlowe posted on 16 Mar 2012 09:17 AM PST
Top Contributor
1768 Forum Posts

Don't worry, it will!

Bob



Re: can I change UART baud rate in run time?

boulder posted on 23 Mar 2012 02:01 PM PST
Top Contributor
35 Forum Posts

 I thought this should be easy, but I got stuck again

 

the external clock is 1.8432MHz and I have the following code:

 

        UART_CLOCK_Start();

switch (BaudRate) {

case BR_9600:

UART_CLOCK_SetDividerValue(11);

break;

case BR_19200:

UART_CLOCK_SetDividerValue(5);

break;

case BR_38400:

UART_CLOCK_SetDividerValue(2);

break;

case BR_57600:

UART_CLOCK_SetDividerValue(1);

break;

case BR_115200:

break;

}

 

work great with 115200, but as soon as I change to any other baud rate, for example 57600, then my UART ISR will be triggered twice if I press keyboard once. For example, I type "U", (0x55), and my ISR pick up 0xB8 the first time, then 0x0 the second time.

I tried replacing CLOCK_SetDividerValue() with Clock_SetDivider(), does not make any difference.

 



Re: can I change UART baud rate in run time?

Gautam Das posted on 25 Mar 2012 12:15 AM PST
Cypress Employee
742 Forum Posts

Hi Boulder,

 

Is the clock exactly 1.8432MHz? If it is derived from one of the internal clocks, then please look at the clock tree to know what the actual value of the clock is, based on the divider used.

Can you please upload the project, or atleast the project containing the UART part. That'll make it easier to find out the cause of the issue.



Re: can I change UART baud rate in run time?

Bob Marlowe posted on 25 Mar 2012 02:07 AM PST
Top Contributor
1768 Forum Posts

The clock will be 1.846 MHz when you remove the 5% checkbox, that is an error of about 0,2%. That will be quite sufficant for UART specs.

 

Bob



Re: can I change UART baud rate in run time?

Bob Marlowe posted on 25 Mar 2012 03:28 AM PST
Top Contributor
1768 Forum Posts

Obvously the UART has to be stopped and started again. Have a look at the attached (working) project. It did not work, when the UART was free running.

 

Happy coding

Bob



Re: can I change UART baud rate in run time?

boulder posted on 26 Mar 2012 11:18 AM PST
Top Contributor
35 Forum Posts

 I located the problem:

The problem is the clock itself. I routed the UartClock to a I/O pin and monitored the signal on a scope. When the baud rate is 115200, so no divide, I can see the 1.8432 MHz signal on the scope. However if I set baud rate to be 9600, and I set a break point on UART_CLOCK_SetDivider(11); the the break point trigger, I can still see the 1.8432MHz, and one single step after the setDivider (or SetDividerValue()) the wave on scope become 1.99MHz. If I use 19200, then after SetDivider(5), I am measuring 4MHz, so if the baud rate is 38400, then I got 8MHz.

It seems the API doc is backward. It is multiply, not divide. I am going to configure it as multiply and give it another try.



Re: can I change UART baud rate in run time?

boulder posted on 26 Mar 2012 11:57 AM PST
Top Contributor
35 Forum Posts

 purely by trial and error, I found the parameters to setup the baud rate:

the clock is set to 9600 x 16 = 153.6KHz

switch (BaudRate) {

case BR_9600:

// do nothing

break;

case BR_19200:

UART_CLOCK_SetDivider(78);

break;

case BR_38400:

UART_CLOCK_SetDivider(38);

break;

case BR_57600:

UART_CLOCK_SetDivider(25);

break;

case BR_115200:

UART_CLOCK_SetDivider(12);

break;

default:

break;

}

 

by setting to these numbers, I can measure the correct clock on scope and also talking to my computer with corrsponding baud rate. I guess I will need to talk to a FAE about this issue, the only thing I am worried about now is that a year from now, with a different patch of chip, all the constant number setting will still give me correct baud rate.



Re: can I change UART baud rate in run time?

Bob Marlowe posted on 26 Mar 2012 12:45 PM PST
Top Contributor
1768 Forum Posts

It is not multiply, it is easier:

The clock is derived from the 24MHz Bus-clock. Divider set to 13 gives the reqiured 1.843 MHz

When the divider is set to 26 you get half the frequency, just as desired.

No multiplication...

 

Bob



Re: can I change UART baud rate in run time?

boulder posted on 26 Mar 2012 12:55 PM PST
Top Contributor
35 Forum Posts

 so the divider is based on 24MHz, and it has nothing to do with the my Uart_Clock...I thought the reference is the uart_clock. That's certainly confusing.

Well at least now these number make some sense now. Thanks for the help!



Re: can I change UART baud rate in run time?

Gautam Das posted on 26 Mar 2012 09:21 PM PST
Cypress Employee
742 Forum Posts

Hi boulder,

 

As Bob has correctly pointed out, you need to set the divider taking into account the source from which the clock is derived.

To know the source of the clock used, you must look into the "Clocks" section in the .cydwr and set the appropriate divider. You can also force the Source of the clock from the configuration window of the clock.






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20