Steve Martin / libdev_pca9544a
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCA9544a.h Source File

PCA9544a.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 AT&T, IIoT Foundry, Plano, TX, USA
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /** \addtogroup drivers */
00018 
00019 /** Support for the NXP PCA9544A 4-channel I2C switch.
00020  *
00021  * Example:
00022  * @code
00023  *
00024  * #include "mbed.h"
00025  * #include "PCA9544A.h"
00026  *
00027  * // PCA9544A strapped for address option 0 (7-bit I2C address 0x70)
00028  *
00029  * I2C            i2c(I2C_SDA, I2C_SCL);
00030  * PCA9544A<I2C>  pca9544a(&i2c, 0);
00031  *
00032  * int main() {
00033  *     bool                ok;
00034  *
00035  *     // Enable channel 1
00036  *     bool ok = _pca9544a->select_channel(1);
00037  *     if (ok) {
00038  *         printf("I2C device on channel 1 is now accessible\r\n);
00039  *     } else {
00040  *         printf("pca9544a error!\r\n");
00041  *     }
00042  *
00043  *     // Disconnect all downstream devices from the I2C bus
00044  *     _pca9544a->reset();
00045  * }
00046  * @endcode
00047  * @ingroup drivers
00048  */
00049 
00050 #pragma once
00051 
00052 #define PCA9544A_BASE_ADDR_7BIT     0x70
00053 #define MAX_CHANNELS                4
00054 
00055 template <class T>
00056 class PCA9544A
00057 {
00058 public:
00059     /**
00060     * Constructor
00061     *
00062     * @param i2c I2C class servicing the multiplexer
00063     * @param addr_3bit address of the multiplexer (A0-A2 pin strapping)
00064     *          Valid values are 0-7
00065     */
00066     PCA9544A(T * i2c, uint8_t addr_3bit) : _i2c(i2c) {
00067         _addr_8bit = ((addr_3bit & 0x7) + PCA9544A_BASE_ADDR_7BIT) << 1;
00068     }
00069 
00070     /**
00071     * Reset the multiplexer.  All devices connected to the downstream side of
00072     * the multiplexer are removed from the I2C bus.  Reset is accomplished by
00073     * deselecting all channels through soft configuration.
00074     *
00075     * @returns true if successful
00076     */
00077     bool reset(void) {
00078         const char channel = 0;
00079         return _i2c->write(_addr_8bit, &channel, 1) == 0;
00080     }
00081 
00082     /**
00083     * Enable access to one of the eight devices on the downstream side of
00084     * the multiplexer.
00085     *
00086     * @param channel channel to activate.  Valid values are 0-7.
00087     *
00088     * @returns true if successful
00089     */
00090     bool select_channel(const uint8_t channel) {
00091         if (channel < MAX_CHANNELS) {
00092             char channel_mask = 0x4 | channel;
00093             return _i2c->write(_addr_8bit, (const char *)&channel_mask, 1) == 0;
00094         } else {
00095             return false;
00096         }
00097     }
00098 
00099 protected:
00100     int     _addr_8bit;
00101     T      *_i2c;
00102 };
00103