This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Tue Jul 23 14:01:42 2013 +0000
Revision:
1:ac68f0368a77
Parent:
0:978110f7f027
Other accelerometer added

Who changed what in which revision?

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