mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
bogdanm
Date:
Wed Aug 07 16:43:59 2013 +0300
Revision:
15:4892fe388435
Parent:
13:0645d8841f51
Child:
41:e8b66477f5bf
Added LPC4088 target and interrupt chaining code

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 */
bogdanm 15:4892fe388435 36 CANMessage() {
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.
bogdanm 15:4892fe388435 134 *
bogdanm 15:4892fe388435 135 * @returns
bogdanm 15:4892fe388435 136 * 0 if no message arrived,
bogdanm 15:4892fe388435 137 * 1 if message arrived
bogdanm 15:4892fe388435 138 */
bogdanm 15:4892fe388435 139 int read(CANMessage &msg);
bogdanm 15:4892fe388435 140
bogdanm 15:4892fe388435 141 /** Reset CAN interface.
bogdanm 15:4892fe388435 142 *
bogdanm 15:4892fe388435 143 * To use after error overflow.
bogdanm 15:4892fe388435 144 */
bogdanm 15:4892fe388435 145 void reset();
bogdanm 15:4892fe388435 146
bogdanm 15:4892fe388435 147 /** Puts or removes the CAN interface into silent monitoring mode
bogdanm 15:4892fe388435 148 *
bogdanm 15:4892fe388435 149 * @param silent boolean indicating whether to go into silent mode or not
bogdanm 15:4892fe388435 150 */
bogdanm 15:4892fe388435 151 void monitor(bool silent);
bogdanm 15:4892fe388435 152
bogdanm 15:4892fe388435 153 enum Mode {
bogdanm 15:4892fe388435 154 Reset = 0,
bogdanm 15:4892fe388435 155 Normal,
bogdanm 15:4892fe388435 156 Silent,
bogdanm 15:4892fe388435 157 LocalTest,
bogdanm 15:4892fe388435 158 GlobalTest,
bogdanm 15:4892fe388435 159 SilentTest
bogdanm 15:4892fe388435 160 };
bogdanm 15:4892fe388435 161
bogdanm 15:4892fe388435 162 /** Change CAN operation to the specified mode
bogdanm 15:4892fe388435 163 *
bogdanm 15:4892fe388435 164 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
bogdanm 15:4892fe388435 165 *
bogdanm 15:4892fe388435 166 * @returns
bogdanm 15:4892fe388435 167 * 0 if mode change failed or unsupported,
bogdanm 15:4892fe388435 168 * 1 if mode change was successful
bogdanm 15:4892fe388435 169 */
bogdanm 15:4892fe388435 170 int mode(Mode mode);
bogdanm 15:4892fe388435 171
bogdanm 15:4892fe388435 172 /** Returns number of read errors to detect read overflow errors.
bogdanm 15:4892fe388435 173 */
bogdanm 15:4892fe388435 174 unsigned char rderror();
bogdanm 15:4892fe388435 175
bogdanm 15:4892fe388435 176 /** Returns number of write errors to detect write overflow errors.
bogdanm 15:4892fe388435 177 */
bogdanm 15:4892fe388435 178 unsigned char tderror();
bogdanm 15:4892fe388435 179
bogdanm 15:4892fe388435 180 enum IrqType {
bogdanm 15:4892fe388435 181 RxIrq = 0,
bogdanm 15:4892fe388435 182 TxIrq,
bogdanm 15:4892fe388435 183 EwIrq,
bogdanm 15:4892fe388435 184 DoIrq,
bogdanm 15:4892fe388435 185 WuIrq,
bogdanm 15:4892fe388435 186 EpIrq,
bogdanm 15:4892fe388435 187 AlIrq,
bogdanm 15:4892fe388435 188 BeIrq,
bogdanm 15:4892fe388435 189 IdIrq
bogdanm 15:4892fe388435 190 };
bogdanm 15:4892fe388435 191
bogdanm 15:4892fe388435 192 /** Attach a function to call whenever a CAN frame received interrupt is
bogdanm 15:4892fe388435 193 * generated.
bogdanm 15:4892fe388435 194 *
bogdanm 15:4892fe388435 195 * @param fptr A pointer to a void function, or 0 to set as none
bogdanm 15:4892fe388435 196 * @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 197 */
bogdanm 15:4892fe388435 198 void attach(void (*fptr)(void), IrqType type=RxIrq);
bogdanm 15:4892fe388435 199
bogdanm 15:4892fe388435 200 /** Attach a member function to call whenever a CAN frame received interrupt
bogdanm 15:4892fe388435 201 * is generated.
bogdanm 15:4892fe388435 202 *
bogdanm 15:4892fe388435 203 * @param tptr pointer to the object to call the member function on
bogdanm 15:4892fe388435 204 * @param mptr pointer to the member function to be called
bogdanm 15:4892fe388435 205 * @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 206 */
bogdanm 15:4892fe388435 207 template<typename T>
bogdanm 15:4892fe388435 208 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
bogdanm 15:4892fe388435 209 if((mptr != NULL) && (tptr != NULL)) {
bogdanm 15:4892fe388435 210 _irq[type].attach(tptr, mptr);
bogdanm 15:4892fe388435 211 can_irq_set(&_can, (CanIrqType)type, 1);
bogdanm 15:4892fe388435 212 }
bogdanm 15:4892fe388435 213 else {
bogdanm 15:4892fe388435 214 can_irq_set(&_can, (CanIrqType)type, 0);
bogdanm 15:4892fe388435 215 }
bogdanm 15:4892fe388435 216 }
bogdanm 15:4892fe388435 217
bogdanm 15:4892fe388435 218 static void _irq_handler(uint32_t id, CanIrqType type);
bogdanm 15:4892fe388435 219
bogdanm 15:4892fe388435 220 protected:
bogdanm 15:4892fe388435 221 can_t _can;
bogdanm 15:4892fe388435 222 FunctionPointer _irq[9];
bogdanm 15:4892fe388435 223 };
bogdanm 15:4892fe388435 224
bogdanm 15:4892fe388435 225 } // namespace mbed
bogdanm 15:4892fe388435 226
bogdanm 15:4892fe388435 227 #endif
bogdanm 15:4892fe388435 228
bogdanm 15:4892fe388435 229 #endif // MBED_CAN_H