+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
99480b6
master
1// UART.c 2// Runs on LM4F120/TM4C123 3// Use UART0 to implement bidirectional data transfer to and from a 4// computer running PuTTy. Interrupts and software FIFOs are used. 5// Daniel Valvano 6// September 20, 2018 7// Modified by EE345L students Charlie Gough && Matt Hawk 8// Modified by EE345M students Agustinus Darmawan && Mingjie Qiu 9 10/* This example accompanies the book 11"Embedded Systems: Real Time Interfacing to Arm Cortex M Microcontrollers", 12ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2018 13Program 5.11 Section 5.6, Program 3.10 14 15Copyright 2018 by Jonathan W. Valvano, valvano@mail.utexas.edu 16You may use, edit, run or distribute this file 17as long as the above copyright notice remains 18THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED 19OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 20MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. 21VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, 22OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 23For more information about my classes, my research, and my books, see 24http://users.ece.utexas.edu/~valvano/ 25*/ 26 27// U0Rx (VCP receive) connected to PA0 28// U0Tx (VCP transmit) connected to PA1 29#include <stdint.h> 30#include "../inc/tm4c123gh6pm.h" 31 32#include "UART.h" 33 34#define NVIC_EN0_INT5 0x00000020 // Interrupt 5 enable 35 36#define UART_FR_RXFF 0x00000040 // UART Receive FIFO Full 37#define UART_FR_TXFF 0x00000020 // UART Transmit FIFO Full 38#define UART_FR_RXFE 0x00000010 // UART Receive FIFO Empty 39#define UART_LCRH_WLEN_8 0x00000060 // 8 bit word length 40#define UART_LCRH_FEN 0x00000010 // UART Enable FIFOs 41#define UART_CTL_UARTEN 0x00000001 // UART Enable 42#define UART_IFLS_RX1_8 0x00000000 // RX FIFO >= 1/8 full 43#define UART_IFLS_TX1_8 0x00000000 // TX FIFO <= 1/8 full 44#define UART_IM_RTIM 0x00000040 // UART Receive Time-Out Interrupt 45// Mask 46#define UART_IM_TXIM 0x00000020 // UART Transmit Interrupt Mask 47#define UART_IM_RXIM 0x00000010 // UART Receive Interrupt Mask 48#define UART_RIS_RTRIS 0x00000040 // UART Receive Time-Out Raw 49// Interrupt Status 50#define UART_RIS_TXRIS 0x00000020 // UART Transmit Raw Interrupt 51// Status 52#define UART_RIS_RXRIS 0x00000010 // UART Receive Raw Interrupt 53// Status 54#define UART_ICR_RTIC 0x00000040 // Receive Time-Out Interrupt Clear 55#define UART_ICR_TXIC 0x00000020 // Transmit Interrupt Clear 56#define UART_ICR_RXIC 0x00000010 // Receive Interrupt Clear 57 58 59 60void DisableInterrupts (void );// Disable interrupts 61void EnableInterrupts (void );// Enable interrupts 62long StartCritical (void );// previous I bit, disable interrupts 63void EndCritical (long sr );// restore I bit to previous value 64void WaitForInterrupt (void );// low power mode 65#define FIFOSUCCESS 1 // return value on success 66#define FIFOFAIL 0 // return value on failure 67 68// Two-index implementation of the receive FIFO 69// can hold 0 to RXFIFOSIZE elements 70#define RXFIFOSIZE 64 // must be a power of 2 71uint32_t volatile RxPutI ;// put next 72uint32_t volatile RxGetI ;// get next 73char static RxFifo [RXFIFOSIZE ]; 74 75// initialize index FIFO 76void RxFifo_Init (void ){long sr ; 77sr = StartCritical ();// make atomic 78RxPutI = RxGetI = 0 ;// Empty 79EndCritical (sr ); 80} 81// add element to end of index FIFO 82// return FIFOSUCCESS if successful 83int RxFifo_Put (char data ){ 84if ((RxPutI - RxGetI )& ~(RXFIFOSIZE - 1 )){ 85return (FIFOFAIL );// Failed, fifo full 86 } 87RxFifo [RxPutI & (RXFIFOSIZE - 1 )]= data ;// put 88RxPutI ++ ;// Success, update 89return (FIFOSUCCESS ); 90} 91// remove element from front of index FIFO 92// return FIFOSUCCESS if successful 93int RxFifo_Get (char * datapt ){ 94if (RxPutI == RxGetI ){ 95return (FIFOFAIL );// Empty if RxPutI=RxGetI 96 } 97* datapt = RxFifo [RxGetI & (RXFIFOSIZE - 1 )]; 98RxGetI ++ ;// Success, update 99return (FIFOSUCCESS ); 100} 101// number of elements in index FIFO 102// 0 to RXFIFOSIZE-1 103uint32_t RxFifo_Size (void ){ 104return ((uint32_t )(RxPutI - RxGetI )); 105} 106 107// Two-index implementation of the transmit FIFO 108// can hold 0 to TXFIFOSIZE elements 109#define TXFIFOSIZE 64 // must be a power of 2 110uint32_t volatile TxPutI ;// put next 111uint32_t volatile TxGetI ;// get next 112char static TxFifo [TXFIFOSIZE ]; 113 114// initialize index FIFO 115void TxFifo_Init (void ){long sr ; 116sr = StartCritical ();// make atomic 117TxPutI = TxGetI = 0 ;// Empty 118EndCritical (sr ); 119} 120// add element to end of index FIFO 121// return FIFOSUCCESS if successful 122int TxFifo_Put (char data ){ 123if ((TxPutI - TxGetI )& ~(TXFIFOSIZE - 1 )){ 124return (FIFOFAIL );// Failed, fifo full 125 } 126TxFifo [TxPutI & (TXFIFOSIZE - 1 )]= data ;// put 127TxPutI ++ ;// Success, update 128return (FIFOSUCCESS ); 129} 130// remove element from front of index FIFO 131// return FIFOSUCCESS if successful 132int TxFifo_Get (char * datapt ){ 133if (TxPutI == TxGetI ){ 134return (FIFOFAIL );// Empty if TxPutI=TxGetI 135 } 136* datapt = TxFifo [TxGetI & (TXFIFOSIZE - 1 )]; 137TxGetI ++ ;// Success, update 138return (FIFOSUCCESS ); 139} 140// number of elements in index FIFO 141// 0 to TXFIFOSIZE-1 142uint32_t TxFifo_Size (void ){ 143return ((uint32_t )(TxPutI - TxGetI )); 144} 145 146// Initialize UART0 147// Baud rate is 115200 bits/sec 148// assuming 80 MHz bus clock 149void UART_Init (uint32_t priority ){ 150SYSCTL_RCGCUART_R |=0x01 ;// activate UART0 151SYSCTL_RCGCGPIO_R |=0x01 ;// activate port A 152RxFifo_Init ();// initialize empty FIFOs 153TxFifo_Init (); 154UART0_CTL_R &= ~UART_CTL_UARTEN ;// disable UART 155UART0_IBRD_R = 43 ;// IBRD = int(80,000,000 / (16 * 115,200)) = int(43.403) 156UART0_FBRD_R = 26 ;// FBRD = round(0.403 * 64) = 25 157// 8 bit word length (no parity bits, one stop bit, FIFOs) 158UART0_LCRH_R = (UART_LCRH_WLEN_8 |UART_LCRH_FEN ); 159UART0_IFLS_R &= ~0x3F ;// clear TX and RX interrupt FIFO level fields 160// configure interrupt for TX FIFO <= 1/8 full 161// configure interrupt for RX FIFO >= 1/8 full 162UART0_IFLS_R += (UART_IFLS_TX1_8 |UART_IFLS_RX1_8 ); 163// enable TX and RX FIFO interrupts and RX time-out interrupt 164UART0_IM_R |= (UART_IM_RXIM |UART_IM_TXIM |UART_IM_RTIM ); 165UART0_CTL_R |=0x301 ;// enable UART 166GPIO_PORTA_AFSEL_R |=0x03 ;// enable alt funct on PA1-0 167GPIO_PORTA_DEN_R |=0x03 ;// enable digital I/O on PA1-0 168// configure PA1-0 as UART 169GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R & 0xFFFFFF00 )+ 0x00000011 ; 170GPIO_PORTA_AMSEL_R = 0 ;// disable analog functionality on PA 171// UART0=priority 2 172NVIC_PRI1_R = (NVIC_PRI1_R & 0xFFFF00FF )|0x00004000 ;// bits 13-15 173NVIC_EN0_R = NVIC_EN0_INT5 ;// enable interrupt 5 in NVIC 174EnableInterrupts (); 175} 176// copy from hardware RX FIFO to software RX FIFO 177// stop when hardware RX FIFO is empty or software RX FIFO is full 178void static copyHardwareToSoftware (void ){ 179char letter ; 180while (((UART0_FR_R & UART_FR_RXFE )== 0 )&& (RxFifo_Size ()< (RXFIFOSIZE - 1 ))){ 181letter = UART0_DR_R ; 182RxFifo_Put (letter ); 183 } 184} 185// copy from software TX FIFO to hardware TX FIFO 186// stop when software TX FIFO is empty or hardware TX FIFO is full 187void static copySoftwareToHardware (void ){ 188char letter ; 189while (((UART0_FR_R & UART_FR_TXFF )== 0 )&& (TxFifo_Size ()> 0 )){ 190TxFifo_Get (& letter ); 191UART0_DR_R = letter ; 192 } 193} 194// input ASCII character from UART 195// spin if RxFifo is empty 196char UART_InChar (void ){ 197char letter ; 198while (RxFifo_Get (& letter )== FIFOFAIL ){}; 199return (letter ); 200} 201// input ASCII character from UART 202// return 0 if RxFifo is empty 203char UART_InCharNonBlock (void ){ 204char letter ; 205if (RxFifo_Get (& letter )== FIFOFAIL ){ 206return 0 ;// no data 207 } 208return (letter ); 209} 210// output ASCII character to UART 211// spin if TxFifo is full 212void UART_OutChar (char data ){ 213while (TxFifo_Put (data )== FIFOFAIL ){}; 214UART0_IM_R &= ~UART_IM_TXIM ;// disable TX FIFO interrupt 215copySoftwareToHardware (); 216UART0_IM_R |=UART_IM_TXIM ;// enable TX FIFO interrupt 217} 218// at least one of three things has happened: 219// hardware TX FIFO goes from 3 to 2 or less items 220// hardware RX FIFO goes from 1 to 2 or more items 221// UART receiver has timed out 222void UART0_Handler (void ){ 223if (UART0_RIS_R & UART_RIS_TXRIS ){// hardware TX FIFO <= 2 items 224UART0_ICR_R = UART_ICR_TXIC ;// acknowledge TX FIFO 225// copy from software TX FIFO to hardware TX FIFO 226copySoftwareToHardware (); 227if (TxFifo_Size ()== 0 ){// software TX FIFO is empty 228UART0_IM_R &= ~UART_IM_TXIM ;// disable TX FIFO interrupt 229 } 230 } 231if (UART0_RIS_R & UART_RIS_RXRIS ){// hardware RX FIFO >= 2 items 232UART0_ICR_R = UART_ICR_RXIC ;// acknowledge RX FIFO 233// copy from hardware RX FIFO to software RX FIFO 234copyHardwareToSoftware (); 235 } 236if (UART0_RIS_R & UART_RIS_RTRIS ){// receiver timed out 237UART0_ICR_R = UART_ICR_RTIC ;// acknowledge receiver time out 238// copy from hardware RX FIFO to software RX FIFO 239copyHardwareToSoftware (); 240 } 241} 242 243//------------UART_OutString------------ 244// Output String (NULL termination) 245// Input: pointer to a NULL-terminated string to be transferred 246// Output: none 247void UART_OutString (char * pt ){ 248while (* pt ){ 249UART_OutChar (* pt ); 250pt ++ ; 251 } 252} 253 254//------------UART_InUDec------------ 255// InUDec accepts ASCII input in unsigned decimal format 256// and converts to a 32-bit unsigned number 257// valid range is 0 to 4294967295 (2^32-1) 258// Input: none 259// Output: 32-bit unsigned number 260// If you enter a number above 4294967295, it will return an incorrect value 261// Backspace will remove last digit typed 262uint32_t UART_InUDec (void ){ 263uint32_t number = 0 ,length = 0 ; 264char character ; 265character = UART_InChar (); 266while (character != CR ){// accepts until <enter> is typed 267// The next line checks that the input is a digit, 0-9. 268// If the character is not 0-9, it is ignored and not echoed 269if ((character >='0' )&& (character <='9' )) { 270number = 10 * number + (character - '0' );// this line overflows if above 4294967295 271length ++ ; 272UART_OutChar (character ); 273 } 274// If the input is a backspace, then the return number is 275// changed and a backspace is outputted to the screen 276else if ((character == BS )&& length ){ 277number /=10 ; 278length -- ; 279UART_OutChar (character ); 280 } 281character = UART_InChar (); 282 } 283return number ; 284} 285 286//-----------------------UART_OutUDec----------------------- 287// Output a 32-bit number in unsigned decimal format 288// Input: 32-bit number to be transferred 289// Output: none 290// Variable format 1-10 digits with no space before or after 291void UART_OutUDec (uint32_t n ){ 292// This function uses recursion to convert decimal number 293// of unspecified length as an ASCII string 294if (n >=10 ){ 295UART_OutUDec (n /10 ); 296n = n %10 ; 297 } 298UART_OutChar (n + '0' );/* n is between 0 and 9 */ 299} 300 301//---------------------UART_InUHex---------------------------------------- 302// Accepts ASCII input in unsigned hexadecimal (base 16) format 303// Input: none 304// Output: 32-bit unsigned number 305// No '$' or '0x' need be entered, just the 1 to 8 hex digits 306// It will convert lower case a-f to uppercase A-F 307// and converts to a 16 bit unsigned number 308// value range is 0 to FFFFFFFF 309// If you enter a number above FFFFFFFF, it will return an incorrect value 310// Backspace will remove last digit typed 311uint32_t UART_InUHex (void ){ 312uint32_t number = 0 ,digit ,length = 0 ; 313char character ; 314character = UART_InChar (); 315while (character != CR ){ 316digit = 0x10 ;// assume bad 317if ((character >='0' )&& (character <='9' )){ 318digit = character - '0' ; 319 } 320else if ((character >='A' )&& (character <='F' )){ 321digit = (character - 'A' )+ 0xA ; 322 } 323else if ((character >='a' )&& (character <='f' )){ 324digit = (character - 'a' )+ 0xA ; 325 } 326// If the character is not 0-9 or A-F, it is ignored and not echoed 327if (digit <=0xF ){ 328number = number * 0x10 + digit ; 329length ++ ; 330UART_OutChar (character ); 331 } 332// Backspace outputted and return value changed if a backspace is inputted 333else if ((character == BS )&& length ){ 334number /=0x10 ; 335length -- ; 336UART_OutChar (character ); 337 } 338character = UART_InChar (); 339 } 340return number ; 341} 342 343//--------------------------UART_OutUHex---------------------------- 344// Output a 32-bit number in unsigned hexadecimal format 345// Input: 32-bit number to be transferred 346// Output: none 347// Variable format 1 to 8 digits with no space before or after 348void UART_OutUHex (uint32_t number ){ 349// This function uses recursion to convert the number of 350// unspecified length as an ASCII string 351if (number >=0x10 ){ 352UART_OutUHex (number /0x10 ); 353UART_OutUHex (number %0x10 ); 354 } 355else { 356if (number < 0xA ){ 357UART_OutChar (number + '0' ); 358 } 359else { 360UART_OutChar ((number - 0x0A )+ 'A' ); 361 } 362 } 363} 364 365//------------UART_InString------------ 366// Accepts ASCII characters from the serial port 367// and adds them to a string until <enter> is typed 368// or until max length of the string is reached. 369// It echoes each character as it is inputted. 370// If a backspace is inputted, the string is modified 371// and the backspace is echoed 372// terminates the string with a null character 373// uses busy-waiting synchronization on RDRF 374// Input: pointer to empty buffer, size of buffer 375// Output: Null terminated string 376// -- Modified by Agustinus Darmawan + Mingjie Qiu -- 377void UART_InString (char * bufPt ,uint16_t max ) { 378int length = 0 ; 379char character ; 380character = UART_InChar (); 381while (character != CR ){ 382if (character == BS ){ 383if (length ){ 384bufPt -- ; 385length -- ; 386UART_OutChar (BS ); 387 } 388 } 389else if (length < max ){ 390* bufPt = character ; 391bufPt ++ ; 392length ++ ; 393UART_OutChar (character ); 394 } 395character = UART_InChar (); 396 } 397* bufPt = 0 ; 398}