Advertisement
Guest User

Untitled

a guest
Nov 30th, 2012
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include "stm32f30x_conf.h"
  4. #include "usart.h"
  5.  
  6. #define MAX_DATA 4
  7. unsigned char rxData[MAX_DATA+1];
  8. uint8_t rxDataCount = 0;
  9.  
  10. static USART_InitTypeDef USART_InitStructure;
  11. static GPIO_InitTypeDef GPIO_InitStructure;
  12. static NVIC_InitTypeDef NVIC_InitStructure;
  13.  
  14. void USART2_Init(uint32_t speed)
  15. {
  16.     /*!< At this stage the microcontroller clock setting is already configured,
  17.                this is done through SystemInit() function which is called from startup
  18.                file (startup_stm32f37x.s) before to branch to application main.
  19.                To reconfigure the default setting of SystemInit() function, refer to
  20.                system_stm32f37x.c file
  21.      */
  22.     /* USARTx configured as follow:
  23.                 - BaudRate = 115200 baud
  24.                 - Word Length = 8 Bits
  25.                 - One Stop Bit
  26.                 - No parity
  27.                 - Hardware flow control disabled (RTS and CTS signals)
  28.                 - Receive and transmit enabled
  29.      */
  30.     USART_InitStructure.USART_BaudRate = speed;
  31.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  32.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  33.     USART_InitStructure.USART_Parity = USART_Parity_No;
  34.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  35.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  36.  
  37.     /* Enable GPIO clock */
  38.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  39.  
  40.     /* Enable USART clock */
  41.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  42.  
  43.     /* Connect PXx to USARTx_Tx */
  44.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
  45.  
  46.     /* Connect PXx to USARTx_Rx */
  47.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
  48.  
  49.     /* Configure USART Tx as alternate function push-pull */
  50.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  51.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  52.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  53.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  54.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  55.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  56.  
  57.     /* Configure USART Rx as alternate function push-pull */
  58.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  59.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  60.  
  61.     /* USART configuration */
  62.     USART_Init(USART2, &USART_InitStructure);
  63.  
  64.     /* Here the USART2 receive interrupt is enabled
  65.      * and the interrupt controller is configured
  66.      * to jump to the USART2_IRQHandler() function
  67.      * if the USART2 receive interrupt occurs
  68.      */
  69.     USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // enable the USART2 receive interrupt
  70.  
  71.     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;        // we want to configure the USART2 interrupts
  72.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART2 interrupts
  73.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;       // this sets the subpriority inside the group
  74.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;          // the USART2 interrupts are globally enabled
  75.     NVIC_Init(&NVIC_InitStructure);                          // the properties are passed to the NVIC_Init function which takes care of the low level stuff
  76.  
  77.     /* Enable USART */
  78.     USART_Cmd(USART2, ENABLE);
  79. }
  80.  
  81. // this is the interrupt request handler (IRQ) for ALL USART2 interrupts
  82. void USART2_IRQHandler(void){
  83.  
  84.     // check if the USART2 receive interrupt flag was set
  85.     if( USART_GetITStatus(USART2, USART_IT_RXNE) != RESET ){
  86.         rxData[rxDataCount++] = USART_ReceiveData(USART2) & 0x7F;
  87.         if (rxDataCount >= MAX_DATA) rxDataCount = 0;
  88.     } else if(USART_GetITStatus(USART2, USART_IT_ORE) != RESET ){
  89.         USART_ClearFlag(USART2, USART_FLAG_ORE);
  90.     } else if(USART_GetITStatus(USART2, USART_IT_NE) != RESET ){
  91.         USART_ClearFlag(USART2, USART_FLAG_NE);
  92.     } else if(USART_GetITStatus(USART2, USART_IT_FE) != RESET ){
  93.         USART_ClearFlag(USART2, USART_FLAG_FE);
  94.     } else if(USART_GetITStatus(USART2, USART_IT_PE) != RESET ){
  95.         USART_ClearFlag(USART2, USART_FLAG_PE);
  96.     }
  97. }
  98.  
  99. /**
  100.  * @brief  Retargets the C library printf function to the USART.
  101.  *          Overwrite __io_putchar function used by printf
  102.  * @param  None
  103.  * @retval None
  104.  */
  105. PUTCHAR_PROTOTYPE
  106. {
  107.     /* Put character on the serial line */
  108.     USART2->TDR = (ch & (uint16_t)0x01FF);
  109.  
  110.     /* Loop until transmit data register is empty */
  111.     while ((USART2->ISR & USART_FLAG_TXE) == (uint16_t) RESET);
  112.  
  113.     return ch;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement