Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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