initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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