论坛元老
 
- 积分
- 7464
- 金钱
- 7464
- 注册时间
- 2015-1-15
- 在线时间
- 1368 小时
|
发表于 2019-7-30 08:30:09
|
显示全部楼层
发一个STM32F103ZET6芯片通过DAM实现USART<->USART,楼主可以参考,代码如下:
void sUSART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* USART1 Pins configuration ************************************************/
/* Enable the USART3 Pins Software Remapping */
//GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
/* Enable the USART2 Pins Software Remapping */
//GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
/* Configure USARTy Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTz Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTy Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTz Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*------------------------------- USART-------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600
- Word Length = USART_WordLength_7b
- Stop Bit = USART_StopBits_1
- Parity = USART_Parity_No
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
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_Tx| USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Init(USART2, &USART_InitStructure);
/*------------------------------- DMA---------------------------------------*/
/* Common DMA configuration */
DMA_InitStructure.DMA_BufferSize = 65535;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
#if 0
/* DMA1 Channel4 configuration */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(&USART1->DR);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
#endif
#if 1
/* DMA1 Channel5 configuration */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(&USART2->DR);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR);
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
#endif
#if 0
/* DMA1 Channel7 configuration */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(&USART2->DR);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR);
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
#endif
#if 1
/* DMA1 Channel6 configuration */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(&USART1->DR);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
#endif
/* Enable the USART4 Rx and Tx DMA1 requests */
USART_DMACmd(USART2, USART_DMAReq_Rx|USART_DMAReq_Tx, ENABLE);
/* Enable the USART1 Rx and Tx DMA1 requests */
USART_DMACmd(USART1, USART_DMAReq_Tx|USART_DMAReq_Rx, ENABLE);
/* Enable the DMA1 channels */
#if 0
DMA_Cmd(DMA1_Channel4, ENABLE);
#endif
#if 1
DMA_Cmd(DMA1_Channel5, ENABLE);
#endif
#if 0
DMA_Cmd(DMA1_Channel7, ENABLE);
#endif
#if 1
DMA_Cmd(DMA1_Channel6, ENABLE);
#endif
/* Enable Usart */
USART_Cmd(USART2, ENABLE);
USART_Cmd(USART1, ENABLE);
/* Test on Channels DMA_FLAG_TC flag */
#if 0
while(!DMA_GetFlagStatus(DMA1_FLAG_TC4));
#endif
#if 1
while(!DMA_GetFlagStatus(DMA1_FLAG_TC5));
#endif
#if 0
while(!DMA_GetFlagStatus(DMA1_FLAG_TC7));
#endif
#if 1
while(!DMA_GetFlagStatus(DMA1_FLAG_TC6));
#endif
/* Clear DMA1 TC flags */
#if 0
DMA_ClearFlag(DMA1_FLAG_TC4);
#endif
#if 1
DMA_ClearFlag(DMA1_FLAG_TC5);
#endif
#if 0
DMA_ClearFlag(DMA1_FLAG_TC7);
#endif
#if 1
DMA_ClearFlag(DMA1_FLAG_TC6);
#endif
/* Disable DMA2 channels */
#if 0
DMA_Cmd(DMA1_Channel4, DISABLE);
#endif
#if 0
DMA_Cmd(DMA1_Channel5, DISABLE);
#endif
#if 0
DMA_Cmd(DMA1_Channel7, DISABLE);
#endif
#if 0
DMA_Cmd(DMA1_Channel6, DISABLE);
#endif
/* Enable the USARTx Receive interrupt: this interrupt is generated when the
USARTx receive data register is not empty */
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* NVIC configuration */
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
} |
|