/*====================================================================================================*/ void RS232_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; USART_InitTypeDef USART_InitStruct; /* UART Clk Init *************************************************************/ RCC_APB2PeriphClockCmd(USARTx_CLK, ENABLE); RCC_AHBPeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE); GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF); GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF); /* UART NVIC Config **********************************************************/ NVIC_InitStruct.NVIC_IRQChannel = USARTx_IRQ; NVIC_InitStruct.NVIC_IRQChannelPriority = 2; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); /* USARTx Tx PA9 */ GPIO_InitStruct.GPIO_Pin = USARTx_TX_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct); /* USARTx Rx PA10 */ GPIO_InitStruct.GPIO_Pin = USARTx_RX_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct); /* UART Init *****************************************************************/ USART_InitStruct.USART_BaudRate = USARTx_BAUDRATE; USART_InitStruct.USART_WordLength = USARTx_BYTESIZE; USART_InitStruct.USART_StopBits = USARTx_STOPBITS; USART_InitStruct.USART_Parity = USARTx_PARITY; USART_InitStruct.USART_HardwareFlowControl = USARTx_HARDWARECTRL; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USARTx, &USART_InitStruct); USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); USART_Cmd(USARTx, ENABLE); USART_ClearFlag(USARTx, USART_FLAG_TC); }
void printf3(char *fmt, ...) { char buffer[BUF_USART3+1]; u8 i = 0; va_list arg_ptr; va_start(arg_ptr, fmt); vsnprintf(buffer,BUF_USART3+1,fmt,arg_ptr); USART_ClearFlag(USART3,USART_FLAG_TC); while ((i <BUF_USART3) && buffer[i]) { USART_SendData(USART3, (u8) buffer[i++]); while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); } va_end(arg_ptr); }
void USART1_IRQHandler(void) #endif { if(USART_GetITStatus( GPRS_USART, USART_IT_RXNE) == SET) { gprs_receive_process_event(USART_ReceiveData( GPRS_USART)); USART_ClearITPendingBit( GPRS_USART,USART_IT_RXNE); } //溢出,如果发生溢出需要先读SR,再读DR寄存器可清除不再进入中断的问题 if(USART_GetFlagStatus( GPRS_USART,USART_FLAG_ORE) == SET) { gprs_receive_process_event(USART_ReceiveData( GPRS_USART)); USART_ClearFlag( GPRS_USART,USART_FLAG_ORE); } }
/******************************************************************************* * Function Name : USART1_IRQHandler * Description : This function handles USART1 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void USART1_IRQHandler(void) { debugpins_isr_set(); if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) { USART_ClearFlag(USART1, USART_FLAG_RXNE); uart_rx_isr(); } if(USART_GetFlagStatus(USART1, USART_FLAG_TXE) != RESET) { uart_tx_isr(); } debugpins_isr_clr(); }
/** ************************************************* @Function :Usart_Send() @Return_Value :Null @Peremater : @Brief : ***************************************************/ u8 Usart1_Send(char *pBuffer,u32 SendNumber) { // if(USART_GetFlagStatus(USART1,USART_FLAG_TC) == SET) { DMA_Cmd(DMA1_Channel4,DISABLE); DMA_ClearFlag(DMA1_FLAG_TC4); USART_ClearFlag(USART1,USART_FLAG_TC); DMA1_Channel4->CNDTR =SendNumber; DMA1_Channel4->CMAR=(u32)pBuffer; DMA_Cmd(DMA1_Channel4,ENABLE); return 1; } return 0; }
/******************************************************************************* * Function Name : EXTI9_5_IRQHandler * Description : This function handles External lines 9 to 5 interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void EXTI9_5_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_LINE_KEY_BUTTON) != RESET) { /* Flush DR register */ USART_ReceiveData(USART2); /* Clear the USART2 RXNE Flag */ USART_ClearFlag(USART2, USART_FLAG_RXNE); /* Enable the USART2 mute mode*/ USART_ReceiverWakeUpCmd(USART2, ENABLE); /* Clear Key Button EXTI Line Pending Bit */ EXTI_ClearITPendingBit(EXTI_LINE_KEY_BUTTON); } }
void USART1_Config(USART_TypeDef* USARTx,INT32U bandrate) { USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); USART_InitStructure.USART_BaudRate = bandrate; 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_Init(USARTx, &USART_InitStructure); USART_ClearFlag(USARTx,USART_FLAG_TC); USART_Cmd(USARTx, ENABLE); }
void USART1_IRQHandler(void) { int i=0; int status=SUCCESS; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //interrupt called in when you receive a char on usart1 { GPIO_WriteBit(GPIOA,GPIO_Pin_12,Bit_SET); //pause the RF trasmission USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);//disable the RXNE interrupts for (i=0;i<32;i++){ GPIO_WriteBit(GPIOA,GPIO_Pin_12,Bit_SET); //pause the RF trasmission status=UsartGetTimed(USART1, &Receivedpackage.bytes[i]); //Receive the char in the buffer GPIO_WriteBit(GPIOA,GPIO_Pin_12,Bit_RESET); //Restart the RF trasmission if (status==ERROR) break; //if the tramission Timed out than break } if (status==SUCCESS){ //if you received 32 chars if(checkDatagram(&Receivedpackage)) //and the datagram is correct parse it status=parseDatagram(&Receivedpackage); else status=ERROR; } USART_ClearFlag(USART1, USART_IT_RXNE); USART_ClearITPendingBit(USART1, USART_IT_RXNE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); GPIO_WriteBit(GPIOA,GPIO_Pin_12,Bit_RESET); //Restart the RF trasmission if (status==SUCCESS) //if everything is correct than send ACK sendAck(); else //If not send NACK sendNack(); } /*if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { /* Write one byte to the transmit data register // USART_SendData(USART1, TxBuffer[TxCount++]); if(TxCount == NbrOfDataToTransfer) { // Disable the USART1 Transmit interrupt USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } */ }
/*====================================================================================================*/ void Serial_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef UART_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; /* UART Clk ******************************************************************/ UARTx_CLK_ENABLE(); /* UART AF *******************************************************************/ GPIO_PinAFConfig(UARTx_TX_GPIO_PORT, UARTx_TX_SOURCE, UARTx_TX_AF); GPIO_PinAFConfig(UARTx_RX_GPIO_PORT, UARTx_RX_SOURCE, UARTx_RX_AF); /* UART Pin ******************************************************************/ GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Pin = UARTx_TX_PIN; GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = UARTx_RX_PIN; GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct); /* UART IT *******************************************************************/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStruct.NVIC_IRQChannel = UARTx_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x000F; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); /* UART Init *****************************************************************/ UART_InitStruct.USART_BaudRate = UARTx_BAUDRATE; UART_InitStruct.USART_WordLength = UARTx_BYTESIZE; UART_InitStruct.USART_StopBits = UARTx_STOPBITS; UART_InitStruct.USART_Parity = UARTx_PARITY; UART_InitStruct.USART_HardwareFlowControl = UARTx_HARDWARECTRL; UART_InitStruct.USART_Mode = UARTx_MODE; USART_Init(UARTx, &UART_InitStruct); /* UART Enable ***************************************************************/ USART_ITConfig(UARTx, USART_IT_RXNE, ENABLE); USART_Cmd(UARTx, ENABLE); USART_ClearFlag(UARTx, USART_FLAG_TC); }
/* 函数原型:Uart1_Init(u32 bound) 参数说明:bound:波特率参数 功能描述:设置UART1串口 返回状态: 备注: 开启串口1接收中断 ,需要改变宏定义USART1_RX_IRQ_ENABLE的值 */ void Uart1_Init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟 USART_DeInit(USART1); //串口复位,一般外设使用之前都要进行复位 //串口1对应引脚复用映射 GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1 GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1 //USART1端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10 //USART1 初始化设置 USART_InitStructure.USART_BaudRate = bound;//波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 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_Init(USART1, &USART_InitStructure); //初始化串口1 #if USART1_RX_IRQ_ENABLE //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启相关中断 #endif USART_Cmd(USART1, ENABLE); //使能串口1 USART_ClearFlag(USART1, USART_FLAG_TC); //避免第一个字节发送失败 }
/******************************************************************************* * 函数名 : USART1_Init_Config * 描述 : USART1初始化配置 * 输入 : bound:波特率(常用:2400、4800、9600、19200、38400、115200等) * 输出 : 无 * 返回 : 无 * 说明 : 无 *******************************************************************************/ void USART1_Init_Config(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /*使能USART1和GPIOA外设时钟*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /*复位串口1*/ USART_DeInit(USART1); /*USART1_GPIO初始化设置*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TXD(PA.9) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚输出最大速率为50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART1_TXD(PA.9) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1_RXD(PA.10) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART1_RXD(PA.10) /*USART1 初始化设置*/ USART_InitStructure.USART_BaudRate = bound; //设置波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1; //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_Init(USART1, &USART_InitStructure); //初始化串口1 /*Usart1 NVIC配置*/ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能串口1接收中断 USART_Cmd(USART1, ENABLE); //使能串口 USART_ClearFlag(USART1, USART_FLAG_TC); //清除发送完成标志 }
int UART_Send(u8 *sendBuf, u32 bufLen) { DMA_InitTypeDef DMA_InitStructure; mico_mcu_powersave_config(mxDisable); DMA_DeInit(UART_TX_DMA_Stream); DMA_InitStructure.DMA_PeripheralBaseAddr = USARTx_DR_Base; 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_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /****************** USART will Transmit Specific Command ******************/ /* Prepare the DMA to transfer the transaction command (2bytes) from the memory to the USART */ DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)sendBuf; DMA_InitStructure.DMA_BufferSize = (uint16_t)bufLen; DMA_Init(UART_TX_DMA_Stream, &DMA_InitStructure); DMA_ITConfig(UART_TX_DMA_Stream, DMA_IT_TC, ENABLE ); /* Enable the USART DMA requests */ /* Clear the TC bit in the SR register by writing 0 to it */ USART_ClearFlag(USARTx, USART_FLAG_TC); /* Enable the DMA TX Stream, USART will start sending the command code (2bytes) */ DMA_Cmd(UART_TX_DMA_Stream, ENABLE); if(sem_init) mico_rtos_get_semaphore(&tx_complete, 10); while( ( USARTx->SR & USART_SR_TC )== 0 ); mico_mcu_powersave_config(mxEnable); return 0; }
void USART1_IRQHandler (void) { if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET) { if (USART_ReceiveData(USART1)=='c') { queue=0; } ReceiveBuff[queue]=USART_ReceiveData(USART1); ++queue; if (USART_ReceiveData(USART1)=='d') { USART_ITConfig(USART1,USART_IT_RXNE,DISABLE); USART_FLAG=1; } } USART_ClearFlag(USART1,USART_IT_RXNE); USART_ClearITPendingBit(USART1,USART_IT_RXNE); }
/******************************************************************************* * Function Name : USART1_IRQHandler * Description : This function handles USART1 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void USART1_IRQHandler(void) { if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) //判断读寄存器是否非空 { // unsigned int i; USART_SendData(USART1,USART_ReceiveData(USART1)); } //溢出-如果发生溢出需要先读 SR,再读 DR 寄存器则可清除不断入中断的问题[牛人说要这样] if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET) { USART_ClearFlag(USART1,USART_FLAG_ORE); //读 SR 其实就是清除标志 USART_ReceiveData(USART1); //读 DR } }
static void uartResumeDma() { if (dmaIsPaused) { // Update DMA counter DMA_SetCurrDataCounter(UART_DMA_STREAM, remainingDMACount); // Update memory read address UART_DMA_STREAM->M0AR = (uint32_t)&dmaBuffer[initialDMACount - remainingDMACount]; // Enable the Transfer Complete interrupt DMA_ITConfig(UART_DMA_STREAM, DMA_IT_TC, ENABLE); /* Clear transfer complete */ USART_ClearFlag(UART_TYPE, USART_FLAG_TC); /* Enable DMA USART TX Stream */ DMA_Cmd(UART_DMA_STREAM, ENABLE); dmaIsPaused = false; } }
/******************************************************************************* * USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled *******************************************************************************/ void USART3_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; 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; /* Configure the USARTx */ USART_Init(USART3, &USART_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // USART_ITConfig(USART3, USART_IT_TXE, ENABLE); USART_ClearFlag(USART3,USART_FLAG_TC); /* Enable the USARTx */ USART_Cmd(USART3, ENABLE); }
void USART_DMA_send(USART_TypeDef* USARTx, DMA_Channel_TypeDef* DMAy_Channelx, char *buf, uint8_t len){ // DMA_InitTypeDef DMA_InitStructure; // DMA на запись DMA_DeInit(DMAy_Channelx); //DMA1_Channel4 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USARTx->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) buf; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 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_Low; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); // старт цикла отправки USART_ClearFlag(USARTx, USART_FLAG_TC | USART_FLAG_TXE); USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMAy_Channelx, ENABLE); // wait until transmission complite from dma if (DMAy_Channelx == DMA1_Channel1) { while (DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET) { } } if (DMAy_Channelx == DMA1_Channel2) { while (DMA_GetFlagStatus(DMA1_FLAG_TC2) == RESET) { } } if (DMAy_Channelx == DMA1_Channel3) { while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET) { } } if (DMAy_Channelx == DMA1_Channel4) { while (DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET) { } } DMA_Cmd(DMAy_Channelx, DISABLE); USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); }
void DMA_TX(void) { //Enable DMA Stream Tx & Enable USART DMA TX Requsts DMA_Cmd(DMA2_Stream7, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); //Waiting the end of Data transfer while (USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); while (DMA_GetFlagStatus(DMA2_Stream7, DMA_FLAG_TCIF7)==RESET); //Disable DMA Stream Tx DMA_Cmd(DMA2_Stream7, DISABLE); //Clear DMA Transfer Complete Flags DMA_ClearFlag(DMA2_Stream7, DMA_FLAG_TCIF7); //Clear USART Transfer Complete Flags USART_ClearFlag(USART1, USART_FLAG_TC); }/*-----------------------------------------End of DMA_TX functions---------------------------------*/
void USART2_Config() { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitTypeDef USART_InitStructure; //定义串口初始化结构体 RCC_AHBPeriphClockCmd(USART2_GPIO_RCC, ENABLE); RCC_APB1PeriphClockCmd(USART2_RCC, ENABLE);//使能USART的时钟 //使能串口中断,并设置优先级 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority= 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); GPIO_PinAFConfig(USART2_GPIO_PORT, USART2_TX_GPIO_PinSource, GPIO_AF_1);//配置PA2成第二功能引脚 TX GPIO_PinAFConfig(USART2_GPIO_PORT, USART2_RX_GPIO_PinSource, GPIO_AF_1);//配置PA3成第二功能引脚 RX /*USART1_TX ->PA2 USART1_RX ->PA3*/ GPIO_InitStructure.GPIO_Pin = USART2_TX|USART2_RX; //选中串口默认输出管脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //定义输出最大速率 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(USART_GPIO_PORT, &GPIO_InitStructure); //调用函数,把结构体参数输入进行初始化 /*串口通讯参数设置*/ // USART_InitStructure.USART_BaudRate = BaudRate; //波特率 USART_InitStructure.USART_BaudRate = 115200; //波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位 USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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_Init(USART2, &USART_InitStructure); USART_ClearFlag(USART2,USART_FLAG_TC); // USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // USART_ITConfig(USART2, USART_IT_TXE, ENABLE); USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); }
void UART5_Configuration(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 , ENABLE); //IO port---------------------------------------------------------------------- /* Configure UART5 RTS and UART5 Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure UART5 CTS and UART5 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); /* UART5 configuration ------------------------------------------------------*/ /* UART5 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control enabled (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_Rx | USART_Mode_Tx; USART_Init(UART5, &USART_InitStructure); /* Enable the UART5 */ USART_Cmd(UART5, ENABLE); USART_ClearFlag(UART5, USART_FLAG_TC); // clear flag USART_ClearITPendingBit(UART5, USART_IT_RXNE); }
void USART3_MODE_Config(void) { USART_InitTypeDef USART_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); USART_InitStructure.USART_BaudRate = 115200; 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_Init(USART3,&USART_InitStructure); USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); USART_Cmd(USART3,ENABLE); USART_ClearFlag(USART3, USART_FLAG_TC); }
void USB_LP_CAN1_RX0_IRQHandler(void) { if(CAN_GetITStatus(CAN1, CAN_IT_FMP0)) { CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0); CAN_Receive(CAN1, 0, &CanRxMsgStructure); for(countCAN = 0; countCAN < 8; countCAN++) { USART_SendData(USART2, CanRxMsgStructure.Data[countCAN]); while(!USART_GetFlagStatus(USART2, USART_FLAG_TC)); USART_ClearFlag(USART2, USART_FLAG_TC); } autoSendFlag++; startFlag = 1; } }
/******************************************************************************* * Function Name : USART_Scanf_Name * Description : Gets Char values from the hyperterminal. * Input : None * Output : None * Return : Length *******************************************************************************/ int USART_Scanf_Name(char * str) { u16 index = 0; USART_ClearFlag(USART1,USART_FLAG_RXNE);//清一下接收标志位 while(1) { /* Loop until RXNE = 1 */ while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){}//等待直到有数据的到来 str[index++] = (USART_ReceiveData(USART1)); printf("%c",str[index - 1]); //返回输入的内容 if(str[index - 1] == 0x0d) //回车键\r { index--; //去掉回车符并以\0结尾 str[index] ='\0'; //结束符号 return index; } } }
void USART1_IRQHandler(void) { static u8 i=0; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) /*Receive Data register not empty interrupt*/ { rece_string[i++]=USART1->DR; if((rece_string[i-1]=='!')&&(rece_string[0]=='#')) /*'!'为字符串结束标志位*/ { i=0; /*勿忘清零*/ rece_flag=1; USART_ClearFlag (USART1,USART_IT_RXNE); /*clear Receive data register not empty flag*/ } } }
void USART1_Init(void){ //Create GPIO_InitStruct structure for all GPIO Config. usage. GPIO_InitTypeDef GPIO_InitStruct; //USART_InitStructure for USART Init config. USART_InitTypeDef USART_InitStructure; //Enable clock source for GPIOA, USART1 on APB2 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE); //GPIO Config. for PA9(USART1 TX) to AF PP. GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP; //GPIO Init for PA9 Config. GPIO_Init(GPIOA,&GPIO_InitStruct); //GPIO Config. for PA10(USART1 RX) to Input Floating. GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10; GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING; //GPIO Init for PA10 Config. GPIO_Init(GPIOA,&GPIO_InitStruct); //Config USART parameters. USART_InitStructure.USART_BaudRate = 115200; //Fpclk2=72MHz will be more accurate. 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; //Init USART1 Peripheral. USART_Init(USART1,&USART_InitStructure); //Enable USART1 Peripheral. USART_Cmd(USART1,ENABLE); //Clear TC after Init(Or the fist data could be lost). Seems to be TC not RESET to 0 after Enabled. Gotta check. USART_ClearFlag(USART1,USART_FLAG_TC); }
/******************************************************************************************************************************* * 函数名 :DC_USART_Conf * 参数 :void * 返回值 :void * 功能 :配置USART6,并初始化 *******************************************************************************************************************************/ void DC_USART_Conf(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //开启USART6时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //开启GPIOC时钟 GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);// GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);// /*配置GPIOC*/ /*配置GPIOC_Pin7为TX输入*/ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); /*配置GPIOC_Pin6为RX输出*/ GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); /*配置USART6*/ USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate =115200; 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_Init(USART6, &USART_InitStructure); USART_ClockStructInit(&USART_ClockInitStruct); USART_ClockInit(USART6, &USART_ClockInitStruct); // USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); //使能 USART6中断 USART_Cmd(USART6, ENABLE); //使能 USART6 USART_ClearFlag(USART6, USART_FLAG_TC); /* 清发送完成标志,Transmission Complete flag */ }
void Uart5_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);//外设时钟使能 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//外设时钟使能 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);//连接复用引脚 GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化串口1的GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化串口1的GPIO USART_InitStructure.USART_BaudRate = 115200;//波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据模式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//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_ITConfig(UART5,USART_IT_RXNE,ENABLE); USART_Init(UART5, &USART_InitStructure); USART_ClearFlag(UART5, USART_FLAG_TC);//清传送完成标志 USART_Cmd(UART5, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //嵌套优先级分组为1 NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;//嵌套通道为USART6_IRQn NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级为0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能 NVIC_Init(&NVIC_InitStructure); }
void USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;// //GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_OType= GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_NOPULL; // GPIO_Init(GPIOG, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); /* USART1 mode config */ USART_InitStructure.USART_BaudRate = 115200; 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_Init(USART1, &USART_InitStructure); USART_ClearFlag(USART1,USART_IT_RXNE); USART_ClearITPendingBit(USART1,USART_IT_RXNE); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_Cmd(USART1, ENABLE); }
/*====================================================================================================*/ void Serial_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; // NVIC_InitTypeDef NVIC_InitStruct; USART_InitTypeDef USART_InitStruct; /* USART Clk Init *************************************************************/ USARTx_CLK(); USARTx_TX_GPIO_CLK(); USARTx_RX_GPIO_CLK(); /* USART GPIO Config **********************************************************/ GPIO_InitStruct.GPIO_Pin = USARTx_TX_PIN; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = USARTx_RX_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct); /* UART IT ********************************************************************/ // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // NVIC_InitStruct.NVIC_IRQChannel = USARTx_IRQn; // NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; // NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; // NVIC_Init(&NVIC_InitStruct); /* UART Init ******************************************************************/ USART_InitStruct.USART_BaudRate = USARTx_BAUDRATE; USART_InitStruct.USART_WordLength = USARTx_BYTESIZE; USART_InitStruct.USART_StopBits = USARTx_STOPBITS; USART_InitStruct.USART_Parity = USARTx_PARITY; USART_InitStruct.USART_HardwareFlowControl = USARTx_HARDWARECTRL; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USARTx, &USART_InitStruct); /* UART Enable ****************************************************************/ // USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); USART_Cmd(USARTx, ENABLE); USART_ClearFlag(USARTx, USART_FLAG_TC); }
//DRV UART void UART_DRV_IRQ(){ static int32_t datapos = -1; static data_t data; USART_ClearITPendingBit(UART_DRV, USART_IT_RXNE); USART_ClearFlag(UART_DRV, USART_FLAG_RXNE); rxbuf = UART_DRV->DR; if(rxbuf == 0x154){//start condition datapos = 0; }else if(datapos >= 0 && datapos < DATALENGTH*2){ data.byte[datapos++] = (uint8_t)rxbuf;//append data to buffer } if(datapos == DATALENGTH*2){//all data received datapos = -1; PIN(g_amp) = (data.data[0] * AREF / ARES - AREF / (R10 + R11) * R11) / (RCUR * R10) * (R10 + R11); PIN(g_vlt) = data.data[1] / ARES * AREF / VDIVDOWN * (VDIVUP + VDIVDOWN); if(data.data[2] < ARES && data.data[2] > 0.0) PIN(g_tmp) = log10f(data.data[2] * AREF / ARES * TPULLUP / (AREF - data.data[2] * AREF / ARES)) * (-53) + 290; } }