Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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