dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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