test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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