gd32f30x_hw.c
7.24 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*!
\file gd32f30x_hw.c
\brief USB hardware configuration for GD32F30x
\version 2020-08-01, V3.0.0, firmware for GD32F30x
\version 2022-06-10, V3.1.0, firmware for GD32F30x
*/
/*
Copyright (c) 2022, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "drv_usb_hw.h"
#define TIM_MSEC_DELAY 0x01U
#define TIM_USEC_DELAY 0x02U
__IO uint32_t delay_time = 0U;
__IO uint16_t timer_prescaler = 9U;
uint32_t usbfs_prescaler = 0U;
/* local function prototypes ('static') */
static void hw_time_set (uint8_t unit);
static void hw_delay (uint32_t ntime, uint8_t unit);
/*!
\brief configure the gpio peripheral
\param[in] none
\param[out] none
\retval none
*/
void usb_pullup_gpio_config(void)
{
/* configure usb pull-up pin */
gpio_init(USB_PULLUP, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, USB_PULLUP_PIN);
gpio_bit_reset(USB_PULLUP, USB_PULLUP_PIN);
}
/*!
\brief configure USB clock
\param[in] none
\param[out] none
\retval none
*/
void usb_rcu_config(void)
{
uint32_t system_clock = rcu_clock_freq_get(CK_SYS);
/* enable USB pull-up pin clock */
rcu_periph_clock_enable(RCC_AHBPeriph_GPIO_PULLUP);
usb_pullup_gpio_config();
if (48000000U == system_clock) {
usbfs_prescaler = RCU_CKUSB_CKPLL_DIV1;
timer_prescaler = 3U;
} else if (72000000U == system_clock) {
usbfs_prescaler = RCU_CKUSB_CKPLL_DIV1_5;
timer_prescaler = 5U;
} else if (96000000U == system_clock) {
usbfs_prescaler = RCU_CKUSB_CKPLL_DIV2;
timer_prescaler = 7U;
} else if (120000000U == system_clock) {
usbfs_prescaler = RCU_CKUSB_CKPLL_DIV2_5;
timer_prescaler = 9U;
} else {
/* reserved */
}
rcu_usb_clock_config(usbfs_prescaler);
rcu_periph_clock_enable(RCU_USBFS);
}
void rcu_config(void)
{
usb_rcu_config();
/* GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOD);
rcu_periph_clock_enable(RCU_AF);
/* Timer clock */
rcu_periph_clock_enable(RCU_TIMER1);
rcu_periph_clock_enable(RCU_TIMER2);
rcu_periph_clock_enable(RCU_TIMER3);
/* SPI clock */
rcu_periph_clock_enable(RCU_SPI0);
rcu_periph_clock_enable(RCU_SPI2);
}
/*!
\brief configure USB interrupt
\param[in] none
\param[out] none
\retval none
*/
void usb_intr_config(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE3_SUB1);//(NVIC_PRIGROUP_PRE2_SUB2);//(NVIC_PRIGROUP_PRE3_SUB1);//
nvic_irq_enable((uint8_t)USBFS_IRQn, 1U, 0U);
//#ifdef USB_LOW_POWER
// /* enable the power module clock */
// rcu_periph_clock_enable(RCU_PMU);
// /* USB wakeup EXTI line configuration */
// exti_interrupt_flag_clear(EXTI_18);
// exti_init(EXTI_18, EXTI_INTERRUPT, EXTI_TRIG_RISING);
// exti_interrupt_enable(EXTI_18);
// nvic_irq_enable((uint8_t)USBFS_WKUP_IRQn, 1U, 0U);
//#endif
}
/*!
\brief initializes delay unit using Timer2
\param[in] none
\param[out] none
\retval none
*/
void usb_timer_init (void)
{
/* configure the priority group to 2 bits */
// nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
/* enable the TIMER3 global interrupt */
nvic_irq_enable((uint8_t)TIMER3_IRQn, 4U, 0U);
rcu_periph_clock_enable(RCU_TIMER3);
}
/*!
\brief delay in micro seconds
\param[in] usec: value of delay required in micro seconds
\param[out] none
\retval none
*/
void usb_udelay (const uint32_t usec)
{
hw_delay(usec, TIM_USEC_DELAY);
}
/*!
\brief delay in milli seconds
\param[in] msec: value of delay required in milli seconds
\param[out] none
\retval none
*/
void usb_mdelay (const uint32_t msec)
{
hw_delay(msec, TIM_MSEC_DELAY);
}
/*!
\brief timer base IRQ
\param[in] none
\param[out] none
\retval none
*/
void usb_timer_irq (void)
{
if (delay_time > 0x00U) {
delay_time--;
} else {
timer_disable(TIMER3);
}
}
/*!
\brief delay routine based on TIMER3
\param[in] nTime: delay Time
\param[in] unit: delay Time unit = mili sec / micro sec
\param[out] none
\retval none
*/
static void hw_delay(uint32_t ntime, uint8_t unit)
{
delay_time = ntime;
hw_time_set(unit);
while (0U != delay_time) {
}
timer_disable(TIMER3);
}
/*!
\brief configures TIMER3 for delay routine based on TIMER3
\param[in] unit: msec /usec
\param[out] none
\retval none
*/
static void hw_time_set(uint8_t unit)
{
timer_parameter_struct timer_basestructure;
timer_disable(TIMER3);
timer_interrupt_disable(TIMER3, TIMER_INT_UP);
if (TIM_USEC_DELAY == unit) {
timer_basestructure.period = 11U;
} else if(TIM_MSEC_DELAY == unit) {
timer_basestructure.period = 11999U;
} else {
/* no operation */
}
timer_basestructure.prescaler = timer_prescaler;
timer_basestructure.alignedmode = TIMER_COUNTER_EDGE;
timer_basestructure.counterdirection = TIMER_COUNTER_UP;
timer_basestructure.clockdivision = TIMER_CKDIV_DIV1;
timer_basestructure.repetitioncounter = 0U;
timer_init(TIMER3, &timer_basestructure);
timer_interrupt_flag_clear(TIMER3, TIMER_INT_UP);
timer_auto_reload_shadow_enable(TIMER3);
/* TIMER3 interrupt enable */
timer_interrupt_enable(TIMER3, TIMER_INT_UP);
/* TIMER3 enable counter */
timer_enable(TIMER3);
}
void systick_config(void)
{
/* setup systick timer for 1000Hz interrupts */
if (SysTick_Config(SystemCoreClock / 1000U))
{
/* capture error */
while (1)
{
}
}
/* configure the systick handler priority */
NVIC_SetPriority(SysTick_IRQn, 0x0fU);
}