Describes predefine macros for mbed online compiler (armcc)

Committer:
MACRUM
Date:
Thu Mar 16 21:58:09 2017 +0900
Revision:
6:40e873bbc5f7
Add licence header info

Who changed what in which revision?

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