Skip to content

Commit 4dde6c8

Browse files
authored
Uart2bufmod - mod for using 2 UARTs simultaneously (#1516)
* Mod for using 2 UARTs simultaneously (Beken) * . * uartindex checking * return undefided bug * changed index to pointer to struct, corrected buffer on devices with UART_2_UARTS_CONCURRENT disabled * retrigger checks
1 parent c249826 commit 4dde6c8

File tree

5 files changed

+232
-67
lines changed

5 files changed

+232
-67
lines changed

src/cmnds/cmd_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ static commandResult_t CMD_SafeMode(const void* context, const char* cmd, const
516516
void CMD_UARTConsole_Init() {
517517
#if PLATFORM_BEKEN
518518
UART_InitUART(115200, 0);
519-
cmd_uartInitIndex = g_uart_init_counter;
519+
cmd_uartInitIndex = get_g_uart_init_counter;
520520
UART_InitReceiveRingBuffer(512);
521521
#endif
522522
}
@@ -559,7 +559,7 @@ void CMD_UARTConsole_Run() {
559559
void CMD_RunUartCmndIfRequired() {
560560
#if PLATFORM_BEKEN
561561
if (CFG_HasFlag(OBK_FLAG_CMD_ACCEPT_UART_COMMANDS)) {
562-
if (cmd_uartInitIndex && cmd_uartInitIndex == g_uart_init_counter) {
562+
if (cmd_uartInitIndex && cmd_uartInitIndex == get_g_uart_init_counter) {
563563
CMD_UARTConsole_Run();
564564
}
565565
}

src/driver/drv_uart.c

Lines changed: 177 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,165 @@
77
#include "../logging/logging.h"
88
#include "../hal/hal_uart.h"
99

10-
static byte *g_recvBuf = 0;
11-
static int g_recvBufSize = 0;
12-
static int g_recvBufIn = 0;
13-
static int g_recvBufOut = 0;
10+
//#define UART_ALWAYSFIRSTBYTES
11+
#define UART_DEFAULT_BUFIZE 512
12+
#ifdef UART_2_UARTS_CONCURRENT
13+
#define UART_BUF_CNT 2
14+
#else
15+
#define UART_BUF_CNT 1
16+
#endif
17+
//index of buffer
18+
// if we have 2 buffers, port UART_PORT_INDEX_0 uses buf 0 and UART_PORT_INDEX_1 uses buf 1
19+
// if we have 1 buffer, both ports using buf 0 (as before)
20+
#define UART_BUF_INDEX_0 0
21+
#ifdef UART_2_UARTS_CONCURRENT
22+
#define UART_BUF_INDEX_1 1
23+
#endif
24+
25+
int UART_GetSelectedPortIndex() {
26+
return (CFG_HasFlag(OBK_FLAG_USE_SECONDARY_UART)) ? UART_PORT_INDEX_1 : UART_PORT_INDEX_0;
27+
}
28+
29+
int UART_GetBufIndexFromPort(int aport) {
30+
#if UART_BUF_CNT==2
31+
return (aport == UART_PORT_INDEX_1) ? UART_BUF_INDEX_1 : UART_BUF_INDEX_0;
32+
#else
33+
return UART_BUF_INDEX_0;
34+
#endif
35+
}
36+
37+
typedef struct {
38+
byte* g_recvBuf;
39+
int g_recvBufSize;
40+
int g_recvBufIn;
41+
int g_recvBufOut;
1442
// used to detect uart reinit
15-
int g_uart_init_counter = 0;
43+
int g_uart_init_counter;
1644
// used to detect uart manual mode
17-
int g_uart_manualInitCounter = -1;
45+
int g_uart_manualInitCounter;
46+
} uartbuf_t;
47+
48+
static uartbuf_t uartbuf[UART_BUF_CNT] = { {0,0,0,0,0,-1}
49+
#if UART_BUF_CNT == 2
50+
, { 0,0,0,0,0,-1 }
51+
#endif
52+
};
53+
54+
uartbuf_t * UART_GetBufFromPort(int aport) {
55+
return &uartbuf[UART_GetBufIndexFromPort(aport)];
56+
}
57+
58+
int get_g_uart_init_counter() {
59+
uartbuf_t* fuartbuf = UART_GetBufFromPort(UART_GetSelectedPortIndex());
60+
return fuartbuf->g_uart_init_counter;
61+
}
1862

19-
void UART_InitReceiveRingBuffer(int size){
20-
//XJIKKA 20241122 - Note that the actual usable buffer size must be g_recvBufSize-1,
63+
void UART_InitReceiveRingBufferEx(int auartindex, int size){
64+
uartbuf_t* fuartbuf=UART_GetBufFromPort(auartindex);
65+
//XJIKKA 20241122 - Note that the actual usable buffer size must be g_recvBufSize-1,
2166
//otherwise there would be no difference between an empty and a full buffer.
22-
if(g_recvBuf!=0)
23-
free(g_recvBuf);
24-
g_recvBuf = (byte*)malloc(size);
25-
memset(g_recvBuf,0,size);
26-
g_recvBufSize = size;
27-
g_recvBufIn = 0;
28-
g_recvBufOut = 0;
67+
if(fuartbuf->g_recvBuf!=0)
68+
free(fuartbuf->g_recvBuf);
69+
fuartbuf->g_recvBuf = (byte*)malloc(size);
70+
memset(fuartbuf->g_recvBuf,0,size);
71+
fuartbuf->g_recvBufSize = size;
72+
fuartbuf->g_recvBufIn = 0;
73+
fuartbuf->g_recvBufOut = 0;
74+
}
75+
76+
void UART_InitReceiveRingBuffer(int size) {
77+
int fuartindex = UART_GetSelectedPortIndex();
78+
UART_InitReceiveRingBufferEx(fuartindex, size);
79+
}
80+
81+
int UART_GetReceiveRingBufferSizeEx(int auartindex) {
82+
uartbuf_t* fuartbuf = UART_GetBufFromPort(auartindex);
83+
return fuartbuf->g_recvBufSize;
84+
}
85+
86+
int UART_GetReceiveRingBufferSize() {
87+
int fuartindex = UART_GetSelectedPortIndex();
88+
return UART_GetReceiveRingBufferSizeEx(fuartindex);
89+
}
90+
91+
int UART_GetDataSizeEx(int auartindex) {
92+
uartbuf_t* fuartbuf = UART_GetBufFromPort(auartindex);
93+
return (fuartbuf->g_recvBufIn >= fuartbuf->g_recvBufOut
94+
? fuartbuf->g_recvBufIn - fuartbuf->g_recvBufOut
95+
: fuartbuf->g_recvBufIn + (fuartbuf->g_recvBufSize - fuartbuf->g_recvBufOut)); //XJIKKA 20241122 fixed buffer size calculation on ring bufferroverflow
2996
}
3097

3198
int UART_GetDataSize() {
32-
return (g_recvBufIn >= g_recvBufOut
33-
? g_recvBufIn - g_recvBufOut
34-
: g_recvBufIn + (g_recvBufSize - g_recvBufOut)); //XJIKKA 20241122 fixed buffer size calculation on ring bufferroverflow
99+
int fuartindex = UART_GetSelectedPortIndex();
100+
return UART_GetDataSizeEx(fuartindex);
101+
}
102+
103+
byte UART_GetByteEx(int auartindex, int idx) {
104+
uartbuf_t* fuartbuf = UART_GetBufFromPort(auartindex);
105+
return fuartbuf->g_recvBuf[(fuartbuf->g_recvBufOut + idx) % fuartbuf->g_recvBufSize];
35106
}
36107

37108
byte UART_GetByte(int idx) {
38-
return g_recvBuf[(g_recvBufOut + idx) % g_recvBufSize];
109+
int fuartindex = UART_GetSelectedPortIndex();
110+
return UART_GetByteEx(fuartindex, idx);
111+
}
112+
113+
void UART_ConsumeBytesEx(int auartindex, int idx) {
114+
uartbuf_t* fuartbuf = UART_GetBufFromPort(auartindex);
115+
fuartbuf->g_recvBufOut += idx;
116+
fuartbuf->g_recvBufOut %= fuartbuf->g_recvBufSize;
39117
}
40118

41119
void UART_ConsumeBytes(int idx) {
42-
g_recvBufOut += idx;
43-
g_recvBufOut %= g_recvBufSize;
120+
int fuartindex = UART_GetSelectedPortIndex();
121+
UART_ConsumeBytesEx(fuartindex, idx);
44122
}
45123

46-
void UART_AppendByteToReceiveRingBuffer(int rc) {
47-
if (UART_GetDataSize() < (g_recvBufSize - 1)) {
48-
g_recvBuf[g_recvBufIn++] = rc;
49-
g_recvBufIn %= g_recvBufSize;
124+
void UART_AppendByteToReceiveRingBufferEx(int auartindex, int rc) {
125+
uartbuf_t* fuartbuf = UART_GetBufFromPort(auartindex);
126+
if (fuartbuf->g_recvBufSize <= 0) {
127+
//if someone (uartFakeHex) send data without init, and if flag 26 changes(UART)
128+
addLogAdv(LOG_ERROR, LOG_FEATURE_DRV, "UART %i not initialized\n",auartindex);
129+
//return;
130+
UART_InitReceiveRingBufferEx(auartindex,UART_DEFAULT_BUFIZE);
50131
}
132+
#ifdef UART_ALWAYSFIRSTBYTES
133+
//20250119 old style, if g_recvBufSize-1 is reached, received byte was ignored
134+
if (UART_GetDataSizeEx(auartindex) < (fuartbuf->g_recvBufSize - 1)) {
135+
#else
136+
//20250119 new style, we have always last g_recvBufSize-1 bytes
137+
//if g_recvBufSize-1 is reached, first byte is overwritten
138+
#endif
139+
fuartbuf->g_recvBuf[fuartbuf->g_recvBufIn++] = rc;
140+
fuartbuf->g_recvBufIn %= fuartbuf->g_recvBufSize;
141+
#ifdef UART_ALWAYSFIRSTBYTES
142+
}
143+
#endif
51144
//XJIKKA 20241122 if the same pointer is reached (in and out), we must also advance
52145
//the outbuffer pointer, otherwise UART_GetDataSize will return g_recvBufSize.
53146
//This way now we always have the last (g_recvBufSize - 1) bytes
54-
if (g_recvBufIn == g_recvBufOut) {
55-
g_recvBufOut++;
56-
g_recvBufOut %= g_recvBufSize;
147+
if (fuartbuf->g_recvBufIn == fuartbuf->g_recvBufOut) {
148+
fuartbuf->g_recvBufOut++;
149+
fuartbuf->g_recvBufOut %= fuartbuf->g_recvBufSize;
57150
}
58151
}
59152

60-
void UART_SendByte(byte b)
61-
{
62-
HAL_UART_SendByte(b);
153+
void UART_AppendByteToReceiveRingBuffer(int rc) {
154+
int fuartindex = UART_GetSelectedPortIndex();
155+
UART_AppendByteToReceiveRingBufferEx(fuartindex, rc);
156+
}
157+
158+
void UART_SendByteEx(int auartindex, byte b) {
159+
#ifdef UART_2_UARTS_CONCURRENT
160+
HAL_UART_SendByteEx(auartindex, b);
161+
#else
162+
HAL_UART_SendByte(b);
163+
#endif
164+
}
165+
166+
void UART_SendByte(byte b) {
167+
int fuartindex = UART_GetSelectedPortIndex();
168+
UART_SendByteEx(fuartindex, b);
63169
}
64170

65171
commandResult_t CMD_UART_Send_Hex(const void *context, const char *cmd, const char *args, int cmdFlags) {
@@ -116,7 +222,7 @@ commandResult_t CMD_UART_FakeHex(const void *context, const char *cmd, const cha
116222

117223
args += 2;
118224
}
119-
return 1;
225+
return CMD_RES_OK;
120226
}
121227

122228
// uartSendASCII test123
@@ -135,45 +241,65 @@ commandResult_t CMD_UART_Send_ASCII(const void *context, const char *cmd, const
135241
return CMD_RES_OK;
136242
}
137243

138-
void UART_ResetForSimulator()
139-
{
140-
g_uart_init_counter = 0;
244+
void UART_ResetForSimulator() {
245+
for (int i = 0; i < UART_BUF_CNT; i++) {
246+
uartbuf[i].g_uart_init_counter = 0;
247+
}
141248
}
142249

143-
int UART_InitUART(int baud, int parity)
250+
int UART_InitUARTEx(int auartindex, int baud, int parity)
144251
{
145-
g_uart_init_counter++;
252+
uartbuf_t* fuartbuf = UART_GetBufFromPort(auartindex);
253+
fuartbuf->g_uart_init_counter++;
254+
#ifdef UART_2_UARTS_CONCURRENT
255+
HAL_UART_InitEx(auartindex, baud, parity);
256+
#else
146257
HAL_UART_Init(baud, parity);
147-
return g_uart_init_counter;
258+
#endif
259+
return fuartbuf->g_uart_init_counter;
148260
}
149261

150-
void UART_DebugTool_Run() {
262+
int UART_InitUART(int baud, int parity) {
263+
int fuartindex = UART_GetSelectedPortIndex();
264+
return UART_InitUARTEx(fuartindex, baud, parity);
265+
}
266+
267+
void UART_LogBufState(int auartindex) {
268+
uartbuf_t* fuartbuf = UART_GetBufFromPort(auartindex);
269+
ADDLOG_WARN(LOG_INFO,
270+
"Uart ix %d inbuf %i inptr %i outptr %i \n",
271+
auartindex, UART_GetDataSizeEx(auartindex), fuartbuf->g_recvBufIn, fuartbuf->g_recvBufOut
272+
);
273+
}
274+
void UART_DebugTool_Run(int auartindex) {
151275
byte b;
152276
char tmp[128];
153277
char *p = tmp;
154278
int i;
155279

156280
for (i = 0; i < sizeof(tmp) - 4; i++) {
157-
if (UART_GetDataSize()==0) {
281+
if (UART_GetDataSizeEx(auartindex)==0) {
158282
break;
159283
}
160-
b = UART_GetByte(0);
284+
b = UART_GetByteEx(auartindex,0);
161285
if (i) {
162286
*p = ' ';
163287
p++;
164288
}
165289
sprintf(p, "%02X", b);
166290
p += 2;
167-
UART_ConsumeBytes(1);
291+
UART_ConsumeBytesEx(auartindex,1);
168292
}
169293
*p = 0;
170-
addLogAdv(LOG_INFO, LOG_FEATURE_CMD, "UART received: %s\n", tmp);
294+
addLogAdv(LOG_INFO, LOG_FEATURE_CMD, "UART %i received: %s\n", auartindex, tmp);
171295
}
172296

173297
void UART_RunEverySecond() {
174-
if (g_uart_manualInitCounter == g_uart_init_counter) {
175-
UART_DebugTool_Run();
298+
for (int i = 0; i < UART_BUF_CNT; i++) {
299+
if (uartbuf[i].g_uart_manualInitCounter == uartbuf[i].g_uart_init_counter) {
300+
UART_DebugTool_Run(i);
176301
}
302+
}
177303
}
178304

179305
commandResult_t CMD_UART_Init(const void *context, const char *cmd, const char *args, int cmdFlags) {
@@ -187,12 +313,15 @@ commandResult_t CMD_UART_Init(const void *context, const char *cmd, const char *
187313
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
188314
}
189315

316+
int fuartindex = UART_GetSelectedPortIndex();
317+
190318
baud = Tokenizer_GetArgInteger(0);
191319

192-
UART_InitReceiveRingBuffer(512);
320+
UART_InitReceiveRingBufferEx(fuartindex, UART_DEFAULT_BUFIZE);
193321

194-
UART_InitUART(baud, 0);
195-
g_uart_manualInitCounter = g_uart_init_counter;
322+
UART_InitUARTEx(fuartindex, baud, 0);
323+
uartbuf_t* fuartbuf = UART_GetBufFromPort(fuartindex);
324+
fuartbuf->g_uart_manualInitCounter = fuartbuf->g_uart_init_counter;
196325

197326
return CMD_RES_OK;
198327
}

src/driver/drv_uart.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#pragma once
22

3+
//---------------------------------------------------
4+
// Routines using UART port depending on config
5+
// flag OBK_FLAG_USE_SECONDARY_UART
6+
// Note: using same buffer as routines below
7+
//--------------------------------------------------
38
void UART_InitReceiveRingBuffer(int size);
9+
int UART_GetReceiveRingBufferSize();
410
int UART_GetDataSize();
511
byte UART_GetByte(int idx);
612
void UART_ConsumeBytes(int idx);
@@ -11,4 +17,29 @@ void UART_AddCommands();
1117
void UART_RunEverySecond();
1218

1319
// used to detect uart reinit/takeover by driver
14-
extern int g_uart_init_counter;
20+
int get_g_uart_init_counter();
21+
// used to get selected port from config - OBK_FLAG_USE_SECONDARY_UART
22+
int UART_GetSelectedPortIndex();
23+
24+
//---------------------------------------------------
25+
// XJIKKA 20241123 new routines with uart index param
26+
// BEKEN platform only (yet)
27+
// Independent of OBK_FLAG_USE_SECONDARY_UART
28+
//---------------------------------------------------
29+
//index of UART port
30+
// BEKEN - UART_PORT_INDEX_0 = BK_UART_1 RX1 TX1
31+
// BEKEN - UART_PORT_INDEX_1 = BK_UART_2 RX2 TX2
32+
#define UART_PORT_INDEX_0 0
33+
#define UART_PORT_INDEX_1 1
34+
//
35+
int UART_GetBufIndexFromPort(int aport);
36+
void UART_InitReceiveRingBufferEx(int auartindex, int size);
37+
void UART_AppendByteToReceiveRingBufferEx(int auartindex, int rc);
38+
int UART_GetReceiveRingBufferSizeEx(int auartindex);
39+
int UART_GetDataSizeEx(int auartindex);
40+
byte UART_GetByteEx(int auartindex, int idx);
41+
void UART_ConsumeBytesEx(int auartindex, int idx);
42+
void UART_SendByteEx(int auartindex, byte b);
43+
int UART_InitUARTEx(int auartindex, int baud, int parity);
44+
void UART_LogBufState(int auartindex);
45+

0 commit comments

Comments
 (0)