CANMessage is the primitive CAN message object. It supports creation, parsing, formatting of messages. Can be easily integrated with CANPort and CANQueue libraries.

Committer:
WiredHome
Date:
Wed Oct 23 21:38:12 2019 +0000
Revision:
4:4ba6856f7b4f
Parent:
3:c2a26d9d689b
Changed a Timer API to the old read_us() and typecast it to uint32_t. I lose the 64-bit resolution, but gain compatibility with an older version of the OS that supports the flash file system.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:f0ce97992c5f 1 /// @file CANMessage.h
WiredHome 0:f0ce97992c5f 2 /// CANMessage is the primitive CAN message object. It supports creation,
WiredHome 0:f0ce97992c5f 3 /// parsing, formatting, of messages.
WiredHome 0:f0ce97992c5f 4 ///
WiredHome 0:f0ce97992c5f 5 /// @version 1.0
WiredHome 0:f0ce97992c5f 6 ///
WiredHome 0:f0ce97992c5f 7 /// @note Copyright &copr; 2011 by Smartware Computing, all rights reserved.
WiredHome 0:f0ce97992c5f 8 /// Individuals may use this application for evaluation or non-commercial
WiredHome 0:f0ce97992c5f 9 /// purposes. Within this restriction, changes may be made to this application
WiredHome 0:f0ce97992c5f 10 /// as long as this copyright notice is retained. The user shall make
WiredHome 0:f0ce97992c5f 11 /// clear that their work is a derived work, and not the original.
WiredHome 0:f0ce97992c5f 12 /// Users of this application and sources accept this application "as is" and
WiredHome 0:f0ce97992c5f 13 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 0:f0ce97992c5f 14 /// using this application - whether real or imagined.
WiredHome 0:f0ce97992c5f 15 ///
WiredHome 0:f0ce97992c5f 16 /// @author David Smart, Smartware Computing
WiredHome 0:f0ce97992c5f 17 ///
WiredHome 0:f0ce97992c5f 18
WiredHome 0:f0ce97992c5f 19 #ifndef CANMESSAGE_H
WiredHome 0:f0ce97992c5f 20 #define CANMESSAGE_H
WiredHome 0:f0ce97992c5f 21 #include "mbed.h"
WiredHome 0:f0ce97992c5f 22
WiredHome 0:f0ce97992c5f 23 /// CAN direction, in the set {rcv, xmt}
WiredHome 0:f0ce97992c5f 24 typedef enum {
WiredHome 0:f0ce97992c5f 25 rcv,
WiredHome 0:f0ce97992c5f 26 xmt
WiredHome 0:f0ce97992c5f 27 } CANDIR_T;
WiredHome 0:f0ce97992c5f 28
WiredHome 0:f0ce97992c5f 29
WiredHome 0:f0ce97992c5f 30 /// CAN Channel in the set {CH1, CH2} of CANCHANNELS
WiredHome 0:f0ce97992c5f 31 typedef enum {
WiredHome 0:f0ce97992c5f 32 CH1,
WiredHome 0:f0ce97992c5f 33 CH2,
WiredHome 0:f0ce97992c5f 34 CANCHANNELS
WiredHome 0:f0ce97992c5f 35 } CANCHANNEL_T;
WiredHome 0:f0ce97992c5f 36
WiredHome 0:f0ce97992c5f 37
WiredHome 0:f0ce97992c5f 38 // CAN mode - active(acknowledge) or passive(monitor)
WiredHome 0:f0ce97992c5f 39 //typedef enum {
WiredHome 0:f0ce97992c5f 40 // ACTIVE,
WiredHome 0:f0ce97992c5f 41 // PASSIVE
WiredHome 0:f0ce97992c5f 42 //} CANMODE_T;
WiredHome 0:f0ce97992c5f 43
WiredHome 0:f0ce97992c5f 44
WiredHome 0:f0ce97992c5f 45 /// A super CAN message object, which has additional capabilities
WiredHome 0:f0ce97992c5f 46 ///
WiredHome 0:f0ce97992c5f 47 /// This object is derived from CANMessage, however it adds
WiredHome 0:f0ce97992c5f 48 /// a timestamp and other useful methods.
WiredHome 0:f0ce97992c5f 49 ///
WiredHome 3:c2a26d9d689b 50 /// the CANMessage format element by default is:
WiredHome 3:c2a26d9d689b 51 /// - CANStandard = 0,
WiredHome 3:c2a26d9d689b 52 /// - CANExtended = 1,
WiredHome 3:c2a26d9d689b 53 /// - CANAny = 2
WiredHome 3:c2a26d9d689b 54 /// But for certain purposes it will be convenient to use the CANmsg object,
WiredHome 3:c2a26d9d689b 55 /// which inherits CANMessage, and overload this item with additional values.
WiredHome 3:c2a26d9d689b 56 /// - Error = 128
WiredHome 3:c2a26d9d689b 57 ///
WiredHome 0:f0ce97992c5f 58 class CANmsg : public CANMessage
WiredHome 0:f0ce97992c5f 59 {
WiredHome 0:f0ce97992c5f 60 public:
WiredHome 0:f0ce97992c5f 61 /// Constructor for a CANmsg object, which is a superclass of CANMessage
WiredHome 0:f0ce97992c5f 62 ///
WiredHome 0:f0ce97992c5f 63 /// The CANmsg object included additional information, including
WiredHome 0:f0ce97992c5f 64 /// direction, channel number, timestamp, and the ability to format
WiredHome 0:f0ce97992c5f 65 /// into ASCII
WiredHome 0:f0ce97992c5f 66 ///
WiredHome 0:f0ce97992c5f 67 CANmsg();
WiredHome 0:f0ce97992c5f 68
WiredHome 0:f0ce97992c5f 69 /// Constructor for a CANmsg object, which is a superclass of CANMessage
WiredHome 0:f0ce97992c5f 70 ///
WiredHome 0:f0ce97992c5f 71 /// The CANmsg object included additional information, including
WiredHome 0:f0ce97992c5f 72 /// direction, channel number, timestamp, and the ability to format
WiredHome 0:f0ce97992c5f 73 /// into ASCII.
WiredHome 0:f0ce97992c5f 74 /// @param ch is the channel number (CH1, CH2)
WiredHome 0:f0ce97992c5f 75 /// @param dir is the direction (xmt, rcv)
WiredHome 0:f0ce97992c5f 76 /// @param msg is a CANMessage object from which to construct this object
WiredHome 0:f0ce97992c5f 77 ///
WiredHome 0:f0ce97992c5f 78 CANmsg(CANCHANNEL_T ch, CANDIR_T dir, CANMessage msg);
WiredHome 0:f0ce97992c5f 79
WiredHome 0:f0ce97992c5f 80 /// Constructor for a CANmsg object, which is a superclass of CANMessage
WiredHome 0:f0ce97992c5f 81 ///
WiredHome 0:f0ce97992c5f 82 /// The CANmsg object included additional information, including
WiredHome 0:f0ce97992c5f 83 /// direction, channel number, timestamp, and the ability to format
WiredHome 0:f0ce97992c5f 84 /// into ASCII.
WiredHome 0:f0ce97992c5f 85 /// This constructor creates a message from an ascii buffer with
WiredHome 0:f0ce97992c5f 86 /// the standard message format in it.
WiredHome 0:f0ce97992c5f 87 ///
WiredHome 0:f0ce97992c5f 88 /// @todo Improve the parser, it may not handle malformed input well
WiredHome 0:f0ce97992c5f 89 ///
WiredHome 0:f0ce97992c5f 90 /// @verbatim
WiredHome 0:f0ce97992c5f 91 /// t xtd 02 1CF00400 08 11 22 33 44 55 66 77 88 0 0 1234.567890
WiredHome 0:f0ce97992c5f 92 /// If the first letter is not 't' or 'r', transmit is assumed
WiredHome 0:f0ce97992c5f 93 /// xtd 02 1CF00400 08 11 22 33 44 55 66 77 88 0 0 1234.567890
WiredHome 0:f0ce97992c5f 94 /// @endverbatim
WiredHome 0:f0ce97992c5f 95 ///
WiredHome 0:f0ce97992c5f 96 /// @param p is a pointer to a text string of the message contents,
WiredHome 0:f0ce97992c5f 97 /// in the same format as what would be output with the
WiredHome 0:f0ce97992c5f 98 /// FormatCANMessage method.
WiredHome 0:f0ce97992c5f 99 ///
WiredHome 0:f0ce97992c5f 100 CANmsg(char *p);
WiredHome 0:f0ce97992c5f 101
WiredHome 0:f0ce97992c5f 102 /// Destructor for the CANmsg object
WiredHome 0:f0ce97992c5f 103 ~CANmsg();
WiredHome 0:f0ce97992c5f 104
WiredHome 0:f0ce97992c5f 105 /// Parse a text string into the CANmsg object, which is a superclass of CANMessage
WiredHome 0:f0ce97992c5f 106 ///
WiredHome 0:f0ce97992c5f 107 /// The CANmsg object included additional information, including
WiredHome 0:f0ce97992c5f 108 /// direction, channel number, timestamp, and the ability to format
WiredHome 0:f0ce97992c5f 109 /// into ASCII.
WiredHome 0:f0ce97992c5f 110 /// This constructor creates a message from an ascii buffer with
WiredHome 0:f0ce97992c5f 111 /// the standard message format in it.
WiredHome 0:f0ce97992c5f 112 ///
WiredHome 0:f0ce97992c5f 113 /// @todo Improve the parser, it may not handle malformed input well
WiredHome 0:f0ce97992c5f 114 ///
WiredHome 0:f0ce97992c5f 115 /// @verbatim
WiredHome 0:f0ce97992c5f 116 /// t xtd 02 1CF00400 08 11 22 33 44 55 66 77 88 0 0 1234.567890
WiredHome 0:f0ce97992c5f 117 /// If the first letter is not 't' or 'r', transmit is assumed
WiredHome 0:f0ce97992c5f 118 /// xtd 02 1CF00400 08 11 22 33 44 55 66 77 88 0 0 1234.567890
WiredHome 0:f0ce97992c5f 119 /// @endverbatim
WiredHome 0:f0ce97992c5f 120 ///
WiredHome 0:f0ce97992c5f 121 /// @param p is a pointer to a text string of the message contents,
WiredHome 0:f0ce97992c5f 122 /// in the same format as what would be output with the
WiredHome 0:f0ce97992c5f 123 /// FormatCANMessage method.
WiredHome 0:f0ce97992c5f 124 /// @returns true
WiredHome 0:f0ce97992c5f 125 ///
WiredHome 0:f0ce97992c5f 126 bool ParseCANMessage(char *p);
WiredHome 0:f0ce97992c5f 127
WiredHome 0:f0ce97992c5f 128 /// Formats this CAN message into a text buffer - which should be at least 70
WiredHome 0:f0ce97992c5f 129 ///
WiredHome 0:f0ce97992c5f 130 /// @todo buflen is currently ignored, which is unsafe
WiredHome 0:f0ce97992c5f 131 ///
WiredHome 0:f0ce97992c5f 132 /// @verbatim
WiredHome 0:f0ce97992c5f 133 /// t xtd 02 1CF00400 08 11 22 33 44 55 66 77 88 0 0 1234.567890
WiredHome 0:f0ce97992c5f 134 /// @endverbatim
WiredHome 0:f0ce97992c5f 135 ///
WiredHome 0:f0ce97992c5f 136 /// @param buffer is a pointer to the buffer to fill
WiredHome 0:f0ce97992c5f 137 /// @param buflen is the size of the buffer (minimum 70 recommended)
WiredHome 0:f0ce97992c5f 138 ///
WiredHome 0:f0ce97992c5f 139 /// @returns nothing
WiredHome 0:f0ce97992c5f 140 void FormatCANMessage(char *buffer, int buflen);
WiredHome 0:f0ce97992c5f 141
WiredHome 0:f0ce97992c5f 142 /// Overrides the timestamp in this message with the current time
WiredHome 0:f0ce97992c5f 143 void SetTimestamp();
WiredHome 0:f0ce97992c5f 144
WiredHome 2:4307b498b508 145 /// Gets the 64-bit timestamp of this message
WiredHome 0:f0ce97992c5f 146 /// @returns time in microseconds
WiredHome 2:4307b498b508 147 ///
WiredHome 0:f0ce97992c5f 148 uint64_t GetTimestamp() { return timestamp; };
WiredHome 0:f0ce97992c5f 149
WiredHome 0:f0ce97992c5f 150 /// direction of this CAN message - rcv or xmt
WiredHome 0:f0ce97992c5f 151 CANDIR_T dir;
WiredHome 0:f0ce97992c5f 152
WiredHome 0:f0ce97992c5f 153 /// channel number of this CAN message
WiredHome 0:f0ce97992c5f 154 CANCHANNEL_T ch;
WiredHome 0:f0ce97992c5f 155
WiredHome 0:f0ce97992c5f 156 private:
WiredHome 0:f0ce97992c5f 157 /// internally held timestamp
WiredHome 0:f0ce97992c5f 158 uint64_t timestamp;
WiredHome 0:f0ce97992c5f 159
WiredHome 0:f0ce97992c5f 160 // internally held mode
WiredHome 0:f0ce97992c5f 161 //CANMODE_T mode;
WiredHome 0:f0ce97992c5f 162 };
WiredHome 0:f0ce97992c5f 163
WiredHome 0:f0ce97992c5f 164 #endif // CANMESSAGE_H