+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

jspspikeAdded lab4 files99480b6

master
14.3 KiB398 linesraw
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",
12   ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2018
13   Program 5.11 Section 5.6, Program 3.10
14
15 Copyright 2018 by Jonathan W. Valvano, valvano@mail.utexas.edu
16    You may use, edit, run or distribute this file
17    as long as the above copyright notice remains
18 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
19 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
21 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
22 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
23 For more information about my classes, my research, and my books, see
24 http://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;
77  sr = StartCritical(); // make atomic
78  RxPutI = RxGetI = 0;  // Empty
79  EndCritical(sr);
80}
81// add element to end of index FIFO
82// return FIFOSUCCESS if successful
83int RxFifo_Put(char data){
84  if((RxPutI-RxGetI) & ~(RXFIFOSIZE-1)){
85    return(FIFOFAIL); // Failed, fifo full
86  }
87  RxFifo[RxPutI&(RXFIFOSIZE-1)] = data; // put
88  RxPutI++;  // Success, update
89  return(FIFOSUCCESS);
90}
91// remove element from front of index FIFO
92// return FIFOSUCCESS if successful
93int RxFifo_Get(char *datapt){
94  if(RxPutI == RxGetI ){
95    return(FIFOFAIL); // Empty if RxPutI=RxGetI
96  }
97  *datapt = RxFifo[RxGetI&(RXFIFOSIZE-1)];
98  RxGetI++;  // Success, update
99  return(FIFOSUCCESS);
100}
101// number of elements in index FIFO
102// 0 to RXFIFOSIZE-1
103uint32_t RxFifo_Size(void){
104 return ((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;
116  sr = StartCritical(); // make atomic
117  TxPutI = TxGetI = 0;  // Empty
118  EndCritical(sr);
119}
120// add element to end of index FIFO
121// return FIFOSUCCESS if successful
122int TxFifo_Put(char data){
123  if((TxPutI-TxGetI) & ~(TXFIFOSIZE-1)){
124    return(FIFOFAIL); // Failed, fifo full
125  }
126  TxFifo[TxPutI&(TXFIFOSIZE-1)] = data; // put
127  TxPutI++;  // Success, update
128  return(FIFOSUCCESS);
129}
130// remove element from front of index FIFO
131// return FIFOSUCCESS if successful
132int TxFifo_Get(char *datapt){
133  if(TxPutI == TxGetI ){
134    return(FIFOFAIL); // Empty if TxPutI=TxGetI
135  }
136  *datapt = TxFifo[TxGetI&(TXFIFOSIZE-1)];
137  TxGetI++;  // Success, update
138  return(FIFOSUCCESS);
139}
140// number of elements in index FIFO
141// 0 to TXFIFOSIZE-1
142uint32_t TxFifo_Size(void){
143 return ((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){
150  SYSCTL_RCGCUART_R |= 0x01;            // activate UART0
151  SYSCTL_RCGCGPIO_R |= 0x01;            // activate port A
152  RxFifo_Init();                        // initialize empty FIFOs
153  TxFifo_Init();
154  UART0_CTL_R &= ~UART_CTL_UARTEN;      // disable UART
155  UART0_IBRD_R = 43;                    // IBRD = int(80,000,000 / (16 * 115,200)) = int(43.403)
156  UART0_FBRD_R = 26;                    // FBRD = round(0.403 * 64) = 25
157                                        // 8 bit word length (no parity bits, one stop bit, FIFOs)
158  UART0_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);
159  UART0_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
162  UART0_IFLS_R += (UART_IFLS_TX1_8|UART_IFLS_RX1_8);
163                                        // enable TX and RX FIFO interrupts and RX time-out interrupt
164  UART0_IM_R |= (UART_IM_RXIM|UART_IM_TXIM|UART_IM_RTIM);
165  UART0_CTL_R |= 0x301;                 // enable UART
166  GPIO_PORTA_AFSEL_R |= 0x03;           // enable alt funct on PA1-0
167  GPIO_PORTA_DEN_R |= 0x03;             // enable digital I/O on PA1-0
168                                        // configure PA1-0 as UART
169  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011;
170  GPIO_PORTA_AMSEL_R = 0;               // disable analog functionality on PA
171                                        // UART0=priority 2
172  NVIC_PRI1_R = (NVIC_PRI1_R&0xFFFF00FF)|0x00004000; // bits 13-15
173  NVIC_EN0_R = NVIC_EN0_INT5;           // enable interrupt 5 in NVIC
174  EnableInterrupts();
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){
179  char letter;
180  while(((UART0_FR_R&UART_FR_RXFE) == 0) && (RxFifo_Size() < (RXFIFOSIZE - 1))){
181    letter = UART0_DR_R;
182    RxFifo_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){
188  char letter;
189  while(((UART0_FR_R&UART_FR_TXFF) == 0) && (TxFifo_Size() > 0)){
190    TxFifo_Get(&letter);
191    UART0_DR_R = letter;
192  }
193}
194// input ASCII character from UART
195// spin if RxFifo is empty
196char UART_InChar(void){
197  char letter;
198  while(RxFifo_Get(&letter) == FIFOFAIL){};
199  return(letter);
200}
201// input ASCII character from UART
202// return 0 if RxFifo is empty
203char UART_InCharNonBlock(void){
204  char letter;
205  if(RxFifo_Get(&letter) == FIFOFAIL){
206    return 0; // no data
207  }
208  return(letter);
209}
210// output ASCII character to UART
211// spin if TxFifo is full
212void UART_OutChar(char data){
213  while(TxFifo_Put(data) == FIFOFAIL){};
214  UART0_IM_R &= ~UART_IM_TXIM;          // disable TX FIFO interrupt
215  copySoftwareToHardware();
216  UART0_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){
223  if(UART0_RIS_R&UART_RIS_TXRIS){       // hardware TX FIFO <= 2 items
224    UART0_ICR_R = UART_ICR_TXIC;        // acknowledge TX FIFO
225    // copy from software TX FIFO to hardware TX FIFO
226    copySoftwareToHardware();
227    if(TxFifo_Size() == 0){             // software TX FIFO is empty
228      UART0_IM_R &= ~UART_IM_TXIM;      // disable TX FIFO interrupt
229    }
230  }
231  if(UART0_RIS_R&UART_RIS_RXRIS){       // hardware RX FIFO >= 2 items
232    UART0_ICR_R = UART_ICR_RXIC;        // acknowledge RX FIFO
233    // copy from hardware RX FIFO to software RX FIFO
234    copyHardwareToSoftware();
235  }
236  if(UART0_RIS_R&UART_RIS_RTRIS){       // receiver timed out
237    UART0_ICR_R = UART_ICR_RTIC;        // acknowledge receiver time out
238    // copy from hardware RX FIFO to software RX FIFO
239    copyHardwareToSoftware();
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){
248  while(*pt){
249    UART_OutChar(*pt);
250    pt++;
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;
265  character = UART_InChar();
266  while(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
269    if((character>='0') && (character<='9')) {
270      number = 10*number+(character-'0');   // this line overflows if above 4294967295
271      length++;
272      UART_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
276    else if((character==BS) && length){
277      number /= 10;
278      length--;
279      UART_OutChar(character);
280    }
281    character = UART_InChar();
282  }
283  return 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
294  if(n >= 10){
295    UART_OutUDec(n/10);
296    n = n%10;
297  }
298  UART_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;
314  character = UART_InChar();
315  while(character != CR){
316    digit = 0x10; // assume bad
317    if((character>='0') && (character<='9')){
318      digit = character-'0';
319    }
320    else if((character>='A') && (character<='F')){
321      digit = (character-'A')+0xA;
322    }
323    else if((character>='a') && (character<='f')){
324      digit = (character-'a')+0xA;
325    }
326// If the character is not 0-9 or A-F, it is ignored and not echoed
327    if(digit <= 0xF){
328      number = number*0x10+digit;
329      length++;
330      UART_OutChar(character);
331    }
332// Backspace outputted and return value changed if a backspace is inputted
333    else if((character==BS) && length){
334      number /= 0x10;
335      length--;
336      UART_OutChar(character);
337    }
338    character = UART_InChar();
339  }
340  return 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
351  if(number >= 0x10){
352    UART_OutUHex(number/0x10);
353    UART_OutUHex(number%0x10);
354  }
355  else{
356    if(number < 0xA){
357      UART_OutChar(number+'0');
358     }
359    else{
360      UART_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;
380  character = UART_InChar();
381  while(character != CR){
382    if(character == BS){
383      if(length){
384        bufPt--;
385        length--;
386        UART_OutChar(BS);
387      }
388    }
389    else if(length < max){
390      *bufPt = character;
391      bufPt++;
392      length++;
393      UART_OutChar(character);
394    }
395    character = UART_InChar();
396  }
397  *bufPt = 0;
398}