IOTIO

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Committer:
16038618
Date:
Sat Oct 29 15:11:28 2016 +0000
Revision:
1:4bdfa7d7e8bf
IOTIO

Who changed what in which revision?

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