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:
Sun Jul 15 15:15:19 2012 +0000
Revision:
0:7b81b19d9b10
Child:
1:f0b4e47d948d
[mbed] converted /A_CANAdapter/CANUtilities/CANPort

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