-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport.c
190 lines (150 loc) · 5.03 KB
/
port.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
******************************************************************************
* 初始化代码
* @Copyright Han ZHANG(haldak) @ Wenhao DING(Gilgamesh) | All rights reserved.
* 清华大学电子系硬件部 & 未来通信兴趣团队
* 硬件设计大赛
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "port.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
RCC_ClocksTypeDef RCC_Clocks;
static __IO uint32_t uwTimingDelay;
/* Private function prototypes -----------------------------------------------*/
void RCC_init(void);
void GPIO_init(void);
void NVIC_init(void);
void EXTI_init(void);
void Systick_init(void);
/* Private functions ---------------------------------------------------------*/
//系统时钟配置
void RCC_init(void)
{
ErrorStatus HSEStartUpStatus;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08) {}
}
/* Enable Clock Security System(CSS): this will generate an NMI exception
when HSE clock fails */
RCC_ClockSecuritySystemCmd(ENABLE);
RCC_GetClocksFreq(&RCC_Clocks);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Enable GPIOs clocks */
RCC_APB2PeriphClockCmd(
RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD , ENABLE);
}
//GPIO初始化
void GPIO_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//注释:PA09 TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA10 RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//NVIC - 嵌套向量中断控制器配置
void NVIC_init(void)
{
//NVIC_InitTypeDef NVIC_InitStructure; //NVIC - 嵌套向量中断控制器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//2+2模式
}
//EXTI - 外部中断配置
void EXTI_init(void)
{
// EXTI_InitTypeDef EXTI_InitStructure;
}
/* 外设配置区 */
//Systick配置
//参数是SysTick的数目,默认应该是1ms/Tick
void Delay(__IO uint32_t nTime)
{
uwTimingDelay = nTime;
while(uwTimingDelay != 0);
}
//每次SysTick会调用
void TimingDelay_Decrement(void)
{
if (uwTimingDelay != 0)
{
uwTimingDelay--;
}
}
void Systick_init(void)
{
//Set NVIC for SysTick
NVIC_SetPriority (SysTick_IRQn, 0x05); //抢占优先级1,响应优先级1
/* SysTick end of count event each 1ms */
//RCC_GetClocksFreq(&RCC_Clocks); //系统时钟已经在时钟配置里获取,全局变量
if (SysTick_Config(RCC_Clocks.HCLK_Frequency / CLOCKS_PER_SEC))
{
/* Capture error */
while (1);
}
}
//外设初始化
void Periph_init()
{
USART_InitTypeDef USART_InitStructure;
Systick_init();
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
//放在主程序里,需要时打开,否则马上关闭
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//可以发送时
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//接受完时
USART_Cmd(USART1, ENABLE);
}
//系统总初始化
void Our_Sys_Init(void)
{
RCC_init();
GPIO_init();
NVIC_init();
EXTI_init();
Periph_init();
}