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