SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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