Minor changes for driver

1. remove "\n" when calling ESP_LOGX APIs.
2. modify uart_event_t for uart rx data.
3. use MICRO for uart inverse value
4. add uart_tx_data_t for internal tx function.
This commit is contained in:
Wangjialin 2016-11-07 14:16:52 +08:00
parent 3ec23f1b83
commit e452278194
4 changed files with 114 additions and 110 deletions

View file

@ -22,7 +22,7 @@
static const char* GPIO_TAG = "GPIO"; static const char* GPIO_TAG = "GPIO";
#define GPIO_CHECK(a, str, ret_val) if (!(a)) { \ #define GPIO_CHECK(a, str, ret_val) if (!(a)) { \
ESP_LOGE(GPIO_TAG,"%s:%d (%s):%s\n", __FILE__, __LINE__, __FUNCTION__, str); \ ESP_LOGE(GPIO_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
return (ret_val); \ return (ret_val); \
} }
@ -71,15 +71,15 @@ const uint32_t GPIO_PIN_MUX_REG[GPIO_PIN_COUNT] = {
esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type) esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
GPIO_CHECK(intr_type < GPIO_INTR_MAX, "GPIO interrupt type error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(intr_type < GPIO_INTR_MAX, "GPIO interrupt type error", ESP_ERR_INVALID_ARG);
GPIO.pin[gpio_num].int_type = intr_type; GPIO.pin[gpio_num].int_type = intr_type;
return ESP_OK; return ESP_OK;
} }
esp_err_t gpio_intr_enable(gpio_num_t gpio_num) esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
if(xPortGetCoreID() == 0) { if(xPortGetCoreID() == 0) {
GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr
} else { } else {
@ -90,14 +90,14 @@ esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
esp_err_t gpio_intr_disable(gpio_num_t gpio_num) esp_err_t gpio_intr_disable(gpio_num_t gpio_num)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
GPIO.pin[gpio_num].int_ena = 0; //disable GPIO intr GPIO.pin[gpio_num].int_ena = 0; //disable GPIO intr
return ESP_OK; return ESP_OK;
} }
static esp_err_t gpio_output_disable(gpio_num_t gpio_num) static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
if(gpio_num < 32) { if(gpio_num < 32) {
GPIO.enable_w1tc = (0x1 << gpio_num); GPIO.enable_w1tc = (0x1 << gpio_num);
} else { } else {
@ -108,7 +108,7 @@ static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
static esp_err_t gpio_output_enable(gpio_num_t gpio_num) static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
{ {
GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
if(gpio_num < 32) { if(gpio_num < 32) {
GPIO.enable_w1ts = (0x1 << gpio_num); GPIO.enable_w1ts = (0x1 << gpio_num);
} else { } else {
@ -119,7 +119,7 @@ static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level) esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
if(level) { if(level) {
if(gpio_num < 32) { if(gpio_num < 32) {
GPIO.out_w1ts = (1 << gpio_num); GPIO.out_w1ts = (1 << gpio_num);
@ -147,8 +147,8 @@ int gpio_get_level(gpio_num_t gpio_num)
esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull) esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
switch(pull) { switch(pull) {
case GPIO_PULLUP_ONLY: case GPIO_PULLUP_ONLY:
@ -168,7 +168,7 @@ esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
PIN_PULLDWN_DIS(GPIO_PIN_MUX_REG[gpio_num]); PIN_PULLDWN_DIS(GPIO_PIN_MUX_REG[gpio_num]);
break; break;
default: default:
ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u\n",gpio_num,pull); ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u",gpio_num,pull);
ret = ESP_ERR_INVALID_ARG; ret = ESP_ERR_INVALID_ARG;
break; break;
} }
@ -177,9 +177,9 @@ esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode) esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
if(gpio_num >= 34 && (mode & (GPIO_MODE_DEF_OUTPUT))) { if(gpio_num >= 34 && (mode & (GPIO_MODE_DEF_OUTPUT))) {
ESP_LOGE(GPIO_TAG, "io_num=%d can only be input\n",gpio_num); ESP_LOGE(GPIO_TAG, "io_num=%d can only be input",gpio_num);
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
@ -214,53 +214,56 @@ esp_err_t gpio_config(gpio_config_t *pGPIOConfig)
uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask); uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask);
uint32_t io_reg = 0; uint32_t io_reg = 0;
uint32_t io_num = 0; uint32_t io_num = 0;
uint64_t bit_valid = 0; uint8_t input_en = 0;
uint8_t output_en = 0;
uint8_t od_en = 0;
uint8_t pu_en = 0;
uint8_t pd_en = 0;
if(pGPIOConfig->pin_bit_mask == 0 || pGPIOConfig->pin_bit_mask >= (((uint64_t) 1) << GPIO_PIN_COUNT)) { if(pGPIOConfig->pin_bit_mask == 0 || pGPIOConfig->pin_bit_mask >= (((uint64_t) 1) << GPIO_PIN_COUNT)) {
ESP_LOGE(GPIO_TAG, "GPIO_PIN mask error \n"); ESP_LOGE(GPIO_TAG, "GPIO_PIN mask error ");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if((pGPIOConfig->mode) & (GPIO_MODE_DEF_OUTPUT)) { if((pGPIOConfig->mode) & (GPIO_MODE_DEF_OUTPUT)) {
//GPIO 34/35/36/37/38/39 can only be used as input mode; //GPIO 34/35/36/37/38/39 can only be used as input mode;
if((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) { if((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) {
ESP_LOGE(GPIO_TAG, "GPIO34-39 can only be used as input mode\n"); ESP_LOGE(GPIO_TAG, "GPIO34-39 can only be used as input mode");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
} }
do { do {
io_reg = GPIO_PIN_MUX_REG[io_num]; io_reg = GPIO_PIN_MUX_REG[io_num];
if(((gpio_pin_mask >> io_num) & BIT(0)) && io_reg) { if(((gpio_pin_mask >> io_num) & BIT(0)) && io_reg) {
ESP_LOGI(GPIO_TAG, "Gpio%02d |Mode:",io_num);
if((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) { if((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) {
ESP_LOGI(GPIO_TAG, "INPUT "); input_en = 1;
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[io_num]); PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[io_num]);
} else { } else {
PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[io_num]); PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[io_num]);
} }
if((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) { if((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) {
ESP_LOGI(GPIO_TAG, "OD "); od_en = 1;
GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */ GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */
} else { } else {
GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */ GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */
} }
if((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) { if((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) {
ESP_LOGI(GPIO_TAG, "OUTPUT "); output_en = 1;
gpio_output_enable(io_num); gpio_output_enable(io_num);
} else { } else {
gpio_output_disable(io_num); gpio_output_disable(io_num);
} }
if(pGPIOConfig->pull_up_en) { if(pGPIOConfig->pull_up_en) {
ESP_LOGI(GPIO_TAG, "PU "); pu_en = 1;
PIN_PULLUP_EN(io_reg); PIN_PULLUP_EN(io_reg);
} else { } else {
PIN_PULLUP_DIS(io_reg); PIN_PULLUP_DIS(io_reg);
} }
if(pGPIOConfig->pull_down_en) { if(pGPIOConfig->pull_down_en) {
ESP_LOGI(GPIO_TAG, "PD "); pd_en = 1;
PIN_PULLDWN_EN(io_reg); PIN_PULLDWN_EN(io_reg);
} else { } else {
PIN_PULLDWN_DIS(io_reg); PIN_PULLDWN_DIS(io_reg);
} }
ESP_LOGI(GPIO_TAG, "Intr:%d |\n",pGPIOConfig->intr_type); ESP_LOGI(GPIO_TAG, "GPIO[%d]| InputEn: %d| OutputEn: %d| OpenDrain: %d| Pullup: %d| Pulldown: %d| Intr:%d ", io_num, input_en, output_en, od_en, pu_en, pd_en, pGPIOConfig->intr_type);
gpio_set_intr_type(io_num, pGPIOConfig->intr_type); gpio_set_intr_type(io_num, pGPIOConfig->intr_type);
if(pGPIOConfig->intr_type) { if(pGPIOConfig->intr_type) {
gpio_intr_enable(io_num); gpio_intr_enable(io_num);
@ -268,8 +271,6 @@ esp_err_t gpio_config(gpio_config_t *pGPIOConfig)
gpio_intr_disable(io_num); gpio_intr_disable(io_num);
} }
PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO); /*function number 2 is GPIO_FUNC for each pin */ PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO); /*function number 2 is GPIO_FUNC for each pin */
} else if(bit_valid && (io_reg == 0)) {
ESP_LOGW(GPIO_TAG, "io_num=%d does not exist\n",io_num);
} }
io_num++; io_num++;
} while(io_num < GPIO_PIN_COUNT); } while(io_num < GPIO_PIN_COUNT);
@ -278,7 +279,7 @@ esp_err_t gpio_config(gpio_config_t *pGPIOConfig)
esp_err_t gpio_isr_register(uint32_t gpio_intr_num, void (*fn)(void*), void * arg) esp_err_t gpio_isr_register(uint32_t gpio_intr_num, void (*fn)(void*), void * arg)
{ {
GPIO_CHECK(fn, "GPIO ISR null\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG);
ESP_INTR_DISABLE(gpio_intr_num); ESP_INTR_DISABLE(gpio_intr_num);
intr_matrix_set(xPortGetCoreID(), ETS_GPIO_INTR_SOURCE, gpio_intr_num); intr_matrix_set(xPortGetCoreID(), ETS_GPIO_INTR_SOURCE, gpio_intr_num);
xt_set_interrupt_handler(gpio_intr_num, fn, arg); xt_set_interrupt_handler(gpio_intr_num, fn, arg);
@ -289,13 +290,13 @@ esp_err_t gpio_isr_register(uint32_t gpio_intr_num, void (*fn)(void*), void * ar
/*only level interrupt can be used for wake-up function*/ /*only level interrupt can be used for wake-up function*/
esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type) esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
if((intr_type == GPIO_INTR_LOW_LEVEL) || (intr_type == GPIO_INTR_HIGH_LEVEL)) { if((intr_type == GPIO_INTR_LOW_LEVEL) || (intr_type == GPIO_INTR_HIGH_LEVEL)) {
GPIO.pin[gpio_num].int_type = intr_type; GPIO.pin[gpio_num].int_type = intr_type;
GPIO.pin[gpio_num].wakeup_enable = 0x1; GPIO.pin[gpio_num].wakeup_enable = 0x1;
} else { } else {
ESP_LOGE(GPIO_TAG, "GPIO wakeup only support Level mode,but edge mode set. gpio_num:%u\n",gpio_num); ESP_LOGE(GPIO_TAG, "GPIO wakeup only support Level mode,but edge mode set. gpio_num:%u",gpio_num);
ret = ESP_ERR_INVALID_ARG; ret = ESP_ERR_INVALID_ARG;
} }
return ret; return ret;
@ -303,7 +304,7 @@ esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num) esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)
{ {
GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error\n", ESP_ERR_INVALID_ARG); GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
GPIO.pin[gpio_num].wakeup_enable = 0; GPIO.pin[gpio_num].wakeup_enable = 0;
return ESP_OK; return ESP_OK;
} }

View file

@ -38,12 +38,18 @@ extern "C" {
#define UART_BITRATE_MAX 5000000 #define UART_BITRATE_MAX 5000000
#define UART_PIN_NO_CHANGE (-1) #define UART_PIN_NO_CHANGE (-1)
#define UART_INVERSE_DISABLE (0x0) /*!< Disable UART signal inverse*/
#define UART_INVERSE_RXD (UART_RXD_INV_M) /*!< UART RXD input inverse*/
#define UART_INVERSE_CTS (UART_CTS_INV_M) /*!< UART CTS input inverse*/
#define UART_INVERSE_TXD (UART_TXD_INV_M) /*!< UART TXD output inverse*/
#define UART_INVERSE_RTS (UART_RTS_INV_M) /*!< UART RTS output inverse*/
typedef enum { typedef enum {
UART_DATA_5_BITS = 0x0, /*!< word length: 5bits*/ UART_DATA_5_BITS = 0x0, /*!< word length: 5bits*/
UART_DATA_6_BITS = 0x1, /*!< word length: 6bits*/ UART_DATA_6_BITS = 0x1, /*!< word length: 6bits*/
UART_DATA_7_BITS = 0x2, /*!< word length: 7bits*/ UART_DATA_7_BITS = 0x2, /*!< word length: 7bits*/
UART_DATA_8_BITS = 0x3, /*!< word length: 8bits*/ UART_DATA_8_BITS = 0x3, /*!< word length: 8bits*/
UART_DATA_MAX_BITS = 0X4, UART_DATA_BITS_MAX = 0X4,
} uart_word_length_t; } uart_word_length_t;
typedef enum { typedef enum {
@ -74,14 +80,6 @@ typedef enum {
UART_HW_FLOWCTRL_MAX = 0x4, UART_HW_FLOWCTRL_MAX = 0x4,
} uart_hw_flowcontrol_t; } uart_hw_flowcontrol_t;
typedef enum {
UART_INVERSE_DISABLE = 0x0, /*!< Disable UART wire output inverse*/
UART_INVERSE_RXD = (uint32_t)UART_RXD_INV_M, /*!< UART RXD input inverse*/
UART_INVERSE_CTS = (uint32_t)UART_CTS_INV_M, /*!< UART CTS input inverse*/
UART_INVERSE_TXD = (uint32_t)UART_TXD_INV_M, /*!< UART TXD output inverse*/
UART_INVERSE_RTS = (uint32_t)UART_RTS_INV_M, /*!< UART RTS output inverse*/
} uart_inverse_t;
typedef struct { typedef struct {
int baud_rate; /*!< UART baudrate*/ int baud_rate; /*!< UART baudrate*/
uart_word_length_t data_bits; /*!< UART byte size*/ uart_word_length_t data_bits; /*!< UART byte size*/
@ -110,14 +108,8 @@ typedef enum {
} uart_event_type_t; } uart_event_type_t;
typedef struct { typedef struct {
uart_event_type_t type; uart_event_type_t type; /*!< UART event type */
union { size_t size; /*!< UART data size for UART_DATA event*/
struct {
int brk_len;
size_t size;
uint8_t data[];
} data;
};
} uart_event_t; } uart_event_t;
/** /**
@ -225,14 +217,13 @@ esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t* baudrate);
* *
* @param inverse_mask Choose the wires that need to be inversed. * @param inverse_mask Choose the wires that need to be inversed.
* *
* (inverse_mask should be chosen from uart_inverse_t, combine with OR-OPERATION) * (inverse_mask should be chosen from UART_INVERSE_RXD/UART_INVERSE_TXD/UART_INVERSE_RTS/UART_INVERSE_CTS, combine with OR-OPERATION)
* *
* @return * @return
* - ESP_OK Success * - ESP_OK Success
* - ESP_FAIL Parameter error * - ESP_FAIL Parameter error
*/ */
esp_err_t uart_set_line_inverse(uart_port_t uart_no, uint32_t inverse_mask) ; esp_err_t uart_set_line_inverse(uart_port_t uart_no, uint32_t inverse_mask);
/** /**
* @brief Set hardware flow control. * @brief Set hardware flow control.
@ -701,11 +692,13 @@ esp_err_t uart_flush(uart_port_t uart_num);
* if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) { * if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
* ESP_LOGI(TAG, "uart[%d] event:", uart_num); * ESP_LOGI(TAG, "uart[%d] event:", uart_num);
* switch(event.type) { * switch(event.type) {
* memset(dtmp, 0, sizeof(dtmp));
* //Event of UART receving data * //Event of UART receving data
* case UART_DATA: * case UART_DATA:
* ESP_LOGI(TAG,"data, len: %d\n", event.data.size); * ESP_LOGI(TAG,"data, len: %d", event.size);
* int len = uart_read_bytes(uart_num, dtmp, event.data.size, 10); * int len = uart_read_bytes(uart_num, dtmp, event.size, 10);
* ESP_LOGI(TAG, "uart read: %d\n", len); * ESP_LOGI(TAG, "uart read: %d", len);
uart_write_bytes(uart_num, (const char*)dtmp, len);
* break; * break;
* //Event of HW FIFO overflow detected * //Event of HW FIFO overflow detected
* case UART_FIFO_OVF: * case UART_FIFO_OVF:

View file

@ -23,14 +23,14 @@
static const char* LEDC_TAG = "LEDC"; static const char* LEDC_TAG = "LEDC";
static portMUX_TYPE ledc_spinlock = portMUX_INITIALIZER_UNLOCKED; static portMUX_TYPE ledc_spinlock = portMUX_INITIALIZER_UNLOCKED;
#define LEDC_CHECK(a, str, ret_val) if (!(a)) { \ #define LEDC_CHECK(a, str, ret_val) if (!(a)) { \
ESP_LOGE(LEDC_TAG,"%s:%d (%s):%s\n", __FILE__, __LINE__, __FUNCTION__, str); \ ESP_LOGE(LEDC_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
return (ret_val); \ return (ret_val); \
} }
esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t div_num, uint32_t bit_num, ledc_clk_src_t clk_src) esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t div_num, uint32_t bit_num, ledc_clk_src_t clk_src)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
LEDC.timer_group[speed_mode].timer[timer_sel].conf.div_num = div_num; LEDC.timer_group[speed_mode].timer[timer_sel].conf.div_num = div_num;
LEDC.timer_group[speed_mode].timer[timer_sel].conf.tick_sel = clk_src; LEDC.timer_group[speed_mode].timer[timer_sel].conf.tick_sel = clk_src;
@ -58,8 +58,8 @@ static esp_err_t ledc_duty_config(ledc_mode_t speed_mode, uint32_t channel_num,
esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx) esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(timer_idx <= LEDC_TIMER_3, "ledc timer error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(timer_idx <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel = timer_idx; LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel = timer_idx;
portEXIT_CRITICAL(&ledc_spinlock); portEXIT_CRITICAL(&ledc_spinlock);
@ -68,8 +68,8 @@ esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint
esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel) esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 1; LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 1;
LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 0; LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 0;
@ -79,8 +79,8 @@ esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel) esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 1; LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 1;
portEXIT_CRITICAL(&ledc_spinlock); portEXIT_CRITICAL(&ledc_spinlock);
@ -89,8 +89,8 @@ esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel)
esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel) esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 0; LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 0;
portEXIT_CRITICAL(&ledc_spinlock); portEXIT_CRITICAL(&ledc_spinlock);
@ -99,7 +99,7 @@ esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)
static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel, ledc_intr_type_t type) static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel, ledc_intr_type_t type)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
uint32_t value; uint32_t value;
uint32_t intr_type = type; uint32_t intr_type = type;
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
@ -115,7 +115,7 @@ static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel,
esp_err_t ledc_isr_register(uint32_t ledc_intr_num, void (*fn)(void*), void * arg) esp_err_t ledc_isr_register(uint32_t ledc_intr_num, void (*fn)(void*), void * arg)
{ {
LEDC_CHECK(fn, "ledc isr null\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(fn, "ledc isr null", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
ESP_INTR_DISABLE(ledc_intr_num); ESP_INTR_DISABLE(ledc_intr_num);
intr_matrix_set(xPortGetCoreID(), ETS_LEDC_INTR_SOURCE, ledc_intr_num); intr_matrix_set(xPortGetCoreID(), ETS_LEDC_INTR_SOURCE, ledc_intr_num);
@ -131,13 +131,13 @@ esp_err_t ledc_timer_config(ledc_timer_config_t* timer_conf)
int bit_num = timer_conf->bit_num; int bit_num = timer_conf->bit_num;
int timer_num = timer_conf->timer_num; int timer_num = timer_conf->timer_num;
int speed_mode = timer_conf->speed_mode; int speed_mode = timer_conf->speed_mode;
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
if(freq_hz == 0 || bit_num == 0 || bit_num > LEDC_TIMER_15_BIT) { if(freq_hz == 0 || bit_num == 0 || bit_num > LEDC_TIMER_15_BIT) {
ESP_LOGE(LEDC_TAG, "freq_hz=%u bit_num=%u\n", freq_hz, bit_num); ESP_LOGE(LEDC_TAG, "freq_hz=%u bit_num=%u", freq_hz, bit_num);
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if(timer_num > LEDC_TIMER_3) { if(timer_num > LEDC_TIMER_3) {
ESP_LOGE(LEDC_TAG, "Time Select %u\n", timer_num); ESP_LOGE(LEDC_TAG, "Time Select %u", timer_num);
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
@ -149,7 +149,7 @@ esp_err_t ledc_timer_config(ledc_timer_config_t* timer_conf)
/*Selet the reference tick*/ /*Selet the reference tick*/
div_param = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision; div_param = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
if(div_param <= 256 || div_param > LEDC_DIV_NUM_HSTIMER0_V) { if(div_param <= 256 || div_param > LEDC_DIV_NUM_HSTIMER0_V) {
ESP_LOGE(LEDC_TAG, "div param err,div_param=%u\n", (uint32_t)div_param); ESP_LOGE(LEDC_TAG, "div param err,div_param=%u", (uint32_t)div_param);
ret = ESP_FAIL; ret = ESP_FAIL;
} }
timer_clk_src = LEDC_REF_TICK; timer_clk_src = LEDC_REF_TICK;
@ -166,9 +166,9 @@ esp_err_t ledc_timer_config(ledc_timer_config_t* timer_conf)
esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel) esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel)
{ {
LEDC_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc channel error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "ledc GPIO output number error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "ledc GPIO output number error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO); PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT); gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
if(speed_mode == LEDC_HIGH_SPEED_MODE) { if(speed_mode == LEDC_HIGH_SPEED_MODE) {
@ -187,10 +187,10 @@ esp_err_t ledc_channel_config(ledc_channel_config_t* ledc_conf)
uint32_t timer_select = ledc_conf->timer_sel; uint32_t timer_select = ledc_conf->timer_sel;
uint32_t intr_type = ledc_conf->intr_type; uint32_t intr_type = ledc_conf->intr_type;
uint32_t duty = ledc_conf->duty; uint32_t duty = ledc_conf->duty;
LEDC_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc channel error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "ledc GPIO output number error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "ledc GPIO output number error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(timer_select <= LEDC_TIMER_3, "ledc timer error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(timer_select <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
/*set channel parameters*/ /*set channel parameters*/
/* channel parameters decide how the waveform looks like in one period*/ /* channel parameters decide how the waveform looks like in one period*/
@ -202,7 +202,7 @@ esp_err_t ledc_channel_config(ledc_channel_config_t* ledc_conf)
ledc_bind_channel_timer(speed_mode, ledc_channel, timer_select); ledc_bind_channel_timer(speed_mode, ledc_channel, timer_select);
/*set interrupt type*/ /*set interrupt type*/
ledc_enable_intr_type(speed_mode, ledc_channel, intr_type); ledc_enable_intr_type(speed_mode, ledc_channel, intr_type);
ESP_LOGI(LEDC_TAG, "LEDC_PWM CHANNEL %1u|GPIO %02u|Duty %04u|Time %01u\n", ESP_LOGI(LEDC_TAG, "LEDC_PWM CHANNEL %1u|GPIO %02u|Duty %04u|Time %01u",
ledc_channel, gpio_num, duty, timer_select ledc_channel, gpio_num, duty, timer_select
); );
/*set LEDC signal in gpio matrix*/ /*set LEDC signal in gpio matrix*/
@ -214,8 +214,8 @@ esp_err_t ledc_channel_config(ledc_channel_config_t* ledc_conf)
esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel) esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 1; LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 1;
LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 1; LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 1;
@ -225,8 +225,8 @@ esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level) esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
LEDC.channel_group[speed_mode].channel[channel].conf0.idle_lv = idle_level & 0x1; LEDC.channel_group[speed_mode].channel[channel].conf0.idle_lv = idle_level & 0x1;
LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 0; LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 0;
@ -237,11 +237,11 @@ esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idl
esp_err_t ledc_set_fade(ledc_mode_t speed_mode, uint32_t channel, uint32_t duty, ledc_duty_direction_t fade_direction, esp_err_t ledc_set_fade(ledc_mode_t speed_mode, uint32_t channel, uint32_t duty, ledc_duty_direction_t fade_direction,
uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale) uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(fade_direction <= LEDC_DUTY_DIR_INCREASE, "ledc fade direction error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(fade_direction <= LEDC_DUTY_DIR_INCREASE, "ledc fade direction error", ESP_ERR_INVALID_ARG);
if(step_num > LEDC_DUTY_NUM_HSCH0_V || duty_cyle_num > LEDC_DUTY_CYCLE_HSCH0_V || duty_scale > LEDC_DUTY_SCALE_HSCH0_V) { if(step_num > LEDC_DUTY_NUM_HSCH0_V || duty_cyle_num > LEDC_DUTY_CYCLE_HSCH0_V || duty_scale > LEDC_DUTY_SCALE_HSCH0_V) {
ESP_LOGE(LEDC_TAG, "step_num=%u duty_cyle_num=%u duty_scale=%u\n", step_num, duty_cyle_num, duty_scale); ESP_LOGE(LEDC_TAG, "step_num=%u duty_cyle_num=%u duty_scale=%u", step_num, duty_cyle_num, duty_scale);
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
ledc_duty_config(speed_mode, ledc_duty_config(speed_mode,
@ -258,8 +258,8 @@ esp_err_t ledc_set_fade(ledc_mode_t speed_mode, uint32_t channel, uint32_t duty,
esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty) esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
ledc_duty_config(speed_mode, ledc_duty_config(speed_mode,
channel, //uint32_t chan_num, channel, //uint32_t chan_num,
0, //uint32_t hpoint_val, 0, //uint32_t hpoint_val,
@ -274,14 +274,14 @@ esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t
int ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel) int ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", (-1)); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", (-1));
uint32_t duty = (LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> 4); uint32_t duty = (LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> 4);
return duty; return duty;
} }
esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz) esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", ESP_ERR_INVALID_ARG); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
uint32_t div_num = 0; uint32_t div_num = 0;
@ -294,7 +294,7 @@ esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t
div_num = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision; div_num = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
} }
if(div_num <= 256 || div_num > LEDC_DIV_NUM_HSTIMER0) { if(div_num <= 256 || div_num > LEDC_DIV_NUM_HSTIMER0) {
ESP_LOGE(LEDC_TAG, "div param err,div_param=%u\n", div_num); ESP_LOGE(LEDC_TAG, "div param err,div_param=%u", div_num);
ret = ESP_FAIL; ret = ESP_FAIL;
} }
LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num = div_num; LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num = div_num;
@ -304,7 +304,7 @@ esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t
uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num) uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num)
{ {
LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error\n", (0)); LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", (0));
portENTER_CRITICAL(&ledc_spinlock); portENTER_CRITICAL(&ledc_spinlock);
uint32_t freq = 0; uint32_t freq = 0;
uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel; uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel;

View file

@ -31,7 +31,7 @@
static const char* UART_TAG = "UART"; static const char* UART_TAG = "UART";
#define UART_CHECK(a, str, ret) if (!(a)) { \ #define UART_CHECK(a, str, ret) if (!(a)) { \
ESP_LOGE(UART_TAG,"%s:%d (%s):%s\n", __FILE__, __LINE__, __FUNCTION__, str); \ ESP_LOGE(UART_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
return (ret); \ return (ret); \
} }
#define UART_EMPTY_THRESH_DEFAULT (10) #define UART_EMPTY_THRESH_DEFAULT (10)
@ -42,6 +42,14 @@ static const char* UART_TAG = "UART";
#define UART_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux) #define UART_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
#define UART_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux) #define UART_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
typedef struct {
uart_event_type_t type; /*!< UART TX data type */
struct {
int brk_len;
size_t size;
uint8_t data[0];
} tx_data;
} uart_tx_data_t;
typedef struct { typedef struct {
uart_port_t uart_num; /*!< UART port number*/ uart_port_t uart_num; /*!< UART port number*/
@ -67,7 +75,7 @@ typedef struct {
RingbufHandle_t tx_ring_buf; /*!< TX ring buffer handler*/ RingbufHandle_t tx_ring_buf; /*!< TX ring buffer handler*/
bool tx_waiting_fifo; /*!< this flag indicates that some task is waiting for FIFO empty interrupt, used to send all data without any data buffer*/ bool tx_waiting_fifo; /*!< this flag indicates that some task is waiting for FIFO empty interrupt, used to send all data without any data buffer*/
uint8_t* tx_ptr; /*!< TX data pointer to push to FIFO in TX buffer mode*/ uint8_t* tx_ptr; /*!< TX data pointer to push to FIFO in TX buffer mode*/
uart_event_t* tx_head; /*!< TX data pointer to head of the current buffer in TX ring buffer*/ uart_tx_data_t* tx_head; /*!< TX data pointer to head of the current buffer in TX ring buffer*/
uint32_t tx_len_tot; /*!< Total length of current item in ring buffer*/ uint32_t tx_len_tot; /*!< Total length of current item in ring buffer*/
uint32_t tx_len_cur; uint32_t tx_len_cur;
uint8_t tx_brk_flg; /*!< Flag to indicate to send a break signal in the end of the item sending procedure */ uint8_t tx_brk_flg; /*!< Flag to indicate to send a break signal in the end of the item sending procedure */
@ -75,6 +83,8 @@ typedef struct {
uint8_t tx_waiting_brk; /*!< Flag to indicate that TX FIFO is ready to send break signal after FIFO is empty, do not push data into TX FIFO right now.*/ uint8_t tx_waiting_brk; /*!< Flag to indicate that TX FIFO is ready to send break signal after FIFO is empty, do not push data into TX FIFO right now.*/
} uart_obj_t; } uart_obj_t;
static uart_obj_t *p_uart_obj[UART_NUM_MAX] = {0}; static uart_obj_t *p_uart_obj[UART_NUM_MAX] = {0};
static uart_dev_t* UART[UART_NUM_MAX] = {&UART0, &UART1, &UART2}; static uart_dev_t* UART[UART_NUM_MAX] = {&UART0, &UART1, &UART2};
static portMUX_TYPE uart_spinlock[UART_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED}; static portMUX_TYPE uart_spinlock[UART_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED};
@ -82,7 +92,7 @@ static portMUX_TYPE uart_spinlock[UART_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED,
esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit) esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit)
{ {
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
UART_CHECK((data_bit < UART_DATA_MAX_BITS), "data bit error", ESP_FAIL); UART_CHECK((data_bit < UART_DATA_BITS_MAX), "data bit error", ESP_FAIL);
UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); UART_ENTER_CRITICAL(&uart_spinlock[uart_num]);
UART[uart_num]->conf0.bit_num = data_bit; UART[uart_num]->conf0.bit_num = data_bit;
UART_EXIT_CRITICAL(&uart_spinlock[uart_num]); UART_EXIT_CRITICAL(&uart_spinlock[uart_num]);
@ -366,7 +376,7 @@ esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int r
esp_err_t uart_set_rts(uart_port_t uart_num, int level) esp_err_t uart_set_rts(uart_port_t uart_num, int level)
{ {
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
UART_CHECK((UART[uart_num]->conf1.rx_flow_en != 1), "disable hw flowctrl before using sw control\n", ESP_FAIL); UART_CHECK((UART[uart_num]->conf1.rx_flow_en != 1), "disable hw flowctrl before using sw control", ESP_FAIL);
UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); UART_ENTER_CRITICAL(&uart_spinlock[uart_num]);
UART[uart_num]->conf0.sw_rts = level & 0x1; UART[uart_num]->conf0.sw_rts = level & 0x1;
UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); UART_ENTER_CRITICAL(&uart_spinlock[uart_num]);
@ -385,7 +395,7 @@ esp_err_t uart_set_dtr(uart_port_t uart_num, int level)
esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config) esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)
{ {
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
UART_CHECK((uart_config), "param null\n", ESP_FAIL); UART_CHECK((uart_config), "param null", ESP_FAIL);
if(uart_num == UART_NUM_0) { if(uart_num == UART_NUM_0) {
periph_module_enable(PERIPH_UART0_MODULE); periph_module_enable(PERIPH_UART0_MODULE);
} else if(uart_num == UART_NUM_1) { } else if(uart_num == UART_NUM_1) {
@ -407,7 +417,7 @@ esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_conf
esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf) esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf)
{ {
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
UART_CHECK((intr_conf), "param null\n", ESP_FAIL); UART_CHECK((intr_conf), "param null", ESP_FAIL);
UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); UART_ENTER_CRITICAL(&uart_spinlock[uart_num]);
UART[uart_num]->int_clr.val = UART_INTR_MASK; UART[uart_num]->int_clr.val = UART_INTR_MASK;
if(intr_conf->intr_enable_mask & UART_RXFIFO_TOUT_INT_ENA_M) { if(intr_conf->intr_enable_mask & UART_RXFIFO_TOUT_INT_ENA_M) {
@ -468,17 +478,17 @@ static void IRAM_ATTR uart_rx_intr_handler_default(void *param)
while(tx_fifo_rem) { while(tx_fifo_rem) {
if(p_uart->tx_len_tot == 0 || p_uart->tx_ptr == NULL || p_uart->tx_len_cur == 0) { if(p_uart->tx_len_tot == 0 || p_uart->tx_ptr == NULL || p_uart->tx_len_cur == 0) {
size_t size; size_t size;
p_uart->tx_head = (uart_event_t*) xRingbufferReceiveFromISR(p_uart->tx_ring_buf, &size); p_uart->tx_head = (uart_tx_data_t*) xRingbufferReceiveFromISR(p_uart->tx_ring_buf, &size);
if(p_uart->tx_head) { if(p_uart->tx_head) {
//The first item is the data description //The first item is the data description
//Get the first item to get the data information //Get the first item to get the data information
if(p_uart->tx_len_tot == 0) { if(p_uart->tx_len_tot == 0) {
p_uart->tx_ptr = NULL; p_uart->tx_ptr = NULL;
p_uart->tx_len_tot = p_uart->tx_head->data.size; p_uart->tx_len_tot = p_uart->tx_head->tx_data.size;
if(p_uart->tx_head->type == UART_DATA_BREAK) { if(p_uart->tx_head->type == UART_DATA_BREAK) {
p_uart->tx_len_tot = p_uart->tx_head->data.size; p_uart->tx_len_tot = p_uart->tx_head->tx_data.size;
p_uart->tx_brk_flg = 1; p_uart->tx_brk_flg = 1;
p_uart->tx_brk_len = p_uart->tx_head->data.brk_len; p_uart->tx_brk_len = p_uart->tx_head->tx_data.brk_len;
} }
//We have saved the data description from the 1st item, return buffer. //We have saved the data description from the 1st item, return buffer.
vRingbufferReturnItemFromISR(p_uart->tx_ring_buf, p_uart->tx_head, &HPTaskAwoken); vRingbufferReturnItemFromISR(p_uart->tx_ring_buf, p_uart->tx_head, &HPTaskAwoken);
@ -553,7 +563,7 @@ static void IRAM_ATTR uart_rx_intr_handler_default(void *param)
uart_reg->int_clr.rxfifo_full = 1; uart_reg->int_clr.rxfifo_full = 1;
UART_EXIT_CRITICAL_ISR(&uart_spinlock[uart_num]); UART_EXIT_CRITICAL_ISR(&uart_spinlock[uart_num]);
uart_event.type = UART_DATA; uart_event.type = UART_DATA;
uart_event.data.size = rx_fifo_len; uart_event.size = rx_fifo_len;
//If we fail to push data to ring buffer, we will have to stash the data, and send next time. //If we fail to push data to ring buffer, we will have to stash the data, and send next time.
//Mainly for applications that uses flow control or small ring buffer. //Mainly for applications that uses flow control or small ring buffer.
if(pdFALSE == xRingbufferSendFromISR(p_uart->rx_ring_buf, p_uart->rx_data_buf, p_uart->rx_stash_len, &HPTaskAwoken)) { if(pdFALSE == xRingbufferSendFromISR(p_uart->rx_ring_buf, p_uart->rx_data_buf, p_uart->rx_stash_len, &HPTaskAwoken)) {
@ -711,9 +721,9 @@ static int uart_tx_all(uart_port_t uart_num, const char* src, size_t size, bool
if(p_uart_obj[uart_num]->tx_buf_size > 0) { if(p_uart_obj[uart_num]->tx_buf_size > 0) {
int max_size = xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf); int max_size = xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf);
int offset = 0; int offset = 0;
uart_event_t evt; uart_tx_data_t evt;
evt.data.size = size; evt.tx_data.size = size;
evt.data.brk_len = brk_len; evt.tx_data.brk_len = brk_len;
if(brk_en) { if(brk_en) {
evt.type = UART_DATA_BREAK; evt.type = UART_DATA_BREAK;
} else { } else {
@ -882,12 +892,12 @@ esp_err_t uart_flush(uart_port_t uart_num)
esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, int uart_intr_num, void* uart_queue) esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, int uart_intr_num, void* uart_queue)
{ {
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
UART_CHECK((rx_buffer_size > 0), "uart rx buffer length error\n", ESP_FAIL); UART_CHECK((rx_buffer_size > 0), "uart rx buffer length error", ESP_FAIL);
if(p_uart_obj[uart_num] == NULL) { if(p_uart_obj[uart_num] == NULL) {
ESP_INTR_DISABLE(uart_intr_num); ESP_INTR_DISABLE(uart_intr_num);
p_uart_obj[uart_num] = (uart_obj_t*) malloc(sizeof(uart_obj_t)); p_uart_obj[uart_num] = (uart_obj_t*) malloc(sizeof(uart_obj_t));
if(p_uart_obj[uart_num] == NULL) { if(p_uart_obj[uart_num] == NULL) {
ESP_LOGE(UART_TAG, "UART driver malloc error\n"); ESP_LOGE(UART_TAG, "UART driver malloc error");
return ESP_FAIL; return ESP_FAIL;
} }
p_uart_obj[uart_num]->uart_num = uart_num; p_uart_obj[uart_num]->uart_num = uart_num;
@ -909,7 +919,7 @@ esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_b
if(uart_queue) { if(uart_queue) {
p_uart_obj[uart_num]->xQueueUart = xQueueCreate(queue_size, sizeof(uart_event_t)); p_uart_obj[uart_num]->xQueueUart = xQueueCreate(queue_size, sizeof(uart_event_t));
*((QueueHandle_t*) uart_queue) = p_uart_obj[uart_num]->xQueueUart; *((QueueHandle_t*) uart_queue) = p_uart_obj[uart_num]->xQueueUart;
ESP_LOGI(UART_TAG, "queue free spaces: %d\n", uxQueueSpacesAvailable(p_uart_obj[uart_num]->xQueueUart)); ESP_LOGI(UART_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_uart_obj[uart_num]->xQueueUart));
} else { } else {
p_uart_obj[uart_num]->xQueueUart = NULL; p_uart_obj[uart_num]->xQueueUart = NULL;
} }
@ -927,7 +937,7 @@ esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_b
p_uart_obj[uart_num]->tx_buf_size = 0; p_uart_obj[uart_num]->tx_buf_size = 0;
} }
} else { } else {
ESP_LOGE(UART_TAG, "UART driver already installed\n"); ESP_LOGE(UART_TAG, "UART driver already installed");
return ESP_FAIL; return ESP_FAIL;
} }
uart_isr_register(uart_num, uart_intr_num, uart_rx_intr_handler_default, p_uart_obj[uart_num]); uart_isr_register(uart_num, uart_intr_num, uart_rx_intr_handler_default, p_uart_obj[uart_num]);
@ -951,7 +961,7 @@ esp_err_t uart_driver_delete(uart_port_t uart_num)
{ {
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
if(p_uart_obj[uart_num] == NULL) { if(p_uart_obj[uart_num] == NULL) {
ESP_LOGI(UART_TAG, "ALREADY NULL\n"); ESP_LOGI(UART_TAG, "ALREADY NULL");
return ESP_OK; return ESP_OK;
} }
ESP_INTR_DISABLE(p_uart_obj[uart_num]->intr_num); ESP_INTR_DISABLE(p_uart_obj[uart_num]->intr_num);