1 |
|
2 | /* Systemtakt = 72000000 AHB Prescaler */
|
3 | // init.c
|
4 | /*===================================================*/
|
5 | #include "init.h"
|
6 | void RCC_Enable(void);
|
7 | void init_all(void);
|
8 | void USART2_Config(void);
|
9 | void DMA2_Config(void);
|
10 | /*===================================================*/
|
11 |
|
12 | GPIO_InitTypeDef GPIO_InitStructure;
|
13 | USART_InitTypeDef USART_Initialisation;
|
14 | DMA_InitTypeDef DMA_InitStructure;
|
15 |
|
16 | extern unsigned char buffer[129];
|
17 |
|
18 | void RCC_Enable(void){
|
19 | /* Enable GPIO clock */
|
20 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
|
21 | /* Enable USART clock */
|
22 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
23 | /* Enable DMA2 clock -------------------------------------------------------*/
|
24 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
|
25 |
|
26 | }
|
27 |
|
28 | void init_all(void) {
|
29 | /*===================================================*/
|
30 | RCC_Enable();
|
31 | /* USART2 initialisieren PA2=TX PA3=RX*/
|
32 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
|
33 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
|
34 |
|
35 | /* Configure USART Tx as alternate function push-pull */
|
36 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
|
37 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
38 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
39 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
40 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
41 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
42 |
|
43 | USART2_Config();
|
44 | DMA2_Config();
|
45 | USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
|
46 | /* Enable DMA2 Channel3 */
|
47 | DMA_Cmd(DMA2_Channel3, ENABLE);
|
48 | }
|
49 |
|
50 | void USART2_Config(void){
|
51 | USART_Initialisation.USART_BaudRate = 9600;
|
52 | USART_Initialisation.USART_WordLength = USART_WordLength_8b;
|
53 | USART_Initialisation.USART_StopBits = USART_StopBits_1;
|
54 | USART_Initialisation.USART_Parity = USART_Parity_No;
|
55 | USART_Initialisation.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
56 | USART_Initialisation.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
57 | /* USART configuration */
|
58 | USART_Init(USART2, &USART_Initialisation);
|
59 | /* Enable USART */
|
60 | USART_Cmd(USART2, ENABLE);
|
61 | }
|
62 | void DMA2_Config(void){
|
63 | DMA_DeInit(DMA2_Channel3);
|
64 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->TDR);
|
65 | DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&buffer;
|
66 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
67 | DMA_InitStructure.DMA_BufferSize = 129;
|
68 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
69 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
70 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
|
71 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
|
72 | DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
|
73 | DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
74 | DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
75 | DMA_Init(DMA2_Channel3, &DMA_InitStructure);
|
76 | }
|