nothing changed

Fork of mbed by gokmen ascioglu

Committer:
gokmenascioglu
Date:
Sat Sep 22 10:32:07 2012 +0000
Revision:
0:a8fa94490a0a
alican library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gokmenascioglu 0:a8fa94490a0a 1 /* mbed Microcontroller Library - can
gokmenascioglu 0:a8fa94490a0a 2 * Copyright (c) 2009-2011 ARM Limited. All rights reserved.
gokmenascioglu 0:a8fa94490a0a 3 */
gokmenascioglu 0:a8fa94490a0a 4
gokmenascioglu 0:a8fa94490a0a 5 #ifndef MBED_CAN_H
gokmenascioglu 0:a8fa94490a0a 6 #define MBED_CAN_H
gokmenascioglu 0:a8fa94490a0a 7
gokmenascioglu 0:a8fa94490a0a 8 #include "device.h"
gokmenascioglu 0:a8fa94490a0a 9
gokmenascioglu 0:a8fa94490a0a 10 #if DEVICE_CAN
gokmenascioglu 0:a8fa94490a0a 11
gokmenascioglu 0:a8fa94490a0a 12 #include "Base.h"
gokmenascioglu 0:a8fa94490a0a 13 #include "platform.h"
gokmenascioglu 0:a8fa94490a0a 14 #include "PinNames.h"
gokmenascioglu 0:a8fa94490a0a 15 #include "PeripheralNames.h"
gokmenascioglu 0:a8fa94490a0a 16
gokmenascioglu 0:a8fa94490a0a 17 #include "can_helper.h"
gokmenascioglu 0:a8fa94490a0a 18 #include "FunctionPointer.h"
gokmenascioglu 0:a8fa94490a0a 19
gokmenascioglu 0:a8fa94490a0a 20 #include <string.h>
gokmenascioglu 0:a8fa94490a0a 21
gokmenascioglu 0:a8fa94490a0a 22 namespace mbed {
gokmenascioglu 0:a8fa94490a0a 23
gokmenascioglu 0:a8fa94490a0a 24 /* Class: CANMessage
gokmenascioglu 0:a8fa94490a0a 25 *
gokmenascioglu 0:a8fa94490a0a 26 */
gokmenascioglu 0:a8fa94490a0a 27 class CANMessage : public CAN_Message {
gokmenascioglu 0:a8fa94490a0a 28
gokmenascioglu 0:a8fa94490a0a 29 public:
gokmenascioglu 0:a8fa94490a0a 30
gokmenascioglu 0:a8fa94490a0a 31 /* Constructor: CANMessage
gokmenascioglu 0:a8fa94490a0a 32 * Creates empty CAN message.
gokmenascioglu 0:a8fa94490a0a 33 */
gokmenascioglu 0:a8fa94490a0a 34 CANMessage() {
gokmenascioglu 0:a8fa94490a0a 35 len = 8;
gokmenascioglu 0:a8fa94490a0a 36 type = CANData;
gokmenascioglu 0:a8fa94490a0a 37 format = CANStandard;
gokmenascioglu 0:a8fa94490a0a 38 id = 0;
gokmenascioglu 0:a8fa94490a0a 39 memset(data, 0, 8);
gokmenascioglu 0:a8fa94490a0a 40 }
gokmenascioglu 0:a8fa94490a0a 41
gokmenascioglu 0:a8fa94490a0a 42 /* Constructor: CANMessage
gokmenascioglu 0:a8fa94490a0a 43 * Creates CAN message with specific content.
gokmenascioglu 0:a8fa94490a0a 44 */
gokmenascioglu 0:a8fa94490a0a 45 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
gokmenascioglu 0:a8fa94490a0a 46 len = _len & 0xF;
gokmenascioglu 0:a8fa94490a0a 47 type = _type;
gokmenascioglu 0:a8fa94490a0a 48 format = _format;
gokmenascioglu 0:a8fa94490a0a 49 id = _id;
gokmenascioglu 0:a8fa94490a0a 50 memcpy(data, _data, _len);
gokmenascioglu 0:a8fa94490a0a 51 }
gokmenascioglu 0:a8fa94490a0a 52
gokmenascioglu 0:a8fa94490a0a 53 /* Constructor: CANMessage
gokmenascioglu 0:a8fa94490a0a 54 * Creates CAN remote message.
gokmenascioglu 0:a8fa94490a0a 55 */
gokmenascioglu 0:a8fa94490a0a 56 CANMessage(int _id, CANFormat _format = CANStandard) {
gokmenascioglu 0:a8fa94490a0a 57 len = 0;
gokmenascioglu 0:a8fa94490a0a 58 type = CANRemote;
gokmenascioglu 0:a8fa94490a0a 59 format = _format;
gokmenascioglu 0:a8fa94490a0a 60 id = _id;
gokmenascioglu 0:a8fa94490a0a 61 memset(data, 0, 8);
gokmenascioglu 0:a8fa94490a0a 62 }
gokmenascioglu 0:a8fa94490a0a 63 #if 0 // Inhereted from CAN_Message, for documentation only
gokmenascioglu 0:a8fa94490a0a 64
gokmenascioglu 0:a8fa94490a0a 65 /* Variable: id
gokmenascioglu 0:a8fa94490a0a 66 * The message id.
gokmenascioglu 0:a8fa94490a0a 67 *
gokmenascioglu 0:a8fa94490a0a 68 * If format is CANStandard it must be an 11 bit long id
gokmenascioglu 0:a8fa94490a0a 69 * If format is CANExtended it must be an 29 bit long id
gokmenascioglu 0:a8fa94490a0a 70 */
gokmenascioglu 0:a8fa94490a0a 71 unsigned int id;
gokmenascioglu 0:a8fa94490a0a 72
gokmenascioglu 0:a8fa94490a0a 73 /* Variable: data
gokmenascioglu 0:a8fa94490a0a 74 * Space for 8 byte payload.
gokmenascioglu 0:a8fa94490a0a 75 *
gokmenascioglu 0:a8fa94490a0a 76 * If type is CANData data can store up to 8 byte data.
gokmenascioglu 0:a8fa94490a0a 77 */
gokmenascioglu 0:a8fa94490a0a 78 unsigned char data[8];
gokmenascioglu 0:a8fa94490a0a 79
gokmenascioglu 0:a8fa94490a0a 80 /* Variable: len
gokmenascioglu 0:a8fa94490a0a 81 * Length of data in bytes.
gokmenascioglu 0:a8fa94490a0a 82 *
gokmenascioglu 0:a8fa94490a0a 83 * If type is CANData data can store up to 8 byte data.
gokmenascioglu 0:a8fa94490a0a 84 */
gokmenascioglu 0:a8fa94490a0a 85 unsigned char len;
gokmenascioglu 0:a8fa94490a0a 86
gokmenascioglu 0:a8fa94490a0a 87 /* Variable: format
gokmenascioglu 0:a8fa94490a0a 88 * Defines if the message has standard or extended format.
gokmenascioglu 0:a8fa94490a0a 89 *
gokmenascioglu 0:a8fa94490a0a 90 * Defines the type of message id:
gokmenascioglu 0:a8fa94490a0a 91 * Default is CANStandard which implies 11 bit id.
gokmenascioglu 0:a8fa94490a0a 92 * CANExtended means 29 bit message id.
gokmenascioglu 0:a8fa94490a0a 93 */
gokmenascioglu 0:a8fa94490a0a 94 CANFormat format;
gokmenascioglu 0:a8fa94490a0a 95
gokmenascioglu 0:a8fa94490a0a 96 /* Variable: type
gokmenascioglu 0:a8fa94490a0a 97 * Defines the type of a message.
gokmenascioglu 0:a8fa94490a0a 98 *
gokmenascioglu 0:a8fa94490a0a 99 * The message type can rather be CANData for a message with data (default).
gokmenascioglu 0:a8fa94490a0a 100 * Or CANRemote for a request of a specific CAN message.
gokmenascioglu 0:a8fa94490a0a 101 */
gokmenascioglu 0:a8fa94490a0a 102 CANType type; // 0 - DATA FRAME, 1 - REMOTE FRAME
gokmenascioglu 0:a8fa94490a0a 103 #endif
gokmenascioglu 0:a8fa94490a0a 104 };
gokmenascioglu 0:a8fa94490a0a 105
gokmenascioglu 0:a8fa94490a0a 106 /* Class: CAN
gokmenascioglu 0:a8fa94490a0a 107 * A can bus client, used for communicating with can devices
gokmenascioglu 0:a8fa94490a0a 108 */
gokmenascioglu 0:a8fa94490a0a 109 class CAN : public Base {
gokmenascioglu 0:a8fa94490a0a 110
gokmenascioglu 0:a8fa94490a0a 111 public:
gokmenascioglu 0:a8fa94490a0a 112
gokmenascioglu 0:a8fa94490a0a 113 /* Constructor: CAN
gokmenascioglu 0:a8fa94490a0a 114 * Creates an CAN interface connected to specific pins.
gokmenascioglu 0:a8fa94490a0a 115 *
gokmenascioglu 0:a8fa94490a0a 116 * Example:
gokmenascioglu 0:a8fa94490a0a 117 * > #include "mbed.h"
gokmenascioglu 0:a8fa94490a0a 118 * >
gokmenascioglu 0:a8fa94490a0a 119 * > Ticker ticker;
gokmenascioglu 0:a8fa94490a0a 120 * > DigitalOut led1(LED1);
gokmenascioglu 0:a8fa94490a0a 121 * > DigitalOut led2(LED2);
gokmenascioglu 0:a8fa94490a0a 122 * > CAN can1(p9, p10);
gokmenascioglu 0:a8fa94490a0a 123 * > CAN can2(p30, p29);
gokmenascioglu 0:a8fa94490a0a 124 * >
gokmenascioglu 0:a8fa94490a0a 125 * > char counter = 0;
gokmenascioglu 0:a8fa94490a0a 126 * >
gokmenascioglu 0:a8fa94490a0a 127 * > void send() {
gokmenascioglu 0:a8fa94490a0a 128 * > if(can1.write(CANMessage(1337, &counter, 1))) {
gokmenascioglu 0:a8fa94490a0a 129 * > printf("Message sent: %d\n", counter);
gokmenascioglu 0:a8fa94490a0a 130 * > counter++;
gokmenascioglu 0:a8fa94490a0a 131 * > }
gokmenascioglu 0:a8fa94490a0a 132 * > led1 = !led1;
gokmenascioglu 0:a8fa94490a0a 133 * > }
gokmenascioglu 0:a8fa94490a0a 134 * >
gokmenascioglu 0:a8fa94490a0a 135 * > int main() {
gokmenascioglu 0:a8fa94490a0a 136 * > ticker.attach(&send, 1);
gokmenascioglu 0:a8fa94490a0a 137 * > CANMessage msg;
gokmenascioglu 0:a8fa94490a0a 138 * > while(1) {
gokmenascioglu 0:a8fa94490a0a 139 * > if(can2.read(msg)) {
gokmenascioglu 0:a8fa94490a0a 140 * > printf("Message received: %d\n\n", msg.data[0]);
gokmenascioglu 0:a8fa94490a0a 141 * > led2 = !led2;
gokmenascioglu 0:a8fa94490a0a 142 * > }
gokmenascioglu 0:a8fa94490a0a 143 * > wait(0.2);
gokmenascioglu 0:a8fa94490a0a 144 * > }
gokmenascioglu 0:a8fa94490a0a 145 * > }
gokmenascioglu 0:a8fa94490a0a 146 *
gokmenascioglu 0:a8fa94490a0a 147 * Variables:
gokmenascioglu 0:a8fa94490a0a 148 * rd - read from transmitter
gokmenascioglu 0:a8fa94490a0a 149 * td - transmit to transmitter
gokmenascioglu 0:a8fa94490a0a 150 */
gokmenascioglu 0:a8fa94490a0a 151 CAN(PinName rd, PinName td);
gokmenascioglu 0:a8fa94490a0a 152 virtual ~CAN();
gokmenascioglu 0:a8fa94490a0a 153
gokmenascioglu 0:a8fa94490a0a 154 /* Function: frequency
gokmenascioglu 0:a8fa94490a0a 155 * Set the frequency of the CAN interface
gokmenascioglu 0:a8fa94490a0a 156 *
gokmenascioglu 0:a8fa94490a0a 157 * Variables:
gokmenascioglu 0:a8fa94490a0a 158 * hz - The bus frequency in hertz
gokmenascioglu 0:a8fa94490a0a 159 * returns - 1 if successful, 0 otherwise
gokmenascioglu 0:a8fa94490a0a 160 */
gokmenascioglu 0:a8fa94490a0a 161 int frequency(int hz);
gokmenascioglu 0:a8fa94490a0a 162
gokmenascioglu 0:a8fa94490a0a 163 /* Function: write
gokmenascioglu 0:a8fa94490a0a 164 * Write a CANMessage to the bus.
gokmenascioglu 0:a8fa94490a0a 165 *
gokmenascioglu 0:a8fa94490a0a 166 * Variables:
gokmenascioglu 0:a8fa94490a0a 167 * msg - The CANMessage to write.
gokmenascioglu 0:a8fa94490a0a 168 *
gokmenascioglu 0:a8fa94490a0a 169 * Returns:
gokmenascioglu 0:a8fa94490a0a 170 * 0 - If write failed.
gokmenascioglu 0:a8fa94490a0a 171 * 1 - If write was successful.
gokmenascioglu 0:a8fa94490a0a 172 */
gokmenascioglu 0:a8fa94490a0a 173 int write(CANMessage msg);
gokmenascioglu 0:a8fa94490a0a 174
gokmenascioglu 0:a8fa94490a0a 175 /* Function: read
gokmenascioglu 0:a8fa94490a0a 176 * Read a CANMessage from the bus.
gokmenascioglu 0:a8fa94490a0a 177 *
gokmenascioglu 0:a8fa94490a0a 178 * Variables:
gokmenascioglu 0:a8fa94490a0a 179 * msg - A CANMessage to read to.
gokmenascioglu 0:a8fa94490a0a 180 *
gokmenascioglu 0:a8fa94490a0a 181 * Returns:
gokmenascioglu 0:a8fa94490a0a 182 * 0 - If no message arrived.
gokmenascioglu 0:a8fa94490a0a 183 * 1 - If message arrived.
gokmenascioglu 0:a8fa94490a0a 184 */
gokmenascioglu 0:a8fa94490a0a 185 int read(CANMessage &msg);
gokmenascioglu 0:a8fa94490a0a 186
gokmenascioglu 0:a8fa94490a0a 187 /* Function: reset
gokmenascioglu 0:a8fa94490a0a 188 * Reset CAN interface.
gokmenascioglu 0:a8fa94490a0a 189 *
gokmenascioglu 0:a8fa94490a0a 190 * To use after error overflow.
gokmenascioglu 0:a8fa94490a0a 191 */
gokmenascioglu 0:a8fa94490a0a 192 void reset();
gokmenascioglu 0:a8fa94490a0a 193
gokmenascioglu 0:a8fa94490a0a 194 /* Function: monitor
gokmenascioglu 0:a8fa94490a0a 195 * Puts or removes the CAN interface into silent monitoring mode
gokmenascioglu 0:a8fa94490a0a 196 *
gokmenascioglu 0:a8fa94490a0a 197 * Variables:
gokmenascioglu 0:a8fa94490a0a 198 * silent - boolean indicating whether to go into silent mode or not
gokmenascioglu 0:a8fa94490a0a 199 */
gokmenascioglu 0:a8fa94490a0a 200 void monitor(bool silent);
gokmenascioglu 0:a8fa94490a0a 201
gokmenascioglu 0:a8fa94490a0a 202 /* Function: rderror
gokmenascioglu 0:a8fa94490a0a 203 * Returns number of read errors to detect read overflow errors.
gokmenascioglu 0:a8fa94490a0a 204 */
gokmenascioglu 0:a8fa94490a0a 205 unsigned char rderror();
gokmenascioglu 0:a8fa94490a0a 206
gokmenascioglu 0:a8fa94490a0a 207 /* Function: tderror
gokmenascioglu 0:a8fa94490a0a 208 * Returns number of write errors to detect write overflow errors.
gokmenascioglu 0:a8fa94490a0a 209 */
gokmenascioglu 0:a8fa94490a0a 210 unsigned char tderror();
gokmenascioglu 0:a8fa94490a0a 211
gokmenascioglu 0:a8fa94490a0a 212 /* Function: attach
gokmenascioglu 0:a8fa94490a0a 213 * Attach a function to call whenever a CAN frame received interrupt is
gokmenascioglu 0:a8fa94490a0a 214 * generated.
gokmenascioglu 0:a8fa94490a0a 215 *
gokmenascioglu 0:a8fa94490a0a 216 * Variables:
gokmenascioglu 0:a8fa94490a0a 217 * fptr - A pointer to a void function, or 0 to set as none
gokmenascioglu 0:a8fa94490a0a 218 */
gokmenascioglu 0:a8fa94490a0a 219 void attach(void (*fptr)(void));
gokmenascioglu 0:a8fa94490a0a 220
gokmenascioglu 0:a8fa94490a0a 221 /* Function attach
gokmenascioglu 0:a8fa94490a0a 222 * Attach a member function to call whenever a CAN frame received interrupt
gokmenascioglu 0:a8fa94490a0a 223 * is generated.
gokmenascioglu 0:a8fa94490a0a 224 *
gokmenascioglu 0:a8fa94490a0a 225 * Variables:
gokmenascioglu 0:a8fa94490a0a 226 * tptr - pointer to the object to call the member function on
gokmenascioglu 0:a8fa94490a0a 227 * mptr - pointer to the member function to be called
gokmenascioglu 0:a8fa94490a0a 228 */
gokmenascioglu 0:a8fa94490a0a 229 template<typename T>
gokmenascioglu 0:a8fa94490a0a 230 void attach(T* tptr, void (T::*mptr)(void)) {
gokmenascioglu 0:a8fa94490a0a 231 if((mptr != NULL) && (tptr != NULL)) {
gokmenascioglu 0:a8fa94490a0a 232 _rxirq.attach(tptr, mptr);
gokmenascioglu 0:a8fa94490a0a 233 setup_interrupt();
gokmenascioglu 0:a8fa94490a0a 234 } else {
gokmenascioglu 0:a8fa94490a0a 235 remove_interrupt();
gokmenascioglu 0:a8fa94490a0a 236 }
gokmenascioglu 0:a8fa94490a0a 237 }
gokmenascioglu 0:a8fa94490a0a 238
gokmenascioglu 0:a8fa94490a0a 239 private:
gokmenascioglu 0:a8fa94490a0a 240
gokmenascioglu 0:a8fa94490a0a 241 CANName _id;
gokmenascioglu 0:a8fa94490a0a 242 FunctionPointer _rxirq;
gokmenascioglu 0:a8fa94490a0a 243
gokmenascioglu 0:a8fa94490a0a 244 void setup_interrupt(void);
gokmenascioglu 0:a8fa94490a0a 245 void remove_interrupt(void);
gokmenascioglu 0:a8fa94490a0a 246 };
gokmenascioglu 0:a8fa94490a0a 247
gokmenascioglu 0:a8fa94490a0a 248 } // namespace mbed
gokmenascioglu 0:a8fa94490a0a 249
gokmenascioglu 0:a8fa94490a0a 250 #endif // MBED_CAN_H
gokmenascioglu 0:a8fa94490a0a 251
gokmenascioglu 0:a8fa94490a0a 252 #endif