新手上路
- 积分
- 22
- 金钱
- 22
- 注册时间
- 2017-9-1
- 在线时间
- 22 小时
|
#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, <v_Msg, <v_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 鏁版嵁鍙戦 |
|