,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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