SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lharoon 0:22612ae617a0 1 /* mbed Microcontroller Library - I2C
lharoon 0:22612ae617a0 2 * Copyright (c) 2007-2011 ARM Limited. All rights reserved.
lharoon 0:22612ae617a0 3 */
lharoon 0:22612ae617a0 4
lharoon 0:22612ae617a0 5 #ifndef MBED_I2C_H
lharoon 0:22612ae617a0 6 #define MBED_I2C_H
lharoon 0:22612ae617a0 7
lharoon 0:22612ae617a0 8 #include "device.h"
lharoon 0:22612ae617a0 9
lharoon 0:22612ae617a0 10 #if DEVICE_I2C
lharoon 0:22612ae617a0 11
lharoon 0:22612ae617a0 12 #include "platform.h"
lharoon 0:22612ae617a0 13 #include "PinNames.h"
lharoon 0:22612ae617a0 14 #include "PeripheralNames.h"
lharoon 0:22612ae617a0 15 #include "Base.h"
lharoon 0:22612ae617a0 16
lharoon 0:22612ae617a0 17 namespace mbed {
lharoon 0:22612ae617a0 18
lharoon 0:22612ae617a0 19 /* Class: I2C
lharoon 0:22612ae617a0 20 * An I2C Master, used for communicating with I2C slave devices
lharoon 0:22612ae617a0 21 *
lharoon 0:22612ae617a0 22 * Example:
lharoon 0:22612ae617a0 23 * > // Read from I2C slave at address 0x62
lharoon 0:22612ae617a0 24 * >
lharoon 0:22612ae617a0 25 * > #include "mbed.h"
lharoon 0:22612ae617a0 26 * >
lharoon 0:22612ae617a0 27 * > I2C i2c(p28, p27);
lharoon 0:22612ae617a0 28 * >
lharoon 0:22612ae617a0 29 * > int main() {
lharoon 0:22612ae617a0 30 * > int address = 0x62;
lharoon 0:22612ae617a0 31 * > char data[2];
lharoon 0:22612ae617a0 32 * > i2c.read(address, data, 2);
lharoon 0:22612ae617a0 33 * > }
lharoon 0:22612ae617a0 34 */
lharoon 0:22612ae617a0 35 class I2C : public Base {
lharoon 0:22612ae617a0 36
lharoon 0:22612ae617a0 37 public:
lharoon 0:22612ae617a0 38
lharoon 0:22612ae617a0 39 enum RxStatus {
lharoon 0:22612ae617a0 40 NoData
lharoon 0:22612ae617a0 41 , MasterGeneralCall
lharoon 0:22612ae617a0 42 , MasterWrite
lharoon 0:22612ae617a0 43 , MasterRead
lharoon 0:22612ae617a0 44 };
lharoon 0:22612ae617a0 45
lharoon 0:22612ae617a0 46 enum Acknowledge {
lharoon 0:22612ae617a0 47 NoACK = 0
lharoon 0:22612ae617a0 48 , ACK = 1
lharoon 0:22612ae617a0 49 };
lharoon 0:22612ae617a0 50
lharoon 0:22612ae617a0 51 /* Constructor: I2C
lharoon 0:22612ae617a0 52 * Create an I2C Master interface, connected to the specified pins
lharoon 0:22612ae617a0 53 *
lharoon 0:22612ae617a0 54 * Variables:
lharoon 0:22612ae617a0 55 * sda - I2C data line pin
lharoon 0:22612ae617a0 56 * scl - I2C clock line pin
lharoon 0:22612ae617a0 57 */
lharoon 0:22612ae617a0 58 I2C(PinName sda, PinName scl, const char *name = NULL);
lharoon 0:22612ae617a0 59
lharoon 0:22612ae617a0 60 /* Function: frequency
lharoon 0:22612ae617a0 61 * Set the frequency of the I2C interface
lharoon 0:22612ae617a0 62 *
lharoon 0:22612ae617a0 63 * Variables:
lharoon 0:22612ae617a0 64 * hz - The bus frequency in hertz
lharoon 0:22612ae617a0 65 */
lharoon 0:22612ae617a0 66 void frequency(int hz);
lharoon 0:22612ae617a0 67
lharoon 0:22612ae617a0 68 /* Function: read
lharoon 0:22612ae617a0 69 * Read from an I2C slave
lharoon 0:22612ae617a0 70 *
lharoon 0:22612ae617a0 71 * Performs a complete read transaction. The bottom bit of
lharoon 0:22612ae617a0 72 * the address is forced to 1 to indicate a read.
lharoon 0:22612ae617a0 73 *
lharoon 0:22612ae617a0 74 * Variables:
lharoon 0:22612ae617a0 75 * address - 8-bit I2C slave address [ addr | 1 ]
lharoon 0:22612ae617a0 76 * data - Pointer to the byte-array to read data in to
lharoon 0:22612ae617a0 77 * length - Number of bytes to read
lharoon 0:22612ae617a0 78 * repeated - Repeated start, true - don't send stop at end
lharoon 0:22612ae617a0 79 * returns - 0 on success (ack), or non-0 on failure (nack)
lharoon 0:22612ae617a0 80 */
lharoon 0:22612ae617a0 81 int read(int address, char *data, int length, bool repeated = false);
lharoon 0:22612ae617a0 82
lharoon 0:22612ae617a0 83 /* Function: read
lharoon 0:22612ae617a0 84 * Read a single byte from the I2C bus
lharoon 0:22612ae617a0 85 *
lharoon 0:22612ae617a0 86 * Variables:
lharoon 0:22612ae617a0 87 * ack - indicates if the byte is to be acknowledged (1 = acknowledge)
lharoon 0:22612ae617a0 88 * returns - the byte read
lharoon 0:22612ae617a0 89 */
lharoon 0:22612ae617a0 90 int read(int ack);
lharoon 0:22612ae617a0 91
lharoon 0:22612ae617a0 92 /* Function: write
lharoon 0:22612ae617a0 93 * Write to an I2C slave
lharoon 0:22612ae617a0 94 *
lharoon 0:22612ae617a0 95 * Performs a complete write transaction. The bottom bit of
lharoon 0:22612ae617a0 96 * the address is forced to 0 to indicate a write.
lharoon 0:22612ae617a0 97 *
lharoon 0:22612ae617a0 98 * Variables:
lharoon 0:22612ae617a0 99 * address - 8-bit I2C slave address [ addr | 0 ]
lharoon 0:22612ae617a0 100 * data - Pointer to the byte-array data to send
lharoon 0:22612ae617a0 101 * length - Number of bytes to send
lharoon 0:22612ae617a0 102 * repeated - Repeated start, true - do not send stop at end
lharoon 0:22612ae617a0 103 * returns - 0 on success (ack), or non-0 on failure (nack)
lharoon 0:22612ae617a0 104 */
lharoon 0:22612ae617a0 105 int write(int address, const char *data, int length, bool repeated = false);
lharoon 0:22612ae617a0 106
lharoon 0:22612ae617a0 107 /* Function: write
lharoon 0:22612ae617a0 108 * Write single byte out on the I2C bus
lharoon 0:22612ae617a0 109 *
lharoon 0:22612ae617a0 110 * Variables:
lharoon 0:22612ae617a0 111 * data - data to write out on bus
lharoon 0:22612ae617a0 112 * returns - a '1' if an ACK was received, a '0' otherwise
lharoon 0:22612ae617a0 113 */
lharoon 0:22612ae617a0 114 int write(int data);
lharoon 0:22612ae617a0 115
lharoon 0:22612ae617a0 116 /* Function: start
lharoon 0:22612ae617a0 117 * Creates a start condition on the I2C bus
lharoon 0:22612ae617a0 118 */
lharoon 0:22612ae617a0 119
lharoon 0:22612ae617a0 120 void start(void);
lharoon 0:22612ae617a0 121
lharoon 0:22612ae617a0 122 /* Function: stop
lharoon 0:22612ae617a0 123 * Creates a stop condition on the I2C bus
lharoon 0:22612ae617a0 124 */
lharoon 0:22612ae617a0 125 void stop(void);
lharoon 0:22612ae617a0 126
lharoon 0:22612ae617a0 127 protected:
lharoon 0:22612ae617a0 128
lharoon 0:22612ae617a0 129 void aquire();
lharoon 0:22612ae617a0 130
lharoon 0:22612ae617a0 131 I2CName _i2c;
lharoon 0:22612ae617a0 132 static I2C *_owner;
lharoon 0:22612ae617a0 133 int _hz;
lharoon 0:22612ae617a0 134
lharoon 0:22612ae617a0 135 };
lharoon 0:22612ae617a0 136
lharoon 0:22612ae617a0 137 } // namespace mbed
lharoon 0:22612ae617a0 138
lharoon 0:22612ae617a0 139 #endif
lharoon 0:22612ae617a0 140
lharoon 0:22612ae617a0 141 #endif
lharoon 0:22612ae617a0 142