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:
Fri Nov 04 13:54:10 2016 +0000
Revision:
4:07d73bfb6bea
Parent:
3:40648bfee1bc
Child:
5:4f73b59cfe7b
initial commit.

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 1:ebd382c0a2b8 11 typedef int (*CanMessageCallback)(CANMessage &msg);
ptpaterson 0:26f78923d093 12
ptpaterson 1:ebd382c0a2b8 13 /** A stack on top of mbed CAN class to handle complex filtered callbacks
ptpaterson 0:26f78923d093 14 */
ptpaterson 1:ebd382c0a2b8 15 class CanPipe
ptpaterson 1:ebd382c0a2b8 16 {
ptpaterson 1:ebd382c0a2b8 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 1:ebd382c0a2b8 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 1:ebd382c0a2b8 31 kFilterSoftwareOnly
ptpaterson 0:26f78923d093 32 };
ptpaterson 1:ebd382c0a2b8 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 1:ebd382c0a2b8 42 * #include "mbed.h"
ptpaterson 1:ebd382c0a2b8 43 * #include "CanPipe.h"
ptpaterson 1:ebd382c0a2b8 44 *
ptpaterson 1:ebd382c0a2b8 45 * Ticker ticker;
ptpaterson 1:ebd382c0a2b8 46 * DigitalOut led1(LED1);
ptpaterson 1:ebd382c0a2b8 47 *
ptpaterson 1:ebd382c0a2b8 48 * CAN m_can(P0_11, P0_31);
ptpaterson 1:ebd382c0a2b8 49 * CanPipe m_can_pipe(&m_can);
ptpaterson 1:ebd382c0a2b8 50 *
ptpaterson 1:ebd382c0a2b8 51 * char counter = 0;
ptpaterson 1:ebd382c0a2b8 52 *
ptpaterson 1:ebd382c0a2b8 53 * void send() {
ptpaterson 1:ebd382c0a2b8 54 * led1 = !led1;
ptpaterson 1:ebd382c0a2b8 55 * m_can_pipe.PostMessage(CANMessage(1337, &counter, 1));
ptpaterson 1:ebd382c0a2b8 56 * }
ptpaterson 1:ebd382c0a2b8 57 *
ptpaterson 3:40648bfee1bc 58 * int callback1(CANMessage &msg) { return CanPipe::kOkay; }
ptpaterson 3:40648bfee1bc 59 * int callback1(CANMessage &msg) { return CanPipe::kOkay; }
ptpaterson 3:40648bfee1bc 60 * int callback1(CANMessage &msg) { return CanPipe::kOkay; }
ptpaterson 1:ebd382c0a2b8 61 *
ptpaterson 1:ebd382c0a2b8 62 * int main() {
ptpaterson 1:ebd382c0a2b8 63 * int handle;
ptpaterson 1:ebd382c0a2b8 64 *
ptpaterson 2:388d2c07299e 65 * handle = m_can_pipe.RegisterFilter(0x200, 0x780);
ptpaterson 1:ebd382c0a2b8 66 * m_can_pipe.RegisterCallback(callback1, handle);
ptpaterson 1:ebd382c0a2b8 67 *
ptpaterson 1:ebd382c0a2b8 68 * handle = m_can_pipe.RegisterFilter(0x281, 0);
ptpaterson 1:ebd382c0a2b8 69 * m_can_pipe.RegisterCallback(callback2, handle);
ptpaterson 1:ebd382c0a2b8 70 * m_can_pipe.RegisterCallback(callback3, handle);
ptpaterson 1:ebd382c0a2b8 71 *
ptpaterson 1:ebd382c0a2b8 72 * ticker.attach(send, 1);
ptpaterson 1:ebd382c0a2b8 73 *
ptpaterson 1:ebd382c0a2b8 74 * while (1) {
ptpaterson 1:ebd382c0a2b8 75 * __WFI(); //sleep();
ptpaterson 1:ebd382c0a2b8 76 * m_can_pipe.HandleMessages();
ptpaterson 1:ebd382c0a2b8 77 * }
ptpaterson 1:ebd382c0a2b8 78 * }
ptpaterson 0:26f78923d093 79 * @endcode
ptpaterson 0:26f78923d093 80 */
ptpaterson 0:26f78923d093 81 CanPipe(CAN *p_can, FilterMode filter_mode = kFilterAuto);
ptpaterson 0:26f78923d093 82
ptpaterson 0:26f78923d093 83 /** Assigns a filter to apply to CAN messages. Can create software filters
ptpaterson 0:26f78923d093 84 * if device fails to create filters in hardware
ptpaterson 0:26f78923d093 85 *
ptpaterson 0:26f78923d093 86 * @param id 29 bit identifier to base filter on
ptpaterson 0:26f78923d093 87 * @param mask Bit mask applied to the id
ptpaterson 0:26f78923d093 88 * @param format CAN message format (Default CANAny)
ptpaterson 0:26f78923d093 89 * @param handle Number to associate with message when passing this filter (Optional)
ptpaterson 0:26f78923d093 90 */
ptpaterson 0:26f78923d093 91 int RegisterFilter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
ptpaterson 1:ebd382c0a2b8 92
ptpaterson 0:26f78923d093 93 /** Assigns a callback to apply to CAN messages associated with a given filter.
ptpaterson 0:26f78923d093 94 *
ptpaterson 0:26f78923d093 95 * @param callback 29 bit identifier to base filter on
ptpaterson 0:26f78923d093 96 * @param handle Filter handle to associate with this callback (Optional)
ptpaterson 0:26f78923d093 97 */
ptpaterson 0:26f78923d093 98 int RegisterCallback(CanMessageCallback callback, int handle);
ptpaterson 0:26f78923d093 99
ptpaterson 0:26f78923d093 100 /** Stage a message to be written to the bus.
ptpaterson 0:26f78923d093 101 *
ptpaterson 0:26f78923d093 102 * @param msg message to write
ptpaterson 0:26f78923d093 103 */
ptpaterson 0:26f78923d093 104 void PostMessage(CANMessage msg);
ptpaterson 1:ebd382c0a2b8 105
ptpaterson 0:26f78923d093 106 /** Passes all receieved messages through the software-filters, if any, and
ptpaterson 0:26f78923d093 107 * writes any messages that have been posted.
ptpaterson 0:26f78923d093 108 */
ptpaterson 0:26f78923d093 109 void HandleMessages(void);
ptpaterson 0:26f78923d093 110
ptpaterson 0:26f78923d093 111 private:
ptpaterson 0:26f78923d093 112 /* Private Type definitions and forward declarations **********************/
ptpaterson 0:26f78923d093 113 static const int kMaxHandles = 32;
ptpaterson 0:26f78923d093 114 static const int kMaxCallbacks = 64;
ptpaterson 1:ebd382c0a2b8 115
ptpaterson 0:26f78923d093 116 struct SoftwareFilter {
ptpaterson 0:26f78923d093 117 unsigned int id;
ptpaterson 0:26f78923d093 118 unsigned int mask;
ptpaterson 0:26f78923d093 119 CANFormat format;
ptpaterson 0:26f78923d093 120 int handle;
ptpaterson 0:26f78923d093 121 };
ptpaterson 1:ebd382c0a2b8 122
ptpaterson 0:26f78923d093 123 struct FilteredMessage {
ptpaterson 0:26f78923d093 124 CANMessage msg;
ptpaterson 0:26f78923d093 125 int handle;
ptpaterson 0:26f78923d093 126 };
ptpaterson 1:ebd382c0a2b8 127
ptpaterson 0:26f78923d093 128 struct CallbackNode {
ptpaterson 0:26f78923d093 129 CanMessageCallback callback;
ptpaterson 0:26f78923d093 130 CallbackNode *next_node;
ptpaterson 0:26f78923d093 131 };
ptpaterson 1:ebd382c0a2b8 132
ptpaterson 0:26f78923d093 133 struct CallbackList {
ptpaterson 0:26f78923d093 134 int handle;
ptpaterson 0:26f78923d093 135 CallbackNode *begin;
ptpaterson 0:26f78923d093 136 };
ptpaterson 0:26f78923d093 137
ptpaterson 0:26f78923d093 138 /* Private Members ********************************************************/
ptpaterson 0:26f78923d093 139 CAN *p_can_;
ptpaterson 0:26f78923d093 140
ptpaterson 0:26f78923d093 141 FilterMode filter_mode_;
ptpaterson 0:26f78923d093 142
ptpaterson 1:ebd382c0a2b8 143 SoftwareFilter software_filters_[kMaxHandles];
ptpaterson 0:26f78923d093 144 CallbackNode callback_node_pool_[kMaxCallbacks];
ptpaterson 0:26f78923d093 145 CallbackList callback_list_map_[kMaxHandles];
ptpaterson 1:ebd382c0a2b8 146
ptpaterson 0:26f78923d093 147 int num_software_filters_;
ptpaterson 0:26f78923d093 148 int num_nodes_;
ptpaterson 0:26f78923d093 149 int num_lists_;
ptpaterson 1:ebd382c0a2b8 150
ptpaterson 0:26f78923d093 151 CircularBuffer<FilteredMessage, 8> read_buffer;
ptpaterson 0:26f78923d093 152 CircularBuffer<CANMessage, 8> write_buffer;
ptpaterson 1:ebd382c0a2b8 153
ptpaterson 0:26f78923d093 154 /* Private Methods ********************************************************/
ptpaterson 0:26f78923d093 155 void ReadIrq(void);
ptpaterson 1:ebd382c0a2b8 156
ptpaterson 0:26f78923d093 157 int FilterMessage();
ptpaterson 1:ebd382c0a2b8 158
ptpaterson 0:26f78923d093 159 CallbackNode* AllocateNode(CanMessageCallback callback);
ptpaterson 0:26f78923d093 160 CallbackList* AllocateList(int handle);
ptpaterson 1:ebd382c0a2b8 161
ptpaterson 0:26f78923d093 162 CallbackList* FindListWithHandle(int handle);
ptpaterson 0:26f78923d093 163 };
ptpaterson 0:26f78923d093 164
ptpaterson 0:26f78923d093 165 #endif /* CANPIPE_H */