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