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)

Committer:
WiredHome
Date:
Thu Sep 19 21:46:28 2019 +0000
Revision:
3:4c780c641125
Parent:
2:1824d1421b6d
Clean up CANPort to have access to underlying CAN methods.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:7b81b19d9b10 1 /// @file CANPort.h is where all the port level functionality is defined
WiredHome 0:7b81b19d9b10 2 ///
WiredHome 0:7b81b19d9b10 3 /// This module creates the physical interface, services the hardware, and provides
WiredHome 0:7b81b19d9b10 4 /// additional functionality on top of that (e.g. timestamp messages).
WiredHome 0:7b81b19d9b10 5 ///
WiredHome 0:7b81b19d9b10 6 /// @todo Instead of providing a callback directly to the user code, all
WiredHome 0:7b81b19d9b10 7 /// callbacks should be handled in this class, and then the user
WiredHome 0:7b81b19d9b10 8 /// callback could be activated. In this way, the rxCounter, timestamps,
WiredHome 0:7b81b19d9b10 9 /// and other features are preserved.
WiredHome 0:7b81b19d9b10 10 ///
WiredHome 0:7b81b19d9b10 11 /// @note Copyright &copr; 2011 by Smartware Computing, all rights reserved.
WiredHome 0:7b81b19d9b10 12 /// Individuals may use this application for evaluation or non-commercial
WiredHome 0:7b81b19d9b10 13 /// purposes. Within this restriction, changes may be made to this application
WiredHome 0:7b81b19d9b10 14 /// as long as this copyright notice is retained. The user shall make
WiredHome 0:7b81b19d9b10 15 /// clear that their work is a derived work, and not the original.
WiredHome 0:7b81b19d9b10 16 /// Users of this application and sources accept this application "as is" and
WiredHome 0:7b81b19d9b10 17 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 0:7b81b19d9b10 18 /// using this application - whether real or imagined.
WiredHome 0:7b81b19d9b10 19 ///
WiredHome 0:7b81b19d9b10 20 /// @author David Smart, Smartware Computing
WiredHome 0:7b81b19d9b10 21 ///
WiredHome 0:7b81b19d9b10 22 /// History
WiredHome 0:7b81b19d9b10 23 /// v1.01 20110608
WiredHome 0:7b81b19d9b10 24 /// \li initial version numbered to match other CAN components
WiredHome 0:7b81b19d9b10 25 ///
WiredHome 0:7b81b19d9b10 26 #ifndef CANPORT_H
WiredHome 0:7b81b19d9b10 27 #define CANPORT_H
WiredHome 0:7b81b19d9b10 28 #include "mbed.h"
WiredHome 0:7b81b19d9b10 29 #include "CANMessage.h"
WiredHome 0:7b81b19d9b10 30
WiredHome 0:7b81b19d9b10 31 /// This is the CANPort, which is the physical interface to CAN
WiredHome 0:7b81b19d9b10 32 ///
WiredHome 0:7b81b19d9b10 33 /// This derived class has a number of additional capabilities:
WiredHome 0:7b81b19d9b10 34 /// \li activity indicator to show receive and transmit activity per port
WiredHome 0:7b81b19d9b10 35 /// \li slope control pin to permit controlling performance and power modes
WiredHome 0:7b81b19d9b10 36 /// \li counters, to keep track of received and transmitted messages
WiredHome 0:7b81b19d9b10 37 /// \li and more...
WiredHome 0:7b81b19d9b10 38 ///
WiredHome 3:4c780c641125 39 class CANPort : public CAN {
WiredHome 0:7b81b19d9b10 40 public:
WiredHome 0:7b81b19d9b10 41
WiredHome 3:4c780c641125 42 typedef enum {
WiredHome 3:4c780c641125 43 HIGHSPEED,
WiredHome 3:4c780c641125 44 NORMALSPEED,
WiredHome 3:4c780c641125 45 STANDBY
WiredHome 3:4c780c641125 46 } CANSlopeControl_T;
WiredHome 3:4c780c641125 47
WiredHome 3:4c780c641125 48 typedef enum {
WiredHome 3:4c780c641125 49 MONITOR,
WiredHome 3:4c780c641125 50 ACTIVE
WiredHome 3:4c780c641125 51 } CANBusMode_T;
WiredHome 3:4c780c641125 52
WiredHome 0:7b81b19d9b10 53 /// The advanced form of the constructure to create a CANPort, name
WiredHome 0:7b81b19d9b10 54 /// an activity indicator, and name a slope control pin
WiredHome 0:7b81b19d9b10 55 ///
WiredHome 0:7b81b19d9b10 56 /// This version lets you specify the receive and transmit pins
WiredHome 0:7b81b19d9b10 57 /// for a CANPort, indicate a pin which is used to indicate
WiredHome 0:7b81b19d9b10 58 /// CAN activity, both receive and transmit, and identify a slope
WiredHome 0:7b81b19d9b10 59 /// control pin.
WiredHome 0:7b81b19d9b10 60 ///
WiredHome 0:7b81b19d9b10 61 /// @param chNum is the user assigned channel number given to this port
WiredHome 0:7b81b19d9b10 62 /// @param rd is the receive pin
WiredHome 0:7b81b19d9b10 63 /// @param td is the transmit pin
WiredHome 0:7b81b19d9b10 64 /// @param activityPin is the PWM pin used to indicate CAN traffic
WiredHome 0:7b81b19d9b10 65 /// @param slopePin is the DigitalInOut pin used to control slope
WiredHome 0:7b81b19d9b10 66 /// @param slope is the initial slope setting
WiredHome 0:7b81b19d9b10 67 ///
WiredHome 0:7b81b19d9b10 68 CANPort(CANCHANNEL_T chNum, PinName rd, PinName td, PinName activityPin = NC, PinName slopePin = NC, CANSlopeControl_T slope = NORMALSPEED);
WiredHome 0:7b81b19d9b10 69
WiredHome 0:7b81b19d9b10 70 /// Destroys the CANPort
WiredHome 0:7b81b19d9b10 71 virtual ~CANPort();
WiredHome 0:7b81b19d9b10 72
WiredHome 0:7b81b19d9b10 73 /// Transmit a message to the bus
WiredHome 0:7b81b19d9b10 74 ///
WiredHome 0:7b81b19d9b10 75 /// This method transmits a message to the bus.
WiredHome 0:7b81b19d9b10 76 ///
WiredHome 0:7b81b19d9b10 77 /// @param msg is a CANmsg
WiredHome 0:7b81b19d9b10 78 /// @returns true if the message was successfully written to CAN
WiredHome 0:7b81b19d9b10 79 ///
WiredHome 0:7b81b19d9b10 80 bool TransmitMsg(CANmsg msg);
WiredHome 0:7b81b19d9b10 81
WiredHome 0:7b81b19d9b10 82 /// Read a message from the bus into a buffer
WiredHome 0:7b81b19d9b10 83 ///
WiredHome 0:7b81b19d9b10 84 /// This method will extract a message from the bus and put it
WiredHome 0:7b81b19d9b10 85 /// in the message space provided
WiredHome 0:7b81b19d9b10 86 ///
WiredHome 0:7b81b19d9b10 87 /// @param msg is a reference to a CANmsg into which the msg is placed
WiredHome 0:7b81b19d9b10 88 /// @returns true if a message was received and processed
WiredHome 0:7b81b19d9b10 89 ///
WiredHome 0:7b81b19d9b10 90 bool ReceiveMsg(CANmsg &msg);
WiredHome 0:7b81b19d9b10 91
WiredHome 0:7b81b19d9b10 92 /// This flashes the activity indicator associated with this CANPort
WiredHome 0:7b81b19d9b10 93 ///
WiredHome 0:7b81b19d9b10 94 /// If the activity indicator is configured.
WiredHome 0:7b81b19d9b10 95 ///
WiredHome 0:7b81b19d9b10 96 /// @param tx indiciates if the flash is to be a sign of xmt or rcv
WiredHome 0:7b81b19d9b10 97 /// it will flash dimly for a transmit and bright for a receive
WiredHome 0:7b81b19d9b10 98 ///
WiredHome 0:7b81b19d9b10 99 void Flash(CANDIR_T tx);
WiredHome 0:7b81b19d9b10 100
WiredHome 0:7b81b19d9b10 101 /// This extinguishes the activity indicator associated with this CANPort
WiredHome 0:7b81b19d9b10 102 ///
WiredHome 0:7b81b19d9b10 103 /// If the activity indicator is configured.
WiredHome 0:7b81b19d9b10 104 ///
WiredHome 0:7b81b19d9b10 105 void Extinguish(void);
WiredHome 0:7b81b19d9b10 106
WiredHome 0:7b81b19d9b10 107 /// Attach a callback whenever a CAN receive interrupt is generated
WiredHome 0:7b81b19d9b10 108 ///
WiredHome 0:7b81b19d9b10 109 /// This attaches a simple callback to the CAN ISR
WiredHome 0:7b81b19d9b10 110 ///
WiredHome 0:7b81b19d9b10 111 /// @param fptr pointer to the function to be called
WiredHome 0:7b81b19d9b10 112 ///
WiredHome 0:7b81b19d9b10 113 void Attach( void (*fptr)(void) );
WiredHome 0:7b81b19d9b10 114
WiredHome 0:7b81b19d9b10 115 /// Attach a callback whenever a CAN receive interrupt is generated
WiredHome 0:7b81b19d9b10 116 ///
WiredHome 0:7b81b19d9b10 117 /// This attaches a callback to the CAN ISR
WiredHome 0:7b81b19d9b10 118 ///
WiredHome 0:7b81b19d9b10 119 /// @param tptr pointer to the object to call the member function on
WiredHome 0:7b81b19d9b10 120 /// @param mptr pointer to the member function to be called
WiredHome 0:7b81b19d9b10 121 ///
WiredHome 3:4c780c641125 122 //template <typename T> void Attach(T * tptr, void (T::*mptr)(void)) {
WiredHome 3:4c780c641125 123 // can->attach(callback(tptr, mptr));
WiredHome 3:4c780c641125 124 //}
WiredHome 0:7b81b19d9b10 125
WiredHome 0:7b81b19d9b10 126 /// This provides control of the AutoReset feature
WiredHome 0:7b81b19d9b10 127 ///
WiredHome 0:7b81b19d9b10 128 /// AutoReset is a feature that permits automatically resetting the
WiredHome 0:7b81b19d9b10 129 /// CAN chip when it is trapped in an error state (e.g. bus off).
WiredHome 0:7b81b19d9b10 130 /// The default is disabled.
WiredHome 0:7b81b19d9b10 131 ///
WiredHome 0:7b81b19d9b10 132 /// @param enable is used to enable or disable the auto reset feature
WiredHome 0:7b81b19d9b10 133 /// @returns true if the command was accepted
WiredHome 0:7b81b19d9b10 134 ///
WiredHome 0:7b81b19d9b10 135 bool SetAutoReset(bool enable);
WiredHome 0:7b81b19d9b10 136
WiredHome 0:7b81b19d9b10 137 /// This returns the current state of the AutoReset feature
WiredHome 0:7b81b19d9b10 138 ///
WiredHome 0:7b81b19d9b10 139 /// Returns a value indicating the current state
WiredHome 0:7b81b19d9b10 140 ///
WiredHome 0:7b81b19d9b10 141 /// @returns true if AutoReset is enabled
WiredHome 0:7b81b19d9b10 142 /// @returns false if AutoReset is disabled
WiredHome 0:7b81b19d9b10 143 ///
WiredHome 0:7b81b19d9b10 144 bool GetAutoReset() { return autoReset; }
WiredHome 0:7b81b19d9b10 145
WiredHome 0:7b81b19d9b10 146 /// This provides control to set the bus mode as active or listener only
WiredHome 0:7b81b19d9b10 147 ///
WiredHome 0:7b81b19d9b10 148 /// The CAN adapter can be placed into a monitor(LISTENER) mode (sometimes
WiredHome 0:7b81b19d9b10 149 /// called promiscuous mode) or into an active mode, in which case it
WiredHome 0:7b81b19d9b10 150 /// will send hardware acknowledge.
WiredHome 0:7b81b19d9b10 151 ///
WiredHome 0:7b81b19d9b10 152 /// @param mode is either MONITOR or ACTIVE
WiredHome 0:7b81b19d9b10 153 /// @returns true if the command was accepted
WiredHome 0:7b81b19d9b10 154 ///
WiredHome 0:7b81b19d9b10 155 bool SetBusMode(CANBusMode_T mode);
WiredHome 0:7b81b19d9b10 156
WiredHome 0:7b81b19d9b10 157 /// This returns the current state of the bus mode
WiredHome 0:7b81b19d9b10 158 ///
WiredHome 0:7b81b19d9b10 159 /// @returns MONITOR if the chip is in the monitor (listen / promiscuous) mode
WiredHome 0:7b81b19d9b10 160 /// @returns ACTIVE if the chip is in the active mode
WiredHome 0:7b81b19d9b10 161 ///
WiredHome 0:7b81b19d9b10 162 CANBusMode_T GetBusMode();
WiredHome 0:7b81b19d9b10 163
WiredHome 0:7b81b19d9b10 164 /// This provides control to set the transceiver slope control mode
WiredHome 0:7b81b19d9b10 165 ///
WiredHome 0:7b81b19d9b10 166 /// Many CAN transceivers can be operated in one of several modes -
WiredHome 0:7b81b19d9b10 167 /// \li HIGHSPEED - which supports the highest frequency of communication, but
WiredHome 0:7b81b19d9b10 168 /// does tend to generate more EMI unless the cable is properly shielded
WiredHome 0:7b81b19d9b10 169 /// \li NORMALSPEED - which wave-shapes the rising and falling edge, and
WiredHome 0:7b81b19d9b10 170 /// significantly reduces the EMI concerns, but may trade off the
WiredHome 0:7b81b19d9b10 171 /// highest performance.
WiredHome 0:7b81b19d9b10 172 /// \li STANDBY - which puts the chip into the lowest power state, and prevents
WiredHome 0:7b81b19d9b10 173 /// transmission of a message. It can typically still receive messages,
WiredHome 0:7b81b19d9b10 174 /// but this mode may also be useful for other purposes (wake on CAN,
WiredHome 0:7b81b19d9b10 175 /// autobaud w/o disrupting bus traffic, etc.)
WiredHome 0:7b81b19d9b10 176 ///
WiredHome 0:7b81b19d9b10 177 /// @param slope sets the slope control to one of the states HIGHSPEED,
WiredHome 0:7b81b19d9b10 178 /// NORMALSPEED, or STANDBY
WiredHome 0:7b81b19d9b10 179 /// @returns true if the command succeeded
WiredHome 0:7b81b19d9b10 180 /// @returns false if the command failed, which may be due to not having
WiredHome 0:7b81b19d9b10 181 /// defined a slope control pin.
WiredHome 0:7b81b19d9b10 182 ///
WiredHome 0:7b81b19d9b10 183 bool SetSlopeControl(CANSlopeControl_T slope);
WiredHome 0:7b81b19d9b10 184
WiredHome 0:7b81b19d9b10 185 /// This returns the current state of the slope control
WiredHome 0:7b81b19d9b10 186 ///
WiredHome 0:7b81b19d9b10 187 /// The state is controlled by the SetSlopeControl command.
WiredHome 0:7b81b19d9b10 188 ///
WiredHome 0:7b81b19d9b10 189 /// @returns the current state; one of HIGHSPEED, NORMALSPEED, or STANDBY
WiredHome 0:7b81b19d9b10 190 ///
WiredHome 0:7b81b19d9b10 191 CANSlopeControl_T GetSlopeControl();
WiredHome 0:7b81b19d9b10 192
WiredHome 0:7b81b19d9b10 193 /// This sets the bitrate for the CAN channel
WiredHome 0:7b81b19d9b10 194 ///
WiredHome 0:7b81b19d9b10 195 /// This sets the bitrate for the CAN channel. The rate is in bits per
WiredHome 0:7b81b19d9b10 196 /// second. The actual bitrate and the sample point is automagically
WiredHome 0:7b81b19d9b10 197 /// determined from the internal algorithms provided by the mbed library.
WiredHome 0:7b81b19d9b10 198 ///
WiredHome 0:7b81b19d9b10 199 /// This API appears redundant to the frequency api, however this one
WiredHome 0:7b81b19d9b10 200 /// will retain the rate setting and then permits the query of the bitrate.
WiredHome 0:7b81b19d9b10 201 ///
WiredHome 0:7b81b19d9b10 202 /// @param rate is the desired bitrate in bits per second.
WiredHome 3:4c780c641125 203 /// @returns true if the command succeeded
WiredHome 0:7b81b19d9b10 204 ///
WiredHome 3:4c780c641125 205 bool SetBitRate(uint32_t rate);
WiredHome 0:7b81b19d9b10 206
WiredHome 0:7b81b19d9b10 207 /// This returns the current desired bitrate for the CAN channel
WiredHome 0:7b81b19d9b10 208 ///
WiredHome 0:7b81b19d9b10 209 /// This returns the previously set bitrate. Note that the actual bitrate
WiredHome 0:7b81b19d9b10 210 /// may be different, due to the internal calculations of the mbed library.
WiredHome 0:7b81b19d9b10 211 ///
WiredHome 0:7b81b19d9b10 212 /// @returns the bitrate in bits per second
WiredHome 0:7b81b19d9b10 213 ///
WiredHome 3:4c780c641125 214 uint32_t GetBitRate();
WiredHome 0:7b81b19d9b10 215
WiredHome 0:7b81b19d9b10 216 /// This returns the number of messages that were sent by this CAN channel
WiredHome 0:7b81b19d9b10 217 ///
WiredHome 0:7b81b19d9b10 218 /// The counter is never reset.
WiredHome 0:7b81b19d9b10 219 ///
WiredHome 0:7b81b19d9b10 220 /// @returns the number of messages sent by this CAN channel
WiredHome 0:7b81b19d9b10 221 ///
WiredHome 0:7b81b19d9b10 222 int GetTxCounter();
WiredHome 0:7b81b19d9b10 223
WiredHome 0:7b81b19d9b10 224 /// This returns the number of messages that were received by this CAN channel
WiredHome 0:7b81b19d9b10 225 ///
WiredHome 0:7b81b19d9b10 226 /// The counter is never reset.
WiredHome 0:7b81b19d9b10 227 ///
WiredHome 0:7b81b19d9b10 228 /// @returns the number of messages received by this CAN channel
WiredHome 0:7b81b19d9b10 229 ///
WiredHome 0:7b81b19d9b10 230 int GetRxCounter();
WiredHome 0:7b81b19d9b10 231
WiredHome 0:7b81b19d9b10 232 /// This returns the number of transmit errors
WiredHome 0:7b81b19d9b10 233 ///
WiredHome 0:7b81b19d9b10 234 /// As counted by the hardware
WiredHome 0:7b81b19d9b10 235 ///
WiredHome 0:7b81b19d9b10 236 /// @returns the number of transmit errors
WiredHome 0:7b81b19d9b10 237 int GetTxErrorCounter();
WiredHome 0:7b81b19d9b10 238
WiredHome 0:7b81b19d9b10 239 /// This returns the number of receive errors
WiredHome 0:7b81b19d9b10 240 ///
WiredHome 0:7b81b19d9b10 241 /// As counted by the hardware
WiredHome 0:7b81b19d9b10 242 ///
WiredHome 0:7b81b19d9b10 243 /// @returns the number of receive errors
WiredHome 0:7b81b19d9b10 244 int GetRxErrorCounter();
WiredHome 0:7b81b19d9b10 245
WiredHome 0:7b81b19d9b10 246 /// This resets the CAN interface
WiredHome 0:7b81b19d9b10 247 ///
WiredHome 0:7b81b19d9b10 248 /// Unconditionally.
WiredHome 0:7b81b19d9b10 249 ///
WiredHome 0:7b81b19d9b10 250 /// @returns true if success
WiredHome 0:7b81b19d9b10 251 ///
WiredHome 0:7b81b19d9b10 252 bool ResetChip();
WiredHome 0:7b81b19d9b10 253
WiredHome 0:7b81b19d9b10 254 /// This writes various CAN info to the selected serial channel
WiredHome 0:7b81b19d9b10 255 ///
WiredHome 0:7b81b19d9b10 256 /// When supplied with a Serial port channel, this will output
WiredHome 0:7b81b19d9b10 257 /// some possibly interesting information about the CAN configuration.
WiredHome 0:7b81b19d9b10 258 /// It does not output details for only the active objects channel,
WiredHome 0:7b81b19d9b10 259 /// rather it outputs information about the CAN subsystem.
WiredHome 0:7b81b19d9b10 260 ///
WiredHome 0:7b81b19d9b10 261 /// @param stream is the handle of a serial channel
WiredHome 0:7b81b19d9b10 262 /// @returns nothing
WiredHome 0:7b81b19d9b10 263 ///
WiredHome 0:7b81b19d9b10 264 void PrintInfo(Serial * stream);
WiredHome 0:7b81b19d9b10 265
WiredHome 0:7b81b19d9b10 266 private:
WiredHome 0:7b81b19d9b10 267 CANCHANNEL_T channel; // user assigned port number of this port
WiredHome 3:4c780c641125 268 //CAN * can; // bind to a specific CAN
WiredHome 0:7b81b19d9b10 269 CANBusMode_T busMode; // monitor or active mode
WiredHome 0:7b81b19d9b10 270 int bitRate; // bit rate for this bus
WiredHome 0:7b81b19d9b10 271 PwmOut * activityPin; // LED to indicate activity
WiredHome 0:7b81b19d9b10 272 Timeout activityTimeout; // used to extinguish the LED
WiredHome 0:7b81b19d9b10 273
WiredHome 0:7b81b19d9b10 274 DigitalInOut * slopePin; // pin used to control the transceiver slope modes
WiredHome 0:7b81b19d9b10 275 PinName slopePinName;
WiredHome 0:7b81b19d9b10 276 CANSlopeControl_T slopeMode;// current mode
WiredHome 0:7b81b19d9b10 277
WiredHome 0:7b81b19d9b10 278 bool autoReset; // true when auto reset on error is enabled
WiredHome 0:7b81b19d9b10 279 uint32_t txCounter;
WiredHome 0:7b81b19d9b10 280 uint32_t rxCounter;
WiredHome 0:7b81b19d9b10 281 };
WiredHome 0:7b81b19d9b10 282
WiredHome 0:7b81b19d9b10 283 #endif // CANPORT_H