test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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