revised code

Dependencies:   mbed

Committer:
nmaududi
Date:
Sat Oct 05 21:17:17 2019 +0000
Revision:
2:64a34ae90bb1
Parent:
1:9fa7cc80f1a7
revised version for module 4;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nmaududi 0:4fb921928934 1 /**-----------------------------------------------------------------------------
nmaududi 0:4fb921928934 2 \file UART_poll.cpp
nmaududi 0:4fb921928934 3
nmaududi 0:4fb921928934 4 -- --
nmaududi 0:4fb921928934 5 -- ECEN 5803 Mastering Embedded System Architecture --
nmaududi 0:4fb921928934 6 -- Project 1 Module 3 --
nmaududi 0:4fb921928934 7 -- Microcontroller Firmware --
nmaududi 0:4fb921928934 8 -- UART_poll.c --
nmaududi 0:4fb921928934 9 -- --
nmaududi 0:4fb921928934 10 -------------------------------------------------------------------------------
nmaududi 0:4fb921928934 11 --
nmaududi 0:4fb921928934 12 -- Designed for: University of Colorado at Boulder
nmaududi 0:4fb921928934 13 --
nmaududi 0:4fb921928934 14 --
nmaududi 0:4fb921928934 15 -- Designed by: Tim Scherr
nmaududi 0:4fb921928934 16 -- Revised by: Student's name
nmaududi 0:4fb921928934 17 --
nmaududi 0:4fb921928934 18 -- Version: 2.1
nmaududi 0:4fb921928934 19 -- Date of current revision: 2017-09-20
nmaududi 0:4fb921928934 20 -- Target Microcontroller: Freescale MKL25ZVMT4
nmaududi 0:4fb921928934 21 -- Tools used: ARM mbed compiler
nmaududi 0:4fb921928934 22 -- ARM mbed SDK
nmaududi 0:4fb921928934 23 -- Freescale FRDM-KL25Z Freedom Board
nmaududi 0:4fb921928934 24 --
nmaududi 0:4fb921928934 25 --
nmaududi 0:4fb921928934 26 -- Functional Description: This file contains routines that support messages
nmaududi 0:4fb921928934 27 -- to and from the UART port. Included are:
nmaududi 0:4fb921928934 28 -- Serial() - a routine to send/receive bytes on the UART port to
nmaududi 0:4fb921928934 29 -- the transmit/receive buffers
nmaududi 0:4fb921928934 30 -- UART_put() - a routine that puts a character in the transmit buffer
nmaududi 0:4fb921928934 31 -- UART_get() - a routine that gets the next character from the receive
nmaududi 0:4fb921928934 32 -- buffer
nmaududi 0:4fb921928934 33 -- UART_msg_put() - a routine that puts a string in the transmit buffer
nmaududi 0:4fb921928934 34 -- UART_direct_msg_put() - routine that sends a string out the UART port
nmaududi 0:4fb921928934 35 -- UART_input() - determines if a character has been received
nmaududi 0:4fb921928934 36 -- UART_hex_put() - a routine that puts a hex byte in the transmit buffer
nmaududi 0:4fb921928934 37 --
nmaududi 0:4fb921928934 38 -- Copyright (c) 2015 Tim Scherr All rights reserved.
nmaududi 0:4fb921928934 39 --
nmaududi 0:4fb921928934 40 */
nmaududi 0:4fb921928934 41
nmaududi 0:4fb921928934 42
nmaududi 0:4fb921928934 43
nmaududi 0:4fb921928934 44 /*******************/
nmaududi 0:4fb921928934 45 /* Configurations */
nmaududi 0:4fb921928934 46 /*******************/
nmaududi 0:4fb921928934 47 /*
nmaududi 0:4fb921928934 48
nmaududi 0:4fb921928934 49 */
nmaududi 0:4fb921928934 50
nmaududi 0:4fb921928934 51 #include <stdio.h>
nmaududi 0:4fb921928934 52 #include "shared.h"
nmaududi 0:4fb921928934 53 #include "MKL25Z4.h"
nmaududi 0:4fb921928934 54
nmaududi 0:4fb921928934 55 // NOTE: UART0 is also called UARTLP in mbed
nmaududi 0:4fb921928934 56 #define OERR (UART0->S1 & UARTLP_S1_OR_MASK) // Overrun Error bit
nmaududi 0:4fb921928934 57 #define CREN (UART0->C2 & UARTLP_C2_RE_MASK) // continuous receive enable bit
nmaududi 0:4fb921928934 58 #define RCREG UART0->D // Receive Data Register
nmaududi 0:4fb921928934 59 #define FERR (UART0->S1 & UARTLP_S1_FE_MASK) // Framing Error bit
nmaududi 0:4fb921928934 60 #define RCIF (UART0->S1 & UARTLP_S1_RDRF_MASK) // Receive Interrupt Flag (full)
nmaududi 0:4fb921928934 61 #define TXIF (UART0->S1 & UARTLP_S1_TDRE_MASK) // Transmit Interrupt Flag (empty)
nmaududi 0:4fb921928934 62 #define TXREG UART0->D // Transmit Data Register
nmaududi 0:4fb921928934 63 #define TRMT (UART0->S1 & UARTLP_S1_TC_MASK) // Transmit Shift Register Empty
nmaududi 0:4fb921928934 64
nmaududi 0:4fb921928934 65 /***********************************
nmaududi 0:4fb921928934 66 * Start of code *
nmaududi 0:4fb921928934 67 ***********************************/
nmaududi 0:4fb921928934 68
nmaududi 0:4fb921928934 69 UCHAR error_count = 0;
nmaududi 0:4fb921928934 70
nmaududi 0:4fb921928934 71 /// \fn void serial(void)
nmaududi 0:4fb921928934 72 /// function polls the serial port for Rx or Tx data
nmaududi 0:4fb921928934 73 void serial(void) // The serial function polls the serial port for
nmaududi 0:4fb921928934 74 // received data or data to transmit
nmaududi 0:4fb921928934 75 {
nmaududi 0:4fb921928934 76 // deals with error handling first
nmaududi 0:4fb921928934 77 if ( OERR ) // if an overrun error, clear it and continue.
nmaududi 0:4fb921928934 78 {
nmaududi 0:4fb921928934 79 error_count++;
nmaududi 0:4fb921928934 80 // resets and sets continous receive enable bit
nmaududi 0:4fb921928934 81 UART0->C2 = UART0->C2 & (!UARTLP_C2_RE_MASK);
nmaududi 0:4fb921928934 82 UART0->C2 = UART0->C2 | UARTLP_C2_RE_MASK;
nmaududi 0:4fb921928934 83 }
nmaududi 0:4fb921928934 84
nmaududi 0:4fb921928934 85 if ( FERR){ // if a framing error, read bad byte, clear it and continue.
nmaududi 0:4fb921928934 86 error_count++;
nmaududi 0:4fb921928934 87 RCREG; // This will also clear RCIF if only one byte has been
nmaududi 0:4fb921928934 88 // received since the last int, which is our assumption.
nmaududi 0:4fb921928934 89
nmaududi 0:4fb921928934 90 // resets and sets continous receive enable bit
nmaududi 0:4fb921928934 91 UART0->C2 = UART0->C2 & (!UARTLP_C2_RE_MASK);
nmaududi 0:4fb921928934 92 UART0->C2 = UART0->C2 | UARTLP_C2_RE_MASK;
nmaududi 0:4fb921928934 93 }
nmaududi 0:4fb921928934 94 else // else if no frame error,
nmaududi 0:4fb921928934 95 {
nmaududi 0:4fb921928934 96 if ( RCIF ) // Check if we have received a byte
nmaududi 0:4fb921928934 97 { // Read byte to enable reception of more bytes
nmaududi 0:4fb921928934 98 // For PIC, RCIF automatically cleared when RCREG is read
nmaududi 0:4fb921928934 99 // Also true of Freescale KL25Z
nmaududi 0:4fb921928934 100 *rx_in_ptr++ = RCREG; /* get received character */
nmaududi 0:4fb921928934 101 if( rx_in_ptr >= RX_BUF_SIZE + rx_buf )
nmaududi 0:4fb921928934 102 {
nmaududi 0:4fb921928934 103 rx_in_ptr = rx_buf; /* if at end of buffer, circles rx_in_ptr
nmaududi 0:4fb921928934 104 to top of buffer */
nmaududi 0:4fb921928934 105 }
nmaududi 0:4fb921928934 106
nmaududi 0:4fb921928934 107 }
nmaududi 0:4fb921928934 108 }
nmaududi 0:4fb921928934 109
nmaududi 0:4fb921928934 110 if (TXIF) // Check if transmit buffer empty
nmaududi 0:4fb921928934 111 {
nmaududi 0:4fb921928934 112 if ((tx_in_ptr != tx_out_ptr) && (display_mode != QUIET))
nmaududi 0:4fb921928934 113 {
nmaududi 0:4fb921928934 114 TXREG = *tx_out_ptr++; /* send next char */
nmaududi 0:4fb921928934 115 if( tx_out_ptr >= TX_BUF_SIZE + tx_buf )
nmaududi 0:4fb921928934 116 tx_out_ptr = tx_buf; /* 0 <= tx_out_idx < TX_BUF_SIZE */
nmaududi 0:4fb921928934 117 tx_in_progress = YES; /* flag needed to start up after idle */
nmaududi 0:4fb921928934 118 }
nmaududi 0:4fb921928934 119 else
nmaududi 0:4fb921928934 120 {
nmaududi 0:4fb921928934 121 tx_in_progress = NO; /* no more to send */
nmaududi 0:4fb921928934 122 }
nmaududi 0:4fb921928934 123 }
nmaududi 0:4fb921928934 124 // serial_count++; // increment serial counter, for debugging only
nmaududi 0:4fb921928934 125 serial_flag = 1; // and set flag
nmaududi 0:4fb921928934 126 }
nmaududi 0:4fb921928934 127
nmaududi 0:4fb921928934 128 /*******************************************************************************
nmaududi 0:4fb921928934 129 * The function UART_direct_msg_put puts a null terminated string directly
nmaududi 0:4fb921928934 130 * (no ram buffer) to the UART in ASCII format.
nmaududi 0:4fb921928934 131 *******************************************************************************/
nmaududi 0:4fb921928934 132 void UART_direct_msg_put(const char *str)
nmaududi 0:4fb921928934 133 {
nmaududi 0:4fb921928934 134 while( *str != '\0' )
nmaududi 0:4fb921928934 135 {
nmaududi 0:4fb921928934 136 TXREG = *str++;
nmaududi 0:4fb921928934 137 while( TXIF == 0 || TRMT == 0 ) // waits here for UART transmit buffer
nmaududi 0:4fb921928934 138 // to be empty
nmaududi 0:4fb921928934 139 {
nmaududi 0:4fb921928934 140 // __clear_watchdog_timer();
nmaududi 0:4fb921928934 141 }
nmaududi 0:4fb921928934 142 }
nmaududi 0:4fb921928934 143 }
nmaududi 0:4fb921928934 144
nmaududi 0:4fb921928934 145 /*******************************************************************************
nmaududi 0:4fb921928934 146 * The function UART_put puts a byte, to the transmit buffer at the location
nmaududi 0:4fb921928934 147 * pointed to by tx_in_idx. The pointer is incremented circularly as described
nmaududi 0:4fb921928934 148 * previously. If the transmit buffer should wrap around (should be designed
nmaududi 0:4fb921928934 149 * not to happen), data will be lost. The serial interrupt must be temporarily
nmaududi 0:4fb921928934 150 * disabled since it reads tx_in_idx and this routine updates tx_in_idx which is
nmaududi 0:4fb921928934 151 * a 16 bit value.
nmaududi 0:4fb921928934 152 *******************************************************************************/
nmaududi 0:4fb921928934 153 void UART_put(UCHAR c)
nmaududi 0:4fb921928934 154 {
nmaududi 0:4fb921928934 155 *tx_in_ptr++ = c; /* save character to transmit buffer */
nmaududi 0:4fb921928934 156 if( tx_in_ptr >= TX_BUF_SIZE + tx_buf)
nmaududi 0:4fb921928934 157 tx_in_ptr = tx_buf; /* 0 <= tx_in_idx < TX_BUF_SIZE */
nmaududi 0:4fb921928934 158 }
nmaududi 0:4fb921928934 159
nmaududi 0:4fb921928934 160 /*******************************************************************************
nmaududi 0:4fb921928934 161 * The function UART_get gets the next byte if one is available from the receive
nmaududi 0:4fb921928934 162 * buffer at the location pointed to by rx_out_idx. The pointer is circularly
nmaududi 0:4fb921928934 163 * incremented and the byte is returned in R7. Should no byte be available the
nmaududi 0:4fb921928934 164 * function will wait until one is available. There is no need to disable the
nmaududi 0:4fb921928934 165 * serial interrupt which modifies rx_in_idx since the function is looking for a
nmaududi 0:4fb921928934 166 * compare only between rx_in_idx & rx_out_idx.
nmaududi 0:4fb921928934 167 *******************************************************************************/
nmaududi 0:4fb921928934 168 UCHAR UART_get(void)
nmaududi 0:4fb921928934 169 {
nmaududi 0:4fb921928934 170 UCHAR c;
nmaududi 0:4fb921928934 171 while( rx_in_ptr == rx_out_ptr ); /* wait for a received character,
nmaududi 0:4fb921928934 172 indicated */
nmaududi 0:4fb921928934 173 // when pointers are different
nmaududi 0:4fb921928934 174 // this could be an infinite loop, but
nmaududi 0:4fb921928934 175 // is not because of UART_input check
nmaududi 0:4fb921928934 176 c = *rx_out_ptr++;
nmaududi 0:4fb921928934 177 if( rx_out_ptr >= RX_BUF_SIZE + rx_buf ) // if at end of buffer
nmaududi 0:4fb921928934 178 {
nmaududi 0:4fb921928934 179 rx_out_ptr = rx_buf; /* 0 <= rx_out_idx < RX_BUF_SIZE */
nmaududi 0:4fb921928934 180 // return byte from beginning of buffer
nmaududi 0:4fb921928934 181 } // next time.
nmaududi 0:4fb921928934 182 return(c);
nmaududi 0:4fb921928934 183 }
nmaududi 0:4fb921928934 184
nmaududi 0:4fb921928934 185 /*******************************************************************************
nmaududi 0:4fb921928934 186 * The function UART_input returns a 1 if 1 or more receive byte(s) is(are)
nmaududi 0:4fb921928934 187 * available and a 0 if the receive buffer rx_buf is empty. There is no need to
nmaududi 0:4fb921928934 188 * disable the serial interrupt which modifies rx_in_idx since function is
nmaududi 0:4fb921928934 189 * looking for a compare only between rx_in_idx & rx_out_idx.
nmaududi 0:4fb921928934 190 *******************************************************************************/
nmaududi 0:4fb921928934 191 UCHAR UART_input(void)
nmaududi 0:4fb921928934 192 {
nmaududi 0:4fb921928934 193 if( rx_in_ptr == rx_out_ptr )
nmaududi 0:4fb921928934 194 return(0); /* no characters in receive buffer */
nmaududi 0:4fb921928934 195 else
nmaududi 0:4fb921928934 196 return(1); /* 1 or more receive characters ready */
nmaududi 0:4fb921928934 197 }
nmaududi 0:4fb921928934 198
nmaududi 0:4fb921928934 199 /*******************************************************************************
nmaududi 0:4fb921928934 200 * The function UART_msg_put puts a null terminated string through the transmit
nmaududi 0:4fb921928934 201 * buffer to the UART port in ASCII format.
nmaududi 0:4fb921928934 202 *******************************************************************************/
nmaududi 0:4fb921928934 203 void UART_msg_put(const char *str)
nmaududi 0:4fb921928934 204 {
nmaududi 0:4fb921928934 205 while( *str != '\0' )
nmaududi 0:4fb921928934 206 {
nmaududi 0:4fb921928934 207 *tx_in_ptr++ = *str++; /* save character to transmit buffer */
nmaududi 0:4fb921928934 208 if( tx_in_ptr >= TX_BUF_SIZE + tx_buf)
nmaududi 0:4fb921928934 209 tx_in_ptr = tx_buf; /* 0 <= tx_in_idx < TX_BUF_SIZE */
nmaududi 0:4fb921928934 210 }
nmaududi 0:4fb921928934 211 }
nmaududi 0:4fb921928934 212
nmaududi 0:4fb921928934 213
nmaududi 0:4fb921928934 214 /*******************************************************************************
nmaududi 0:4fb921928934 215 * The function UART_low_nibble_put puts the low nibble of a byte in hex through
nmaududi 0:4fb921928934 216 * the transmit buffer to the UART port.
nmaududi 0:4fb921928934 217 *******************************************************************************/
nmaududi 0:4fb921928934 218 //void UART_low_nibble_put(UCHAR c)
nmaududi 0:4fb921928934 219 //{
nmaududi 0:4fb921928934 220 // UART_put( hex_to_asc( c & 0x0f ));
nmaududi 0:4fb921928934 221 //}
nmaududi 0:4fb921928934 222
nmaududi 0:4fb921928934 223 /*******************************************************************************
nmaududi 0:4fb921928934 224 * The function UART_high_nibble_put puts the high nibble of a byte in h
nmaududi 0:4fb921928934 225 * UART port.
nmaududi 0:4fb921928934 226 *******************************************************************************/
nmaududi 0:4fb921928934 227 //void UART_high_nibble_put(unsigned char c)
nmaududi 0:4fb921928934 228 //{
nmaududi 0:4fb921928934 229 // UART_put( hex_to_asc( (c>>4) & 0x0f ));
nmaududi 0:4fb921928934 230 //}
nmaududi 0:4fb921928934 231
nmaududi 0:4fb921928934 232 /*******************************************************************************
nmaududi 0:4fb921928934 233 * HEX_TO_ASC Function
nmaududi 0:4fb921928934 234 * Function takes a single hex character (0 thru Fh) and converts to ASCII.
nmaududi 0:4fb921928934 235 ******************************************************************************/
nmaududi 0:4fb921928934 236 UCHAR hex_to_asc(UCHAR c)
nmaududi 0:4fb921928934 237 {
nmaududi 0:4fb921928934 238 if( c <= 9 )
nmaududi 0:4fb921928934 239 return( c + 0x30 );
nmaududi 0:4fb921928934 240 return( ((c & 0x0f) + 0x37 )); /* add 37h */
nmaududi 0:4fb921928934 241 }
nmaududi 0:4fb921928934 242
nmaududi 0:4fb921928934 243 /*******************************************************************************
nmaududi 0:4fb921928934 244 * ASC_TO_HEX Function
nmaududi 0:4fb921928934 245 * Function takes a single ASCII character and converts to hex.
nmaududi 0:4fb921928934 246 *******************************************************************************/
nmaududi 0:4fb921928934 247 UCHAR asc_to_hex(UCHAR c)
nmaududi 0:4fb921928934 248 {
nmaududi 0:4fb921928934 249 if( c <= '9' )
nmaududi 0:4fb921928934 250 return( c - 0x30 );
nmaududi 0:4fb921928934 251 return( (c & 0xdf) - 0x37 ); /* clear bit 5 (lower case) & subtract 37h */
nmaududi 0:4fb921928934 252 }
nmaududi 0:4fb921928934 253
nmaududi 0:4fb921928934 254
nmaududi 0:4fb921928934 255 /*******************************************************************************
nmaududi 0:4fb921928934 256 * The function UART_hex_put puts 1 byte in hex through the transmit buffer to
nmaududi 0:4fb921928934 257 * the UART port.
nmaududi 0:4fb921928934 258 *******************************************************************************/
nmaududi 0:4fb921928934 259 void UART_hex_put(unsigned char c)
nmaududi 0:4fb921928934 260 {
nmaududi 0:4fb921928934 261 UART_put( hex_to_asc( (c>>4) & 0x0f )); // could eliminate & as >> of UCHAR
nmaududi 0:4fb921928934 262 // by definition clears upper bits.
nmaududi 0:4fb921928934 263 UART_put( hex_to_asc( c & 0x0f ));
nmaududi 0:4fb921928934 264 }
nmaududi 0:4fb921928934 265
nmaududi 0:4fb921928934 266 /*******************************************************************************
nmaududi 0:4fb921928934 267 * The function UART_direct_hex_put puts 1 byte in hex directly (no ram buffer)
nmaududi 0:4fb921928934 268 * to the UART.
nmaududi 0:4fb921928934 269 *******************************************************************************/
nmaududi 0:4fb921928934 270 void UART_direct_hex_put(unsigned char c)
nmaududi 0:4fb921928934 271 {
nmaududi 0:4fb921928934 272 TXREG = hex_to_asc( (c>>4) & 0x0f );
nmaududi 0:4fb921928934 273 while( TXIF == 0 )
nmaududi 0:4fb921928934 274 {
nmaududi 0:4fb921928934 275 // __clear_watchdog_timer();
nmaududi 0:4fb921928934 276 }
nmaududi 0:4fb921928934 277 TXREG = hex_to_asc( c & 0x0f );
nmaududi 0:4fb921928934 278 while( TXIF == 0 )
nmaududi 0:4fb921928934 279 {
nmaududi 0:4fb921928934 280 // __clear_watchdog_timer();
nmaududi 0:4fb921928934 281 }
nmaududi 0:4fb921928934 282 }
nmaududi 0:4fb921928934 283
bcis93 1:9fa7cc80f1a7 284 /*******************************************************************************
bcis93 1:9fa7cc80f1a7 285 * The function UART_direct_hex_put_word puts 1 word in hex directly (no ram buffer)
bcis93 1:9fa7cc80f1a7 286 * to the UART. Used to display full words within registers and memory locations
bcis93 1:9fa7cc80f1a7 287 *******************************************************************************/
bcis93 1:9fa7cc80f1a7 288 void UART_direct_hex_put_word(uint32_t c)
bcis93 1:9fa7cc80f1a7 289 {
bcis93 1:9fa7cc80f1a7 290 UART_direct_hex_put((c>>24));
bcis93 1:9fa7cc80f1a7 291 UART_direct_hex_put((c>>16));
bcis93 1:9fa7cc80f1a7 292 UART_direct_hex_put((c>>8));
bcis93 1:9fa7cc80f1a7 293 UART_direct_hex_put((c));
bcis93 1:9fa7cc80f1a7 294 }
nmaududi 0:4fb921928934 295
nmaududi 0:4fb921928934 296
bcis93 1:9fa7cc80f1a7 297