Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Dec 2, 2023
2 parents 9b378e1 + 1ac68d5 commit 1d8efea
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/cmnds/cmd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ static commandResult_t CMD_SafeMode(const void* context, const char* cmd, const

void CMD_UARTConsole_Init() {
#if PLATFORM_BEKEN
UART_InitUART(115200);
UART_InitUART(115200, 0);
cmd_uartInitIndex = g_uart_init_counter;
UART_InitReceiveRingBuffer(512);
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/driver/drv_bl0942.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ static void Init(void) {
void BL0942_UART_Init(void) {
Init();

UART_InitUART(BL0942_UART_BAUD_RATE);
UART_InitUART(BL0942_UART_BAUD_RATE, 0);
UART_InitReceiveRingBuffer(BL0942_UART_RECEIVE_BUFFER_SIZE);

UART_WriteReg(BL0942_REG_USR_WRPROT, BL0942_USR_WRPROT_DISABLE);
Expand All @@ -229,7 +229,7 @@ void BL0942_UART_Init(void) {
void BL0942_UART_RunEverySecond(void) {
UART_TryToGetNextPacket();

UART_InitUART(BL0942_UART_BAUD_RATE);
UART_InitUART(BL0942_UART_BAUD_RATE, 0);

UART_SendByte(BL0942_UART_CMD_READ(BL0942_UART_ADDR));
UART_SendByte(BL0942_UART_REG_PACKET);
Expand Down
2 changes: 1 addition & 1 deletion src/driver/drv_cse7766.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void CSE7766_Init(void) {
PwrCal_Init(PWR_CAL_MULTIPLY, DEFAULT_VOLTAGE_CAL, DEFAULT_CURRENT_CAL,
DEFAULT_POWER_CAL);

UART_InitUART(CSE7766_BAUD_RATE);
UART_InitUART(CSE7766_BAUD_RATE, 0);
UART_InitReceiveRingBuffer(512);
}

Expand Down
2 changes: 2 additions & 0 deletions src/driver/drv_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void ChargingLimit_Init();
void ChargingLimit_OnEverySecond();
void ChargingLimit_AppendInformationToHTTPIndexPage(http_request_t *request);

void RN8209_Init(void);
void RN8029_RunEverySecond(void);

#define SM2135_DELAY 4

Expand Down
7 changes: 7 additions & 0 deletions src/driver/drv_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ static driver_t g_drivers[] = {
//drvdetail:"requires":""}
{ "I2C", DRV_I2C_Init, DRV_I2C_EverySecond, NULL, NULL, NULL, NULL, false },
#endif
#if ENABLE_DRIVER_BL0942
//drvdetail:{"name":"qq",
//drvdetail:"title":"TODO",
//drvdetail:"descr":"Bqqqqqqqqqq ",
//drvdetail:"requires":""}
{ "RN8209", RN8209_Init, RN8029_RunEverySecond, NULL, NULL, NULL, NULL, false },
#endif
#if ENABLE_DRIVER_BL0942
//drvdetail:{"name":"BL0942",
//drvdetail:"title":"TODO",
Expand Down
182 changes: 103 additions & 79 deletions src/driver/drv_rn8209.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,26 @@
#include "drv_bl_shared.h"
#include "drv_uart.h"

#define BL0942_UART_PACKET_LEN 12
#define BL0942_UART_PACKET_HEAD 12

static int UART_TryToGetNextPacket(void) {
int cs;
int i;
int c_garbage_consumed = 0;
byte checksum;

do {
cs = UART_GetDataSize();
i = UART_GetByte(0);

ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
"UA %i\n",
i);
UART_ConsumeBytes(1);
} while (cs > 0);


//if(cs < BL0942_UART_PACKET_LEN) {
// return 0;
//}
//// skip garbage data (should not happen)
//while(cs > 0) {
// if (UART_GetByte(0) != BL0942_UART_PACKET_HEAD) {
// UART_ConsumeBytes(1);
// c_garbage_consumed++;
// cs--;
// } else {
// break;
// }
//}
//if(c_garbage_consumed > 0){
// ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
// "Consumed %i unwanted non-header byte in BL0942 buffer\n",
// c_garbage_consumed);
//}
//if(cs < BL0942_UART_PACKET_LEN) {
// return 0;
//}
// if (UART_GetByte(0) != 0x55)
// return 0;
// checksum = BL0942_UART_CMD_READ(BL0942_UART_ADDR);

// for(i = 0; i < BL0942_UART_PACKET_LEN-1; i++) {
// checksum += UART_GetByte(i);
//}
//checksum ^= 0xFF;

// if (checksum != UART_GetByte(BL0942_UART_PACKET_LEN - 1)) {
// ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
// "Skipping packet with bad checksum %02X wanted %02X\n",
// UART_GetByte(BL0942_UART_PACKET_LEN - 1), checksum);
// UART_ConsumeBytes(BL0942_UART_PACKET_LEN);
// return 1;
//}


// UART_ConsumeBytes(BL0942_UART_PACKET_LEN);
return 0;
}

static void UART_WriteReg(byte reg, byte *data, int len) {
#define RN8209_RMS_CURRENT_A 0x22
#define RN8209_RMS_CURRENT_A_SIZE 3
#define RN8209_RMS_CURRENT_B 0x23
#define RN8209_RMS_CURRENT_B_SIZE 3
#define RN8209_RMS_VOLTAGE 0x24
#define RN8209_RMS_VOLTAGE_SIZE 3
#define RN8209_RMS_FREQUENCY 0x25
#define RN8209_RMS_FREQUENCY_SIZE 2
#define RN8209_AVG_ACTIVEPOWER_A 0x26
#define RN8209_AVG_ACTIVEPOWER_A_SIZE 4
#define RN8209_AVG_ACTIVEPOWER_B 0x27
#define RN8209_AVG_ACTIVEPOWER_B_SIZE 4
#define RN8209_AVG_REACTIVEPOWER 0x28
#define RN8209_AVG_REACTIVEPOWER_SIZE 4
#define RN8209_AVG_REACTIVEENERGY_1 0x29
#define RN8209_AVG_REACTIVEENERGY_1_SIZE 3
#define RN8209_AVG_REACTIVEENERGY_2 0x2A
#define RN8209_AVG_REACTIVEENERGY_2_SIZE 3

static void RN8209_WriteReg(byte reg, byte *data, int len) {
byte crc;

reg = reg | 0x80;
Expand All @@ -84,32 +40,100 @@ static void UART_WriteReg(byte reg, byte *data, int len) {

UART_SendByte(crc);
}
static void UART_ReadReg(byte reg, byte *data, int len) {
static bool RN8029_ReadReg(byte reg, int *res) {
byte crc;
byte data[32];
int size, i;
int dataSize;

UART_SendByte(reg);

/*crc = reg;
rtos_delay_milliseconds(30);

for (int i = 0; i < len; i++) {
UART_SendByte(data[i]);
data[0] = reg;
size = 1;
crc = reg;
while(UART_GetDataSize()) {
data[size] = UART_GetByte(0);
ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
"UA %i\n",
(int)data[size]);
size++;
UART_ConsumeBytes(1);
}
for (i = 1; i < size - 1; i++) {
crc += data[i];
}
crc = ~crc;
UART_SendByte(crc);*/
if (crc != data[size - 1]) {
ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
"CRC BAD, expected %i, got %i\n", (int)crc, (int)data[size - 1]);
return 1;
}
ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
"CRC OK\n");
*res = 0;
dataSize = size - 2;
// Multi-byte registers will first transmit high-byte contents followed by low-byte contents
for (i = 0; i < dataSize; i++) {
int val = data[i + 1];
*res += val << ((dataSize-1-i) * 8);
}
return 0;
}

void RN8209_UART_Init(void) {
UART_InitUART(4800);
// startDriver RN8209
void RN8209_Init(void) {
UART_InitUART(4800, 1);
UART_InitReceiveRingBuffer(256);

}
int g_voltage = 0, g_currentA = 0, g_currentB = 0, g_powerA = 0, g_powerB = 0;
int g_meas = 0;
void RN8029_RunEverySecond(void) {
g_meas++;
g_meas %= 5;
switch (g_meas) {
case 0:
RN8029_ReadReg(RN8209_RMS_VOLTAGE, &g_voltage);
break;
case 1:
RN8029_ReadReg(RN8209_RMS_CURRENT_A, &g_currentA);
break;
case 2:
RN8029_ReadReg(RN8209_RMS_CURRENT_B, &g_currentB);
break;
case 3:
RN8029_ReadReg(RN8209_AVG_ACTIVEPOWER_A, &g_powerA);
break;
case 4:
RN8029_ReadReg(RN8209_AVG_ACTIVEPOWER_B, &g_powerB);
break;
}

void RN8029_UART_RunEverySecond(void) {
UART_TryToGetNextPacket();

UART_ReadReg(0x24,0,0);

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

/*
Send: 36 (command code)
Received:
Warn:EnergyMeter:UA 32
Warn:EnergyMeter:UA 51
Warn:EnergyMeter:UA 225
Warn:EnergyMeter:UA 167
Let's verify checksum (sum modulo 256 and negated):
(36+32+51+225)%256=88
88 -> 01011000
167 -> 10100111
Send: 36 (command code)
Warn:EnergyMeter:UA 32
Warn:EnergyMeter:UA 38
Warn:EnergyMeter:UA 227
Warn:EnergyMeter:UA 178
Let's verify checksum (sum modulo 256 and negated):
(36+32+38+227)%256=77
77 -> 01001101
178 -> 10110010
*/
6 changes: 3 additions & 3 deletions src/driver/drv_tuyaMCU.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void TuyaMCU_SendCommandWithData(byte cmdType, byte* data, int payload_len) {

byte check_sum = (0xFF + cmdType + (payload_len >> 8) + (payload_len & 0xFF));

UART_InitUART(g_baudRate);
UART_InitUART(g_baudRate, 0);
if (CFG_HasFlag(OBK_FLAG_TUYAMCU_USE_QUEUE)) {
tuyaMCUPacket_t *p = TUYAMCU_AddToQueue(payload_len + 4);
p->data[0] = cmdType;
Expand Down Expand Up @@ -1970,7 +1970,7 @@ commandResult_t TuyaMCU_SetBaudRate(const void* context, const char* cmd, const
}

g_baudRate = Tokenizer_GetArgInteger(0);
UART_InitUART(g_baudRate);
UART_InitUART(g_baudRate, 0);

return CMD_RES_OK;
}
Expand Down Expand Up @@ -2056,7 +2056,7 @@ void TuyaMCU_Init()
g_tuyaMCUpayloadBuffer = (byte*)malloc(TUYAMCU_BUFFER_SIZE);
}

UART_InitUART(g_baudRate);
UART_InitUART(g_baudRate, 0);
UART_InitReceiveRingBuffer(256);
// uartSendHex 55AA0008000007
//cmddetail:{"name":"tuyaMcu_testSendTime","args":"",
Expand Down
6 changes: 3 additions & 3 deletions src/driver/drv_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ void UART_ResetForSimulator() {
g_uart_init_counter = 0;
}

int UART_InitUART(int baud) {
int UART_InitUART(int baud, int parity) {
g_uart_init_counter++;
#if PLATFORM_BK7231T | PLATFORM_BK7231N
bk_uart_config_t config;

config.baud_rate = baud;
config.data_width = 0x03;
config.parity = 0; //0:no parity,1:odd,2:even
config.parity = parity; //0:no parity,1:odd,2:even
config.stop_bits = 0; //0:1bit,1:2bit
config.flow_control = 0; //FLOW_CTRL_DISABLED
config.flags = 0;
Expand Down Expand Up @@ -400,7 +400,7 @@ commandResult_t CMD_UART_Init(const void *context, const char *cmd, const char *

baud = Tokenizer_GetArgInteger(0);

UART_InitUART(baud);
UART_InitUART(baud, 0);
g_uart_manualInitCounter = g_uart_init_counter;
UART_InitReceiveRingBuffer(512);

Expand Down
2 changes: 1 addition & 1 deletion src/driver/drv_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ byte UART_GetByte(int idx);
void UART_ConsumeBytes(int idx);
void UART_AppendByteToReceiveRingBuffer(int rc);
void UART_SendByte(byte b);
void UART_InitUART(int baud);
void UART_InitUART(int baud, int parity);
void UART_AddCommands();
void UART_RunEverySecond();

Expand Down

0 comments on commit 1d8efea

Please sign in to comment.