From 3ee03be298a058ca8889ccd08c233d520208544f Mon Sep 17 00:00:00 2001 From: Tester23 Date: Thu, 30 Nov 2023 18:25:56 +0100 Subject: [PATCH] RN8209 first signs of communication --- src/cmnds/cmd_main.c | 2 +- src/driver/drv_bl0942.c | 4 ++-- src/driver/drv_cse7766.c | 2 +- src/driver/drv_local.h | 2 ++ src/driver/drv_main.c | 7 +++++++ src/driver/drv_rn8209.c | 42 ++++++++++++++++++++++++++++++++++------ src/driver/drv_tuyaMCU.c | 6 +++--- src/driver/drv_uart.c | 6 +++--- src/driver/drv_uart.h | 2 +- 9 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/cmnds/cmd_main.c b/src/cmnds/cmd_main.c index 56941ddf3..9294cd1e1 100644 --- a/src/cmnds/cmd_main.c +++ b/src/cmnds/cmd_main.c @@ -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 diff --git a/src/driver/drv_bl0942.c b/src/driver/drv_bl0942.c index 6589a1b38..c68647c3d 100644 --- a/src/driver/drv_bl0942.c +++ b/src/driver/drv_bl0942.c @@ -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); @@ -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); diff --git a/src/driver/drv_cse7766.c b/src/driver/drv_cse7766.c index d6acdd8b5..0ed850fe5 100644 --- a/src/driver/drv_cse7766.c +++ b/src/driver/drv_cse7766.c @@ -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); } diff --git a/src/driver/drv_local.h b/src/driver/drv_local.h index bfdf25289..32344be46 100644 --- a/src/driver/drv_local.h +++ b/src/driver/drv_local.h @@ -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 diff --git a/src/driver/drv_main.c b/src/driver/drv_main.c index 47c6ddd32..74c5e922c 100644 --- a/src/driver/drv_main.c +++ b/src/driver/drv_main.c @@ -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", diff --git a/src/driver/drv_rn8209.c b/src/driver/drv_rn8209.c index 29e27ff24..41de1b264 100644 --- a/src/driver/drv_rn8209.c +++ b/src/driver/drv_rn8209.c @@ -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, @@ -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++) { @@ -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 +*/ diff --git a/src/driver/drv_tuyaMCU.c b/src/driver/drv_tuyaMCU.c index 23bea63ab..9ea995d76 100644 --- a/src/driver/drv_tuyaMCU.c +++ b/src/driver/drv_tuyaMCU.c @@ -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; @@ -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; } @@ -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":"", diff --git a/src/driver/drv_uart.c b/src/driver/drv_uart.c index 6dc210265..1e81060eb 100644 --- a/src/driver/drv_uart.c +++ b/src/driver/drv_uart.c @@ -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; @@ -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); diff --git a/src/driver/drv_uart.h b/src/driver/drv_uart.h index 2392754c3..ede6e7fed 100644 --- a/src/driver/drv_uart.h +++ b/src/driver/drv_uart.h @@ -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();