LPC11U35 ADC Tick & USBSerial

Dependencies:   mbed

Dependents:   SmallDoseMeter_SingleCH_AE_lpc11u35_V1_00

Committer:
H_Tsunemoto
Date:
Mon Feb 19 08:51:33 2018 +0000
Revision:
0:871ab6846b18
test

Who changed what in which revision?

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