cpu.c
2.2 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
/*
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
* 2015 FreshTemp, LLC.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_saml21
* @{
*
* @file cpu.c
* @brief Implementation of the CPU initialization for Atmel SAML21 MCUs
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @}
*/
#include "cpu.h"
#include "periph/init.h"
static void _gclk_setup(int gclk, uint32_t reg)
{
while (GCLK->SYNCBUSY.reg & GCLK_SYNCBUSY_GENCTRL(gclk)) {}
GCLK->GENCTRL[gclk].reg = reg;
}
/**
* @brief Initialize the CPU, set IRQ priorities, clocks
*/
void cpu_init(void)
{
/* disable the watchdog timer */
WDT->CTRLA.bit.ENABLE = 0;
/* initialize the Cortex-M core */
cortexm_init();
/* turn on only needed APB peripherals */
MCLK->APBAMASK.reg =
MCLK_APBAMASK_PM
|MCLK_APBAMASK_MCLK
|MCLK_APBAMASK_RSTC
|MCLK_APBAMASK_OSCCTRL
|MCLK_APBAMASK_OSC32KCTRL
|MCLK_APBAMASK_SUPC
|MCLK_APBAMASK_GCLK
|MCLK_APBAMASK_WDT
|MCLK_APBAMASK_RTC
|MCLK_APBAMASK_EIC
|MCLK_APBAMASK_PORT
//|MCLK_APBAMASK_TAL
;
/* Software reset the GCLK module to ensure it is re-initialized correctly */
GCLK->CTRLA.reg = GCLK_CTRLA_SWRST;
while (GCLK->CTRLA.reg & GCLK_CTRLA_SWRST) {}
while (GCLK->SYNCBUSY.reg & GCLK_SYNCBUSY_SWRST) {}
/* set OSC16M to 16MHz */
OSCCTRL->OSC16MCTRL.bit.FSEL = 3;
OSCCTRL->OSC16MCTRL.bit.ONDEMAND = 0;
OSCCTRL->OSC16MCTRL.bit.RUNSTDBY = 0;
/* Setup GCLK generators */
_gclk_setup(0, GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSC16M);
_gclk_setup(1, GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K);
#ifdef FEATURE_PERIPH_PM
/* enable power managemet module */
MCLK->APBAMASK.reg |= MCLK_APBAMASK_PM;
PM->CTRLA.reg = PM_CTRLA_MASK & (~PM_CTRLA_IORET);
/* disable brownout detection
* (Caused unexplicable reboots from sleep on saml21. /KS)
*/
SUPC->BOD33.bit.ENABLE=0;
#endif
/* trigger static peripheral initialization */
periph_init();
}