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