USART : UART モードUART mode

ここではUSARTモジュールのUARTモードについてのみ説明しています。USARTモジュール全般についてはこちらを参照ください Here, UART mode of USART module is described. Please refer here for overall USART module.

USART: UARTモード レジスタ一覧Mode Register List

機能別レジスタ項目詳細Functional Groupings of Register Items

(勝手な分類であり、公式なものではありません)(This is not universally endorsed)
作成中WIP

ボーレートの送信/受信エラーについてAbout baudrate transmit/receive error

ボーレートとはWhat is baudrate?
ボーレートとは「1秒間に何回複変調を行うか」のこと。たとえばボーレート2400というのは1秒間に2400回の複変調を行うことを示す。このとき1回の複変調で1ビットを表すとすれば2400bpsになる。 Buadrate is a number of modulations/demodulations per a second. For example,baudrate 2400 means 2400 modulations/demodulations per a second. If a modulation/demodulationis defined by a bit, it means 2400bps.

タイミングエラーTiming error
マイコンで複変調を行う際のタイミングは水晶振動子などのクロックが基準になる。つまりボーレートは一般にクロック周波数の整数倍となる(プリスケーラ/分周期がある場合にはそれを考慮した周波数の整数倍)。そうでない場合はタイミングにズレが生じる(タイミングエラー)。このズレが累積ずると正確な情報のやりとりができなくなるため、最小限にとどめる必要がある。 Modulation timing is based on clock frequency. So the baudrate is generallyequal to the integral multiple of the frequency. (When a prescaler or afrequency divider is working, it is equal to the integral multiple of theoutput frequency) If not, it makes a deviation (timing error). If the timingerror is accumulated, information exchange will be failed. So it shouldbe minimized.

変調装置Modulation circuit
MSP430のUARTモジュールには上記の累積するタイミングエラーを最小にする目的で変調装置が組み込まれている。これは簡単に言うと、ビットごとにボーレートをずらす機能だ。

たとえばクロック数32768Hzを用いてボーレート2400で情報を送信する際には32768 / 2400 = 13.65333..となり0.6533..の分だけズレが生じる。これは送信が進むと累積していくエラーである。

図で示すと下図のようになる。一番上の「Precise Timing」は理想的なボーレートタイミングで12ビットを送信した際のもの。1ビットを表すのにそれぞれ13.6533..クロックかかっている。しかし実際は1ビットを表すのにクロックの整数倍を使うしかないため、13.65..に近い整数の、たとえば14クロック分で1ビットを表すことになる(RoughApproximationの図)。すると送信が進むごとに0.6533..のズレが累積し、ストップビットの手前で大きなエラーになってしまっている。これでは受信側で後ろの方のパリティビットやストップビットの判別を誤る可能性が高い。

UARTの変調装置は、このエラーを抑えるために、それぞれのビットに対してクロック数を変調する。一番下のCorrected Timingの図がそれを示しており、1ビットを表すために毎回14クロックではなく、ところどころ13クロックにしている。こうすることでエラーの累積を最小限に抑えることが可能となるのである。

変調制御レジスタのUMCTLは、この変調を加えるか加えないかを、1か0にて示すものである。図の一番下にあるように、UMCTLが0のときは1ビットに13クロックを使い、UMCTLが1のときは1ビットに14クロックを使うことになる。UMCTLは8ビットレジスタなので、8ビット分送信が終わるとまたUMCTLの0ビット目から順に適用される。
MSP430 UART module has a modulation device in order to minimize the timingerror mentioned aboue. In a simple term, it adjusts the baudrate on eachbit.

For example, if you use 32768Hz clock source and transmit information onbaudrate 2400, there is 0.65333.. clock gap per a baudrate (32768/2400= 13.65333..). This will be accumulated if you continue the transmission.

Following figure shows the accumulation of the error. The top "PreciseTiming " case shows 12 bits transmission in an ideal baudrate timing:13.65333.. clocks per a bit. But actually, number of clocks per a bit shouldbe integral multiple, so integer number near 13.6533.. - in this case 14- is used (See Rough approximation figure). Then on each transmission,0.6533... clocks timing deviation is being accumulated, and just beforea stop bit it becomes a huge difference. This will leads to misreadingof a stop bit or a parity bit.

UART module's modulation circuit modulates each bit clock in order to suppressthe error. The bottom figure "Corrected Timing" shows it. Using13 clocks per a bit in spots instead of using 14 clocks. This clock modulationin spots can suppress and minimize the timing error.

Modulation control register UMCTL represents whether the modulation shouldbe applied or not in each bit by "1" or "0". As youcan see in the bottomo figure, 13 clocks are used when UMCTL bit is "0",and 14 clocks are used when it is "1". As UMCTL is a 8bits register,when 8 bits transmission is finished, the sequence is repeated again fromfirst bit.

From slaa024.pdf

UMCTLの算出(送信エラーを最小に)UMCTL calculation (to minimize transimit error)
送信時の累積エラーが最小になる変調レジスタUMCTLの各ビット値を算出する以下のような方法がslaa024.pdfに紹介されている。

例として、ここではクロック 32768Hz ボーレート 4800 での送信を考える。
N = 32768 / 4800 = 6.82667
ここでUBR0, UBR1にはこの分割数を入れるので、UBR0 = 6, UBR1(MSB) = 0 。
Nの小数点以下は1ビット当り0.82667クロック分ズレがあることを示している。このズレを変調で最小限に抑える。8ビット分では 8 x 0.82667= 6.613..となり、変調レジスタで7ビット分変調すれば、エラーが最小になると予想がつく。つまり、送信する8ビットのうち7ビットは7クロックで、残りの1ビットは6クロックで送信することになる。
すると実際のボーレートは

となり、ボーレートのエラーは

となる。

slaa024.pdfのp6-296によれば、UMCTLそれぞれのビットは以下のようなアルゴリズムで算出できるとされている。

ビットごとに0.82667クロックのズレが累積するため、この数値を加算していく際、整数部が増加した場合にはUMCTLのビットを1にする。整数部が増加しない場合にはビットを0にする。

これによって得られるUMCTLでの各ビットにおけるエラーは以下のように算出される。

N = 32768 / 4800 = 6.82667 :理想は1ビットあたり6.82667クロック
UMCTLのビットが1のときは1ビットあたり7クロック、0のときは1ビットあたり6クロックとなる。
0ビット目エラー: (7 - 6.82667) / 6.82667 = 2.54 %
1ビット目エラー: (7 - 6.82667) x 2 / 6.82667 = 5.08 %
2ビット目エラー: (7 - 6.82667) x 3 / 6.82667 = 7.62 %
3ビット目エラー: (7 - 6.82667) x 4 / 6.82667 = 10.2 %
4ビット目エラー:((7 - 6.82667) x 4 + (6-6.82667)) / 6.82667 = -1.95 %
5ビット目エラー:((7 - 6.82667) x 5 + (6-6.82667)) / 6.82667 = 0.59 %
6ビット目エラー:((7 - 6.82667) x 6 + (6-6.82667)) / 6.82667 = 3.13 %
7ビット目エラー:((7 - 6.82667) x 7 + (6-6.82667)) / 6.82667 = 5.66 %

エラーの最大値は3ビット目で10.2%である。

ちなみに上記の式から、j ビット目のエラーは次のように与えられる。

これよりslau049f.pdfなどに記載されている次式が導かれる。

(なおここでは j ビット目のエラーとして表示されているが添字は j ではなく i を用いるのが好ましい。実際例えばslaui208.pdfなどではi ビット目のエラーという表記になっている。以降の説明では 添字 i を用いるので混乱されぬよう。また以降の説明ではBRCLK = fBRCLKと表記する)


なお上記のアルゴリズムは簡単ではあるが、エラーを最小にするものではない。
エラーを最小にするにはもう少し面倒な計算が必要になる。
面倒とはいえ、今の時代はエクセルを用いて簡単に行うことができる。
Download UMCTL Baudrate Calculation Sheet
↑送信時のエラーを最小にするUMCTLのエクセル計算シート。
CALC-1のシートはslaa024.pdfの方法。エラーは最小ではない。
CALC-2のシートはエラーを最小にするUMCTLを総当りで求めるもの。
総当りはスマートではないが、十分早いので問題ない。

USART設定自動コード生成プログラムではこのエクセルシートと同等の計算を行い、送信エラーが最小になるようにUMCTLを算出している。なおボーレートとクロックの組み合わせによっては、送信エラーを最小にするUMCTLの値が複数存在する場合があり、その際には、それらのうち受信エラーが最小となるUMCTLを選択している。
In slaa024.pdf, a method for calculating UMCTL values is described as follows.

For example, think about transmission using 32768Hz clock soucre and 4800baudrate.
N = 32768 / 4800 = 6.82667
Here, division number is stored in UBR0 and UBR1, so UBR0 = 6, UBR1(MSB)= 0.
N's fractional part means 0.82667 clocks gap per a bit. Accumulation ofthe gap should be minimized. As it is 8 bits, it becomes 8 x 0.82667 =6.613..., so you can see that 7 bits modulation is needed to minimize theaccumulated error. That is to say, out of 8 bits transmission, 7 bits shouldconsist of 7 clocks, and the left 1 bit 6 clocks.
Then actual baudrate will be:

And the baudrate error will be:



According to slaa024.pdf, each bit of UMCTL can be calculated by the followingalgorithm.

As 0.82667 clocks gap is accumulated on each bit, set UMCTL bit "1"if the integral part of the accumulated number is increased. If the integralpart is not increased, set "0".

By the method, error on each UMCTL bit can be derived as follows.

N = 32768 / 4800 = 6.82667 : Ideally, 6.82667 clocks per a bit.
When UMCTL's bit is "1", 7 clocks per a bit. when "0",6 clocks per a bit.
0th bit error: (7 - 6.82667) / 6.82667 = 2.54 %
1st bit error: (7 - 6.82667) x 2 / 6.82667 = 5.08 %
2nd bit error: (7 - 6.82667) x 3 / 6.82667 = 7.62 %
3rd bit error: (7 - 6.82667) x 4 / 6.82667 = 10.2 %
4th bit error:((7 - 6.82667) x 4 + (6-6.82667)) / 6.82667 = -1.95 %
5th bit error:((7 - 6.82667) x 5 + (6-6.82667)) / 6.82667 = 0.59 %
6th bit error:((7 - 6.82667) x 6 + (6-6.82667)) / 6.82667 = 3.13 %
7th bit error:((7 - 6.82667) x 7 + (6-6.82667)) / 6.82667 = 5.66 %

The maximum error is 10.2% at 3rd bit.

By the way, you can see the j th bit error can be calculated as follows:

and derive the equation which is shown in sau049f.pdf.

Here, the additional character is "j" but it is better to use"i". Actually such as slaui208.pdf document uses "i".From here, I use "i" as an additional character, so please becareful not to be confused. I also use fBRCLK as BRCLK.


Above algorithm is very easy but it cannot necessarily minimize the error.
To minimize the error, messy calculation is needed.
But these days, spread sheet software such as microsoft's Excel can calculateit easily.

Download UMCTL Baudrate Calculation Sheet
This is UMCTL calculator which minimizes the transmisson error.
CALC-1 sheet calculates UMCTL as the same way described in slaa024.pdf
CALC-2 sheet calculates UMCTL using all possible regression method.
All possible regression method is not smart, but it is fast enough.

The program in USART configuration flow uses the same method as the excel sheet does and calculates the UMCTLwhich minimizes the transmit error. Some combination of baudrate nunberand clock frequency gives multiple UMCTL results. If it occurs, the programchoose one which gives smaller "receive" error.

上記のアルゴリズムで算出したUMCTLでの送信エラー。実は最適なUMCTLは算出されない。UMCTL transmission errors using above algorithm. By this method, optimumUMCTL cannot be calculated.エラーを最小にする計算によるUMCTLでの送信エラー。左図と比較して最大エラーが小さくなっている。Using error minimization approach. Maximum error is suppressed in comparisonwith the left graph.


UMCTLの算出(受信エラーを最小に)UMCTL calculation (to minimize receive error)
次に例としてクロック 32768Hz ボーレート 2400での受信を考える。
N = 32768 / 2400 = 13.6533
つまり約13クロックで1ビットの受信を行うことになる。理想的にはビットの真ん中でそのビットをテストすることが望ましく、理想的な受信タイミングは以下のようになる。
1ビット目: t = 0.5 x (1/2400) [s]
2ビット目: t = (1+0.5) x (1/2400) [s]
3ビット目: t = (2+0.5) x (1/2400) [s]
4ビット目: t = (3+0.5) x (1/2400) [s]
...

つまり理想的な受信タイミングtidealは i ビット目で tideal = (1/Baudrate)(i + 0.5) [s] である。

実際の受信タイミング
実際の受信タイミングのエラーは、スタートビットエッジの同期タイミングエラーと、受信ビットのタイミングエラーの両方を考える必要がある。

(1) スタートビットエッジの同期タイミングエラー
これはボーレートと関係ない-0.5xBRCLK ~ 0.5xBRCLK 以内のズレであり、 最大で
tSYNC = ±1/(0.5xBRCLK) のタイミングエラーが発生する。

(2) 受信ビットのタイミングエラー
スタートビットから始まる受信ビットのタイミングは
1ビット目: trx = tSYNC+ (INT(UCBR/2) + m0) / fBRCLK
2ビット目: trx = tSYNC+ (UCBR+m0 + INT(UCBR/2) + m1) / fBRCLK
3ビット目: trx = tSYNC+ (UCBR+m0 + UCBR+m1 + INT(UCBR/2) + m2) / fBRCLK
4ビット目: trx = tSYNC+ (UCBR+m0 + UCBR+m1 + UCBR+m2 + INT(UCBR/2) + m3) / fBRCLK
...
であり、i ビット目ではslau208.pdfなどにあるような

ただし

というような式になる。
ここでmiは先述したUMCTLの各ビットである

trealとtidealの差がエラーとなる。1ビット当りの時間で規格化する(つまり 1/Baudrateで割る)と、受信タイミングのビットごとのエラーは
(trx - tideal) x Baudrate x 100%
と算出することができる。
tSYNCを除いて上式よりエラーを算出すると

となる。

これはslau049f.pdfにあるような以下の式

と比べ、m0 + int(UBR/2)だけ少ない値となる。

この差異は「ビット中間でのエラーを算出するか」「エッジでのエラーを算出するか」の違いに起因すると思われる。

つまりslau049f.pdfの式はエッジでのエラーを算出しており、他方先ほどの式はビットの中間でのエラーを算出している。データはビット中間でテストされるため、先ほどの式の方がより実用に即した計算であると思われる。

*図はslau049Fより


UMCTL Baudrate Calculation Sheet
こちらのエクセルファイルで、CALC-3のシートは受信時のエラーを最小にするUMCTLを算出するもの。

USART設定自動コード生成プログラムではこのエクセルシートと同等の計算を行い、受信エラーが最小になるようにUMCTLを算出している。なおボーレートとクロックの組み合わせによっては、受信エラーを最小にするUMCTLの値が複数存在する場合があり、その際には、それらのうち送信エラーが最小となるUMCTLを選択している。
Next, think about data receiving using 32768Hz clock source and baudrate2400.
N = 32768 / 2400 = 13.6533
Data is recieved by 13 clocks per a bit. Ideally, it is better to testreceived data in the middle of the bit. So the ideal receive timing is:1st bit: t = 0.5 x (1/2400) [s]
2nd bit: t = (1+0.5) x (1/2400) [s]
3rd bit: t = (2+0.5) x (1/2400) [s]
4th bit: t = (3+0.5) x (1/2400) [s]
...

And ideal receive timing tideal at i th bit is
tideal = (1/Baudrate)(i + 0.5) [s]

Actual reecive timing
Actual receive timing error is a sum of "Start bit edge synchronizationerror" and "Received bit timing error".

(1) Start bit edge synchronization error
This is not related to the baudrate and is within -0.5xBRCLK to 0.5xBRCLKgap. Maximum timing error is:
tSYNC = ±1/(0.5xBRCLK).

(2) Received bit timing error
Received bit timing from start bit is
1st bit: trx = tSYNC+ (INT(UCBR/2) + m0) / fBRCLK
2nd bit: trx = tSYNC+ (UCBR+m0 + INT(UCBR/2) + m1) / fBRCLK
3rd bit: trx = tSYNC+ (UCBR+m0 + UCBR+m1 + INT(UCBR/2) + m2) / fBRCLK
4th bit: trx = tSYNC+ (UCBR+m0 + UCBR+m1 + UCBR+m2 + INT(UCBR/2) + m3) / fBRCLK
...
And at i th bit, it gives (like described in slau208.pdf):

where

Here, mi is UMCTL's each bit.
The error is the difference between treal and tideal. When nomalized by 1 bit, ie divided by 1/Baudrate, each bit error isgiven by:
(trx - tideal) x Baudrate x 100%
Excepting tSYNC, the receiving error can be calculated as:


There is m0 + int(UBR/2) difference to the equation described in slau049f.pdf:


This may be attributed to the difference between "Error at the middlepoint of a bit" and "Error at the edge of a bit".

An equation described in slau049f.pdf calculates error at the edge of abit and the equation described above does at the middle of a bit.
As a bit is ckecked at the middle point, so the previous equation wouldbe better.

*Figure is from slau049F