Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdio.h>
- #include "stm32f30x_conf.h"
- #include "usart.h"
- #define MAX_DATA 4
- unsigned char rxData[MAX_DATA+1];
- uint8_t rxDataCount = 0;
- static USART_InitTypeDef USART_InitStructure;
- static GPIO_InitTypeDef GPIO_InitStructure;
- static NVIC_InitTypeDef NVIC_InitStructure;
- void USART2_Init(uint32_t speed)
- {
- /*!< At this stage the microcontroller clock setting is already configured,
- this is done through SystemInit() function which is called from startup
- file (startup_stm32f37x.s) before to branch to application main.
- To reconfigure the default setting of SystemInit() function, refer to
- system_stm32f37x.c file
- */
- /* USARTx configured as follow:
- - BaudRate = 115200 baud
- - Word Length = 8 Bits
- - One Stop Bit
- - No parity
- - Hardware flow control disabled (RTS and CTS signals)
- - Receive and transmit enabled
- */
- USART_InitStructure.USART_BaudRate = speed;
- 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;
- /* Enable GPIO clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- /* Enable USART clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- /* Connect PXx to USARTx_Tx */
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
- /* Connect PXx to USARTx_Rx */
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
- /* Configure USART Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- 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_Init(GPIOA, &GPIO_InitStructure);
- /* Configure USART Rx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* USART configuration */
- USART_Init(USART2, &USART_InitStructure);
- /* Here the USART2 receive interrupt is enabled
- * and the interrupt controller is configured
- * to jump to the USART2_IRQHandler() function
- * if the USART2 receive interrupt occurs
- */
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // enable the USART2 receive interrupt
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // we want to configure the USART2 interrupts
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART2 interrupts
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART2 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
- /* Enable USART */
- USART_Cmd(USART2, ENABLE);
- }
- // this is the interrupt request handler (IRQ) for ALL USART2 interrupts
- void USART2_IRQHandler(void){
- // check if the USART2 receive interrupt flag was set
- if( USART_GetITStatus(USART2, USART_IT_RXNE) != RESET ){
- rxData[rxDataCount++] = USART_ReceiveData(USART2) & 0x7F;
- if (rxDataCount >= MAX_DATA) rxDataCount = 0;
- } else if(USART_GetITStatus(USART2, USART_IT_ORE) != RESET ){
- USART_ClearFlag(USART2, USART_FLAG_ORE);
- } else if(USART_GetITStatus(USART2, USART_IT_NE) != RESET ){
- USART_ClearFlag(USART2, USART_FLAG_NE);
- } else if(USART_GetITStatus(USART2, USART_IT_FE) != RESET ){
- USART_ClearFlag(USART2, USART_FLAG_FE);
- } else if(USART_GetITStatus(USART2, USART_IT_PE) != RESET ){
- USART_ClearFlag(USART2, USART_FLAG_PE);
- }
- }
- /**
- * @brief Retargets the C library printf function to the USART.
- * Overwrite __io_putchar function used by printf
- * @param None
- * @retval None
- */
- PUTCHAR_PROTOTYPE
- {
- /* Put character on the serial line */
- USART2->TDR = (ch & (uint16_t)0x01FF);
- /* Loop until transmit data register is empty */
- while ((USART2->ISR & USART_FLAG_TXE) == (uint16_t) RESET);
- return ch;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement