mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon May 26 18:15:07 2014 +0100
Revision:
212:34d62c0b2af6
Parent:
41:e8b66477f5bf
Synchronized with git revision f65b7d907785089e8dd942446f1c974b3a213a39

Full URL: https://github.com/mbedmicro/mbed/commit/f65b7d907785089e8dd942446f1c974b3a213a39/

Who changed what in which revision?

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