1 | void initPC_Com()
|
2 | {
|
3 | USART_Config conf;
|
4 | //USART_DMA_Config dmaConf;
|
5 | configureUSART2(&conf);
|
6 | //configureUSART2_DMA(&dmaConf);
|
7 | USART_Ini(&conf);
|
8 |
|
9 | currentUsedUSART = &conf;
|
10 |
|
11 |
|
12 | USARTDMA_Ini(&TestBuf,9);
|
13 | }
|
14 |
|
15 | static inline void configureUSART2(USART_Config* conf)
|
16 | {
|
17 | conf->Rx_Source = GPIO_PinSource3;
|
18 | conf->Tx_Source = GPIO_PinSource2;
|
19 | conf->LowSpeed_APB_PeripheralClock = RCC_APB1Periph_USART2;
|
20 | conf->AHB1_PeripheralClock_Rx = RCC_AHB1Periph_GPIOA;
|
21 | conf->AHB1_PeripheralClock_Tx = RCC_AHB1Periph_GPIOA;
|
22 | conf->GPIO_Rx_Pin = GPIO_Pin_3;
|
23 | conf->GPIO_Tx_Pin = GPIO_Pin_2;
|
24 | conf->Rx_PORT = GPIOA;
|
25 | conf->Tx_PORT = GPIOA;
|
26 | conf->GPIO_AlternativeFunction = GPIO_AF_USART2;
|
27 | conf->USARTx = USART2;
|
28 |
|
29 | conf->InterfaceConfig.USART_BaudRate = 115200;
|
30 | conf->InterfaceConfig.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
31 | conf->InterfaceConfig.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
32 | conf->InterfaceConfig.USART_Parity = USART_Parity_No;
|
33 | conf->InterfaceConfig.USART_StopBits = USART_CR2_STOP_1;
|
34 | conf->InterfaceConfig.USART_WordLength = USART_WordLength_8b;
|
35 |
|
36 | conf->USART_GPIO_Config.GPIO_Mode = GPIO_Mode_AF;
|
37 | conf->USART_GPIO_Config.GPIO_Speed = GPIO_Speed_25MHz;
|
38 | conf->USART_GPIO_Config.GPIO_OType = GPIO_OType_PP;
|
39 | conf->USART_GPIO_Config.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
40 | };
|
41 |
|
42 | void USART_Ini(USART_Config* conf)
|
43 | {
|
44 | RCC_APB1PeriphClockCmd(conf->LowSpeed_APB_PeripheralClock, ENABLE);
|
45 | RCC_AHB1PeriphClockCmd(conf->AHB1_PeripheralClock_Rx, ENABLE);
|
46 | RCC_AHB1PeriphClockCmd(conf->AHB1_PeripheralClock_Tx, ENABLE);
|
47 |
|
48 | //Changes the mapping for the pins to use for USART for Port A
|
49 | GPIO_PinAFConfig(conf->Tx_PORT, conf->Tx_Source, conf->GPIO_AlternativeFunction);
|
50 | GPIO_PinAFConfig(conf->Rx_PORT, conf->Rx_Source, conf->GPIO_AlternativeFunction);
|
51 |
|
52 | conf->USART_GPIO_Config.GPIO_Pin = conf->GPIO_Rx_Pin;
|
53 | GPIO_Init(conf->Rx_PORT,&(conf->USART_GPIO_Config));
|
54 | conf->USART_GPIO_Config.GPIO_Pin = conf->GPIO_Tx_Pin;
|
55 | GPIO_Init(conf->Tx_PORT,&(conf->USART_GPIO_Config));
|
56 |
|
57 | //Set upt the USART configuration
|
58 | USART_Init(conf->USARTx,&(conf->InterfaceConfig));
|
59 |
|
60 | //Enable USART 2 now
|
61 | USART_Cmd(conf->USARTx, ENABLE);
|
62 | }
|
63 |
|
64 | void USARTDMA_Ini(uint8_t* buffer,uint32_t size)
|
65 | {
|
66 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
|
67 |
|
68 | DMA_DeInit(DMA1_Stream2);
|
69 | DMA_Cmd(DMA1_Stream2, DISABLE);
|
70 | DMA_InitTypeDef DMA_InitStruct;
|
71 | DMA_StructInit(&DMA_InitStruct);
|
72 | DMA_InitStruct.DMA_Channel = DMA_Channel_0;
|
73 | DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);
|
74 | DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)buffer;
|
75 | DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
|
76 | DMA_InitStruct.DMA_BufferSize = size;
|
77 | DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
78 | DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
79 | DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
|
80 | DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
|
81 | DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
|
82 | DMA_InitStruct.DMA_Priority = DMA_Priority_Medium;
|
83 | DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
|
84 | DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
|
85 | DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
|
86 | DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
|
87 | DMA_Init(DMA1_Stream2, &DMA_InitStruct);
|
88 | DMA_Cmd(DMA1_Stream2, ENABLE);
|
89 | USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
|
90 |
|
91 | NVIC_InitTypeDef NVIC_InitStructure;
|
92 | /* Enable the USARTx Interrupt */
|
93 | NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream2_IRQn;
|
94 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f;
|
95 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f;
|
96 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
97 | NVIC_Init(&NVIC_InitStructure);
|
98 |
|
99 | /* Enable USART */
|
100 | DMA_ITConfig(DMA1_Stream2, DMA_IT_TC, ENABLE);
|
101 | }
|