Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uart2bufmod #1443

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cmnds/cmd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ static commandResult_t CMD_SafeMode(const void* context, const char* cmd, const
void CMD_UARTConsole_Init() {
#if PLATFORM_BEKEN
UART_InitUART(115200, 0);
cmd_uartInitIndex = g_uart_init_counter;
cmd_uartInitIndex = get_g_uart_init_counter;
UART_InitReceiveRingBuffer(512);
#endif
}
Expand Down Expand Up @@ -499,7 +499,7 @@ void CMD_UARTConsole_Run() {
void CMD_RunUartCmndIfRequired() {
#if PLATFORM_BEKEN
if (CFG_HasFlag(OBK_FLAG_CMD_ACCEPT_UART_COMMANDS)) {
if (cmd_uartInitIndex && cmd_uartInitIndex == g_uart_init_counter) {
if (cmd_uartInitIndex && cmd_uartInitIndex == get_g_uart_init_counter) {
CMD_UARTConsole_Run();
}
}
Expand Down
199 changes: 139 additions & 60 deletions src/driver/drv_uart.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../new_common.h"
#include "../new_pins.h"
#include "../new_cfg.h"
#include "drv_uart.h"
// Commands register, execution API and cmd tokenizer
#include "../cmnds/cmd_public.h"
#include "../cmnds/cmd_local.h"
Expand Down Expand Up @@ -82,7 +83,6 @@
#if PLATFORM_BK7231T | PLATFORM_BK7231N
// from uart_bk.c
extern void bk_send_byte(UINT8 uport, UINT8 data);
int g_chosenUART = BK_UART_1;
#elif WINDOWS

#elif PLATFORM_BL602
Expand All @@ -108,63 +108,127 @@ uint8_t* data = NULL;
#else
#endif

static byte *g_recvBuf = 0;
static int g_recvBufSize = 0;
static int g_recvBufIn = 0;
static int g_recvBufOut = 0;
#define UART_BUF_CNT 2
//index of buffer
// if we have 2 buffers, port UART_PORT_INDEX_0 uses buf 0 and UART_PORT_INDEX_1 uses buf 1
// if we have 1 buffer, both ports using buf 0 (as before)
#define UART_BUF_INDEX_0 0
#define UART_BUF_INDEX_1 1

int UART_GetSelectedPortIndex() {
return (CFG_HasFlag(OBK_FLAG_USE_SECONDARY_UART)) ? UART_PORT_INDEX_1 : UART_PORT_INDEX_0;
}

int UART_GetBufIndexFromPort(int aport) {
#if UART_BUF_CNT==2
return (aport == UART_PORT_INDEX_1) ? UART_BUF_INDEX_1 : UART_BUF_INDEX_0;
#else
return UART_BUF_INDEX_0;
#endif
}

typedef struct {
byte* g_recvBuf;
int g_recvBufSize;
int g_recvBufIn;
int g_recvBufOut;
// used to detect uart reinit
int g_uart_init_counter = 0;
int g_uart_init_counter;
// used to detect uart manual mode
int g_uart_manualInitCounter = -1;
int g_uart_manualInitCounter;
} uartbuf_t;

static uartbuf_t uartbuf[UART_BUF_CNT] = { {0,0,0,0,0,-1}
#if UART_BUF_CNT == 2
, { 0,0,0,0,0,-1 }
#endif
};

int get_g_uart_init_counter() {
int fbufindex = UART_GetBufIndexFromPort(UART_GetSelectedPortIndex());
return uartbuf[fbufindex].g_uart_init_counter;
}

void UART_InitReceiveRingBuffer(int size){
//XJIKKA 20241122 - Note that the actual usable buffer size must be g_recvBufSize-1,
void UART_InitReceiveRingBufferEx(int auartindex, int size){
//XJIKKA 20241122 - Note that the actual usable buffer size must be uartbuf[auartindex].g_recvBufSize-1,
//otherwise there would be no difference between an empty and a full buffer.
if(g_recvBuf!=0)
free(g_recvBuf);
g_recvBuf = (byte*)malloc(size);
memset(g_recvBuf,0,size);
g_recvBufSize = size;
g_recvBufIn = 0;
g_recvBufOut = 0;
if(uartbuf[auartindex].g_recvBuf!=0)
free(uartbuf[auartindex].g_recvBuf);
uartbuf[auartindex].g_recvBuf = (byte*)malloc(size);
memset(uartbuf[auartindex].g_recvBuf,0,size);
uartbuf[auartindex].g_recvBufSize = size;
uartbuf[auartindex].g_recvBufIn = 0;
uartbuf[auartindex].g_recvBufOut = 0;
}

void UART_InitReceiveRingBuffer(int size) {
int fuartindex = UART_GetSelectedPortIndex();
UART_InitReceiveRingBufferEx(fuartindex, size);
}

int UART_GetDataSizeEx(int auartindex) {
return (uartbuf[auartindex].g_recvBufIn >= uartbuf[auartindex].g_recvBufOut
? uartbuf[auartindex].g_recvBufIn - uartbuf[auartindex].g_recvBufOut
: uartbuf[auartindex].g_recvBufIn + (uartbuf[auartindex].g_recvBufSize - uartbuf[auartindex].g_recvBufOut)); //XJIKKA 20241122 fixed buffer size calculation on ring bufferroverflow
}

int UART_GetDataSize() {
return (g_recvBufIn >= g_recvBufOut
? g_recvBufIn - g_recvBufOut
: g_recvBufIn + (g_recvBufSize - g_recvBufOut)); //XJIKKA 20241122 fixed buffer size calculation on ring bufferroverflow
int fuartindex = UART_GetSelectedPortIndex();
return UART_GetDataSizeEx(fuartindex);
}

byte UART_GetByteEx(int auartindex, int idx) {
return uartbuf[auartindex].g_recvBuf[(uartbuf[auartindex].g_recvBufOut + idx) % uartbuf[auartindex].g_recvBufSize];
}

byte UART_GetByte(int idx) {
return g_recvBuf[(g_recvBufOut + idx) % g_recvBufSize];
int fuartindex = UART_GetSelectedPortIndex();
return UART_GetByteEx(fuartindex, idx);
}

void UART_ConsumeBytesEx(int auartindex, int idx) {
uartbuf[auartindex].g_recvBufOut += idx;
uartbuf[auartindex].g_recvBufOut %= uartbuf[auartindex].g_recvBufSize;
}

void UART_ConsumeBytes(int idx) {
g_recvBufOut += idx;
g_recvBufOut %= g_recvBufSize;
int fuartindex = UART_GetSelectedPortIndex();
UART_ConsumeBytesEx(fuartindex, idx);
}

void UART_AppendByteToReceiveRingBuffer(int rc) {
if (UART_GetDataSize() < (g_recvBufSize - 1)) {
g_recvBuf[g_recvBufIn++] = rc;
g_recvBufIn %= g_recvBufSize;
void UART_AppendByteToReceiveRingBufferEx(int auartindex, int rc) {
if (UART_GetDataSize() < (uartbuf[auartindex].g_recvBufSize - 1)) {
uartbuf[auartindex].g_recvBuf[uartbuf[auartindex].g_recvBufIn++] = rc;
uartbuf[auartindex].g_recvBufIn %= uartbuf[auartindex].g_recvBufSize;
}
//XJIKKA 20241122 if the same pointer is reached (in and out), we must also advance
//the outbuffer pointer, otherwise UART_GetDataSize will return g_recvBufSize.
//This way now we always have the last (g_recvBufSize - 1) bytes
if (g_recvBufIn == g_recvBufOut) {
g_recvBufOut++;
g_recvBufOut %= g_recvBufSize;
if (uartbuf[auartindex].g_recvBufIn == uartbuf[auartindex].g_recvBufOut) {
uartbuf[auartindex].g_recvBufOut++;
uartbuf[auartindex].g_recvBufOut %= uartbuf[auartindex].g_recvBufSize;
}
}

void UART_AppendByteToReceiveRingBuffer(int rc) {
int fuartindex = UART_GetSelectedPortIndex();
UART_AppendByteToReceiveRingBufferEx(fuartindex, rc);
}

#if PLATFORM_BK7231T | PLATFORM_BK7231N
//BK_UART_1 is defined to 0
int bk_port_from_portindex(int auartindex) {
return (auartindex == 0) ? BK_UART_1 : BK_UART_2; //just to avoid error if more uarts in future
}

void test_ty_read_uart_data_to_buffer(int port, void* param)
{
int rc = 0;
int fbufindex = UART_GetBufIndexFromPort(port);

while((rc = uart_read_byte(port)) != -1)
UART_AppendByteToReceiveRingBuffer(rc);
while ((rc = uart_read_byte(port)) != -1) {
UART_AppendByteToReceiveRingBufferEx(fbufindex, rc);
}
}
#endif

Expand Down Expand Up @@ -247,10 +311,10 @@ static void uart_event_task(void* pvParameters)

#endif

void UART_SendByte(byte b) {
void UART_SendByteEx(int auartindex, byte b) {
#if PLATFORM_BK7231T | PLATFORM_BK7231N
// BK_UART_1 is defined to 0
bk_send_byte(g_chosenUART, b);
int fport = bk_port_from_portindex(auartindex);
bk_send_byte(fport, b);
#elif WINDOWS
void SIM_AppendUARTByte(byte b);
// STUB - for testing
Expand All @@ -266,6 +330,12 @@ void UART_SendByte(byte b) {
uart_write_bytes(uartnum, &b, 1);
#endif
}

void UART_SendByte(byte b) {
int fuartindex = UART_GetSelectedPortIndex();
UART_SendByteEx(fuartindex, b);
}

commandResult_t CMD_UART_Send_Hex(const void *context, const char *cmd, const char *args, int cmdFlags) {
if (!(*args)) {
addLogAdv(LOG_INFO, LOG_FEATURE_TUYAMCU, "CMD_UART_Send_Hex: requires 1 argument (hex string, like FFAABB00CCDD\n");
Expand Down Expand Up @@ -340,12 +410,14 @@ commandResult_t CMD_UART_Send_ASCII(const void *context, const char *cmd, const
}

void UART_ResetForSimulator() {
g_uart_init_counter = 0;
for (int i = 0; i < UART_BUF_CNT; i++) {
uartbuf[i].g_uart_init_counter = 0;
}
}

int UART_InitUART(int baud, int parity)
int UART_InitUARTEx(int auartindex, int baud, int parity)
{
g_uart_init_counter++;
uartbuf[auartindex].g_uart_init_counter++;
#if PLATFORM_BK7231T | PLATFORM_BK7231N
bk_uart_config_t config;

Expand All @@ -356,18 +428,9 @@ int UART_InitUART(int baud, int parity)
config.flow_control = 0; //FLOW_CTRL_DISABLED
config.flags = 0;


// BK_UART_1 is defined to 0
if(CFG_HasFlag(OBK_FLAG_USE_SECONDARY_UART))
{
g_chosenUART = BK_UART_2;
}
else
{
g_chosenUART = BK_UART_1;
}
bk_uart_initialize(g_chosenUART, &config, NULL);
bk_uart_set_rx_callback(g_chosenUART, test_ty_read_uart_data_to_buffer, NULL);
int fport = bk_port_from_portindex(auartindex);
bk_uart_initialize(fport, &config, NULL);
bk_uart_set_rx_callback(fport, test_ty_read_uart_data_to_buffer, NULL);
#elif PLATFORM_BL602
if(fd_console < 0)
{
Expand Down Expand Up @@ -440,35 +503,49 @@ int UART_InitUART(int baud, int parity)
xTaskCreate(uart_event_task, "uart_event_task", 1024, NULL, 16, NULL);
}
#endif
return g_uart_init_counter;
return uartbuf[auartindex].g_uart_init_counter;
}

int UART_InitUART(int baud, int parity) {
int fuartindex = UART_GetSelectedPortIndex();
return UART_InitUARTEx(fuartindex, baud, parity);
}

void UART_LogBufState(int auartindex) {
ADDLOG_WARN(LOG_FEATURE_ENERGYMETER,
"Uart ix %d inbuf %i inptr %i outptr %i \n",
auartindex, UART_GetDataSizeEx(auartindex), uartbuf[auartindex].g_recvBufIn, uartbuf[auartindex].g_recvBufOut
);
}

void UART_DebugTool_Run() {
void UART_DebugTool_Run(int auartindex) {
byte b;
char tmp[128];
char *p = tmp;
int i;

for (i = 0; i < sizeof(tmp) - 4; i++) {
if (UART_GetDataSize()==0) {
if (UART_GetDataSizeEx(auartindex)==0) {
break;
}
b = UART_GetByte(0);
b = UART_GetByteEx(auartindex,0);
if (i) {
*p = ' ';
p++;
}
sprintf(p, "%02X", b);
p += 2;
UART_ConsumeBytes(1);
UART_ConsumeBytesEx(auartindex,1);
}
*p = 0;
addLogAdv(LOG_INFO, LOG_FEATURE_CMD, "UART received: %s\n", tmp);
addLogAdv(LOG_INFO, LOG_FEATURE_CMD, "UART %i received: %s\n", auartindex, tmp);
}

void UART_RunEverySecond() {
if (g_uart_manualInitCounter == g_uart_init_counter) {
UART_DebugTool_Run();
for (int i = 0; i < UART_BUF_CNT; i++) {
if (uartbuf[i].g_uart_manualInitCounter == uartbuf[i].g_uart_init_counter) {
UART_DebugTool_Run(i);
}
}
}

Expand All @@ -483,12 +560,14 @@ commandResult_t CMD_UART_Init(const void *context, const char *cmd, const char *
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}

int fuartindex = UART_GetSelectedPortIndex();

baud = Tokenizer_GetArgInteger(0);

UART_InitReceiveRingBuffer(512);
UART_InitReceiveRingBufferEx(fuartindex,512);

UART_InitUART(baud, 0);
g_uart_manualInitCounter = g_uart_init_counter;
UART_InitUARTEx(fuartindex, baud, 0);
uartbuf[fuartindex].g_uart_manualInitCounter = uartbuf[fuartindex].g_uart_init_counter;

return CMD_RES_OK;
}
Expand Down
29 changes: 28 additions & 1 deletion src/driver/drv_uart.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

//---------------------------------------------------
// Routines using UART port depending on config
// flag OBK_FLAG_USE_SECONDARY_UART
// Note: using same buffer as routines below
//--------------------------------------------------
void UART_InitReceiveRingBuffer(int size);
int UART_GetDataSize();
byte UART_GetByte(int idx);
Expand All @@ -11,4 +16,26 @@ void UART_AddCommands();
void UART_RunEverySecond();

// used to detect uart reinit/takeover by driver
extern int g_uart_init_counter;
int get_g_uart_init_counter();
// used to get selected port from config - OBK_FLAG_USE_SECONDARY_UART
int UART_GetSelectedPortIndex();

//---------------------------------------------------
// XJIKKA 20241123 new routines with uart index param
// BEKEN platform only (yet)
// Independent of OBK_FLAG_USE_SECONDARY_UART
//---------------------------------------------------
//index of UART port
// BEKEN - UART_PORT_INDEX_0 = BK_UART_1 RX1 TX1
// BEKEN - UART_PORT_INDEX_1 = BK_UART_2 RX2 TX2
#define UART_PORT_INDEX_0 0
#define UART_PORT_INDEX_1 1
//
void UART_InitReceiveRingBufferEx(int auartindex, int size);
int UART_GetDataSizeEx(int auartindex);
byte UART_GetByteEx(int auartindex, int idx);
void UART_ConsumeBytesEx(int auartindex, int idx);
void UART_SendByteEx(int auartindex, byte b);
int UART_InitUARTEx(int auartindex, int baud, int parity);
void UART_LogBufState(int auartindex);

Loading