/******************************************************************
ÎÄμμÃû£ouart.c .h
±àòëÆ÷£oMDK5.24.2.0
M C U:STM32F103RB
ê1óÿa£oSTD3.5.0
×÷ óãoÅäÖÃ103μÄèy¸ö′®¿ú
°æ ±¾£oV1.0.1
Ëμ Ã÷£o
ê± ¼ä£o2019Äê11ÔÂ8èÕ 15:33:40
×÷ Õߣo·éÏèμç×ó
@mail :957939065@qq.com
******************************************************************/
#include "config.h"
USART1_DR_TYPEDEF usart1_dr_struct;
USART2_DR_TYPEDEF usart2_dr_struct;
USART3_DR_TYPEDEF usart3_dr_struct;
u8 Uartx;
/*************************************************************
oˉêyÃû£ovoid USART1_GPIO_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSART11ØáaμÄTXD£¨PA9£©£¬RXD£¨PA10£©
***************************************************************/
void USART1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüUSART1£¬GPIOAê±Öó
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //IOÅäÖÃ
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
/*************************************************************
oˉêyÃû£ovoid USART1_RCC_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃTXD£¬RXDIOê±Öó£¬òÔ¼°USART1Ä£¿éμÄê±Öó
***************************************************************/
void USART1_RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO,ENABLE); //IO¿úê±ÖóÔúAPB2×üÏßéÏ
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
/*************************************************************
oˉêyÃû£ovoid USART1_NVIC_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSARTμÄÖD¶Ïèë¿ú£¬ÖD¶ÏóÅÏ輶£¬2¢ê1ÄüUSART2½óêÕÖD¶Ï
***************************************************************/
void USART1_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //ÅäÖÃ′®¿úÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART1_IDLE_PRIO ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1,USART_IT_TC,DISABLE); //
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE); //
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); //¿aÆô¿ÕÏD½óêÕÖD¶Ï
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //ê1ÄüDMA½óêÕ
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //ê1ÄüDMA·¢Ëí
}
/*************************************************************
oˉêyÃû£ovoid USART1_Mode_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£oÅäÖÃUSART2 1¤×÷Ä£ê½
***************************************************************/
void USART1_Mode_Config(u32 baud)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=baud; //ÅäÖÃ′®¿úêôDÔ
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
/*************************************************************
oˉêyÃû£o void USART1_DMA_Configuration(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o °ÑUSART1 ÅäÖÃDMA½óêÕ·¢Ëí·½ê½
***************************************************************/
void USART1_DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
/*****************************************************
ÅäÖ÷¢Ëí·½ê½
****************************************************/
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)usart1_dr_struct.usart_tx_buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = USART1_MAX_SEND_LEN; //
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
/*****************************************************
ÅäÖýóêÕ·½ê½
****************************************************/
DMA_DeInit(DMA1_Channel5); //DMA1í¨μà5ÅäÖÃ
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); //íaéèμØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)usart1_dr_struct.usart_rx_buf; //Äú′æμØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //dma′«êä·½Ïòμ¥Ïò
DMA_InitStructure.DMA_BufferSize = USART1_MAX_RECV_LEN; //éèÖÃDMAÔú′«êäê±»o3åÇøμÄ3¤¶è
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //éèÖÃDMAμÄíaéèμYÔöģ꽣¬ò»¸öíaéè
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //éèÖÃDMAμÄÄú′æμYÔöÄ£ê½
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //íaéèêy¾Y×Ö3¤
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //Äú′æêy¾Y×Ö3¤
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //éèÖÃDMAμÄ′«êäÄ£ê½
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //éèÖÃDMAμÄóÅÏ輶±e
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //éèÖÃDMAμÄ2¸ömemoryÖDμıäá¿»¥Ïà·ÃÎê
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel5,ENABLE); //ê1Äüí¨μà5
}
/*************************************************************
oˉêyÃû£ovoid USART2_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSART2Ä£¿é
***************************************************************/
void USART1_Config(u32 baud)
{
USART1_RCC_Config();
USART1_GPIO_Config();
USART1_Mode_Config(baud);
USART1_NVIC_Config();
USART1_DMA_Configuration();
// USART1_RX_Box=OSMboxCreate((void*)0); //3õê¼»ˉ′®¿ú1½óêÕóêÏäÎa¿Õ
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel5,DISABLE);
// USART1_RX_Flag = 1;
// USART1_RX_STA = USART1->SR;
// USART1_RX_STA = USART1->DR; //ÇåUSART_IT_IDLE±êÖ¾
// USART1_RX_STA = USART1_MAX_RECV_LEN - DMA_GetCurrDataCounter(DMA1_Channel5); //éèÖÃ′«êäêy¾Y3¤¶è
usart1_dr_struct.usart_rx_flag = 1;
usart1_dr_struct.usart_rx_len = USART1->SR;
usart1_dr_struct.usart_rx_len = USART1->DR; //ÇåUSART_IT_IDLE±êÖ¾
usart1_dr_struct.usart_rx_len = USART1_MAX_RECV_LEN - DMA_GetCurrDataCounter(DMA1_Channel5); //éèÖÃ′«êäêy¾Y3¤¶è
DMA1_Channel5->CNDTR = USART1_MAX_RECV_LEN;//ÖØ×°ìî,2¢èýóêÕμØÖ·Æ«Ö·′ó0¿aê¼
DMA_Cmd(DMA1_Channel5, ENABLE);//′|àííê,ÖØ¿aDMA
}
__nop();
}
/*************************************************************
oˉêyÃû£ovoid USART2_RCC_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃTXD£¬RXDIOê±Öó£¬òÔ¼°USART2Ä£¿éμÄê±Öó
***************************************************************/
void USART2_RCC_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_AFIO,ENABLE); //uart2 ê±ÖóÔúAPB1×üÏßéÏ
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
/*************************************************************
oˉêyÃû£ovoid USART2_GPIO_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSART1ØáaμÄTXD£¨PA2£©£¬RXD£¨PA3£©
***************************************************************/
void USART2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüUSART1£¬GPIOAê±Öó
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //IOÅäÖÃ
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
/*************************************************************
oˉêyÃû£ovoid USART2_NVIC_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSART2μÄÖD¶Ïèë¿ú£¬ÖD¶ÏóÅÏ輶£¬2¢ê1ÄüUSART2½óêÕÖD¶Ï
***************************************************************/
void USART2_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //ÅäÖÃ′®¿úÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART2_IDLE_PRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// USART_ITConfig(USART2,USART_IT_TC,DISABLE); //
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //
// USART_ITConfig(USART2,USART_IT_IDLE,ENABLE); //¿aÆô¿ÕÏD½óêÕÖD¶Ï
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE); //ê1ÄüDMA½óêÕ
USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //ê1ÄüDMA·¢Ëí
}
/*************************************************************
oˉêyÃû£ovoid USART2_Mode_Config(void)
2Î êy£o baud 2¨ìØÂê
×÷ óão ÎT
Ëμ Ã÷£oÅäÖÃUSART2 1¤×÷Ä£ê½
***************************************************************/
void USART2_Mode_Config(u32 baud)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=115200; //ÅäÖÃ′®¿úêôDÔ
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE);
}
/*************************************************************
oˉêyÃû£o void USART1_DMA_Configuration(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o °ÑUSART1 ÅäÖÃDMA½óêÕ·¢Ëí·½ê½
***************************************************************/
void USART2_DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
/*****************************************************
ÅäÖ÷¢Ëí·½ê½
****************************************************/
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART2->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)usart2_dr_struct.usart_tx_buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = USART2_MAX_SEND_LEN; //
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
}
/*************************************************************
oˉêyÃû£ovoid USART2_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSART2Ä£¿é
***************************************************************/
void USART2_Config(u32 baud)
{
USART2_RCC_Config();
USART2_GPIO_Config();
USART2_Mode_Config(baud);
USART2_DMA_Configuration();
USART2_NVIC_Config();
memset(&usart2_dr_struct,0,sizeof(usart2_dr_struct));
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //ÖD¶Ï2úéú
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Çå3yÖD¶Ï±êÖ¾
usart2_dr_struct.usart_rx_flag = TRUE;
if(usart2_dr_struct.usart_rx_len<USART2_MAX_RECV_LEN) //èç1û»1Äü½óêÕ
{
usart2_dr_struct.usart_rx_buf[usart2_dr_struct.usart_rx_len++]= USART_ReceiveData(USART2);
}
else
{}
}
__nop();
}
/*************************************************************
oˉêyÃû£ovoid USART3_GPIO_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSART31ØáaμÄTXD£¨PB10£©£¬RXD£¨PB9£©
***************************************************************/
void USART3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //IOÅäÖÃ
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
/*************************************************************
oˉêyÃû£ovoid USART3_NVIC_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSARTμÄÖD¶Ïèë¿ú£¬ÖD¶ÏóÅÏ輶£¬2¢ê1ÄüUSART2½óêÕÖD¶Ï
***************************************************************/
void USART3_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //ÅäÖÃ′®¿úÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART3_IDLE_PRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3,USART_IT_TC ,DISABLE); //
USART_ITConfig(USART3,USART_IT_RXNE ,DISABLE); //
USART_ITConfig(USART3,USART_IT_IDLE ,ENABLE); //¿aÆô¿ÕÏD½óêÕÖD¶Ï
USART_DMACmd(USART3,USART_DMAReq_Rx ,ENABLE); //ê1ÄüDMA½óêÕ
USART_DMACmd(USART3,USART_DMAReq_Tx ,ENABLE); //ê1ÄüDMA·¢Ëí
}
/*************************************************************
oˉêyÃû£ovoid USART3_Mode_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£oÅäÖÃUSART3 1¤×÷Ä£ê½
***************************************************************/
void USART3_Mode_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200; //ÅäÖÃ′®¿úêôDÔ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART3,&USART_InitStructure);
USART_Cmd(USART3,ENABLE);
}
/*************************************************************
oˉêyÃû£ovoid USART1_RCC_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃTXD£¬RXDIOê±Öó£¬òÔ¼°USART1Ä£¿éμÄê±Öó
***************************************************************/
void USART3_RCC_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3|RCC_APB2Periph_AFIO,ENABLE); //IO¿úê±ÖóÔúAPB2×üÏßéÏ
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
/*************************************************************
oˉêyÃû£o void USART3_DMA_Configuration(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o °ÑUSART3 ÅäÖÃDMA½óêÕ·¢Ëí·½ê½
***************************************************************/
void USART3_DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
/*****************************************************
ÅäÖ÷¢Ëí·½ê½
****************************************************/
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART3->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)usart3_dr_struct.usart_tx_buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = USART3_MAX_SEND_LEN; //
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/*****************************************************
ÅäÖýóêÕ·½ê½
****************************************************/
DMA_DeInit(DMA1_Channel3); //DMA1í¨μà5ÅäÖÃ
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART3->DR); //íaéèμØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)usart3_dr_struct.usart_rx_buf; //Äú′æμØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //dma′«êä·½Ïòμ¥Ïò
DMA_InitStructure.DMA_BufferSize = USART3_MAX_RECV_LEN; //éèÖÃDMAÔú′«êäê±»o3åÇøμÄ3¤¶è
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //éèÖÃDMAμÄíaéèμYÔöģ꽣¬ò»¸öíaéè
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //éèÖÃDMAμÄÄú′æμYÔöÄ£ê½
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //íaéèêy¾Y×Ö3¤
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //Äú′æêy¾Y×Ö3¤
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //éèÖÃDMAμÄ′«êäÄ£ê½
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //éèÖÃDMAμÄóÅÏ輶±e
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //éèÖÃDMAμÄ2¸ömemoryÖDμıäá¿»¥Ïà·ÃÎê
DMA_Init(DMA1_Channel3,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel3,ENABLE); //ê1Äüí¨μà5
}
/*************************************************************
oˉêyÃû£ovoid USART2_Config(void)
2Î êy£o ÎT
×÷ óão ÎT
Ëμ Ã÷£o ÅäÖÃUSART2Ä£¿é
***************************************************************/
void USART3_Config(void)
{
USART3_RCC_Config();
USART3_GPIO_Config();
USART3_Mode_Config();
USART3_DMA_Configuration();
USART3_NVIC_Config();
// USART3_RX_Box=OSMboxCreate((void*)0); //3õê¼»ˉ′®¿ú1½óêÕóêÏäÎa¿Õ
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel3,DISABLE);
usart3_dr_struct.usart_rx_flag = 1;
usart3_dr_struct.usart_rx_len = USART3->SR;
usart3_dr_struct.usart_rx_len = USART3->DR; //ÇåUSART_IT_IDLE±êÖ¾
usart3_dr_struct.usart_rx_len = USART3_MAX_RECV_LEN - DMA_GetCurrDataCounter(DMA1_Channel3); //éèÖÃ′«êäêy¾Y3¤¶è
DMA1_Channel3->CNDTR = USART3_MAX_RECV_LEN;//ÖØ×°ìî,2¢èýóêÕμØÖ·Æ«Ö·′ó0¿aê¼
DMA_Cmd(DMA1_Channel3, ENABLE);//′|àííê,ÖØ¿aDMA
}
__nop();
}
/*******************************************************************
oˉêyÃû£ovoid Uart_Send_Char(unsigned char data)
2Î êy£ounsigned char data òa·¢ËíμÄêy¾Y
·μ»ØÖμ£oÎT
×÷ óãoêμÏÖ¿éÑ¡Ôñ′®¿ú·¢Ëíêy¾Y
*******************************************************************/
void Uart_Send_Char(u8 uart,u8 data)
{
switch(uart)
{
case UART1:
{
usart1_dr_struct.usart_tx_buf[0] = data;
break;
}
case UART2:
{
usart2_dr_struct.usart_tx_buf[0] = data;
break;
}
case UART3:
{
usart3_dr_struct.usart_tx_buf[0] = data;
break;
}
default:break;
}
En_Uart_Send(uart,1);
}
/*******************************************************************
oˉêyÃû£ovoid En_Uart_Send(u8 uart,u16 len)
2Î êy£o
u8 uart Ñ¡Ôñòa·¢ËíμĶ˿ú UART1 UART2 UART3 ÖDμÄò»¸ö
u16 len òa·¢ËíμÄ3¤¶è
·μ»ØÖμ£oÎT
×÷ óãoêμÏÖ¿éÑ¡Ôñ′®¿ú·¢Ëíêy¾Y
*******************************************************************/
void En_Uart_Send(u8 uart,u16 len)
{
// u8 time=1;
switch(uart)
{
case UART1:
{
usart1_dr_struct.usart_tx_busy = TRUE;
DMA_Cmd(DMA1_Channel4, DISABLE ); //1رÕUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
DMA_SetCurrDataCounter(DMA1_Channel4,len); //DMAí¨μàμÄDMA»o′æμÄ′óD¡
DMA_Cmd(DMA1_Channel4, ENABLE); //ê1ÄüUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET);//ÅD¶Ïí¨μà4′«êäíê3é ·àËàμè
usart1_dr_struct.usart_tx_busy = FALSE;
DMA_ClearFlag(DMA1_FLAG_TC4); //Çå3yí¨μà4′«êäíê3é±êÖ¾
DMA_Cmd(DMA1_Channel4, DISABLE ); //1رÕUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
break;
}
case UART2:
{
usart3_dr_struct.usart_tx_busy = TRUE;
DMA_Cmd(DMA1_Channel7, DISABLE ); //1رÕUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
DMA_SetCurrDataCounter(DMA1_Channel7,len); //DMAí¨μàμÄDMA»o′æμÄ′óD¡
DMA_Cmd(DMA1_Channel7, ENABLE); //ê1ÄüUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
while(DMA_GetFlagStatus(DMA1_FLAG_TC7) == RESET); //ÅD¶Ïí¨μà4′«êäíê3é
usart2_dr_struct.usart_tx_busy = FALSE;
DMA_ClearFlag(DMA1_FLAG_TC7); //Çå3yí¨μà4′«êäíê3é±êÖ¾
DMA_Cmd(DMA1_Channel7, DISABLE ); //1رÕUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
break;
}
case UART3:
{
usart3_dr_struct.usart_tx_busy = TRUE;
DMA_Cmd(DMA1_Channel2, DISABLE ); //1رÕUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
DMA_SetCurrDataCounter(DMA1_Channel2,len); //DMAí¨μàμÄDMA»o′æμÄ′óD¡
DMA_Cmd(DMA1_Channel2, ENABLE); //ê1ÄüUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
while(DMA_GetFlagStatus(DMA1_FLAG_TC2) == RESET); //ÅD¶Ïí¨μà4′«êäíê3é
usart3_dr_struct.usart_tx_busy = FALSE;
DMA_ClearFlag(DMA1_FLAG_TC2); //Çå3yí¨μà4′«êäíê3é±êÖ¾
DMA_Cmd(DMA1_Channel2, DISABLE ); //1رÕUSART1 TX DMA1 ËùÖ¸ê¾μÄí¨μà
break;
}
default:break;
}
}
/************************************************
oˉêyÃû£ovoid Send_String(int8_t uart,unsigned char *p)
2Î êy£oÎT
·μ»ØÖμ£oÎT
×÷ óãoÏò′®¿ú·¢Ëí×Ö·û′®
Ëμ Ã÷£o
*************************************************/
void Send_String(u8 uart,char *p)
{
u16 i;
switch(uart)
{
case UART1:
{
while(TRUE== usart1_dr_struct.usart_tx_busy);
for (i=0;*p!='\0';i++)//×¼±¸òa·¢ËíμÄêy¾Y
{
usart1_dr_struct.usart_tx_buf[i] =*p++; // RXBUF0 to TXBUF0
}
break;
}
case UART2:
{
while(TRUE== usart2_dr_struct.usart_tx_busy);
for (i=0;*p!='\0';i++)//×¼±¸òa·¢ËíμÄêy¾Y
{
usart2_dr_struct.usart_tx_buf[i] =*p++; // RXBUF0 to TXBUF0
}
break;
}
case UART3:
{
while(TRUE== usart3_dr_struct.usart_tx_busy);
for (i=0;*p!='\0';i++)//×¼±¸òa·¢ËíμÄêy¾Y
{
usart3_dr_struct.usart_tx_buf[i] =*p++; // RXBUF0 to TXBUF0
}
break;
}
default:break;
}
En_Uart_Send(uart,i);
}
/******************************************************************
oˉêyÃû£ovoid Rest_USART_RX_BUF(void)
2Î êy£oÎT
·μ»ØÖμ£oÎT
×÷ óão¸′à-′®¿ú2½óêÕ»o′æ
Ëμ Ã÷£o
******************************************************************/
void Rest_USART_RX_BUF(USART_ENUM usart)
{
switch(usart)
{
case UART1: {memset(usart1_dr_struct.usart_rx_buf,0,sizeof(usart1_dr_struct.usart_rx_buf));usart1_dr_struct.usart_rx_len=0;}
case UART2: {memset(usart2_dr_struct.usart_rx_buf,0,sizeof(usart2_dr_struct.usart_rx_buf));usart2_dr_struct.usart_rx_len=0;}
case UART3: {memset(usart3_dr_struct.usart_rx_buf,0,sizeof(usart3_dr_struct.usart_rx_buf));usart3_dr_struct.usart_rx_len=0;}
default:break;
}
}
void Send_Datas(u8 uart,u8 *buf,u16 len)
{
u16 i = 0;
switch(uart)
{
case UART1:
{
while(TRUE== usart1_dr_struct.usart_tx_busy);
for(i=0;i<len;i++)
{
usart1_dr_struct.usart_tx_buf[i] = buf[i];
}
break;
}
case UART2:
{
while(TRUE== usart2_dr_struct.usart_tx_busy);
for(i=0;i<len;i++)
{
usart2_dr_struct.usart_tx_buf[i] = buf[i];
}
break;
}
case UART3:
{
while(TRUE== usart3_dr_struct.usart_tx_busy);
for(i=0;i<len;i++)
{
usart3_dr_struct.usart_tx_buf[i] = buf[i];
}
break;
}
}
En_Uart_Send(uart,len);
}
|