Please use mbed-src instead of this library.mbed-src supports GR-PEACH rev.C. mbed-srcライブラリをご利用ください。mbed-srcはGR-PEACH rev.Cに対応しています。

Fork of mbed-src_GR-PEACH_rev_c by GR-PEACH_producer_meeting

Committer:
bogdanm
Date:
Mon Aug 05 14:12:34 2013 +0300
Revision:
13:0645d8841f51
Parent:
10:3bc89ef62ce7
Child:
15:4892fe388435
Update mbed sources to revision 64

Who changed what in which revision?

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