I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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