void _USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { /* received data */ USART_GetInputString(); } /* If overrun condition occurs, clear the ORE flag and recover communication */ if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET) { (void)USART_ReceiveData(USART1); } if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { /* Write one byte to the transmit data register */ USART_SendBufferData(); } }
void serialout(USART_TypeDef* uart, unsigned int intr) { int fd; char c; int doread = 1; mkfifo("/dev/tty0/out", 0); fd = open("/dev/tty0/out", 0); while (1) { if (doread) read(fd, &c, 1); doread = 0; if (USART_GetFlagStatus(uart, USART_FLAG_TXE) == SET) { USART_SendData(uart, c); USART_ITConfig(USART2, USART_IT_TXE, ENABLE); doread = 1; } interrupt_wait(intr); USART_ITConfig(USART2, USART_IT_TXE, DISABLE); } }
void EVAL_COM2_IRQHandler(void) { extern int bypass_mode; if (USART_GetITStatus(EVAL_COM2, USART_IT_RXNE) != RESET) { if(bypass_mode) USART_To_USB_Send_Data(); else usart_2_rx_buffer(USART_ReceiveData(EVAL_COM2)); } /* If overrun condition occurs, clear the ORE flag and recover communication */ if (USART_GetFlagStatus(EVAL_COM2, USART_FLAG_ORE) != RESET) { (void)USART_ReceiveData(EVAL_COM2); } }
size_t USARTSerial::write(uint8_t c) { // interrupts are off and data in queue; if ((USART_GetITStatus(usartMap->usart_peripheral, USART_IT_TXE) == RESET) && _tx_buffer.head != _tx_buffer.tail) { // Get him busy USART_ITConfig(usartMap->usart_peripheral, USART_IT_TXE, ENABLE); } unsigned i = (_tx_buffer.head + 1) % SERIAL_BUFFER_SIZE; // If the output buffer is full, there's nothing for it other than to // wait for the interrupt handler to empty it a bit // no space so or Called Off Panic with interrupt off get the message out! // make space Enter Polled IO mode while (i == _tx_buffer.tail || ((__get_PRIMASK() & 1) && _tx_buffer.head != _tx_buffer.tail) ) { // Interrupts are on but they are not being serviced because this was called from a higher // Priority interrupt if (USART_GetITStatus(usartMap->usart_peripheral, USART_IT_TXE) && USART_GetFlagStatus(usartMap->usart_peripheral, USART_FLAG_TXE)) { // protect for good measure USART_ITConfig(usartMap->usart_peripheral, USART_IT_TXE, DISABLE); // Write out a byte USART_SendData(usartMap->usart_peripheral, _tx_buffer.buffer[_tx_buffer.tail++]); _tx_buffer.tail %= SERIAL_BUFFER_SIZE; // unprotect USART_ITConfig(usartMap->usart_peripheral, USART_IT_TXE, ENABLE); } } _tx_buffer.buffer[_tx_buffer.head] = c; _tx_buffer.head = i; transmitting = true; USART_ITConfig(usartMap->usart_peripheral, USART_IT_TXE, ENABLE); return 1; }
/** * @brief This function handles USART1 interrupt request. * @param None * @retval None * @global */ void Uart1_IRQHandler(void) { unsigned char RxByte = 0x00; if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET){ // 溢出 // 注意!不能使用if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)来判断 // http://bbs.21ic.com/icview-160999-1-1.html USART_ReceiveData(USART1); } // 接收中断 (接收寄存器非空) if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){ // Clear the USART1 Receive interrupt USART_ClearITPendingBit(USART1, USART_IT_RXNE); // unsigned char com_data = USART1->DR; // Read one byte from the receive data register RxByte = USART_ReceiveData(USART1); if((cmdStart == FALSE) && (RxByte == BYTE_START)){ cmdStart = TRUE; RxCounter = 0; cmdLen = RX_BUFFER_SIZE; } else if(cmdStart == TRUE){ RxBuf[RxCounter++] = RxByte; if(RxCounter == 2){ cmdLen = RxByte + 4; // cmd code + parameter number + Xor byte + end byte } else if(RxCounter == cmdLen){ RxComplete = TRUE; } else if(RxCounter >= 28){ // 目前最多不超过28(写块)个字节 RxCounter = 28; } } } }
void poll_serial() { uint8_t c; if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) {return;} c=USART_ReceiveData(USART2); if(state<3) { *dlc=0; } if(state<0) { if(c==0xFF) { state=0; } } else if(state>=0 && state<*dlc+3) { can_frame[state]=c; state++; } else if(state>=*dlc+3) { if(c==0xFF) { canout(); serial_putstr("OK\r",3); state=0; } else { state=-1; } } }
unsigned char _Device_Uart_Send_Bytes(unsigned char *sendByte, unsigned int length){ unsigned int i; //if(Usart_Peripheral_Enable_Flag == DeviceOff){ if((Usart_Peripheral_Flag & fUart_Enable) == 0){ return Func_Fail; } #if (_Set_UART_AS_Half_Duplex_Transmission_ == 1) _Device_Set_Uart_RX_Interrupt(DeviceOff); #endif //GPIO_WriteHigh(LED2_PORT, LED2_PIN); //set TX status // GPIO_WriteHigh(URAT_TX_Setting_PORT, URAT_TX_Setting_PIN); // GPIO_WriteHigh(URAT_TX_Setting_PORT, URAT_TX_Setting_PIN); //for delay for(i = 0; i < length; i++){ USART_SendData8(USART1, (*(sendByte + i))); SendingWhileTimeOutCount = 0; //while (USART_GetFlagStatus(USART1, UART2_FLAG_TXE) == RESET){ //Transmit Data Register Empty flag while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){ //Transmission Complete flag if(SendingWhileTimeOutCount >= SendingTimeOutCycle){ break; } SendingWhileTimeOutCount++; }//while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) } //disable TX status // GPIO_WriteLow(URAT_TX_Setting_PORT, URAT_TX_Setting_PIN); // GPIO_WriteLow(URAT_TX_Setting_PORT, URAT_TX_Setting_PIN); //for delay //GPIO_WriteLow(LED2_PORT, LED2_PIN); #if (_Set_UART_AS_Half_Duplex_Transmission_ == 1) _Device_Set_Uart_RX_Interrupt(DeviceOn); #endif return Func_Complete; }
void Com_USART2_Init() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitTypeStructure; NVIC_InitTypeStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitTypeStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitTypeStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitTypeStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitTypeStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_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(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_TXE, ENABLE); USART_Cmd(USART2, ENABLE); USART_SendData(USART2, 0); //dummpy send while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); }
//----------------------------------------------------------------------------- // осуществляет сброс и проверку на наличие устройств на шине //----------------------------------------------------------------------------- uint8_t OW_Reset() { uint8_t ow_presence; USART_InitTypeDef USART_InitStructure; 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(OW_USART, &USART_InitStructure); // отправляем 0xf0 на скорости 9600 USART_ClearFlag(OW_USART, USART_FLAG_TC); USART_SendData(OW_USART, 0xf0); while (USART_GetFlagStatus(OW_USART, USART_FLAG_TC) == RESET) { #ifdef OW_GIVE_TICK_RTOS taskYIELD(); IWDG_ReloadCounter(); #endif } ow_presence = USART_ReceiveData(OW_USART); 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_Tx | USART_Mode_Rx; USART_Init(OW_USART, &USART_InitStructure); if (ow_presence != 0xf0) { return OW_OK; } return OW_NO_DEVICE; }
/************************************************************************************//** ** \brief Callback that gets called each time new log information becomes ** available during a firmware update. ** \param info_string Pointer to a character array with the log entry info. ** \return none. ** ****************************************************************************************/ void FileFirmwareUpdateLogHook(blt_char *info_string) { /* write the string to the log file */ if (logfile.canUse == BLT_TRUE) { if (f_puts(info_string, &logfile.handle) < 0) { logfile.canUse = BLT_FALSE; f_close(&logfile.handle); } } /* echo all characters in the string on UART */ while(*info_string != '\0') { /* write character to transmit holding register */ USART_SendData(USART6, *info_string); /* wait for tx holding register to be empty */ while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); /* point to the next character in the string */ info_string++; } } /*** end of FileFirmwareUpdateLogHook ***/
uint32_t USART_SendBlock(USART_TypeDef *USARTPort, uint8_t txbuf, uint8_t buflen) { uint8_t bToSend, bSent, timeOut; bToSend = buflen; bSent = 0; while (bToSend) { /* if(!(USARTPort->SR & USART_FLAG_TXE)) { break; }*/ while(USART_GetFlagStatus(USARTPort, USART_FLAG_TXE)) { USART_SendData(USARTPort, txbuf++); bToSend--; bSent++; } } return bSent; }
/** * @brief Main program * @param None * @retval None */ int main(void) { uint16_t v; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ USART_Configuration(); for(v=0;v<257;v++) data_buf[v]=v; /*data_buf[0]= 0x04; data_buf[1]= 0x06; data_buf[2]=0x02; data_buf[3]=0x32; data_buf[4]= 0x06; data_buf[5]= 0x06; data_buf[6]= 0x06; data_buf[7]= 0x06; */ config_frame(0, 0, 0,3);//configurin frame preamble,head,tail and dlay send_frame( data_buf,25); //adding info to frame delay(); for(v=0;v<=tx_buffer.index;v++) { USART_SendData(USART2, tx_buffer.frame_buffer[v]); while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); if((v%11==0)&&(v!=0)) delay(); } }
int Test_fgetc(void) { uint8_t ch = 0; while (1) { if(ESC(escape)==1) break; } while(DBGU_RxBufferHead == DBGU_RxBufferTail); ch = DBGU_RxBuffer[DBGU_RxBufferTail]; DBGU_RxBufferTail = (DBGU_RxBufferTail + 1) % DBGU_RX_BUFFER_SIZE; if (DBGU_RxBufferHead == DBGU_RxBufferTail) { DBGU_InputReady = 0; escape = 0; } while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); return ch; }int ESC(int escape)
void Usart_Configuration(void) //配置Usart1 Tx->PA9 Rx->PA10 { GPIO_InitTypeDef GPIO_InitStructure; //GPIO库函数结构体 USART_InitTypeDef USART_InitStructure;//USART库函数结构体 USART_ClockInitTypeDef USART_ClockInitStructure; //使能串口1,GPIOA,AFIO总线 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE); /* Configure USART1 Tx (PA9) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//PA9时钟速度50MHz GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用输出 GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate =115200; //波特率9600 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_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;//空闲时钟为低电平 USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//时钟第二个边沿进行数据捕获 USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据的时钟脉冲不从SCLK输出 //使能串口1接收中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ClockInit(USART1, &USART_ClockInitStructure); USART_Init(USART1, &USART_InitStructure); //初始化结构体 USART_Cmd(USART1, ENABLE); //使能串口1 // USART_ClearFlag(USART1,USART_FLAG_TC);//清除发送完成标志,不然有时可能会有第一个字符发送不出去的情况 USART_GetFlagStatus(USART1, USART_FLAG_TC);//清除发送完成标志,不然有时可能会有第一个字符发送不出去的情况 }
/////////////////// ///串口发送数据 ///@param pbuffer 需要发送数据的首地址 ///@param size 需要发送的数据的长度 ///@retval 发送是否成功 -false:发送失败(队列溢出) -true:发送成功(即将发送) /////////////////// bool USART::SendData(uint8_t *pbuffer, uint32_t size) { //将需要发送的数据放入缓冲区队列 bufferTx.Puts(pbuffer,size); if(mUseDma && !isBusySend)//使用DMA并且没有忙于发送,则开启发送 { //断发送队列里是否还存在数据,如果存在,继续发送,否则关闭DMA if(bufferTx.Size()>0) { isBusySend=true; //标记忙于发送 if(bufferTx.Size()<=USART_DMA_TX_BUFFER_SIZE) //队列中的数据长度小于dma缓冲区的最大长度 { dmaChannelTx->CNDTR = (uint32_t)bufferTx.Size(); bufferTx.Gets(bufferTxDma,bufferTx.Size());//将剩下的数据放到dma缓冲区 } else { dmaChannelTx->CNDTR = USART_DMA_TX_BUFFER_SIZE; bufferTx.Gets(bufferTxDma,USART_DMA_TX_BUFFER_SIZE);//将数据放到dma缓冲区 } DMA_Cmd (dmaChannelTx,ENABLE); //使能DMA,开始发送 } } if(!mUseDma && !isBusySend)//使用中断发送 { if(bufferTx.Size()>0)//数据区确实存在数据 { USART_ClearITPendingBit(usart, USART_IT_TC); //清标记,防止第一个数据发送不出去的情况,一定是要先清再打开 USART_ITConfig(usart, USART_IT_TC, ENABLE);//开启发送中断,然后发送数据 USART_GetFlagStatus(usart, USART_FLAG_TC);//读取sr,清除发送完成标志,不然有时可能会有第一个字符发送不出去的情况 isBusySend=true; static u8 dataToSend=0; bufferTx.Get(dataToSend); USART_SendData(usart,dataToSend); } } return 1; }
void PowerWatchProc(void *pdata) { u32 nVoltage; char pStr[32], *p; pdata = pdata; for(;;) { nVoltage = GetADCValue(ADC_Channel_11); sprintf(pStr, "ADC_Value:%d\r\n", nVoltage); for(p = pStr; *p; p++) { USART_SendData(USART1, (uint8_t)*p); while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}; } if(nVoltage < POWER_UV_LIMIT) { //输入电压过低,应进行保护处理 } OSTimeDly(100); } }
extern int _write( int file, char *ptr, int len ) { int iIndex ; // for ( ; *ptr != 0 ; ptr++ ) for ( iIndex=0 ; iIndex < len ; iIndex++, ptr++ ) { // UART_PutChar( *ptr ) ; USART_SendData(USART1,*ptr); // Check if the transmitter is ready //while ((UART->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY) while (USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET) ; // Send character //UART->UART_THR = *ptr; } return iIndex ; }
void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_TC) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_TC); if(USART_GetFlagStatus(USART1, USART_FLAG_TXE)!=RESET) { GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET); USART_ITConfig(USART1, USART_IT_TC, DISABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } } if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { canal_rx_buf[canal_rx_cnt]=USART_ReceiveData(USART1); canal_rx_cnt++; if(canal_rx_cnt>=BUF_SIZE) canal_rx_cnt=0; USART_ClearITPendingBit(USART1, USART_IT_RXNE); TIM2->CNT=0; } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* 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 */ 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; STM_EVAL_COMInit(COM1, &USART_InitStructure); /* Output a message on Hyperterminal using printf function */ printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r"); /* Loop until the end of transmission */ /* The software must wait until TC=1. The TC flag remains cleared during all data transfers and it is set by hardware at the last frame’s end of transmission*/ while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET) {} while (1) { } }
void serialin(void* arg) { int fd; char c; mkfifo("/dev/tty0/in", 0); fd = open("/dev/tty0/in", 0); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); while (1) { // interrupt_wait(intr); interrupt_wait(USART2_IRQn); /* if (USART_GetFlagStatus(uart, USART_FLAG_RXNE) == SET) { c = USART_ReceiveData(uart); */ if (USART_GetFlagStatus((USART_TypeDef *)USART2, USART_FLAG_RXNE) == SET) { c = USART_ReceiveData((USART_TypeDef *)USART2); write(fd, &c, 1); } } }
/** * @brief This function handles USART3 global interrupt request. * @param None * @retval None */ void USART3_IRQHandler(void) { /* If a Frame error is signaled by the card */ if(USART_GetITStatus(USART3, USART_IT_FE) != RESET) { /* Clear the USART3 Frame error pending bit */ USART_ClearITPendingBit(USART3, USART_IT_FE); USART_ReceiveData(USART3); /* Resend the byte that failed to be received (by the Smartcard) correctly */ SC_ParityErrorHandler(); } /* If the USART3 detects a parity error */ if(USART_GetITStatus(USART3, USART_IT_PE) != RESET) { while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET) { } /* Clear the USART3 Parity error pending bit */ USART_ClearITPendingBit(USART3, USART_IT_PE); USART_ReceiveData(USART3); } /* If a Overrun error is signaled by the card */ if(USART_GetITStatus(USART3, USART_IT_ORE) != RESET) { /* Clear the USART3 Frame error pending bit */ USART_ClearITPendingBit(USART3, USART_IT_ORE); USART_ReceiveData(USART3); } /* If a Noise error is signaled by the card */ if(USART_GetITStatus(USART3, USART_IT_NE) != RESET) { /* Clear the USART3 Frame error pending bit */ USART_ClearITPendingBit(USART3, USART_IT_NE); USART_ReceiveData(USART3); } }
uint8_t Iap_wait(void) { uint8_t temp = 0; static uint8_t flag = 0; while (USART_GetFlagStatus(UART7, USART_IT_RXNE) != RESET) { temp = (uint8_t)USART_ReceiveData(UART7); switch(flag) { case 0: if(temp == ':')flag = 1;break; case 1: case 2: case 3: case 4: case 5: case 6: if(temp != ';')flag++; break; case 7: if(temp == ';')flag = 8;break; default:break; } if(flag == 8)break; } return flag; }
//Task For Sending Data Via USART void vUsartSendTask(void *pvParameters) { //Variable to store received data uint32_t Data; uint8_t curr_char; while(1) { serial_str_msg msg; while (!xQueueReceive(xQueueUARTSend , &msg, portMAX_DELAY)); /* Write each character of the message to the RS232 port. */ curr_char = 0; while (msg.str[curr_char] != '\0') { //Wait till the flag resets while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); //Send the data USART_SendData(USART2, msg.str[curr_char]); // Send Char from queue curr_char++; } } while(1); }
void USART1_IRQHandler(void) { u8 res; //存储接收到的数据 u8 err; //串口硬件错误出发的中断 if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET) //接收中断 { if(USART_GetFlagStatus(USART1,USART_FLAG_NE|USART_FLAG_FE|USART_FLAG_PE)) err=1;//检查到噪音、帧错误或校验错误 else err=0; res=USART_ReceiveData(USART1); //读接收到的字节,同时相关标志自动清除 if((RS485_RX_CNT<2047)&&(err==0)) //接收到的字节数量,在合理的范围内,同时无其他错误 { RS485_RX_BUFF[RS485_RX_CNT]=res; //获取数据 RS485_RX_CNT++; //计数 TIM_ClearITPendingBit(TIM1,TIM_IT_Update); //清除定时器溢出中断 TIM_SetCounter(TIM1,0); //当接收到一个新的字节时,将定时器1复位为0,重新计时(相当于watchdog) TIM_Cmd(TIM1,ENABLE); //使能定时器1,开始计时 } } }
void vMBMasterPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable) { if (xRxEnable) { /* 485通信时,等待串口移位寄存器中的数据发送完成后,再去使能485的接收、失能485的发送*/ while (!USART_GetFlagStatus(USART2,USART_FLAG_TC)); MASTER_RS485_RECEIVE_MODE; USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); } else { MASTER_RS485_SEND_MODE; USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); } if (xTxEnable) { USART_ITConfig(USART2, USART_IT_TXE, ENABLE); } else { USART_ITConfig(USART2, USART_IT_TXE, DISABLE); } }
void UART4_IRQHandler(void) { if(USART_GetITStatus(UART4, USART_IT_TC) != RESET) { USART_ClearITPendingBit(UART4, USART_IT_TC); if(USART_GetFlagStatus(UART4, USART_FLAG_TXE)!=RESET) { GPIO_WriteBit(GPIOD, GPIO_Pin_7, Bit_RESET); USART_ITConfig(UART4, USART_IT_TC, DISABLE); USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); } } if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) { canal2_rx_buf[canal2_rx_cnt]=USART_ReceiveData(UART4); if(canal2_rx_buf[canal2_rx_cnt]=='$') ascii_type2=_RELKON; if(canal2_rx_buf[canal2_rx_cnt]==':') {ascii_type2=_MODBUS;} canal2_rx_cnt++; if(canal2_rx_cnt>=BUF_SIZE) canal2_rx_cnt=0; USART_ClearITPendingBit(UART4, USART_IT_RXNE); TIM5->CNT=0; } }
static int uart_putchar(int data) { char c = (char) data; #ifdef ENABLE_TX_DMA int i, j, n; //wait ... no enough fifo space to use do { n = DMA_GetCurrDataCounter(dma_ch_tx); } while(TX_FIFO_SZ - n < 1); //copy DMA_Cmd(dma_ch_tx, DISABLE); n = DMA_GetCurrDataCounter(dma_ch_tx); uart_fifo_tp += uart_fifo_tn - n; if(uart_fifo_tp + n + 2 > TX_FIFO_SZ) { for(i = 0, j = uart_fifo_tp; i < n; i ++, j ++) { if(i != j) uart_fifo_tx[i] = uart_fifo_tx[j]; } uart_fifo_tp = 0; } else { i = uart_fifo_tp + n; } uart_fifo_tx[i ++] = c; n ++; uart_fifo_tn = n; uart_SetupTxDMA(uart_fifo_tx + uart_fifo_tp, n); #else USART_SendData(uart, c); while(USART_GetFlagStatus(uart, USART_FLAG_TXE) == RESET); #endif return 0; }
void sendTxDMA(uint32_t adress,uint32_t size){ DMA_InitStructure.DMA_Channel = USARTx_TX_DMA_CHANNEL ; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ; DMA_InitStructure.DMA_Memory0BaseAddr = adress; DMA_InitStructure.DMA_BufferSize = size; DMA_Init(USARTx_TX_DMA_STREAM,&DMA_InitStructure); /* Enable DMA USART TX Stream */ DMA_Cmd(USARTx_TX_DMA_STREAM,ENABLE); /* Enable USART DMA TX Requests */ USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); /* Waiting the end of Data transfer */ while (USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET); while (DMA_GetFlagStatus(USARTx_TX_DMA_STREAM,USARTx_TX_DMA_FLAG_TCIF)==RESET); /* Clear DMA Transfer Complete Flags */ DMA_ClearFlag(USARTx_TX_DMA_STREAM,USARTx_TX_DMA_FLAG_TCIF); DMA_Cmd(USARTx_TX_DMA_STREAM, DISABLE); /* Clear USART Transfer Complete Flags */ USART_ClearFlag(USARTx,USART_FLAG_TC); USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); }
////////////////////////////////////////// //tyh:用于和 AI采样芯片通信 void COM3_Configuration(uint32_t BaudRate) { USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = BaudRate; 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); DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE); //设置发送完成中断 //USART_ITConfig(USART3, /*USART_IT_ORE*/USART_IT_RXNE, ENABLE); //设置ORE中断 0x0360 USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE); //启用DMA传输 USART_DMACmd(USART3, USART_DMAReq_Rx , ENABLE); USART_Cmd(USART3, ENABLE); //如下语句解决第1个字节无法正确发送出去的问题 USART_GetFlagStatus(USART3, USART_FLAG_TC); // 清标志 }
signed portBASE_TYPE xSerialPutChar( signed portCHAR cOutChar, portTickType xBlockTime ) { signed portBASE_TYPE xReturn; #ifdef USE_INT if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS ) { xReturn = pdPASS; //USART_ITConfig( USART_PORT, USART_IT_TXE, ENABLE ); } else { xReturn = pdFAIL; } #else USART_CON_TOSEND; USART_SendData(USART_PORT, (u8)cOutChar); while(USART_GetFlagStatus(USART_PORT, USART_IT_TXE) == RESET) {} GPIO_ResetBits(USART_GPIO, USART_RTS_PIN); xReturn = pdPASS; #endif return xReturn; }