Paul Paterson / CanPipe Featured

Dependents:   CanPipe_Example

CanPipe Library

Introduction


CanPipe is a library to make it simple to implement more complex CAN protocols, such as CANOpen. You can easily create message filters and attach callbacks associated with those filters. Callbacks for a given filter can be chained together to create complex responses to any given message.

Message Handling


The CanPipe class stores incoming and outgoing messages in buffers so that the handling of complex and timing sensitive routines and be executed outside of the interrupt context. The user must call CanPipe::HandleMessages on a regular basis to handle all incoming messages as well as actually write posted messages to the CAN bus. This call should not be made from inside of an interrupt routine.

Received Messages

When a message is received, the CanPipe class automatically adds the message to a buffer. User callbacks will be executed the next time HandleMessages is called.

Outgoing Messages

Users can use CanPipe::PostMessage to add new messages to a buffer. They will be written to the bus the next time HandleMessages is called.

Because messages are handled outside of the interrupt, it is safe to handle and post messages in response to others however you wish without threat of blocking additional messages.

Filters


To create a filter use the CanPipe::RegisterFilter method. This method gets passes an id, which is the 11- or 29-bit message id to filter for and a 32-bit mask. The mask is applied to both the incoming message ID and the filter id, which are then compared for a match. If no handle is specified, or is zero, then the next available filter handle will be used. If handle is specified as a value valid for your device, then it will override any existing filter at that handle. Filters are triggered in the order in which they are registered (In the future, it is desirable to sort software filters so that filters with the lowest handle are always triggered first). The first filter that passes is the one that is used.

An optional FilterMode can be passed during CanPipe construction.

  • CanPipe::kFilterAuto: Hardware filter will be attempted if possible, and software filter will be created (Default).
  • CanPipe::kFilterHardwareOnly: Hardware filter will be attempted, and do nothing if it fails.
  • CanPipe::kFilterSoftwareOnly: Software filter will be created.

If a hardware filter succeeds, it will prevent any unmatched messages from triggering an interrupt. A software copy of the filter is saved in order to sort messages when they are handled. Specifying kFilterHardwareOnly will turn off software filter sorting, and all callback messages should be attached to handle 0. If kFilterSoftwareOnly is specified, then all messages will be let through and cause an interrupt. The software filter will then sort the message when handled.

NOTE: Hardware filters are currently available only to LPC11Cxx (handles 1 to 31), LPC15xx (handles 1 to 31), and mbed Renesas (handle 1 only) devices.

NOTE: The filter "handle" specified here is similar to "mailbox" or "message object" used in some device user manuals.

Callbacks


Users can attach a callback of the form int(CANMessage&) to a filter in order to handle received messages. For each pending received message, during CanPipe::HandleMessages call:

  • A filter handle is obtained, either from hardware when message was received, or by executing software filters
  • A list callbacks is obtained based on the message's filter handle.
  • Each callback in the list is called in the order in which it is attached.

If a callback returns anything other than 0 (CanPipe::kOkay), the chain of callbacks will be broken. The user can use this feature to end execution in the event of an error or simply claim that the message is fully handled (i.e. returned CanPipe::kDone).

Callbacks can be attached to handle 0 in order to handle messages that failed to match a filter or in a case where no filters were created.

Known Issues


The mbed CAN api implementation for LPC15xx does not currently execute attached interrupts correctly. This library is being developed with LPC15xx hardware, so several lines of LPC15xx specific code have been added to the CanPipe::ReadIrq method to get it to work for development hardware.

Example


https://developer.mbed.org/users/ptpaterson/code/CanPipe_Example/

Example demonstrates how to create a message filter and use the returned handle to associate callback functions with messages that pass that filter.

Committer:
ptpaterson
Date:
Thu Nov 03 20:48:12 2016 +0000
Revision:
0:26f78923d093
Child:
1:ebd382c0a2b8
basic message pipe implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptpaterson 0:26f78923d093 1
ptpaterson 0:26f78923d093 2
ptpaterson 0:26f78923d093 3 #ifndef CANPIPE_H
ptpaterson 0:26f78923d093 4 #define CANPIPE_H
ptpaterson 0:26f78923d093 5
ptpaterson 0:26f78923d093 6 #include "mbed.h"
ptpaterson 0:26f78923d093 7 #include "CircularBuffer.h"
ptpaterson 0:26f78923d093 8
ptpaterson 0:26f78923d093 9 /** Function pointer type for user to attach and handle CAN messages
ptpaterson 0:26f78923d093 10 */
ptpaterson 0:26f78923d093 11 typedef int (*CanMessageCallback)(CAN *p_can_, CANMessage msg);
ptpaterson 0:26f78923d093 12
ptpaterson 0:26f78923d093 13 /** A stack on top of mbed CAN class to handle complex filtered callbacks
ptpaterson 0:26f78923d093 14 * @class
ptpaterson 0:26f78923d093 15 */
ptpaterson 0:26f78923d093 16 class CanPipe {
ptpaterson 0:26f78923d093 17 public:
ptpaterson 0:26f78923d093 18 /* Public Type definitions and forward declarations ***********************/
ptpaterson 0:26f78923d093 19 /** Enumeration for method return values */
ptpaterson 0:26f78923d093 20 enum CanPipeResult {
ptpaterson 0:26f78923d093 21 kOkay = 0,
ptpaterson 0:26f78923d093 22 kErrorCbNodeMemory,
ptpaterson 0:26f78923d093 23 kErrorCbListMemory,
ptpaterson 0:26f78923d093 24 kDone
ptpaterson 0:26f78923d093 25 };
ptpaterson 0:26f78923d093 26
ptpaterson 0:26f78923d093 27 /** Enumeration for how filters should be handled */
ptpaterson 0:26f78923d093 28 enum FilterMode {
ptpaterson 0:26f78923d093 29 kFilterAuto = 0,
ptpaterson 0:26f78923d093 30 kFilterHardwareOnly,
ptpaterson 0:26f78923d093 31 kFilterSoftwareOnly
ptpaterson 0:26f78923d093 32 };
ptpaterson 0:26f78923d093 33
ptpaterson 0:26f78923d093 34 public:
ptpaterson 0:26f78923d093 35 /* Public Methods *********************************************************/
ptpaterson 0:26f78923d093 36 /** Creates message handler linked with the CAN device
ptpaterson 0:26f78923d093 37 *
ptpaterson 0:26f78923d093 38 * @param p_can reference to CAN device
ptpaterson 0:26f78923d093 39 *
ptpaterson 0:26f78923d093 40 * Example:
ptpaterson 0:26f78923d093 41 * @code
ptpaterson 0:26f78923d093 42 *
ptpaterson 0:26f78923d093 43 * @endcode
ptpaterson 0:26f78923d093 44 */
ptpaterson 0:26f78923d093 45 CanPipe(CAN *p_can, FilterMode filter_mode = kFilterAuto);
ptpaterson 0:26f78923d093 46
ptpaterson 0:26f78923d093 47 /** Assigns a filter to apply to CAN messages. Can create software filters
ptpaterson 0:26f78923d093 48 * if device fails to create filters in hardware
ptpaterson 0:26f78923d093 49 *
ptpaterson 0:26f78923d093 50 * @param id 29 bit identifier to base filter on
ptpaterson 0:26f78923d093 51 * @param mask Bit mask applied to the id
ptpaterson 0:26f78923d093 52 * @param format CAN message format (Default CANAny)
ptpaterson 0:26f78923d093 53 * @param handle Number to associate with message when passing this filter (Optional)
ptpaterson 0:26f78923d093 54 */
ptpaterson 0:26f78923d093 55 int RegisterFilter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
ptpaterson 0:26f78923d093 56
ptpaterson 0:26f78923d093 57 /** Assigns a callback to apply to CAN messages associated with a given filter.
ptpaterson 0:26f78923d093 58 *
ptpaterson 0:26f78923d093 59 * @param callback 29 bit identifier to base filter on
ptpaterson 0:26f78923d093 60 * @param handle Filter handle to associate with this callback (Optional)
ptpaterson 0:26f78923d093 61 */
ptpaterson 0:26f78923d093 62 int RegisterCallback(CanMessageCallback callback, int handle);
ptpaterson 0:26f78923d093 63
ptpaterson 0:26f78923d093 64 /** Stage a message to be written to the bus.
ptpaterson 0:26f78923d093 65 *
ptpaterson 0:26f78923d093 66 * @param msg message to write
ptpaterson 0:26f78923d093 67 */
ptpaterson 0:26f78923d093 68 void PostMessage(CANMessage msg);
ptpaterson 0:26f78923d093 69
ptpaterson 0:26f78923d093 70 /** Passes all receieved messages through the software-filters, if any, and
ptpaterson 0:26f78923d093 71 * writes any messages that have been posted.
ptpaterson 0:26f78923d093 72 */
ptpaterson 0:26f78923d093 73 void HandleMessages(void);
ptpaterson 0:26f78923d093 74
ptpaterson 0:26f78923d093 75 private:
ptpaterson 0:26f78923d093 76 /* Private Type definitions and forward declarations **********************/
ptpaterson 0:26f78923d093 77 static const int kMaxHandles = 32;
ptpaterson 0:26f78923d093 78 static const int kMaxCallbacks = 64;
ptpaterson 0:26f78923d093 79
ptpaterson 0:26f78923d093 80 struct SoftwareFilter {
ptpaterson 0:26f78923d093 81 unsigned int id;
ptpaterson 0:26f78923d093 82 unsigned int mask;
ptpaterson 0:26f78923d093 83 CANFormat format;
ptpaterson 0:26f78923d093 84 int handle;
ptpaterson 0:26f78923d093 85 };
ptpaterson 0:26f78923d093 86
ptpaterson 0:26f78923d093 87 struct FilteredMessage {
ptpaterson 0:26f78923d093 88 CANMessage msg;
ptpaterson 0:26f78923d093 89 int handle;
ptpaterson 0:26f78923d093 90 };
ptpaterson 0:26f78923d093 91
ptpaterson 0:26f78923d093 92 struct CallbackNode {
ptpaterson 0:26f78923d093 93 CanMessageCallback callback;
ptpaterson 0:26f78923d093 94 CallbackNode *next_node;
ptpaterson 0:26f78923d093 95 };
ptpaterson 0:26f78923d093 96
ptpaterson 0:26f78923d093 97 struct CallbackList {
ptpaterson 0:26f78923d093 98 int handle;
ptpaterson 0:26f78923d093 99 CallbackNode *begin;
ptpaterson 0:26f78923d093 100 };
ptpaterson 0:26f78923d093 101
ptpaterson 0:26f78923d093 102 /* Private Members ********************************************************/
ptpaterson 0:26f78923d093 103 CAN *p_can_;
ptpaterson 0:26f78923d093 104
ptpaterson 0:26f78923d093 105 FilterMode filter_mode_;
ptpaterson 0:26f78923d093 106 FilterMode filter_auto_result_;
ptpaterson 0:26f78923d093 107
ptpaterson 0:26f78923d093 108 SoftwareFilter software_filters_[kMaxHandles];
ptpaterson 0:26f78923d093 109 CallbackNode callback_node_pool_[kMaxCallbacks];
ptpaterson 0:26f78923d093 110 CallbackList callback_list_map_[kMaxHandles];
ptpaterson 0:26f78923d093 111
ptpaterson 0:26f78923d093 112 int num_software_filters_;
ptpaterson 0:26f78923d093 113 int num_nodes_;
ptpaterson 0:26f78923d093 114 int num_lists_;
ptpaterson 0:26f78923d093 115
ptpaterson 0:26f78923d093 116 CircularBuffer<FilteredMessage, 8> read_buffer;
ptpaterson 0:26f78923d093 117 CircularBuffer<CANMessage, 8> write_buffer;
ptpaterson 0:26f78923d093 118
ptpaterson 0:26f78923d093 119 /* Private Methods ********************************************************/
ptpaterson 0:26f78923d093 120 void ReadIrq(void);
ptpaterson 0:26f78923d093 121
ptpaterson 0:26f78923d093 122 int FilterMessage();
ptpaterson 0:26f78923d093 123
ptpaterson 0:26f78923d093 124 CallbackNode* AllocateNode(CanMessageCallback callback);
ptpaterson 0:26f78923d093 125 CallbackList* AllocateList(int handle);
ptpaterson 0:26f78923d093 126
ptpaterson 0:26f78923d093 127 CallbackList* FindListWithHandle(int handle);
ptpaterson 0:26f78923d093 128 };
ptpaterson 0:26f78923d093 129
ptpaterson 0:26f78923d093 130 #endif /* CANPIPE_H */