// USART配置 void Uart_Config(void) { USART_InitTypeDef my_USART_InitStructure; // USART初始化结构 USART_StructInit(&my_USART_InitStructure); my_USART_InitStructure.USART_BaudRate = 115200; my_USART_InitStructure.USART_WordLength = USART_WordLength_8b; my_USART_InitStructure.USART_StopBits = USART_StopBits_1; my_USART_InitStructure.USART_Parity = USART_Parity_No; my_USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; my_USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; STM_EVAL_COMInit(COM1, &my_USART_InitStructure);//io cofig,GPIO /* Enable the EVAL_COM1 Transmit interrupt: this interrupt is generated when the EVAL_COM1 transmit data register is empty */ USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE);//中断使能。 }
/******************************************************************************* * Function Name :static s32 BspUsartxInit(void) * Description :串口及引脚初始化 * Input : * Output : * Other : * Date :2013.01.27 *******************************************************************************/ void BspUsart1Init(void) { GPIO_InitTypeDef GpioInitdef; USART_InitTypeDef UsartInitdef; NVIC_InitTypeDef NvicInitdef; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); USART_DeInit(USART1); USART_StructInit( &UsartInitdef ); UsartInitdef.USART_BaudRate = 115200; UsartInitdef.USART_StopBits = USART_StopBits_1; UsartInitdef.USART_WordLength = USART_WordLength_8b; UsartInitdef.USART_Parity = USART_Parity_No; UsartInitdef.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &UsartInitdef); GpioInitdef.GPIO_Pin = GPIO_Pin_9; // GpioInitdef.GPIO_Speed = GPIO_Speed_10MHz; GpioInitdef.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GpioInitdef); GpioInitdef.GPIO_Pin = GPIO_Pin_10; // GpioInitdef.GPIO_Speed = GPIO_Speed_10MHz; GpioInitdef.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GpioInitdef); USART_ITConfig(USART1, USART_IT_TXE, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NvicInitdef.NVIC_IRQChannel = USART1_IRQChannel; NvicInitdef.NVIC_IRQChannelPreemptionPriority = 1; NvicInitdef.NVIC_IRQChannelSubPriority = 0; NvicInitdef.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NvicInitdef ); USART_Cmd(USART1, ENABLE); }
void printf2_Init(int baudrate) { #ifndef MULTIPLEXED_OUTPUT RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7); USART_InitTypeDef USART_InitStructure; USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = baudrate; USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); #endif }
void cereal_init(uint32_t baud) { ringbuffer_init(&cereal_incoming, malloc(CEREAL_INCOMING_MAX_SIZE), CEREAL_INCOMING_MAX_SIZE); #ifdef ENABLE_CEREAL_BUFFERED_TX ringbuffer_init(&cereal_outgoing, malloc(CEREAL_OUTGOING_MAX_SIZE), CEREAL_OUTGOING_MAX_SIZE); #endif RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); GPIO_InitTypeDef gi; GPIO_StructInit(&gi); gi.GPIO_Pin = GPIO_Pin_9; gi.GPIO_Mode = GPIO_Mode_AF; gi.GPIO_Speed = GPIO_Speed_50MHz; gi.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &gi); gi.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &gi); USART_InitTypeDef ui; USART_StructInit(&ui); ui.USART_BaudRate = baud; USART_Init(CEREAL_USARTx, &ui); USART_Cmd(CEREAL_USARTx, ENABLE); USART_ITConfig(CEREAL_USARTx, USART_IT_RXNE, ENABLE); NVIC_InitTypeDef ni; ni.NVIC_IRQChannel = USART1_IRQn; ni.NVIC_IRQChannelPreemptionPriority = 0; ni.NVIC_IRQChannelSubPriority = 0; ni.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&ni); }
void gpsInit() { GPIO_InitTypeDef gpio; USART_InitTypeDef uart; NVIC_InitTypeDef nvic; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); GPIO_StructInit(&gpio); gpio.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; gpio.GPIO_Mode = GPIO_Mode_AF; gpio.GPIO_Speed = GPIO_Speed_25MHz; gpio.GPIO_OType = GPIO_OType_PP; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &gpio); GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); USART_StructInit(&uart); uart.USART_BaudRate = 9600; uart.USART_WordLength = USART_WordLength_8b; uart.USART_StopBits = USART_StopBits_1; uart.USART_HardwareFlowControl = USART_HardwareFlowControl_None; uart.USART_Parity = USART_Parity_No; uart.USART_Mode = USART_Mode_Rx; USART_Init(UART4, &uart); USART_Cmd(UART4, ENABLE); USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); nvic.NVIC_IRQChannel = UART4_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0; nvic.NVIC_IRQChannelSubPriority = 0; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); }
void usart_setup(usart_dev *dev, uint32_t baudRate, uint16_t wordLength, uint16_t stopBits, uint16_t parity, uint16_t mode, uint16_t hardwareFlowControl, uint32_t tx_timeout) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); assert_param(IS_USART_BAUDRATE(baud)); assert_param(IS_USART_STOPBITS(stopbits)); assert_param(IS_USART_PARITY(parity)); assert_param(IS_USART_WORD_LENGTH(wordLength)); assert_param(IS_USART_MODE(mode)); assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hardwareFlowControl)); dev->tx_timeout = tx_timeout; dev->txbusy = 0; dev->usetxrb = 1; dev->use_timeout = 0; /* Disable USARTx */ USART_Cmd(dev->USARTx, DISABLE); /* Enables the USART's 8x oversampling mode. */ USART_OverSampling8Cmd(dev->USARTx, ENABLE); USART_ClockInitTypeDef USART_InitClock; USART_ClockStructInit(&USART_InitClock); USART_ClockInit(dev->USARTx, &USART_InitClock); USART_InitTypeDef USART_config; USART_StructInit(&USART_config); USART_config.USART_BaudRate = baudRate; USART_config.USART_WordLength = wordLength; USART_config.USART_StopBits = stopBits; USART_config.USART_Parity = parity; USART_config.USART_Mode = mode; USART_config.USART_HardwareFlowControl = hardwareFlowControl; USART_Init(dev->USARTx, &USART_config); NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = dev->irq; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(dev->USARTx, USART_IT_PE, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_IDLE, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_LBD, DISABLE); if (IS_USART_1236_PERIPH(dev->USARTx)) USART_ITConfig(dev->USARTx, USART_IT_CTS, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_ERR, DISABLE); /* Enable USART2 Rx request */ USART_ITConfig(dev->USARTx, USART_IT_RXNE, ENABLE); USART_ClearFlag(dev->USARTx, USART_FLAG_RXNE); USART_ITConfig(dev->USARTx, USART_IT_TC, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_TXE, ENABLE); USART_ClearFlag(dev->USARTx, USART_FLAG_TC); /* USART_ITConfig(dev->USARTx, USART_IT_RXNE, ENABLE); USART_ITConfig(dev->USARTx, USART_IT_PE, ENABLE); USART_ITConfig(dev->USARTx, USART_IT_ERR, ENABLE); */ }
int deviceInit(void) { int ret =0; USART_InitTypeDef usartInfo; // EXTI_InitTypeDef extiInfo; GPIO_InitTypeDef gpioInfo; NVIC_InitTypeDef nvicInfo; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3, ENABLE); gpioInfo.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3; gpioInfo.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &gpioInfo); gpioInfo.GPIO_Pin = GPIO_Pin_11; gpioInfo.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &gpioInfo); gpioInfo.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2; gpioInfo.GPIO_Speed = GPIO_Speed_50MHz; gpioInfo.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &gpioInfo); gpioInfo.GPIO_Pin = GPIO_Pin_10; gpioInfo.GPIO_Speed = GPIO_Speed_50MHz; gpioInfo.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &gpioInfo); USART_DeInit(USART1); USART_DeInit(USART2); USART_DeInit(USART3); USART_StructInit(&usartInfo); usartInfo.USART_BaudRate = 115200; usartInfo.USART_WordLength = 8; USART_Init(USART1, &usartInfo); USART_Init(USART2, &usartInfo); USART_Init(USART3, &usartInfo); USART_Cmd(USART1, ENABLE); USART_Cmd(USART2, ENABLE); USART_Cmd(USART3, ENABLE); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET); while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET); while( USART_GetFlagStatus(USART3,USART_FLAG_TC)!= SET); // SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); // SysTick_Config(72000000 /100); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); nvicInfo.NVIC_IRQChannel = USART1_IRQn; nvicInfo.NVIC_IRQChannelPreemptionPriority = 0; nvicInfo.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvicInfo); return ret; }
void telemetryInit(uint32_t baudrate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_StructInit(&GPIO_InitStructure); USART_StructInit(&USART_InitStructure); DMA_StructInit(&DMA_InitStructure); //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); GPIO_InitStructure.GPIO_Pin = UART1_TX_PIN | UART1_RX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_PinAFConfig(UART1_GPIO, UART1_TX_PINSOURCE, GPIO_AF_USART1); GPIO_PinAFConfig(UART1_GPIO, UART1_RX_PINSOURCE, GPIO_AF_USART1); GPIO_Init(UART1_GPIO, &GPIO_InitStructure); // DMA TX Interrupt NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_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_Mode = USART_Mode_Rx | USART_Mode_Tx; //USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1, &USART_InitStructure); // Receive DMA into a circular buffer DMA_DeInit(DMA2_Stream5); DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)rx1Buffer; //DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = UART1_BUFFER_SIZE; //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_Medium; //DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull; //DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream5, &DMA_InitStructure); DMA_Cmd(DMA2_Stream5, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); rx1DMAPos = DMA_GetCurrDataCounter(DMA2_Stream5); // Transmit DMA DMA_DeInit(DMA2_Stream7); //DMA_InitStructure.DMA_Channel = DMA_Channel_4; //DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)tx1Buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //DMA_InitStructure.DMA_BufferSize = UART_BUFFER_SIZE; //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_Medium; //DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull; //DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream7, &DMA_InitStructure); DMA_SetCurrDataCounter(DMA2_Stream7, 0); DMA_ITConfig(DMA2_Stream7, DMA_IT_TC, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); USART_Cmd(USART1, ENABLE); //evrRegisterListener(telemetryListenerCB); }
/******************************************************************************* * Function Name : uart_open * Description : Open the UART port communication * Input : - Uart: Select the USART or the UART peripheral * : - BaudRate: Baud rate configuration * : - DmaBufSize: DMA buffer size * : - RxBufSize: Receive buffer size * : - TxBufSize: Transmit buffer size * : - HwCtrl: Hardware control options * : - Gpio: GPIO used for hardware control * Output : None * Return : 0 if OK, -1 in case of error *******************************************************************************/ int uart_open (const _Uart_Descriptor *Uart, u32 BaudRate, u8 DmaBufSize, u16 RxBufSize, u16 TxBufSize, u8 HwCtrl, const _Gpio_Descriptor *Gpio) { USART_InitTypeDef usart_init_structure; DMA_InitTypeDef dma_init_structure; NVIC_InitTypeDef NVIC_InitStructure; /* Init control variables and bufers */ if (*Uart->Ctrl) _sys_free(*Uart->Ctrl); if ((*Uart->Ctrl = _sys_malloc(sizeof(_Uart_Ctrl) + DmaBufSize + RxBufSize + TxBufSize)) == 0) return(-1); memset(*Uart->Ctrl, 0, sizeof(_Uart_Ctrl)); (*Uart->Ctrl)->DmaBufPtr = (char *)*Uart->Ctrl + sizeof(_Uart_Ctrl); (*Uart->Ctrl)->DmaBufSize = DmaBufSize; (*Uart->Ctrl)->iDma = DmaBufSize; (*Uart->Ctrl)->RxBufSize = RxBufSize; (*Uart->Ctrl)->RxBufPtr = (*Uart->Ctrl)->DmaBufPtr + (*Uart->Ctrl)->DmaBufSize; (*Uart->Ctrl)->TxBufSize = TxBufSize; (*Uart->Ctrl)->TxBufPtr = (*Uart->Ctrl)->RxBufPtr + (*Uart->Ctrl)->RxBufSize; (*Uart->Ctrl)->HwCtrl = HwCtrl; (*Uart->Ctrl)->Gpio = Gpio; #ifdef _UART_OS_SUPPORT (*Uart->Ctrl)->Event = SYS_EVT_INCOMING_DATA; (*Uart->Ctrl)->Task = sys_task_self(); #endif /* Enable peripheral clock */ (*Uart->RCC_APBxPeriphClockCmd)(Uart->RCC_APBxPeriph, ENABLE); /* Init GPIO */ gpio_set_function(Uart->TxGpio, Uart->GPIO_AF); gpio_set_function(Uart->RxGpio, Uart->GPIO_AF); gpio_set_mode(Uart->TxGpio, GPIO_MODE_AF, 0); gpio_set_mode(Uart->RxGpio, GPIO_MODE_AF, 0); if ((*Uart->Ctrl)->HwCtrl & (UART_HW_FLOW_CTRL_RX | UART_HALF_DUPLEX)) gpio_set_mode((*Uart->Ctrl)->Gpio, GPIO_FCT_OUT, 0); /* Init UART peripheral */ USART_DeInit(Uart->UARTx); USART_StructInit(&usart_init_structure); usart_init_structure.USART_BaudRate = BaudRate; if ((*Uart->Ctrl)->HwCtrl & UART_HW_FLOW_CTRL_TX) usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS; USART_Init(Uart->UARTx, &usart_init_structure); /* Configure DMA (if used) */ if ((*Uart->Ctrl)->DmaBufSize) { DMA_DeInit(Uart->DMAy_Streamx); DMA_StructInit(&dma_init_structure); dma_init_structure.DMA_Channel = Uart->DMA_Channel; dma_init_structure.DMA_PeripheralBaseAddr = (u32)&Uart->UARTx->DR; dma_init_structure.DMA_Memory0BaseAddr = (u32)(*Uart->Ctrl)->DmaBufPtr; dma_init_structure.DMA_BufferSize = (*Uart->Ctrl)->DmaBufSize; dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable; dma_init_structure.DMA_Mode = DMA_Mode_Circular; dma_init_structure.DMA_Priority = DMA_Priority_Medium; dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_Init(Uart->DMAy_Streamx, &dma_init_structure); DMA_ITConfig(Uart->DMAy_Streamx, DMA_IT_TC | DMA_IT_HT, ENABLE); DMA_Cmd(Uart->DMAy_Streamx, ENABLE); NVIC_EnableIRQ(Uart->DMAx_IRQn); // //NVIC_SetPriority((IRQn_Type)Uart->DMAx_IRQn, (uint32_t)129); NVIC_SetPriority((IRQn_Type)Uart->DMAx_IRQn, (1 << __NVIC_PRIO_BITS) -3); // USART_DMACmd(Uart->UARTx, USART_DMAReq_Rx, ENABLE); USART_ITConfig(Uart->UARTx, USART_IT_IDLE, ENABLE); } else USART_ITConfig(Uart->UARTx, USART_IT_RXNE, ENABLE); /* Enable IT and start peripheral */ //NVIC_InitStructure.NVIC_IRQChannel = Uart->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); // //NVIC_SetPriority((IRQn_Type)Uart->IRQn, (uint32_t)129); NVIC_SetPriority((IRQn_Type)Uart->IRQn, (1 << __NVIC_PRIO_BITS) -4); // USART_ITConfig(Uart->UARTx, USART_IT_TC, ENABLE); NVIC_EnableIRQ(Uart->IRQn); USART_Cmd(Uart->UARTx, ENABLE); while (!gpio_read(Uart->TxGpio)); return(0); }
int serial_open(char *name, unsigned int *subdevice) { unsigned int port; unsigned int baudrate; USART_InitTypeDef USART_config; GPIO_InitTypeDef GPIO_config; errno_r = 0; // Look up serial port number port = serial_name_to_port(name); if (port < 0) return port; // Pass up port number, if requested if (subdevice != NULL) *subdevice = port; // Extract baud rate from device name baudrate = atoi(name+5); // Turn on USART switch (port) { case 0 : #ifdef OLIMEX_STM32_P107 // Turn on peripheral clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Configure TX pin GPIO_config.GPIO_Pin = GPIO_Pin_6; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_config); // Configure RX pin GPIO_config.GPIO_Pin = GPIO_Pin_7; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_config); // Remap USART1 pins GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); #else // Turn on peripheral clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Configure TX pin GPIO_config.GPIO_Pin = GPIO_Pin_9; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_config); // Configure RX pin GPIO_config.GPIO_Pin = GPIO_Pin_10; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_config); #endif break; case 1 : #ifdef OLIMEX_STM32_P107 // Turn on peripheral clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Configure TX pin GPIO_config.GPIO_Pin = GPIO_Pin_5; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_config); // Configure RX pin GPIO_config.GPIO_Pin = GPIO_Pin_6; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_config); // Remap USART2 pins GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); #else // Turn on peripheral clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Configure TX pin GPIO_config.GPIO_Pin = GPIO_Pin_2; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_config); // Configure RX pin GPIO_config.GPIO_Pin = GPIO_Pin_3; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_config); #endif break; case 2 : #ifdef OLIMEX_STM32_P107 // Turn on peripheral clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // Configure TX pin GPIO_config.GPIO_Pin = GPIO_Pin_8; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_config); // Configure RX pin GPIO_config.GPIO_Pin = GPIO_Pin_9; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_config); // Remap USART3 pins GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); #else // Turn on peripheral clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // Configure TX pin GPIO_config.GPIO_Pin = GPIO_Pin_10; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_config); // Configure RX pin GPIO_config.GPIO_Pin = GPIO_Pin_11; GPIO_config.GPIO_Speed = GPIO_Speed_50MHz; GPIO_config.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_config); #endif break; default : errno_r = ENODEV; return -1; } // Configure USART USART_StructInit(&USART_config); USART_config.USART_BaudRate = baudrate; USART_Init(UARTS[port], &USART_config); // Enable USART USART_Cmd(UARTS[port], ENABLE); return 0; }
/************************************************************************* * Function Name: UartInit * Parameters: Int32U IrqSlot * * Return: Boolean * * Description: Init UART * *************************************************************************/ Boolean UartInit(UartNum_t Uart,Int32U IrqSlot) { volatile Int8U Tmp; USART_InitTypeDef UART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; USART_StructInit(&UART_InitStructure); UART_InitStructure.USART_Parity = USART_Parity_No; switch(Uart) { case UART_1: pUart1RxFifo = (pUartFifo_t)malloc(sizeof(UartFifo_t)); if(pUart1RxFifo == NULL) { return(FALSE); } pUart1TxFifo = (pUartFifo_t)malloc(sizeof(UartFifo_t)); if(pUart1TxFifo == NULL) { free(pUart1RxFifo); return(FALSE); } // Init receive and transmit FIFOs pUart1RxFifo->PopIndx = pUart1RxFifo->PushIndx = \ pUart1TxFifo->PopIndx = pUart1TxFifo->PushIndx = 0; // Release reset and enable clock USART_DeInit(USART1); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // GPIO Init // Enable GPIO clock and release reset RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE); GPIO_PinRemapConfig(GPIO_Remap_USART1,DISABLE); // Assign PA9 to UART1 (Tx) GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOA, &GPIO_InitStructure); // Assign PA10 to UART1 (Rx) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); // Init UART1 USART_Init(USART1,&UART_InitStructure); // Enable and configure the priority of the UART1 Update IRQ Channel NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART1_INTR_PRI; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ClearFlag(USART1, USART_FLAG_CTS | USART_FLAG_LBD | USART_FLAG_TXE | USART_FLAG_TC | USART_FLAG_RXNE | USART_FLAG_IDLE | USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE); // Enable UART1 interrupts USART_ITConfig(USART1,USART_IT_PE ,ENABLE); USART_ITConfig(USART1,USART_IT_TC ,DISABLE); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_ITConfig(USART1,USART_IT_IDLE,DISABLE); USART_ITConfig(USART1,USART_IT_LBD ,DISABLE); USART_ITConfig(USART1,USART_IT_CTS ,DISABLE); USART_ITConfig(USART1,USART_IT_ERR ,DISABLE); // Enable the UART1 USART_Cmd(USART1, ENABLE); break; case UART_2: pUart2RxFifo = (pUartFifo_t)malloc(sizeof(UartFifo_t)); if(pUart2RxFifo == NULL) { return(FALSE); } pUart2TxFifo = (pUartFifo_t)malloc(sizeof(UartFifo_t)); if(pUart2TxFifo == NULL) { free(pUart2RxFifo); return(FALSE); } // Init receive and transmit FIFOs pUart2RxFifo->PopIndx = pUart2RxFifo->PushIndx = \ pUart2TxFifo->PopIndx = pUart2TxFifo->PushIndx = 0; // Release reset and enable clock USART_DeInit(USART2); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // GPIO Init // Enable GPIO clock and release reset RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2,DISABLE); // Assign PA2 to UART2 (Tx) GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOA, &GPIO_InitStructure); // Assign PA3 to UART2 (Rx) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure); // Init UART2 USART_Init(USART2,&UART_InitStructure); // Enable and configure the priority of the UART2 Update IRQ Channel NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART2_INTR_PRI; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ClearFlag(USART2, USART_FLAG_CTS | USART_FLAG_LBD | USART_FLAG_TXE | USART_FLAG_TC | USART_FLAG_RXNE | USART_FLAG_IDLE | USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE); // Enable UART2 interrupts USART_ITConfig(USART2,USART_IT_PE ,ENABLE); USART_ITConfig(USART2,USART_IT_TXE ,ENABLE); USART_ITConfig(USART2,USART_IT_TC ,DISABLE); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); USART_ITConfig(USART2,USART_IT_IDLE,DISABLE); USART_ITConfig(USART2,USART_IT_LBD ,DISABLE); USART_ITConfig(USART2,USART_IT_CTS ,DISABLE); USART_ITConfig(USART2,USART_IT_ERR ,DISABLE); // Enable the UART2 USART_Cmd(USART2, ENABLE); break; case UART_3: pUart3RxFifo = (pUartFifo_t)malloc(sizeof(UartFifo_t)); if(pUart3RxFifo == NULL) { return(FALSE); } pUart3TxFifo = (pUartFifo_t)malloc(sizeof(UartFifo_t)); if(pUart3TxFifo == NULL) { free(pUart3RxFifo); return(FALSE); } // Init receive and transmit FIFOs pUart3RxFifo->PopIndx = pUart3RxFifo->PushIndx = \ pUart3TxFifo->PopIndx = pUart3TxFifo->PushIndx = 0; // Release reset and enable clock USART_DeInit(USART3); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // GPIO Init // Enable GPIO clock and release reset RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, DISABLE); GPIO_PinRemapConfig(GPIO_PartialRemap_USART3,ENABLE); // Assign PC10 to UART3 (Tx) GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOC, &GPIO_InitStructure); // Assign PC11 to UART3 (Rx) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOC, &GPIO_InitStructure); // Init UART3 USART_Init(USART3,&UART_InitStructure); // Enable and configure the priority of the UART3 Update IRQ Channel NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART3_INTR_PRI; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ClearFlag(USART3, USART_FLAG_CTS | USART_FLAG_LBD | USART_FLAG_TXE | USART_FLAG_TC | USART_FLAG_RXNE | USART_FLAG_IDLE | USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE); // Enable UART3 interrupts USART_ITConfig(USART3,USART_IT_PE ,ENABLE); USART_ITConfig(USART3,USART_IT_TXE ,DISABLE); USART_ITConfig(USART3,USART_IT_TC ,DISABLE); USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); USART_ITConfig(USART3,USART_IT_IDLE,DISABLE); USART_ITConfig(USART3,USART_IT_LBD ,DISABLE); USART_ITConfig(USART3,USART_IT_CTS ,DISABLE); USART_ITConfig(USART3,USART_IT_ERR ,DISABLE); // Enable the UART3 USART_Cmd(USART3, ENABLE); break; default: return(FALSE); } return(TRUE); }
void initUart() { RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); // Transmit pin. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9, GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Receive pin. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // Default clock structure works for us. USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(USART1, &USART_ClockInitStructure); // Default USART structure works for us. USART_InitTypeDef USART_InitStructure; USART_StructInit(&USART_InitStructure); USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); // // Disable data pushes from GPS. // usartTx("$PUBX,40,GGA,0,0,0,0*5A\r\n"); // usartTx("$PUBX,40,GGA,0,0,0,0*5A\r\n"); // Repeat first; timing work around. // usartTx("$PUBX,40,GLL,0,0,0,0*5C\r\n"); // usartTx("$PUBX,40,GSA,0,0,0,0*4E\r\n"); // usartTx("$PUBX,40,GSV,0,0,0,0*59\r\n"); // usartTx("$PUBX,40,RMC,0,0,0,0*47\r\n"); // usartTx("$PUBX,40,VTG,0,0,0,0*5E\r\n"); // // Keep just the recommended minimum flowing, slowly. // usartTx("$PUBX,40,GGA,0,15,0,0*6E\r\n"); // usartTx("$PUBX,40,GGA,0,15,0,0*6E\r\n"); // Repeat first; timing work around. // usartTx("$PUBX,40,GLL,0,15,0,0*68\r\n"); // usartTx("$PUBX,40,GSA,0,15,0,0*7A\r\n"); // usartTx("$PUBX,40,GSV,0,15,0,0*6D\r\n"); // usartTx("$PUBX,40,RMC,0,15,0,0*73\r\n"); // usartTx("$PUBX,40,VTG,0,15,0,0*6A\r\n"); // The first character is sometimes (always?) dropped, so send a few // buffer characters that can be safely discarded. gpsTxChar('\r'); gpsTxChar('\n'); gpsTxChar('\r'); gpsTxChar('\n'); // Disable all data pushes from GPS. gpsSetPushFreq("DTM", 0); gpsSetPushFreq("GBS", 0); gpsSetPushFreq("GGA", 0); gpsSetPushFreq("GLL", 0); gpsSetPushFreq("GPQ", 0); gpsSetPushFreq("GRS", 0); gpsSetPushFreq("GSA", 0); gpsSetPushFreq("GST", 0); gpsSetPushFreq("GSV", 0); gpsSetPushFreq("RMC", 0); gpsSetPushFreq("THS", 0); gpsSetPushFreq("VTG", 0); // Except the one which tells us what time it is. gpsSetPushFreq("ZDA", 2); // Now that it won't be noisy, enable the RX interrupt. NVIC_InitTypeDef NVIC_InitStructure = { .NVIC_IRQChannel = USART1_IRQn, .NVIC_IRQChannelPreemptionPriority = 1, .NVIC_IRQChannelSubPriority = 1, .NVIC_IRQChannelCmd = ENABLE, }; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // void RTC_IRQHandler(void) { if (RTC_GetFlagStatus(RTC_FLAG_SEC) != RESET) { RTC_ClearFlag(RTC_FLAG_SEC); gSecondFlag = 1; gSeconds = RTC_GetCounter(); } } void SysTick_Handler(void) { gBlinkTick++; if (gBlinkTick > 125) { gBlinkTick = 0; gBlinkStatus = !gBlinkStatus; } uint8_t btnPressed = 0; uint16_t btns = GPIO_ReadInputData(BTN_PORT) & BTN_ALL_PINS; if ((btns & BTN_MAPLE_PIN) == BTN_MAPLE_PIN) btnPressed = BTN_MAPLE; else if ((btns & BTN_DIM_PIN) == 0) btnPressed = BTN_DIM; else if ((btns & BTN_DN_PIN) == 0) btnPressed = BTN_DOWN; else if ((btns & BTN_UP_PIN) == 0) btnPressed = BTN_UP; else if ((btns & BTN_SET_PIN) == 0) btnPressed = BTN_SET; if (btnPressed && gButtonPending == btnPressed) { if (gButtonDebounce > 10) { // Button already activated, still held, do nothing. } else if (gButtonDebounce == 10) { // Transition! Set pressed button. gButtonPressed = btnPressed; gButtonDebounce++; } else { gButtonDebounce++; } } else { gButtonDebounce = 0; gButtonPending = btnPressed; } if (gDpTick > 0) { gDpTick--; if (gDpTick == 0) { GPIO_WriteBit(DP_PORT, DP_PIN, RESET); } } }
static void stm32_params2init(const struct uart_params *params, USART_InitTypeDef *USART_InitStruct) { /* TODO */ USART_StructInit(USART_InitStruct); USART_InitStruct->USART_BaudRate = params->baud_rate; }
static void hardware_init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; USART_InitTypeDef USART_InitStructure; /* Configure GPIO */ GPIO_InitStructure.GPIO_Pin = PLAY_BUTTON_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = HIT_BUTTON_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(LAMP_PORT, LAMP_PIN); GPIO_InitStructure.GPIO_Pin = LAMP_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LAMP_PORT, &GPIO_InitStructure); GPIO_SetBits(LOCK_PORT, LOCK_PIN); GPIO_InitStructure.GPIO_Pin = LOCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LOCK_PORT, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2); EXTI_InitStructure.EXTI_Line = EXTI_Line2; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable USART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure 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); /* USART configured using USART_StructInit(): - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_StructInit(&USART_InitStructure); /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); USART_ITConfig( USART1, USART_IT_RXNE, ENABLE ); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); USART_Cmd( USART1, ENABLE ); }
/** * @brief Main program * @param None * @retval None */ int main(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; BufferInit(&TxRingBuf); BufferInit(&RxRingBuf); /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f401xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* TIM Configuration */ //TIM_Config(); /* ----------------------------------------------------------------------- TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles. In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM3CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK = HCLK / 2 = SystemCoreClock /2 To get TIM3 counter clock at 14 MHz, the prescaler is computed as follows: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /14 MHz) - 1 To get TIM3 output clock at 21 KHz, the period (ARR)) is computed as follows: ARR = (TIM3 counter clock / TIM3 output clock) - 1 = 665 TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50% TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5% TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25% TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5% Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() function to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. ----------------------------------------------------------------------- */ //=========================================================== //UART config //=========================================================== USART_InitTypeDef UartHandle; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_ClockInitTypeDef USART_ClockInitStruct; RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); USART_StructInit(&UartHandle); USART_ClockStructInit(&USART_ClockInitStruct); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOD, &GPIO_InitStructure); USART_Init(USART2,&UartHandle); USART_ClockInit(USART2,&USART_ClockInitStruct); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); USART_ITConfig(USART2, USART_IT_TC,ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART2, ENABLE); Delay(40); Sevenseg_Setup(); Delay(40); USART_SendData(USART2,0xaaaa); //GPIO_SetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1); GPIO_SetBits(GPIOD,GPIO_Pin_0); //====================================================== //---------------------------------------------------- //======================================================== // /* Compute the prescaler value */ // PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1; // // /* Time base configuration */ // TIM_TimeBaseStructure.TIM_Period = 5000; // TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; // TIM_TimeBaseStructure.TIM_ClockDivision = 0; // TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // // TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // // /* PWM1 Mode configuration: Channel1 */ // TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // TIM_OCInitStructure.TIM_Pulse = CCR1_Val; // TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // // TIM_OC1Init(TIM3, &TIM_OCInitStructure); // // TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); // // // /* PWM1 Mode configuration: Channel2 */ // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // TIM_OCInitStructure.TIM_Pulse = CCR2_Val; // // TIM_OC2Init(TIM3, &TIM_OCInitStructure); // // TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); // // /* PWM1 Mode configuration: Channel3 */ // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // TIM_OCInitStructure.TIM_Pulse = CCR3_Val; // // TIM_OC3Init(TIM3, &TIM_OCInitStructure); // // TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); // // /* PWM1 Mode configuration: Channel4 */ // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // TIM_OCInitStructure.TIM_Pulse = CCR4_Val; // // TIM_OC4Init(TIM3, &TIM_OCInitStructure); // // TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); // // TIM_ARRPreloadConfig(TIM3, ENABLE); // // /* TIM3 enable counter */ // TIM_Cmd(TIM3, ENABLE); int bts = 0; Sevenseg_Send(0x33); while (1) { if(RxRingBuf.length > 10) { int i; int ntosend = RxRingBuf.length; for(i = 0; i < ntosend;i++) { bts = BufferRead(&RxRingBuf); //if(bts == -1) break; USART2_SendByte(bts); } /* while(TxRingBuf.length > 0) { if((TxRingBuf.length > 0) && (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == SET)) { BufferSend(&TxRingBuf); } } */ } /* GPIO_ResetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1); GPIO_ResetBits(GPIOA,GPIO_Pin_0); //GPIO_ToggleBits(GPIOB, GPIO_Pin_0); GPIO_SetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1); GPIO_SetBits(GPIOA,GPIO_Pin_0); */ //USART_SendData(USART2,0x0051); //USART_SendData(USART2,0x003b); } }
int do_set_rs232cfg(struct rs232_param *rs232cfg, int intno) { USART_TypeDef *uartbase; USART_InitTypeDef USART_InitStructure; if (NULL==rs232cfg) return RT_ERROR; USART_StructInit(&USART_InitStructure); switch (rs232cfg->databits) { case 8: USART_InitStructure.USART_WordLength = USART_WordLength_8b; break; case 9: USART_InitStructure.USART_WordLength = USART_WordLength_9b; break; default: USART_InitStructure.USART_WordLength = USART_WordLength_8b; break; } switch (rs232cfg->stopbits) { case 1: USART_InitStructure.USART_StopBits = USART_StopBits_1; break; case 2: USART_InitStructure.USART_StopBits = USART_StopBits_2; break; default: USART_InitStructure.USART_StopBits = USART_StopBits_1; break; } switch (rs232cfg->paritybit) { 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: USART_InitStructure.USART_Parity = USART_Parity_No; break; } /* 应该检查数据的合法性, 现在没有做检查!!!!! */ USART_InitStructure.USART_BaudRate = rs232cfg->baudrate; switch (intno) { case 0: uartbase = USART1; break; case 1: uartbase = USART2; break; default: return RT_ERROR; } USART_Init(uartbase, &USART_InitStructure); return RT_EOK; }
USART2Class::USART2Class(){ m_queue = xQueueCreate(TX_BUFFERSIZE,sizeof(char)); vQueueAddToRegistry(m_queue,"u2tx"); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); GPIO_InitTypeDef pd5def; GPIO_StructInit(&pd5def); pd5def.GPIO_Pin = GPIO_Pin_5; pd5def.GPIO_Mode = GPIO_Mode_AF; pd5def.GPIO_OType = GPIO_OType_PP; pd5def.GPIO_PuPd = GPIO_PuPd_NOPULL; pd5def.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOD,&pd5def); GPIO_InitTypeDef pd6def; GPIO_StructInit(&pd6def); pd6def.GPIO_Pin = GPIO_Pin_6; pd6def.GPIO_Mode = GPIO_Mode_AF; pd6def.GPIO_PuPd = GPIO_PuPd_NOPULL; pd6def.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOD,&pd6def); GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); USART_InitTypeDef usart2; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); USART_StructInit(&usart2); usart2.USART_BaudRate = 115200; usart2.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart2.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; usart2.USART_Parity = USART_Parity_No; usart2.USART_StopBits = USART_StopBits_1; usart2.USART_WordLength = USART_WordLength_8b; USART_Init(USART2,&usart2); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE); DMA_InitTypeDef dma1_6; DMA_StructInit(&dma1_6); dma1_6.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR); dma1_6.DMA_Memory0BaseAddr = (uint32_t)m_txBuf; dma1_6.DMA_DIR = DMA_DIR_MemoryToPeripheral; dma1_6.DMA_BufferSize = 1; dma1_6.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma1_6.DMA_MemoryInc = DMA_MemoryInc_Enable; dma1_6.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; dma1_6.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; dma1_6.DMA_Mode = DMA_Mode_Normal; dma1_6.DMA_Priority = DMA_Priority_High; dma1_6.DMA_Channel = DMA_Channel_4; DMA_Init(DMA1_Stream6,&dma1_6); DMA_InitTypeDef dma1_5; DMA_StructInit(&dma1_5); dma1_5.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR); dma1_5.DMA_Memory0BaseAddr = (uint32_t)m_rxBuf; dma1_5.DMA_DIR = DMA_DIR_PeripheralToMemory; dma1_5.DMA_BufferSize = RX_BUFFERSIZE; dma1_5.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma1_5.DMA_MemoryInc = DMA_MemoryInc_Enable; dma1_5.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; dma1_5.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; dma1_5.DMA_Mode = DMA_Mode_Circular; dma1_5.DMA_Priority = DMA_Priority_Low; dma1_5.DMA_Channel = DMA_Channel_4; DMA_Init(DMA1_Stream5,&dma1_5); DMA_Cmd(DMA1_Stream5,ENABLE); for(int i=0;i<RX_BUFFERSIZE;i++){ m_rxBuf[i] = 0; } USART_DMACmd(USART2,USART_DMAReq_Tx|USART_DMAReq_Rx, ENABLE); USART_Cmd(USART2, ENABLE); }
void gpsCLI() { USART_InitTypeDef USART_InitStructure; uint8_t gpsQuery = 'x'; uint8_t validQuery = false; cliBusy = true; cliPrint("\nEntering GPS CLI....\n\n"); while(true) { cliPrint("GPS CLI -> "); while ((cliAvailable() == false) && (validQuery == false)); if (validQuery == false) gpsQuery = cliRead(); cliPrint("\n"); switch(gpsQuery) { /////////////////////////// case 'a': // GPS Installation Data cliPrint("\n"); switch(eepromConfig.gpsType) { /////////////// case NO_GPS: cliPrint("No GPS Installed....\n\n"); break; /////////////// case MEDIATEK_3329_BINARY: cliPrint("MediaTek 3329 GPS installed, Binary Mode....\n\n"); break; /////////////// case MEDIATEK_3329_NMEA: cliPrint("MediaTek 3329 GPS Installed, NMEA Mode....\n\n"); break; /////////////// case UBLOX: cliPrint("UBLOX GPS Installed, Binary Mode....\n\n"); break; /////////////// } cliPrintF("GPS Baud Rate: %6ld\n\n", eepromConfig.gpsBaudRate); validQuery = false; break; /////////////////////////// case 'x': cliPrint("\nExiting GPS CLI....\n\n"); cliBusy = false; return; break; /////////////////////////// case 'A': // Set GPS Installed State to False eepromConfig.gpsType = NO_GPS; gpsQuery = 'a'; validQuery = true; break; /////////////////////////// case 'B': // Set GPS Type to MediaTek 3329 Binary eepromConfig.gpsType = MEDIATEK_3329_BINARY; initGPS(); gpsQuery = 'a'; validQuery = true; break; /////////////////////////// case 'C': // Set GPS Type to MediaTek 3329 NMEA eepromConfig.gpsType = MEDIATEK_3329_NMEA; initGPS(); gpsQuery = 'a'; validQuery = true; break; /////////////////////////// case 'D': // Set GPS Type to UBLOX Binary eepromConfig.gpsType = UBLOX; initGPS(); gpsQuery = 'a'; validQuery = true; break; /////////////////////////// case 'S': // Read GPS Baud Rate eepromConfig.gpsBaudRate = (uint16_t)readFloatCLI(); USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = eepromConfig.gpsBaudRate; USART_Init(USART2, &USART_InitStructure); gpsQuery = 'a'; validQuery = true; break; /////////////////////////// case 'W': // Write EEPROM Parameters cliPrint("\nWriting EEPROM Parameters....\n\n"); writeEEPROM(); break; /////////////////////////// case '?': cliPrint("\n"); cliPrint("'a' Display GPS Installation Data 'A' Set GPS Type to No GPS\n"); cliPrint(" 'B' Set GPS Type to MediaTek 3329 Binary\n"); cliPrint(" 'C' Set GPS Type to MediaTek 3329 NMEA\n"); cliPrint(" 'D' Set GPS Type to UBLOX\n"); cliPrint(" 'S' Set GPS Baud Rate\n"); cliPrint(" 'W' Write EEPROM Parameters\n"); cliPrint("'x' Exit GPS CLI '?' Command Summary\n"); cliPrint("\n"); break; /////////////////////////// } } }
void USART_Configuration(u8 PORT, u32 baudrate) { USART_InitTypeDef USART_InitStructure; USART_StructInit(&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; if( PORT == USART_DXL ) { Baudrate_DXL = baudrate; USART_DeInit(USART1); /* Configure the USART1 */ USART_Init(USART1, &USART_InitStructure); /* Enable USART1 Receive and Transmit interrupts */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART1, USART_IT_TC, ENABLE); /* Enable the USART1 */ USART_Cmd(USART1, ENABLE); } else if( PORT == USART_ZIGBEE ) { Baudrate_ZIGBEE = baudrate; USART_DeInit(UART5); /* Configure the UART5 */ USART_Init(UART5, &USART_InitStructure); /* Enable UART5 Receive and Transmit interrupts */ USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); /* Enable the UART5 */ USART_Cmd(UART5, ENABLE); } else if( PORT == USART_PC ) { Baudrate_PC = baudrate; USART_DeInit(USART3); /* Configure the USART3 */ USART_Init(USART3, &USART_InitStructure); /* Enable USART3 Receive and Transmit interrupts */ USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART3, USART_IT_TC, ENABLE); /* Enable the USART3 */ USART_Cmd(USART3, ENABLE); } }
int uart_open (uint8_t uart, uint32_t baud, uint32_t flags) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; if (uart == 1) { // get things to a known state USART_DeInit(USART1); // Enable clock for GPIOA RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); // Turn on clocks for USART1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // DEBUG GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed; GPIO_Init(GPIOB, &GPIO_InitStructure); // Configure TX pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed; GPIO_Init(GPIOB, &GPIO_InitStructure); // Configure RX pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO_Mode_IN;//GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed; GPIO_Init(GPIOB, &GPIO_InitStructure); #ifdef HWFLOWCTRL // Configure CTS pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure RTS pin -- software controlled GPIO_WriteBit(GPIOA, GPIO_Pin_12, 1); //TODO // nRTS disabled GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif // Configure the UART USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = baud; #ifdef HWFLOWCTRL USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS; #else USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; #endif USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); // Enable RX Interrupt. TX interrupt enabled in send routine USART_ClearITPendingBit(USART1, USART_IT_RXNE); //disable Transmit Data Register empty interrupt USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //enable Receive Data register not empty interrupt USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); InitQueue(&UART1_RXq); InitQueue(&UART1_TXq); // Configure NVIC /* Configure the NVIC Preemption Priority Bits */ //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // Enable USART1 USART_Cmd(USART1, ENABLE); #ifdef HWFLOWCTRL // nRTS enabled GPIO_WriteBit(GPIOA, GPIO_Pin_12, 0); #endif return 0; } return 1; // only handle UART2 }