BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:55:34 2018 +0000
Revision:
2:798925c9e4a8
Parent:
1:9c5af431a1f1
bluetooth

Who changed what in which revision?

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