Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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