Birkbeck College Mobile and Ubiquitous Computing IoT Lab Exercise 2
Dependencies: BLE_API_Native_blog
mbed/CAN.h@0:e8fdba0ed044, 2015-03-07 (annotated)
- Committer:
- gkroussos
- Date:
- Sat Mar 07 16:34:53 2015 +0000
- Revision:
- 0:e8fdba0ed044
MUC IoT Workshop v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gkroussos | 0:e8fdba0ed044 | 1 | /* mbed Microcontroller Library |
gkroussos | 0:e8fdba0ed044 | 2 | * Copyright (c) 2006-2013 ARM Limited |
gkroussos | 0:e8fdba0ed044 | 3 | * |
gkroussos | 0:e8fdba0ed044 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
gkroussos | 0:e8fdba0ed044 | 5 | * you may not use this file except in compliance with the License. |
gkroussos | 0:e8fdba0ed044 | 6 | * You may obtain a copy of the License at |
gkroussos | 0:e8fdba0ed044 | 7 | * |
gkroussos | 0:e8fdba0ed044 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
gkroussos | 0:e8fdba0ed044 | 9 | * |
gkroussos | 0:e8fdba0ed044 | 10 | * Unless required by applicable law or agreed to in writing, software |
gkroussos | 0:e8fdba0ed044 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
gkroussos | 0:e8fdba0ed044 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
gkroussos | 0:e8fdba0ed044 | 13 | * See the License for the specific language governing permissions and |
gkroussos | 0:e8fdba0ed044 | 14 | * limitations under the License. |
gkroussos | 0:e8fdba0ed044 | 15 | */ |
gkroussos | 0:e8fdba0ed044 | 16 | #ifndef MBED_CAN_H |
gkroussos | 0:e8fdba0ed044 | 17 | #define MBED_CAN_H |
gkroussos | 0:e8fdba0ed044 | 18 | |
gkroussos | 0:e8fdba0ed044 | 19 | #include "platform.h" |
gkroussos | 0:e8fdba0ed044 | 20 | |
gkroussos | 0:e8fdba0ed044 | 21 | #if DEVICE_CAN |
gkroussos | 0:e8fdba0ed044 | 22 | |
gkroussos | 0:e8fdba0ed044 | 23 | #include "can_api.h" |
gkroussos | 0:e8fdba0ed044 | 24 | #include "can_helper.h" |
gkroussos | 0:e8fdba0ed044 | 25 | #include "FunctionPointer.h" |
gkroussos | 0:e8fdba0ed044 | 26 | |
gkroussos | 0:e8fdba0ed044 | 27 | namespace mbed { |
gkroussos | 0:e8fdba0ed044 | 28 | |
gkroussos | 0:e8fdba0ed044 | 29 | /** CANMessage class |
gkroussos | 0:e8fdba0ed044 | 30 | */ |
gkroussos | 0:e8fdba0ed044 | 31 | class CANMessage : public CAN_Message { |
gkroussos | 0:e8fdba0ed044 | 32 | |
gkroussos | 0:e8fdba0ed044 | 33 | public: |
gkroussos | 0:e8fdba0ed044 | 34 | /** Creates empty CAN message. |
gkroussos | 0:e8fdba0ed044 | 35 | */ |
gkroussos | 0:e8fdba0ed044 | 36 | CANMessage() { |
gkroussos | 0:e8fdba0ed044 | 37 | len = 8; |
gkroussos | 0:e8fdba0ed044 | 38 | type = CANData; |
gkroussos | 0:e8fdba0ed044 | 39 | format = CANStandard; |
gkroussos | 0:e8fdba0ed044 | 40 | id = 0; |
gkroussos | 0:e8fdba0ed044 | 41 | memset(data, 0, 8); |
gkroussos | 0:e8fdba0ed044 | 42 | } |
gkroussos | 0:e8fdba0ed044 | 43 | |
gkroussos | 0:e8fdba0ed044 | 44 | /** Creates CAN message with specific content. |
gkroussos | 0:e8fdba0ed044 | 45 | */ |
gkroussos | 0:e8fdba0ed044 | 46 | CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
gkroussos | 0:e8fdba0ed044 | 47 | len = _len & 0xF; |
gkroussos | 0:e8fdba0ed044 | 48 | type = _type; |
gkroussos | 0:e8fdba0ed044 | 49 | format = _format; |
gkroussos | 0:e8fdba0ed044 | 50 | id = _id; |
gkroussos | 0:e8fdba0ed044 | 51 | memcpy(data, _data, _len); |
gkroussos | 0:e8fdba0ed044 | 52 | } |
gkroussos | 0:e8fdba0ed044 | 53 | |
gkroussos | 0:e8fdba0ed044 | 54 | /** Creates CAN remote message. |
gkroussos | 0:e8fdba0ed044 | 55 | */ |
gkroussos | 0:e8fdba0ed044 | 56 | CANMessage(int _id, CANFormat _format = CANStandard) { |
gkroussos | 0:e8fdba0ed044 | 57 | len = 0; |
gkroussos | 0:e8fdba0ed044 | 58 | type = CANRemote; |
gkroussos | 0:e8fdba0ed044 | 59 | format = _format; |
gkroussos | 0:e8fdba0ed044 | 60 | id = _id; |
gkroussos | 0:e8fdba0ed044 | 61 | memset(data, 0, 8); |
gkroussos | 0:e8fdba0ed044 | 62 | } |
gkroussos | 0:e8fdba0ed044 | 63 | }; |
gkroussos | 0:e8fdba0ed044 | 64 | |
gkroussos | 0:e8fdba0ed044 | 65 | /** A can bus client, used for communicating with can devices |
gkroussos | 0:e8fdba0ed044 | 66 | */ |
gkroussos | 0:e8fdba0ed044 | 67 | class CAN { |
gkroussos | 0:e8fdba0ed044 | 68 | |
gkroussos | 0:e8fdba0ed044 | 69 | public: |
gkroussos | 0:e8fdba0ed044 | 70 | /** Creates an CAN interface connected to specific pins. |
gkroussos | 0:e8fdba0ed044 | 71 | * |
gkroussos | 0:e8fdba0ed044 | 72 | * @param rd read from transmitter |
gkroussos | 0:e8fdba0ed044 | 73 | * @param td transmit to transmitter |
gkroussos | 0:e8fdba0ed044 | 74 | * |
gkroussos | 0:e8fdba0ed044 | 75 | * Example: |
gkroussos | 0:e8fdba0ed044 | 76 | * @code |
gkroussos | 0:e8fdba0ed044 | 77 | * #include "mbed.h" |
gkroussos | 0:e8fdba0ed044 | 78 | * |
gkroussos | 0:e8fdba0ed044 | 79 | * Ticker ticker; |
gkroussos | 0:e8fdba0ed044 | 80 | * DigitalOut led1(LED1); |
gkroussos | 0:e8fdba0ed044 | 81 | * DigitalOut led2(LED2); |
gkroussos | 0:e8fdba0ed044 | 82 | * CAN can1(p9, p10); |
gkroussos | 0:e8fdba0ed044 | 83 | * CAN can2(p30, p29); |
gkroussos | 0:e8fdba0ed044 | 84 | * |
gkroussos | 0:e8fdba0ed044 | 85 | * char counter = 0; |
gkroussos | 0:e8fdba0ed044 | 86 | * |
gkroussos | 0:e8fdba0ed044 | 87 | * void send() { |
gkroussos | 0:e8fdba0ed044 | 88 | * if(can1.write(CANMessage(1337, &counter, 1))) { |
gkroussos | 0:e8fdba0ed044 | 89 | * printf("Message sent: %d\n", counter); |
gkroussos | 0:e8fdba0ed044 | 90 | * counter++; |
gkroussos | 0:e8fdba0ed044 | 91 | * } |
gkroussos | 0:e8fdba0ed044 | 92 | * led1 = !led1; |
gkroussos | 0:e8fdba0ed044 | 93 | * } |
gkroussos | 0:e8fdba0ed044 | 94 | * |
gkroussos | 0:e8fdba0ed044 | 95 | * int main() { |
gkroussos | 0:e8fdba0ed044 | 96 | * ticker.attach(&send, 1); |
gkroussos | 0:e8fdba0ed044 | 97 | * CANMessage msg; |
gkroussos | 0:e8fdba0ed044 | 98 | * while(1) { |
gkroussos | 0:e8fdba0ed044 | 99 | * if(can2.read(msg)) { |
gkroussos | 0:e8fdba0ed044 | 100 | * printf("Message received: %d\n\n", msg.data[0]); |
gkroussos | 0:e8fdba0ed044 | 101 | * led2 = !led2; |
gkroussos | 0:e8fdba0ed044 | 102 | * } |
gkroussos | 0:e8fdba0ed044 | 103 | * wait(0.2); |
gkroussos | 0:e8fdba0ed044 | 104 | * } |
gkroussos | 0:e8fdba0ed044 | 105 | * } |
gkroussos | 0:e8fdba0ed044 | 106 | * @endcode |
gkroussos | 0:e8fdba0ed044 | 107 | */ |
gkroussos | 0:e8fdba0ed044 | 108 | CAN(PinName rd, PinName td); |
gkroussos | 0:e8fdba0ed044 | 109 | virtual ~CAN(); |
gkroussos | 0:e8fdba0ed044 | 110 | |
gkroussos | 0:e8fdba0ed044 | 111 | /** Set the frequency of the CAN interface |
gkroussos | 0:e8fdba0ed044 | 112 | * |
gkroussos | 0:e8fdba0ed044 | 113 | * @param hz The bus frequency in hertz |
gkroussos | 0:e8fdba0ed044 | 114 | * |
gkroussos | 0:e8fdba0ed044 | 115 | * @returns |
gkroussos | 0:e8fdba0ed044 | 116 | * 1 if successful, |
gkroussos | 0:e8fdba0ed044 | 117 | * 0 otherwise |
gkroussos | 0:e8fdba0ed044 | 118 | */ |
gkroussos | 0:e8fdba0ed044 | 119 | int frequency(int hz); |
gkroussos | 0:e8fdba0ed044 | 120 | |
gkroussos | 0:e8fdba0ed044 | 121 | /** Write a CANMessage to the bus. |
gkroussos | 0:e8fdba0ed044 | 122 | * |
gkroussos | 0:e8fdba0ed044 | 123 | * @param msg The CANMessage to write. |
gkroussos | 0:e8fdba0ed044 | 124 | * |
gkroussos | 0:e8fdba0ed044 | 125 | * @returns |
gkroussos | 0:e8fdba0ed044 | 126 | * 0 if write failed, |
gkroussos | 0:e8fdba0ed044 | 127 | * 1 if write was successful |
gkroussos | 0:e8fdba0ed044 | 128 | */ |
gkroussos | 0:e8fdba0ed044 | 129 | int write(CANMessage msg); |
gkroussos | 0:e8fdba0ed044 | 130 | |
gkroussos | 0:e8fdba0ed044 | 131 | /** Read a CANMessage from the bus. |
gkroussos | 0:e8fdba0ed044 | 132 | * |
gkroussos | 0:e8fdba0ed044 | 133 | * @param msg A CANMessage to read to. |
gkroussos | 0:e8fdba0ed044 | 134 | * @param handle message filter handle (0 for any message) |
gkroussos | 0:e8fdba0ed044 | 135 | * |
gkroussos | 0:e8fdba0ed044 | 136 | * @returns |
gkroussos | 0:e8fdba0ed044 | 137 | * 0 if no message arrived, |
gkroussos | 0:e8fdba0ed044 | 138 | * 1 if message arrived |
gkroussos | 0:e8fdba0ed044 | 139 | */ |
gkroussos | 0:e8fdba0ed044 | 140 | int read(CANMessage &msg, int handle = 0); |
gkroussos | 0:e8fdba0ed044 | 141 | |
gkroussos | 0:e8fdba0ed044 | 142 | /** Reset CAN interface. |
gkroussos | 0:e8fdba0ed044 | 143 | * |
gkroussos | 0:e8fdba0ed044 | 144 | * To use after error overflow. |
gkroussos | 0:e8fdba0ed044 | 145 | */ |
gkroussos | 0:e8fdba0ed044 | 146 | void reset(); |
gkroussos | 0:e8fdba0ed044 | 147 | |
gkroussos | 0:e8fdba0ed044 | 148 | /** Puts or removes the CAN interface into silent monitoring mode |
gkroussos | 0:e8fdba0ed044 | 149 | * |
gkroussos | 0:e8fdba0ed044 | 150 | * @param silent boolean indicating whether to go into silent mode or not |
gkroussos | 0:e8fdba0ed044 | 151 | */ |
gkroussos | 0:e8fdba0ed044 | 152 | void monitor(bool silent); |
gkroussos | 0:e8fdba0ed044 | 153 | |
gkroussos | 0:e8fdba0ed044 | 154 | enum Mode { |
gkroussos | 0:e8fdba0ed044 | 155 | Reset = 0, |
gkroussos | 0:e8fdba0ed044 | 156 | Normal, |
gkroussos | 0:e8fdba0ed044 | 157 | Silent, |
gkroussos | 0:e8fdba0ed044 | 158 | LocalTest, |
gkroussos | 0:e8fdba0ed044 | 159 | GlobalTest, |
gkroussos | 0:e8fdba0ed044 | 160 | SilentTest |
gkroussos | 0:e8fdba0ed044 | 161 | }; |
gkroussos | 0:e8fdba0ed044 | 162 | |
gkroussos | 0:e8fdba0ed044 | 163 | /** Change CAN operation to the specified mode |
gkroussos | 0:e8fdba0ed044 | 164 | * |
gkroussos | 0:e8fdba0ed044 | 165 | * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest) |
gkroussos | 0:e8fdba0ed044 | 166 | * |
gkroussos | 0:e8fdba0ed044 | 167 | * @returns |
gkroussos | 0:e8fdba0ed044 | 168 | * 0 if mode change failed or unsupported, |
gkroussos | 0:e8fdba0ed044 | 169 | * 1 if mode change was successful |
gkroussos | 0:e8fdba0ed044 | 170 | */ |
gkroussos | 0:e8fdba0ed044 | 171 | int mode(Mode mode); |
gkroussos | 0:e8fdba0ed044 | 172 | |
gkroussos | 0:e8fdba0ed044 | 173 | /** Filter out incomming messages |
gkroussos | 0:e8fdba0ed044 | 174 | * |
gkroussos | 0:e8fdba0ed044 | 175 | * @param id the id to filter on |
gkroussos | 0:e8fdba0ed044 | 176 | * @param mask the mask applied to the id |
gkroussos | 0:e8fdba0ed044 | 177 | * @param format format to filter on (Default CANAny) |
gkroussos | 0:e8fdba0ed044 | 178 | * @param handle message filter handle (Optional) |
gkroussos | 0:e8fdba0ed044 | 179 | * |
gkroussos | 0:e8fdba0ed044 | 180 | * @returns |
gkroussos | 0:e8fdba0ed044 | 181 | * 0 if filter change failed or unsupported, |
gkroussos | 0:e8fdba0ed044 | 182 | * new filter handle if successful |
gkroussos | 0:e8fdba0ed044 | 183 | */ |
gkroussos | 0:e8fdba0ed044 | 184 | int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0); |
gkroussos | 0:e8fdba0ed044 | 185 | |
gkroussos | 0:e8fdba0ed044 | 186 | /** Returns number of read errors to detect read overflow errors. |
gkroussos | 0:e8fdba0ed044 | 187 | */ |
gkroussos | 0:e8fdba0ed044 | 188 | unsigned char rderror(); |
gkroussos | 0:e8fdba0ed044 | 189 | |
gkroussos | 0:e8fdba0ed044 | 190 | /** Returns number of write errors to detect write overflow errors. |
gkroussos | 0:e8fdba0ed044 | 191 | */ |
gkroussos | 0:e8fdba0ed044 | 192 | unsigned char tderror(); |
gkroussos | 0:e8fdba0ed044 | 193 | |
gkroussos | 0:e8fdba0ed044 | 194 | enum IrqType { |
gkroussos | 0:e8fdba0ed044 | 195 | RxIrq = 0, |
gkroussos | 0:e8fdba0ed044 | 196 | TxIrq, |
gkroussos | 0:e8fdba0ed044 | 197 | EwIrq, |
gkroussos | 0:e8fdba0ed044 | 198 | DoIrq, |
gkroussos | 0:e8fdba0ed044 | 199 | WuIrq, |
gkroussos | 0:e8fdba0ed044 | 200 | EpIrq, |
gkroussos | 0:e8fdba0ed044 | 201 | AlIrq, |
gkroussos | 0:e8fdba0ed044 | 202 | BeIrq, |
gkroussos | 0:e8fdba0ed044 | 203 | IdIrq |
gkroussos | 0:e8fdba0ed044 | 204 | }; |
gkroussos | 0:e8fdba0ed044 | 205 | |
gkroussos | 0:e8fdba0ed044 | 206 | /** Attach a function to call whenever a CAN frame received interrupt is |
gkroussos | 0:e8fdba0ed044 | 207 | * generated. |
gkroussos | 0:e8fdba0ed044 | 208 | * |
gkroussos | 0:e8fdba0ed044 | 209 | * @param fptr A pointer to a void function, or 0 to set as none |
gkroussos | 0:e8fdba0ed044 | 210 | * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error) |
gkroussos | 0:e8fdba0ed044 | 211 | */ |
gkroussos | 0:e8fdba0ed044 | 212 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
gkroussos | 0:e8fdba0ed044 | 213 | |
gkroussos | 0:e8fdba0ed044 | 214 | /** Attach a member function to call whenever a CAN frame received interrupt |
gkroussos | 0:e8fdba0ed044 | 215 | * is generated. |
gkroussos | 0:e8fdba0ed044 | 216 | * |
gkroussos | 0:e8fdba0ed044 | 217 | * @param tptr pointer to the object to call the member function on |
gkroussos | 0:e8fdba0ed044 | 218 | * @param mptr pointer to the member function to be called |
gkroussos | 0:e8fdba0ed044 | 219 | * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) |
gkroussos | 0:e8fdba0ed044 | 220 | */ |
gkroussos | 0:e8fdba0ed044 | 221 | template<typename T> |
gkroussos | 0:e8fdba0ed044 | 222 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
gkroussos | 0:e8fdba0ed044 | 223 | if((mptr != NULL) && (tptr != NULL)) { |
gkroussos | 0:e8fdba0ed044 | 224 | _irq[type].attach(tptr, mptr); |
gkroussos | 0:e8fdba0ed044 | 225 | can_irq_set(&_can, (CanIrqType)type, 1); |
gkroussos | 0:e8fdba0ed044 | 226 | } |
gkroussos | 0:e8fdba0ed044 | 227 | else { |
gkroussos | 0:e8fdba0ed044 | 228 | can_irq_set(&_can, (CanIrqType)type, 0); |
gkroussos | 0:e8fdba0ed044 | 229 | } |
gkroussos | 0:e8fdba0ed044 | 230 | } |
gkroussos | 0:e8fdba0ed044 | 231 | |
gkroussos | 0:e8fdba0ed044 | 232 | static void _irq_handler(uint32_t id, CanIrqType type); |
gkroussos | 0:e8fdba0ed044 | 233 | |
gkroussos | 0:e8fdba0ed044 | 234 | protected: |
gkroussos | 0:e8fdba0ed044 | 235 | can_t _can; |
gkroussos | 0:e8fdba0ed044 | 236 | FunctionPointer _irq[9]; |
gkroussos | 0:e8fdba0ed044 | 237 | }; |
gkroussos | 0:e8fdba0ed044 | 238 | |
gkroussos | 0:e8fdba0ed044 | 239 | } // namespace mbed |
gkroussos | 0:e8fdba0ed044 | 240 | |
gkroussos | 0:e8fdba0ed044 | 241 | #endif |
gkroussos | 0:e8fdba0ed044 | 242 | |
gkroussos | 0:e8fdba0ed044 | 243 | #endif // MBED_CAN_H |