Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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