1 | void USART_SETUP(void){
|
2 |
|
3 | GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX
|
4 | USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
|
5 | NVIC_InitTypeDef NVIC_InitStructure1; // this is used to configure the NVIC (nested vector interrupt controller)
|
6 | NVIC_InitTypeDef NVIC_InitStructure6; // this is used to configure the NVIC (nested vector interrupt controller)
|
7 |
|
8 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_USART6, ENABLE);
|
9 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
|
10 |
|
11 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them
|
12 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate!
|
13 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this defines the output type as push pull mode (as opposed to open drain)
|
14 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // this activates the pullup resistors on the IO pins
|
15 |
|
16 | //------------USART1 PIN SETTINGS----------------------------------
|
17 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used
|
18 | GPIO_Init(GPIOB, &GPIO_InitStruct); // now all the values are passed to the GPIO_Init() function which sets the GPIO registers
|
19 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
|
20 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
|
21 |
|
22 | //------------USART6 PIN SETTINGS----------------------------------
|
23 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used
|
24 | GPIO_Init(GPIOC, &GPIO_InitStruct); // now all the values are passed to the GPIO_Init() function which sets the GPIO registers
|
25 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
|
26 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
|
27 |
|
28 |
|
29 | USART_InitStruct.USART_WordLength = USART_WordLength_8b; // we want the data frame size to be 8 bits (standard)
|
30 | USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)
|
31 | USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)
|
32 | USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
|
33 | USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver
|
34 |
|
35 | //----------------USART1 aktivieren-----------------------------
|
36 | USART_InitStruct.USART_BaudRate = 115200; // the baudrate is set to the value we passed into this init function
|
37 |
|
38 | //----Interrupt USART1---
|
39 | USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt
|
40 |
|
41 | NVIC_InitStructure1.NVIC_IRQChannel = USART1_IRQn; // we want to configure the USART1 interrupts
|
42 | NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts
|
43 | NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
|
44 | NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE; // the USART1 interrupts are globally enabled
|
45 | NVIC_Init(&NVIC_InitStructure1); // the properties are passed to the NVIC_Init function which takes care of the low level stuff
|
46 |
|
47 | USART_Init(USART1, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting
|
48 | USART_Cmd(USART1, ENABLE);
|
49 |
|
50 |
|
51 | //----------------USART6 aktivieren-----------------------------
|
52 | USART_InitStruct.USART_BaudRate = 38400; // the baudrate is set to the value we passed into this init function
|
53 |
|
54 | //----Interrupt USART1---
|
55 | USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt
|
56 |
|
57 | NVIC_InitStructure6.NVIC_IRQChannel = USART6_IRQn; // we want to configure the USART1 interrupts
|
58 | NVIC_InitStructure6.NVIC_IRQChannelPreemptionPriority = 1;// this sets the priority group of the USART1 interrupts
|
59 | NVIC_InitStructure6.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
|
60 | NVIC_InitStructure6.NVIC_IRQChannelCmd = ENABLE; // the USART6 interrupts are globally enabled
|
61 | NVIC_Init(&NVIC_InitStructure6); // the properties are passed to the NVIC_Init function which takes care of the low level stuff
|
62 |
|
63 | USART_Init(USART6, &USART_InitStruct);
|
64 | USART_Cmd(USART6, ENABLE);
|
65 |
|
66 | }
|