1 | /* Includes ------------------------------------------------------------------*/
|
2 | #include "stm32f10x_conf.h"
|
3 | #include <stdio.h>
|
4 | #include <string.h>
|
5 |
|
6 | GPIO_InitTypeDef GPIO_InitStructure;
|
7 |
|
8 |
|
9 | void RCC_Configuration(void);
|
10 | void NVIC_Configuration(void);
|
11 | void Delay(vu32 nCount);
|
12 |
|
13 |
|
14 |
|
15 | #define UART2_TX_PIN 5;
|
16 | #define UART2_RX_PIN 6;
|
17 |
|
18 | #define UART2_GPIO GPIOD;
|
19 | #define UART1_CLK_EN 14;
|
20 | #define UART2_CLK_EN 17;
|
21 | #define UART_EN 13;
|
22 | #define UART_RX_EN 2;
|
23 | #define UART_TX_EN 3;
|
24 | #define TXE 7; // TX Reg empty
|
25 | #define UART_M 12; //
|
26 |
|
27 | typedef struct
|
28 | {
|
29 | int32_t UART_CLK;
|
30 | int32_t BAUD;
|
31 | uint8_t DATA_BITS;
|
32 | uint8_t STOP_BITS;
|
33 | uint8_t PARITY;
|
34 | }UART_INITSTRUCT;
|
35 |
|
36 |
|
37 | void UART_INIT(UART_INITSTRUCT* initstruct)
|
38 | {
|
39 |
|
40 | uint16_t baud_temp = initstruct->UART_CLK/(initstruct->BAUD*16);
|
41 | uint16_t baud_temp2 = ((initstruct->UART_CLK / (initstruct->BAUD*16/100) - baud_temp*100) * 16 + 5) /100;
|
42 | baud_temp2 |= baud_temp<<4;
|
43 |
|
44 | /* Enable GPIOD clock */
|
45 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
|
46 |
|
47 | GPIO_InitStructure.GPIO_Pin = 1<<UART2_TX_PIN;
|
48 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
49 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
50 | GPIO_Init(GPIOD, &GPIO_InitStructure);
|
51 |
|
52 | GPIO_InitStructure.GPIO_Pin = 1<<UART2_RX_PIN;
|
53 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
54 | GPIO_Init(GPIOD, &GPIO_InitStructure);
|
55 |
|
56 |
|
57 | RCC->APB2ENR |= (1<<0);
|
58 | RCC->APB1ENR |= 1<<UART2_CLK_EN;
|
59 |
|
60 | USART2-> BRR = baud_temp2;
|
61 |
|
62 | USART2->CR1 |= 1<<UART_TX_EN;
|
63 | USART2->CR1 |= 1<<UART_RX_EN;
|
64 | USART2->CR1 |= 1<<UART_EN;
|
65 |
|
66 | AFIO->MAPR |= (1<<3);
|
67 | }
|
68 |
|
69 |
|
70 | void u2_putchar(char c)
|
71 | {
|
72 | while (!(USART2->SR & (1<<7))); //bit 7 = TX ready
|
73 | USART2->DR = (c);
|
74 |
|
75 | }
|
76 |
|
77 | void u2_putstring(const char *s)
|
78 | {
|
79 | while(*s) u2_putchar(*s++); // send string char by char
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | int main(void)
|
87 | {
|
88 | #ifdef DEBUG
|
89 | debug();
|
90 | #endif
|
91 |
|
92 | /* System Clocks Configuration */
|
93 | RCC_Configuration();
|
94 |
|
95 | /* NVIC Configuration */
|
96 | NVIC_Configuration();
|
97 |
|
98 |
|
99 | // UART_INITSTRUCT.UART = "UART2";
|
100 | UART_INITSTRUCT UART2;
|
101 | UART2.UART_CLK = 36000000;
|
102 | UART2.BAUD = 115200;
|
103 |
|
104 |
|
105 | UART_INIT(&UART2);
|
106 |
|
107 |
|
108 |
|
109 | uint8_t daten[] = "Das ist ein String Test";
|
110 |
|
111 |
|
112 | long long int delay = 10000;
|
113 | while(1)
|
114 | {
|
115 | u2_putstring(daten);
|
116 |
|
117 | u2_putchar(0x0A);
|
118 |
|
119 |
|
120 | /* Insert delay */
|
121 | Delay(delay*10);
|
122 | }
|
123 | }
|
124 |
|
125 | void RCC_Configuration(void)
|
126 | {
|
127 | //Clock control register
|
128 | RCC->CR = RCC_CR_HSEON ; //HSE on
|
129 |
|
130 | // Wait for HSE
|
131 | while (!(RCC->CR & RCC_CR_HSERDY))
|
132 | {
|
133 | }
|
134 |
|
135 | //setup PLL 8Mhz in 72Mhz Sys Clock
|
136 |
|
137 | //Clock configuration register
|
138 | RCC->CFGR |= RCC_CFGR_PLLSRC; //HSE als PLL-Eingangssignal
|
139 | RCC->CFGR |= RCC_CFGR_PLLMULL9; //PLL=HSE*9
|
140 | RCC->CR |= RCC_CR_PLLON; //PLL einschalten
|
141 |
|
142 | //Wait for PLL
|
143 | while (!(RCC->CR & RCC_CR_PLLRDY))
|
144 | {
|
145 | }
|
146 |
|
147 | RCC->CFGR = RCC_CFGR_SW_PLL; //PLL als Systemtakt
|
148 |
|
149 | //Wait for SYSCLK
|
150 | while (!(RCC->CFGR & RCC_CFGR_SWS_PLL))
|
151 | {
|
152 | }
|
153 | //Takte für die Peripherie einschalten
|
154 | RCC->CFGR |= RCC_CFGR_PPRE1_DIV2; //APB1 /2 teilen
|
155 | RCC->CFGR |= RCC_CFGR_PPRE2_DIV1; //APB2 /1 teilen
|
156 |
|
157 | {;}
|
158 | }
|
159 |
|
160 |
|
161 | void NVIC_Configuration(void)
|
162 | {
|
163 | #ifdef VECT_TAB_RAM
|
164 | /* Set the Vector Table base location at 0x20000000 */
|
165 | NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
|
166 | #else /* VECT_TAB_FLASH */
|
167 | /* Set the Vector Table base location at 0x08000000 */
|
168 | NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
|
169 | #endif
|
170 | }
|
171 |
|
172 | void assert_failed(u8* file, u32 line)
|
173 | {
|
174 | /* User can add his own implementation to report the file name and line number,
|
175 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
176 |
|
177 | /* Infinite loop */
|
178 | while (1)
|
179 | {
|
180 | }
|
181 | }
|
182 |
|
183 |
|
184 | void Delay(vu32 nCount)
|
185 | {
|
186 | for(; nCount!= 0;nCount--);
|
187 | }
|