Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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