-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
436 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#include "TCA9548.h" | ||
|
||
// | ||
// FILE: TCA9548.cpp | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.0 | ||
// DATE: 2021-03-16 | ||
// PURPOSE: Library for TCA9548 I2C multiplexer | ||
// | ||
// HISTORY: | ||
// 0.1.0 2021-03-16 initial version | ||
|
||
#include "TCA9548.h" | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
extern "C" { | ||
#include <linux/i2c-dev.h> | ||
} | ||
#include <sys/ioctl.h> | ||
|
||
TCA9548::TCA9548(const char *i2c_device_filepath, const uint8_t deviceAddress) | ||
: _address(deviceAddress), i2c_file(0), i2c_filepath(i2c_device_filepath) | ||
{ | ||
_mask = 0x00; | ||
_resetPin = -1; | ||
_forced = false; | ||
} | ||
|
||
bool TCA9548::begin(uint8_t mask) | ||
{ | ||
//Open i2c device | ||
int file; | ||
if ((file = open(i2c_filepath.data(), O_RDWR)) < 0) | ||
{ | ||
return false; | ||
} | ||
i2c_file = file; | ||
|
||
//Set address | ||
if (ioctl(file, I2C_SLAVE_FORCE, _address) < 0) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!isConnected()) | ||
return false; | ||
setChannelMask(mask); | ||
return true; | ||
} | ||
|
||
int TCA9548::i2c_read() | ||
{ | ||
uint8_t reg_val = 0; | ||
|
||
int result = write(i2c_file, &_address, 1); | ||
if (result < 0) | ||
{ | ||
return result; | ||
} | ||
|
||
uint8_t data; | ||
result = read(i2c_file, &data, 1); | ||
if (result < 0) | ||
{ | ||
return result; | ||
} | ||
else | ||
{ | ||
reg_val = data; | ||
} | ||
|
||
return reg_val; | ||
} | ||
|
||
int TCA9548::i2c_write(uint8_t data) | ||
{ | ||
uint8_t buff[2]; | ||
buff[0] = _address; | ||
buff[1] = data; | ||
int result = write(i2c_file, buff, 2); | ||
if (result < 0) | ||
{ | ||
return result; | ||
} | ||
return 0; | ||
} | ||
|
||
bool TCA9548::isConnected() | ||
{ | ||
int retval = i2c_read(); | ||
return (retval >= 0); | ||
} | ||
|
||
void TCA9548::enableChannel(uint8_t channel) | ||
{ | ||
if (isEnabled(channel)) | ||
return; | ||
setChannelMask(_mask | (0x01 << channel)); | ||
} | ||
|
||
void TCA9548::disableChannel(uint8_t channel) | ||
{ | ||
if (!isEnabled(channel)) | ||
return; | ||
setChannelMask(_mask & ~(0x01 << channel)); | ||
} | ||
|
||
void TCA9548::selectChannel(uint8_t channel) | ||
{ | ||
setChannelMask(0x01 << channel); | ||
} | ||
|
||
bool TCA9548::isEnabled(uint8_t channel) | ||
{ | ||
if (channel > 7) | ||
return false; | ||
return (_mask & (0x01 << channel)); | ||
} | ||
|
||
void TCA9548::setChannelMask(uint8_t mask) | ||
{ | ||
if ((_mask == mask) && (!_forced)) | ||
return; | ||
_mask = mask; | ||
i2c_write(mask); | ||
} | ||
|
||
uint8_t TCA9548::getChannelMask() | ||
{ | ||
return _mask; | ||
} | ||
|
||
int TCA9548::getError() | ||
{ | ||
int e = _error; | ||
_error = 0; | ||
return e; | ||
} | ||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <string> | ||
|
||
// | ||
// FILE: TCA9548.h | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.0 | ||
// DATE: 2021-03-16 | ||
// PURPOSE: Library for TCA9548 I2C multiplexer | ||
// | ||
// URL: https://github.com/RobTillaart/TCA9548 | ||
// | ||
// Adapted for rpi PA0PHH | ||
|
||
#define TCA9548_LIB_VERSION (F("0.1.0")) | ||
|
||
class TCA9548 | ||
{ | ||
public: | ||
// address = 0x70 .. 0x77 | ||
TCA9548(const char *i2c_device_filepath, const uint8_t deviceAddress = 0x70); | ||
bool begin(uint8_t mask = 0x00); // default no channels enabled | ||
bool isConnected(); // find multiplexer on I2C bus | ||
|
||
// channel = 0.. 7 | ||
void enableChannel(uint8_t channel); | ||
void disableChannel(uint8_t channel); | ||
void selectChannel(uint8_t channel); // enable only this channel | ||
bool isEnabled(uint8_t channel); | ||
|
||
// mask = 0x00 .. 0xFF - every bit is a channel. | ||
void setChannelMask(uint8_t mask); | ||
uint8_t getChannelMask(); | ||
|
||
// set forced write | ||
void setForced(bool forced) { _forced = forced; }; | ||
bool getForced() { return _forced; }; | ||
|
||
// TODO improve errorhandling ? | ||
int getError(); | ||
|
||
private: | ||
uint8_t _mask = 0x00; // caching mask | ||
uint8_t _resetPin = -1; | ||
int _error = 0; | ||
uint8_t _address; | ||
bool _forced; | ||
|
||
int i2c_file; | ||
std::string i2c_filepath; | ||
|
||
int i2c_read(); | ||
int i2c_write(uint8_t data); | ||
}; | ||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.