Knud Dalgaard / 310-TMC3-TestHW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers uart.cpp Source File

uart.cpp

00001 #include "mbed.h"
00002 #include "uart.h"
00003 
00004 
00005 //---------------------------------------
00006 // Hardware recources
00007 //---------------------------------------
00008 Serial uartPC(USBTX, USBRX);
00009 Timeout uartTimeout;
00010 
00011 
00012 
00013 //---------------------------------------
00014 // Prototypes
00015 //---------------------------------------
00016 void UART_calcCRC16( void );
00017 void UART_initStructure( void );
00018 void UART_rxCallback( void );
00019 void UART_rxTimeout( void );
00020 
00021 //---------------------------------------
00022 // Internal variables
00023 //---------------------------------------
00024 char uartBuffer[UART_BUFFER_LENGTH];
00025 
00026 
00027 
00028 //---------------------------------------
00029 // External variables
00030 //---------------------------------------
00031 sUART_handler UART_handler;
00032 
00033 
00034 
00035 //---------------------------------------
00036 // Global Functions
00037 //---------------------------------------
00038 
00039 void UART_init( void )
00040 {
00041     uartPC.baud(UART_BAUDRATE);
00042     uartPC.format(UART_BITS, Serial::None, UART_STOPBIT );
00043     UART_reset();
00044     uartPC.attach( &UART_rxCallback, Serial::RxIrq );      // call UART_rx() on every RX interrupt
00045 }
00046 
00047 
00048 void UART_reset( void )
00049 {
00050     UART_handler.ptrBuffer = &uartBuffer[0];
00051     UART_handler.ptrReadPositon = &uartBuffer[0];
00052     UART_handler.ptrWritePosition = &uartBuffer[0];
00053     UART_handler.bytesToRead = 0;
00054     UART_handler.bytesToWrite = 0;
00055     UART_handler.mode = UART_MODE_LISTEN;
00056 }
00057 
00058 
00059 // Only send, if UART is not listining because the same buffer is used for RX and TX
00060 // Return 1: Sending was successfull
00061 // Return 0: Could not send data, eithter to many bytes, zero bytes or UART is still listining
00062 int UART_sendData( void )
00063 {
00064     int i;
00065 
00066     // 2013-07-04 SJ: Remove check for UART_MODE_RX_READY because UART_reset() is called on UART_newFrame
00067    // if( (UART_handler.bytesToWrite > 0) && (UART_handler.mode == UART_MODE_RX_READY) &&  (UART_handler.bytesToWrite < UART_BUFFER_LENGTH - 2 )  ) 
00068     if( (UART_handler.bytesToWrite > 0) &&  (UART_handler.bytesToWrite < UART_BUFFER_LENGTH - 2 )  ) 
00069     {
00070         // Calc and add CRC16 to buffer
00071         UART_calcCRC16();
00072         // Send data
00073         for( i = 0; i < UART_handler.bytesToWrite; i++ ) {
00074             while( !uartPC.writeable() );
00075             uartPC.putc(uartBuffer[i]);
00076         }//for
00077         // Reset UART function, ready to receive again
00078         UART_reset();
00079 
00080         return 1;
00081     }//if
00082     else {
00083         return 0;
00084     }
00085 
00086 }
00087 
00088 
00089 int UART_newFrame( void )
00090 {
00091     if( UART_handler.mode == UART_MODE_RX_READY ) 
00092     {
00093         // 2013-07-04 SJ: 
00094         #warning UART: This also resets the read pointer position!!!
00095         UART_reset();
00096         return 1;
00097     } 
00098     else 
00099     {
00100         return 0;
00101     }
00102 }
00103 
00104 
00105 // Return 0: CRC16 check failed
00106 // Return 1: CRC16 check passed
00107 int UART_checkReceivedCRC( void )
00108 {
00109     // calculate CRC16 of received stream and compare with received CRC16
00110 
00111     return 1;
00112 }
00113 
00114 
00115 
00116 //---------------------------------------
00117 // Internal Functions
00118 //---------------------------------------
00119 
00120 // Calculate CRC16 and add to buffer
00121 void UART_calcCRC16( void )
00122 {
00123 
00124 
00125 }
00126 
00127 
00128 // Function is called on RX interrupt
00129 void UART_rxCallback( void )
00130 {
00131     char tempChar;
00132 
00133     tempChar = uartPC.getc();
00134 
00135     // Only save data, if not in blocked mode
00136     if( UART_handler.mode == UART_MODE_LISTEN ) {
00137         if( UART_handler.bytesToRead < UART_BUFFER_LENGTH ) { // no buffer overflow
00138             // Restart timeout timer
00139             uartTimeout.attach_us( &UART_rxTimeout, UART_RX_TIMEOUT_US );
00140             // Save received byte
00141             *UART_handler.ptrReadPositon = tempChar;
00142             UART_handler.ptrReadPositon++;
00143             UART_handler.bytesToRead++;
00144         }
00145     }// if
00146 }
00147 
00148 
00149 void UART_rxTimeout( void )
00150 {
00151     // Disable timeout
00152     uartTimeout.detach();
00153     UART_handler.ptrReadPositon = &uartBuffer[0];
00154     UART_handler.mode = UART_MODE_RX_READY;   // Transmission is ready
00155 }
00156