This is a repository for all my programs or modified programs.

Committer:
mturner5
Date:
Sun Sep 11 23:48:09 2016 +0000
Revision:
0:72480818e4a9
Made delays for the LEDS and button presses

Who changed what in which revision?

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