OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4404|回复: 2

stm32 usart DMA 双缓冲 中断问题

[复制链接]

4

主题

9

帖子

0

精华

新手上路

积分
22
金钱
22
注册时间
2017-9-1
在线时间
22 小时
发表于 2017-10-19 21:22:26 | 显示全部楼层 |阅读模式
#include "FreeRTOS.h"
#include "stm32f4xx.h"
#include "stm32f4xx_dma.h"
#include "queue.h"
#include "task.h"
#include "zigbee_task.h"
#include "bsp_zigbee.h"



unsigned char gcav_Usart1DmaRxBuffer[2][1024];
unsigned char gcav_Usart1DmaTxBuffer[2][1024] = {{1},{'+','+','+',}};



void zigbee_usart1_init()
{

    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef UART_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    DMA_InitTypeDef DMA_InitStruct;

    /*浣胯兘GPIOB&USART&DMA2*/
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);


    /*GPIO閰嶇疆*/
    /*澶嶇敤GPIO*/
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);


    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;       
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOB,&GPIO_InitStruct);

    USART_DeInit(USART1);

    UART_InitStruct.USART_BaudRate = 115200;
    UART_InitStruct.USART_StopBits = USART_StopBits_1;
    UART_InitStruct.USART_Parity = USART_Parity_No;
    UART_InitStruct.USART_WordLength = USART_WordLength_8b;
    UART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    UART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART1, &UART_InitStruct);

        /*涓?柇閰嶇疆*/
    NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 5;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    /*涓?柇浣胯兘*/
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    /*涓插彛浣胯兘*/
    USART_Cmd(USART1, ENABLE);

    /*RX*/
    /*鍏抽棴DMA閫氶亾*/
    DMA_DeInit(DMA2_Stream5);

    while(DMA_GetCmdStatus(DMA2_Stream5) != DISABLE);

    DMA_InitStruct.DMA_Channel = DMA_Channel_4;
    DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
    DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&gcav_Usart1DmaRxBuffer[0];
    DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStruct.DMA_BufferSize = 1024;
    DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
    DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
    DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

    DMA_DoubleBufferModeConfig(DMA2_Stream5, (uint32_t)&gcav_Usart1DmaRxBuffer[1], DMA_Memory_0);
    DMA_DoubleBufferModeCmd(DMA2_Stream5, ENABLE);

    DMA_Init(DMA2_Stream5, &DMA_InitStruct);
        /*涓?柇浣胯兘*/
   // DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE);



     /*TX*/
    DMA_DeInit(DMA2_Stream7);

    while(DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);

    DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&gcav_Usart1DmaTxBuffer[0];
    DMA_InitStruct.DMA_DIR = DMA_DIR_MemoryToPeripheral;

    DMA_DoubleBufferModeConfig(DMA2_Stream7, (uint32_t)&gcav_Usart1DmaTxBuffer[1], DMA_Memory_0);
    DMA_DoubleBufferModeCmd(DMA2_Stream7, ENABLE);

    DMA_Init(DMA2_Stream7, &DMA_InitStruct);

    /*DMA TX*/
    NVIC_InitStruct.NVIC_IRQChannel = DMA2_Stream7_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    /*涓?柇浣胯兘*/
    DMA_ITConfig(DMA2_Stream7, DMA_IT_TC, ENABLE);

    /*DMA浣胯兘*/
    DMA_Cmd(DMA2_Stream5, ENABLE);

    USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

}
/**

USART1 中断函数

**/
void USART1_IRQHandler(void)
{
    zigbee_msg_st ltv_Msg;
    BaseType_t ltv_HigherPriorityTaskWoken;
       
  if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET){
          //关闭,防干扰
    DMA_Cmd(DMA2_Stream5, DISABLE);
       
    /*获取当前正在使用的buff*/
    ltv_Msg.mcv_RxIndex = DMA_GetCurrentMemoryTarget(DMA2_Stream5);
    ltv_Msg.mcv_TxIndex = DMA_GetCurrentMemoryTarget(DMA2_Stream7);
    /*切换buff*/
    DMA_DoubleBufferModeCmd(DMA2_Stream5, DISABLE);
    if(!ltv_Msg.mcv_RxIndex) {
       DMA_DoubleBufferModeConfig(DMA2_Stream5, (uint32_t)&gcav_Usart1DmaRxBuffer[0], DMA_Memory_1);
     } else {
       DMA_DoubleBufferModeConfig(DMA2_Stream5, (uint32_t)&gcav_Usart1DmaRxBuffer[1], DMA_Memory_0);
     }

         DMA_DoubleBufferModeCmd(DMA2_Stream5, ENABLE);
         
    ltv_Msg.msv_RcvLen=1024-DMA_GetCurrDataCounter(DMA2_Stream5);
        /**重新定义长度 下次接收**/
    DMA_SetCurrDataCounter(DMA2_Stream5, 1024);
    DMA_Cmd(DMA2_Stream5, ENABLE);

        USART1->SR;
        USART1->DR;
                /*Send Msg*/
        xQueueSendFromISR(gtv_ZigbeeTaskMsgQueueHandle, &ltv_Msg, &ltv_HigherPriorityTaskWoken);
   }
}

/**
  * @brief  DMA1 Usart1 Tx 涓?柇鍑芥暟
  * @param  None
  * @retval None
  */
void DMA2_Stream7_IRQHandler(void)
{

    if(DMA_GetITStatus(DMA2_Stream7, DMA_IT_TCIF7) != RESET) {
        DMA_Cmd(DMA2_Stream7, DISABLE);
        DMA_ClearFlag(DMA2_Stream7, DMA_IT_TCIF7);
    }
}


/**
  * @brief  USART1 鏁版嵁鍙戦
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

4

主题

9

帖子

0

精华

新手上路

积分
22
金钱
22
注册时间
2017-9-1
在线时间
22 小时
 楼主| 发表于 2017-10-19 21:23:08 | 显示全部楼层
中断进不去,发送不了数据!
回复 支持 反对

使用道具 举报

2

主题

18

帖子

0

精华

初级会员

Rank: 2

积分
159
金钱
159
注册时间
2019-4-30
在线时间
59 小时
发表于 2019-6-5 13:49:46 | 显示全部楼层
你的问题搞定了吗我也这样
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-25 03:44

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表