Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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