DHT11

Committer:
jhon309
Date:
Thu Aug 13 00:21:57 2015 +0000
Revision:
0:c52df770855b
DHT11

Who changed what in which revision?

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