Adds uart.getconfig(0) to get the current uart parameters (#1658)

This commit is contained in:
Philip Gladstone 2016-12-11 14:35:04 -05:00 committed by Marcel Stör
parent 12a7b1c239
commit 017b4637c2
6 changed files with 161 additions and 11 deletions

View File

@ -356,3 +356,18 @@ uart_setup(uint8 uart_no)
void ICACHE_FLASH_ATTR uart_set_alt_output_uart0(void (*fn)(char)) { void ICACHE_FLASH_ATTR uart_set_alt_output_uart0(void (*fn)(char)) {
alt_uart0_tx = fn; alt_uart0_tx = fn;
} }
UartConfig ICACHE_FLASH_ATTR uart_get_config(uint8 uart_no) {
UartConfig config;
config.baut_rate = UART_CLK_FREQ / READ_PERI_REG(UART_CLKDIV(uart_no));
uint32_t conf = READ_PERI_REG(UART_CONF0(uart_no));
config.exist_parity = (conf >> UART_PARITY_EN_S) & UART_PARITY_EN_M;
config.parity = (conf >> UART_PARITY_S) & UART_PARITY_M;
config.stop_bits = (conf >> UART_STOP_BIT_NUM_S) & UART_STOP_BIT_NUM;
config.data_bits = (conf >> UART_BIT_NUM_S) & UART_BIT_NUM;
return config;
}

View File

@ -101,7 +101,16 @@ typedef struct {
int buff_uart_no; //indicate which uart use tx/rx buffer int buff_uart_no; //indicate which uart use tx/rx buffer
} UartDevice; } UartDevice;
typedef struct {
UartBautRate baut_rate;
UartBitsNum4Char data_bits;
UartExistParity exist_parity;
UartParityMode parity;
UartStopBitsNum stop_bits;
} UartConfig;
void uart_init(UartBautRate uart0_br, UartBautRate uart1_br, os_signal_t sig_input, uint8 *flag_input); void uart_init(UartBautRate uart0_br, UartBautRate uart1_br, os_signal_t sig_input, uint8 *flag_input);
UartConfig uart_get_config(uint8 uart_no);
void uart0_alt(uint8 on); void uart0_alt(uint8 on);
void uart0_sendStr(const char *str); void uart0_sendStr(const char *str);
void uart0_putc(const char c); void uart0_putc(const char c);

View File

@ -27,7 +27,7 @@ bool uart_on_data_cb(const char *buf, size_t len){
uint16_t need_len = 0; uint16_t need_len = 0;
int16_t end_char = -1; int16_t end_char = -1;
// Lua: uart.on("method", [number/char], function, [run_input]) // Lua: uart.on("method", [number/char], function, [run_input])
static int uart_on( lua_State* L ) static int l_uart_on( lua_State* L )
{ {
size_t sl, el; size_t sl, el;
int32_t run = 1; int32_t run = 1;
@ -89,13 +89,14 @@ static int uart_on( lua_State* L )
bool uart0_echo = true; bool uart0_echo = true;
// Lua: actualbaud = setup( id, baud, databits, parity, stopbits, echo ) // Lua: actualbaud = setup( id, baud, databits, parity, stopbits, echo )
static int uart_setup( lua_State* L ) static int l_uart_setup( lua_State* L )
{ {
unsigned id, databits, parity, stopbits, echo = 1; uint32_t id, databits, parity, stopbits, echo = 1;
u32 baud, res; uint32_t baud, res;
id = luaL_checkinteger( L, 1 ); id = luaL_checkinteger( L, 1 );
MOD_CHECK_ID( uart, id ); MOD_CHECK_ID( uart, id );
baud = luaL_checkinteger( L, 2 ); baud = luaL_checkinteger( L, 2 );
databits = luaL_checkinteger( L, 3 ); databits = luaL_checkinteger( L, 3 );
parity = luaL_checkinteger( L, 4 ); parity = luaL_checkinteger( L, 4 );
@ -113,8 +114,26 @@ static int uart_setup( lua_State* L )
return 1; return 1;
} }
// uart.getconfig(id)
static int l_uart_getconfig( lua_State* L )
{
uint32_t id, databits, parity, stopbits;
uint32_t baud;
id = luaL_checkinteger( L, 1 );
MOD_CHECK_ID( uart, id );
platform_uart_get_config(id, &baud, &databits, &parity, &stopbits);
lua_pushinteger(L, baud);
lua_pushinteger(L, databits);
lua_pushinteger(L, parity);
lua_pushinteger(L, stopbits);
return 4;
}
// Lua: alt( set ) // Lua: alt( set )
static int uart_alt( lua_State* L ) static int l_uart_alt( lua_State* L )
{ {
unsigned set; unsigned set;
@ -125,7 +144,7 @@ static int uart_alt( lua_State* L )
} }
// Lua: write( id, string1, [string2], ..., [stringn] ) // Lua: write( id, string1, [string2], ..., [stringn] )
static int uart_write( lua_State* L ) static int l_uart_write( lua_State* L )
{ {
int id; int id;
const char* buf; const char* buf;
@ -156,10 +175,11 @@ static int uart_write( lua_State* L )
// Module function map // Module function map
static const LUA_REG_TYPE uart_map[] = { static const LUA_REG_TYPE uart_map[] = {
{ LSTRKEY( "setup" ), LFUNCVAL( uart_setup ) }, { LSTRKEY( "setup" ), LFUNCVAL( l_uart_setup ) },
{ LSTRKEY( "write" ), LFUNCVAL( uart_write ) }, { LSTRKEY( "getconfig" ), LFUNCVAL( l_uart_getconfig ) },
{ LSTRKEY( "on" ), LFUNCVAL( uart_on ) }, { LSTRKEY( "write" ), LFUNCVAL( l_uart_write ) },
{ LSTRKEY( "alt" ), LFUNCVAL( uart_alt ) }, { LSTRKEY( "on" ), LFUNCVAL( l_uart_on ) },
{ LSTRKEY( "alt" ), LFUNCVAL( l_uart_alt ) },
{ LSTRKEY( "STOPBITS_1" ), LNUMVAL( PLATFORM_UART_STOPBITS_1 ) }, { LSTRKEY( "STOPBITS_1" ), LNUMVAL( PLATFORM_UART_STOPBITS_1 ) },
{ LSTRKEY( "STOPBITS_1_5" ), LNUMVAL( PLATFORM_UART_STOPBITS_1_5 ) }, { LSTRKEY( "STOPBITS_1_5" ), LNUMVAL( PLATFORM_UART_STOPBITS_1_5 ) },
{ LSTRKEY( "STOPBITS_2" ), LNUMVAL( PLATFORM_UART_STOPBITS_2 ) }, { LSTRKEY( "STOPBITS_2" ), LNUMVAL( PLATFORM_UART_STOPBITS_2 ) },

View File

@ -32,6 +32,26 @@ static struct gpio_hook platform_gpio_hook;
#endif #endif
#endif #endif
static const int uart_bitrates[] = {
BIT_RATE_300,
BIT_RATE_600,
BIT_RATE_1200,
BIT_RATE_2400,
BIT_RATE_4800,
BIT_RATE_9600,
BIT_RATE_19200,
BIT_RATE_38400,
BIT_RATE_57600,
BIT_RATE_74880,
BIT_RATE_115200,
BIT_RATE_230400,
BIT_RATE_256000,
BIT_RATE_460800,
BIT_RATE_921600,
BIT_RATE_1843200,
BIT_RATE_3686400
};
int platform_init() int platform_init()
{ {
// Setup the various forward and reverse mappings for the pins // Setup the various forward and reverse mappings for the pins
@ -410,6 +430,64 @@ uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int pari
return baud; return baud;
} }
void platform_uart_get_config(unsigned id, uint32_t *baudp, uint32_t *databitsp, uint32_t *parityp, uint32_t *stopbitsp) {
UartConfig config = uart_get_config(id);
int i;
int offset = config.baut_rate;
for (i = 0; i < sizeof(uart_bitrates) / sizeof(uart_bitrates[0]); i++) {
int diff = config.baut_rate - uart_bitrates[i];
if (diff < 0) {
diff = -diff;
}
if (diff < offset) {
offset = diff;
*baudp = uart_bitrates[i];
}
}
switch( config.data_bits )
{
case FIVE_BITS:
*databitsp = 5;
break;
case SIX_BITS:
*databitsp = 6;
break;
case SEVEN_BITS:
*databitsp = 7;
break;
case EIGHT_BITS:
default:
*databitsp = 8;
break;
}
switch (config.stop_bits)
{
case ONE_HALF_STOP_BIT:
*stopbitsp = PLATFORM_UART_STOPBITS_1_5;
break;
case TWO_STOP_BIT:
*stopbitsp = PLATFORM_UART_STOPBITS_2;
break;
default:
*stopbitsp = PLATFORM_UART_STOPBITS_1;
break;
}
if (config.exist_parity == STICK_PARITY_DIS) {
*parityp = PLATFORM_UART_PARITY_NONE;
} else if (config.parity == EVEN_BITS) {
*parityp = PLATFORM_UART_PARITY_EVEN;
} else {
*parityp = PLATFORM_UART_PARITY_ODD;
}
}
// if set=1, then alternate serial output pins are used. (15=rx, 13=tx) // if set=1, then alternate serial output pins are used. (15=rx, 13=tx)
void platform_uart_alt( int set ) void platform_uart_alt( int set )
{ {

View File

@ -7,6 +7,7 @@
#include "c_types.h" #include "c_types.h"
#include "driver/pwm.h" #include "driver/pwm.h"
#include "driver/uart.h"
#include "task/task.h" #include "task/task.h"
// Error / status codes // Error / status codes
@ -161,6 +162,7 @@ int platform_s_uart_recv( unsigned id, timer_data_type timeout );
int platform_uart_set_flow_control( unsigned id, int type ); int platform_uart_set_flow_control( unsigned id, int type );
int platform_s_uart_set_flow_control( unsigned id, int type ); int platform_s_uart_set_flow_control( unsigned id, int type );
void platform_uart_alt( int set ); void platform_uart_alt( int set );
void platform_uart_get_config(unsigned id, uint32_t *baudp, uint32_t *databitsp, uint32_t *parityp, uint32_t *stopbitsp);
// ***************************************************************************** // *****************************************************************************
// PWM subsection // PWM subsection

View File

@ -67,7 +67,7 @@ end, 0)
## uart.setup() ## uart.setup()
(Re-)configures the communication parameters of the UART. (Re-)configures the communication parameters of the UART.
#### Syntax #### Syntax
`uart.setup(id, baud, databits, parity, stopbits, echo)` `uart.setup(id, baud, databits, parity, stopbits, echo)`
@ -89,6 +89,32 @@ configured baud rate (number)
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1) uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
``` ```
## uart.getconfig()
Returns the current configuration parameters of the UART.
#### Syntax
`uart.getconfig(id)`
#### Parameters
- `id` always zero, only one uart supported
#### Returns
Four values as follows:
- `baud` one of 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400
- `databits` one of 5, 6, 7, 8
- `parity` `uart.PARITY_NONE`, `uart.PARITY_ODD`, or `uart.PARITY_EVEN`
- `stopbits` `uart.STOPBITS_1`, `uart.STOPBITS_1_5`, or `uart.STOPBITS_2`
#### Example
```lua
print (uart.getconfig(0))
-- prints 9600 8 0 1 for 9600, 8N1
```
## uart.write() ## uart.write()
Write string or byte to the UART. Write string or byte to the UART.