2021-03-11 11:11:53 +01:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
2016-10-06 13:21:30 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "esp32-hal-uart.h"
|
2016-10-14 02:02:40 +02:00
|
|
|
#include "esp32-hal.h"
|
2016-10-06 13:21:30 +02:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2016-10-14 02:02:40 +02:00
|
|
|
#include "freertos/queue.h"
|
2016-10-06 13:21:30 +02:00
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "soc/uart_reg.h"
|
2016-10-14 02:02:40 +02:00
|
|
|
#include "soc/uart_struct.h"
|
2016-10-06 13:21:30 +02:00
|
|
|
#include "soc/io_mux_reg.h"
|
|
|
|
#include "soc/gpio_sig_map.h"
|
2018-12-20 01:57:32 +01:00
|
|
|
#include "soc/rtc.h"
|
2021-04-14 17:10:05 +02:00
|
|
|
#include "hal/uart_ll.h"
|
2017-09-27 07:50:05 +02:00
|
|
|
#include "esp_intr_alloc.h"
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
#include "esp_system.h"
|
|
|
|
#ifdef ESP_IDF_VERSION_MAJOR // IDF 4+
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
|
2021-04-14 17:10:05 +02:00
|
|
|
#include "soc/dport_reg.h"
|
2021-04-05 13:23:58 +02:00
|
|
|
#include "esp32/rom/ets_sys.h"
|
|
|
|
#include "esp32/rom/uart.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2021-04-14 17:10:05 +02:00
|
|
|
#include "soc/dport_reg.h"
|
2021-04-05 13:23:58 +02:00
|
|
|
#include "esp32s2/rom/ets_sys.h"
|
|
|
|
#include "esp32s2/rom/uart.h"
|
|
|
|
#include "soc/periph_defs.h"
|
2021-04-14 17:10:05 +02:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/ets_sys.h"
|
|
|
|
#include "esp32c3/rom/uart.h"
|
|
|
|
#include "soc/periph_defs.h"
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
#error Target CONFIG_IDF_TARGET is not supported
|
|
|
|
#endif
|
|
|
|
#else // ESP32 Before IDF 4.0
|
|
|
|
#include "rom/ets_sys.h"
|
|
|
|
#include "rom/uart.h"
|
|
|
|
#include "esp_intr.h"
|
|
|
|
#endif
|
|
|
|
|
2021-04-14 17:10:05 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#define UART_PORTS_NUM 3
|
|
|
|
#define UART_REG_BASE(u) ((u==0)?DR_REG_UART_BASE:( (u==1)?DR_REG_UART1_BASE:( (u==2)?DR_REG_UART2_BASE:0)))
|
|
|
|
#define UART_RXD_IDX(u) ((u==0)?U0RXD_IN_IDX:( (u==1)?U1RXD_IN_IDX:( (u==2)?U2RXD_IN_IDX:0)))
|
|
|
|
#define UART_TXD_IDX(u) ((u==0)?U0TXD_OUT_IDX:( (u==1)?U1TXD_OUT_IDX:( (u==2)?U2TXD_OUT_IDX:0)))
|
|
|
|
#define UART_INTR_SOURCE(u) ((u==0)?ETS_UART0_INTR_SOURCE:( (u==1)?ETS_UART1_INTR_SOURCE:((u==2)?ETS_UART2_INTR_SOURCE:0)))
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2021-04-05 13:23:58 +02:00
|
|
|
#define UART_PORTS_NUM 2
|
|
|
|
#define UART_REG_BASE(u) ((u==0)?DR_REG_UART_BASE:( (u==1)?DR_REG_UART1_BASE:0))
|
|
|
|
#define UART_RXD_IDX(u) ((u==0)?U0RXD_IN_IDX:( (u==1)?U1RXD_IN_IDX:0))
|
|
|
|
#define UART_TXD_IDX(u) ((u==0)?U0TXD_OUT_IDX:( (u==1)?U1TXD_OUT_IDX:0))
|
|
|
|
#define UART_INTR_SOURCE(u) ((u==0)?ETS_UART0_INTR_SOURCE:( (u==1)?ETS_UART1_INTR_SOURCE:0))
|
|
|
|
#else
|
2021-04-14 17:10:05 +02:00
|
|
|
#define UART_PORTS_NUM 2
|
|
|
|
#define UART_REG_BASE(u) ((u==0)?DR_REG_UART_BASE:( (u==1)?DR_REG_UART1_BASE:0))
|
|
|
|
#define UART_RXD_IDX(u) ((u==0)?U0RXD_IN_IDX:( (u==1)?U1RXD_IN_IDX:0))
|
|
|
|
#define UART_TXD_IDX(u) ((u==0)?U0TXD_OUT_IDX:( (u==1)?U1TXD_OUT_IDX:0))
|
|
|
|
#define UART_INTR_SOURCE(u) ((u==0)?ETS_UART0_INTR_SOURCE:( (u==1)?ETS_UART1_INTR_SOURCE:0))
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
static int s_uart_debug_nr = 0;
|
|
|
|
|
2016-10-14 02:02:40 +02:00
|
|
|
struct uart_struct_t {
|
|
|
|
uart_dev_t * dev;
|
2016-11-18 14:07:25 +01:00
|
|
|
#if !CONFIG_DISABLE_HAL_LOCKS
|
2016-10-14 02:07:21 +02:00
|
|
|
xSemaphoreHandle lock;
|
2016-11-18 14:07:25 +01:00
|
|
|
#endif
|
2016-10-14 02:02:40 +02:00
|
|
|
uint8_t num;
|
|
|
|
xQueueHandle queue;
|
2017-09-27 07:50:05 +02:00
|
|
|
intr_handle_t intr_handle;
|
2016-10-14 02:02:40 +02:00
|
|
|
};
|
|
|
|
|
2016-11-18 14:07:25 +01:00
|
|
|
#if CONFIG_DISABLE_HAL_LOCKS
|
|
|
|
#define UART_MUTEX_LOCK()
|
|
|
|
#define UART_MUTEX_UNLOCK()
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
static uart_t _uart_bus_array[] = {
|
|
|
|
{&UART0, 0, NULL, NULL},
|
|
|
|
{&UART1, 1, NULL, NULL},
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
{&UART2, 2, NULL, NULL}
|
|
|
|
#endif
|
2016-11-18 14:07:25 +01:00
|
|
|
};
|
|
|
|
#else
|
2017-06-03 19:10:15 +02:00
|
|
|
#define UART_MUTEX_LOCK() do {} while (xSemaphoreTake(uart->lock, portMAX_DELAY) != pdPASS)
|
|
|
|
#define UART_MUTEX_UNLOCK() xSemaphoreGive(uart->lock)
|
2016-10-14 02:07:21 +02:00
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
static uart_t _uart_bus_array[] = {
|
|
|
|
{&UART0, NULL, 0, NULL, NULL},
|
|
|
|
{&UART1, NULL, 1, NULL, NULL},
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
{&UART2, NULL, 2, NULL, NULL}
|
|
|
|
#endif
|
2016-10-14 02:02:40 +02:00
|
|
|
};
|
2016-11-18 14:07:25 +01:00
|
|
|
#endif
|
2016-10-14 02:02:40 +02:00
|
|
|
|
2019-01-09 10:07:54 +01:00
|
|
|
static void uart_on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb, uint32_t new_apb);
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
static void ARDUINO_ISR_ATTR _uart_isr(void *arg)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
uint8_t i, c;
|
2016-10-06 13:21:30 +02:00
|
|
|
BaseType_t xHigherPriorityTaskWoken;
|
2016-10-14 02:02:40 +02:00
|
|
|
uart_t* uart;
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
for(i=0;i<UART_PORTS_NUM;i++){
|
2016-10-14 02:02:40 +02:00
|
|
|
uart = &_uart_bus_array[i];
|
2017-09-27 07:50:05 +02:00
|
|
|
if(uart->intr_handle == NULL){
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-14 02:02:40 +02:00
|
|
|
uart->dev->int_clr.rxfifo_full = 1;
|
|
|
|
uart->dev->int_clr.frm_err = 1;
|
|
|
|
uart->dev->int_clr.rxfifo_tout = 1;
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-09-17 21:19:51 +02:00
|
|
|
while(uart->dev->status.rxfifo_cnt || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
|
2016-10-14 02:02:40 +02:00
|
|
|
c = uart->dev->fifo.rw_byte;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
uint32_t fifo_reg = UART_FIFO_AHB_REG(i);
|
|
|
|
while(uart->dev->status.rxfifo_cnt) {
|
|
|
|
c = ESP_REG(fifo_reg);
|
|
|
|
#endif
|
|
|
|
if(uart->queue != NULL) {
|
2016-10-14 02:02:40 +02:00
|
|
|
xQueueSendFromISR(uart->queue, &c, &xHigherPriorityTaskWoken);
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xHigherPriorityTaskWoken) {
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
static void uartEnableInterrupt(uart_t* uart, uint8_t rxfifo_full_thrhd)
|
2016-10-14 02:02:40 +02:00
|
|
|
{
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2021-04-08 14:29:53 +02:00
|
|
|
uart->dev->conf1.rxfifo_full_thrhd = rxfifo_full_thrhd;
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-10-14 02:02:40 +02:00
|
|
|
uart->dev->conf1.rx_tout_thrhd = 2;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
uart->dev->mem_conf.rx_tout_thrhd = 2;
|
|
|
|
#endif
|
2016-10-14 02:02:40 +02:00
|
|
|
uart->dev->conf1.rx_tout_en = 1;
|
|
|
|
uart->dev->int_ena.rxfifo_full = 1;
|
|
|
|
uart->dev->int_ena.frm_err = 1;
|
|
|
|
uart->dev->int_ena.rxfifo_tout = 1;
|
|
|
|
uart->dev->int_clr.val = 0xffffffff;
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
esp_intr_alloc(UART_INTR_SOURCE(uart->num), (int)ARDUINO_ISR_FLAG, _uart_isr, NULL, &uart->intr_handle);
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2016-10-14 02:02:40 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
static void uartDisableInterrupt(uart_t* uart)
|
2016-10-14 02:02:40 +02:00
|
|
|
{
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2021-07-21 16:02:06 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-10-14 02:02:40 +02:00
|
|
|
uart->dev->conf1.val = 0;
|
2021-07-21 16:02:06 +02:00
|
|
|
#endif
|
2016-10-14 02:02:40 +02:00
|
|
|
uart->dev->int_ena.val = 0;
|
|
|
|
uart->dev->int_clr.val = 0xffffffff;
|
2017-09-27 07:50:05 +02:00
|
|
|
|
|
|
|
esp_intr_free(uart->intr_handle);
|
|
|
|
uart->intr_handle = NULL;
|
|
|
|
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2016-10-14 02:02:40 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
static void uartDetachRx(uart_t* uart, uint8_t rxPin)
|
2016-10-14 02:02:40 +02:00
|
|
|
{
|
|
|
|
if(uart == NULL) {
|
|
|
|
return;
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
2021-07-15 01:07:43 +02:00
|
|
|
pinMatrixInDetach(UART_RXD_IDX(uart->num), false, false);
|
2016-10-14 02:02:40 +02:00
|
|
|
uartDisableInterrupt(uart);
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
static void uartDetachTx(uart_t* uart, uint8_t txPin)
|
2016-10-14 02:02:40 +02:00
|
|
|
{
|
|
|
|
if(uart == NULL) {
|
|
|
|
return;
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
2020-09-30 14:04:18 +02:00
|
|
|
pinMatrixOutDetach(txPin, false, false);
|
2016-10-14 02:02:40 +02:00
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
static void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted, uint8_t rxfifo_full_thrhd)
|
2016-10-14 02:02:40 +02:00
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
if(uart == NULL || rxPin >= GPIO_PIN_COUNT) {
|
2016-10-14 02:02:40 +02:00
|
|
|
return;
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
2016-10-14 02:02:40 +02:00
|
|
|
pinMode(rxPin, INPUT);
|
2021-04-08 14:29:53 +02:00
|
|
|
uartEnableInterrupt(uart, rxfifo_full_thrhd);
|
2021-04-05 13:23:58 +02:00
|
|
|
pinMatrixInAttach(rxPin, UART_RXD_IDX(uart->num), inverted);
|
2016-10-14 02:02:40 +02:00
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
static void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
|
2016-10-14 02:02:40 +02:00
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
if(uart == NULL || txPin >= GPIO_PIN_COUNT) {
|
2016-10-14 02:02:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pinMode(txPin, OUTPUT);
|
|
|
|
pinMatrixOutAttach(txPin, UART_TXD_IDX(uart->num), inverted, false);
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd)
|
2016-10-14 02:02:40 +02:00
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
if(uart_nr >= UART_PORTS_NUM) {
|
2016-10-14 02:02:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2016-10-14 02:02:40 +02:00
|
|
|
if(rxPin == -1 && txPin == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2016-10-14 02:02:40 +02:00
|
|
|
uart_t* uart = &_uart_bus_array[uart_nr];
|
2016-10-14 02:07:21 +02:00
|
|
|
|
2016-11-18 14:07:25 +01:00
|
|
|
#if !CONFIG_DISABLE_HAL_LOCKS
|
2016-10-14 02:07:21 +02:00
|
|
|
if(uart->lock == NULL) {
|
|
|
|
uart->lock = xSemaphoreCreateMutex();
|
|
|
|
if(uart->lock == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-11-18 14:07:25 +01:00
|
|
|
#endif
|
2016-10-14 02:07:21 +02:00
|
|
|
|
2016-10-14 02:02:40 +02:00
|
|
|
if(queueLen && uart->queue == NULL) {
|
2016-10-06 13:21:30 +02:00
|
|
|
uart->queue = xQueueCreate(queueLen, sizeof(uint8_t)); //initialize the queue
|
|
|
|
if(uart->queue == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 17:10:05 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
|
|
|
|
#else
|
2017-09-10 19:24:41 +02:00
|
|
|
if(uart_nr == 1){
|
|
|
|
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_UART1_CLK_EN);
|
|
|
|
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_UART1_RST);
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2017-09-10 19:24:41 +02:00
|
|
|
} else if(uart_nr == 2){
|
|
|
|
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_UART2_CLK_EN);
|
|
|
|
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_UART2_RST);
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2017-09-22 08:17:25 +02:00
|
|
|
} else {
|
|
|
|
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_UART_CLK_EN);
|
|
|
|
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_UART_RST);
|
2017-09-10 19:24:41 +02:00
|
|
|
}
|
2021-04-14 17:10:05 +02:00
|
|
|
#endif
|
2016-10-14 02:02:40 +02:00
|
|
|
uartFlush(uart);
|
2016-10-06 13:21:30 +02:00
|
|
|
uartSetBaudRate(uart, baudrate);
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2016-10-06 13:21:30 +02:00
|
|
|
uart->dev->conf0.val = config;
|
2017-06-22 19:40:31 +02:00
|
|
|
#define TWO_STOP_BITS_CONF 0x3
|
|
|
|
#define ONE_STOP_BITS_CONF 0x1
|
2017-08-04 11:00:51 +02:00
|
|
|
|
2017-06-22 19:40:31 +02:00
|
|
|
if ( uart->dev->conf0.stop_bit_num == TWO_STOP_BITS_CONF) {
|
|
|
|
uart->dev->conf0.stop_bit_num = ONE_STOP_BITS_CONF;
|
|
|
|
uart->dev->rs485_conf.dl1_en = 1;
|
|
|
|
}
|
2020-01-26 23:38:06 +01:00
|
|
|
|
|
|
|
// tx_idle_num : idle interval after tx FIFO is empty(unit: the time it takes to send one bit under current baudrate)
|
|
|
|
// Setting it to 0 prevents line idle time/delays when sending messages with small intervals
|
|
|
|
uart->dev->idle_conf.tx_idle_num = 0; //
|
|
|
|
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2016-10-14 02:02:40 +02:00
|
|
|
|
|
|
|
if(rxPin != -1) {
|
2021-04-08 14:29:53 +02:00
|
|
|
uartAttachRx(uart, rxPin, inverted, rxfifo_full_thrhd);
|
2016-10-14 02:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(txPin != -1) {
|
|
|
|
uartAttachTx(uart, txPin, inverted);
|
|
|
|
}
|
2019-01-09 10:07:54 +01:00
|
|
|
addApbChangeCallback(uart, uart_on_apb_change);
|
2016-10-06 13:21:30 +02:00
|
|
|
return uart;
|
|
|
|
}
|
|
|
|
|
2020-09-30 14:04:18 +02:00
|
|
|
void uartEnd(uart_t* uart, uint8_t txPin, uint8_t rxPin)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-01-09 10:07:54 +01:00
|
|
|
removeApbChangeCallback(uart, uart_on_apb_change);
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart->queue != NULL) {
|
2016-10-06 13:21:30 +02:00
|
|
|
vQueueDelete(uart->queue);
|
2017-08-17 18:04:20 +02:00
|
|
|
uart->queue = NULL;
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 02:02:40 +02:00
|
|
|
uart->dev->conf0.val = 0;
|
2017-08-04 11:00:51 +02:00
|
|
|
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2017-08-04 11:00:51 +02:00
|
|
|
|
2020-09-30 14:04:18 +02:00
|
|
|
uartDetachRx(uart, rxPin);
|
|
|
|
uartDetachTx(uart, txPin);
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 21:16:18 +02:00
|
|
|
size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) {
|
|
|
|
if(uart == NULL) {
|
2018-11-19 16:51:55 +01:00
|
|
|
return 0;
|
2018-09-17 21:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UART_MUTEX_LOCK();
|
|
|
|
if(uart->queue != NULL) {
|
|
|
|
vQueueDelete(uart->queue);
|
|
|
|
uart->queue = xQueueCreate(new_size, sizeof(uint8_t));
|
|
|
|
if(uart->queue == NULL) {
|
2020-09-30 13:56:41 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2021-04-05 13:23:58 +02:00
|
|
|
return 0;
|
2018-09-17 21:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UART_MUTEX_UNLOCK();
|
|
|
|
|
|
|
|
return new_size;
|
|
|
|
}
|
|
|
|
|
2020-10-01 12:58:48 +02:00
|
|
|
void uartSetRxInvert(uart_t* uart, bool invert)
|
|
|
|
{
|
|
|
|
if (uart == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (invert)
|
|
|
|
uart->dev->conf0.rxd_inv = 1;
|
|
|
|
else
|
|
|
|
uart->dev->conf0.rxd_inv = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
uint32_t uartAvailable(uart_t* uart)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL || uart->queue == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-08 14:29:53 +02:00
|
|
|
#ifdef UART_READ_RX_FIFO
|
2020-01-26 23:38:06 +01:00
|
|
|
return (uxQueueMessagesWaiting(uart->queue) + uart->dev->status.rxfifo_cnt) ;
|
2021-04-08 14:29:53 +02:00
|
|
|
#else
|
|
|
|
return uxQueueMessagesWaiting(uart->queue);
|
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:07:46 +02:00
|
|
|
uint32_t uartAvailableForWrite(uart_t* uart)
|
|
|
|
{
|
|
|
|
if(uart == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0x7f - uart->dev->status.txfifo_cnt;
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:29:53 +02:00
|
|
|
#ifdef UART_READ_RX_FIFO
|
2020-01-26 23:38:06 +01:00
|
|
|
void uartRxFifoToQueue(uart_t* uart)
|
|
|
|
{
|
|
|
|
uint8_t c;
|
|
|
|
UART_MUTEX_LOCK();
|
2020-05-05 16:55:58 +02:00
|
|
|
//disable interrupts
|
|
|
|
uart->dev->int_ena.val = 0;
|
|
|
|
uart->dev->int_clr.val = 0xffffffff;
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2020-05-05 16:55:58 +02:00
|
|
|
while (uart->dev->status.rxfifo_cnt || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
|
|
|
|
c = uart->dev->fifo.rw_byte;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
uint32_t fifo_reg = UART_FIFO_AHB_REG(uart->num);
|
|
|
|
while (uart->dev->status.rxfifo_cnt) {
|
|
|
|
c = ESP_REG(fifo_reg);
|
|
|
|
#endif
|
2020-05-05 16:55:58 +02:00
|
|
|
xQueueSend(uart->queue, &c, 0);
|
|
|
|
}
|
|
|
|
//enable interrupts
|
|
|
|
uart->dev->int_ena.rxfifo_full = 1;
|
|
|
|
uart->dev->int_ena.frm_err = 1;
|
|
|
|
uart->dev->int_ena.rxfifo_tout = 1;
|
|
|
|
uart->dev->int_clr.val = 0xffffffff;
|
2020-01-26 23:38:06 +01:00
|
|
|
UART_MUTEX_UNLOCK();
|
|
|
|
}
|
2021-04-08 14:29:53 +02:00
|
|
|
#endif
|
2020-01-26 23:38:06 +01:00
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
uint8_t uartRead(uart_t* uart)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL || uart->queue == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
uint8_t c;
|
2021-04-08 14:29:53 +02:00
|
|
|
#ifdef UART_READ_RX_FIFO
|
2020-01-26 23:38:06 +01:00
|
|
|
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
|
|
|
|
{
|
|
|
|
uartRxFifoToQueue(uart);
|
|
|
|
}
|
2021-04-08 14:29:53 +02:00
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
if(xQueueReceive(uart->queue, &c, 0)) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t uartPeek(uart_t* uart)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL || uart->queue == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
uint8_t c;
|
2021-04-08 14:29:53 +02:00
|
|
|
#ifdef UART_READ_RX_FIFO
|
2020-01-26 23:38:06 +01:00
|
|
|
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
|
|
|
|
{
|
|
|
|
uartRxFifoToQueue(uart);
|
|
|
|
}
|
2021-04-08 14:29:53 +02:00
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
if(xQueuePeek(uart->queue, &c, 0)) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uartWrite(uart_t* uart, uint8_t c)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2021-07-16 02:21:49 +02:00
|
|
|
while(uart->dev->status.txfifo_cnt >= 0x7E);
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-10-06 13:21:30 +02:00
|
|
|
uart->dev->fifo.rw_byte = c;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
ESP_REG(UART_FIFO_AHB_REG(uart->num)) = c;
|
|
|
|
#endif
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uartWriteBuf(uart_t* uart, const uint8_t * data, size_t len)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2021-04-05 13:23:58 +02:00
|
|
|
#ifndef CONFIG_IDF_TARGET_ESP32
|
|
|
|
uint32_t fifo_reg = UART_FIFO_AHB_REG(uart->num);
|
|
|
|
#endif
|
2016-10-06 15:31:28 +02:00
|
|
|
while(len) {
|
2021-07-16 02:21:49 +02:00
|
|
|
while(uart->dev->status.txfifo_cnt >= 0x7E);
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-01-09 10:07:54 +01:00
|
|
|
uart->dev->fifo.rw_byte = *data++;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
ESP_REG(fifo_reg) = *data++;
|
|
|
|
#endif
|
2019-01-09 10:07:54 +01:00
|
|
|
len--;
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uartFlush(uart_t* uart)
|
2019-11-11 15:37:35 +01:00
|
|
|
{
|
2020-10-01 14:30:45 +02:00
|
|
|
uartFlushTxOnly(uart,true);
|
2019-11-11 15:37:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void uartFlushTxOnly(uart_t* uart, bool txOnly)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-11-19 17:04:05 +01:00
|
|
|
while(uart->dev->status.txfifo_cnt || uart->dev->status.st_utx_out);
|
2019-11-11 15:37:35 +01:00
|
|
|
|
|
|
|
if( !txOnly ){
|
|
|
|
//Due to hardware issue, we can not use fifo_rst to reset uart fifo.
|
|
|
|
//See description about UART_TXFIFO_RST and UART_RXFIFO_RST in <<esp32_technical_reference_manual>> v2.6 or later.
|
|
|
|
|
|
|
|
// we read the data out and make `fifo_len == 0 && rd_addr == wr_addr`.
|
|
|
|
while(uart->dev->status.rxfifo_cnt != 0 || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
|
|
|
|
READ_PERI_REG(UART_FIFO_REG(uart->num));
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-11-11 15:37:35 +01:00
|
|
|
xQueueReset(uart->queue);
|
2018-07-03 17:54:08 +02:00
|
|
|
}
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
while(uart->dev->status.txfifo_cnt);
|
|
|
|
uart->dev->conf0.txfifo_rst = 1;
|
|
|
|
uart->dev->conf0.txfifo_rst = 0;
|
|
|
|
#endif
|
2019-11-11 15:37:35 +01:00
|
|
|
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uartSetBaudRate(uart_t* uart, uint32_t baud_rate)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_LOCK();
|
2021-04-14 17:10:05 +02:00
|
|
|
uart_ll_set_baudrate(uart->dev, baud_rate);
|
2016-10-14 02:07:21 +02:00
|
|
|
UART_MUTEX_UNLOCK();
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2019-01-09 10:07:54 +01:00
|
|
|
static void uart_on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb, uint32_t new_apb)
|
|
|
|
{
|
|
|
|
uart_t* uart = (uart_t*)arg;
|
|
|
|
if(ev_type == APB_BEFORE_CHANGE){
|
|
|
|
UART_MUTEX_LOCK();
|
|
|
|
//disabple interrupt
|
|
|
|
uart->dev->int_ena.val = 0;
|
|
|
|
uart->dev->int_clr.val = 0xffffffff;
|
|
|
|
// read RX fifo
|
|
|
|
uint8_t c;
|
2019-12-30 20:35:29 +01:00
|
|
|
// BaseType_t xHigherPriorityTaskWoken;
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-01-09 10:07:54 +01:00
|
|
|
while(uart->dev->status.rxfifo_cnt != 0 || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
|
|
|
|
c = uart->dev->fifo.rw_byte;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
uint32_t fifo_reg = UART_FIFO_AHB_REG(uart->num);
|
|
|
|
while(uart->dev->status.rxfifo_cnt != 0) {
|
|
|
|
c = ESP_REG(fifo_reg);
|
|
|
|
#endif
|
2019-12-30 20:35:29 +01:00
|
|
|
if(uart->queue != NULL ) {
|
|
|
|
xQueueSend(uart->queue, &c, 1); //&xHigherPriorityTaskWoken);
|
2019-01-09 10:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 20:35:29 +01:00
|
|
|
UART_MUTEX_UNLOCK();
|
|
|
|
|
2019-01-09 10:07:54 +01:00
|
|
|
// wait TX empty
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-01-09 10:07:54 +01:00
|
|
|
while(uart->dev->status.txfifo_cnt || uart->dev->status.st_utx_out);
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
while(uart->dev->status.txfifo_cnt);
|
|
|
|
#endif
|
2019-01-09 10:07:54 +01:00
|
|
|
} else {
|
|
|
|
//todo:
|
|
|
|
// set baudrate
|
2019-12-30 20:35:29 +01:00
|
|
|
UART_MUTEX_LOCK();
|
2019-01-09 10:07:54 +01:00
|
|
|
uint32_t clk_div = (uart->dev->clk_div.div_int << 4) | (uart->dev->clk_div.div_frag & 0x0F);
|
|
|
|
uint32_t baud_rate = ((old_apb<<4)/clk_div);
|
|
|
|
clk_div = ((new_apb<<4)/baud_rate);
|
|
|
|
uart->dev->clk_div.div_int = clk_div>>4 ;
|
|
|
|
uart->dev->clk_div.div_frag = clk_div & 0xf;
|
|
|
|
//enable interrupts
|
|
|
|
uart->dev->int_ena.rxfifo_full = 1;
|
|
|
|
uart->dev->int_ena.frm_err = 1;
|
|
|
|
uart->dev->int_ena.rxfifo_tout = 1;
|
|
|
|
uart->dev->int_clr.val = 0xffffffff;
|
|
|
|
UART_MUTEX_UNLOCK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
uint32_t uartGetBaudRate(uart_t* uart)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(uart == NULL) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-09-08 23:59:32 +02:00
|
|
|
|
2016-10-14 02:02:40 +02:00
|
|
|
uint32_t clk_div = (uart->dev->clk_div.div_int << 4) | (uart->dev->clk_div.div_frag & 0x0F);
|
2019-09-08 23:59:32 +02:00
|
|
|
if(!clk_div) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-09 10:07:54 +01:00
|
|
|
return ((getApbFrequency()<<4)/clk_div);
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
static void ARDUINO_ISR_ATTR uart0_write_char(char c)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2021-07-16 02:21:49 +02:00
|
|
|
while(((ESP_REG(0x01C+DR_REG_UART_BASE) >> UART_TXFIFO_CNT_S) & 0x7F) >= 0x7E);
|
2016-10-06 13:21:30 +02:00
|
|
|
ESP_REG(DR_REG_UART_BASE) = c;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
while(UART0.status.txfifo_cnt == 0x7F);
|
|
|
|
WRITE_PERI_REG(UART_FIFO_AHB_REG(0), c);
|
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
static void ARDUINO_ISR_ATTR uart1_write_char(char c)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2021-07-16 02:21:49 +02:00
|
|
|
while(((ESP_REG(0x01C+DR_REG_UART1_BASE) >> UART_TXFIFO_CNT_S) & 0x7F) >= 0x7E);
|
2016-10-06 13:21:30 +02:00
|
|
|
ESP_REG(DR_REG_UART1_BASE) = c;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
while(UART1.status.txfifo_cnt == 0x7F);
|
|
|
|
WRITE_PERI_REG(UART_FIFO_AHB_REG(1), c);
|
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
static void ARDUINO_ISR_ATTR uart2_write_char(char c)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2021-07-16 02:21:49 +02:00
|
|
|
while(((ESP_REG(0x01C+DR_REG_UART2_BASE) >> UART_TXFIFO_CNT_S) & 0x7F) >= 0x7E);
|
2016-10-06 13:21:30 +02:00
|
|
|
ESP_REG(DR_REG_UART2_BASE) = c;
|
|
|
|
}
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2018-12-20 01:57:32 +01:00
|
|
|
void uart_install_putc()
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
|
|
|
switch(s_uart_debug_nr) {
|
|
|
|
case 0:
|
|
|
|
ets_install_putc1((void (*)(char)) &uart0_write_char);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ets_install_putc1((void (*)(char)) &uart1_write_char);
|
|
|
|
break;
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-10-06 13:21:30 +02:00
|
|
|
case 2:
|
|
|
|
ets_install_putc1((void (*)(char)) &uart2_write_char);
|
|
|
|
break;
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
default:
|
|
|
|
ets_install_putc1(NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 01:57:32 +01:00
|
|
|
void uartSetDebug(uart_t* uart)
|
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
if(uart == NULL || uart->num >= UART_PORTS_NUM) {
|
2018-12-20 01:57:32 +01:00
|
|
|
s_uart_debug_nr = -1;
|
|
|
|
//ets_install_putc1(NULL);
|
|
|
|
//return;
|
|
|
|
} else
|
|
|
|
if(s_uart_debug_nr == uart->num) {
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
s_uart_debug_nr = uart->num;
|
|
|
|
uart_install_putc();
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
int uartGetDebug()
|
|
|
|
{
|
|
|
|
return s_uart_debug_nr;
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:40:31 +01:00
|
|
|
int log_printf(const char *format, ...)
|
|
|
|
{
|
2017-06-03 19:10:15 +02:00
|
|
|
static char loc_buf[64];
|
2016-11-13 12:40:31 +01:00
|
|
|
char * temp = loc_buf;
|
2017-06-03 19:10:15 +02:00
|
|
|
int len;
|
2016-11-13 12:40:31 +01:00
|
|
|
va_list arg;
|
|
|
|
va_list copy;
|
|
|
|
va_start(arg, format);
|
|
|
|
va_copy(copy, arg);
|
|
|
|
len = vsnprintf(NULL, 0, format, arg);
|
|
|
|
va_end(copy);
|
|
|
|
if(len >= sizeof(loc_buf)){
|
|
|
|
temp = (char*)malloc(len+1);
|
|
|
|
if(temp == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-11-18 14:07:25 +01:00
|
|
|
#if !CONFIG_DISABLE_HAL_LOCKS
|
2021-04-05 13:23:58 +02:00
|
|
|
if(s_uart_debug_nr != -1 && _uart_bus_array[s_uart_debug_nr].lock){
|
2019-01-09 21:37:31 +01:00
|
|
|
xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY);
|
2016-11-13 12:40:31 +01:00
|
|
|
}
|
2021-08-11 12:46:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
vsnprintf(temp, len+1, format, arg);
|
2017-06-03 19:10:15 +02:00
|
|
|
ets_printf("%s", temp);
|
2021-08-11 12:46:08 +02:00
|
|
|
|
|
|
|
#if !CONFIG_DISABLE_HAL_LOCKS
|
|
|
|
if(s_uart_debug_nr != -1 && _uart_bus_array[s_uart_debug_nr].lock){
|
|
|
|
xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock);
|
|
|
|
}
|
2016-11-18 14:07:25 +01:00
|
|
|
#endif
|
2016-11-13 12:40:31 +01:00
|
|
|
va_end(arg);
|
2019-01-09 21:37:31 +01:00
|
|
|
if(len >= sizeof(loc_buf)){
|
2016-11-13 12:40:31 +01:00
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2018-11-19 16:51:55 +01:00
|
|
|
|
Add log_buf to pretty print buffers
```
/* 0x0000 */ 0x7b, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // {{..............
/* 0x0010 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
```
2021-08-17 16:01:16 +02:00
|
|
|
static void log_print_buf_line(const uint8_t *b, size_t len, size_t total_len){
|
|
|
|
for(size_t i = 0; i<len; i++){
|
|
|
|
log_printf("%s0x%02x,",i?" ":"", b[i]);
|
|
|
|
}
|
|
|
|
if(total_len > 16){
|
|
|
|
for(size_t i = len; i<16; i++){
|
|
|
|
log_printf(" ");
|
|
|
|
}
|
|
|
|
log_printf(" // ");
|
|
|
|
} else {
|
|
|
|
log_printf(" // ");
|
|
|
|
}
|
|
|
|
for(size_t i = 0; i<len; i++){
|
|
|
|
log_printf("%c",((b[i] >= 0x20) && (b[i] < 0x80))?b[i]:'.');
|
|
|
|
}
|
|
|
|
log_printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_print_buf(const uint8_t *b, size_t len){
|
|
|
|
if(!len || !b){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(size_t i = 0; i<len; i+=16){
|
|
|
|
if(len > 16){
|
|
|
|
log_printf("/* 0x%04X */ ", i);
|
|
|
|
}
|
|
|
|
log_print_buf_line(b+i, ((len-i)<16)?(len - i):16, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 16:51:55 +01:00
|
|
|
/*
|
|
|
|
* if enough pulses are detected return the minimum high pulse duration + minimum low pulse duration divided by two.
|
|
|
|
* This equals one bit period. If flag is true the function return inmediately, otherwise it waits for enough pulses.
|
|
|
|
*/
|
|
|
|
unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
|
|
|
|
{
|
|
|
|
while(uart->dev->rxd_cnt.edge_cnt < 30) { // UART_PULSE_NUM(uart_num)
|
|
|
|
if(flg) return 0;
|
|
|
|
ets_delay_us(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
UART_MUTEX_LOCK();
|
|
|
|
unsigned long ret = ((uart->dev->lowpulse.min_cnt + uart->dev->highpulse.min_cnt) >> 1) + 12;
|
|
|
|
UART_MUTEX_UNLOCK();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
|
|
|
|
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
|
|
|
|
* rounded to the closed real baudrate.
|
|
|
|
*/
|
2019-09-08 23:59:32 +02:00
|
|
|
void uartStartDetectBaudrate(uart_t *uart) {
|
|
|
|
if(!uart) return;
|
2021-04-14 17:10:05 +02:00
|
|
|
#ifndef CONFIG_IDF_TARGET_ESP32C3
|
2019-09-08 23:59:32 +02:00
|
|
|
uart->dev->auto_baud.glitch_filt = 0x08;
|
|
|
|
uart->dev->auto_baud.en = 0;
|
|
|
|
uart->dev->auto_baud.en = 1;
|
2021-04-14 17:10:05 +02:00
|
|
|
#endif
|
2019-09-08 23:59:32 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 16:51:55 +01:00
|
|
|
unsigned long
|
|
|
|
uartDetectBaudrate(uart_t *uart)
|
|
|
|
{
|
2021-04-14 17:10:05 +02:00
|
|
|
#ifndef CONFIG_IDF_TARGET_ESP32C3
|
2018-11-19 16:51:55 +01:00
|
|
|
static bool uartStateDetectingBaudrate = false;
|
|
|
|
|
|
|
|
if(!uartStateDetectingBaudrate) {
|
|
|
|
uart->dev->auto_baud.glitch_filt = 0x08;
|
|
|
|
uart->dev->auto_baud.en = 0;
|
|
|
|
uart->dev->auto_baud.en = 1;
|
|
|
|
uartStateDetectingBaudrate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long divisor = uartBaudrateDetect(uart, true);
|
|
|
|
if (!divisor) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uart->dev->auto_baud.en = 0;
|
|
|
|
uartStateDetectingBaudrate = false; // Initialize for the next round
|
|
|
|
|
2019-01-09 10:07:54 +01:00
|
|
|
unsigned long baudrate = getApbFrequency() / divisor;
|
2018-11-19 16:51:55 +01:00
|
|
|
|
|
|
|
static const unsigned long default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400};
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
for (i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) // find the nearest real baudrate
|
|
|
|
{
|
|
|
|
if (baudrate <= default_rates[i])
|
|
|
|
{
|
|
|
|
if (baudrate - default_rates[i - 1] < default_rates[i] - baudrate) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_rates[i];
|
2021-04-14 17:10:05 +02:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2018-11-19 16:51:55 +01:00
|
|
|
}
|
2019-03-03 15:47:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the status of the RX state machine, if the value is non-zero the state machine is active.
|
|
|
|
*/
|
|
|
|
bool uartRxActive(uart_t* uart) {
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-03-03 15:47:24 +01:00
|
|
|
return uart->dev->status.st_urx_out != 0;
|
2021-04-05 13:23:58 +02:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2019-03-03 15:47:24 +01:00
|
|
|
}
|