Skip to content

Commit dc334f8

Browse files
authored
uart: expose fifo depth counters (#3177)
1 parent 139af0c commit dc334f8

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

app/modules/uart.c

+26
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,45 @@ static int l_uart_write( lua_State* L )
142142
return 0;
143143
}
144144

145+
#define DIR_RX 0
146+
#define DIR_TX 1
147+
148+
static int l_uart_fifodepth( lua_State* L )
149+
{
150+
int id = luaL_optinteger( L, 1, 0 );
151+
if ((id != 0) && (id != 1))
152+
return luaL_argerror(L, 1, "Bad UART id; must be 0 or 1");
153+
154+
int dir = luaL_optinteger( L, 2, 0 );
155+
if ((dir != DIR_RX) && (dir != DIR_TX))
156+
return luaL_argerror(L, 2, "Bad direction; must be DIR_RX or DIR_TX");
157+
158+
int reg = READ_PERI_REG(UART_STATUS(id));
159+
int rsh = reg >> ((dir == DIR_RX) ? UART_RXFIFO_CNT_S : UART_TXFIFO_CNT_S);
160+
int rm = rsh & ((dir == DIR_RX) ? UART_RXFIFO_CNT : UART_TXFIFO_CNT );
161+
162+
lua_pushinteger(L, rm);
163+
return 1;
164+
}
165+
145166
// Module function map
146167
LROT_BEGIN(uart, NULL, 0)
147168
LROT_FUNCENTRY( setup, l_uart_setup )
148169
LROT_FUNCENTRY( getconfig, l_uart_getconfig )
149170
LROT_FUNCENTRY( write, l_uart_write )
150171
LROT_FUNCENTRY( on, l_uart_on )
151172
LROT_FUNCENTRY( alt, l_uart_alt )
173+
LROT_FUNCENTRY( fifodepth, l_uart_fifodepth )
174+
152175
LROT_NUMENTRY( STOPBITS_1, PLATFORM_UART_STOPBITS_1 )
153176
LROT_NUMENTRY( STOPBITS_1_5, PLATFORM_UART_STOPBITS_1_5 )
154177
LROT_NUMENTRY( STOPBITS_2, PLATFORM_UART_STOPBITS_2 )
155178
LROT_NUMENTRY( PARITY_NONE, PLATFORM_UART_PARITY_NONE )
156179
LROT_NUMENTRY( PARITY_EVEN, PLATFORM_UART_PARITY_EVEN )
157180
LROT_NUMENTRY( PARITY_ODD, PLATFORM_UART_PARITY_ODD )
181+
182+
LROT_NUMENTRY( DIR_RX, DIR_RX )
183+
LROT_NUMENTRY( DIR_TX, DIR_TX )
158184
LROT_END(uart, NULL, 0)
159185

160186

docs/modules/uart.md

+14
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,17 @@ Write string or byte to the UART.
145145
uart.write(0, "Hello, world\n")
146146
```
147147

148+
## uart.fifodepth()
149+
150+
Report the depth, in bytes, of TX or RX hardware queues associated with the
151+
UART.
152+
153+
#### Syntax
154+
`uart.fifodepth(id, dir)`
155+
156+
#### Parameters
157+
- `id` UART id (0 or 1).
158+
- `dir` `uart.DIR_RX` for the RX FIFO, `uart.DIR_TX` for TX FIFO.
159+
160+
#### Returns
161+
The number of bytes in the selected FIFO.

0 commit comments

Comments
 (0)