mbed library sources. Supersedes mbed-src.

Dependents:   SPIne CH_Communicatuin_Test CH_Communicatuin_Test2 MCP_SPIne ... more

Fork of mbed-dev-f303 by Ben Katz

Committer:
AnnaBridge
Date:
Mon Oct 02 15:33:19 2017 +0100
Revision:
174:b96e65c34a4d
Parent:
168:9672193075cf
This updates the lib to the mbed lib v 152

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