Skip to content

Commit

Permalink
add tca9548
Browse files Browse the repository at this point in the history
  • Loading branch information
paulh002 committed May 21, 2023
1 parent 4897157 commit d6c2dda
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 185 deletions.
2 changes: 2 additions & 0 deletions SoapyHifiBerry.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<ClCompile Include="SoapyHifiBerrySettings.cpp" />
<ClCompile Include="SoapyHifiBerryStreaming.cpp" />
<ClCompile Include="strlib.cpp" />
<ClCompile Include="TCA9548.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Audiodefs.h" />
Expand All @@ -101,6 +102,7 @@
<ClInclude Include="si5351.h" />
<ClInclude Include="SoapyHifiBerry.h" />
<ClInclude Include="strlib.h" />
<ClInclude Include="TCA9548.h" />
</ItemGroup>
<ItemGroup>
<None Include="SoapyHifiBerry-Debug.vgdbsettings" />
Expand Down
6 changes: 6 additions & 0 deletions SoapyHifiBerry.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<ClCompile Include="RtAudio.cpp">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="TCA9548.cpp">
<Filter>Source files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SoapyHifiBerry.h">
Expand Down Expand Up @@ -81,6 +84,9 @@
<ClInclude Include="RtAudio.h">
<Filter>Header files</Filter>
</ClInclude>
<ClInclude Include="TCA9548.h">
<Filter>Header files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="SoapyHifiBerry-Debug.vgdbsettings">
Expand Down
145 changes: 145 additions & 0 deletions TCA9548.cpp
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 --
58 changes: 58 additions & 0 deletions TCA9548.h
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 --
6 changes: 4 additions & 2 deletions si5351.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ extern "C" {
/* Public functions */
/********************/

Si5351::Si5351(const char* i2c_device_filepath, uint8_t i2c_addr):
i2c_bus_addr(i2c_addr),i2c_file(0),i2c_filepath(i2c_device_filepath)
Si5351::Si5351(const char *i2c_device_filepath, uint8_t i2c_addr)
: i2c_bus_addr(i2c_addr), i2c_file(0), i2c_filepath(i2c_device_filepath)


{
xtal_freq[0] = SI5351_XTAL_FREQ;

Expand Down
Loading

0 comments on commit d6c2dda

Please sign in to comment.