Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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