mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Fri Oct 05 09:16:41 2012 +0000
Revision:
0:8024c367e29f
Child:
8:c14af7958ef5
First release of the mbed libraries for KL25Z

Who changed what in which revision?

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