CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Sun Nov 04 18:44:51 2018 -0700
Revision:
0:aa13e1c335cd
create new repo for show2me

Who changed what in which revision?

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