Skip to content

Commit

Permalink
rn8209 voltaege works and power works, current not yet?
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Dec 7, 2023
1 parent 73e9abf commit e91fe8c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/driver/drv_rn8209.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@


// NOTE:
// Use this for reference:
// https://github.com/RN8209C/RN8209C-SDK/blob/master/src/rn8209c_u.c
// Search for rn8209c_read_current
#include "../logging/logging.h"
#include "../new_pins.h"
#include "drv_bl_shared.h"
Expand Down Expand Up @@ -42,7 +45,7 @@ static void RN8209_WriteReg(byte reg, byte *data, int len) {

UART_SendByte(crc);
}
static bool RN8029_ReadReg(byte reg, int *res) {
static bool RN8029_ReadReg(byte reg, unsigned int *res) {
byte crc;
byte data[32];
int size, i;
Expand Down Expand Up @@ -85,7 +88,7 @@ static bool RN8029_ReadReg(byte reg, int *res) {
}
#define DEFAULT_VOLTAGE_CAL 8810
#define DEFAULT_CURRENT_CAL 57
#define DEFAULT_POWER_CAL 8810
#define DEFAULT_POWER_CAL 71572720
// startDriver RN8209
void RN8209_Init(void) {
UART_InitUART(4800, 1);
Expand All @@ -95,7 +98,7 @@ void RN8209_Init(void) {
PwrCal_Init(PWR_CAL_DIVIDE, DEFAULT_VOLTAGE_CAL, DEFAULT_CURRENT_CAL,
DEFAULT_POWER_CAL);
}
int g_voltage = 0, g_currentA = 0, g_currentB = 0, g_powerA = 0, g_powerB = 0;
unsigned int g_voltage = 0, g_currentA = 0, g_currentB = 0, g_powerA = 0, g_powerB = 0;
int g_meas = 0;
void RN8029_RunEverySecond(void) {
float final_v;
Expand All @@ -109,21 +112,30 @@ void RN8029_RunEverySecond(void) {
RN8029_ReadReg(RN8209_RMS_VOLTAGE, &g_voltage);
break;
case 1:
RN8029_ReadReg(RN8209_RMS_CURRENT_A, &g_currentA);
if (RN8029_ReadReg(RN8209_RMS_CURRENT_A, &g_currentA) == 0) {
if (g_currentA & 0x800000) {
g_currentA = 0;
}
}
break;
case 2:
RN8029_ReadReg(RN8209_RMS_CURRENT_B, &g_currentB);
break;
case 3:
RN8029_ReadReg(RN8209_AVG_ACTIVEPOWER_A, &g_powerA);
if (RN8029_ReadReg(RN8209_AVG_ACTIVEPOWER_A, &g_powerA) == 0) {
if (g_powerA & 0x80000000) {
g_powerA = ~g_powerA;
g_powerA += 1;
}
}
break;
case 4:
RN8029_ReadReg(RN8209_AVG_ACTIVEPOWER_B, &g_powerB);
break;
}

ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
"V %i, C %i %i, P %i %i\n", g_voltage, g_currentA, g_currentB, g_powerA, g_powerB);
"V %u, C %u %u, P %u %u\n", g_voltage, g_currentA, g_currentB, g_powerA, g_powerB);

PwrCal_Scale(g_voltage, g_currentA, g_powerA, &final_v, &final_c, &final_p);
BL_ProcessUpdate(final_v, final_c, final_p, NAN, NAN);
Expand Down

0 comments on commit e91fe8c

Please sign in to comment.