Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FastIO FastPWM USBDevice
BitBangI2C/BitBangI2C.h@82:4f6209cb5c33, 2017-04-13 (annotated)
- Committer:
- mjr
- Date:
- Thu Apr 13 23:20:28 2017 +0000
- Revision:
- 82:4f6209cb5c33
- Child:
- 86:e30a1f60f783
Plunger refactoring; AEDR-8300 added; TSL1401CL in progress; VL6180X added
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mjr | 82:4f6209cb5c33 | 1 | // Bit-bang I2C driver. This provides I2C functionality on arbitrary |
| mjr | 82:4f6209cb5c33 | 2 | // GPIO pins, allowing more flexibility in wiring an I2C device. Most |
| mjr | 82:4f6209cb5c33 | 3 | // MCUs with hardware I2C modules can only access the hardware I2C |
| mjr | 82:4f6209cb5c33 | 4 | // functionality through specific GPIO pins. |
| mjr | 82:4f6209cb5c33 | 5 | // |
| mjr | 82:4f6209cb5c33 | 6 | // Hardware I2C is preferable when available, and can be accessed through |
| mjr | 82:4f6209cb5c33 | 7 | // the mbed I2C class. The only reason to use this class rather than the |
| mjr | 82:4f6209cb5c33 | 8 | // native mbed I2C is that you need I2C functionality on pins that don't |
| mjr | 82:4f6209cb5c33 | 9 | // support I2C in the MCU hardware. |
| mjr | 82:4f6209cb5c33 | 10 | // |
| mjr | 82:4f6209cb5c33 | 11 | // This class isn't meant to be directly compatible with the mbed I2C class, |
| mjr | 82:4f6209cb5c33 | 12 | // but we try to adhere to the mbed conventions and method names to make it |
| mjr | 82:4f6209cb5c33 | 13 | // a mostly drop-in replacement. In particular, we use the mbed library's |
| mjr | 82:4f6209cb5c33 | 14 | // "2X" device address convention. |
| mjr | 82:4f6209cb5c33 | 15 | // |
| mjr | 82:4f6209cb5c33 | 16 | // IMPORTANT: Wherever a device address is specified, pass in 2X the nominal |
| mjr | 82:4f6209cb5c33 | 17 | // address. For example, if the device's actual address is 0x10, pass 0x20 |
| mjr | 82:4f6209cb5c33 | 18 | // for all 'addr' parameters. |
| mjr | 82:4f6209cb5c33 | 19 | |
| mjr | 82:4f6209cb5c33 | 20 | #ifndef _BITBANGI2C_H_ |
| mjr | 82:4f6209cb5c33 | 21 | #define _BITBANGI2C_H_ |
| mjr | 82:4f6209cb5c33 | 22 | |
| mjr | 82:4f6209cb5c33 | 23 | #include "mbed.h" |
| mjr | 82:4f6209cb5c33 | 24 | |
| mjr | 82:4f6209cb5c33 | 25 | class BitBangI2C |
| mjr | 82:4f6209cb5c33 | 26 | { |
| mjr | 82:4f6209cb5c33 | 27 | public: |
| mjr | 82:4f6209cb5c33 | 28 | // create the interface |
| mjr | 82:4f6209cb5c33 | 29 | BitBangI2C(PinName sda, PinName scl); |
| mjr | 82:4f6209cb5c33 | 30 | |
| mjr | 82:4f6209cb5c33 | 31 | // set the bus frequency in Hz |
| mjr | 82:4f6209cb5c33 | 32 | void setFrequency(uint32_t freq); |
| mjr | 82:4f6209cb5c33 | 33 | |
| mjr | 82:4f6209cb5c33 | 34 | // set START condition on the bus |
| mjr | 82:4f6209cb5c33 | 35 | void start(); |
| mjr | 82:4f6209cb5c33 | 36 | |
| mjr | 82:4f6209cb5c33 | 37 | // set STOP condition on the bus |
| mjr | 82:4f6209cb5c33 | 38 | void stop(); |
| mjr | 82:4f6209cb5c33 | 39 | |
| mjr | 82:4f6209cb5c33 | 40 | // Write a series of bytes. Returns 0 on success, non-zero on failure. |
| mjr | 82:4f6209cb5c33 | 41 | // Important: 'addr' is 2X the nominal address - shift left by one bit. |
| mjr | 82:4f6209cb5c33 | 42 | int write(uint8_t addr, const uint8_t *data, size_t len, bool repeated = false); |
| mjr | 82:4f6209cb5c33 | 43 | |
| mjr | 82:4f6209cb5c33 | 44 | // write a byte; returns true if ACK was received |
| mjr | 82:4f6209cb5c33 | 45 | int write(uint8_t data); |
| mjr | 82:4f6209cb5c33 | 46 | |
| mjr | 82:4f6209cb5c33 | 47 | // Read a series of bytes. Returns 0 on success, non-zero on failure. |
| mjr | 82:4f6209cb5c33 | 48 | // Important: 'addr' is 2X the nominal address - shift left by one bit. |
| mjr | 82:4f6209cb5c33 | 49 | int read(uint8_t addr, uint8_t *data, size_t len, bool repeated = false); |
| mjr | 82:4f6209cb5c33 | 50 | |
| mjr | 82:4f6209cb5c33 | 51 | // read a byte, optionally sending an ACK on receipt |
| mjr | 82:4f6209cb5c33 | 52 | int read(bool ack); |
| mjr | 82:4f6209cb5c33 | 53 | |
| mjr | 82:4f6209cb5c33 | 54 | // wait for ACK; returns true if ACK was received |
| mjr | 82:4f6209cb5c33 | 55 | bool wait(uint32_t timeout_us); |
| mjr | 82:4f6209cb5c33 | 56 | |
| mjr | 82:4f6209cb5c33 | 57 | // reset the bus |
| mjr | 82:4f6209cb5c33 | 58 | void reset(); |
| mjr | 82:4f6209cb5c33 | 59 | |
| mjr | 82:4f6209cb5c33 | 60 | protected: |
| mjr | 82:4f6209cb5c33 | 61 | // read/write a bit |
| mjr | 82:4f6209cb5c33 | 62 | int readBit(); |
| mjr | 82:4f6209cb5c33 | 63 | void writeBit(int bit); |
| mjr | 82:4f6209cb5c33 | 64 | |
| mjr | 82:4f6209cb5c33 | 65 | // set SCL/SDA lines to high (1) or low(0) |
| mjr | 82:4f6209cb5c33 | 66 | inline void scl(int level) { setLevel(sclPin, level); } |
| mjr | 82:4f6209cb5c33 | 67 | inline void sda(int level) { setLevel(sdaPin, level); } |
| mjr | 82:4f6209cb5c33 | 68 | void setLevel(DigitalInOut pin, int level); |
| mjr | 82:4f6209cb5c33 | 69 | |
| mjr | 82:4f6209cb5c33 | 70 | protected: |
| mjr | 82:4f6209cb5c33 | 71 | // SCL and SDA pins |
| mjr | 82:4f6209cb5c33 | 72 | DigitalInOut sclPin; |
| mjr | 82:4f6209cb5c33 | 73 | DigitalInOut sdaPin; |
| mjr | 82:4f6209cb5c33 | 74 | |
| mjr | 82:4f6209cb5c33 | 75 | // inverse of frequency = clock period in microseconds |
| mjr | 82:4f6209cb5c33 | 76 | uint32_t clkPeriod_us; |
| mjr | 82:4f6209cb5c33 | 77 | }; |
| mjr | 82:4f6209cb5c33 | 78 | |
| mjr | 82:4f6209cb5c33 | 79 | #endif /* _BITBANGI2C_H_ */ |