Support for MSP430 launchpad.

Fork of mbed by mbed official

Committer:
rolf.meyer@arm.com
Date:
Fri Aug 28 12:10:11 2009 +0000
Revision:
11:1c1ebd0324fa
Child:
12:f63353af7be8
A shiny new version

Who changed what in which revision?

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