Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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