mbed library sources. Supersedes mbed-src. RTC working even after reset

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

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