发帖
11 0 0

AI_WB2_12F UART通信问题

twinkleyin
注册会员

1

主题

5

回帖

90

积分

注册会员

积分
90
Ai-WB2系列 1236 11 2024-12-8 08:07:04
最近用12F进行串口通信,采用轮询方式,12F问询下位机,下位机回复,问询数据最后一位是结尾,数据倒数4,5位是下位机回复的校验,用于下一条问询的标记。
222.png
上图红框就是收到回复并将校验用于下条问询,绿色的就是没有收到回复(通过串口助手,回复消息确定已经到达12F,而且数据没有出错)。代码如下图,一个循环任务不停接收串口消息,收到以后进行处理,超时时间200毫秒,红框回复的60毫秒左右,绿框的消息超时200毫秒。期间通过修改超时时间烧录程序后完全正常过两次,一次修改成2秒,一次修改成5秒,所有消息都是60毫秒左右收到,诡异的问题来了,断电重新上电后,又变得和截图一样出现断断续续情况,多次重开也无法恢复正常。
333.png
串口设置
444.png


──── 0人觉得很赞 ────
11.png

使用道具 举报

twinkleyin 楼主
2024-12-8 15:22:23
来个技术主持帮忙看看,这俩天持续关注
2024-12-9 08:35:41
马上抓个技术给你解决~
要不你改成中断接收的方式吧,串口轮询接收本身就慢
2024-12-9 15:18:59
Ai-Thinker小泽 发表于 2024-12-9 08:49
要不你改成中断接收的方式吧,串口轮询接收本身就慢

按照 https://bbs.ai-thinker.com/forum ... _UART_RX_TRIGGER_ON这个找不到
twinkleyin 发表于 2024-12-9 15:18
按照 https://bbs.ai-thinker.com/forum.php?mod=viewthread&tid=45176&fromuid=12784HOSAL_UART_RX_TRIG ...

int hosal_uart_ioctl(hosal_uart_dev_t *uart, int ctl, void *p_arg)
{
        hosal_uart_dma_cfg_t *dma_cfg;

    switch (ctl) {
    case HOSAL_UART_BAUD_SET:
        uart->config.baud_rate = (uint32_t)p_arg;
        __uart_config_set(uart, &uart->config);
        break;
    case HOSAL_UART_BAUD_GET:
        if (p_arg) {
            *(uint32_t *)p_arg = uart->config.baud_rate;
        }
        break;
    case HOSAL_UART_DATA_WIDTH_SET:
        uart->config.data_width = (hosal_uart_data_width_t)p_arg;
        __uart_config_set(uart, &uart->config);
        break;
    case HOSAL_UART_DATA_WIDTH_GET:
        if (p_arg) {
            *(hosal_uart_data_width_t *)p_arg = uart->config.data_width;
        }
        break;
    case HOSAL_UART_STOP_BITS_SET:
        uart->config.stop_bits = (hosal_uart_stop_bits_t)p_arg;
        __uart_config_set(uart, &uart->config);
        break;
    case HOSAL_UART_STOP_BITS_GET:
        if (p_arg) {
            *(hosal_uart_stop_bits_t *)p_arg = uart->config.stop_bits;
        }
        break;
    case HOSAL_UART_FLOWMODE_SET:
        uart->config.flow_control = (hosal_uart_flow_control_t)p_arg;
        __uart_config_set(uart, &uart->config);
        break;
    case HOSAL_UART_FLOWSTAT_GET:
        if (p_arg) {
            *(hosal_uart_flow_control_t *)p_arg = uart->config.flow_control;
        }
        break;
    case HOSAL_UART_PARITY_SET:
        uart->config.parity = (hosal_uart_parity_t)p_arg;
        __uart_config_set(uart, &uart->config);
        break;
    case HOSAL_UART_PARITY_GET:
        if (p_arg) {
            *(hosal_uart_parity_t *)p_arg = uart->config.parity;
        }
        break;
    case HOSAL_UART_MODE_SET:
        uart->config.mode = (hosal_uart_mode_t)p_arg;
        __uart_config_set(uart, &uart->config);
        break;
    case HOSAL_UART_MODE_GET:
        if (p_arg) {
            *(hosal_uart_mode_t *)p_arg = uart->config.mode;
        }
        break;
    case HOSAL_UART_FREE_TXFIFO_GET:
        if (p_arg) {
            *(uint32_t *)p_arg = UART_GetTxFifoCount(uart->port);
        }
        break;
    case HOSAL_UART_FREE_RXFIFO_GET:
        if (p_arg) {
            *(uint32_t *)p_arg = UART_GetRxFifoCount(uart->port);
        }
        break;
    case HOSAL_UART_FLUSH:
        bl_uart_flush(uart->port);
        break;
    case HOSAL_UART_TX_TRIGGER_ON:
            bl_uart_int_tx_enable(uart->port);
            break;
    case HOSAL_UART_TX_TRIGGER_OFF:
            bl_uart_int_tx_disable(uart->port);
            break;
    case HOSAL_UART_DMA_TX_START:
            dma_cfg = (hosal_uart_dma_cfg_t *)p_arg;
            if (__uart_dma_txcfg(uart, dma_cfg) != 0) {
                    return -1;
            }
            hosal_dma_chan_start(uart->dma_tx_chan);
            break;
    case HOSAL_UART_DMA_RX_START:
            dma_cfg = (hosal_uart_dma_cfg_t *)p_arg;
            if (__uart_dma_rxcfg(uart, dma_cfg) != 0) {
                    return -1;
            }
            hosal_dma_chan_start(uart->dma_rx_chan);
            break;
    default :
        return -1;
    }
    return 0;
}
不是有吗,函数实现这一部分
Ai-Thinker小泽 发表于 2024-12-9 15:48
int hosal_uart_ioctl(hosal_uart_dev_t *uart, int ctl, void *p_arg)
{
        hosal_uart_dma_cfg_t *dma_cf ...

好像真没有,这我去
Ai-Thinker小泽 发表于 2024-12-9 15:50
好像真没有,这我去

研究了一下,hosal的果然不好用,你用那个更底层的,bl_uart.c里面的接口
Ai-Thinker小泽 发表于 2024-12-9 16:21
研究了一下,hosal的果然不好用,你用那个更底层的,bl_uart.c里面的接口

static void __uart_rx_irq(void *p_arg)
{
    uint8_t data;

    int ch;

    while ((ch = bl_uart_data_recv(AT_UART_NUM)) >= 0) {
        data = (uint8_t)ch;
        axk_at_data_recv((char *)&data, 1);
    }
}
void axk_hal_uart_config(at_uart_t *uart_cfg)
{
    int dataBits = UART_DATABITS_8;
    int stopBits = UART_STOPBITS_1;
    int parity = UART_PARITY_NONE;
    int cts_pin = 0xff;
    int rts_pin = 0xff;

    if (uart_cfg->databits == 5) {
        dataBits = UART_DATABITS_5;
    } else if (uart_cfg->databits == 6) {
        dataBits = UART_DATABITS_6;
    } else if (uart_cfg->databits == 7) {
        dataBits = UART_DATABITS_7;
    } else if (uart_cfg->databits == 9) {
        dataBits = UART_DATABITS_8;
    }

    if (uart_cfg->stopbits == UART_STOP_BITS_1) {
        stopBits = UART_STOPBITS_1;
    } else if (uart_cfg->stopbits == UART_STOP_BITS_1_5) {
        stopBits = UART_STOPBITS_1_5;
    } else if (uart_cfg->stopbits == UART_STOP_BITS_2) {
        stopBits = UART_STOPBITS_2;
    }

    vTaskDelay(20 / portTICK_PERIOD_MS);

    if (uart_cfg->flowcontrol & AT_UART_FLOWCTRL_RTS) {
        rts_pin = 4;
    } else if (uart_cfg->flowcontrol & AT_UART_FLOWCTRL_CTS) {
        cts_pin = 3;
    }

    uart_fwctrl = uart_cfg->flowcontrol;

    parity = uart_cfg->parity;
    bl_uart_init_ex(AT_UART_NUM, GLB_GPIO_PIN_16, GLB_GPIO_PIN_7, cts_pin, rts_pin, uart_cfg->baudrate,
                    dataBits, stopBits, parity);

    bl_uart_int_tx_notify_register(AT_UART_NUM, __uart_tx_irq, NULL);
    bl_uart_int_rx_notify_register(AT_UART_NUM, __uart_rx_irq, NULL);
    bl_uart_int_enable(AT_UART_NUM);
    bl_uart_int_tx_disable(AT_UART_NUM);
}
2024-12-10 08:39:21
Ai-Thinker小泽 发表于 2024-12-9 16:22
static void __uart_rx_irq(void *p_arg)
{
    uint8_t data;

我用这个,还是用轮询吗?
您需要登录后才可以回帖 立即登录
高级模式
12下一页
统计信息
  • 会员数: 28078 个
  • 话题数: 39675 篇