1  | // Function to initialize USART-Register, needs the information which UART to initialize
  | 
2  | int UART_INIT(int uart_x)
  | 
3  | {
 | 
4  |     // Define a variable of the structure to initialize GPIO
  | 
5  |     GPIO_InitTypeDef GPIO_InitStructure;
  | 
6  |     // Define initialize structure for UART
  | 
7  |     USART_InitTypeDef  USART_InitStructure;
  | 
8  | 
  | 
9  |     if(uart_x == 1)
  | 
10  |     {
 | 
11  |       // Enable clocks BEFORE using/configuring peripherals that expect them to be running
  | 
12  |       RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  | 
13  |       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  | 
14  |       // Enable UART peripheral by activating clock
  | 
15  |       RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  | 
16  | 
  | 
17  |       //Configure USART1 Tx as output floating with 50MHz
  | 
18  |       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  | 
19  |       GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;
  | 
20  |       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  | 
21  |       GPIO_Init(GPIOA, &GPIO_InitStructure);
  | 
22  |       //Configure USART1 Rx as alternate function push pull
  | 
23  |       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  | 
24  |       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  | 
25  |       GPIO_Init(GPIOA, &GPIO_InitStructure);
  | 
26  | 
  | 
27  |       USART_InitStructure.USART_BaudRate = 38400;
  | 
28  |       USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //Word Length = 8 Bits
  | 
29  |       USART_InitStructure.USART_StopBits = USART_StopBits_1;  //Two Stop Bit
  | 
30  |         USART_InitStructure.USART_Parity = USART_Parity_No ;   //No parity
  | 
31  |       USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  //Hardware flow control disabled (RTS and CTS signals)
  | 
32  |       USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  //Receive and transmit enabled
  | 
33  | 
  | 
34  |         //Configure USART
  | 
35  |         USART_Init(USART1,&USART_InitStructure);
  | 
36  | 
  | 
37  |         //Enable USART
  | 
38  |         USART_Cmd(USART1, ENABLE);
  | 
39  |     }
  | 
40  |     else if(uart_x == 2)
  | 
41  |     {
 | 
42  |       // Enable clocks BEFORE using/configuring peripherals that expect them to be running
  | 
43  |       RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  | 
44  |       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  | 
45  |       // Enable UART peripheral by activating clock
  | 
46  |       RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  | 
47  | 
  | 
48  |       //Configure USART2 Tx as output floating with 50MHz
  | 
49  |       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  | 
50  |       GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;
  | 
51  |       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  | 
52  |       GPIO_Init(GPIOA, &GPIO_InitStructure);
  | 
53  |       //Configure USART2 Rx as alternate function push pull
  | 
54  |       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  | 
55  |       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  | 
56  |       GPIO_Init(GPIOA, &GPIO_InitStructure);
  | 
57  | 
  | 
58  |       USART_InitStructure.USART_BaudRate = 38400;
  | 
59  |       USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //Word Length = 8 Bits
  | 
60  |       USART_InitStructure.USART_StopBits = USART_StopBits_1;  //Two Stop Bit
  | 
61  |         USART_InitStructure.USART_Parity = USART_Parity_No ;   //No parity
  | 
62  |       USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  //Hardware flow control disabled (RTS and CTS signals)
  | 
63  |       USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  //Receive and transmit enabled
  | 
64  | 
  | 
65  |         //Configure USART
  | 
66  |         USART_Init(USART2,&USART_InitStructure);
  | 
67  | 
  | 
68  |         //Enable USART
  | 
69  |         USART_Cmd(USART2, ENABLE);
  | 
70  |     }
  | 
71  |     else
  | 
72  |       // If there is a failure, then return 2 to flash error led
  | 
73  |       return 2;
  | 
74  | 
  | 
75  |     // If it is all okay
  | 
76  |     return 0;
  | 
77  | }
  |