mbed library sources, with proposed I2C for Seed Arch BLE - not working -
Dependents: Seeed_Barometer_Sensor_Example
Fork of mbed-src by
cpp/CAN.h@0:fd0d7bdfcdc2, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:24:08 2012 +0000
- Revision:
- 0:fd0d7bdfcdc2
- Child:
- 2:143cac498751
mbed sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:fd0d7bdfcdc2 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
mbed_official | 0:fd0d7bdfcdc2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:fd0d7bdfcdc2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:fd0d7bdfcdc2 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:fd0d7bdfcdc2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:fd0d7bdfcdc2 | 10 | * |
mbed_official | 0:fd0d7bdfcdc2 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:fd0d7bdfcdc2 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:fd0d7bdfcdc2 | 13 | * |
mbed_official | 0:fd0d7bdfcdc2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:fd0d7bdfcdc2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:fd0d7bdfcdc2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:fd0d7bdfcdc2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:fd0d7bdfcdc2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:fd0d7bdfcdc2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:fd0d7bdfcdc2 | 20 | * SOFTWARE. |
mbed_official | 0:fd0d7bdfcdc2 | 21 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #ifndef MBED_CAN_H |
mbed_official | 0:fd0d7bdfcdc2 | 23 | #define MBED_CAN_H |
mbed_official | 0:fd0d7bdfcdc2 | 24 | |
mbed_official | 0:fd0d7bdfcdc2 | 25 | #include "platform.h" |
mbed_official | 0:fd0d7bdfcdc2 | 26 | |
mbed_official | 0:fd0d7bdfcdc2 | 27 | #if DEVICE_CAN |
mbed_official | 0:fd0d7bdfcdc2 | 28 | |
mbed_official | 0:fd0d7bdfcdc2 | 29 | #include "can_api.h" |
mbed_official | 0:fd0d7bdfcdc2 | 30 | #include "can_helper.h" |
mbed_official | 0:fd0d7bdfcdc2 | 31 | #include "FunctionPointer.h" |
mbed_official | 0:fd0d7bdfcdc2 | 32 | |
mbed_official | 0:fd0d7bdfcdc2 | 33 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 34 | |
mbed_official | 0:fd0d7bdfcdc2 | 35 | /** CANMessage class |
mbed_official | 0:fd0d7bdfcdc2 | 36 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 37 | class CANMessage : public CAN_Message { |
mbed_official | 0:fd0d7bdfcdc2 | 38 | |
mbed_official | 0:fd0d7bdfcdc2 | 39 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 40 | /** Creates empty CAN message. |
mbed_official | 0:fd0d7bdfcdc2 | 41 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 42 | CANMessage() { |
mbed_official | 0:fd0d7bdfcdc2 | 43 | len = 8; |
mbed_official | 0:fd0d7bdfcdc2 | 44 | type = CANData; |
mbed_official | 0:fd0d7bdfcdc2 | 45 | format = CANStandard; |
mbed_official | 0:fd0d7bdfcdc2 | 46 | id = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 47 | memset(data, 0, 8); |
mbed_official | 0:fd0d7bdfcdc2 | 48 | } |
mbed_official | 0:fd0d7bdfcdc2 | 49 | |
mbed_official | 0:fd0d7bdfcdc2 | 50 | /** Creates CAN message with specific content. |
mbed_official | 0:fd0d7bdfcdc2 | 51 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 52 | CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
mbed_official | 0:fd0d7bdfcdc2 | 53 | len = _len & 0xF; |
mbed_official | 0:fd0d7bdfcdc2 | 54 | type = _type; |
mbed_official | 0:fd0d7bdfcdc2 | 55 | format = _format; |
mbed_official | 0:fd0d7bdfcdc2 | 56 | id = _id; |
mbed_official | 0:fd0d7bdfcdc2 | 57 | memcpy(data, _data, _len); |
mbed_official | 0:fd0d7bdfcdc2 | 58 | } |
mbed_official | 0:fd0d7bdfcdc2 | 59 | |
mbed_official | 0:fd0d7bdfcdc2 | 60 | /** Creates CAN remote message. |
mbed_official | 0:fd0d7bdfcdc2 | 61 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 62 | CANMessage(int _id, CANFormat _format = CANStandard) { |
mbed_official | 0:fd0d7bdfcdc2 | 63 | len = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 64 | type = CANRemote; |
mbed_official | 0:fd0d7bdfcdc2 | 65 | format = _format; |
mbed_official | 0:fd0d7bdfcdc2 | 66 | id = _id; |
mbed_official | 0:fd0d7bdfcdc2 | 67 | memset(data, 0, 8); |
mbed_official | 0:fd0d7bdfcdc2 | 68 | } |
mbed_official | 0:fd0d7bdfcdc2 | 69 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 70 | |
mbed_official | 0:fd0d7bdfcdc2 | 71 | /** A can bus client, used for communicating with can devices |
mbed_official | 0:fd0d7bdfcdc2 | 72 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 73 | class CAN { |
mbed_official | 0:fd0d7bdfcdc2 | 74 | |
mbed_official | 0:fd0d7bdfcdc2 | 75 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 76 | /** Creates an CAN interface connected to specific pins. |
mbed_official | 0:fd0d7bdfcdc2 | 77 | * |
mbed_official | 0:fd0d7bdfcdc2 | 78 | * @param rd read from transmitter |
mbed_official | 0:fd0d7bdfcdc2 | 79 | * @param td transmit to transmitter |
mbed_official | 0:fd0d7bdfcdc2 | 80 | * |
mbed_official | 0:fd0d7bdfcdc2 | 81 | * Example: |
mbed_official | 0:fd0d7bdfcdc2 | 82 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 83 | * #include "mbed.h" |
mbed_official | 0:fd0d7bdfcdc2 | 84 | * |
mbed_official | 0:fd0d7bdfcdc2 | 85 | * Ticker ticker; |
mbed_official | 0:fd0d7bdfcdc2 | 86 | * DigitalOut led1(LED1); |
mbed_official | 0:fd0d7bdfcdc2 | 87 | * DigitalOut led2(LED2); |
mbed_official | 0:fd0d7bdfcdc2 | 88 | * CAN can1(p9, p10); |
mbed_official | 0:fd0d7bdfcdc2 | 89 | * CAN can2(p30, p29); |
mbed_official | 0:fd0d7bdfcdc2 | 90 | * |
mbed_official | 0:fd0d7bdfcdc2 | 91 | * char counter = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 92 | * |
mbed_official | 0:fd0d7bdfcdc2 | 93 | * void send() { |
mbed_official | 0:fd0d7bdfcdc2 | 94 | * if(can1.write(CANMessage(1337, &counter, 1))) { |
mbed_official | 0:fd0d7bdfcdc2 | 95 | * printf("Message sent: %d\n", counter); |
mbed_official | 0:fd0d7bdfcdc2 | 96 | * counter++; |
mbed_official | 0:fd0d7bdfcdc2 | 97 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 98 | * led1 = !led1; |
mbed_official | 0:fd0d7bdfcdc2 | 99 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 100 | * |
mbed_official | 0:fd0d7bdfcdc2 | 101 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 102 | * ticker.attach(&send, 1); |
mbed_official | 0:fd0d7bdfcdc2 | 103 | * CANMessage msg; |
mbed_official | 0:fd0d7bdfcdc2 | 104 | * while(1) { |
mbed_official | 0:fd0d7bdfcdc2 | 105 | * if(can2.read(msg)) { |
mbed_official | 0:fd0d7bdfcdc2 | 106 | * printf("Message received: %d\n\n", msg.data[0]); |
mbed_official | 0:fd0d7bdfcdc2 | 107 | * led2 = !led2; |
mbed_official | 0:fd0d7bdfcdc2 | 108 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 109 | * wait(0.2); |
mbed_official | 0:fd0d7bdfcdc2 | 110 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 111 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 112 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 113 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 114 | CAN(PinName rd, PinName td); |
mbed_official | 0:fd0d7bdfcdc2 | 115 | virtual ~CAN(); |
mbed_official | 0:fd0d7bdfcdc2 | 116 | |
mbed_official | 0:fd0d7bdfcdc2 | 117 | /** Set the frequency of the CAN interface |
mbed_official | 0:fd0d7bdfcdc2 | 118 | * |
mbed_official | 0:fd0d7bdfcdc2 | 119 | * @param hz The bus frequency in hertz |
mbed_official | 0:fd0d7bdfcdc2 | 120 | * |
mbed_official | 0:fd0d7bdfcdc2 | 121 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 122 | * 1 if successful, |
mbed_official | 0:fd0d7bdfcdc2 | 123 | * 0 otherwise |
mbed_official | 0:fd0d7bdfcdc2 | 124 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 125 | int frequency(int hz); |
mbed_official | 0:fd0d7bdfcdc2 | 126 | |
mbed_official | 0:fd0d7bdfcdc2 | 127 | /** Write a CANMessage to the bus. |
mbed_official | 0:fd0d7bdfcdc2 | 128 | * |
mbed_official | 0:fd0d7bdfcdc2 | 129 | * @param msg The CANMessage to write. |
mbed_official | 0:fd0d7bdfcdc2 | 130 | * |
mbed_official | 0:fd0d7bdfcdc2 | 131 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 132 | * 0 if write failed, |
mbed_official | 0:fd0d7bdfcdc2 | 133 | * 1 if write was successful |
mbed_official | 0:fd0d7bdfcdc2 | 134 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 135 | int write(CANMessage msg); |
mbed_official | 0:fd0d7bdfcdc2 | 136 | |
mbed_official | 0:fd0d7bdfcdc2 | 137 | /** Read a CANMessage from the bus. |
mbed_official | 0:fd0d7bdfcdc2 | 138 | * |
mbed_official | 0:fd0d7bdfcdc2 | 139 | * @param msg A CANMessage to read to. |
mbed_official | 0:fd0d7bdfcdc2 | 140 | * |
mbed_official | 0:fd0d7bdfcdc2 | 141 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 142 | * 0 if no message arrived, |
mbed_official | 0:fd0d7bdfcdc2 | 143 | * 1 if message arrived |
mbed_official | 0:fd0d7bdfcdc2 | 144 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 145 | int read(CANMessage &msg); |
mbed_official | 0:fd0d7bdfcdc2 | 146 | |
mbed_official | 0:fd0d7bdfcdc2 | 147 | /** Reset CAN interface. |
mbed_official | 0:fd0d7bdfcdc2 | 148 | * |
mbed_official | 0:fd0d7bdfcdc2 | 149 | * To use after error overflow. |
mbed_official | 0:fd0d7bdfcdc2 | 150 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 151 | void reset(); |
mbed_official | 0:fd0d7bdfcdc2 | 152 | |
mbed_official | 0:fd0d7bdfcdc2 | 153 | /** Puts or removes the CAN interface into silent monitoring mode |
mbed_official | 0:fd0d7bdfcdc2 | 154 | * |
mbed_official | 0:fd0d7bdfcdc2 | 155 | * @param silent boolean indicating whether to go into silent mode or not |
mbed_official | 0:fd0d7bdfcdc2 | 156 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 157 | void monitor(bool silent); |
mbed_official | 0:fd0d7bdfcdc2 | 158 | |
mbed_official | 0:fd0d7bdfcdc2 | 159 | /** Returns number of read errors to detect read overflow errors. |
mbed_official | 0:fd0d7bdfcdc2 | 160 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 161 | unsigned char rderror(); |
mbed_official | 0:fd0d7bdfcdc2 | 162 | |
mbed_official | 0:fd0d7bdfcdc2 | 163 | /** Returns number of write errors to detect write overflow errors. |
mbed_official | 0:fd0d7bdfcdc2 | 164 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 165 | unsigned char tderror(); |
mbed_official | 0:fd0d7bdfcdc2 | 166 | |
mbed_official | 0:fd0d7bdfcdc2 | 167 | /** Attach a function to call whenever a CAN frame received interrupt is |
mbed_official | 0:fd0d7bdfcdc2 | 168 | * generated. |
mbed_official | 0:fd0d7bdfcdc2 | 169 | * |
mbed_official | 0:fd0d7bdfcdc2 | 170 | * @param fptr A pointer to a void function, or 0 to set as none |
mbed_official | 0:fd0d7bdfcdc2 | 171 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 172 | void attach(void (*fptr)(void)); |
mbed_official | 0:fd0d7bdfcdc2 | 173 | |
mbed_official | 0:fd0d7bdfcdc2 | 174 | /** Attach a member function to call whenever a CAN frame received interrupt |
mbed_official | 0:fd0d7bdfcdc2 | 175 | * is generated. |
mbed_official | 0:fd0d7bdfcdc2 | 176 | * |
mbed_official | 0:fd0d7bdfcdc2 | 177 | * @param tptr pointer to the object to call the member function on |
mbed_official | 0:fd0d7bdfcdc2 | 178 | * @param mptr pointer to the member function to be called |
mbed_official | 0:fd0d7bdfcdc2 | 179 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 180 | template<typename T> |
mbed_official | 0:fd0d7bdfcdc2 | 181 | void attach(T* tptr, void (T::*mptr)(void)) { |
mbed_official | 0:fd0d7bdfcdc2 | 182 | if((mptr != NULL) && (tptr != NULL)) { |
mbed_official | 0:fd0d7bdfcdc2 | 183 | _rxirq.attach(tptr, mptr); |
mbed_official | 0:fd0d7bdfcdc2 | 184 | setup_interrupt(); |
mbed_official | 0:fd0d7bdfcdc2 | 185 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 186 | remove_interrupt(); |
mbed_official | 0:fd0d7bdfcdc2 | 187 | } |
mbed_official | 0:fd0d7bdfcdc2 | 188 | } |
mbed_official | 0:fd0d7bdfcdc2 | 189 | |
mbed_official | 0:fd0d7bdfcdc2 | 190 | private: |
mbed_official | 0:fd0d7bdfcdc2 | 191 | can_t _can; |
mbed_official | 0:fd0d7bdfcdc2 | 192 | FunctionPointer _rxirq; |
mbed_official | 0:fd0d7bdfcdc2 | 193 | |
mbed_official | 0:fd0d7bdfcdc2 | 194 | void setup_interrupt(void); |
mbed_official | 0:fd0d7bdfcdc2 | 195 | void remove_interrupt(void); |
mbed_official | 0:fd0d7bdfcdc2 | 196 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 197 | |
mbed_official | 0:fd0d7bdfcdc2 | 198 | } // namespace mbed |
mbed_official | 0:fd0d7bdfcdc2 | 199 | |
mbed_official | 0:fd0d7bdfcdc2 | 200 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 201 | |
mbed_official | 0:fd0d7bdfcdc2 | 202 | #endif // MBED_CAN_H |