Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
ethaderu
Date:
Tue Mar 05 02:34:44 2019 +0000
Revision:
3:78f223d34f36
Publish 1

Who changed what in which revision?

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