aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/libboard/common/source/board_lowlevel.c
blob: b82f6f3fe29d186bc6bd4525846f0cc5de6ceba5 (plain)
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
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support
 * ----------------------------------------------------------------------------
 * Copyright (c) 2009, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL 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.
 * ----------------------------------------------------------------------------
 */

/**
 * \file
 *
 * Provides the low-level initialization function that called on chip startup.
 */

/*----------------------------------------------------------------------------
 *        Headers
 *----------------------------------------------------------------------------*/

#include "board.h"

/*----------------------------------------------------------------------------
 *        Local definitions
 *----------------------------------------------------------------------------*/

#define BOARD_OSCOUNT   (CKGR_MOR_MOSCXTST(0x8))
#define BOARD_MCKR      (PMC_MCKR_PRES_CLK | PMC_MCKR_CSS_PLLA_CLK)

#if (BOARD_MCK == 48000000)
#if (BOARD_MAINOSC == 18432000)
/* Clock settings at 48MHz  for 18 MHz crystal */
#define BOARD_PLLAR     (CKGR_PLLAR_STUCKTO1 \
					   | CKGR_PLLAR_MULA(13-1) \
					   | CKGR_PLLAR_PLLACOUNT(0x1) \
					   | CKGR_PLLAR_DIVA(5))
#elif (BOARD_MAINOSC == 12000000)
/* QMod has 12 MHz clock, so multply by 8 (96 MHz) and divide by 2 */
#define BOARD_PLLAR     (CKGR_PLLAR_STUCKTO1 \
					   | CKGR_PLLAR_MULA(8-1) \
					   | CKGR_PLLAR_PLLACOUNT(0x1) \
					   | CKGR_PLLAR_DIVA(2))
#else
#error "Please define PLLA config for your MAINOSC frequency"
#endif /* MAINOSC */
#elif (BOARD_MCK == 64000000)
#if (BOARD_MAINOSC == 18432000)
/* Clock settings at 64MHz  for 18 MHz crystal: 64.512 MHz */
#define BOARD_PLLAR     (CKGR_PLLAR_STUCKTO1 \
					   | CKGR_PLLAR_MULA(7-1) \
					   | CKGR_PLLAR_PLLACOUNT(0x1) \
					   | CKGR_PLLAR_DIVA(2))
#elif (BOARD_MAINOSC == 12000000)
/* QMod has 12 MHz clock, so multply by 10 / div by 2: 60 MHz */
#define BOARD_PLLAR     (CKGR_PLLAR_STUCKTO1 \
					   | CKGR_PLLAR_MULA(10-1) \
					   | CKGR_PLLAR_PLLACOUNT(0x1) \
					   | CKGR_PLLAR_DIVA(2))
#error "Please define PLLA config for your MAINOSC frequency"
#endif /* MAINOSC */
#else
	#error "No PLL settings for current BOARD_MCK."
#endif

#if (BOARD_MAINOSC == 12000000)
#define PLLB_CFG (CKGR_PLLBR_DIVB(2)|CKGR_PLLBR_MULB(8-1)|CKGR_PLLBR_PLLBCOUNT_Msk)
#elif (BOARD_MAINOSC == 18432000)
#define PLLB_CFG (CKGR_PLLBR_DIVB(5)|CKGR_PLLBR_MULB(13-1)|CKGR_PLLBR_PLLBCOUNT_Msk)
#else
#error "Please configure PLLB for your MAINOSC freq"
#endif

/* Define clock timeout */
#define CLOCK_TIMEOUT    0xFFFFFFFF

/**
 * \brief Configure 48MHz Clock for USB
 */
static void _ConfigureUsbClock(void)
{
	/* Enable PLLB for USB */
	PMC->CKGR_PLLBR = PLLB_CFG;
	while ((PMC->PMC_SR & PMC_SR_LOCKB) == 0) ;

	/* USB Clock uses PLLB */
	PMC->PMC_USB = PMC_USB_USBDIV(0)	/* /1 (no divider)  */
			    | PMC_USB_USBS;	/* PLLB */
}

/*----------------------------------------------------------------------------
 *        Exported functions
 *----------------------------------------------------------------------------*/

/**
 * \brief Performs the low-level initialization of the chip.
 * This includes EFC and master clock configuration.
 * It also enable a low level on the pin NRST triggers a user reset.
 */
extern WEAK void LowLevelInit( void )
{
	uint32_t timeout = 0;

	/* Configure the Supply Monitor to reset the CPU in case VDDIO is
	 * lower than 3.0V.  As we run the board on 3.3V, any lower voltage
	 * might be some kind of leakage that creeps in some way, but is not
	 * the "official" power supply */
	SUPC->SUPC_SMMR = SUPC_SMMR_SMTH_3_0V | SUPC_SMMR_SMSMPL_CSM |
	    	      SUPC_SMMR_SMRSTEN_ENABLE;

	/* enable both LED and green LED */
	PIOA->PIO_PER |= PIO_LED_RED | PIO_LED_GREEN;
	PIOA->PIO_OER |= PIO_LED_RED | PIO_LED_GREEN;
	PIOA->PIO_CODR |= PIO_LED_RED | PIO_LED_GREEN;

	/* Set 3 FWS for Embedded Flash Access */
	EFC->EEFC_FMR = EEFC_FMR_FWS(3);

	/* Select external slow clock */
/*    if ((SUPC->SUPC_SR & SUPC_SR_OSCSEL) != SUPC_SR_OSCSEL_CRYST)
	{
		SUPC->SUPC_CR = (uint32_t)(SUPC_CR_XTALSEL_CRYSTAL_SEL | SUPC_CR_KEY(0xA5));
		timeout = 0;
		while (!(SUPC->SUPC_SR & SUPC_SR_OSCSEL_CRYST) );
	}
*/

#ifndef qmod
	/* Initialize main oscillator */
	if ( !(PMC->CKGR_MOR & CKGR_MOR_MOSCSEL) )
	{
		PMC->CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN;
		timeout = 0;
		while (!(PMC->PMC_SR & PMC_SR_MOSCXTS) && (timeout++ < CLOCK_TIMEOUT));
	}

	/* Switch to 3-20MHz Xtal oscillator */
	PIOB->PIO_PDR = (1 << 8) | (1 << 9);
	PIOB->PIO_PUDR = (1 << 8) | (1 << 9);
	PIOB->PIO_PPDDR = (1 << 8) | (1 << 9);
	PMC->CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN | CKGR_MOR_MOSCSEL;
	/* wait for Main XTAL oscillator stabilization */
	timeout = 0;
	while (!(PMC->PMC_SR & PMC_SR_MOSCSELS) && (timeout++ < CLOCK_TIMEOUT));
#else
	/* QMOD has external 12MHz clock source */
	PIOB->PIO_PDR = (1 << 9);
	PIOB->PIO_PUDR = (1 << 9);
	PIOB->PIO_PPDDR = (1 << 9);
	PMC->CKGR_MOR = CKGR_MOR_KEY(0x37) | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTBY| CKGR_MOR_MOSCSEL;
#endif

	/* disable the red LED after main clock initialization */
	PIOA->PIO_SODR = PIO_LED_RED;

	/* "switch" to main clock as master clock source (should already be the case */
	PMC->PMC_MCKR = (PMC->PMC_MCKR & ~(uint32_t)PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_MAIN_CLK;
	/* wait for master clock to be ready */
	for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );

	/* Initialize PLLA */
	PMC->CKGR_PLLAR = BOARD_PLLAR;
	/* Wait for PLLA to lock */
	timeout = 0;
	while (!(PMC->PMC_SR & PMC_SR_LOCKA) && (timeout++ < CLOCK_TIMEOUT));

	/* Switch to main clock (again ?!?) */
	PMC->PMC_MCKR = (BOARD_MCKR & ~PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_MAIN_CLK;
	/* wait for master clock to be ready */
	for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );

	/* switch to PLLA as master clock source */
	PMC->PMC_MCKR = BOARD_MCKR ;
	/* wait for master clock to be ready */
	for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );

	/* Configure SysTick for 1ms */
	SysTick_Config(BOARD_MCK/1000);

	_ConfigureUsbClock();
}

/* SysTick based delay function */

volatile uint32_t jiffies;

/* Interrupt handler for SysTick interrupt */
void SysTick_Handler(void)
{
	jiffies++;
}

void mdelay(unsigned int msecs)
{
	uint32_t jiffies_start = jiffies;
	do {
	} while ((jiffies - jiffies_start) < msecs);
}