add board_getchar() for non-blocking getchar()

This commit is contained in:
hathach
2022-11-21 16:28:54 +07:00
parent 460bef9dbb
commit a394273ed2
4 changed files with 16 additions and 8 deletions

View File

@@ -76,13 +76,13 @@ void msc_app_task(void)
{ {
if (!_cli) return; if (!_cli) return;
int ch = board_uart_getchar(); int ch = board_getchar();
if ( ch > 0 ) if ( ch > 0 )
{ {
while( ch > 0 ) while( ch > 0 )
{ {
embeddedCliReceiveChar(_cli, (char) ch); embeddedCliReceiveChar(_cli, (char) ch);
ch = board_uart_getchar(); ch = board_getchar();
} }
embeddedCliProcess(_cli); embeddedCliProcess(_cli);
} }

View File

@@ -106,6 +106,7 @@ TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count)
int rd = (int) SEGGER_RTT_Read(0, buf, count); int rd = (int) SEGGER_RTT_Read(0, buf, count);
return (rd > 0) ? rd : -1; return (rd > 0) ? rd : -1;
} }
#endif #endif
#elif defined(LOGGER_SWO) #elif defined(LOGGER_SWO)
@@ -149,3 +150,9 @@ TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count)
} }
#endif #endif
int board_getchar(void)
{
char c;
return ( sys_read(0, &c, 1) > 0 ) ? (int) c : (-1);
}

View File

@@ -132,12 +132,8 @@ static inline void board_delay(uint32_t ms)
} }
} }
// stdio getchar() is blocking, this is non-blocking version for uart // stdio getchar() is blocking, this is non-blocking version
static inline int board_uart_getchar(void) int board_getchar(void);
{
uint8_t c;
return ( board_uart_read(&c, 1) > 0 ) ? (int) c : (-1);
}
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -219,6 +219,11 @@ int board_uart_write(void const * buf, int len)
#endif #endif
} }
int board_getchar(void)
{
return getchar_timeout_us(0);
}
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// USB Interrupt Handler // USB Interrupt Handler
// rp2040 implementation will install approriate handler when initializing // rp2040 implementation will install approriate handler when initializing