Gsma Version
Dependents: GSMACode stripReader
pca9548.h@1:fc2dbccffe34, 2017-04-21 (annotated)
- Committer:
- elmkom
- Date:
- Fri Apr 21 17:58:08 2017 +0000
- Revision:
- 1:fc2dbccffe34
- Parent:
- 0:5e15a306a518
Proximity strip reader
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elmkom | 0:5e15a306a518 | 1 | // NXP PCA9548: 8-channel I2C switch with reset |
elmkom | 0:5e15a306a518 | 2 | |
elmkom | 0:5e15a306a518 | 3 | #define PCA9548_BASE_ADDR_7BIT 0x70 |
elmkom | 0:5e15a306a518 | 4 | |
elmkom | 0:5e15a306a518 | 5 | #define MAX_CHANNELS 8 |
elmkom | 0:5e15a306a518 | 6 | |
elmkom | 0:5e15a306a518 | 7 | #define ASSERT_RESET 0 |
elmkom | 0:5e15a306a518 | 8 | #define NEGATE_RESET 1 |
elmkom | 0:5e15a306a518 | 9 | |
elmkom | 0:5e15a306a518 | 10 | class PCA9548 |
elmkom | 0:5e15a306a518 | 11 | { |
elmkom | 0:5e15a306a518 | 12 | public: |
elmkom | 0:5e15a306a518 | 13 | /** |
elmkom | 0:5e15a306a518 | 14 | * Constructor |
elmkom | 0:5e15a306a518 | 15 | * |
elmkom | 0:5e15a306a518 | 16 | * @param i2c I2C class servicing the multiplexer |
elmkom | 0:5e15a306a518 | 17 | * @param addr_3bit address of the multiplexer (A0-A2 pin strapping) |
elmkom | 0:5e15a306a518 | 18 | * Valid values are 0-7 |
elmkom | 0:5e15a306a518 | 19 | * @param reset optional DigitalOut class that resets the multiplexer (disables all I2C channels) |
elmkom | 0:5e15a306a518 | 20 | * Not needed if hardware reset is not used or if reset |
elmkom | 0:5e15a306a518 | 21 | * is not managed by the class. |
elmkom | 0:5e15a306a518 | 22 | */ |
elmkom | 0:5e15a306a518 | 23 | PCA9548(I2C * i2c, uint8_t addr_3bit, DigitalOut * reset = NULL) : _i2c(i2c), _reset(reset) { |
elmkom | 0:5e15a306a518 | 24 | _addr_8bit = ((addr_3bit & 0x7) + PCA9548_BASE_ADDR_7BIT) << 1; |
elmkom | 0:5e15a306a518 | 25 | } |
elmkom | 0:5e15a306a518 | 26 | |
elmkom | 0:5e15a306a518 | 27 | /** |
elmkom | 0:5e15a306a518 | 28 | * Reset the multiplexer. All devices connected to the downstream side of |
elmkom | 0:5e15a306a518 | 29 | * the multiplexer are removed from the I2C bus. If hardware reset is being |
elmkom | 0:5e15a306a518 | 30 | * used, reset is done via asserting the reset pin. Otherwise, reset is |
elmkom | 0:5e15a306a518 | 31 | * accomplished by deselecting all channels through soft configuration. |
elmkom | 0:5e15a306a518 | 32 | * |
elmkom | 0:5e15a306a518 | 33 | * @returns true if successful |
elmkom | 0:5e15a306a518 | 34 | */ |
elmkom | 0:5e15a306a518 | 35 | bool reset(void) { |
elmkom | 0:5e15a306a518 | 36 | if (_reset) { |
elmkom | 0:5e15a306a518 | 37 | _reset->write(ASSERT_RESET); |
elmkom | 0:5e15a306a518 | 38 | return true; |
elmkom | 0:5e15a306a518 | 39 | } else { |
elmkom | 0:5e15a306a518 | 40 | const char channel = 0; |
elmkom | 0:5e15a306a518 | 41 | return _i2c->write(_addr_8bit, &channel, 1) == 0; |
elmkom | 0:5e15a306a518 | 42 | } |
elmkom | 0:5e15a306a518 | 43 | } |
elmkom | 0:5e15a306a518 | 44 | |
elmkom | 0:5e15a306a518 | 45 | /** |
elmkom | 0:5e15a306a518 | 46 | * Enable access to one of the eight devices on the downstream side of |
elmkom | 0:5e15a306a518 | 47 | * the multiplexer. |
elmkom | 0:5e15a306a518 | 48 | * |
elmkom | 0:5e15a306a518 | 49 | * @param channel channel to activate. Valid values are 0-7. |
elmkom | 0:5e15a306a518 | 50 | * |
elmkom | 0:5e15a306a518 | 51 | * @returns true if successful |
elmkom | 0:5e15a306a518 | 52 | */ |
elmkom | 0:5e15a306a518 | 53 | bool select_channel(const uint8_t channel) { |
elmkom | 0:5e15a306a518 | 54 | if (channel < MAX_CHANNELS) { |
elmkom | 0:5e15a306a518 | 55 | char channel_mask = 1 << channel; |
elmkom | 0:5e15a306a518 | 56 | if (_reset) { |
elmkom | 0:5e15a306a518 | 57 | _reset->write(NEGATE_RESET); |
elmkom | 0:5e15a306a518 | 58 | } |
elmkom | 0:5e15a306a518 | 59 | return _i2c->write(_addr_8bit, (const char *)&channel_mask, 1) == 0; |
elmkom | 0:5e15a306a518 | 60 | } else { |
elmkom | 0:5e15a306a518 | 61 | return false; |
elmkom | 0:5e15a306a518 | 62 | } |
elmkom | 0:5e15a306a518 | 63 | } |
elmkom | 0:5e15a306a518 | 64 | |
elmkom | 0:5e15a306a518 | 65 | protected: |
elmkom | 0:5e15a306a518 | 66 | int _addr_8bit; |
elmkom | 0:5e15a306a518 | 67 | I2C * _i2c; |
elmkom | 0:5e15a306a518 | 68 | DigitalOut * _reset; |
elmkom | 0:5e15a306a518 | 69 | }; |