mbed lib with startup delay fixed for Nucleo401RE

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Fri Jun 14 17:49:17 2013 +0100
Revision:
10:3bc89ef62ce7
Parent:
9:0ce32e54c9a7
Child:
13:0645d8841f51
Unify mbed library sources

Who changed what in which revision?

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