This is a RS485 class that uses the second UART and was tested on a Nucleo F030R8. A main demo program howto use the class is included. This class control the direction pin on the transceiver buffer automatically, and used transmit and receive interrupts. Ring buffers (256 bytes) are implemented on both transmission and reception. It assumes a ADM3485 'type' buffer where pins 2 ans 3 are connected and seen as direction. This test program could easily be adapted as base for other programs.

Dependencies:   mbed

Revision:
7:cfe1e0eafb7e
Parent:
6:70460dcbc43c
Child:
8:595258a79939
--- a/dlms_comms.cpp	Tue Nov 11 18:17:52 2014 +0000
+++ b/dlms_comms.cpp	Tue Nov 11 19:19:47 2014 +0000
@@ -8,7 +8,8 @@
    // Instantiate the raw serial (2nd uart) Tx then Rx
    RawSerial  rs485(PA_9, PA_10);
   // pin to switch the rs485 buffer direction
-   DigitalOut dir_485 (PB_0);
+  DigitalOut dir_485 (PB_0);
+//bool dir_485 = 0;   
 
 /** ---------------------------------------------------------------------------
  * @brief  C O N S T R U C T O R
@@ -23,21 +24,38 @@
     rx_tail_ptr = 0;
     memset (rx_buffer, 0, sizeof(rx_buffer));
 
-//    // attach the receiver input to 'run' a method
-//    // when characters received
-    rs485.attach(this,
-                 &dlms_comms::Rx_interrupt,
-                 RawSerial::RxIrq);
     // transmit empty flag .. need to set buffer
     // in receive mode again after we have transmitted
     // all data characters
 //    rs485.attach(this,
 //                 &dlms_comms::Tx_interrupt,
-//                 RawSerial::TxIrq);
-//    timer = NULL;
+//                 //RawSerial::TxIrq
+//                 Serial::TxIrq);
+    
+    // attach the receiver input to 'run' a method
+    rs485.attach(NULL, Serial::TxIrq);
+                
+    // when characters received
+    rs485.attach(this,
+                 &dlms_comms::Rx_interrupt,
+                 //RawSerial::RxIrq
+                 Serial::RxIrq);
+    timer = NULL;
     debug_uart = NULL;
     tx_irq_count = 0l;
     rx_irq_count = 0l;
+    
+    
+        /** Set the flow control type on the serial port
+     *
+     *  @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
+     *  @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
+     *  @param flow2 the second flow control pin (CTS for RTSCTS)
+     */
+//    rs485.set_flow_control(RTSCTS , //Flow type, 
+//                           dir_485, //PB_0, //PinName flow1=NC, 
+//                           dir_485 //PB_0 //PinName flow2=NC
+//                           );
 }
 /** ---------------------------------------------------------------------------
  * @brief bq34z110::init_port_expander Put all ports in INPUT mode
@@ -51,7 +69,7 @@
     debug_uart  = parent_uart; 
     timer       = parent_timer;
 //#ifdef DEBUG_DLMS_COMMS    
-    debug_uart->printf ("   init_dlms_comms..\r\n");
+    //debug_uart->printf ("   init_dlms_comms..\r\n");
 //#endif      
 }
 /** ---------------------------------------------------------------------------
@@ -97,10 +115,10 @@
     rs485.format((int) bits,
                  my_parity,
                  (int) stop_bits);
-#  ifdef DEBUG_DLMS_COMMS    
-     debug_uart->printf ("   init_dlms_comms..Baudrate (%d) %d bits Parity=%c\r\n",
-                          baudrate, bits, parity);
-#  endif                          
+//#  ifdef DEBUG_DLMS_COMMS    
+     //debug_uart->printf ("   init_dlms_comms..Baudrate (%d) %d bits Parity=%c\r\n",
+     //                     baudrate, bits, parity);
+//#  endif                          
      // dummy send packet to rs485 
      //send_packet ("\r\nHello cruel world\r\n",12);       
      if ( rs485.readable())
@@ -122,8 +140,8 @@
     tx_irq_count++;
     // enable the receiver, we are finito with the transmission of characters
     // this changes the direction on the transceiver buffer
-    if (dir_485 == 1)
-        dir_485 = 0;       
+    rs485.attach(NULL, Serial::RxIrq);
+    dir_485 = 0;       
 }
 /** ---------------------------------------------------------------------------
  * @brief dlms_comms::Rx_interrupt . This method received all characters from
@@ -186,6 +204,10 @@
     {
         rs485.putc (*tmp_ptr++);
     }
+    rs485.attach(this,
+                 &dlms_comms::Tx_interrupt,
+                 //RawSerial::TxIrq
+                 Serial::TxIrq);
 }
 UINT_64 dlms_comms::ret_rx_irq_count (void)
 {