//-------------------------------------------------------------- // init aller UARTs //-------------------------------------------------------------- void UB_Uart_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; UART_NAME_t nr; for(nr=0;nr<UART_ANZ;nr++) { // Clock enable der TX und RX Pins RCC_AHB1PeriphClockCmd(UART[nr].TX.CLK, ENABLE); RCC_AHB1PeriphClockCmd(UART[nr].RX.CLK, ENABLE); // Clock enable der UART if((UART[nr].UART==USART1) || (UART[nr].UART==USART6)) { RCC_APB2PeriphClockCmd(UART[nr].CLK, ENABLE); } else { RCC_APB1PeriphClockCmd(UART[nr].CLK, ENABLE); } // UART Alternative-Funktions mit den IO-Pins verbinden GPIO_PinAFConfig(UART[nr].TX.PORT,UART[nr].TX.SOURCE,UART[nr].AF); GPIO_PinAFConfig(UART[nr].RX.PORT,UART[nr].RX.SOURCE,UART[nr].AF); // UART als Alternative-Funktion mit PushPull 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_UP; // TX-Pin GPIO_InitStructure.GPIO_Pin = UART[nr].TX.PIN; GPIO_Init(UART[nr].TX.PORT, &GPIO_InitStructure); // RX-Pin GPIO_InitStructure.GPIO_Pin = UART[nr].RX.PIN; GPIO_Init(UART[nr].RX.PORT, &GPIO_InitStructure); // Oversampling USART_OverSampling8Cmd(UART[nr].UART, ENABLE); // init mit Baudrate, 8Databits, 1Stopbit, keine Parität, kein RTS+CTS USART_InitStructure.USART_BaudRate = UART[nr].BAUD; 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(UART[nr].UART, &USART_InitStructure); // UART enable USART_Cmd(UART[nr].UART, ENABLE); // RX-Interrupt enable USART_ITConfig(UART[nr].UART, USART_IT_RXNE, ENABLE); // enable UART Interrupt-Vector NVIC_InitStructure.NVIC_IRQChannel = UART[nr].INT; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // RX-Puffer vorbereiten UART_RX[nr].rx_buffer[0]=RX_END_CHR; UART_RX[nr].wr_ptr=0; UART_RX[nr].status=RX_EMPTY; } }
void Usart::resume() { USART_Cmd(USARTx,ENABLE); }
/* * 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; }
void Serial_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); /* 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); //VS1003 PE12, PE13, PE14 CS,SI,CLK GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //PD13 VS1003 RST GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PC6 VS1003 XDCS GPIO_Init(GPIOC, &GPIO_InitStructure); //PENIRQ, SO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOE, &GPIO_InitStructure); /* USART1 configuration ------------------------------------------------------*/ // USART1 configured as follow: USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure the USART1 */ USART_Init(USART1, &USART_InitStructure); /* Enable the USART Transmoit interrupt: this interrupt is generated when the USART1 transmit data register is empty */ USART_ITConfig(USART1, USART_IT_TXE, ENABLE); /* Enable the USART Receive interrupt: this interrupt is generated when the USART1 receive data register is not empty */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* Enable USART1 */ USART_Cmd(USART1, ENABLE); }
/*********************************************************** * Function: * Description: 配置控电管脚,配置对应的串口设备uart4 * Input: * Input: * Output: * Return: * Others: ***********************************************************/ static rt_err_t m66_init( rt_device_t dev ) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART4, ENABLE ); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GSM_PWR_PIN; GPIO_Init( GSM_PWR_PORT, &GPIO_InitStructure ); GPIO_ResetBits( GSM_PWR_PORT, GSM_PWR_PIN ); GPIO_InitStructure.GPIO_Pin = GSM_TERMON_PIN; GPIO_Init( GSM_TERMON_PORT, &GPIO_InitStructure ); GPIO_ResetBits( GSM_TERMON_PORT, GSM_TERMON_PIN ); /* RESET在开机过程不需要做任何时序配合(和通常CPU 的 reset不同)。 建议该管脚接OC输出的GPIO,开机时 OC 输出高阻。 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GSM_RST_PIN; GPIO_Init( GSM_RST_PORT, &GPIO_InitStructure ); GPIO_SetBits( GSM_RST_PORT, GSM_RST_PIN ); GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init( GPIOD, &GPIO_InitStructure ); /*uart4 管脚设置*/ /* 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_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GSM_TX_PIN | GSM_RX_PIN; GPIO_Init( GSM_GPIO, &GPIO_InitStructure ); GPIO_PinAFConfig( GSM_GPIO, GSM_TX_PIN_SOURCE, GPIO_AF_UART4 ); GPIO_PinAFConfig( GSM_GPIO, GSM_RX_PIN_SOURCE, GPIO_AF_UART4 ); NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); USART_InitStructure.USART_BaudRate = 57600; 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( UART4, &USART_InitStructure ); /* Enable USART */ USART_Cmd( UART4, ENABLE ); USART_ITConfig( UART4, USART_IT_RXNE, ENABLE ); return RT_EOK; }
/*初始化*/ static rt_err_t dev_gps_init( rt_device_t dev ) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE ); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART5, ENABLE ); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPS_PWR_PIN; GPIO_Init( GPS_PWR_PORT, &GPIO_InitStructure ); GPIO_ResetBits( GPS_PWR_PORT, GPS_PWR_PIN ); if(Module_3017A!=GPS_MODULE_TYPE) { //#ifdef HC_595_CONTROL // 2013-4-20 更改PCB 用PD4 : GPS 天线开路 PB6 : GPS 天线短路 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // IN //------------------- PD4 ----------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //GPS 天线开路 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(GPIOD, &GPIO_InitStructure); //------------------- PB6 ----------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //GPS 天线短路 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(GPIOB, &GPIO_InitStructure); //#endif } /*uart5 管脚设置*/ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init( GPIOC, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init( GPIOD, &GPIO_InitStructure ); GPIO_PinAFConfig( GPIOC, GPIO_PinSource12, GPIO_AF_UART5 ); GPIO_PinAFConfig( GPIOD, GPIO_PinSource2, GPIO_AF_UART5 ); /*NVIC 设置*/ NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); gps_baud( 9600 ); USART_Cmd( UART5, ENABLE ); USART_ITConfig( UART5, USART_IT_RXNE, ENABLE ); GPIO_ResetBits( GPIOD, GPIO_Pin_10 ); return RT_EOK; }
/* * Uart 3 initialisieren */ void Xbee::Init(void) { /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure2; USART_InitTypeDef USART_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable UART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); /* 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_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure2); 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 configuration */ USART_Init(USART2, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/*..........................................................................*/ uint8_t QS_onStartup(void const *arg) { static uint8_t qsBuf[QS_BUF_SIZE]; /* buffer for Quantum Spy */ QS_initBuf(qsBuf, sizeof(qsBuf)); /* enable USART2 and GPIOA/AFIO clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); /* configure GPIOD.5 as push-pull... */ GPIO_InitTypeDef gpio_init; gpio_init.GPIO_Pin = GPIO_Pin_5; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &gpio_init); /* configure GPIOD.6 as input floating... */ gpio_init.GPIO_Pin = GPIO_Pin_6; gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &gpio_init); USART_InitTypeDef usart_init; usart_init.USART_BaudRate = QS_BAUD_RATE; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No ; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart_init.USART_Mode = USART_Mode_Tx; USART_Init(USART2, &usart_init); USART_ClockInitTypeDef usart_clk_init; usart_clk_init.USART_Clock = USART_Clock_Disable; usart_clk_init.USART_CPOL = USART_CPOL_Low; usart_clk_init.USART_CPHA = USART_CPHA_2Edge; usart_clk_init.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART2, &usart_clk_init); USART_Cmd(USART2, ENABLE); /* enable USART2 */ QS_tickPeriod_ = (QSTimeCtr)(SystemFrequency_SysClk / BSP_TICKS_PER_SEC); QS_tickTime_ = QS_tickPeriod_; /* to start the timestamp at zero */ /* setup the QS filters... */ QS_FILTER_ON(QS_ALL_RECORDS); // QS_FILTER_OFF(QS_QEP_STATE_EMPTY); // QS_FILTER_OFF(QS_QEP_STATE_ENTRY); // QS_FILTER_OFF(QS_QEP_STATE_EXIT); // QS_FILTER_OFF(QS_QEP_STATE_INIT); // QS_FILTER_OFF(QS_QEP_INIT_TRAN); // QS_FILTER_OFF(QS_QEP_INTERN_TRAN); // QS_FILTER_OFF(QS_QEP_TRAN); // QS_FILTER_OFF(QS_QEP_IGNORED); QS_FILTER_OFF(QS_QF_ACTIVE_ADD); QS_FILTER_OFF(QS_QF_ACTIVE_REMOVE); QS_FILTER_OFF(QS_QF_ACTIVE_SUBSCRIBE); QS_FILTER_OFF(QS_QF_ACTIVE_UNSUBSCRIBE); QS_FILTER_OFF(QS_QF_ACTIVE_POST_FIFO); QS_FILTER_OFF(QS_QF_ACTIVE_POST_LIFO); QS_FILTER_OFF(QS_QF_ACTIVE_GET); QS_FILTER_OFF(QS_QF_ACTIVE_GET_LAST); QS_FILTER_OFF(QS_QF_EQUEUE_INIT); QS_FILTER_OFF(QS_QF_EQUEUE_POST_FIFO); QS_FILTER_OFF(QS_QF_EQUEUE_POST_LIFO); QS_FILTER_OFF(QS_QF_EQUEUE_GET); QS_FILTER_OFF(QS_QF_EQUEUE_GET_LAST); QS_FILTER_OFF(QS_QF_MPOOL_INIT); QS_FILTER_OFF(QS_QF_MPOOL_GET); QS_FILTER_OFF(QS_QF_MPOOL_PUT); QS_FILTER_OFF(QS_QF_PUBLISH); QS_FILTER_OFF(QS_QF_NEW); QS_FILTER_OFF(QS_QF_GC_ATTEMPT); QS_FILTER_OFF(QS_QF_GC); // QS_FILTER_OFF(QS_QF_TICK); QS_FILTER_OFF(QS_QF_TIMEEVT_ARM); QS_FILTER_OFF(QS_QF_TIMEEVT_AUTO_DISARM); QS_FILTER_OFF(QS_QF_TIMEEVT_DISARM_ATTEMPT); QS_FILTER_OFF(QS_QF_TIMEEVT_DISARM); QS_FILTER_OFF(QS_QF_TIMEEVT_REARM); QS_FILTER_OFF(QS_QF_TIMEEVT_POST); QS_FILTER_OFF(QS_QF_INT_LOCK); QS_FILTER_OFF(QS_QF_INT_UNLOCK); QS_FILTER_OFF(QS_QF_ISR_ENTRY); QS_FILTER_OFF(QS_QF_ISR_EXIT); // QS_FILTER_OFF(QS_QK_MUTEX_LOCK); // QS_FILTER_OFF(QS_QK_MUTEX_UNLOCK); QS_FILTER_OFF(QS_QK_SCHEDULE); return (uint8_t)1; /* return success */ }
/** * @brief Main program. * @param None * @retval None */ int main(void) { uint8_t *buffptr; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ if (SysTick_Config(SystemCoreClock / 1000)) { /* error */ while (1); } /* Configure clock GPIO, USARTs */ RCC_Configuration(); /* Configure GPIO Pin Tx/Rx for Usart communication */ GPIO_Configuration(); /* Configure NVIC */ NVIC_Configuration(); /* 8xUSARTs configuration --------------------------------------------------*/ /* 8xUSARTs configured as follow: - 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_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; /* Prepare all uart to receive a data packet */ for(UsartIndex = 0; UsartIndex < USART_MAX_INDEX; UsartIndex++) { USART_Init(UsartInstance[UsartIndex], &USART_InitStructure); /* Enable 8xUSARTs Receive interrupts */ USART_ITConfig(UsartInstance[UsartIndex], USART_IT_RXNE, ENABLE); /* Enable the 8xUSARTs */ USART_Cmd(UsartInstance[UsartIndex], ENABLE); } /* Infinite Loop */ while(1) { /* Set aRxBuffer to 0 */ memset(aRxBuffer, 0x0, BUFFER_SIZE * USART_MAX_INDEX); /* When data has been transferred start the next transfer */ for(UsartIndex = 0; UsartIndex < USART_MAX_INDEX; UsartIndex++) { /* Initialize the global variable to handle the data transfer */ TxCounter = 0; ReceiveState = 0; /* Delay to let time to see counter incrementation on the screen */ Delay(2000); /* Set the data buffptr to use for the transfer */ if (UsartIndex == 0) { buffptr = aTxBuffer; } else { buffptr = aRxBuffer[UsartIndex]; } while(TxCounter < BUFFER_SIZE) { /* Send one byte from USART1 to USARTx */ USART_SendData(UsartInstance[UsartIndex], buffptr[TxCounter++]); /* Loop until USART1 DR register is empty */ while(USART_GetFlagStatus(UsartInstance[UsartIndex], USART_FLAG_TXE) == RESET) {} } while(ReceiveState == 0); /* Compares two buffers */ if(UsartIndex == (USART_MAX_INDEX - 1)) { if( Buffercmp((uint8_t*) aRxBuffer[0], (uint8_t*) aTxBuffer, BUFFER_SIZE)) { /* Error */ while(1); } } else { if( Buffercmp((uint8_t*) aRxBuffer[UsartIndex+1], (uint8_t*) aTxBuffer, BUFFER_SIZE)) { /* Error */ while(1); } } ReceiveState = 0; } /* Insert delay */ Delay(2000); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* USARTy and USARTz configuration ------------------------------------------------------*/ /* USARTy and USARTz configured as follow: - BaudRate = 230400 baud - Word Length = 8 Bits - One Stop Bit - Even parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 230400; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_Even; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure USARTy */ USART_Init(USARTy, &USART_InitStructure); /* Configure USARTz */ USART_Init(USARTz, &USART_InitStructure); /* Enable the USARTy */ USART_Cmd(USARTy, ENABLE); /* Enable the USARTz */ USART_Cmd(USARTz, ENABLE); while(TxCounter < TxBufferSize) { /* Send one byte from USARTy to USARTz */ USART_SendData(USARTy, TxBuffer[TxCounter++]); /* Loop until USARTy DR register is empty */ while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET) { } /* Loop until the USARTz Receive Data Register is not empty */ while(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) == RESET) { } /* Store the received byte in RxBuffer */ RxBuffer[RxCounter++] = (USART_ReceiveData(USARTz) & 0x7F); } /* Check the received data with the send ones */ TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize); /* TransferStatus = PASSED, if the data transmitted from USARTy and received by USARTz are the same */ /* TransferStatus = FAILED, if the data transmitted from USARTy and received by USARTz are different */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* Configure the SPI */ SPI_Configuration(); /* USARTy configuration ------------------------------------------------------*/ /* USARTy configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled - USART Clock Enabled - USART CPOL: Clock is active High - USART CPHA: Data is captured on the second edge - USART LastBit: The clock pulse of the last data bit is output to the SCLK pin */ USART_ClockInitStructure.USART_Clock = USART_Clock_Enable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_High; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable; USART_ClockInit(USARTy, &USART_ClockInitStructure); 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(USARTy, &USART_InitStructure); /* Configure the USARTy */ USART_Init(USARTy, &USART_InitStructure); /* Enable the USARTy */ USART_Cmd(USARTy, ENABLE); while(NbrOfDataToRead2--) { /* Write one byte in the USARTy Transmit Data Register */ USART_SendData(USARTy, TxBuffer1[TxCounter1++]); /* Wait until end of transmit */ while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET) { } /* Wait the byte is entirtly received by SPIy */ while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET) { } /* Store the received byte in the RxBuffer2 */ RxBuffer2[RxCounter2++] = SPI_I2S_ReceiveData(SPIy); } /* Clear the USARTy Data Register */ USART_ReceiveData(USARTy); while(NbrOfDataToRead1--) { /* Wait until end of transmit */ while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE)== RESET) { } /* Write one byte in the SPIy Transmit Data Register */ SPI_I2S_SendData(SPIy, TxBuffer2[TxCounter2++]); /* Send a Dummy byte to generate clock to slave */ USART_SendData(USARTy, DYMMY_BYTE); /* Wait until end of transmit */ while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET) { } /* Wait the byte is entirtly received by USARTy */ while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET) { } /* Store the received byte in the RxBuffer1 */ RxBuffer1[RxCounter1++] = USART_ReceiveData(USARTy); } /* Check the received data with the send ones */ TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1); /* TransferStatus = PASSED, if the data transmitted from USARTy and received by SPIy are the same */ /* TransferStatus = FAILED, if the data transmitted from USARTy and received by SPIy are different */ TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2); /* TransferStatus = PASSED, if the data transmitted from SPIy and received by USARTy are the same */ /* TransferStatus = FAILED, if the data transmitted from SPIy and received by USARTy are different */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { RCC_Configuration(); GPIO_Configuration(); CAN_Configuration(); NVIC_Configuration(); //TIM4_Configuration(); //CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); //CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE); USART_Config(USART1); USART_Cmd(USART1, ENABLE); USART_Config(USART3); USART_Cmd(USART3, ENABLE); //GPIO_ResetBits(GPIOB, GPIO_Pin_8|GPIO_Pin_9); //GPIO_SetBits(GPIOB, GPIO_Pin_6|GPIO_Pin_7); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); while (1) { if(send_data_flag==TRUE) { send_data_flag=FALSE; USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); TxMessage.ExtId=0x111215; TxMessage.RTR=CAN_RTR_DATA; TxMessage.IDE=CAN_ID_EXT; TxMessage.DLC=8; if(distance_valid_flag==TRUE) { TxMessage.Data[0]=0xaa; } else { TxMessage.Data[0]=0xff; } TxMessage.Data[1]=y_bias; TxMessage.Data[2]=0x00; TxMessage.Data[3]=0x00; TxMessage.Data[4]=0x00; TxMessage.Data[5]=0x00; TxMessage.Data[6]=0x00; TxMessage.Data[7]=y_bias; CAN_Transmit(CAN1, &TxMessage); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } // for(n=0;n<8;n++) // { // TxMessage.Data[n]=RxBuffer1[n]; // } // CAN_Transmit(CAN1, &TxMessage); // // } } }
/** * Initialize the UART : set pins, enable clocks, set uart, enable interrupt * */ static inline void init(uint32_t baudrate) { GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; NVIC_InitTypeDef NVIC_InitStructure; //General settings of pins TX/RX GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; switch (USART_ID) { case 1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins B6 (TX) and B7 (RX) GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; break; case 2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // Pins A2 (TX) and A3 (RX) GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; break; case 3: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // Pins D8 (TX) and D9 (RX) GPIO_Init(GPIOD, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; break; case 4: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // Pins C10 (TX) and C11 (RX) GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; break; case 5: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; // Pin C12 (TX) GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; // Pin D2 (RX) GPIO_Init(GPIOD, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; break; case 6: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins C6 (TX) and C7 (RX) GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; break; } //UART setting USART_InitStruct.USART_BaudRate = baudrate; USART_InitStruct.USART_WordLength = USART_WordLength_8b; // octet comme taille élémentaore (standard) USART_InitStruct.USART_StopBits = USART_StopBits_1; // bit de stop = 1 (standard) USART_InitStruct.USART_Parity = USART_Parity_No; // pas de bit de parité (standard) USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // pas de controle de flux (standard) USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USARTx, &USART_InitStruct); USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); //Setting of interrupt NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //Enable UART USART_Cmd(USARTx, ENABLE); }
inline void Usart::pause() { USART_Cmd(USARTx,DISABLE); }
/* This funcion initializes the USART1 peripheral * * Arguments: baudrate --> the baudrate at which the USART is * supposed to operate */ void init_USART1(uint32_t baudrate){ /* 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); }
int32_t init(USART_TypeDef * USARTxx, GPIO_TypeDef * GPIOx, uint16_t TxPin, uint16_t RxPin, int32_t baudrate) { this->USARTx = USARTxx; #if defined(USART1) if( USARTxx == USART1 ) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); } #endif #if defined(USART2) //TODO: extend #endif #if defined(GPIOA) if( GPIOx == GPIOA ) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); } #endif GPIO_PinAFConfig(GPIOx, RxPin, GPIO_AF_1); GPIO_PinAFConfig(GPIOx, TxPin, GPIO_AF_1); GPIO_InitTypeDef GPIOInit; GPIOInit.GPIO_Mode = GPIO_Mode_AF; GPIOInit.GPIO_OType = GPIO_OType_PP; GPIOInit.GPIO_Pin = RxPin | TxPin; GPIOInit.GPIO_PuPd = GPIO_PuPd_UP; GPIOInit.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOx,&GPIOInit); USART_InitTypeDef USARTInit; USARTInit.USART_BaudRate = (baudrate == 0 ? 115200 : baudrate);//115200;//9600; USARTInit.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USARTInit.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USARTInit.USART_Parity = USART_Parity_No; USARTInit.USART_StopBits = USART_StopBits_1; USARTInit.USART_WordLength = USART_WordLength_8b; USART_Init(USARTxx,&USARTInit); USART_Cmd(USARTxx, ENABLE); if( baudrate == 0 ) { while(true) { USART_AutoBaudRateConfig(USART1, USART_AutoBaudRate_StartBit); USART_AutoBaudRateCmd(USART1, ENABLE); /* Wait until Receive enable acknowledge flag is set */ while (USART_GetFlagStatus(USART1, USART_FLAG_REACK) == RESET){} /* Wait until Transmit enable acknowledge flag is set */ while (USART_GetFlagStatus(USART1, USART_FLAG_TEACK) == RESET){} /* Loop until the end of Autobaudrate phase */ while (USART_GetFlagStatus(USART1, USART_FLAG_ABRF) == RESET){} /* If AutoBaudRate error occurred */ if (USART_GetFlagStatus(USART1, USART_FLAG_ABRE) == RESET) { break; } } } return USARTInit.USART_BaudRate; }
void USART_open(USART_TypeDef* USARTx, uint32_t baud, uint16_t WordLength, uint16_t Parity, uint16_t StopBits) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; assert_param(IS_USART_123_PERIPH(USARTx)); if (USARTx == USART1) { // Turn on clocks RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); } //else { if (USARTx == USART2) { // Turn on clocks RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); } if (USARTx == USART3) { // Turn on clocks RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3); } USART_InitStructure.USART_BaudRate = baud; //USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_WordLength = WordLength; //USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_StopBits = StopBits; //USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_Parity = Parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USARTx, &USART_InitStructure); USART_Cmd(USARTx, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //return -1; }
void UartConfig() { SerialPortInit(&serialPort3, 3); serialPort_t *s; s = serialPort3; serialPort3->thisUsart = USARTx; USARTx_TX_GPIO_CLK_EN; USARTx_RX_GPIO_CLK_EN; USARTx_DMAx_CLK_EN; USARTx_CLK_EN; USARTx_DMAx_CLK_EN; USARTx_TX_AFConfig; USARTx_RX_AFConfig; 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_UP; GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN; GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN; GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = BAUD; 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); DMA_DeInit(USARTx_TX_DMA_STREAM); DMA_InitStructure.DMA_Channel = USARTx_TX_DMA_CHANNEL; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(USARTx->DR)); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) serialPort3->txBuf; DMA_InitStructure.DMA_BufferSize = SERIAL_BUFSIZE; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(USARTx_TX_DMA_STREAM, &DMA_InitStructure); DMA_SetCurrDataCounter(USARTx_TX_DMA_STREAM, 0); DMA_ITConfig(USARTx_TX_DMA_STREAM, DMA_IT_TC | DMA_IT_FE | DMA_IT_TE, ENABLE); USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); DMA_Cmd(USARTx_TX_DMA_STREAM, DISABLE); DMA_DeInit(USARTx_RX_DMA_STREAM); DMA_InitStructure.DMA_Channel = USARTx_RX_DMA_CHANNEL; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & USARTx->DR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) s->rxBuf; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = SERIAL_BUFSIZE; 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_PeripheralDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(USARTx_RX_DMA_STREAM, &DMA_InitStructure); DMA_ClearFlag(USARTx_RX_DMA_STREAM, USARTx_RX_DMA_FLAG_TCIF | USARTx_RX_DMA_FLAG_HTIF | USARTx_RX_DMA_FLAG_TEIF); DMA_Cmd(USARTx_RX_DMA_STREAM, ENABLE); USART_DMACmd(USARTx, USART_DMAReq_Rx, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USARTx_DMA_TX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable USART */ USART_Cmd(USARTx, ENABLE); }
void USARTClass::begin( const uint32_t dwBaudRate ) { //Serial if(_dwId == id_serial) { // AFIO clock enable //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); // Enable USART Clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Configure USART Rx as input floating pinMode(RX, INPUT); // Configure USART Tx as alternate function push-pull pinMode(TX, AF_OUTPUT_PUSHPULL); } else if(_dwId == id_serial1)//Serial1 { #ifdef STM32F10X_HD RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); #endif #ifdef STM32F10X_MD RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); #endif pinMode(RX0, INPUT); pinMode(TX0, AF_OUTPUT_PUSHPULL); } else if(_dwId == id_serial2)//Serial2 { #ifdef STM32F10X_HD RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); #endif #ifdef STM32F10X_MD RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); #endif pinMode(RX1, INPUT); pinMode(TX1, AF_OUTPUT_PUSHPULL); } #ifdef STM32F10X_HD else if(_dwId == id_serial3)//Serial3 { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); pinMode(RX2, INPUT); pinMode(TX2, AF_OUTPUT_PUSHPULL); } #endif // USART default configuration // USART configured as follow: // - BaudRate = (set baudRate as 9600 baud) // - Word Length = 8 Bits // - One Stop Bit // - No parity // - Hardware flow control disabled (RTS and CTS signals) // - Receive and transmit enabled USART_InitStructure.USART_BaudRate = dwBaudRate; 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 USART USART_Init(_pUsart, &USART_InitStructure); NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); /* Enable the USARTy Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = _dwIrq; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = _dwId; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable USART Receive interrupts */ USART_ITConfig(_pUsart, USART_IT_RXNE, ENABLE); // Enable UART interrupt in NVIC NVIC_EnableIRQ( _dwIrq ) ; // Enable the USART USART_Cmd(_pUsart, ENABLE); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* USARTy and USARTz configuration -------------------------------------------*/ /* USARTy and USARTz configured as follow: - BaudRate = 230400 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 230400; 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(USARTy, &USART_InitStructure); /* Configure USARTz */ USART_Init(USARTz, &USART_InitStructure); /* Enable the USARTy */ USART_Cmd(USARTy, ENABLE); /* Enable the USARTz */ USART_Cmd(USARTz, ENABLE); /* Enable USARTy Half Duplex Mode*/ USART_HalfDuplexCmd(USARTy, ENABLE); /* Enable USARTz Half Duplex Mode*/ USART_HalfDuplexCmd(USARTz, ENABLE); while(NbrOfDataToRead2--) { /* Wait until end of transmit */ while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET) { } /* Write one byte in the USARTy Transmit Data Register */ USART_SendData(USARTy, TxBuffer1[TxCounter1++]); /* Wait the byte is entirtly received by USARTz */ while(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) == RESET) { } /* Store the received byte in the RxBuffer2 */ RxBuffer2[RxCounter2++] = USART_ReceiveData(USARTz); } /* Clear the USARTy Data Register */ USART_ReceiveData(USARTy); while(NbrOfDataToRead1--) { /* Wait until end of transmit */ while(USART_GetFlagStatus(USARTz, USART_FLAG_TXE)== RESET) { } /* Write one byte in the USARTz Transmit Data Register */ USART_SendData(USARTz, TxBuffer2[TxCounter2++]); /* Wait the byte is entirtly received by USARTy */ while(USART_GetFlagStatus(USARTy,USART_FLAG_RXNE) == RESET) { } /* Store the received byte in the RxBuffer1 */ RxBuffer1[RxCounter1++] = USART_ReceiveData(USARTy); } /* Check the received data with the send ones */ TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1); /* TransferStatus = PASSED, if the data transmitted from USARTy and received by USARTz are the same */ /* TransferStatus = FAILED, if the data transmitted from USARTy and received by USARTz are different */ TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2); /* TransferStatus = PASSED, if the data transmitted from USARTz and received by USARTy are the same */ /* TransferStatus = FAILED, if the data transmitted from USARTz and received by USARTy are different */ while (1) { } }
OSStatus internal_uart_init( mico_uart_t uart, const mico_uart_config_t* config, ring_buffer_t* optional_rx_buffer ) { GPIO_InitTypeDef gpio_init_structure; USART_InitTypeDef usart_init_structure; NVIC_InitTypeDef nvic_init_structure; DMA_InitTypeDef dma_init_structure; #ifndef NO_MICO_RTOS mico_rtos_init_semaphore(&uart_interfaces[uart].tx_complete, 1); mico_rtos_init_semaphore(&uart_interfaces[uart].rx_complete, 1); #else uart_interfaces[uart].tx_complete = false; uart_interfaces[uart].rx_complete = false; #endif MicoMcuPowerSaveConfig(false); /* Enable GPIO peripheral clocks for TX and RX pins */ RCC_AHB1PeriphClockCmd( uart_mapping[uart].pin_rx->peripheral_clock | uart_mapping[uart].pin_tx->peripheral_clock, ENABLE ); /* Configure USART TX Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_tx->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_PP; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( uart_mapping[uart].pin_tx->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_tx->bank, uart_mapping[uart].pin_tx->number, uart_mapping[uart].gpio_af ); /* Configure USART RX Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_rx->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_OD; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init( uart_mapping[uart].pin_rx->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_rx->bank, uart_mapping[uart].pin_rx->number, uart_mapping[uart].gpio_af ); #ifndef NO_MICO_RTOS if(config->flags & UART_WAKEUP_ENABLE){ current_uart = uart; mico_rtos_init_semaphore( &uart_interfaces[uart].sem_wakeup, 1 ); mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "UART_WAKEUP", thread_wakeup, 0x100, ¤t_uart); } #endif /* Check if any of the flow control is enabled */ if ( uart_mapping[uart].pin_cts && (config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { /* Enable peripheral clock */ RCC_AHB1PeriphClockCmd( uart_mapping[uart].pin_cts->peripheral_clock, ENABLE ); /* Configure CTS Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_cts->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_OD; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init( uart_mapping[uart].pin_cts->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_cts->bank, uart_mapping[uart].pin_cts->number, uart_mapping[uart].gpio_af ); } if ( uart_mapping[uart].pin_cts && (config->flow_control == FLOW_CONTROL_RTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { /* Enable peripheral clock */ RCC_AHB1PeriphClockCmd( uart_mapping[uart].pin_rts->peripheral_clock, ENABLE ); /* Configure RTS Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_rts->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_OD; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init( uart_mapping[uart].pin_rts->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_rts->bank, uart_mapping[uart].pin_rts->number, uart_mapping[uart].gpio_af ); } /* Enable UART peripheral clock */ uart_mapping[uart].usart_peripheral_clock_func( uart_mapping[uart].usart_peripheral_clock, ENABLE ); /************************************************************************** * Initialise STM32 USART registers * NOTE: * - Both transmitter and receiver are disabled until usart_enable_transmitter/receiver is called. * - Only 1 and 2 stop bits are implemented at the moment. **************************************************************************/ usart_init_structure.USART_Mode = 0; usart_init_structure.USART_BaudRate = config->baud_rate; usart_init_structure.USART_WordLength = ( ( config->data_width == DATA_WIDTH_9BIT ) || ( ( config->data_width == DATA_WIDTH_8BIT ) && ( config->parity != NO_PARITY ) ) ) ? USART_WordLength_9b : USART_WordLength_8b; usart_init_structure.USART_StopBits = ( config->stop_bits == STOP_BITS_1 ) ? USART_StopBits_1 : USART_StopBits_2; switch ( config->parity ) { case NO_PARITY: usart_init_structure.USART_Parity = USART_Parity_No; break; case EVEN_PARITY: usart_init_structure.USART_Parity = USART_Parity_Even; break; case ODD_PARITY: usart_init_structure.USART_Parity = USART_Parity_Odd; break; default: return kParamErr; } switch ( config->flow_control ) { case FLOW_CONTROL_DISABLED: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; break; case FLOW_CONTROL_CTS: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS; break; case FLOW_CONTROL_RTS: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS; break; case FLOW_CONTROL_CTS_RTS: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; break; default: return kParamErr; } /* Initialise USART peripheral */ USART_Init( uart_mapping[uart].usart, &usart_init_structure ); /************************************************************************** * Initialise STM32 DMA registers * Note: If DMA is used, USART interrupt isn't enabled. **************************************************************************/ /* Enable DMA peripheral clock */ uart_mapping[uart].tx_dma_peripheral_clock_func( uart_mapping[uart].tx_dma_peripheral_clock, ENABLE ); uart_mapping[uart].rx_dma_peripheral_clock_func( uart_mapping[uart].rx_dma_peripheral_clock, ENABLE ); /* Fill init structure with common DMA settings */ dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable; dma_init_structure.DMA_Priority = DMA_Priority_VeryHigh; dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable; dma_init_structure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; dma_init_structure.DMA_MemoryBurst = DMA_MemoryBurst_Single; dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; if ( config->data_width == DATA_WIDTH_9BIT ) { dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; } else { dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; } /* Initialise TX DMA */ DMA_DeInit( uart_mapping[uart].tx_dma_stream ); dma_init_structure.DMA_Channel = uart_mapping[uart].tx_dma_channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &uart_mapping[uart].usart->DR; dma_init_structure.DMA_Memory0BaseAddr = (uint32_t) 0; dma_init_structure.DMA_DIR = DMA_DIR_MemoryToPeripheral; dma_init_structure.DMA_BufferSize = 0; dma_init_structure.DMA_Mode = DMA_Mode_Normal; DMA_Init( uart_mapping[uart].tx_dma_stream, &dma_init_structure ); /* Initialise RX DMA */ DMA_DeInit( uart_mapping[uart].rx_dma_stream ); dma_init_structure.DMA_Channel = uart_mapping[uart].rx_dma_channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &uart_mapping[uart].usart->DR; dma_init_structure.DMA_Memory0BaseAddr = 0; dma_init_structure.DMA_DIR = DMA_DIR_PeripheralToMemory; dma_init_structure.DMA_BufferSize = 0; dma_init_structure.DMA_Mode = DMA_Mode_Normal; DMA_Init( uart_mapping[uart].rx_dma_stream, &dma_init_structure ); /************************************************************************** * Initialise STM32 DMA interrupts * Note: Only TX DMA interrupt is enabled. **************************************************************************/ /* Configure TX DMA interrupt on Cortex-M3 */ nvic_init_structure.NVIC_IRQChannel = uart_mapping[uart].tx_dma_irq; nvic_init_structure.NVIC_IRQChannelPreemptionPriority = (uint8_t) 0x5; nvic_init_structure.NVIC_IRQChannelSubPriority = 0x8; nvic_init_structure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &nvic_init_structure ); /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */ DMA_ITConfig( uart_mapping[uart].tx_dma_stream, DMA_IT_TC | DMA_IT_TE | DMA_IT_DME | DMA_IT_FE, ENABLE ); /* Enable USART's RX DMA interfaces */ USART_DMACmd( uart_mapping[uart].usart, USART_DMAReq_Rx, ENABLE ); /************************************************************************** * Initialise STM32 USART interrupt **************************************************************************/ nvic_init_structure.NVIC_IRQChannel = uart_mapping[uart].usart_irq; nvic_init_structure.NVIC_IRQChannelPreemptionPriority = (uint8_t) 0x6; nvic_init_structure.NVIC_IRQChannelSubPriority = 0x7; nvic_init_structure.NVIC_IRQChannelCmd = ENABLE; /* Enable USART interrupt vector in Cortex-M3 */ NVIC_Init( &nvic_init_structure ); /* Enable USART */ USART_Cmd( uart_mapping[uart].usart, ENABLE ); /* Enable both transmit and receive */ uart_mapping[uart].usart->CR1 |= USART_CR1_TE; uart_mapping[uart].usart->CR1 |= USART_CR1_RE; /* Setup ring buffer */ if (optional_rx_buffer != NULL) { /* Note that the ring_buffer should've been initialised first */ uart_interfaces[uart].rx_buffer = optional_rx_buffer; uart_interfaces[uart].rx_size = 0; platform_uart_receive_bytes( uart, optional_rx_buffer->buffer, optional_rx_buffer->size, 0 ); } else { /* Not using ring buffer. Configure RX DMA interrupt on Cortex-M3 */ nvic_init_structure.NVIC_IRQChannel = uart_mapping[uart].rx_dma_irq; nvic_init_structure.NVIC_IRQChannelPreemptionPriority = (uint8_t) 0x5; nvic_init_structure.NVIC_IRQChannelSubPriority = 0x8; nvic_init_structure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &nvic_init_structure ); /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */ DMA_ITConfig( uart_mapping[uart].rx_dma_stream, DMA_IT_TC | DMA_IT_TE | DMA_IT_DME | DMA_IT_FE, ENABLE ); } MicoMcuPowerSaveConfig(true); return kNoErr; }