Skip to content

Commit

Permalink
lessen i2c reads
Browse files Browse the repository at this point in the history
  • Loading branch information
p-sam committed Oct 14, 2024
1 parent ed4ba7b commit c86ce66
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion sysmodule/lib/nxExt/include/nxExt/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C"

#include <switch.h>

Result i2csessionExtSendU8Receive(I2cSession* s, u8 in, void* out, u8 out_size);
Result i2csessionExtRegReceive(I2cSession* s, u8 in, void* out, u8 out_size);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion sysmodule/lib/nxExt/src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define I2C_CMD_SND 0
#define I2C_CMD_RCV 1

Result i2csessionExtSendU8Receive(I2cSession* s, u8 in, void* out, u8 out_size)
Result i2csessionExtRegReceive(I2cSession* s, u8 in, void* out, u8 out_size)
{
u8 cmdlist[5] = {
I2C_CMD_SND | (I2cTransactionOption_Start << 6),
Expand Down
36 changes: 23 additions & 13 deletions sysmodule/lib/nxExt/src/max17050.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,36 @@ static u64 g_update_ticks = 0;
static s32 g_power_now = 0;
static s32 g_power_avg = 0;

static Result _max17050_get_power(u8 creg, u8 vreg, s32 *out_mw)
static Result _max17050_get_power(s32 *out_mw_now, s32 *out_mw_avg)
{
u16 current = 0;
u16 voltage = 0;
s64 ma, mv;
u16 values[3] = {0};

Result rc = i2csessionExtSendU8Receive(&g_i2c_session, creg, &current, sizeof(current));
Result rc = i2csessionExtRegReceive(&g_i2c_session, MAX17050_VCELL, values, sizeof(values));

if(R_SUCCEEDED(rc))
if (R_SUCCEEDED(rc))
{
rc = i2csessionExtSendU8Receive(&g_i2c_session, vreg, &voltage, sizeof(voltage));
ma = (s16)values[1];
ma = ma * 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);

mv = (int)(values[0] >> 3) * 625 / 1000;

*out_mw_now = ma * mv / 1000000;
}

if(R_SUCCEEDED(rc))
if (R_SUCCEEDED(rc))
{
s64 ma = (s16)current;
ma *= 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);
rc = i2csessionExtRegReceive(&g_i2c_session, MAX17050_AvgVCELL, values, sizeof(u16));
}

if (R_SUCCEEDED(rc))
{
ma = (s16)values[2];
ma = ma * 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);

mv = (int)(values[0] >> 3) * 625 / 1000;

s64 mv = (int)(voltage >> 3) * 625 / 1000;
*out_mw = ma * mv / 1000000;
*out_mw_avg = ma * mv / 1000000;
}

return rc;
Expand All @@ -79,8 +90,7 @@ static void _max17050_update()
return;
}

_max17050_get_power(MAX17050_Current, MAX17050_VCELL, &g_power_now);
_max17050_get_power(MAX17050_AvgCurrent, MAX17050_AvgVCELL, &g_power_avg);
_max17050_get_power(&g_power_now, &g_power_avg);
}

Result max17050Initialize(void)
Expand Down
4 changes: 2 additions & 2 deletions sysmodule/lib/nxExt/src/tmp451.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ static s32 g_temp_soc = 0;
static Result _tmp451_get_temp(u8 reg, u8 dec_reg, s32* out)
{
u8 val = 0;
Result rc = i2csessionExtSendU8Receive(&g_i2c_session, reg, &val, sizeof(val));
Result rc = i2csessionExtRegReceive(&g_i2c_session, reg, &val, sizeof(val));

if(R_SUCCEEDED(rc))
{
*out = (s32)val * 1000;
rc = i2csessionExtSendU8Receive(&g_i2c_session, dec_reg, &val, sizeof(val));
rc = i2csessionExtRegReceive(&g_i2c_session, dec_reg, &val, sizeof(val));
}

if(R_SUCCEEDED(rc))
Expand Down
4 changes: 2 additions & 2 deletions sysmodule/src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Board::Initialize()
ASSERT_RESULT_OK(rc, "max17050Initialize");

rc = tmp451Initialize();
ASSERT_RESULT_OK(rc, "max17050Initialize");
ASSERT_RESULT_OK(rc, "tmp451Initialize");

FetchHardwareInfos();
}
Expand Down Expand Up @@ -374,4 +374,4 @@ void Board::FetchHardwareInfos()
default:
g_socType = SysClkSocType_Erista;
}
}
}

0 comments on commit c86ce66

Please sign in to comment.