CANPort provides a higher level interface to a CAN communication channel, and provides timestamping, servicing additional hardware interfaces (optional activity LED, CAN transceiver slope control)

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Thu Sep 19 21:46:28 2019 +0000
Parent:
2:1824d1421b6d
Commit message:
Clean up CANPort to have access to underlying CAN methods.

Changed in this revision

CANPort.cpp Show annotated file Show diff for this revision Revisions of this file
CANPort.h Show annotated file Show diff for this revision Revisions of this file
--- a/CANPort.cpp	Fri Aug 30 21:44:31 2019 +0000
+++ b/CANPort.cpp	Thu Sep 19 21:46:28 2019 +0000
@@ -28,9 +28,10 @@
 #define FLASH_RX_LEVEL 1.00
 
 
-CANPort::CANPort(CANCHANNEL_T chNum, PinName rd, PinName td, PinName _activityPin, PinName _slopePin, CANSlopeControl_T slope) {
+CANPort::CANPort(CANCHANNEL_T chNum, PinName rd, PinName td, PinName _activityPin, PinName _slopePin, CANSlopeControl_T slope) 
+    : CAN(rd,td) {
     channel = chNum;
-    can = new CAN(rd, td);
+    //can = new CAN(rd, td);
     if (_activityPin != NC) {
         activityPin = new PwmOut(_activityPin);
         activityPin->pulsewidth_us(100);
@@ -53,11 +54,11 @@
         delete slopePin;
     if (activityPin)
         delete activityPin;
-    if (can)
-        delete can;
+    //if (can)
+    //    delete can;
     slopePin = NULL;
     activityPin = NULL;
-    can = NULL;
+    //can = NULL;
 }
 
 
@@ -66,7 +67,7 @@
 
     if (msg.dir == xmt) {   // we have to have indicated our intent to transmit
         msg.ch = channel;
-        if (can->write(CANMessage(msg.id, (char *)&msg.data, msg.len, CANData, msg.format))) {
+        if ( write(CANMessage(msg.id, (char *)&msg.data, msg.len, CANData, msg.format))) {
             txCounter++;
             Flash(msg.dir);
             success = true;
@@ -80,7 +81,7 @@
     bool success = false;
     CANMessage _msg;
 
-    if (can->read(_msg)) {
+    if (read(_msg)) {
         /// @TODO This looks like a very inefficient method, but it works.
         CANmsg Xmsg(channel, rcv, _msg);
         msg = Xmsg;
@@ -92,9 +93,9 @@
 }
 
 
-void CANPort::Attach( void (*fptr)(void)) {
-    can->attach(fptr);
-}
+//void CANPort::Attach( void (*fptr)(void) ) {
+//    can->attach(fptr);
+//}
 
 
 void CANPort::Extinguish(void) {
@@ -125,11 +126,11 @@
 bool CANPort::SetBusMode(CANBusMode_T mode) {
     switch (mode) {
         case MONITOR:
-            can->monitor(true);
+            monitor(true);
             busMode = mode;
             break;
         case ACTIVE:
-            can->monitor(false);
+            monitor(false);
             busMode = mode;
             break;
         default:
@@ -139,7 +140,7 @@
 }
 
 
-CANBusMode_T CANPort::GetBusMode() {
+CANPort::CANBusMode_T CANPort::GetBusMode() {
     return busMode;
 }
 
@@ -169,13 +170,13 @@
 }
 
 
-CANSlopeControl_T CANPort::GetSlopeControl() {
+CANPort::CANSlopeControl_T CANPort::GetSlopeControl() {
     return slopeMode;
 }
 
 
-bool CANPort::SetBitRate(int rate) {
-    if (can->frequency(rate)) {
+bool CANPort::SetBitRate(uint32_t rate) {
+    if (frequency(rate)) {
         bitRate = rate;
         return true;
     } else {
@@ -184,7 +185,7 @@
 }
 
 
-int CANPort::GetBitRate() {
+uint32_t CANPort::GetBitRate() {
     return bitRate;
 }
 
@@ -200,17 +201,17 @@
 
 
 int CANPort::GetTxErrorCounter() {
-    return can->tderror();
+    return tderror();
 }
 
 
 int CANPort::GetRxErrorCounter() {
-    return can->rderror();
+    return rderror();
 }
 
 
 bool CANPort::ResetChip() {
-    can->reset();
+    reset();
     return true;
 }
 
--- a/CANPort.h	Fri Aug 30 21:44:31 2019 +0000
+++ b/CANPort.h	Thu Sep 19 21:46:28 2019 +0000
@@ -28,17 +28,6 @@
 #include "mbed.h"
 #include "CANMessage.h"
 
-typedef enum {
-    HIGHSPEED,
-    NORMALSPEED,
-    STANDBY
-} CANSlopeControl_T;
-
-typedef enum {
-    MONITOR,
-    ACTIVE
-} CANBusMode_T;
-
 /// This is the CANPort, which is the physical interface to CAN
 ///
 /// This derived class has a number of additional capabilities:
@@ -47,9 +36,20 @@
 /// \li counters, to keep track of received and transmitted messages
 /// \li and more...
 ///
-class CANPort {
+class CANPort : public CAN {
 public:
 
+    typedef enum {
+        HIGHSPEED,
+        NORMALSPEED,
+        STANDBY
+    } CANSlopeControl_T;
+    
+    typedef enum {
+        MONITOR,
+        ACTIVE
+    } CANBusMode_T;
+
     /// The advanced form of the constructure to create a CANPort, name 
     /// an activity indicator, and name a slope control pin
     ///
@@ -119,9 +119,9 @@
     /// @param tptr pointer to the object to call the member function on 
     /// @param mptr pointer to the member function to be called 
     ///
-    template <typename T> void Attach(T * tptr, void (T::*mptr)(void)) {
-        can->attach(callback(tptr, mptr));
-    }
+    //template <typename T> void Attach(T * tptr, void (T::*mptr)(void)) {
+    //    can->attach(callback(tptr, mptr));
+    //}
     
     /// This provides control of the AutoReset feature
     ///
@@ -200,9 +200,9 @@
     /// will retain the rate setting and then permits the query of the bitrate.
     /// 
     /// @param rate is the desired bitrate in bits per second.
-    /// @returns true if teh command succeeded
+    /// @returns true if the command succeeded
     ///
-    bool SetBitRate(int rate);
+    bool SetBitRate(uint32_t rate);
     
     /// This returns the current desired bitrate for the CAN channel
     ///
@@ -211,7 +211,7 @@
     ///
     /// @returns the bitrate in bits per second
     ///
-    int GetBitRate();
+    uint32_t GetBitRate();
 
     /// This returns the number of messages that were sent by this CAN channel
     ///
@@ -265,7 +265,7 @@
 
 private:
     CANCHANNEL_T channel;       // user assigned port number of this port
-    CAN * can;                  // bind to a specific CAN
+    //CAN * can;                  // bind to a specific CAN
     CANBusMode_T busMode;       // monitor or active mode
     int bitRate;                // bit rate for this bus
     PwmOut * activityPin;       // LED to indicate activity