FX3(CX3)学习笔记9-串口DMA

1、实验环境

  • 硬件平台:CYUSB3065芯片自制板

  • sdk版本:EZ-USB FX3 SDK1.3

  • 实验例程:cycx3_uvc_ov5640(cypress官网下载demo)

2、实验目的

  • 使用串口的DMA接收功能,将接收到的数据打印出来

3、程序代码

  • 3.1 串口初始化
  • 注意:dmaConfig.size必须为16的整数倍
void CyCx3AppUartDmaInit (void)
{
    CyU3PUartConfig_t uartConfig;
    CyU3PDmaChannelConfig_t dmaConfig;
    CyU3PReturnStatus_t status = CY_U3P_SUCCESS;

    /* Initialize the UART module */
    status = CyU3PUartInit ();
    if (status != CY_U3P_SUCCESS)
    {
        /* Error handling */
        CyCx3AppErrorHandler(status);
    }

    /* Configure the UART
       Baudrate = 115200, One stop bit, No parity, Hardware flow control enabled.
     */
    CyU3PMemSet ((uint8_t *)&uartConfig, 0, sizeof(uartConfig));
    uartConfig.baudRate = CY_U3P_UART_BAUDRATE_115200;
    uartConfig.stopBit = CY_U3P_UART_ONE_STOP_BIT;
    uartConfig.parity = CY_U3P_UART_NO_PARITY;
    uartConfig.flowCtrl = CyFalse;
    uartConfig.txEnable = CyTrue;
    uartConfig.rxEnable = CyTrue;
    uartConfig.isDma = CyTrue; /* DMA mode */

    /* Set the UART configuration */
    status = CyU3PUartSetConfig (&uartConfig, NULL);
    if (status != CY_U3P_SUCCESS )
    {
        /* Error handling */
        CyCx3AppErrorHandler(status);
    }

    /* Create a DMA Manual channel between UART producer socket
       and UART consumer socket */
    CyU3PMemSet ((uint8_t *)&dmaConfig, 0, sizeof(dmaConfig));
    dmaConfig.size = 16;
    dmaConfig.count = 2;
    dmaConfig.prodSckId = CY_U3P_LPP_SOCKET_UART_PROD;
    dmaConfig.dmaMode = CY_U3P_DMA_MODE_BUFFER;
    dmaConfig.notification = CY_U3P_DMA_CB_PROD_EVENT;
    dmaConfig.cb = CyFxUartLpDmaCallback;
    dmaConfig.prodHeader = 0;
    dmaConfig.prodFooter = 0;
    dmaConfig.consHeader = 0;
    dmaConfig.prodAvailCount = 0;

    /* Create the channel */
    status = CyU3PDmaChannelCreate (&glUartLpChHandle,
                                    CY_U3P_DMA_TYPE_MANUAL, &dmaConfig);
    if (status != CY_U3P_SUCCESS)
    {
        /* Error handling */
        CyCx3AppErrorHandler(status);
    }

    /* Set UART Tx and Rx transfer Size to infinite */
    status = CyU3PUartRxSetBlockXfer(0xFFFFFFFF);
    if (status != CY_U3P_SUCCESS)
    {
        /* Error handling */
        CyCx3AppErrorHandler(status);
    }

    /* Set DMA Channel transfer size */
    status = CyU3PDmaChannelSetXfer (&glUartLpChHandle, 0);
    if (status != CY_U3P_SUCCESS)
    {
        /* Error handling */
        CyCx3AppErrorHandler(status);
    }

    /* Initialize the debug application */
    status = CyU3PDebugInit (CY_U3P_LPP_SOCKET_UART_CONS, 8);
    if (status != CY_U3P_SUCCESS)
    {
        return;
    }

    CyU3PDebugPreamble (CyFalse);
}
  • 3.2 DMA接收数据回调接口
 void CyFxUartLpDmaCallback (
    CyU3PDmaChannel   *chHandle, /* Handle to the DMA channel. */
    CyU3PDmaCbType_t  type,      /* Callback type.             */
    CyU3PDmaCBInput_t *input)    /* Callback status.           */
{
    CyU3PReturnStatus_t status;
    imu_packet_t imu_packet;

    if (type == CY_U3P_DMA_CB_PROD_EVENT)
    {
        /* This is a produce event notification to the CPU. This notification is
         * received upon reception of every buffer. The buffer will not be sent
         * out unless it is explicitly committed. The call shall fail if there
         * is any application error. */
         
        input->buffer_p.buffer[input->buffer_p.size] = '\0';
        CyU3PDebugPrint (4, "%s", input->buffer_p.buffer);
        
        status = CyU3PDmaChannelDiscardBuffer (chHandle);

        if (status != CY_U3P_SUCCESS)
        {
            CyCx3AppErrorHandler (status);
        }
    }
}

4、实验结果

  • 接收数据通过tx io打印出来
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路人 假

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值