]> git.sur5r.net Git - freertos/blob - Demo/RX600_RX63N-RSK_Renesas/RTOSDemo/Renesas-Files/board/rskrx63n/resetprg.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / RX600_RX63N-RSK_Renesas / RTOSDemo / Renesas-Files / board / rskrx63n / resetprg.c
1 /***********************************************************************************************************************\r
2 * DISCLAIMER\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
14 * following link:\r
15 * http://www.renesas.com/disclaimer \r
16 *\r
17 * Copyright (C) 2012 Renesas Electronics Corporation. All rights reserved.    \r
18 ***********************************************************************************************************************/\r
19 /***********************************************************************************************************************\r
20 * File Name        : resetprg.c\r
21 * Device(s)    : RX63x\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
32 \r
33 /***********************************************************************************************************************\r
34 Includes   <System Includes> , "Project Includes"\r
35 ***********************************************************************************************************************/
36 /* Defines machine level functions used in this file */
37 #include    <machine.h>
38 /* Defines MCU configuration functions used in this file */
39 #include    <_h_c_lib.h>
40 /* Defines standard variable types used in this file */
41 #include    <stdbool.h>
42 #include    <stdint.h>\r
43 \r
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
48 \r
49 /***********************************************************************************************************************\r
50 Macro definitions\r
51 ***********************************************************************************************************************/
52 #define PSW_init  (0x00030000)
53 #define FPSW_init (0x00000100)
54
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
61 \r
62 /* Set this as the entry point from a power-on reset */
63 #pragma entry PowerON_Reset_PC\r
64 \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
71
72 /***********************************************************************************************************************\r
73 Private global variables and functions\r
74 ***********************************************************************************************************************/
75 /* Power-on reset function declaration */
76 void PowerON_Reset_PC(void);\r
77 \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
82     #endif
83 #endif\r
84
85 /* Main program function delcaration */
86 void main(void);
87 static void operating_frequency_set(void);
88 \r
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
97 *                   vector table.\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
112 *\r
113 *                Once this initialization is complete, the user's main() function is called.  It should not return.\r
114 * Arguments    : none\r
115 * Return value : none\r
116 ***********************************************************************************************************************/\r
117 void PowerON_Reset_PC(void)
118 {\r
119     /* Stack pointers are setup prior to calling this function - see comments above */    \r
120     
121     /* Initialise the MCU processor word */
122 #if __RENESAS_VERSION__ >= 0x01010000    
123     set_intb((void *)__sectop("C$VECT"));
124 #else
125     set_intb((unsigned long)__sectop("C$VECT"));
126 #endif    \r
127 \r
128     /* Initialize FPSW for floating-point operations */\r
129 #ifdef __ROZ\r
130 #define _ROUND 0x00000001  /* Let FPSW RMbits=01 (round to zero) */\r
131 #else \r
132 #define _ROUND 0x00000000  /* Let FPSW RMbits=00 (round to nearest) */\r
133 #endif \r
134 #ifdef __DOFF \r
135 #define _DENOM 0x00000100  /* Let FPSW DNbit=1 (denormal as zero) */\r
136 #else \r
137 #define _DENOM 0x00000000  /* Let FPSW DNbit=0 (denormal as is) */\r
138 #endif 
139     set_fpsw(FPSW_init | _ROUND | _DENOM); \r
140     \r
141     /* Switch to high-speed operation */\r
142     operating_frequency_set();
143
144     /* Initialize C runtime environment */
145     _INITSCT();
146 \r
147     /* Comment this out if not using I/O lib\r
148     _INIT_IOLIB(); */\r
149
150     /* Configure the MCU and YRDK hardware */
151     hardware_setup();
152
153     /* Change the MCU's usermode from supervisor to user */        
154     nop();
155     set_psw(PSW_init);      
156 #if RUN_IN_USER_MODE==1\r
157     /* Use chg_pmusr() intrinsic if possible. */\r
158     #if __RENESAS_VERSION__ >= 0x01010000
159     chg_pmusr() ;\r
160     #else\r
161     Change_PSW_PM_to_UserMode();\r
162     #endif
163 #endif\r
164 \r
165 \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
176 \r
177     /* Call the main program function (should not return) */
178     main();\r
179     \r
180     /* Comment this out if not using I/O lib - cleans up open files */\r
181     _CLOSEALL();
182
183     while(1)\r
184     {\r
185         /* Infinite loop. Put a breakpoint here if you want to catch an exit of main(). */\r
186     }\r
187 }\r
188 \r
189 /***********************************************************************************************************************\r
190 * Function name: operating_frequency_set\r
191 * Description  : Configures the clock settings for each of the device clocks\r
192 * Arguments    : none\r
193 * Return value : none\r
194 ***********************************************************************************************************************/\r
195 void operating_frequency_set(void)
196 {
197     /* Used for constructing value to write to SCKCR register. */\r
198     uint32_t temp_clock = 0;\r
199     
200     /* \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
209 \r
210         volatile unsigned int i;\r
211 \r
212     /* Protect off. */\r
213     SYSTEM.PRCR.WORD = 0xA50B;                  \r
214         \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
218 \r
219     /* Wait 131,072 cycles * 12 MHz = 10.9 ms */\r
220     SYSTEM.MOSCWTCR.BYTE = 0x0D;                \r
221 \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
224 \r
225     /* Set PLL Input Divisor. */\r
226     SYSTEM.PLLCR.BIT.PLIDIV = PLL_DIV >> 1;\r
227 \r
228     /* Set PLL Multiplier. */\r
229     SYSTEM.PLLCR.BIT.STC = PLL_MUL - 1;\r
230 \r
231     /* EXTAL ON */\r
232     SYSTEM.MOSCCR.BYTE = 0x00;                  \r
233 \r
234     /* PLL ON */\r
235     SYSTEM.PLLCR2.BYTE = 0x00;                  \r
236 \r
237         for(i = 0;i< 0x168;i++)             \r
238     {\r
239         /* Wait over 12ms */\r
240         nop() ;\r
241         }\r
242 \r
243     /* Figure out setting for FCK bits. */\r
244 #if   FCK_DIV == 1\r
245     /* Do nothing since FCK bits should be 0. */\r
246 #elif FCK_DIV == 2\r
247     temp_clock |= 0x10000000;\r
248 #elif FCK_DIV == 4\r
249     temp_clock |= 0x20000000;\r
250 #elif FCK_DIV == 8\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
258 #else\r
259     #error "Error! Invalid setting for FCK_DIV in r_bsp_config.h"\r
260 #endif\r
261 \r
262     /* Figure out setting for ICK bits. */\r
263 #if   ICK_DIV == 1\r
264     /* Do nothing since ICK bits should be 0. */\r
265 #elif ICK_DIV == 2\r
266     temp_clock |= 0x01000000;\r
267 #elif ICK_DIV == 4\r
268     temp_clock |= 0x02000000;\r
269 #elif ICK_DIV == 8\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
277 #else\r
278     #error "Error! Invalid setting for ICK_DIV in r_bsp_config.h"\r
279 #endif\r
280 \r
281     /* SDCLK Pin Output and BCLK Pin Output are disabled by default. */\r
282     temp_clock |= 0x00C00000;\r
283 \r
284     /* Figure out setting for BCK bits. */\r
285 #if   BCK_DIV == 1\r
286     /* Do nothing since BCK bits should be 0. */\r
287 #elif BCK_DIV == 2\r
288     temp_clock |= 0x00010000;\r
289 #elif BCK_DIV == 4\r
290     temp_clock |= 0x00020000;\r
291 #elif BCK_DIV == 8\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
299 #else\r
300     #error "Error! Invalid setting for BCK_DIV in r_bsp_config.h"\r
301 #endif\r
302 \r
303     /* Figure out setting for PCKA bits. */\r
304 #if   PCKA_DIV == 1\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
318 #else\r
319     #error "Error! Invalid setting for PCKA_DIV in r_bsp_config.h"\r
320 #endif\r
321 \r
322     /* Figure out setting for PCKB bits. */\r
323 #if   PCKB_DIV == 1\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
337 #else\r
338     #error "Error! Invalid setting for PCKB_DIV in r_bsp_config.h"\r
339 #endif\r
340 \r
341     /* Bottom byte of SCKCR register must be set to 0x11 */\r
342     temp_clock |= 0x00000011;\r
343 \r
344     /* Set SCKCR register. */\r
345     SYSTEM.SCKCR.LONG = temp_clock;\r
346     \r
347     /* Re-init temp_clock to use to set SCKCR2. */\r
348     temp_clock = 0;\r
349 \r
350     /* Figure out setting for IEBCK bits. */\r
351 #if   IEBCK_DIV == 2\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
365 #else\r
366     #error "Error! Invalid setting for IEBCK_DIV in r_bsp_config.h"\r
367 #endif\r
368 \r
369     /* Figure out setting for UCK bits. */\r
370 #if   UCK_DIV == 3\r
371     temp_clock |= 0x00000020;\r
372 #elif UCK_DIV == 4\r
373     temp_clock |= 0x00000030;\r
374 #else\r
375     #error "Error! Invalid setting for UCK_DIV in r_bsp_config.h"\r
376 #endif\r
377 \r
378     /* Set SCKCR2 register. */\r
379     SYSTEM.SCKCR2.WORD = (uint16_t)temp_clock;\r
380 \r
381     /* Choose clock source. Default for r_bsp_config.h is PLL. */\r
382     SYSTEM.SCKCR3.WORD = ((uint16_t)CLOCK_SOURCE) << 8;\r
383 \r
384     /* Protect on. */\r
385     SYSTEM.PRCR.WORD = 0xA500;                  
386 }
387 \r
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
391 * Arguments    : none\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)
397 {
398     MVFC   PSW,R1
399     OR     #00100000h,R1
400     PUSH.L R1
401     MVFC   PC,R1
402     ADD    #10,R1
403     PUSH.L R1
404     RTE
405     NOP
406     NOP
407 }\r
408     #endif
409 #endif