5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
group-onsemi
Date:
Wed Jan 25 20:34:15 2017 +0000
Revision:
0:098463de4c5d
Initial commit

Who changed what in which revision?

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