Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of nucleo_test_soft_serial by
soft_uart.cpp
00001 // UART.C 00002 // 00003 // Generic software uart written in C, requiring a timer set to 3 times 00004 // the baud rate, and two software read/write pins for the receive and 00005 // transmit functions. 00006 // 00007 // * Received characters are buffered 00008 // * putchar(), getchar(), kbhit() and flush_input_buffer() are available 00009 // * There is a facility for background processing while waiting for input 00010 // 00011 // Colin Gittins, Software Engineer, Halliburton Energy Services 00012 // 00013 // The baud rate can be configured by changing the BAUD_RATE macro as 00014 // follows: 00015 // 00016 // #define BAUD_RATE 19200.0 00017 // 00018 // The function init_uart() must be called before any comms can take place 00019 // 00020 // Interface routines required: 00021 // 1. get_rx_pin_status() 00022 // Returns 0 or 1 dependent on whether the receive pin is high or low. 00023 // 2. set_tx_pin_high() 00024 // Sets the transmit pin to the high state. 00025 // 3. set_tx_pin_low() 00026 // Sets the transmit pin to the low state. 00027 // 4. idle() 00028 // Background functions to execute while waiting for input. 00029 // 5. timer_set( BAUD_RATE ) 00030 // Sets the timer to 3 times the baud rate. 00031 // 6. set_timer_interrupt( timer_isr ) 00032 // Enables the timer interrupt. 00033 // 00034 // Functions provided: 00035 // 1. void flush_input_buffer( void ) 00036 // Clears the contents of the input buffer. 00037 // 2. char kbhit( void ) 00038 // Tests whether an input character has been received. 00039 // 3. char getchar( void ) 00040 // Reads a character from the input buffer, waiting if necessary. 00041 // 4. void turn_rx_on( void ) 00042 // Turns on the receive function. 00043 // 5. void turn_rx_off( void ) 00044 // Turns off the receive function. 00045 // 6. void putchar( char ) 00046 // Writes a character to the serial port. 00047 #include "mbed.h" 00048 #include <stdio.h> 00049 00050 #define BAUD_RATE 9600 00051 #define IN_BUF_SIZE 256 00052 00053 #define TRUE 1 00054 #define FALSE 0 00055 00056 static unsigned char inbuf[IN_BUF_SIZE]; 00057 static unsigned char qin = 0; 00058 static unsigned char qout = 0; 00059 00060 static char flag_rx_waiting_for_stop_bit; 00061 static char flag_rx_off; 00062 static char rx_mask; 00063 static char flag_rx_ready; 00064 static char flag_tx_ready; 00065 static char timer_rx_ctr; 00066 static char timer_tx_ctr; 00067 static char bits_left_in_rx; 00068 static char bits_left_in_tx; 00069 static char rx_num_of_bits; 00070 static char tx_num_of_bits; 00071 static int internal_rx_buffer; 00072 static int internal_tx_buffer; 00073 static int user_tx_buffer; 00074 00075 DigitalOut TX(PA_4); 00076 DigitalIn RX(PA_3); 00077 Ticker ticker; 00078 00079 00080 00081 //Background functions to execute while waiting for input. 00082 void idle(){ 00083 wait_us(0.2); 00084 } 00085 00086 //Sets the transmit pin to the high state. 00087 void set_tx_pin_high() { 00088 TX = 1; 00089 } 00090 00091 //Sets the transmit pin to the low state. 00092 void set_tx_pin_low() { 00093 TX = 0; 00094 } 00095 00096 //Returns 0 or 1 dependent on whether the receive pin is high or low 00097 int get_rx_pin_status() { 00098 return RX.read(); 00099 } 00100 00101 void timer_isr(void) 00102 { 00103 char mask, start_bit, flag_in; 00104 00105 00106 // Transmitter Section 00107 if ( flag_tx_ready ) 00108 { 00109 00110 if ( --timer_tx_ctr<=0 ) 00111 { 00112 mask = internal_tx_buffer&1; 00113 internal_tx_buffer >>= 1; 00114 if ( mask ) 00115 { 00116 set_tx_pin_high(); 00117 } 00118 else 00119 { 00120 set_tx_pin_low(); 00121 } 00122 timer_tx_ctr = 3; 00123 if ( --bits_left_in_tx<=0 ) 00124 { 00125 flag_tx_ready = FALSE; 00126 } 00127 } 00128 } 00129 // Receiver Section 00130 if ( flag_rx_off==FALSE ) 00131 { 00132 if ( flag_rx_waiting_for_stop_bit ) 00133 { 00134 if ( --timer_rx_ctr<=0 ) 00135 { 00136 flag_rx_waiting_for_stop_bit = FALSE; 00137 flag_rx_ready = FALSE; 00138 internal_rx_buffer &= 0xFF; 00139 if ( internal_rx_buffer!=0xC2 ) 00140 { 00141 inbuf[qin] = internal_rx_buffer; 00142 if ( ++qin>=IN_BUF_SIZE ) 00143 { 00144 qin = 0; 00145 } 00146 } 00147 } 00148 } 00149 else // rx_test_busy 00150 { 00151 if ( flag_rx_ready==FALSE ) 00152 { 00153 start_bit = get_rx_pin_status(); 00154 // Test for Start Bit 00155 if ( start_bit==0 ) 00156 { 00157 flag_rx_ready = TRUE; 00158 internal_rx_buffer = 0; 00159 timer_rx_ctr = 4; 00160 bits_left_in_rx = rx_num_of_bits; 00161 rx_mask = 1; 00162 } 00163 } 00164 else // rx_busy 00165 { 00166 if ( --timer_rx_ctr<=0 ) 00167 { // rcv 00168 timer_rx_ctr = 3; 00169 flag_in = get_rx_pin_status(); 00170 if ( flag_in ) 00171 { 00172 internal_rx_buffer |= rx_mask; 00173 } 00174 rx_mask <<= 1; 00175 if ( --bits_left_in_rx<=0 ) 00176 { 00177 flag_rx_waiting_for_stop_bit = TRUE; 00178 } 00179 } 00180 } 00181 } 00182 } 00183 } 00184 00185 void init_uart( void ) 00186 { 00187 flag_tx_ready = FALSE; 00188 flag_rx_ready = FALSE; 00189 flag_rx_waiting_for_stop_bit = FALSE; 00190 flag_rx_off = FALSE; 00191 rx_num_of_bits = 8;//10 00192 tx_num_of_bits = 10;//10 00193 00194 set_tx_pin_low(); 00195 ticker.attach_us(&timer_isr, 1000000.0 / (BAUD_RATE * 3.0)); 00196 00197 } 00198 00199 char _getchar( void ) 00200 { 00201 char ch; 00202 00203 do 00204 { 00205 while ( qout==qin ) 00206 { 00207 idle(); 00208 } 00209 ch = inbuf[qout] & 0xFF; 00210 if ( ++qout>=IN_BUF_SIZE ) 00211 { 00212 qout = 0; 00213 } 00214 } 00215 while ( ch==0x0A || ch==0xC2 ); 00216 return( ch ); 00217 } 00218 00219 void _putchar( int ch ) 00220 { 00221 while ( flag_tx_ready ); 00222 user_tx_buffer = ch; 00223 00224 // invoke_UART_transmit 00225 timer_tx_ctr = 3; 00226 bits_left_in_tx = tx_num_of_bits; 00227 internal_tx_buffer = (user_tx_buffer<<1) | 0x200;; 00228 flag_tx_ready = TRUE; 00229 } 00230 00231 void flush_input_buffer( void ) 00232 { 00233 qin = 0; 00234 qout = 0; 00235 } 00236 00237 char kbhit( void ) 00238 { 00239 return( qin!=qout ); 00240 } 00241 00242 void turn_rx_on( void ) 00243 { 00244 flag_rx_off = FALSE; 00245 } 00246 00247 void turn_rx_off( void ) 00248 { 00249 flag_rx_off = TRUE; 00250 } 00251 00252 void printStr(char* str){ 00253 int i = 0; 00254 int len = strlen(str); 00255 // pc.printf(":%d",len); 00256 for(i = 0; i<len; i++){ 00257 wait(0.01); 00258 _putchar(str[i]); 00259 } 00260 } 00261
Generated on Mon Jul 25 2022 07:30:39 by
1.7.2
