Nico Bollen / LIN

Dependents:   MBED_LIN_RGB_Master_Example

Revision:
5:b42737f5dabc
Parent:
4:41b153e9a39c
Child:
8:63d341e53cce
diff -r 41b153e9a39c -r b42737f5dabc LinMaster.cpp
--- a/LinMaster.cpp	Tue Jun 16 06:22:42 2015 +0000
+++ b/LinMaster.cpp	Thu Jun 25 06:30:47 2015 +0000
@@ -37,7 +37,6 @@
     
     this->LinInPin.mode(PullUp);
     
-    this->LinIntPin.fall(this, &LinMaster::PinEventHndl);
     this->LinIntPin.disable_irq();
     
     this->DriverState = IDLE;
@@ -57,8 +56,7 @@
     this->LinOutPin.write(1);
     
     this->LinInPin.mode(PullUp);
-    
-    this->LinIntPin.fall(this, &LinMaster::PinEventHndl);
+
     this->LinIntPin.disable_irq();
     
     this->DriverState = IDLE;
@@ -91,13 +89,13 @@
     if ( (this->DriverState == IDLE) && (this->LinInPin.read() == 1) )
     {
         /* Clear and initialize all registers */
+        memset(this->RXbuf, 0, 11);
         
         /* Disable half bit interrupt */
         this->MyTicker.detach();
         
-        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 */
@@ -165,7 +163,7 @@
     return ( blReturn );
 }
 
-bool LinMaster::get_rx_data(Frame_t *ptrFrame)
+bool LinMaster::get_rx_data(Frame_t &ptrFrame)
 {
     uint16_t u16Crc;
     uint8_t i;
@@ -176,7 +174,7 @@
     }
 
     /* Copy data and check RX frame CRC */
-    if (ptrFrame->CrcType == Enhanced)
+    if (ptrFrame.CrcType == Enhanced)
     {
         u16Crc = RXbuf[1];
     }
@@ -185,9 +183,9 @@
         u16Crc = 0;
     }
 
-    for (i = 0; i < ptrFrame->DataLen; i++)
+    for (i = 0; i < ptrFrame.DataLen; i++)
     {
-        ptrFrame->Data[i] = RXbuf[1 + 1 + i];
+        ptrFrame.Data[i] = RXbuf[1 + 1 + i];
         u16Crc += RXbuf[1 + 1 + i];
         if (u16Crc >= 256)
         {
@@ -195,7 +193,7 @@
         }
     }
     
-    if (this->RXbuf[ptrFrame->DataLen + 2] == (uint8_t)(~u16Crc))
+    if (this->RXbuf[ptrFrame.DataLen + 2] == (uint8_t)(~u16Crc))
     {
         return (true);
     }
@@ -326,14 +324,15 @@
                     else
                     {
                         /* Wait for a new data byte */
+                        this->MyTimer.start();
                         
                         /* Disable LIN bus level interrupt */
-                        this->MyTimer.start();
+                        this->LinIntPin.fall(this, &LinMaster::PinEventHndl);
                         this->LinIntPin.enable_irq();
                     }
 
                     break;
-
+                    
                 case Databit0Edge:
                 case Databit1Edge:
                 case Databit2Edge:
@@ -399,7 +398,7 @@
                 case Databit7Sample:
                     /* Odd overflow, mid of bit time ==> sample the bus for RX */
                     this->RXbuf[this->RXbufIndex] >>= 1;
-                    this->RXbuf[this->RXbufIndex] |= (LinInPin.read() << 7);
+                    this->RXbuf[this->RXbufIndex] |= ((LinInPin.read() << 7) & 0x80);
                     break;
 
                 case StopbitEdge:
@@ -438,6 +437,9 @@
                         {
                             /* Stop bit of header is sent, now start receiving data bytes */
                             this->MyTimer.start();
+                            
+                            /* Enable LIN bus level interrupt */
+                            this->LinIntPin.fall(this, &LinMaster::PinEventHndl);
                             this->LinIntPin.enable_irq();
                         }
                     }