{"id":93,"date":"2020-09-04T03:10:43","date_gmt":"2020-09-04T03:10:43","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=93"},"modified":"2020-09-04T03:10:43","modified_gmt":"2020-09-04T03:10:43","slug":"working-with-universal-asynchronous-receiver-transmitter-uart","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=93","title":{"rendered":"Working with Universal Asynchronous Receiver\/ Transmitter (UART)"},"content":{"rendered":"\n<p>To transfer data, computers adopt two method: serial and parallel. In serial communication data is sent a bit at a time (1 line)while in parallel communication 8 or more lines are often used to transfer data. The UART is a form of serial communication. When distance is short, digital signal can be transferred as it is on a single wire and require no modulation. This is how PC keyboard transfer data between the keyboard and motherboard. For long distances like telephone lines, serial data communication requires a modem to modulate.We will not go into the details of this in this lesson, this shall be left for subsequent lessons. <\/p>\n\n\n\n<p><strong>Synchronous vs. Asynchronous<\/strong><\/p>\n\n\n\n<p>Serial communication operate in two ways : synchronous and asynchronous. In the synchronous mode, a block of data is sent at a time while in the asynchronous mode, a single byte of data is sent at a time. It is possible to write code to provide both the synchronous and asynchronous serial communication functions however this could turn out tedious and over complicated therefore, special chips are manufactured to perform these functions. When these chips are added to a microcontroller, they become known as Serial Communication Interface (SCI). The chip that provides the the asynchronous communication (<strong>UART<\/strong>) is the main theme of this lesson. The chip for synchronous communication is known as the\u00a0<strong>USART<\/strong>\u00a0and shall left for another lesson. <\/p>\n\n\n\n<p><strong>Asynchronous Communication Data Framing<\/strong><\/p>\n\n\n\n<p>The data is received in zeros and ones format, in order to make sense of the data, the sender and the receiver must agree on a set of rules i.e. a protocol, on how data is packed, how many bits constitute a character and when data begins and ends.<\/p>\n\n\n\n<p><strong>The Protocol<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Start and Stop bits<\/strong><\/li><\/ol>\n\n\n\n<p>Asynchronous serial communication is widely used in character transfer. Each character is packed between start and stop bits. This is known as the framing.<\/p>\n\n\n\n<p><strong>start bit<\/strong>&nbsp;: &nbsp;Always&nbsp;<strong>1<\/strong>&nbsp;bit, value is always&nbsp;<strong>0<\/strong><\/p>\n\n\n\n<p><strong>stop bit<\/strong>: &nbsp;<strong>&nbsp;1<\/strong>&nbsp;or&nbsp;<strong>2<\/strong>&nbsp;bits, value is always<strong>&nbsp;1<\/strong><\/p>\n\n\n\n<p>Lets take an example. Lets say we are transferring ASCII character \u2018A\u2019 <\/p>\n\n\n\n<p>its binary code is 0100 0001 and 0x41 in hex.Furthermore, it is framed between a start bit and 2 stop bits. This is what its frame will look like.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/1frame-750x234-1.png\" alt=\"\" width=\"710\" height=\"221\"\/><\/figure>\n\n\n\n<p>&nbsp;2.<strong>&nbsp;Parity bit<\/strong><\/p>\n\n\n\n<p>Some systems require parity bit in order to maintain data integrity.The parity bit of a character byte is included in the data frame after the stop bit(s).<\/p>\n\n\n\n<p>&nbsp;3.<strong>Rate of data transfer : Baud rate\/bps<\/strong><\/p>\n\n\n\n<p>We shall explain this concept with an example. Lets say we are asked to calculate the number of bits used \u00a0and time taken in transferring 50 pages of text, each with 80\u00d725 characters. Assuming 8 bits per character and 1 stop bit<\/p>\n\n\n\n<p><em>solution<\/em><\/p>\n\n\n\n<p>For each character a total of 10 bits is used ( 1 start bit, 8 bits character, 1 stop bit).<\/p>\n\n\n\n<p>Therefore total number of bits &nbsp;=&nbsp;80 x 25 x10 = 20,000 bits per page<\/p>\n\n\n\n<p>50 pages &nbsp;implies,&nbsp;50\u00d720,000 &nbsp;= &nbsp;1,000,000 bits.<\/p>\n\n\n\n<p>Therefore it will take 1 million bits to transfer &nbsp;this information.<\/p>\n\n\n\n<p>The time it takes to transfer the entire data using<\/p>\n\n\n\n<p><strong>a<\/strong>. 9600 baudrate implies, &nbsp;1,000,000 \/ 9600 = 204 seconds<\/p>\n\n\n\n<p><strong>b<\/strong>. 57,600 baudrate implies&nbsp;1,000,000 \/57,600 = 17 seconds<\/p>\n\n\n\n<p>Baudrate simply means the transfer of bits per second. There are various standardized baudrate we can choose from when we program our serial communication devices. \u00a0The key thing is, both communication devices must have the same baudrate. We shall see this later on in our example code.<\/p>\n\n\n\n<p><strong>Programming the ARM Cortex-M UART<\/strong><\/p>\n\n\n\n<p>ARM Cortex \u2013 \u00a0microconrtollers from different vendors come with more than 1 internal UARTs. For instance the the ARM Cortex-M4 Tiva Launchpad from Texas Instrument comes with 8 UARTs.<\/p>\n\n\n\n<p>Two registers are used to set the baudrate:<\/p>\n\n\n\n<p>-UART Integer Baudrate Divisor<\/p>\n\n\n\n<p>-UART Fraction Baudrate Divisor<\/p>\n\n\n\n<p>To get your desired baudrate, its simply &nbsp;SystemClock \/ (16 x ClkDiv)<\/p>\n\n\n\n<p>SystemClock can be \u00a0: XTAL,PLL, internal RC etc.<\/p>\n\n\n\n<p>ClkDiv \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0: \u00a0value we load into the divisor register<\/p>\n\n\n\n<p><strong>Steps to configure UART and Transmit a byte of data\u00a0<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Provide clock to UART by writing 1 to UART clock gating register<\/li><li>Provide clock to the GPIO port the UART function is connected .<\/li><li>Disable the UART by writing 0 to the UART control register.<\/li><li>Write the integer portion of the Baudrate to the UARTIBRD register.<\/li><li>Write the fractional portion of the Baudrate to the UARTFBRD register.<\/li><li>Select the system clock as UART clock source .<\/li><\/ol>\n\n\n\n<p><strong>Important Registers<\/strong><\/p>\n\n\n\n<p>Before we move on to provide example source code, there are 3 important registers we need to take a closer look at.<\/p>\n\n\n\n<p><strong>UART Control Register<\/strong><\/p>\n\n\n\n<p>As mentioned in the earlier section, in calculating the baudrate, the default system clock is divided by 16, however, we can decide to divide it by 8 by the setting the 1 to the HSE bit of the UART control register. The HSE implies, High-Speed-Enable.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/uartctl-750x140-1.png\" alt=\"\" width=\"822\" height=\"153\"\/><\/figure>\n\n\n\n<p>Important bits in the UART control register include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>UARTEN :<\/strong>\u00a0To enable and disable UART<\/li><li><strong>HSE :<\/strong>\u00a0High speed enable, to run high baudrate with low frequency<\/li><li><strong>RXE:<\/strong>\u00a0This must be enabled in order to receive data<\/li><li><strong>TXE:<\/strong>\u00a0This must be enabled in order to transmit data<\/li><\/ul>\n\n\n\n<p><strong>UART Line Control Register<\/strong><\/p>\n\n\n\n<p>This register is used to set the number bits per character i.e. data length<\/p>\n\n\n\n<p>Also used to set the stop bit. This what the register looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/uart_line_r-750x82-1.png\" alt=\"\" width=\"890\" height=\"97\"\/><\/figure>\n\n\n\n<p>The default stop bit is\u00a0<strong>1<\/strong>\u00a0to set the stop bit to\u00a0<strong>2<\/strong>\u00a0\u00a0set\u00a0<strong>STP2\u00a0<\/strong>(D3) bit =<strong>1<\/strong><\/p>\n\n\n\n<p><strong>FEN<\/strong>(D4) : It is known as the FIFO enable.<\/p>\n\n\n\n<p>Because of the 1 byte transfer \/ receive limitation, the CPU keeps getting interrupted every time a single byte of data is transferred. This could create bottleneck &nbsp;especially in multitasking systems. Therefore &nbsp;cortex -microcontrollers provide FIFO buffers to store data for transmission. The ARM Cortex-M4 TM4C123 &nbsp;provides a 16-bytes FIFO buffer to store data for transmission and another 16-bytes FIFO buffer to save all received data.<\/p>\n\n\n\n<p>When FEN is enabled, we can write up 16-bytes of data block into the transmission FIFO buffer and let it transfer a byte a time.<\/p>\n\n\n\n<p>Also, the developer may set threshold for UART to notify the CPU when level of FIFO passes threshold.<\/p>\n\n\n\n<p><strong>WLEN (D6 \u2013 D5)<\/strong>&nbsp;&nbsp;: Word length. this is used to set the number of bits per character. &nbsp;Character data in each frame can be&nbsp;5,6,7&nbsp;or&nbsp;8.&nbsp;Generally, 8 bits is used. &nbsp;The default value is 5 on some cortex-microcontrollers and must be set to 8 simply by setting&nbsp;<strong>D5 =1<\/strong>&nbsp;and&nbsp;<strong>D6=1<\/strong>.<\/p>\n\n\n\n<p><strong>UART Data Register\u00a0<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/uart_data_rrrr-750x76-1.png\" alt=\"\" width=\"955\" height=\"96\"\/><\/figure>\n\n\n\n<p>Data to be transmitted is placed in the UART data register<\/p>\n\n\n\n<p>Only the lower 8-bits are used<\/p>\n\n\n\n<p>A write to this register initializes a transmission from the UART<\/p>\n\n\n\n<p>A read \u00a0receives transmitted bytes. For read, the lower 8-bits hold the received byte, other 4 bits are used for error detection e.g. parity, framing etc.<\/p>\n\n\n\n<p><strong>Programming UART on \u00a0ARM Cortex-m4 TM4C123 Tiva C LaunchPad\u00a0<\/strong><\/p>\n\n\n\n<p>In this example the TM4C123 micocontroller communicates with the PC by means of UART communication protocol. On the PC side, we need to download a serial terminal program to receive and send data from the pc. Tera Term is a highly recommended one, it can be downloaded just by doing a google search. After downloading and installing, follow the setup depicted in the two figures below.<\/p>\n\n\n\n<p>When the program is opened, select the \u201c<strong>Serial<\/strong>\u201d \u00a0radio-button and in the drop-down select \u201c<strong>COMx :Stellaris Virtual Serial Port<\/strong>\u201d or any other port name you think relates to your microcontroller (the names in the drop-down vary sometimes)<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/tera-300x155-1.png\" alt=\"\" width=\"320\" height=\"165\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/Screen-Shot-2016-08-08-at-6.54.49-PM-300x185-1.png\" alt=\"\" width=\"337\" height=\"208\"\/><\/figure>\n\n\n\n<p>This is the default setting but just to sure &nbsp;go to &nbsp;<strong>Setup -&gt; Serial Port\u2026<\/strong><\/p>\n\n\n\n<p>and verify.<\/p>\n\n\n\n<p>In the program presented below, when any key is pressed in Tera terminal, the LEDs on the TM4C123 Tiva C launchpad either change color or go off depending on the ascii value of the key pressed .&nbsp;<\/p>\n\n\n\n<p>Note : The pressed key will not be printed in the terminal, just the LEDs will change.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;darcula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&lt;br&gt;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;TM4C123.h&quot; \/\/ Device header\n#include &lt;stdint.h&gt;\n \nchar UART0Rx(void); \nvoid delayMs(int n);\n \nint main(void) \n{\n char c;\n \n SYSCTL-&gt;RCGCUART |= 1; \/* provide clock to UART0 *\/\n SYSCTL-&gt;RCGCGPIO |= 1; \/* enable clock to PORTA *\/\n SYSCTL-&gt;RCGCGPIO |= 0x20; \/* enable clock to PORTF *\/\n \n \/* UART0 initialization *\/\n UART0-&gt;CTL = 0; \/* disable UART0 *\/\n UART0-&gt;IBRD = 104; \/* 16MHz\/16=1MHz, 1MHz\/104=9600 baud rate *\/\n UART0-&gt;FBRD = 11; \/* fraction part, see Example 4-4 *\/\n UART0-&gt;CC = 0; \/* use system clock *\/\n UART0-&gt;LCRH = 0x60; \/* 8-bit, no parity, 1-stop bit, no FIFO *\/\n UART0-&gt;CTL = 0x301; \/* enable UART0, TXE, RXE *\/\n \n \/* UART0 TX0 and RX0 use PA0 and PA1. Set them up. *\/\n GPIOA-&gt;DEN = 0x03; \/* Make PA0 and PA1 as digital *\/\n GPIOA-&gt;AFSEL = 0x03; \/* Use PA0,PA1 alternate function *\/\n GPIOA-&gt;PCTL = 0x11; \/* configure PA0 and PA1 for UART *\/\n \n GPIOF-&gt;DIR = 0x0E; \/* configure Port F to control the LEDs *\/\n GPIOF-&gt;DEN = 0x0E;\n GPIOF-&gt;DATA = 0;\n \n for(;;)\n {\n c = UART0Rx(); \/* get a character from UART *\/\n GPIOF-&gt;DATA = c &lt;&lt; 1; \/* shift left and write it to LEDs *\/ } } \/* UART0 Receive *\/ \/* This function waits until a character is received then returns it. *\/ char UART0Rx(void) { char c; while((UART0-&gt;FR &amp; 0x10) != 0); \/* wait until the buffer is not empty *\/\n c = UART0-&gt;DR; \/* read the received data *\/\n return c; \/* and return it *\/\n}\n \n\/* Append delay functions and SystemInit() here *\/\nvoid delayMs(int n){\nint i,j; \n \n for(i=0;i&lt;n;i++){\n for(j=0;j&lt;3180;j++)\n {}\n }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>To transfer data, computers adopt two method: serial and parallel. In serial communication data is sent a bit at a time (1 line)while in parallel communication 8 or more lines are often used to transfer data. The UART is a form of serial communication. When distance is short, digital signal can be transferred as it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[10],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-peripheral-drivers","tag-peripheral-drivers"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/93"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=93"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}