library for C++ CANOpen implementation. mbed independant, but is easy to attach into with mbed.

Dependents:   ppCANOpen_Example DISCO-F746NG_rtos_test

Example:

Import programppCANOpen_Example

I am no longer actively working on the ppCANOpen library, however, I want to publish this project so that anyone who wants to pick up any of the pieces can have a good example. This is a a project I was working on using the ppCANOpen library. It has a pretty in deep use of the object dictionary structure. And a number of functions to control high voltage pinball drivers, if you're into that sort of thing.

Committer:
ptpaterson
Date:
Sat Feb 13 20:22:59 2016 +0000
Revision:
5:22a337cdc0e3
Parent:
4:2034b04c86d2
PDO receive complete

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptpaterson 2:c724ff3a4e4d 1 /**
ptpaterson 2:c724ff3a4e4d 2 ******************************************************************************
ptpaterson 2:c724ff3a4e4d 3 * @file
ptpaterson 2:c724ff3a4e4d 4 * @author Paul Paterson
ptpaterson 2:c724ff3a4e4d 5 * @version
ptpaterson 2:c724ff3a4e4d 6 * @date 2015-12-14
ptpaterson 2:c724ff3a4e4d 7 * @brief CANOpen implementation library
ptpaterson 2:c724ff3a4e4d 8 ******************************************************************************
ptpaterson 2:c724ff3a4e4d 9 * @attention
ptpaterson 2:c724ff3a4e4d 10 *
ptpaterson 2:c724ff3a4e4d 11 * <h2><center>&copy; COPYRIGHT(c) 2015 Paul Paterson
ptpaterson 2:c724ff3a4e4d 12 *
ptpaterson 2:c724ff3a4e4d 13 * All rights reserved.
ptpaterson 2:c724ff3a4e4d 14
ptpaterson 2:c724ff3a4e4d 15 This program is free software: you can redistribute it and/or modify
ptpaterson 2:c724ff3a4e4d 16 it under the terms of the GNU General Public License as published by
ptpaterson 2:c724ff3a4e4d 17 the Free Software Foundation, either version 3 of the License, or
ptpaterson 2:c724ff3a4e4d 18 (at your option) any later version.
ptpaterson 2:c724ff3a4e4d 19
ptpaterson 2:c724ff3a4e4d 20 This program is distributed in the hope that it will be useful,
ptpaterson 2:c724ff3a4e4d 21 but WITHOUT ANY WARRANTY; without even the implied warranty of
ptpaterson 2:c724ff3a4e4d 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ptpaterson 2:c724ff3a4e4d 23 GNU General Public License for more details.
ptpaterson 2:c724ff3a4e4d 24
ptpaterson 2:c724ff3a4e4d 25 You should have received a copy of the GNU General Public License
ptpaterson 2:c724ff3a4e4d 26 along with this program. If not, see <http://www.gnu.org/licenses/>.
ptpaterson 2:c724ff3a4e4d 27 */
ptpaterson 2:c724ff3a4e4d 28
ptpaterson 2:c724ff3a4e4d 29 #ifndef PPCAN_NODE_H
ptpaterson 2:c724ff3a4e4d 30 #define PPCAN_NODE_H
ptpaterson 2:c724ff3a4e4d 31
ptpaterson 5:22a337cdc0e3 32 #include "ObjectDictionary.h"
ptpaterson 4:2034b04c86d2 33
ptpaterson 4:2034b04c86d2 34 /*=========================================================================
ptpaterson 4:2034b04c86d2 35 * Forward declarations
ptpaterson 4:2034b04c86d2 36 *=========================================================================
ptpaterson 4:2034b04c86d2 37 */
ptpaterson 4:2034b04c86d2 38 struct CanOpenMessage;
ptpaterson 5:22a337cdc0e3 39 //struct ObjectData;
ptpaterson 4:2034b04c86d2 40
ptpaterson 4:2034b04c86d2 41 /* NMT Constants --------------------------------------------------------*/
ptpaterson 4:2034b04c86d2 42 #define NMT_STATE_TOGGLE_BIT 0x80
ptpaterson 4:2034b04c86d2 43
ptpaterson 4:2034b04c86d2 44 /* NMT Macros -----------------------------------------------------------*/
ptpaterson 4:2034b04c86d2 45 #define NMT_SET_STATE(curState, newState) (curState) = (((curState) & NMT_STATE_TOGGLE_BIT) | ((newState) & (~NMT_STATE_TOGGLE_BIT))))
ptpaterson 2:c724ff3a4e4d 46
ptpaterson 2:c724ff3a4e4d 47 namespace ppCANOpen
ptpaterson 2:c724ff3a4e4d 48 {
ptpaterson 2:c724ff3a4e4d 49
ptpaterson 2:c724ff3a4e4d 50 /* Avoid circular reference */
ptpaterson 2:c724ff3a4e4d 51 class ServiceProvider;
ptpaterson 4:2034b04c86d2 52
ptpaterson 4:2034b04c86d2 53
ptpaterson 2:c724ff3a4e4d 54 /** Node Class to implement feature of a CANOpen NMT node
ptpaterson 2:c724ff3a4e4d 55 */
ptpaterson 2:c724ff3a4e4d 56 class Node
ptpaterson 2:c724ff3a4e4d 57 {
ptpaterson 2:c724ff3a4e4d 58 public:
ptpaterson 3:12b3c25bdeba 59
ptpaterson 5:22a337cdc0e3 60 /* ========================================================================
ptpaterson 5:22a337cdc0e3 61 * Internal structures and constants
ptpaterson 5:22a337cdc0e3 62 * ========================================================================
ptpaterson 5:22a337cdc0e3 63 */
ptpaterson 5:22a337cdc0e3 64
ptpaterson 4:2034b04c86d2 65 /** Node network management state */
ptpaterson 4:2034b04c86d2 66 struct State {
ptpaterson 4:2034b04c86d2 67 static const int INITIALIZED = 0x00;
ptpaterson 4:2034b04c86d2 68 static const int DISCONNECTED = 0x01;
ptpaterson 4:2034b04c86d2 69 static const int CONNECTING = 0x02;
ptpaterson 4:2034b04c86d2 70 static const int PREPARING = 0x02;
ptpaterson 4:2034b04c86d2 71 static const int STOPPED = 0x04;
ptpaterson 4:2034b04c86d2 72 static const int OPERATIONAL = 0x05;
ptpaterson 4:2034b04c86d2 73 static const int PREOPERATIONAL = 0x7F;
ptpaterson 4:2034b04c86d2 74 static const int UNKNOWN = 0x0F;
ptpaterson 4:2034b04c86d2 75
ptpaterson 4:2034b04c86d2 76 int nmtState;
ptpaterson 3:12b3c25bdeba 77 int bBoot;
ptpaterson 3:12b3c25bdeba 78 int bSDO;
ptpaterson 3:12b3c25bdeba 79 int bEmergency;
ptpaterson 3:12b3c25bdeba 80 int bSYNC;
ptpaterson 3:12b3c25bdeba 81 int bLifeGuard;
ptpaterson 3:12b3c25bdeba 82 int bPDO;
ptpaterson 3:12b3c25bdeba 83 int bLSS;
ptpaterson 4:2034b04c86d2 84
ptpaterson 4:2034b04c86d2 85 int bLifeGuardToggle;
ptpaterson 4:2034b04c86d2 86 };
ptpaterson 4:2034b04c86d2 87
ptpaterson 5:22a337cdc0e3 88 /* ========================================================================
ptpaterson 5:22a337cdc0e3 89 * Construction
ptpaterson 5:22a337cdc0e3 90 * ========================================================================
ptpaterson 5:22a337cdc0e3 91 */
ptpaterson 5:22a337cdc0e3 92
ptpaterson 4:2034b04c86d2 93 /** Maintain the multitude of states for a node */
ptpaterson 5:22a337cdc0e3 94 Node (int id, ServiceProvider * provider, int bLoop = 0);
ptpaterson 3:12b3c25bdeba 95
ptpaterson 3:12b3c25bdeba 96 /* ========================================================================
ptpaterson 5:22a337cdc0e3 97 * Public Methods
ptpaterson 3:12b3c25bdeba 98 * ========================================================================
ptpaterson 3:12b3c25bdeba 99 */
ptpaterson 3:12b3c25bdeba 100
ptpaterson 5:22a337cdc0e3 101 /** Call from ServiceProvider on an interrupt */
ptpaterson 5:22a337cdc0e3 102 void FixedUpdate (uint32_t time);
ptpaterson 5:22a337cdc0e3 103
ptpaterson 5:22a337cdc0e3 104 /** Call from ServiceProvider every loop of Run() */
ptpaterson 5:22a337cdc0e3 105 void Update (void);
ptpaterson 2:c724ff3a4e4d 106
ptpaterson 5:22a337cdc0e3 107 /** Handle message given by the ServiceProvider*/
ptpaterson 5:22a337cdc0e3 108 int DispatchMessage(CanOpenMessage *canOpenMsg);
ptpaterson 5:22a337cdc0e3 109
ptpaterson 2:c724ff3a4e4d 110 /* ========================================================================
ptpaterson 5:22a337cdc0e3 111 * Public Member Variables
ptpaterson 3:12b3c25bdeba 112 * ========================================================================
ptpaterson 3:12b3c25bdeba 113 */
ptpaterson 3:12b3c25bdeba 114
ptpaterson 5:22a337cdc0e3 115 /* Common Node properties -----------------------------------------------*/
ptpaterson 5:22a337cdc0e3 116 /** Network id for the node
ptpaterson 5:22a337cdc0e3 117 * @note Ref is held in object dictionary and upon setting, may need to
ptpaterson 5:22a337cdc0e3 118 * update some other objects.
ptpaterson 3:12b3c25bdeba 119 */
ptpaterson 5:22a337cdc0e3 120 int nodeId;
ptpaterson 3:12b3c25bdeba 121
ptpaterson 5:22a337cdc0e3 122 /** Loopback Mode. Default off (0), if on (1) then will hear it's own
ptpaterson 5:22a337cdc0e3 123 * messages
ptpaterson 5:22a337cdc0e3 124 */
ptpaterson 5:22a337cdc0e3 125 int bLoopbackOn;
ptpaterson 3:12b3c25bdeba 126
ptpaterson 3:12b3c25bdeba 127 protected:
ptpaterson 3:12b3c25bdeba 128
ptpaterson 3:12b3c25bdeba 129 /* ========================================================================
ptpaterson 5:22a337cdc0e3 130 * Protected? Methods to handle various messages
ptpaterson 5:22a337cdc0e3 131 * Used to deal with everything from inside of Dispatch Message
ptpaterson 2:c724ff3a4e4d 132 * ========================================================================
ptpaterson 2:c724ff3a4e4d 133 */
ptpaterson 2:c724ff3a4e4d 134
ptpaterson 2:c724ff3a4e4d 135 /* PDO (7.2.2), MPDO (7.2.3) --------------------------------------------*/
ptpaterson 2:c724ff3a4e4d 136
ptpaterson 5:22a337cdc0e3 137 int HandlePdo (CanOpenMessage *canOpenMsg);
ptpaterson 2:c724ff3a4e4d 138
ptpaterson 2:c724ff3a4e4d 139
ptpaterson 2:c724ff3a4e4d 140 /* SDO (7.2.4) ----------------------------------------------------------*/
ptpaterson 5:22a337cdc0e3 141 int HandleSdo (CanOpenMessage *canOpenMsg);
ptpaterson 2:c724ff3a4e4d 142
ptpaterson 2:c724ff3a4e4d 143 int HandleExpeditedDownload (int sdoNum, int index, int subIndex, int dataCount, char * data);
ptpaterson 2:c724ff3a4e4d 144 int HandleInitiateDownloadRequest (int sdoNum, int index, int subIndex, int dataCount, char * data);
ptpaterson 2:c724ff3a4e4d 145
ptpaterson 2:c724ff3a4e4d 146 int HandleExpeditedUpload (int sdoNum, int index, int subIndex);
ptpaterson 2:c724ff3a4e4d 147
ptpaterson 2:c724ff3a4e4d 148
ptpaterson 2:c724ff3a4e4d 149 // TODO: express and not express
ptpaterson 2:c724ff3a4e4d 150
ptpaterson 2:c724ff3a4e4d 151 /* SYNC object (7.2.5) --------------------------------------------------*/
ptpaterson 5:22a337cdc0e3 152 int HandleSync (CanOpenMessage *canOpenMsg);
ptpaterson 2:c724ff3a4e4d 153
ptpaterson 2:c724ff3a4e4d 154 /* Time Stamp object (7.2.6) --------------------------------------------*/
ptpaterson 5:22a337cdc0e3 155 /* Handled all inside of the ServiceProvider */
ptpaterson 2:c724ff3a4e4d 156
ptpaterson 2:c724ff3a4e4d 157 /* Emergency object (7.2.7) ---------------------------------------------*/
ptpaterson 2:c724ff3a4e4d 158
ptpaterson 2:c724ff3a4e4d 159 int ConsumeEmergency (void);// TODO: fix params
ptpaterson 2:c724ff3a4e4d 160
ptpaterson 2:c724ff3a4e4d 161
ptpaterson 2:c724ff3a4e4d 162 /* Network Management (7.2.8) -------------------------------------------*/
ptpaterson 2:c724ff3a4e4d 163 /* ---- Node Control (7.2.8.2.1) ----------------------------------------*/
ptpaterson 5:22a337cdc0e3 164 int HandleNodeControl (CanOpenMessage *canOpenMsg);
ptpaterson 2:c724ff3a4e4d 165
ptpaterson 2:c724ff3a4e4d 166
ptpaterson 2:c724ff3a4e4d 167 /* ---- Error Control (7.2.8.2.2) ---------------------------------------*/
ptpaterson 2:c724ff3a4e4d 168
ptpaterson 2:c724ff3a4e4d 169 int HandleNodeGuardRequest (int masterId);
ptpaterson 2:c724ff3a4e4d 170 int ConsumeHeartbeat (int producerId);
ptpaterson 2:c724ff3a4e4d 171
ptpaterson 5:22a337cdc0e3 172 protected:
ptpaterson 5:22a337cdc0e3 173
ptpaterson 2:c724ff3a4e4d 174 /* ========================================================================
ptpaterson 5:22a337cdc0e3 175 * Protected Methods to implement node application in derived classes
ptpaterson 2:c724ff3a4e4d 176 * ========================================================================
ptpaterson 2:c724ff3a4e4d 177 */
ptpaterson 3:12b3c25bdeba 178
ptpaterson 5:22a337cdc0e3 179 /** Perform actions every cycle (when in operational mode)
ptpaterson 5:22a337cdc0e3 180 * @note Override to implement user application
ptpaterson 5:22a337cdc0e3 181 * @note The OnTick function should avoid modifying data in the object
ptpaterson 5:22a337cdc0e3 182 * dictionary unless necessary, because there will almost certainly be
ptpaterson 5:22a337cdc0e3 183 * a race condition.
ptpaterson 5:22a337cdc0e3 184 */
ptpaterson 5:22a337cdc0e3 185 virtual void OnFixedUpdate(void) = 0;
ptpaterson 5:22a337cdc0e3 186
ptpaterson 5:22a337cdc0e3 187
ptpaterson 5:22a337cdc0e3 188
ptpaterson 5:22a337cdc0e3 189 virtual void OnUpdate(void) = 0;
ptpaterson 5:22a337cdc0e3 190
ptpaterson 5:22a337cdc0e3 191
ptpaterson 5:22a337cdc0e3 192 /* SYNC -----------------------------------------------------------------*/
ptpaterson 5:22a337cdc0e3 193 /** Perform actions when state changed to stop
ptpaterson 3:12b3c25bdeba 194 * @note Override to implement user application
ptpaterson 3:12b3c25bdeba 195 */
ptpaterson 5:22a337cdc0e3 196 virtual void OnSync (uint8_t counter) = 0;
ptpaterson 2:c724ff3a4e4d 197
ptpaterson 5:22a337cdc0e3 198
ptpaterson 5:22a337cdc0e3 199 /* NMT Node Control -----------------------------------------------------*/
ptpaterson 3:12b3c25bdeba 200 /** Perform actions when node reset
ptpaterson 3:12b3c25bdeba 201 * @note Override to implement user application
ptpaterson 3:12b3c25bdeba 202 */
ptpaterson 3:12b3c25bdeba 203 virtual void OnInitialize (void) = 0;
ptpaterson 3:12b3c25bdeba 204
ptpaterson 3:12b3c25bdeba 205 /** Perform actions when state changed to pre-operational
ptpaterson 3:12b3c25bdeba 206 * @note Override to implement user application
ptpaterson 3:12b3c25bdeba 207 */
ptpaterson 3:12b3c25bdeba 208 virtual void OnPreoperational (void) = 0;
ptpaterson 2:c724ff3a4e4d 209
ptpaterson 3:12b3c25bdeba 210 /** Perform actions when state changed to operational
ptpaterson 3:12b3c25bdeba 211 * @note Override to implement user application
ptpaterson 3:12b3c25bdeba 212 */
ptpaterson 3:12b3c25bdeba 213 virtual void OnOperational (void) = 0;
ptpaterson 3:12b3c25bdeba 214
ptpaterson 3:12b3c25bdeba 215 /** Perform actions when state changed to stop
ptpaterson 3:12b3c25bdeba 216 * @note Override to implement user application
ptpaterson 3:12b3c25bdeba 217 */
ptpaterson 3:12b3c25bdeba 218 virtual void OnStopped (void) = 0;
ptpaterson 2:c724ff3a4e4d 219
ptpaterson 5:22a337cdc0e3 220 /* Object Dictionary Handling -------------------------------------------*/
ptpaterson 5:22a337cdc0e3 221 /** Abstract method to give access to the object entries of derived
ptpaterson 5:22a337cdc0e3 222 * classes
ptpaterson 5:22a337cdc0e3 223 */
ptpaterson 5:22a337cdc0e3 224 virtual ObjectData * ScanIndex(IndexSize index) = 0;
ptpaterson 5:22a337cdc0e3 225
ptpaterson 5:22a337cdc0e3 226
ptpaterson 2:c724ff3a4e4d 227 /* ========================================================================
ptpaterson 5:22a337cdc0e3 228 * Protected Methods to Post Messages
ptpaterson 3:12b3c25bdeba 229 * ========================================================================
ptpaterson 3:12b3c25bdeba 230 */
ptpaterson 5:22a337cdc0e3 231
ptpaterson 5:22a337cdc0e3 232 int PostTPDO (int cobId);
ptpaterson 3:12b3c25bdeba 233
ptpaterson 3:12b3c25bdeba 234
ptpaterson 5:22a337cdc0e3 235 /* ========================================================================
ptpaterson 5:22a337cdc0e3 236 * Protected Member Variables
ptpaterson 5:22a337cdc0e3 237 * ========================================================================
ptpaterson 5:22a337cdc0e3 238 */
ptpaterson 5:22a337cdc0e3 239
ptpaterson 5:22a337cdc0e3 240 /* Object Dictionary Handling -------------------------------------------*/
ptpaterson 5:22a337cdc0e3 241 /** Array of ObjectData to contain all of the object dictionary data */
ptpaterson 5:22a337cdc0e3 242 ObjectData *dictionary;
ptpaterson 5:22a337cdc0e3 243
ptpaterson 5:22a337cdc0e3 244 /* Elapsed Time ---------------------------------------------------------*/
ptpaterson 5:22a337cdc0e3 245 /** Millisecond time stamp at this OnTick() */
ptpaterson 5:22a337cdc0e3 246 uint32_t timeCurrentTick;
ptpaterson 5:22a337cdc0e3 247
ptpaterson 5:22a337cdc0e3 248 /** Millisecond difference between current OnTick() and the previous */
ptpaterson 5:22a337cdc0e3 249 uint32_t timeSinceLastTick;
ptpaterson 5:22a337cdc0e3 250
ptpaterson 5:22a337cdc0e3 251 // should be private
ptpaterson 5:22a337cdc0e3 252 // TODO: implement functions in Node class to post all types of messages so
ptpaterson 5:22a337cdc0e3 253 // derived classes do not have to consider message structure.
ptpaterson 5:22a337cdc0e3 254 protected:
ptpaterson 3:12b3c25bdeba 255
ptpaterson 3:12b3c25bdeba 256 /* ========================================================================
ptpaterson 3:12b3c25bdeba 257 * Private members
ptpaterson 2:c724ff3a4e4d 258 * ========================================================================
ptpaterson 2:c724ff3a4e4d 259 */
ptpaterson 2:c724ff3a4e4d 260
ptpaterson 5:22a337cdc0e3 261 /* ServiceProvider ------------------------------------------------------*/
ptpaterson 5:22a337cdc0e3 262 /** Reference to Service Provider that this node is attached to */
ptpaterson 4:2034b04c86d2 263 ServiceProvider * pMyProvider;
ptpaterson 5:22a337cdc0e3 264
ptpaterson 5:22a337cdc0e3 265 private:
ptpaterson 5:22a337cdc0e3 266 /* Common Node Methods --------------------------------------------------*/
ptpaterson 5:22a337cdc0e3 267 /** Change state of the node */
ptpaterson 5:22a337cdc0e3 268 void ChangeState(int newState);
ptpaterson 2:c724ff3a4e4d 269
ptpaterson 5:22a337cdc0e3 270 /* Common Node properties -----------------------------------------------*/
ptpaterson 5:22a337cdc0e3 271 /** Network and communication state of the node */
ptpaterson 5:22a337cdc0e3 272 State state;
ptpaterson 5:22a337cdc0e3 273
ptpaterson 5:22a337cdc0e3 274
ptpaterson 2:c724ff3a4e4d 275 };
ptpaterson 2:c724ff3a4e4d 276
ptpaterson 2:c724ff3a4e4d 277 } /* namespace ppCANOpen */
ptpaterson 2:c724ff3a4e4d 278
ptpaterson 2:c724ff3a4e4d 279 #endif // PPCAN_NODE_H
ptpaterson 2:c724ff3a4e4d 280