Knud Dalgaard / 310-TMC3-TestHW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RS485.cpp Source File

RS485.cpp

00001 #include "mbed.h"
00002 #include "RS485.h"
00003 #include "crc16.h"
00004 
00005 
00006 //---------------------------------------
00007 // Hardware recources
00008 //---------------------------------------
00009 Serial uartRS485(p28, p27);
00010 DigitalOut RS485RTS( p20 );
00011 Timeout RS485Timeout;
00012 Timeout RS485TimeoutTx;
00013 
00014 
00015 
00016 //---------------------------------------
00017 // Prototypes
00018 //---------------------------------------
00019 void RS485_initStructure( void );
00020 void RS485_rxCallback( void );
00021 void RS485_rxTimeout( void );
00022 void RS485_txTimeout( void );
00023 
00024 // void testcomm(void);  // UpdateAll
00025 
00026 //---------------------------------------
00027 // Internal variables
00028 //---------------------------------------
00029 char RS485Buffer[RS485_BUFFER_LENGTH];
00030 
00031 
00032 
00033 //---------------------------------------
00034 // External variables
00035 //---------------------------------------
00036 sRS485_handler RS485_handler;
00037 
00038 
00039 
00040 //---------------------------------------
00041 // Global Functions
00042 //---------------------------------------
00043 
00044 void RS485_init( void )
00045 {
00046     uartRS485.baud(RS485_BAUDRATE);
00047  //   uartRS485.format(RS485_BITS, Serial::None, RS485_STOPBIT );
00048     uartRS485.format(RS485_BITS, Serial::Even, RS485_STOPBIT );   // 21.01.2014 KED
00049     RS485RTS = 0;
00050     RS485_reset();
00051     uartRS485.attach( &RS485_rxCallback, Serial::RxIrq );      // call RS485_rx() on every RX interrupt
00052     
00053     /*
00054     #warning TEST
00055     testcomm();  // UpdateAll
00056     wait(1.0);
00057     testcomm();  // UpdateAll
00058     */
00059 }
00060 
00061 
00062 void RS485_reset( void )
00063 {
00064     RS485_handler.ptrRXBuffer = &RS485Buffer[0];
00065     RS485_handler.bytesToRead = 0;
00066     RS485_handler.mode = RS485_MODE_LISTEN;
00067 }
00068 
00069 
00070 // Return 1: Sending was successfull
00071 // Return 0: Could not send data zero bytes
00072 int RS485_sendData( char *ptrDataToSend, int NumBytes )
00073 {
00074     int i;
00075 
00076     if( NumBytes > 0 ) 
00077     {
00078         // Set RS485 driver to sending mode
00079         RS485RTS = 1;
00080         
00081         // Send data
00082         for( i = 0; i < NumBytes; i++ ) 
00083         {
00084             while( !uartRS485.writeable() );
00085             uartRS485.putc(*ptrDataToSend);
00086             ptrDataToSend++;
00087         }//for
00088         
00089         // Restart timeout timer
00090         RS485TimeoutTx.attach_us( &RS485_txTimeout, RS485_TX_TIMEOUT_US );
00091         
00092         return 1;
00093     }//if
00094 
00095     return 0;
00096 }
00097 
00098 
00099 
00100 
00101 
00102 
00103 // Return 1: New data available, get pointer to buffer and number of bytes to read
00104 // Return 0: No new data available
00105 int RS485_receiveData( char *& ptrData, int *ptrNumBytes )
00106 {    
00107     // Dummy operation to avoid compiler warning
00108     ptrData++;
00109 
00110     if( RS485_handler.mode != RS485_MODE_RX_READY )
00111     {    
00112         *ptrNumBytes = 0;
00113         return 0;
00114     }
00115 
00116     *ptrNumBytes = RS485_handler.bytesToRead;
00117     ptrData = &RS485Buffer[0];
00118         
00119     RS485_reset();
00120     return 1;    
00121 }
00122 
00123 
00124 
00125 //---------------------------------------
00126 // Internal Functions
00127 //---------------------------------------
00128 
00129 // Function is called on RX interrupt
00130 void RS485_rxCallback( void )
00131 {
00132     char tempChar;
00133 
00134     tempChar = uartRS485.getc();
00135 
00136     // Only save data, if not in blocked mode
00137     if( RS485_handler.mode == RS485_MODE_LISTEN ) {
00138         if( RS485_handler.bytesToRead < RS485_BUFFER_LENGTH ) { // no buffer overflow
00139             // Restart timeout timer
00140             RS485Timeout.attach_us( &RS485_rxTimeout, RS485_RX_TIMEOUT_US );
00141             // Save received byte
00142             *RS485_handler.ptrRXBuffer = tempChar;
00143             RS485_handler.ptrRXBuffer++;
00144             RS485_handler.bytesToRead++;
00145         }
00146     }// if
00147 }
00148 
00149 
00150 void RS485_txTimeout( void )
00151 {
00152     // Set RS485 driver to receiving mode
00153     RS485RTS = 0;
00154 }
00155 
00156 
00157 void RS485_rxTimeout( void )
00158 {
00159     // Disable timeout
00160     RS485Timeout.detach();
00161     RS485_handler.mode = RS485_MODE_RX_READY;   // Transmission is ready
00162 }
00163 
00164 
00165 /*
00166 // TEST LCD communication
00167 void testcomm(void)  // UpdateAll
00168 {
00169         char i;
00170         unsigned short clacCRC16;
00171     
00172         RS485Buffer[0] = 0x00;  // Address 
00173         RS485Buffer[1] = 0x04;  // Command = UpdateAll 
00174           
00175         for (i=0; i < 80; i++)
00176         {
00177             RS485Buffer[i+2] = 0x30;
00178         }
00179 
00180         RS485Buffer[82] = 0x01; // linie 
00181         RS485Buffer[83] = 0x02; // pos
00182         RS485Buffer[84] = 0x03; // BlinkingCurserOn
00183    
00184      
00185         RS485Buffer[85] = 0x00; // LED Error
00186     
00187         RS485Buffer[86] = 0x00; // high Contrast  
00188         RS485Buffer[87] = 0x00; // low Contrast
00189     
00190 
00191         calcCRC16(&RS485Buffer[0], 88, &clacCRC16);
00192         RS485Buffer[88] = (unsigned char)(0x00FF & (clacCRC16>>8));     // set High Byte
00193         RS485Buffer[89] = (unsigned char)(0x00FF & clacCRC16);          // set Low Byte  
00194      
00195         RS485_sendData( &RS485Buffer[0], 90 );  
00196 } 
00197 
00198 */