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