void usart3_irq_handler(void) { /* input (RX) handler */ if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){ data_buf = USART_ReceiveData(USART3); if(!gpc_handle_byte(data_buf)){ LED_GREEN_TOGGLE(); }else{ LED_RED_ON(); } } /* output (TX) handler */ if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET){ if((data_buf = gpc_pickup_byte()) >= 0){ USART_SendData(USART3, data_buf); LED_GREEN_TOGGLE(); }else{ usart_disable_send(); } } }
int main(void) { RCC_Configuration(); GPIO_Configuration(); USART1_Configuration(); LED_Initialization(); USART1_puts("Hello World!\r\n"); USART1_puts("Just for STM32F429I Discovery verify USART1 with USB TTL Cable\r\n"); while(1) { LED3_Toggle(); if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) { char t = USART_ReceiveData(USART1); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, t); } } while(1); // Don't want to exit }
static inline void usart_irq_handler(struct uart_periph* p) { if(USART_GetITStatus(p->reg_addr, USART_IT_TXE) != RESET){ // check if more data to send if (p->tx_insert_idx != p->tx_extract_idx) { USART_SendData(p->reg_addr,p->tx_buf[p->tx_extract_idx]); p->tx_extract_idx++; p->tx_extract_idx %= UART_TX_BUFFER_SIZE; } else { p->tx_running = FALSE; // clear running flag USART_ITConfig(p->reg_addr, USART_IT_TXE, DISABLE); } } if(USART_GetITStatus(p->reg_addr, USART_IT_RXNE) != RESET){ uint16_t temp = (p->rx_insert_idx + 1) % UART_RX_BUFFER_SIZE;; p->rx_buf[p->rx_insert_idx] = USART_ReceiveData(p->reg_addr); // check for more room in queue if (temp != p->rx_extract_idx) p->rx_insert_idx = temp; // update insert index } }
void USART1_IRQHandler(void) { if(SET == USART_GetITStatus(USART1, USART_IT_TXE)) { USART_ClearITPendingBit(USART1, USART_IT_TXE); if (m_SendBuff.len > 0 ) { USART_SendData(USART1, m_SendBuff.buf[m_SendBuff.ind++]); m_SendBuff.len--; } else { USART_ITConfig(USART1, USART_IT_TXE, DISABLE); USART_ITConfig(USART1, USART_IT_TC, ENABLE); } } else if (USART_GetITStatus(USART1, USART_IT_TC) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_TC); USART_ITConfig(USART1, USART_IT_TC, DISABLE); m_SendBuff.len = 0; eTXIdle = TRUE; } else if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { u8 ch; USART_ClearITPendingBit(USART1, USART_IT_RXNE); ch = USART_ReceiveData(USART1); ReceOneChar(ch); //IAP // if (receChar != NULL) // (*receChar)(ch); } }
//------------------------------------------------------ void USART1_IRQHandler(void) { uint8_t i; if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { USART_SendData(USART1, UartBuf_RD(&UartTxbuf)); //环形数据缓存发送 if(UartBuf_Cnt(&UartTxbuf)==0) USART_ITConfig(USART1, USART_IT_TXE, DISABLE);//假如缓冲空了,就关闭串口发送中断 } else if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清除接收中断标志 //此种环形缓冲数组串口接收方式,适用于解包各种数据,很方便。对数据的要求是: //发送方必须要求有数据包头,以便解决串口数据无地址的问题 Udatatmp = (uint8_t) USART_ReceiveData(USART1); //临时数据赋值 UartBuf_WD(&UartRxbuf,Udatatmp); //写串口接收缓冲数组 // if(UartBuf_Cnt(&UartRxbuf)==0) USART_SendData(USART1, '');//串口接收数组长度等于0时,发送接收数组空标志 // if(UartBuf_Cnt(&UartRxbuf)==UartRxbuf.Mask) USART_SendData(USART1, '');//串口接收数组长度等于掩码时,发送接收缓冲满标志 //#if(BT_SRC==APP) #ifdef BT_SRC_APP CommApp(Udatatmp);//UartBuf_RD(&UartRxbuf)); #endif #ifdef BT_SRC_PC CommPC(Udatatmp); #endif } }
void UART5_IRQHandler( void ) { uint8_t ch; rt_interrupt_enter( ); if( USART_GetITStatus( UART5, USART_IT_RXNE ) != RESET ) { ch = USART_ReceiveData( UART5 ); if( ( ch == 0x0a ) && ( last_ch == 0x0d ) ) /*遇到0d 0a 表明结束*/ { uart5_rxbuf.body[uart5_rxbuf.wr++] = ch; if( uart5_rxbuf.wr < 124 ) { rt_mq_send( &mq_gps, (void*)&uart5_rxbuf, uart5_rxbuf.wr + 2 ); } uart5_rxbuf.wr = 0; }else { // 1. get head char if(ch=='$') uart5_rxbuf.wr = 0; // 2. judge head char if(uart5_rxbuf.body[0]!='$') // add later uart5_rxbuf.wr = 0; // 3. rx data uart5_rxbuf.body[uart5_rxbuf.wr++] = ch; if( uart5_rxbuf.wr == UART5_RX_SIZE ) { uart5_rxbuf.wr = 0; } uart5_rxbuf.body[uart5_rxbuf.wr] = 0; } last_ch = ch; USART_ClearITPendingBit( UART5, USART_IT_RXNE ); } rt_interrupt_leave( ); }
void vUARTInterruptHandler( void ) { portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; char cChar; if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET ) { /* The interrupt was caused by the THR becoming empty. Are there any more characters to transmit? */ if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE ) { /* A character was retrieved from the queue so can be sent to the THR now. */ USART_SendData( USART1, cChar ); } else { USART_ITConfig( USART1, USART_IT_TXE, DISABLE ); } } if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET ) { cChar = USART_ReceiveData( USART1 ); xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ); } portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); }
/** * @brief UsartReceive 串口接收中断服务函数,用来控制音量 * @param 无 * @retval 无 */ void UsartReceive(void) { //char text[40]; vu16 i=0; if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET) { USART_ClearFlag(USART1, USART_FLAG_RXNE | USART_FLAG_ORE); //读数据并清 RXNE、ORE位 i = USART_ReceiveData(USART1); printf("%c",i); if(i == '+') { Volume_Add(); //printf("+"); }else if(i == '-') { Volume_Dec(); //printf("-"); } } }
void USART1_IRQHandler(void) { static u16 receiveCounter = 0; static u8 temp,flagStart = 0; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { USART_ClearITPendingBit(USART1,USART_IT_RXNE); temp = USART_ReceiveData(USART1); if(flagStart == 1) { receiveBuffer[receiveCounter] = temp; receiveCounter ++; if(receiveCounter == receiveBuffer[3]) { flagStart = 0; receiveCounter = 0; uartDecode(); } /* 最长的报文长度为12 */ else if (receiveCounter > 12) { receiveCounter = 0; flagStart = 0; } } else if(temp == 0x7f) { flagStart = 1; receiveBuffer[receiveCounter] = temp; receiveCounter ++; } } }
void USART2_IRQHandler(void) { uint16_t SR = USART2->SR; //if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET) { if (SR & USART_IT_RXNE) { if (uart2Callback) uart2Callback(USART_ReceiveData(USART2)); } //while(USART_GetITStatus(USART2, USART_IT_TXE) == RESET); //if (USART_GetITStatus(USART2, USART_IT_TXE) == SET) { if (SR & USART_FLAG_TXE) { if (tx2BufferTail != tx2BufferHead) { USART2->DR = tx2Buffer[tx2BufferTail]; tx2BufferTail = (tx2BufferTail + 1) % UART2_BUFFER_SIZE; } else { USART_ITConfig(USART2, USART_IT_TXE, DISABLE); } } /*if (USART_GetITStatus(USART2, USART_IT_TC) != RESET) { // ќчищаем флаг прерывани¤ USART_ClearITPendingBit(USART2, USART_IT_TC); // ќтправл¤ем байт данных USART_SendData(USART2, tx2Buffer[tx2BufferTail]); // ”величиваем счетчик отправленных байт tx2BufferTail = (tx2BufferTail + 1) % UART2_BUFFER_SIZE; }*/ }
void USART2_IRQHandler(void) { unsigned char RxData; // uint32_t idx = 1; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { /* Read one byte from the receive data register */ RxData = USART_ReceiveData(USART2); usart_irq_my(1, RxData); } /* // 处理接收到的数据 while (USART_GetFlagStatus(USART2, USART_IT_RXNE) != RESET) { //STM_EVAL_LEDToggle(LED4); RxData = USART_ReceiveData(USART2); usart_irq_my(1, RxData); } */ }
void usart3_irq_handler(void) { if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET){ // check if more data to send if (uart3_tx_insert_idx != uart3_tx_extract_idx) { USART_SendData(USART3,uart3_tx_buffer[uart3_tx_extract_idx]); uart3_tx_extract_idx++; uart3_tx_extract_idx %= UART3_TX_BUFFER_SIZE; } else { uart3_tx_running = FALSE; // clear running flag USART_ITConfig(USART3, USART_IT_TXE, DISABLE); } } if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){ uint16_t temp = (uart3_rx_insert_idx + 1) % UART3_RX_BUFFER_SIZE;; uart3_rx_buffer[uart3_rx_insert_idx] = USART_ReceiveData(USART3); // check for more room in queue if (temp != uart3_rx_extract_idx) uart3_rx_insert_idx = temp; // update insert index } }
void USART1_IRQHandler(void) //ÖжÏÒì²½½ÓÊÕÉÏλ»ú·¢ËÍÊý¾Ý { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //δ¸´Î» { USART_ClearITPendingBit(USART1,USART_IT_RXNE); //ÇåÖÐ¶Ï Uart1_Rbuf[Uart1_count]=USART_ReceiveData(USART1); Uart1_count++; if(Uart1_count>2) { Uart1_flag=1; //ÓÐЧÊý¾Ý¿ªÊ¼ } if(Uart1_count>(UART1_Buf_Num-1)) { Uart1_count=0; } } }
// 从串口接受数据 uint8_t UsartGet(void) { while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); return (uint8_t)USART_ReceiveData(USART2); }
static void enable_usart1(void) { /* RCC Initialization */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); /* GPIO Initialization */ GPIO_InitTypeDef GPIO_InitStruct = { .GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_UP, .GPIO_Speed = GPIO_Speed_50MHz }; GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); GPIO_Init(GPIOA, &GPIO_InitStruct); /* USART3 Initialization */ USART_InitTypeDef USART_InitStruct = { .USART_BaudRate = 57600, .USART_WordLength = USART_WordLength_8b, .USART_StopBits = USART_StopBits_1, .USART_Parity = USART_Parity_No, .USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_Mode = USART_Mode_Rx | USART_Mode_Tx }; USART_Init(USART1, &USART_InitStruct); USART_Cmd(USART1, ENABLE); } static void enable_usart2(void) { /* RCC Initialization */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); /* GPIO Initialization */ GPIO_InitTypeDef GPIO_InitStruct = { .GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_UP, .GPIO_Speed = GPIO_Speed_50MHz }; GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); GPIO_Init(GPIOD, &GPIO_InitStruct); /* USART2 Initialization */ USART_InitTypeDef USART_InitStruct = { .USART_BaudRate = 9600, .USART_WordLength = USART_WordLength_8b, .USART_StopBits = USART_StopBits_1, .USART_Parity = USART_Parity_No, .USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_Mode = USART_Mode_Rx | USART_Mode_Tx }; USART_Init(USART2, &USART_InitStruct); USART_Cmd(USART2, ENABLE); /* DMA Initialization */ DMA_DeInit(DMA1_Stream6); while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE); } static void enable_usart3(void) { /* RCC Initialization */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* GPIO Initialization */ GPIO_InitTypeDef GPIO_InitStruct = { .GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_UP, .GPIO_Speed = GPIO_Speed_50MHz }; GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); GPIO_Init(GPIOD, &GPIO_InitStruct); /* USART3 Initialization */ USART_InitTypeDef USART_InitStruct = { .USART_BaudRate = 57600, .USART_WordLength = USART_WordLength_8b, .USART_StopBits = USART_StopBits_1, .USART_Parity = USART_Parity_No, .USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_Mode = USART_Mode_Rx | USART_Mode_Tx }; USART_Init(USART3, &USART_InitStruct); USART_Cmd(USART3, ENABLE); USART_ClearFlag(USART3, USART_FLAG_TC); USART_ITConfig(USART3, USART_IT_TXE, DISABLE); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); /* NVIC Initialization */ NVIC_InitTypeDef NVIC_InitStruct = { .NVIC_IRQChannel = USART3_IRQn, .NVIC_IRQChannelPreemptionPriority = configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1, .NVIC_IRQChannelSubPriority = 0, .NVIC_IRQChannelCmd = ENABLE }; NVIC_Init(&NVIC_InitStruct); } static void enable_usart4(void) { /* RCC Initialization */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); /* GPIO Initialization */ GPIO_InitTypeDef GPIO_InitStruct = { .GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_UP, .GPIO_Speed = GPIO_Speed_50MHz }; GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); GPIO_Init(GPIOC, &GPIO_InitStruct); /* USART4 Initialization */ USART_InitTypeDef USART_InitStruct = { .USART_BaudRate = 57600, .USART_WordLength = USART_WordLength_8b, .USART_StopBits = USART_StopBits_1, .USART_Parity = USART_Parity_No, .USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_Mode = USART_Mode_Rx | USART_Mode_Tx }; USART_Init(UART4, &USART_InitStruct); USART_Cmd(UART4, ENABLE); } static void enable_usart5(void) { /* RCC Initialization */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); /* GPIO Initialization */ GPIO_InitTypeDef GPIO_InitStruct = { .GPIO_Pin = GPIO_Pin_2, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_UP, .GPIO_Speed = GPIO_Speed_50MHz }; GPIO_Init(GPIOD, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); /* USART5 Initialization */ USART_InitTypeDef USART_InitStruct = { .USART_BaudRate = 57600, .USART_WordLength = USART_WordLength_8b, .USART_StopBits = USART_StopBits_1, .USART_Parity = USART_Parity_No, .USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_Mode = USART_Mode_Rx | USART_Mode_Tx }; USART_Init(UART5, &USART_InitStruct); USART_Cmd(UART5, ENABLE); } static void enable_usart8(void) { /* RCC Initialization */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8, ENABLE); /* GPIO Initialization */ GPIO_InitTypeDef GPIO_InitStruct = { .GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_UP, .GPIO_Speed = GPIO_Speed_50MHz }; GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_UART8); GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_UART8); GPIO_Init(GPIOE, &GPIO_InitStruct); /* USART8 Initialization */ USART_InitTypeDef USART_InitStruct = { .USART_BaudRate = 57600, .USART_WordLength = USART_WordLength_8b, .USART_StopBits = USART_StopBits_1, .USART_Parity = USART_Parity_No, .USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_Mode = USART_Mode_Rx | USART_Mode_Tx }; USART_Init(UART8, &USART_InitStruct); USART_Cmd(UART8, ENABLE); /* DMA Initialization */ DMA_DeInit(DMA1_Stream6); while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE); } void usart_init() { enable_usart1(); enable_usart2(); enable_usart3(); enable_usart4(); enable_usart5(); enable_usart8(); } void usart2_dma_init() { uint8_t dummy = 0; DMA_InitTypeDef DMA_InitStructure = { /* Configure DMA Initialization Structure */ .DMA_BufferSize = (uint32_t)1, .DMA_FIFOMode = DMA_FIFOMode_Disable, .DMA_FIFOThreshold = DMA_FIFOThreshold_Full, .DMA_MemoryBurst = DMA_MemoryBurst_Single, .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, .DMA_MemoryInc = DMA_MemoryInc_Enable, .DMA_Mode = DMA_Mode_Normal, .DMA_PeripheralBaseAddr = (uint32_t)(&(USART2->DR)), .DMA_PeripheralBurst = DMA_PeripheralBurst_Single, .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, .DMA_PeripheralInc = DMA_PeripheralInc_Disable, .DMA_Priority = DMA_Priority_Medium, /* Configure TX DMA */ .DMA_Channel = DMA_Channel_4, .DMA_DIR = DMA_DIR_MemoryToPeripheral, .DMA_Memory0BaseAddr = (uint32_t)&dummy }; DMA_Init(DMA1_Stream6, &DMA_InitStructure); DMA_Cmd(DMA1_Stream6, ENABLE); USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); } void usart2_dma_send(uint8_t *s) { while (DMA_GetFlagStatus(DMA1_Stream6, DMA_FLAG_TCIF6) == RESET); DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6); DMA_InitTypeDef DMA_InitStructure = { /* Configure DMA Initialization Structure */ .DMA_BufferSize = (uint32_t)strlen((const char *) s), .DMA_FIFOMode = DMA_FIFOMode_Disable, .DMA_FIFOThreshold = DMA_FIFOThreshold_Full, .DMA_MemoryBurst = DMA_MemoryBurst_Single, .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, .DMA_MemoryInc = DMA_MemoryInc_Enable, .DMA_Mode = DMA_Mode_Normal, .DMA_PeripheralBaseAddr = (uint32_t)(&(USART2->DR)), .DMA_PeripheralBurst = DMA_PeripheralBurst_Single, .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, .DMA_PeripheralInc = DMA_PeripheralInc_Disable, .DMA_Priority = DMA_Priority_Medium, /* Configure TX DMA */ .DMA_Channel = DMA_Channel_4, .DMA_DIR = DMA_DIR_MemoryToPeripheral, .DMA_Memory0BaseAddr = (uint32_t)s }; DMA_Init(DMA1_Stream6, &DMA_InitStructure); DMA_Cmd(DMA1_Stream6, ENABLE); USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); } int _write(int fd, char *ptr, int len) { /* Write "len" of char from "ptr" to file id "fd" * Return number of char written. * Need implementing with UART here. */ int i = 0; fd=fd; for (i = 0; i < len ; i++) { USART_SendData(PRINTF_USART, (uint8_t) *ptr); /* Loop until USART2 DR register is empty */ while (USART_GetFlagStatus(PRINTF_USART, USART_FLAG_TXE) == RESET); ptr++; } return len; } xSemaphoreHandle serial_tx_wait_sem = NULL; xQueueHandle serial_rx_queue = NULL; xQueueHandle gps_serial_queue = NULL; void USART3_IRQHandler(void) { long lHigherPriorityTaskWoken = pdFALSE; serial_msg rx_msg; if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET) { xSemaphoreGiveFromISR(serial_tx_wait_sem, &lHigherPriorityTaskWoken); USART_ITConfig(USART3, USART_IT_TXE, DISABLE); } if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { rx_msg.ch = USART_ReceiveData(USART3); if (!xQueueSendToBackFromISR(serial_rx_queue, &rx_msg, &lHigherPriorityTaskWoken)) portEND_SWITCHING_ISR(lHigherPriorityTaskWoken); } portEND_SWITCHING_ISR(lHigherPriorityTaskWoken); } char usart3_read(void) { serial_msg msg; while (!xQueueReceive(serial_rx_queue, &msg, portMAX_DELAY)); return msg.ch; } void usart3_send(char str) { while (!xSemaphoreTake(serial_tx_wait_sem, portMAX_DELAY)); USART_SendData(USART3, (uint16_t)str); USART_ITConfig(USART3, USART_IT_TXE, ENABLE); } void uart8_puts(uint8_t *ptr) { while(*ptr!='\0'){ USART_SendData(PRINTF_USART, (uint8_t) *ptr); /* Loop until USART8 DR register is empty */ while (USART_GetFlagStatus(PRINTF_USART, USART_FLAG_TXE) == RESET); ptr++; } }
/** * 获取串口的字符 * */ u16 Uart1_GetChar(void){ u16 RxData; RxData = USART_ReceiveData(USART1); return RxData; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* Initialize the LCD */ STM3210C_LCD_Init(); /* Clear the LCD */ LCD_Clear(White); /* Set the LCD Text Color */ LCD_SetTextColor(Black); printf(" STM3210C-EVAL \n"); printf("Irda receive example\n"); printf("Set JP16 to IRXD\n\n"); /* Configure the GPIO ports */ GPIO_Configuration(); ////////////////////////////////////////////////////// NVIC_Configuration(); ////////////////////////////////////////////////////// /* Initialize Leds mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* USARTy configuration ------------------------------------------------------*/ /* USARTy 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; /* Configure the USARTy */ USART_Init(USARTy, &USART_InitStructure); /* Enable the USARTy */ USART_Cmd(USARTy, ENABLE); /* Set the USARTy prescaler */ USART_SetPrescaler(USARTy, 0x1); /* Configure the USARTy IrDA mode */ USART_IrDAConfig(USARTy, USART_IrDAMode_Normal); /* Enable the USARTy IrDA mode */ USART_IrDACmd(USARTy, ENABLE); ////////////////////////////////////////////////////////////////////////// TIM_TimeBaseStructure.TIM_Period = 4095; TIM_TimeBaseStructure.TIM_Prescaler = 10; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* Output Compare Toggle Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = speed; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = speed; TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = speed; TIM_OC3Init(TIM3, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable); /* Output Compare Toggle Mode configuration: Channel4 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = speed; TIM_OC4Init(TIM3, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* TIM IT enable */ TIM_ITConfig(TIM3,TIM_IT_Update | TIM_IT_CC1,ENABLE); ////////////////////////////////////////////////////////// while (1) { /* Wait until a byte is received */ while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET) { } /* Read the received byte */ data = USART_ReceiveData(USARTy); //printf("data = %d\n",data); if(data == 0xff) { while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET) { } ReceivedData = (JOY_State_TypeDef)USART_ReceiveData(USARTy); switch(ReceivedData) { case JOY_UP: printf("---JOY_UP---\n"); funcNum = 1; break; case JOY_DOWN: printf("---JOY_DOWN---\n"); funcNum = 2; break; case JOY_LEFT: printf("---JOY_LEFT---\n"); funcNum = 3; break; case JOY_RIGHT: printf("---JOY_RIGHT---\n"); funcNum = 4; break; case JOY_CENTER: printf("---JOY_CENTER---\n"); break; case JOY_NONE: // LCD_ClearLine(Line5); break; default: break; } } else if(data == 0xee) { while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET) { } value = USART_ReceiveData(USARTy); speed = value<<4; //printf("\n speed = %d\n",speed) ; } } }
/** * @brief This function handles USRAT interrupt request. * @param None * @retval None */ void USARTx_IRQHandler(void) { /* USART in mode Tramitter -------------------------------------------------*/ if (USART_GetITStatus(USARTx, USART_IT_TXE) == SET) { /* When Joystick Pressed send the command then send the data */ if (UsartMode == USART_MODE_TRANSMITTER) { /* Send the command */ if (UsartTransactionType == USART_TRANSACTIONTYPE_CMD) { USART_SendData(USARTx, CmdBuffer[TxIndex++]); if (TxIndex == 0x02) { /* Disable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, DISABLE); } } /* Send the data */ else { USART_SendData(USARTx, TxBuffer[TxIndex++]); if (TxIndex == GetVar_NbrOfData()) { /* Disable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, DISABLE); } } } /*If Data Received send the ACK*/ else { USART_SendData(USARTx, AckBuffer[TxIndex++]); if (TxIndex == 0x02) { /* Disable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, DISABLE); } } } /* USART in mode Receiver --------------------------------------------------*/ if (USART_GetITStatus(USARTx, USART_IT_RXNE) == SET) { if (UsartMode == USART_MODE_TRANSMITTER) { AckBuffer[RxIndex++] = USART_ReceiveData(USARTx); } else { /* Receive the command */ if (UsartTransactionType == USART_TRANSACTIONTYPE_CMD) { CmdBuffer[RxIndex++] = USART_ReceiveData(USARTx); } /* Receive the USART data */ else { RxBuffer[RxIndex++] = USART_ReceiveData(USARTx); } } } }
//--------------------------------------------------------------------------------------------------- unsigned char getch_Uart(USART_TypeDef* USARTx) { while (USART_GetFlagStatus(USARTx,USART_FLAG_RXNE) == RESET){} return USART_ReceiveData(USARTx); }
void USART2_IRQHandler() { RecChar2 = USART_ReceiveData(USART2); USART_SendData(USART1,RecChar2); }
void USART1_IRQHandler() { RecChar1 = USART_ReceiveData(USART1); Rec_flag = GPRMC_Analyze(RecChar1,&Data); }
void USART1_IRQHandler(void) //串口1中断服务程序 { uint8_t Res; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收寄存器满中断) { Res =USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据 if(Res<=0x14 && Res >0) { Dac1_Set_Vol(R[Res - 0x01]); //设定DAC值; } else if(Res>0x20 && Res<=0x34) { Dac2_Set_Vol(R[Res - 0x21]); //设定DAC值; } else if (Res==0) { Dac1_Set_Vol(0); //关闭DAC1 } else if (Res==0x20) { Dac2_Set_Vol(0); //关闭DAC2 } /* 以下报文方法废弃,但保留下来 */ // switch (Res) // { // case 'M' : // DacChannel = 1; //DAC1数据 // DacValue = 0; // break; // case 'N' : // DacChannel = 2; //DAC2数据 // DacValue = 0; // break; // default: //处理数据组 // if(BitCounter < 4 ) //正在接收 // { // DacStack[BitCounter] = Res; // BitCounter++; //下标自减 // } // else // { //数据接收完成 // BitCounter = 0; // DacValue = DacStack[0]<<12 + DacStack[1]<<8 + DacStack[2]<<4 + DacStack[3]; // switch (DacChannel) //判断通道几的数据 // { // case 1 : // Dac1_Set_Vol(DacValue); //设定DAC值 // break; // case 2 : // Dac2_Set_Vol(DacValue); //设定DAC值 // break; // default: // break; // } // } // break; // } } if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) //发送中断(发送寄存器空中断) { if(loopCount < getDataLen()) //正在发送 { send16B(SendData0, SendData1, loopCount); loopCount++; } else //发送完成 { USART_ITConfig(USART1, USART_IT_TXE, DISABLE); loopCount = 0; //DataStack0[StackCursor0] = getTIM2Period(); //低频测量方法 DataStack0[StackCursor0] = getTIM2Frequency(); //高频测量方法 SendData0 = pretreatment0(); //预处理 //DataStack1[StackCursor1] = getTIM3Period(); DataStack1[StackCursor1] = getTIM3Frequency(); //高频测量方法 SendData1 = pretreatment1(); //预处理 } } }
/******************************************************************************* * 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; RxBuffer1[RxCounter1]= USART_ReceiveData(USART1); y_bias= RxBuffer1[RxCounter1]; if(RxBuffer1[RxCounter1]==0xF1||RxBuffer1[RxCounter1]==0xF3||RxBuffer1[RxCounter1]==0xa0||RxBuffer1[RxCounter1]==0xa1||RxBuffer1[RxCounter1]==0xb0) { send_data_flag=TRUE; } // if(RxBuffer1[RxCounter1]==0xFF) // { // distance_valid_flag=FALSE; // send_data_flag=TRUE; // } // if(RxBuffer1[RxCounter1]==0x01) // { // //distance_valid_flag=TRUE; // ydir=0x01;//Y IS TOO HIGH // //send_data_flag=TRUE; // } // if(RxBuffer1[RxCounter1]==0x02) // { // //distance_valid_flag=TRUE; // ydir=0x02;//y is too low // //send_data_flag=TRUE; // } // if(RxBuffer1[RxCounter1]==0x03) // { // //distance_valid_flag=TRUE; // ydir=0x03;//y is too low // //send_data_flag=TRUE; // } // if(RxBuffer1[RxCounter1]==0xAA) // { // distance_valid_flag=TRUE; // send_data_flag=TRUE; // } RxCounter1++; if(RxCounter1>1) { RxCounter1=0; } } //溢出-如果发生溢出需要先读 SR,再读 DR 寄存器则可清除不断入中断的问题[牛人说要这样] if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET) { USART_ClearFlag(USART1,USART_FLAG_ORE); //读 SR 其实就是清除标志 USART_ReceiveData(USART1); //读 DR } }
BOOL xMBMasterPortSerialGetByte(CHAR * pucByte) { *pucByte = USART_ReceiveData(USART1); return TRUE; }
void serial1_interrupt(){ Serial1::interface->serialReadChar(USART_ReceiveData(SERIAL1CH)); }
int serial_getc(serial_t *obj) { USART_TypeDef *usart = (USART_TypeDef *)(obj->uart); while (!serial_readable(obj)); return (int)(USART_ReceiveData(usart)); }
int usart_rx_char(pyb_usart_t usart_id) { USART_TypeDef *USARTx = usart_get_base(usart_id); return USART_ReceiveData(USARTx); }
/** * @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_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* Initialize Leds, Wakeup and Key Buttons mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_PBInit(BUTTON_WAKEUP, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); /* USARTy and USARTz configuration -------------------------------------------*/ /* USARTy and USARTz configured as follow: - BaudRate = 9600 baud - Word Length = 9 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_9b; 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 USARTy */ USART_Init(USARTy, &USART_InitStructure); /* Configure USARTz */ USART_Init(USARTz, &USART_InitStructure); /* Enable the USARTy */ USART_Cmd(USARTy, ENABLE); /* Enable the USARTz */ USART_Cmd(USARTz, ENABLE); /* Set the USARTy Address */ USART_SetAddress(USARTy, 0x1); /* Set the USARTz Address */ USART_SetAddress(USARTz, 0x2); /* Select the USARTz WakeUp Method */ USART_WakeUpConfig(USARTz, USART_WakeUp_AddressMark); while (1) { /* Send one byte from USARTy to USARTz */ USART_SendData(USARTy, 0x33); /* Wait while USART1 TXE = 0 */ while(USART_GetFlagStatus(USARTz, USART_FLAG_TXE) == RESET) { } if(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) != RESET) { if(USART_ReceiveData(USARTz) == 0x33) { STM_EVAL_LEDToggle(LED1); Delay(0x5FFFF); STM_EVAL_LEDToggle(LED2); Delay(0x5FFFF); STM_EVAL_LEDToggle(LED3); Delay(0x5FFFF); STM_EVAL_LEDToggle(LED4); Delay(0x5FFFF); } } } }
uint8_t uart_readByte() { uint16_t temp; temp = USART_ReceiveData(USART1); return (uint8_t)temp; }
// @brief : To get a character from UART through polling // @param : none // @retval: character received int RxChar(USART_TypeDef* usart) { while(USART_GetFlagStatus(usart, USART_IT_RXNE) == RESET); // while USART RX buffer is empty, wait. return USART_ReceiveData(usart); // receive data from RX buffer }