This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

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