mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Thu Sep 06 13:40:20 2018 +0100
Revision:
187:0387e8f68319
Parent:
174:b96e65c34a4d
Child:
188:bcfe06ba3d64
mbed-dev library. Release version 163

Who changed what in which revision?

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