Nico Bollen / LIN

Dependents:   MBED_LIN_RGB_Master_Example

Revision:
4:41b153e9a39c
Parent:
3:3656b0de0e43
Child:
5:b42737f5dabc
--- a/LinMaster.cpp	Tue May 26 08:33:46 2015 +0000
+++ b/LinMaster.cpp	Tue Jun 16 06:22:42 2015 +0000
@@ -1,7 +1,7 @@
 /* 
  * Master device LIN communication library for mbed
  *
- * Copyright (C) 2014 TASS Belgium NV
+ * Copyright (C) 2015 Bollen Nico
  * 
  * Released under GPL v2
  *
@@ -28,28 +28,38 @@
 const uint8_t breakPeriodMessage = 40;                      /* number of timer overflows in the break field during normal LIN messages */
 const uint8_t breakPeriodAcfg = 74;                         /* number of timer overflows in the break field during autoconfig messages */
     
-LinMaster::LinMaster(PinName InPin, PinName OutPin)
+LinMaster::LinMaster(PinName InPin, PinName OutPin) : LinOutPin(OutPin), LinInPin(InPin), LinIntPin(InPin)
 {
     this->DriverState  = INIT;
     this->LastError    = NoError;
-    this->MyInPin      = InPin;
-    this->MyOutPin     = OutPin;
+    
+    this->LinOutPin.write(1);
+    
+    this->LinInPin.mode(PullUp);
+    
+    this->LinIntPin.fall(this, &LinMaster::PinEventHndl);
+    this->LinIntPin.disable_irq();
+    
+    this->DriverState = IDLE;
+    
     (void)this->baudrate(9600);
 }
 
 LinMaster::~LinMaster()
 {
+    this->LinIntPin.disable_irq();
     this->MyTicker.detach(); 
     this->MyTimer.stop();
 }
 
 bool LinMaster::init(void)
 {
-    DigitalInOut LinOutPin(this->MyOutPin);
-    LinOutPin.output();
-    LinOutPin.write(1);
-    DigitalInOut LinInPin(this->MyInPin);
-    LinInPin.input();
+    this->LinOutPin.write(1);
+    
+    this->LinInPin.mode(PullUp);
+    
+    this->LinIntPin.fall(this, &LinMaster::PinEventHndl);
+    this->LinIntPin.disable_irq();
     
     this->DriverState = IDLE;
     
@@ -74,26 +84,21 @@
     return ( 1000000 / (2 * this->u16HalfBitPeriod) );
 }
 
-bool LinMaster::tx_frame(Frame_t * ptrFrame)
+bool LinMaster::send_frame(Frame_t * ptrFrame)
 {
     bool blReturn = false;
-    DigitalInOut LinInPin(this->MyInPin);
-    LinInPin.input();
     
-    if ( (this->DriverState == IDLE) && (LinInPin.read() == 1) )
+    if ( (this->DriverState == IDLE) && (this->LinInPin.read() == 1) )
     {
         /* Clear and initialize all registers */
         
         /* Disable half bit interrupt */
         this->MyTicker.detach();
         
-        InterruptIn IntPin(this->MyInPin);
-        IntPin.fall(this, &LinMaster::PinEventHndl);
-        IntPin.disable_irq();
-        
-        DigitalInOut LinPin(this->MyOutPin);        
-        LinPin.output();
-        LinPin.write(1);
+        this->LinIntPin.fall(this, &LinMaster::PinEventHndl);
+        this->LinIntPin.disable_irq();
+              
+        this->LinOutPin.write(1);
         
         this->DriverState = TRANSMIT;                       /* State of the LIN bus is transceiving a frame */
         this->LastError = NoError;
@@ -160,12 +165,12 @@
     return ( blReturn );
 }
 
-bool LinMaster::rx_frame(Frame_t *ptrFrame)
+bool LinMaster::get_rx_data(Frame_t *ptrFrame)
 {
     uint16_t u16Crc;
     uint8_t i;
 
-    if (this->DriverState != IDLE)
+    if ( (this->DriverState != IDLE) || (this->LastError != NoError))
     {
         return (false);
     }
@@ -202,21 +207,18 @@
 
 void LinMaster::TickEventHndl(void)
 {
-    DigitalInOut LinOutPin(this->MyOutPin);  
-    LinOutPin.output();
-    
     if (this->FrameStatus < Break_OK)
     {
         /* Do break field transmission */
         if (this->breakLength > 2)
         {
             /* Dominant Level */
-            LinOutPin.write(0); 
+            this->LinOutPin.write(0); 
         }
         else
         {
             /* Recessive Level */
-            LinOutPin.write(1); 
+            this->LinOutPin.write(1); 
         }
 
         if (this->breakLength > 0)
@@ -239,14 +241,10 @@
             this->MyTicker.detach();
             
             /* Disable LIN bus level interrupt */
-            InterruptIn IntPin(this->MyInPin);
-            IntPin.disable_irq();
+            this->LinIntPin.disable_irq();
         }
         else
         {
-            DigitalInOut LinInPin(this->MyInPin);
-            LinInPin.input();
-            
             /* Data needs to be transmitted or received */
             if ( (this->linMessageType == S2M) &&
                  (this->FrameStatus >= ID_OK))
@@ -270,8 +268,7 @@
                     this->MyTicker.detach();
                     
                     /* Disable LIN bus level interrupt */
-                    InterruptIn IntPin(this->MyInPin);
-                    IntPin.disable_irq();
+                    this->LinIntPin.disable_irq();
                 }
 
                 /* S2M message data receiving */
@@ -309,7 +306,7 @@
                     this->ByteStatus = BStart;
 
                     /* Check the current bus level */
-                    if (LinInPin.read() == 0)
+                    if (this->LinInPin.read() == 0)
                     {
                         this->LastError = FramingErr;       /* stop bit not valid => framing error */
                     }
@@ -324,8 +321,7 @@
                         this->MyTicker.detach();
                         
                         /* Disable LIN bus level interrupt */
-                        InterruptIn IntPin(this->MyInPin);
-                        IntPin.disable_irq();
+                        this->LinIntPin.disable_irq();
                     }
                     else
                     {
@@ -333,8 +329,7 @@
                         
                         /* Disable LIN bus level interrupt */
                         this->MyTimer.start();
-                        InterruptIn IntPin(this->MyInPin);
-                        IntPin.enable_irq();
+                        this->LinIntPin.enable_irq();
                     }
 
                     break;
@@ -364,7 +359,7 @@
                 {
                 case StartbitEdge:
                     /* Start bit : start */
-                    LinOutPin.write(0); 
+                    this->LinOutPin.write(0); 
                     break;
 
                 case StartbitSample:
@@ -383,12 +378,12 @@
                     if (this->TXbuf[this->TXbufIndex] & 0x01)
                     {
                         /* Recessive Level */
-                        LinOutPin.write(1); 
+                        this->LinOutPin.write(1); 
                     }
                     else
                     {
                         /* Dominant Level */
-                        LinOutPin.write(0); 
+                        this->LinOutPin.write(0); 
                     }
 
                     this->TXbuf[this->TXbufIndex] >>= 1;
@@ -409,12 +404,12 @@
 
                 case StopbitEdge:
                     /* Stop bit : start */
-                    LinOutPin.write(1); 
+                    this->LinOutPin.write(1); 
                     break;
 
                 case StopbitSample:
                     /* Stop bit : mid / level check */
-                    if (LinInPin.read() == 0)
+                    if (this->LinInPin.read() == 0)
                     {
                         /* Stop bit not valid => framing error */
                         this->LastError = FramingErr;
@@ -424,8 +419,7 @@
                         this->MyTicker.detach();
                         
                         /* Disable LIN bus level interrupt */
-                        InterruptIn IntPin(this->MyInPin);
-                        IntPin.disable_irq();
+                        this->LinIntPin.disable_irq();
                     }
 
                     break;
@@ -444,8 +438,7 @@
                         {
                             /* Stop bit of header is sent, now start receiving data bytes */
                             this->MyTimer.start();
-                            InterruptIn IntPin(this->MyInPin);
-                            IntPin.enable_irq();
+                            this->LinIntPin.enable_irq();
                         }
                     }
                     else if (this->TXbufIndex >= this->FrameLength)
@@ -458,8 +451,7 @@
                         this->MyTicker.detach();
                         
                         /* Disable LIN bus level interrupt */
-                        InterruptIn IntPin(this->MyInPin);
-                        IntPin.disable_irq();
+                        this->LinIntPin.disable_irq();
                     }
 
                     break;
@@ -488,8 +480,7 @@
         this->MyTicker.attach_us(this, &LinMaster::TickEventHndl, this->u16HalfBitPeriod);
         
         /* Disable LIN bus level interrupt */
-        InterruptIn IntPin(this->MyInPin);
-        IntPin.disable_irq();
+        this->LinIntPin.disable_irq();
 
         if (this->RXtimeoutSubCTR > this->u16HalfBitPeriod)
         {