1 /***********************************************************************************************************************
\r
3 * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
\r
4 * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
\r
5 * applicable laws, including copyright laws.
\r
6 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
\r
7 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
\r
8 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
\r
9 * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
\r
10 * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
\r
11 * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
\r
12 * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
\r
13 * this software. By using this software, you agree to the additional terms and conditions found by accessing the
\r
15 * http://www.renesas.com/disclaimer
\r
17 * Copyright (C) 2012 Renesas Electronics Corporation. All rights reserved.
\r
18 ***********************************************************************************************************************/
\r
19 /***********************************************************************************************************************
\r
20 * File Name : resetprg.c
\r
22 * Description : Defines post-reset routines that are used to configure the MCU prior to the main program starting.
\r
23 * This is were the program counter starts on power-up or reset.
\r
24 ***********************************************************************************************************************/
\r
25 /***********************************************************************************************************************
\r
26 * History : DD.MM.YYYY Version Description
\r
27 * : 26.10.2011 1.00 First Release
\r
28 * : 13.03.2012 1.10 Stack sizes are now defined in r_bsp_config.h. Because of this the #include for
\r
29 * stacksct.h was removed. Settings for SCKCR are now set in r_bsp_config.h and used here
\r
30 * to setup clocks based on user settings.
\r
31 ***********************************************************************************************************************/
\r
33 /***********************************************************************************************************************
\r
34 Includes <System Includes> , "Project Includes"
\r
35 ***********************************************************************************************************************/
36 /* Defines machine level functions used in this file */
38 /* Defines MCU configuration functions used in this file */
40 /* Defines standard variable types used in this file */
44 /* This macro is here so that the stack will be declared here. This is used to prevent multiplication of stack size. */
\r
45 #define BSP_DECLARE_STACK
\r
46 /* Define the target platform */
\r
47 #include "platform.h"
\r
49 /***********************************************************************************************************************
\r
51 ***********************************************************************************************************************/
52 #define PSW_init (0x00030000)
53 #define FPSW_init (0x00000100)
55 /***********************************************************************************************************************
56 Pre-processor Directives
57 ***********************************************************************************************************************/
58 /* Declare the contents of the function 'Change_PSW_PM_to_UserMode' as
59 assembler to the compiler */
60 #pragma inline_asm Change_PSW_PM_to_UserMode
62 /* Set this as the entry point from a power-on reset */
63 #pragma entry PowerON_Reset_PC
\r
65 /***********************************************************************************************************************
66 External function Prototypes
67 ***********************************************************************************************************************/
68 /* Functions to setup I/O library */
69 extern void _INIT_IOLIB(void);
\r
70 extern void _CLOSEALL(void);
\r
72 /***********************************************************************************************************************
\r
73 Private global variables and functions
\r
74 ***********************************************************************************************************************/
75 /* Power-on reset function declaration */
76 void PowerON_Reset_PC(void);
\r
78 #if RUN_IN_USER_MODE==1
\r
79 #if __RENESAS_VERSION__ < 0x01010000
\r
80 /* MCU usermode switcher function declaration */
81 static void Change_PSW_PM_to_UserMode(void);
\r
85 /* Main program function delcaration */
87 static void operating_frequency_set(void);
89 /***********************************************************************************************************************
\r
90 * Function name: PowerON_Reset_PC
\r
91 * Description : This function is the MCU's entry point from a power-on reset.
\r
92 * The following steps are taken in the startup code:
\r
93 * 1. The User Stack Pointer (USP) and Interrupt Stack Pointer (ISP) are both set immediately after entry
\r
94 * to this function. The USP and ISP stack sizes are set in the file stacksct.h.
\r
95 * Default sizes are USP=4K and ISP=1K.
\r
96 * 2. The interrupt vector base register is set to point to the beginning of the relocatable interrupt
\r
98 * 3. The MCU is setup for floating point operations by setting the initial value of the Floating Point
\r
99 * Status Word (FPSW).
\r
100 * 4. The MCU operating frequency is set by configuring the Clock Generation Circuit (CGC) in
\r
101 * operating_frequency_set.
\r
102 * 5. Calls are made to functions to setup the C runtime environment which involves initializing all
\r
103 * initialed data, zeroing all uninitialized variables, and configuring STDIO if used
\r
104 * (calls to _INITSCT and _INIT_IOLIB).
\r
105 * 6. Board-specific hardware setup, including configuring I/O pins on the MCU, in hardware_setup.
\r
106 * 7. Global interrupts are enabled by setting the I bit in the Program Status Word (PSW), and the stack
\r
107 * is switched from the ISP to the USP. The initial Interrupt Priority Level is set to zero, enabling
\r
108 * any interrupts with a priority greater than zero to be serviced.
\r
109 * 8. The processor is optionally switched to user mode. To run in user mode, set the macro
\r
110 * RUN_IN_USER_MODE above to a 1.
\r
111 * 9. The bus error interrupt is enabled to catch any accesses to invalid or reserved areas of memory.
\r
113 * Once this initialization is complete, the user's main() function is called. It should not return.
\r
115 * Return value : none
\r
116 ***********************************************************************************************************************/
\r
117 void PowerON_Reset_PC(void)
119 /* Stack pointers are setup prior to calling this function - see comments above */
\r
121 /* Initialise the MCU processor word */
122 #if __RENESAS_VERSION__ >= 0x01010000
123 set_intb((void *)__sectop("C$VECT"));
125 set_intb((unsigned long)__sectop("C$VECT"));
128 /* Initialize FPSW for floating-point operations */
\r
130 #define _ROUND 0x00000001 /* Let FPSW RMbits=01 (round to zero) */
\r
132 #define _ROUND 0x00000000 /* Let FPSW RMbits=00 (round to nearest) */
\r
135 #define _DENOM 0x00000100 /* Let FPSW DNbit=1 (denormal as zero) */
\r
137 #define _DENOM 0x00000000 /* Let FPSW DNbit=0 (denormal as is) */
\r
139 set_fpsw(FPSW_init | _ROUND | _DENOM);
\r
141 /* Switch to high-speed operation */
\r
142 operating_frequency_set();
144 /* Initialize C runtime environment */
147 /* Comment this out if not using I/O lib
\r
150 /* Configure the MCU and YRDK hardware */
153 /* Change the MCU's usermode from supervisor to user */
156 #if RUN_IN_USER_MODE==1
\r
157 /* Use chg_pmusr() intrinsic if possible. */
\r
158 #if __RENESAS_VERSION__ >= 0x01010000
161 Change_PSW_PM_to_UserMode();
\r
166 /* Enable the bus error interrupt to catch accesses to illegal/reserved areas of memory */
\r
167 /* The ISR for this interrupt can be found in vecttbl.c in the function "bus_error_isr" */
\r
168 /* Clear any pending interrupts */
\r
169 IR(BSC,BUSERR) = 0;
\r
170 /* Make this the highest priority interrupt (adjust as necessary for your application */
\r
171 IPR(BSC,BUSERR) = 0x0F;
\r
172 /* Enable the interrupt in the ICU*/
\r
173 IEN(BSC,BUSERR) = 1;
\r
174 /* Enable illegal address interrupt in the BSC */
\r
175 BSC.BEREN.BIT.IGAEN = 1;
\r
177 /* Call the main program function (should not return) */
180 /* Comment this out if not using I/O lib - cleans up open files */
\r
185 /* Infinite loop. Put a breakpoint here if you want to catch an exit of main(). */
\r
189 /***********************************************************************************************************************
\r
190 * Function name: operating_frequency_set
\r
191 * Description : Configures the clock settings for each of the device clocks
\r
193 * Return value : none
\r
194 ***********************************************************************************************************************/
\r
195 void operating_frequency_set(void)
197 /* Used for constructing value to write to SCKCR register. */
\r
198 uint32_t temp_clock = 0;
\r
201 Clock Description Frequency
\r
202 ----------------------------------------
\r
203 Input Clock Frequency............ 12 MHz
\r
204 PLL frequency (x16).............. 192 MHz
\r
205 Internal Clock Frequency......... 96 MHz
\r
206 Peripheral Clock Frequency....... 48 MHz
\r
207 USB Clock Frequency.............. 48 MHz
\r
208 External Bus Clock Frequency..... 24 MHz */
\r
210 volatile unsigned int i;
\r
213 SYSTEM.PRCR.WORD = 0xA50B;
\r
215 /* Uncomment if not using sub-clock */
\r
216 //SYSTEM.SOSCCR.BYTE = 0x01; /* stop sub-clock */
\r
217 SYSTEM.SOSCCR.BYTE = 0x00; /* Enable sub-clock for RTC */
\r
219 /* Wait 131,072 cycles * 12 MHz = 10.9 ms */
\r
220 SYSTEM.MOSCWTCR.BYTE = 0x0D;
\r
222 /* PLL wait is 4,194,304 cycles (default) * 192 MHz (12 MHz * 16) = 20.1 ms*/
\r
223 SYSTEM.PLLWTCR.BYTE = 0x0F;
\r
225 /* Set PLL Input Divisor. */
\r
226 SYSTEM.PLLCR.BIT.PLIDIV = PLL_DIV >> 1;
\r
228 /* Set PLL Multiplier. */
\r
229 SYSTEM.PLLCR.BIT.STC = PLL_MUL - 1;
\r
232 SYSTEM.MOSCCR.BYTE = 0x00;
\r
235 SYSTEM.PLLCR2.BYTE = 0x00;
\r
237 for(i = 0;i< 0x168;i++)
\r
239 /* Wait over 12ms */
\r
243 /* Figure out setting for FCK bits. */
\r
245 /* Do nothing since FCK bits should be 0. */
\r
247 temp_clock |= 0x10000000;
\r
249 temp_clock |= 0x20000000;
\r
251 temp_clock |= 0x30000000;
\r
252 #elif FCK_DIV == 16
\r
253 temp_clock |= 0x40000000;
\r
254 #elif FCK_DIV == 32
\r
255 temp_clock |= 0x50000000;
\r
256 #elif FCK_DIV == 64
\r
257 temp_clock |= 0x60000000;
\r
259 #error "Error! Invalid setting for FCK_DIV in r_bsp_config.h"
\r
262 /* Figure out setting for ICK bits. */
\r
264 /* Do nothing since ICK bits should be 0. */
\r
266 temp_clock |= 0x01000000;
\r
268 temp_clock |= 0x02000000;
\r
270 temp_clock |= 0x03000000;
\r
271 #elif ICK_DIV == 16
\r
272 temp_clock |= 0x04000000;
\r
273 #elif ICK_DIV == 32
\r
274 temp_clock |= 0x05000000;
\r
275 #elif ICK_DIV == 64
\r
276 temp_clock |= 0x06000000;
\r
278 #error "Error! Invalid setting for ICK_DIV in r_bsp_config.h"
\r
281 /* SDCLK Pin Output and BCLK Pin Output are disabled by default. */
\r
282 temp_clock |= 0x00C00000;
\r
284 /* Figure out setting for BCK bits. */
\r
286 /* Do nothing since BCK bits should be 0. */
\r
288 temp_clock |= 0x00010000;
\r
290 temp_clock |= 0x00020000;
\r
292 temp_clock |= 0x00030000;
\r
293 #elif BCK_DIV == 16
\r
294 temp_clock |= 0x00040000;
\r
295 #elif BCK_DIV == 32
\r
296 temp_clock |= 0x00050000;
\r
297 #elif BCK_DIV == 64
\r
298 temp_clock |= 0x00060000;
\r
300 #error "Error! Invalid setting for BCK_DIV in r_bsp_config.h"
\r
303 /* Figure out setting for PCKA bits. */
\r
305 /* Do nothing since PCKA bits should be 0. */
\r
306 #elif PCKA_DIV == 2
\r
307 temp_clock |= 0x00001000;
\r
308 #elif PCKA_DIV == 4
\r
309 temp_clock |= 0x00002000;
\r
310 #elif PCKA_DIV == 8
\r
311 temp_clock |= 0x00003000;
\r
312 #elif PCKA_DIV == 16
\r
313 temp_clock |= 0x00004000;
\r
314 #elif PCKA_DIV == 32
\r
315 temp_clock |= 0x00005000;
\r
316 #elif PCKA_DIV == 64
\r
317 temp_clock |= 0x00006000;
\r
319 #error "Error! Invalid setting for PCKA_DIV in r_bsp_config.h"
\r
322 /* Figure out setting for PCKB bits. */
\r
324 /* Do nothing since PCKB bits should be 0. */
\r
325 #elif PCKB_DIV == 2
\r
326 temp_clock |= 0x00000100;
\r
327 #elif PCKB_DIV == 4
\r
328 temp_clock |= 0x00000200;
\r
329 #elif PCKB_DIV == 8
\r
330 temp_clock |= 0x00000300;
\r
331 #elif PCKB_DIV == 16
\r
332 temp_clock |= 0x00000400;
\r
333 #elif PCKB_DIV == 32
\r
334 temp_clock |= 0x00000500;
\r
335 #elif PCKB_DIV == 64
\r
336 temp_clock |= 0x00000600;
\r
338 #error "Error! Invalid setting for PCKB_DIV in r_bsp_config.h"
\r
341 /* Bottom byte of SCKCR register must be set to 0x11 */
\r
342 temp_clock |= 0x00000011;
\r
344 /* Set SCKCR register. */
\r
345 SYSTEM.SCKCR.LONG = temp_clock;
\r
347 /* Re-init temp_clock to use to set SCKCR2. */
\r
350 /* Figure out setting for IEBCK bits. */
\r
352 temp_clock |= 0x00000001;
\r
353 #elif IEBCK_DIV == 4
\r
354 temp_clock |= 0x00000002;
\r
355 #elif IEBCK_DIV == 6
\r
356 temp_clock |= 0x0000000C;
\r
357 #elif IEBCK_DIV == 8
\r
358 temp_clock |= 0x00000003;
\r
359 #elif IEBCK_DIV == 16
\r
360 temp_clock |= 0x00000004;
\r
361 #elif IEBCK_DIV == 32
\r
362 temp_clock |= 0x00000005;
\r
363 #elif IEBCK_DIV == 64
\r
364 temp_clock |= 0x00000006;
\r
366 #error "Error! Invalid setting for IEBCK_DIV in r_bsp_config.h"
\r
369 /* Figure out setting for UCK bits. */
\r
371 temp_clock |= 0x00000020;
\r
373 temp_clock |= 0x00000030;
\r
375 #error "Error! Invalid setting for UCK_DIV in r_bsp_config.h"
\r
378 /* Set SCKCR2 register. */
\r
379 SYSTEM.SCKCR2.WORD = (uint16_t)temp_clock;
\r
381 /* Choose clock source. Default for r_bsp_config.h is PLL. */
\r
382 SYSTEM.SCKCR3.WORD = ((uint16_t)CLOCK_SOURCE) << 8;
\r
385 SYSTEM.PRCR.WORD = 0xA500;
388 /***********************************************************************************************************************
\r
389 * Function name: Change_PSW_PM_to_UserMode
\r
390 * Description : Assembler function, used to change the MCU's usermode from supervisor to user.
\r
392 * Return value : none
\r
393 ***********************************************************************************************************************/
\r
394 #if RUN_IN_USER_MODE==1
\r
395 #if __RENESAS_VERSION__ < 0x01010000
396 static void Change_PSW_PM_to_UserMode(void)