/*********************************************************************
* 初始化串口
**********************************************************************/
static u8 mem_addr[] ="0123456789";
void init_uart(void)
{
//定义中断结构体
NVIC_InitTypeDef NVIC_InitStructure ;
//定义IO初始化结构体
GPIO_InitTypeDef GPIO_InitStructure;
//定义串口结构体
USART_InitTypeDef USART_InitStructure;
//定义DMA结构体
DMA_InitTypeDef DMA_InitStructure;
//打开串口对应的外设时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
//串口发DMA配置
//启动DMA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
//DMA发送中断设置
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA通道配置
DMA_DeInit(DMA1_Stream4);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
//外设地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&UART4->DR);
//内存地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)mem_addr;
//dma传输方向
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
//设置DMA在传输时缓冲区的长度
DMA_InitStructure.DMA_BufferSize = 10;
//设置DMA的外设递增模式,一个外设
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//设置DMA的内存递增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外设数据字长
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//内存数据字长
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//设置DMA的传输模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//设置DMA的优先级别
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//指定如果FIFO模式或直接模式将用于指定的流 : 不使能FIFO模式
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
//指定了FIFO阈值水平
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
//指定的Burst转移配置内存传输
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
//指定的Burst转移配置外围转移 */
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//配置DMA1的通道
DMA_Init(DMA1_Stream4, &DMA_InitStructure);
//使能中断
DMA_ITConfig(DMA1_Stream4,DMA_IT_TC,ENABLE);
//初始化串口参数
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_BaudRate = 115200;
//初始化串口
USART_Init(UART4,&USART_InitStructure);
//中断配置
USART_ITConfig(UART4,USART_IT_TC,DISABLE);
USART_ITConfig(UART4,USART_IT_RXNE,DISABLE);
USART_ITConfig(UART4,USART_IT_IDLE,ENABLE);
//配置中断
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //通道设置为串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占先等级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure);
//采用DMA方式发送
USART_DMACmd(UART4,USART_DMAReq_Tx,ENABLE);
//中断配置
USART_ITConfig(UART4,USART_IT_TC,DISABLE);
USART_ITConfig(UART4,USART_IT_RXNE,DISABLE);
USART_ITConfig(UART4,USART_IT_TXE,DISABLE);
USART_ITConfig(UART4,USART_IT_IDLE,ENABLE);
//启动串口
USART_Cmd(UART4, ENABLE);
//设置IO口时钟
//uart config
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_UART4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
while (1)
{
//设置传输数据长度
DMA_SetCurrDataCounter(DMA1_Stream4, 10);
//打开DMA,开始发送
DMA_Cmd(DMA1_Stream4,ENABLE);
OSTimeDly(2000);
}
}
/*********************************************************************
* GSM模块:DMA发送中断处理函数
**********************************************************************/
void DMA1_Stream4_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream4,DMA_IT_TCIF4) != RESET)
{
//清除标志位
DMA_ClearFlag(DMA1_Stream4,DMA_FLAG_TCIF4);
//关闭DMA
DMA_Cmd(DMA1_Stream4,DISABLE);
//打开发送完成中断,发送最后两个字节
USART_ITConfig(UART4,USART_IT_TC,ENABLE);
}
}
/*********************************************************************
* GSM模块:串口中断处理函数
**********************************************************************/
void UART4_IRQHandler(void)
{
if(USART_GetITStatus(UART4, USART_IT_TC) != RESET)
{
//关闭发送完成中断
USART_ITConfig(UART4,USART_IT_TC,DISABLE);
}
}
|