esp32C2串口不够用(esp_log_set_vprintf)使用任意IO模拟串口解决

c 复制代码
#define GPIO_OUTPUT_IO_0    10
#define GPIO_OUTPUT_PIN_SEL ((1ULL << GPIO_OUTPUT_IO_0))

// 模拟串口0初始化
void iouart0_init(void)
{
    gpio_config_t io_conf = {};
    // disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    // set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    // bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
    // disable pull-down mode
    io_conf.pull_down_en = 0;
    // disable pull-up mode
    io_conf.pull_up_en = 0;
    // configure GPIO with the given settings
    gpio_config(&io_conf);
}

void iouart1_TXD(uint8_t option)
{
    if (1 == option) {
        gpio_set_level(GPIO_OUTPUT_IO_0, 1);
    } else {
        gpio_set_level(GPIO_OUTPUT_IO_0, 0);
    }
}

/*****************************************
 *	函 数 名: iouart1_SendByte
 *	功能说明: 模拟串口发送一字节数据
 *	形    参:无
 *	返 回 值: 无
 ******************************************/
void iouart1_SendByte(uint8_t datatoSend)
{
    uint8_t i, tmp;

    // 开始位
    iouart1_TXD(0); // 将TXD的引脚的电平置低
    esp_rom_delay_us(IO_USART_SENDDELAY_TIME);

    for (i = 0; i < 8; i++) {
        tmp = (datatoSend >> i) & 0x01;

        if (tmp == 0) {
            iouart1_TXD(0);
            esp_rom_delay_us(IO_USART_SENDDELAY_TIME); // 0
        } else {
            iouart1_TXD(1);
            esp_rom_delay_us(IO_USART_SENDDELAY_TIME); // 1
        }
    }

    // 结束位
    iouart1_TXD(1); // 将TXD的引脚的电平置高
    esp_rom_delay_us(IO_USART_SENDDELAY_TIME);
}

void USART_Send(uint8_t *buf, uint8_t len)
{
    uint8_t t;
    for (t = 0; t < len; t++) {
        iouart1_SendByte(buf[t]);
    }
}

uint8_t esp_gpio_log_vprintf(const char *fmt, va_list vp)
{
    static char log_buffer[512];

    taskENTER_CRITICAL();
    size_t len = vsnprintf(log_buffer, sizeof(log_buffer), fmt, vp);
    if (len > sizeof(log_buffer) - 1) {
        log_buffer[sizeof(log_buffer) - 1] = 0;
    }
    USART_Send((uint8_t *)log_buffer, len);
    taskEXIT_CRITICAL(); // 退出基本临界区
    return len;
}

然后重点来了

结果打印 9600 如下

相关推荐
山下梅子酒2252 小时前
洛谷-入门-B2079
c语言
Mr_Macallon3 小时前
C++回顾(基础)(01)
c语言·开发语言·c++·学习
wuminyu4 小时前
LockStack在虚拟线程Mount和Unmount拷贝过程剖析
java·linux·c语言·jvm·c++
Felven4 小时前
B. Reverse a Permutation
c语言
Logic1015 小时前
C语言/数据结构排序题解:三个数的最大乘积——排序后比较两种候选情况
c语言·数据结构·数组·排序·时间复杂度·算法题·负数处理
无忧.芙桃5 小时前
位图妙用:512MB 实现 40 亿整数的存在性查询
c语言·c++·面试·哈希算法
听风若依5 小时前
数据在内存中的存储-纸面笔记整理
c语言·笔记
殷色玫瑰6 小时前
C++ string类详解:常用接口、字符串操作与模拟实现
java·linux·c语言·开发语言·数据结构·c++
Code_Solitude6 小时前
C语言:关于二维数组的作业总结
java·c语言·前端
老花眼猫8 小时前
数学艺术图案画-曼陀罗(93)
c语言·经验分享·青少年编程·课程设计