test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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