Lab 1 Program C
Fork of mbed by
I2C.h@11:1c1ebd0324fa, 2009-08-28 (annotated)
- Committer:
- rolf.meyer@arm.com
- Date:
- Fri Aug 28 12:10:11 2009 +0000
- Revision:
- 11:1c1ebd0324fa
- Parent:
- 4:5d1359a283bc
- Child:
- 12:f63353af7be8
A shiny new version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon.ford@mbed.co.uk | 0:82220227f4fa | 1 | /* mbed Microcontroller Library - I2C |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | * sford |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 4 | */ |
simon.ford@mbed.co.uk | 0:82220227f4fa | 5 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 6 | #ifndef MBED_I2C_H |
simon.ford@mbed.co.uk | 0:82220227f4fa | 7 | #define MBED_I2C_H |
simon.ford@mbed.co.uk | 0:82220227f4fa | 8 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 9 | #include "platform.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 10 | #include "PinNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 11 | #include "PeripheralNames.h" |
simon.ford@mbed.co.uk | 0:82220227f4fa | 12 | #include "Base.h" |
simon.ford@mbed.co.uk | 0:82220227f4fa | 13 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 14 | namespace mbed { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 15 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 16 | /* Class: I2C |
simon.ford@mbed.co.uk | 0:82220227f4fa | 17 | * An I2C Master, used for communicating with I2C slave devices |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 18 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 19 | * Example: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 20 | * > // Read from I2C slave at address 0x1234 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 21 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 22 | * > #include "mbed.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 23 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 24 | * > I2C i2c(p28, p27); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 25 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 26 | * > int main() { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 27 | * > int address = 0x1234; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 28 | * > char data[2]; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 29 | * > i2c.read(address,data,2); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 30 | * > // ... |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 31 | * > } |
simon.ford@mbed.co.uk | 0:82220227f4fa | 32 | */ |
simon.ford@mbed.co.uk | 0:82220227f4fa | 33 | class I2C : public Base { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 34 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 35 | public: |
simon.ford@mbed.co.uk | 0:82220227f4fa | 36 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 37 | /* Constructor: I2C |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 38 | * Create an I2C Master interface, connected to the specified pins |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 39 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 40 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 41 | * sda - I2C data line pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 42 | * scl - I2C clock line pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 43 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 44 | I2C(PinName sda, PinName scl, const char *name = NULL); |
simon.ford@mbed.co.uk | 0:82220227f4fa | 45 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 46 | /* Function: frequency |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 47 | * Set the frequency of the I2C interface |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 48 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 49 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 50 | * hz - The bus frequency in hertz |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 51 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 52 | void frequency(int hz); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 53 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 54 | /* Function: read |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 55 | * Read from an I2C slave |
simon.ford@mbed.co.uk | 0:82220227f4fa | 56 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 57 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 58 | * address - 7-bit I2C slave address (0-127) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 59 | * data - Pointer to the byte-array to read data in to |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 60 | * length - Number of bytes to read |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 61 | * returns - 0 on success (ack), or 1 on failure (nack) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 62 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 63 | int read(int address, char *data, int length); |
simon.ford@mbed.co.uk | 0:82220227f4fa | 64 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 65 | /* Function: write |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 66 | * Write to an I2C slave |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 67 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 68 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 69 | * address - 7-bit I2C slave address (0-127) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 70 | * data - Pointer to the byte-array data to send |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 71 | * length - Number of bytes to send |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | * returns - 0 on success (ack), or 1 on failure (nack) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 73 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 74 | int write(int address, const char *data, int length); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 75 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 76 | protected: |
simon.ford@mbed.co.uk | 0:82220227f4fa | 77 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 78 | I2CName _i2c; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 79 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 80 | void aquire(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 81 | static I2C *_owner; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 82 | int _hz; |
simon.ford@mbed.co.uk | 0:82220227f4fa | 83 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 84 | }; |
simon.ford@mbed.co.uk | 0:82220227f4fa | 85 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 86 | } // namespace mbed |
simon.ford@mbed.co.uk | 0:82220227f4fa | 87 | |
simon.ford@mbed.co.uk | 1:6b7f447ca868 | 88 | #endif |