2015-04-16 08:33:35 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2015-05-17 22:19:19 +02:00
|
|
|
static void _gclk_setup(int gclk, uint32_t reg)
|
|
|
|
{
|
2016-01-26 20:01:06 +01:00
|
|
|
while (GCLK->SYNCBUSY.reg & GCLK_SYNCBUSY_GENCTRL(gclk)) {}
|
2015-05-17 22:19:19 +02:00
|
|
|
GCLK->GENCTRL[gclk].reg = reg;
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:33:35 +02:00
|
|
|
/**
|
|
|
|
* @brief Initialize the CPU, set IRQ priorities, clocks
|
|
|
|
*/
|
|
|
|
void cpu_init(void)
|
|
|
|
{
|
|
|
|
/* disable the watchdog timer */
|
|
|
|
WDT->CTRLA.bit.ENABLE = 0;
|
|
|
|
|
2015-05-27 23:07:10 +02:00
|
|
|
/* initialize the Cortex-M core */
|
|
|
|
cortexm_init();
|
2015-04-16 08:33:35 +02:00
|
|
|
|
2015-05-17 22:19:19 +02:00
|
|
|
/* 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
|
|
|
|
;
|
2015-04-16 08:33:35 +02:00
|
|
|
|
|
|
|
/* Software reset the GCLK module to ensure it is re-initialized correctly */
|
|
|
|
GCLK->CTRLA.reg = GCLK_CTRLA_SWRST;
|
2016-01-26 20:01:06 +01:00
|
|
|
while (GCLK->CTRLA.reg & GCLK_CTRLA_SWRST) {}
|
|
|
|
while (GCLK->SYNCBUSY.reg & GCLK_SYNCBUSY_SWRST) {}
|
2015-04-16 08:33:35 +02:00
|
|
|
|
|
|
|
/* set OSC16M to 16MHz */
|
|
|
|
OSCCTRL->OSC16MCTRL.bit.FSEL = 3;
|
2015-05-17 22:19:19 +02:00
|
|
|
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);
|
2017-01-12 17:48:11 +01:00
|
|
|
|
|
|
|
#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
|
2015-04-16 08:33:35 +02:00
|
|
|
}
|