Trying to understand uart_config and uart_div_modify

alonso
Posts: 2
Joined: Wed Nov 18, 2015 10:35 pm

Trying to understand uart_config and uart_div_modify

Postby alonso » Sat May 21, 2016 5:44 am

I am trying to understand and figure out the minimum requirements to get TX working and changing baud rates at will. (I looked at examples/driver_lib/driver/uart.c and other sources)

The software is using esp_iot_sdk_v1.5.2

I have condensed what I though was the basics to the following:

#include "eagle_soc.h"
#include "uart_register.h"

#ifndef uint32
typedef unsigned long uint32;
#endif

#ifndef uint32_t
typedef uint32 uint32_t;
#endif

#ifndef uint8
typedef unsigned char uint8;
#endif

#ifndef BIT
#define BIT(nr) (1UL << (nr))
#endif

//extern UartDevice UartDev;

static void uart_config(uint8 uart_no, uint32 baudrate) {
if (uart_no == 1) {
// Set the TX pin for UART1 (GPIO2) and disable pullup
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
//PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD);
//PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U);
}
else {
// Set the TX pin for UART0 (GPIO1) and disable pullup
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U);
}

// Set the protocol info (normally 8N1)
WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT));

// clear the tx FIFO queue
SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_TXFIFO_RST);
CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_TXFIFO_RST);

// clear all pending UART interrupts
WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
}

void call_user_start(void) {
uint8 loop;
int i;
int bidx;
uint32 bauds[] = {9600, 57600, 115200, 230400};

for(i = 0; i < 5; i++) {
ets_printf("testbaud baud inherited\r\n");
}

for(bidx=(sizeof(bauds)/sizeof(bauds[0]))-1; bidx >=0; bidx--) {

uart_div_modify(1, UART_CLK_FREQ / bauds[bidx]);

/* SET_PERI_REG_BITS(UART_CLKDIV(1), UART_CLKDIV_CNT, UART_CLK_FREQ / bauds[bidx], */
/* UART_CLKDIV_S); */

ets_printf("bidx=%d\n",bidx);
ets_printf("Baudrate changed to %u\n", bauds[bidx]);

uart_config(1,bauds[bidx]);

ets_delay_us(20000);

for(i = 0; i < 5; i++) {
ets_printf("i=%d\n",i);
ets_delay_us(2000);
}
}

}


I have a terminal program looking at UART1 that escapes binary (as in any characters it does not know how to print in ascii)

I expected the terminal program to show only the characters for the baudrate that matches, but it will only show those that match the upload speed via esptool.

This is an example of the output when the terminal listens at 230400 and the same was used with esptool

12 ‡00‡00@@‡01‡08‡02‡00‡07‡07‡12 ‡00‡00@@‡01‡08‡02‡00‡07‡07‡12 ‡00‡00@@‡01‡02‡02‡00‡07‡07‡12 ‡00‡00@@‡01‡03‡02‡00‡07‡07‡12 ‡00‡00@@‡01‡02‡02‡00‡07‡07‡12 ‡00‡00@@‡01‡04‡02‡00‡07‡07‡12 ‡00‡00@load 0x40100000, len 364, room 16
2016-05-20 16:40:48.528897500 tail 12
2016-05-20 16:40:48.530046500 chksum 0xc7
2016-05-20 16:40:48.530047500 ho 0 tail 12 room 4
2016-05-20 16:40:48.530047500 load 0x3ffe8004, len 96, room 12
2016-05-20 16:40:48.530918500 tail 4
2016-05-20 16:40:48.530918500 chksum 0xff
2016-05-20 16:40:48.531993500 csum 0xff
2016-05-20 16:40:48.531994500 testbaud baud inherited
2016-05-20 16:40:48.532914500 testbaud baud inherited
2016-05-20 16:40:48.532915500 testbaud baud inherited
2016-05-20 16:40:48.533995500 testbaud baud inherited
2016-05-20 16:40:48.533996500 testbaud baud inherited
2016-05-20 16:40:48.534935500 bidx=3
2016-05-20 16:40:48.534935500 Baudrate changed to 230400‡7f


Double daggers (as in ‡) are following by the 2 digit hex code of any escaped characters.

Clearly I am missing something while configuring the minimum UART registers.

Can someone let me know what?

Thanks,

Alberto

Who is online

Users browsing this forum: No registered users and 235 guests