The official mbed C/C SDK provides the software platform and libraries to build your applications.

Fork of mbed by mbed official

Committer:
ldyz
Date:
Fri Jul 05 13:16:13 2013 +0000
Revision:
64:75c1708b266b
Parent:
59:0883845fe643
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 44:24d45a770a51 1 /* mbed Microcontroller Library
emilmont 54:71b101360fb9 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 44:24d45a770a51 3 *
emilmont 59:0883845fe643 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 59:0883845fe643 5 * you may not use this file except in compliance with the License.
emilmont 59:0883845fe643 6 * You may obtain a copy of the License at
emilmont 59:0883845fe643 7 *
emilmont 59:0883845fe643 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 44:24d45a770a51 9 *
emilmont 59:0883845fe643 10 * Unless required by applicable law or agreed to in writing, software
emilmont 59:0883845fe643 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 59:0883845fe643 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 59:0883845fe643 13 * See the License for the specific language governing permissions and
emilmont 59:0883845fe643 14 * limitations under the License.
emilmont 44:24d45a770a51 15 */
rolf.meyer@arm.com 11:1c1ebd0324fa 16 #ifndef MBED_CAN_H
rolf.meyer@arm.com 11:1c1ebd0324fa 17 #define MBED_CAN_H
rolf.meyer@arm.com 11:1c1ebd0324fa 18
emilmont 44:24d45a770a51 19 #include "platform.h"
emilmont 27:7110ebee3484 20
emilmont 27:7110ebee3484 21 #if DEVICE_CAN
emilmont 27:7110ebee3484 22
emilmont 44:24d45a770a51 23 #include "can_api.h"
emilmont 55:d722ed6a4237 24 #include "can_helper.h"
simon 22:9114680c05da 25 #include "FunctionPointer.h"
rolf.meyer@arm.com 11:1c1ebd0324fa 26
rolf.meyer@arm.com 11:1c1ebd0324fa 27 namespace mbed {
rolf.meyer@arm.com 11:1c1ebd0324fa 28
emilmont 43:e2ed12d17f06 29 /** CANMessage class
rolf.meyer@arm.com 11:1c1ebd0324fa 30 */
rolf.meyer@arm.com 11:1c1ebd0324fa 31 class CANMessage : public CAN_Message {
rolf.meyer@arm.com 11:1c1ebd0324fa 32
rolf.meyer@arm.com 11:1c1ebd0324fa 33 public:
emilmont 43:e2ed12d17f06 34 /** Creates empty CAN message.
rolf.meyer@arm.com 11:1c1ebd0324fa 35 */
rolf.meyer@arm.com 11:1c1ebd0324fa 36 CANMessage() {
emilmont 44:24d45a770a51 37 len = 8;
emilmont 44:24d45a770a51 38 type = CANData;
emilmont 44:24d45a770a51 39 format = CANStandard;
emilmont 44:24d45a770a51 40 id = 0;
emilmont 44:24d45a770a51 41 memset(data, 0, 8);
rolf.meyer@arm.com 11:1c1ebd0324fa 42 }
emilmont 55:d722ed6a4237 43
emilmont 43:e2ed12d17f06 44 /** Creates CAN message with specific content.
rolf.meyer@arm.com 11:1c1ebd0324fa 45 */
rolf.meyer@arm.com 11:1c1ebd0324fa 46 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
rolf.meyer@arm.com 11:1c1ebd0324fa 47 len = _len & 0xF;
rolf.meyer@arm.com 11:1c1ebd0324fa 48 type = _type;
rolf.meyer@arm.com 11:1c1ebd0324fa 49 format = _format;
rolf.meyer@arm.com 11:1c1ebd0324fa 50 id = _id;
rolf.meyer@arm.com 11:1c1ebd0324fa 51 memcpy(data, _data, _len);
rolf.meyer@arm.com 11:1c1ebd0324fa 52 }
rolf.meyer@arm.com 11:1c1ebd0324fa 53
emilmont 43:e2ed12d17f06 54 /** Creates CAN remote message.
rolf.meyer@arm.com 11:1c1ebd0324fa 55 */
rolf.meyer@arm.com 11:1c1ebd0324fa 56 CANMessage(int _id, CANFormat _format = CANStandard) {
rolf.meyer@arm.com 11:1c1ebd0324fa 57 len = 0;
rolf.meyer@arm.com 11:1c1ebd0324fa 58 type = CANRemote;
rolf.meyer@arm.com 11:1c1ebd0324fa 59 format = _format;
rolf.meyer@arm.com 11:1c1ebd0324fa 60 id = _id;
rolf.meyer@arm.com 11:1c1ebd0324fa 61 memset(data, 0, 8);
rolf.meyer@arm.com 11:1c1ebd0324fa 62 }
rolf.meyer@arm.com 11:1c1ebd0324fa 63 };
rolf.meyer@arm.com 11:1c1ebd0324fa 64
emilmont 43:e2ed12d17f06 65 /** A can bus client, used for communicating with can devices
rolf.meyer@arm.com 11:1c1ebd0324fa 66 */
emilmont 44:24d45a770a51 67 class CAN {
rolf.meyer@arm.com 11:1c1ebd0324fa 68
rolf.meyer@arm.com 11:1c1ebd0324fa 69 public:
emilmont 43:e2ed12d17f06 70 /** Creates an CAN interface connected to specific pins.
emilmont 43:e2ed12d17f06 71 *
emilmont 43:e2ed12d17f06 72 * @param rd read from transmitter
emilmont 43:e2ed12d17f06 73 * @param td transmit to transmitter
rolf.meyer@arm.com 11:1c1ebd0324fa 74 *
rolf.meyer@arm.com 11:1c1ebd0324fa 75 * Example:
emilmont 43:e2ed12d17f06 76 * @code
emilmont 43:e2ed12d17f06 77 * #include "mbed.h"
emilmont 55:d722ed6a4237 78 *
emilmont 43:e2ed12d17f06 79 * Ticker ticker;
emilmont 43:e2ed12d17f06 80 * DigitalOut led1(LED1);
emilmont 43:e2ed12d17f06 81 * DigitalOut led2(LED2);
emilmont 43:e2ed12d17f06 82 * CAN can1(p9, p10);
emilmont 43:e2ed12d17f06 83 * CAN can2(p30, p29);
emilmont 55:d722ed6a4237 84 *
emilmont 43:e2ed12d17f06 85 * char counter = 0;
emilmont 55:d722ed6a4237 86 *
emilmont 43:e2ed12d17f06 87 * void send() {
emilmont 43:e2ed12d17f06 88 * if(can1.write(CANMessage(1337, &counter, 1))) {
emilmont 43:e2ed12d17f06 89 * printf("Message sent: %d\n", counter);
emilmont 43:e2ed12d17f06 90 * counter++;
emilmont 55:d722ed6a4237 91 * }
emilmont 43:e2ed12d17f06 92 * led1 = !led1;
emilmont 43:e2ed12d17f06 93 * }
emilmont 55:d722ed6a4237 94 *
emilmont 43:e2ed12d17f06 95 * int main() {
emilmont 43:e2ed12d17f06 96 * ticker.attach(&send, 1);
emilmont 43:e2ed12d17f06 97 * CANMessage msg;
emilmont 43:e2ed12d17f06 98 * while(1) {
emilmont 43:e2ed12d17f06 99 * if(can2.read(msg)) {
emilmont 43:e2ed12d17f06 100 * printf("Message received: %d\n\n", msg.data[0]);
emilmont 43:e2ed12d17f06 101 * led2 = !led2;
emilmont 55:d722ed6a4237 102 * }
emilmont 43:e2ed12d17f06 103 * wait(0.2);
emilmont 43:e2ed12d17f06 104 * }
emilmont 55:d722ed6a4237 105 * }
emilmont 43:e2ed12d17f06 106 * @endcode
rolf.meyer@arm.com 11:1c1ebd0324fa 107 */
rolf.meyer@arm.com 11:1c1ebd0324fa 108 CAN(PinName rd, PinName td);
rolf.meyer@arm.com 11:1c1ebd0324fa 109 virtual ~CAN();
emilmont 55:d722ed6a4237 110
emilmont 43:e2ed12d17f06 111 /** Set the frequency of the CAN interface
emilmont 43:e2ed12d17f06 112 *
emilmont 43:e2ed12d17f06 113 * @param hz The bus frequency in hertz
rolf.meyer@arm.com 11:1c1ebd0324fa 114 *
emilmont 43:e2ed12d17f06 115 * @returns
emilmont 43:e2ed12d17f06 116 * 1 if successful,
emilmont 43:e2ed12d17f06 117 * 0 otherwise
rolf.meyer@arm.com 11:1c1ebd0324fa 118 */
simon 21:3944f1e2fa4f 119 int frequency(int hz);
emilmont 55:d722ed6a4237 120
emilmont 43:e2ed12d17f06 121 /** Write a CANMessage to the bus.
emilmont 43:e2ed12d17f06 122 *
emilmont 43:e2ed12d17f06 123 * @param msg The CANMessage to write.
rolf.meyer@arm.com 11:1c1ebd0324fa 124 *
emilmont 43:e2ed12d17f06 125 * @returns
emilmont 43:e2ed12d17f06 126 * 0 if write failed,
emilmont 43:e2ed12d17f06 127 * 1 if write was successful
rolf.meyer@arm.com 11:1c1ebd0324fa 128 */
rolf.meyer@arm.com 11:1c1ebd0324fa 129 int write(CANMessage msg);
emilmont 55:d722ed6a4237 130
emilmont 43:e2ed12d17f06 131 /** Read a CANMessage from the bus.
emilmont 55:d722ed6a4237 132 *
emilmont 43:e2ed12d17f06 133 * @param msg A CANMessage to read to.
rolf.meyer@arm.com 11:1c1ebd0324fa 134 *
emilmont 43:e2ed12d17f06 135 * @returns
emilmont 43:e2ed12d17f06 136 * 0 if no message arrived,
emilmont 43:e2ed12d17f06 137 * 1 if message arrived
rolf.meyer@arm.com 11:1c1ebd0324fa 138 */
rolf.meyer@arm.com 11:1c1ebd0324fa 139 int read(CANMessage &msg);
emilmont 55:d722ed6a4237 140
emilmont 43:e2ed12d17f06 141 /** Reset CAN interface.
rolf.meyer@arm.com 11:1c1ebd0324fa 142 *
rolf.meyer@arm.com 11:1c1ebd0324fa 143 * To use after error overflow.
rolf.meyer@arm.com 11:1c1ebd0324fa 144 */
rolf.meyer@arm.com 11:1c1ebd0324fa 145 void reset();
emilmont 55:d722ed6a4237 146
emilmont 43:e2ed12d17f06 147 /** Puts or removes the CAN interface into silent monitoring mode
simon 22:9114680c05da 148 *
emilmont 43:e2ed12d17f06 149 * @param silent boolean indicating whether to go into silent mode or not
simon 22:9114680c05da 150 */
simon 22:9114680c05da 151 void monitor(bool silent);
emilmont 55:d722ed6a4237 152
emilmont 43:e2ed12d17f06 153 /** Returns number of read errors to detect read overflow errors.
rolf.meyer@arm.com 11:1c1ebd0324fa 154 */
rolf.meyer@arm.com 11:1c1ebd0324fa 155 unsigned char rderror();
rolf.meyer@arm.com 11:1c1ebd0324fa 156
emilmont 43:e2ed12d17f06 157 /** Returns number of write errors to detect write overflow errors.
rolf.meyer@arm.com 11:1c1ebd0324fa 158 */
rolf.meyer@arm.com 11:1c1ebd0324fa 159 unsigned char tderror();
simon 22:9114680c05da 160
emilmont 43:e2ed12d17f06 161 /** Attach a function to call whenever a CAN frame received interrupt is
simon 22:9114680c05da 162 * generated.
simon 22:9114680c05da 163 *
emilmont 43:e2ed12d17f06 164 * @param fptr A pointer to a void function, or 0 to set as none
simon 22:9114680c05da 165 */
simon 22:9114680c05da 166 void attach(void (*fptr)(void));
emilmont 55:d722ed6a4237 167
emilmont 43:e2ed12d17f06 168 /** Attach a member function to call whenever a CAN frame received interrupt
simon 22:9114680c05da 169 * is generated.
simon 22:9114680c05da 170 *
emilmont 43:e2ed12d17f06 171 * @param tptr pointer to the object to call the member function on
emilmont 43:e2ed12d17f06 172 * @param mptr pointer to the member function to be called
simon 22:9114680c05da 173 */
simon 22:9114680c05da 174 template<typename T>
emilmont 33:5364839841bd 175 void attach(T* tptr, void (T::*mptr)(void)) {
emilmont 33:5364839841bd 176 if((mptr != NULL) && (tptr != NULL)) {
emilmont 33:5364839841bd 177 _rxirq.attach(tptr, mptr);
emilmont 33:5364839841bd 178 setup_interrupt();
emilmont 33:5364839841bd 179 } else {
emilmont 33:5364839841bd 180 remove_interrupt();
emilmont 33:5364839841bd 181 }
emilmont 33:5364839841bd 182 }
emilmont 55:d722ed6a4237 183
rolf.meyer@arm.com 11:1c1ebd0324fa 184 private:
emilmont 44:24d45a770a51 185 can_t _can;
simon 22:9114680c05da 186 FunctionPointer _rxirq;
emilmont 55:d722ed6a4237 187
simon 22:9114680c05da 188 void setup_interrupt(void);
simon 22:9114680c05da 189 void remove_interrupt(void);
rolf.meyer@arm.com 11:1c1ebd0324fa 190 };
rolf.meyer@arm.com 11:1c1ebd0324fa 191
rolf.meyer@arm.com 11:1c1ebd0324fa 192 } // namespace mbed
rolf.meyer@arm.com 11:1c1ebd0324fa 193
emilmont 44:24d45a770a51 194 #endif
emilmont 27:7110ebee3484 195
emilmont 44:24d45a770a51 196 #endif // MBED_CAN_H