/* *@功能:uart2 的配置 * 波特率:9600 * 数据长度:8位 * 停止位:1位 * 无奇偶校验 * 无硬件流控制 * 全双工 *@说明:配置成这样是RFID模块确定的 */ void USART2_Configuration(void) { 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_Rx | USART_Mode_Tx; //同步通信时用的 //USART_InitStructure.USART_Clock = USART_Clock_Disable; //USART_InitStructure.USART_CPOL = USART_CPOL_Low; //USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; //USART_InitStructure.USART_LastBit = USART_LastBit_Disable; //USART_StructInit(USART_InitStructure); USART_Init(USART2, &USART_InitStructure); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /*接收中断使能*/ }
int uart1_send(u8_t *buff,u32_t size) { uint32_t irq_flag; int ret; if((size == 0) || (buff == 0)) return -1; irq_flag = local_irq_save(); while(!(USART1->SR & USART_FLAG_TXE)); u1_send.size = size; u1_send.cnt = 0; u1_send.buff = buff; u1_send.sending.val = 1; local_irq_restore(irq_flag); USART_ClearFlag(USART1, USART_FLAG_TC); USART_ITConfig(USART1, USART_IT_TC, ENABLE); uart1_send_next(); ret = wait_event_timeout(u1_send.wait, 2000); u1_send.sending.val = 0; if((-1 == ret) || (u1_send.cnt != u1_send.size)) { p_err("uart1_send err:%d,%d,%d\n",ret,u1_send.cnt,u1_send.size); } return 0; }
//------------------------------------------------------ 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 } }
//Configures the serial port hardware to function with interrupts, just like other serial ports //Baud 115200, 8-N-1 //Only Tx, no RX active void debug_config() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 10; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_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_Tx; USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); USART_ITConfig(USART3, USART_IT_TXE, ENABLE); }
/******************************************************************************* * Function Name : UART0_Config_Default. * Description : configure the UART 0 with default values. * Input : None. * Return : None. *******************************************************************************/ void USART_Config_Default(void) { /* USART1 default configuration */ /* USART1 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - Parity Odd - Hardware flow control desabled - 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_Odd; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure the USART1 */ USART_Init(USART1, &USART_InitStructure); /* Enable the USART Receive interrupt */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); }
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 ); }
void usart1_init(void) { USART_InitTypeDef usart_i; GPIO_InitTypeDef gpio_i; GPIO_PinRemapConfig(GPIO_Remap_USART1,DISABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* TX */ gpio_i.GPIO_Pin = GPIO_Pin_9; gpio_i.GPIO_Speed = GPIO_Speed_50MHz; gpio_i.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &gpio_i); /* RX */ gpio_i.GPIO_Pin = GPIO_Pin_10; gpio_i.GPIO_Speed = GPIO_Speed_50MHz; gpio_i.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &gpio_i); /* 115200 8n1 */ usart_i.USART_BaudRate = 115200; usart_i.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart_i.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart_i.USART_Parity = USART_Parity_No; usart_i.USART_StopBits = USART_StopBits_1; usart_i.USART_WordLength = USART_WordLength_8b; USART_Init(USART1, &usart_i); /* enable RXNE interrupt */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_EnableIRQ(USART1_IRQn); USART_Cmd(USART1,ENABLE); }
/** * @brief UART5 GPIO 配置,工作模式配置。115200 8-N-1 * @param 无 * @retval 无 */ void UART5_Config(uint32_t BaudRate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config UART5 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); /* UART5 GPIO config */ /* Configure UART5 Tx (PC.12) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure UART5 Rx (PD.2) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); /* UART5 mode config */ 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(UART5, &USART_InitStructure); /* 使能串口5接收中断 */ USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); USART_Cmd(UART5, ENABLE); UART5_NVIC_Configuration(ENABLE); USART_DMACmd(UART5,USART_DMAReq_Tx,ENABLE); }
/* ********************************************************************************************************* * 函 数 名: UartSend * 功能说明: 填写数据到UART发送缓冲区,并启动发送中断。中断处理函数发送完毕后,自动关闭发送中断 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ static void UartSend(UART_T *_pUart, uint8_t *_ucaBuf, uint16_t _usLen) { uint16_t i; for (i = 0; i < _usLen; i++) { /* 如果发送缓冲区已经满了,则等待缓冲区空 */ while (1) { uint16_t usCount; DISABLE_INT(); usCount = _pUart->usTxCount; ENABLE_INT(); if (usCount < _pUart->usTxBufSize) { break; } } /* 将新数据填入发送缓冲区 */ _pUart->pTxBuf[_pUart->usTxWrite] = _ucaBuf[i]; DISABLE_INT(); if (++_pUart->usTxWrite >= _pUart->usTxBufSize) { _pUart->usTxWrite = 0; } _pUart->usTxCount++; ENABLE_INT(); } USART_ITConfig(_pUart->uart, USART_IT_TXE, ENABLE); }
/* * 函数名:USART1_Config * 描述 :USART1 GPIO 配置,工作模式配置。115200 8-N-1 * 输入 :无 * 输出 : 无 * 调用 :外部调用 */ void USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* 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_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_Cmd(USART1, ENABLE); 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); }
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 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 USART3_Configuration(void) { USART_InitTypeDef usart3; GPIO_InitTypeDef gpio; NVIC_InitTypeDef nvic; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//ʹÄÜʱÖÓ GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); gpio.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; gpio.GPIO_Mode = GPIO_Mode_AF; gpio.GPIO_OType = GPIO_OType_PP; gpio.GPIO_Speed = GPIO_Speed_100MHz; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB,&gpio); usart3.USART_BaudRate = 115200; usart3.USART_WordLength = USART_WordLength_8b; usart3.USART_StopBits = USART_StopBits_1; usart3.USART_Parity = USART_Parity_No; usart3.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; usart3.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART3,&usart3); USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); USART_Cmd(USART3,ENABLE); nvic.NVIC_IRQChannel = USART3_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 3; nvic.NVIC_IRQChannelSubPriority = 3; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); }
serialPort_t *uartOpen(USART_TypeDef *USARTx, serialReceiveCallbackPtr callback, uint32_t baudRate, portMode_t mode) { DMA_InitTypeDef DMA_InitStructure; USART_InitTypeDef USART_InitStructure; uartPort_t *s = NULL; if (USARTx == USART1) s = serialUSART1(baudRate, mode); if (USARTx == USART2) s = serialUSART2(baudRate, mode); s->USARTx = USARTx; // common serial initialisation code should move to serialPort::init() s->port.rxBufferHead = s->port.rxBufferTail = 0; s->port.txBufferHead = s->port.txBufferTail = 0; // callback for IRQ-based RX ONLY s->port.callback = callback; s->port.mode = mode; s->port.baudRate = baudRate; USART_InitStructure.USART_BaudRate = baudRate; USART_InitStructure.USART_WordLength = USART_WordLength_8b; if (mode & MODE_SBUS) { USART_InitStructure.USART_StopBits = USART_StopBits_2; USART_InitStructure.USART_Parity = USART_Parity_Even; } else { 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 = 0; if (mode & MODE_RX) USART_InitStructure.USART_Mode |= USART_Mode_Rx; if (mode & MODE_TX) USART_InitStructure.USART_Mode |= USART_Mode_Tx; USART_Init(USARTx, &USART_InitStructure); USART_Cmd(USARTx, ENABLE); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USARTx->DR; DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // Receive DMA or IRQ if (mode & MODE_RX) { if (s->rxDMAChannel) { DMA_InitStructure.DMA_BufferSize = s->port.rxBufferSize; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)s->port.rxBuffer; DMA_DeInit(s->rxDMAChannel); DMA_Init(s->rxDMAChannel, &DMA_InitStructure); DMA_Cmd(s->rxDMAChannel, ENABLE); USART_DMACmd(USARTx, USART_DMAReq_Rx, ENABLE); s->rxDMAPos = DMA_GetCurrDataCounter(s->rxDMAChannel); } else { USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); } } // Transmit DMA or IRQ if (mode & MODE_TX) { if (s->txDMAChannel) { DMA_InitStructure.DMA_BufferSize = s->port.txBufferSize; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_DeInit(s->txDMAChannel); DMA_Init(s->txDMAChannel, &DMA_InitStructure); DMA_ITConfig(s->txDMAChannel, DMA_IT_TC, ENABLE); DMA_SetCurrDataCounter(s->txDMAChannel, 0); s->txDMAChannel->CNDTR = 0; USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); } else { USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); } } return (serialPort_t *)s; }
int8 wizpf_usart_init(wizpf_usart usart, usart_param *param) { USART_TypeDef *usartx; USART_InitTypeDef USART_InitStructure; switch(usart) { case WIZ_USART1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); #ifdef USART1_TX_PIN wizpf_gpio_init(USART1_TX_PORT, USART1_TX_PIN, GMOD_AF_PUSHPULL); #endif #ifdef USART1_RX_PIN wizpf_gpio_init(USART1_RX_PORT, USART1_RX_PIN, GMOD_IN_FLOAT); #endif #ifdef USART1_RTS_PIN wizpf_gpio_init(USART1_RTS_PORT, USART1_RTS_PIN, GMOD_AF_PUSHPULL); #endif #ifdef USART1_CTS_PIN wizpf_gpio_init(USART1_CTS_PORT, USART1_CTS_PIN, GMOD_IN_FLOAT); #endif #if (USART1_RX_INTERRUPT == VAL_ENABLE) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } #endif usartx = USART1; break; case WIZ_USART2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); #ifdef USART2_TX_PIN wizpf_gpio_init(USART2_TX_PORT, USART2_TX_PIN, GMOD_AF_PUSHPULL); #endif #ifdef USART2_RX_PIN wizpf_gpio_init(USART2_RX_PORT, USART2_RX_PIN, GMOD_IN_FLOAT); #endif #ifdef USART2_RTS_PIN wizpf_gpio_init(USART2_RTS_PORT, USART2_RTS_PIN, GMOD_AF_PUSHPULL); #endif #ifdef USART2_CTS_PIN wizpf_gpio_init(USART2_CTS_PORT, USART2_CTS_PIN, GMOD_IN_FLOAT); #endif #if (USART2_RX_INTERRUPT == VAL_ENABLE) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } #endif usartx = USART2; break; //case WIZ_USART3: // break; default: return RET_NOK; } USART_Cmd(usartx, DISABLE); USART_InitStructure.USART_BaudRate = param->baudrate; switch(param->wordlen) { case UWL_8: USART_InitStructure.USART_WordLength = USART_WordLength_8b; break; case UWL_9: USART_InitStructure.USART_WordLength = USART_WordLength_9b; break; } switch(param->stopbit) { case UST_0d5: USART_InitStructure.USART_StopBits = USART_StopBits_0_5; break; case UST_1: USART_InitStructure.USART_StopBits = USART_StopBits_1; break; case UST_1d5: USART_InitStructure.USART_StopBits = USART_StopBits_1_5; break; case UST_2: USART_InitStructure.USART_StopBits = USART_StopBits_2; break; } switch(param->parity) { case UPB_NO: USART_InitStructure.USART_Parity = USART_Parity_No; break; case UPB_EVEN:USART_InitStructure.USART_Parity = USART_Parity_Even; break; case UPB_ODD: USART_InitStructure.USART_Parity = USART_Parity_Odd; break; } switch(param->flowcon) { case UFC_NO: USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; break; case UFC_HW: USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; break; //case UFC_SW: } USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(usartx, &USART_InitStructure); #if (USART1_RX_INTERRUPT == VAL_ENABLE) USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); #endif #if (USART2_RX_INTERRUPT == VAL_ENABLE) USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); #endif USART_Cmd(usartx, ENABLE); return RET_OK; }
void OSView_TxIntEn (void) { USART_ITConfig(OS_VIEW_USART, USART_IT_TXE, ENABLE); }
void OSView_TxIntDis (void) { USART_ITConfig(OS_VIEW_USART, USART_IT_TXE, DISABLE); }
void OSView_RxIntEn (void) { USART_ITConfig(OS_VIEW_USART, USART_IT_RXNE, ENABLE); }
void OSView_RxIntDis (void) { USART_ITConfig(OS_VIEW_USART, USART_IT_RXNE, DISABLE); }
/* This funcion initializes the USART1 peripheral * * Arguments: baudrate --> the baudrate at which the USART is * supposed to operate */ void usart1_init(uint32_t baudrate, xQueueHandle* p_queue_chars) { _p_queue_uart_char = p_queue_chars; /* This is a concept that has to do with the libraries provided by ST * to make development easier the have made up something similar to * classes, called TypeDefs, which actually just define the common * parameters that every peripheral needs to work correctly * * They make our life easier because we don't have to mess around with * the low level stuff of setting bits in the correct registers */ GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller) /* enable APB2 peripheral clock for USART1 * note that only USART1 and USART6 are connected to APB2 * the other USARTs are connected to APB1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* enable the peripheral clock for the pins used by * USART1, PB6 for TX and PB7 for RX */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* This sequence sets up the TX and RX pins * so they work correctly with the USART1 peripheral */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate! GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this defines the output type as push pull mode (as opposed to open drain) GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // this activates the pullup resistors on the IO pins GPIO_Init(GPIOB, &GPIO_InitStruct); // now all the values are passed to the GPIO_Init() function which sets the GPIO registers /* The RX and TX pins are now connected to their AF * so that the USART1 can take over control of the * pins */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); // GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); /* Now the USART_InitStruct is used to define the * properties of USART1 */ USART_InitStruct.USART_BaudRate = baudrate; // the baudrate is set to the value we passed into this init function USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard) USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard) USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard) USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard) USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver USART_Init(USART1, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting /* Here the USART1 receive interrupt is enabled * and the interrupt controller is configured * to jump to the USART1_IRQHandler() function * if the USART1 receive interrupt occurs */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // we want to configure the USART1 interrupts NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART1 interrupts are globally enabled NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff // finally this enables the complete USART1 peripheral USART_Cmd(USART1, ENABLE); }
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++; } }
/** * 初始化配置 */ void USART1_Init(void) { /** * 1.初始化时钟 */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /** * 2.初始化IO */ GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART1 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 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); /** * 3.初始化控制器 */ 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 USARTy */ USART_Init(USART1, &USART_InitStructure); /** * 4.初始化DMA */ DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Channel5); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&USART1_RxBuffer[0]; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = sizeof(USART1_RxBuffer); 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_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel5, &DMA_InitStructure); USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); DMA_Cmd(DMA1_Channel5, ENABLE); DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&USART1_TxBuffer[0]; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = sizeof(USART1_TxBuffer); DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); USART_Cmd(USART1, ENABLE); /** * 5.初始化中断系统 */ NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the DMA1_Channel7 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void uart_disableInterrupts() { USART_ITConfig(USART1, USART_IT_TXE, DISABLE); USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); }
/** * @brief Configures the sonar sensor Peripheral. */ void sonar_config(void) { valid_data = 0; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); /* Configure l3gd20 CS pin in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOE, &GPIO_InitStructure); /* Configures the nested vectored interrupt controller. */ NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the USART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); /* Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* Connect UART pins to AF7 */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); GPIO_InitTypeDef GPIO_InitStructure_Serial2; GPIO_InitStructure_Serial2.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure_Serial2.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure_Serial2.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure_Serial2.GPIO_PuPd = GPIO_PuPd_UP; /* USART RX pin configuration */ GPIO_InitStructure_Serial2.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOC, &GPIO_InitStructure_Serial2); 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_Rx; /* Configure the UART4 */ USART_Init(UART4, &USART_InitStructure); /* Enable UART4 interrupt */ USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_Cmd(UART4, ENABLE); }
void uart_enableInterrupts() { USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); }
/** * @brief VCP_COMConfig * Configure the COM Port with default values or values received from host. * @param Conf: can be DEFAULT_CONFIG to set the default configuration or OTHER_CONFIG * to set a configuration received from the host. * @retval None. */ static uint16_t VCP_COMConfig(uint8_t Conf) { #if 0 if (Conf == DEFAULT_CONFIG) { /* EVAL_COM1 default configuration */ /* EVAL_COM1 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - Parity Odd - Hardware flow control disabled - 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_Odd; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure and enable the USART */ STM_EVAL_COMInit(COM1, &USART_InitStructure); /* Enable the USART Receive interrupt */ USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE); } else { /* set the Stop bit*/ switch (linecoding.format) { case 0: USART_InitStructure.USART_StopBits = USART_StopBits_1; break; case 1: USART_InitStructure.USART_StopBits = USART_StopBits_1_5; break; case 2: USART_InitStructure.USART_StopBits = USART_StopBits_2; break; default : VCP_COMConfig(DEFAULT_CONFIG); return (USBD_FAIL); } /* set the parity bit*/ switch (linecoding.paritytype) { case 0: USART_InitStructure.USART_Parity = USART_Parity_No; break; case 1: USART_InitStructure.USART_Parity = USART_Parity_Even; break; case 2: USART_InitStructure.USART_Parity = USART_Parity_Odd; break; default : VCP_COMConfig(DEFAULT_CONFIG); return (USBD_FAIL); } /*set the data type : only 8bits and 9bits is supported */ switch (linecoding.datatype) { case 0x07: /* With this configuration a parity (Even or Odd) should be set */ USART_InitStructure.USART_WordLength = USART_WordLength_8b; break; case 0x08: if (USART_InitStructure.USART_Parity == USART_Parity_No) { USART_InitStructure.USART_WordLength = USART_WordLength_8b; } else { USART_InitStructure.USART_WordLength = USART_WordLength_9b; } break; default : VCP_COMConfig(DEFAULT_CONFIG); return (USBD_FAIL); } USART_InitStructure.USART_BaudRate = linecoding.bitrate; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure and enable the USART */ STM_EVAL_COMInit(COM1, &USART_InitStructure); } #endif return USBD_OK; }
/* * See header file for parameter descriptions. */ long lCOMPortInit( unsigned long ulPort, unsigned long ulWantedBaud ) { long lReturn = pdFAIL; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; if( ulPort < serNUM_COM_PORTS ) { /* The common (not port dependent) part of the initialisation. */ USART_InitStructure.USART_BaudRate = ulWantedBaud; 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; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /* Init the buffer structures with the buffer for the COM port being initialised, and perform any non-common initialisation necessary. This does not check to see if the COM port has already been initialised. */ if( ulPort == 0 ) { /* Create the queue of chars that are waiting to be sent to COM0. */ xCharsForTx[ 0 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) ); /* Create the queue used to hold characters received from COM0. */ xRxedChars[ 0 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) ); /* Enable COM0 clock - the ST libraries start numbering from UART1. */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE ); /* Configure USART1 Rx (PA10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init( GPIOA, &GPIO_InitStructure ); /* Configure USART1 Tx (PA9) 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 ); USART_Init( USART1, &USART_InitStructure ); USART_ITConfig( USART1, USART_IT_RXNE, ENABLE ); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; NVIC_Init( &NVIC_InitStructure ); USART_DMACmd( USART1, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE ); USART_Cmd( USART1, ENABLE ); /* Everything is ok. */ lReturn = pdPASS; } else if( ulPort == 1 ) { /* Create the queue of chars that are waiting to be sent to COM1. */ xCharsForTx[ 1 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) ); /* Create the queue used to hold characters received from COM0. */ xRxedChars[ 1 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) ); /* Enable COM0 clock - the ST libraries start numbering from 1. */ RCC_APB2PeriphClockCmd( RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE ); /* Configure USART2 Rx (PA3) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init( GPIOA, &GPIO_InitStructure ); /* Configure USART2 Tx (PA2) 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 ); USART_Init( USART2, &USART_InitStructure ); USART_ITConfig( USART2, USART_IT_RXNE, ENABLE ); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel; NVIC_Init( &NVIC_InitStructure ); USART_DMACmd( USART2, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE ); USART_Cmd( USART2, ENABLE ); /* Everything is ok. */ lReturn = pdPASS; } else { /* Nothing to do unless more than two ports are supported. */ } } return lReturn; }
Serial::Serial(Port port, System *parent) { thisPort = port; GPIO_InitTypeDef GPIO_InitStructure; txBufPos = rxBufPos = 0; if (port == RS485) { if (PortRS485Open) { parent->setFault(FLT_FIRMWARE,500101); return; //fail because already opened } PortRS485Open = true; rxBuffer=(u8*)malloc(RXBUFSIZE); txBuffer=(u8*)malloc(TXBUFSIZE); /* Enable USARTz Clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* Enable the DMA clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); /* Connect USART pins to AF7 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); //TX=PA9 AF7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); //RX=PA10 AF7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); //TXEN=PC10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); RS485_EnableTransmit(false); /* Enable the USART OverSampling by 8 */ //by default oversampling=16 USART_OverSampling8Cmd(USART2, ENABLE); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 460800; //1.5mbps=max @24MHz (koska 24M/16=1.5M) 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 USARTy */ USART_Init(USART1, &USART_InitStructure); /* Enable USARTy DMA TX request */ USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); /* Enable USARTy */ USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_TC, ENABLE); /* Enable the USARTx Interrupt, used for setting RS485 transmit enable=false after transmit complete */ NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;//higher value=lower priority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init_GD(&NVIC_InitStructure); //DMA2_Stream2==RX { //common? DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t) USART1 + 0x04); 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_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //tx DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) txBuffer; DMA_InitStructure.DMA_BufferSize = (uint16_t) TXBUFSIZE; DMA_Init(DMA2_Stream7, &DMA_InitStructure); //RX DMA_DeInit(DMA2_Stream2); DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t) USART1 + 0x04); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) rxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = RXBUFSIZE; 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_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_Init(DMA2_Stream2, &DMA_InitStructure); DMA_Cmd(DMA2_Stream2, ENABLE); } } //for DSC serial if (port == DSC) { if (PortDSCOpen) { parent->setFault(FLT_FIRMWARE,500102); return; //fail because already opened } PortDSCOpen = true; rxBuffer=(u8*)malloc(RXBUFSIZE); txBuffer=(u8*)malloc(TXBUFSIZE); /* Enable USARTz Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable the DMA clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); /* Connect USART pins to AF7 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); //TX=PA2 AF7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); //RX=PA3 AF7 GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable the USART OverSampling by 8 */ //by default oversampling=16 USART_OverSampling8Cmd(USART2, ENABLE); USART_InitTypeDef USART_InitStructure; //USART_InitStructure.USART_BaudRate = 468750; //1.5mbps=max @24MHz (koska 24M/16=1.5M) USART_InitStructure.USART_BaudRate = 416667; //1.5mbps=max @24MHz (koska 24M/16=1.5M) 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 USARTy */ USART_Init(USART2, &USART_InitStructure); /* Enable USARTy DMA TX request */ USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE); /* Enable USARTy */ USART_Cmd(USART2, ENABLE); //DMA1_Stream5==RX { //common? DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t) USART2 + 0x04); 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_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //TX DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) txBuffer; DMA_InitStructure.DMA_BufferSize = (uint16_t) TXBUFSIZE; DMA_Init(DMA1_Stream6, &DMA_InitStructure); DMA_DeInit(DMA1_Stream5); DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t) USART2 + 0x04); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) rxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = RXBUFSIZE; rxPacketLen = RXBUFSIZE; 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_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_Init(DMA1_Stream5, &DMA_InitStructure); DMA_Cmd(DMA1_Stream5, ENABLE); /* Enable the USARTx Interrupt */ NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;//higher value=lower priority NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init_GD(&NVIC_InitStructure); } } }
void USART::interrupt(FunctionalState enable) { USART_ITConfig(_USARTx,USART_IT_RXNE,enable); }
/******************************************************************************* 主函数 *******************************************************************************/ int main(void) { u16 RxData; NVIC_InitTypeDef NVIC_InitStructure; #ifdef DEBUG debug(); #endif RCC_Configuration(); //系统时钟配置函数 NVIC_Configuration(); //NVIC配置函数 GPIO_Configuration_usarts(); //GPIO端口配置 //GPIO_Configuration_key_led(); //串口配置: 波特率 115200 数据位 8 停止位 1 奇偶位 NONE 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(USARTx, &USART_InitStructure); //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 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(USARTx, USART_IT_RXNE, ENABLE);//开启串口接受中断 //启动串口 USART_Cmd(USARTx, ENABLE); pressFlag = 0; appFlag = 0; while (1) { if(tcp_state > 0){ Uart1_PutString("recevice infos success", 23); tcp_state = 0; } //do_ustarts(); /* RxData = Uart1_GetChar(); if(RxData == 97){ Uart1_PutString("absdfsdf", 9); }else{ Uart1_PutString("n", 2); } */ //do_led_key(); } }