Skip to content

Commit

Permalink
RN8209 first signs of communication
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Nov 30, 2023
1 parent 7128885 commit 3ee03be
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 17 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
42 changes: 36 additions & 6 deletions src/driver/drv_rn8209.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ static int UART_TryToGetNextPacket(void) {

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

ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
Expand Down Expand Up @@ -84,11 +86,26 @@ static void UART_WriteReg(byte reg, byte *data, int len) {

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

UART_SendByte(reg);

rtos_delay_milliseconds(10);

data[0] = reg;
size = 1;
/*
while(UART_GetDataSize()) {
data[size] = UART_GetByte(0);
size++;
ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
"UA %i\n",
(int)data[size]);
UART_ConsumeBytes(1);
} while (cs > 0);*/
/*crc = reg;
for (int i = 0; i < len; i++) {
Expand All @@ -99,17 +116,30 @@ static void UART_ReadReg(byte reg, byte *data, int len) {
UART_SendByte(crc);*/
}

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

}

void RN8029_UART_RunEverySecond(void) {
void RN8029_RunEverySecond(void) {
UART_TryToGetNextPacket();

UART_ReadReg(0x24,0,0);

}

/*
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
*/
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 3ee03be

Please sign in to comment.