mbed library sources, include can_api for nucleo-f091rc

Dependents:   CanNucleoF0_example

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Feb 18 11:44:18 2013 +0000
Revision:
2:143cac498751
Parent:
0:fd0d7bdfcdc2
Update mbed sources to Rev 59

Who changed what in which revision?

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