-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.c
76 lines (56 loc) · 1.68 KB
/
clock.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
#include "main.h"
#include "clock.h"
#include "io.h"
volatile unsigned char rtc_flag = 0;
ISR (RTC_OVF_vect)
{
static unsigned char sys_led = 0;
rtc_flag++;
for (unsigned char i=0; i<RTC_HANDLER_NUMBER; i++)
{
if (rtc_handler[i])
{
rtc_handler[i]();
}
}
// if (sys_led > 2)
if (1)
{
LED_PORT.OUTTGL = LED_SYS;
sys_led = 0;
}
else
{
sys_led++;
}
}
void clock_init(void)
{
/*
OSC.CTRL |= OSC_RC2MEN_bm;
loop_until_bit_is_set(OSC.STATUS, OSC_RC2MRDY_bp);
OSC.PLLCTRL = OSC_PLLSRC_RC2M_gc | 16;
OSC.CTRL |= OSC_PLLEN_bm;
loop_until_bit_is_set(OSC.STATUS, OSC_PLLRDY_bp);
CCP = CCP_IOREG_gc;
CLK.CTRL = CLK_SCLKSEL_PLL_gc;
*/
/* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC2MHZ);
XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
/* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
}
void clock_rtc_init(void)
{
OSC.CTRL |= OSC_RC32KEN_bm;
loop_until_bit_is_set(OSC.STATUS, OSC_RC32KRDY_bp);
CLK.RTCCTRL = CLK_RTCEN_bm | CLK_RTCSRC_RCOSC_gc;
loop_until_bit_is_clear(RTC.STATUS, WDT_SYNCBUSY_bp);
RTC.PER = (512-1); /* 8 - 1 */
RTC.CNT = 0;
RTC.CTRL = RTC_PRESCALER_DIV1_gc;
RTC.INTCTRL = RTC_OVFINTLVL_LO_gc;
}