1 /* Adapted for use with IAR Embedded Workbench */
2 /***********************************************************************************************************************
3 * DISCLAIMER
4 * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
5 * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
6 * applicable laws, including copyright laws.
7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
10 * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
11 * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
12 * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
13 * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
14 * this software. By using this software, you agree to the additional terms and conditions found by accessing the
15 * following link:
16 * http://www.renesas.com/disclaimer
17 *
18 * Copyright (C) 2012 Renesas Electronics Corporation. All rights reserved.
19 ***********************************************************************************************************************/
20 /***********************************************************************************************************************
21 * File Name     : hwsetup.c
22 * Version      : 1.0
23 * Device(s)    : RX
24 * H/W Platform : RX65N
25 * Description  : Defines the initialisation routines used each time the MCU is restarted.
26 ***********************************************************************************************************************/
27 
28 /***********************************************************************************************************************
29 Includes   <System Includes> , "Project Includes"
30 ***********************************************************************************************************************/
31 #include <stdint.h>
32 #include <intrinsics.h>
33 
34 /* Contains delcarations for the functions defined in this file */
35 #include "hwsetup.h"
36 
37 /***********************************************************************************************************************
38 Private global variables and functions
39 ***********************************************************************************************************************/
40 /* Clock configuration function delcaration */
41 void operating_frequency_set(void);
42 
43 /* MCU I/O port configuration function delcaration */
44 //static void output_ports_configure(void);
45 
46 /* Interrupt configuration function delcaration */
47 static void interrupts_configure(void);
48 
49 /* MCU peripheral module configuration function declaration */
50 //static void peripheral_modules_enable(void);
51 
52 
53 /*******************************************************************************
54 * Function name: hardware_setup
55 * Description  : Contains setup functions called at device restart
56 * Arguments    : none
57 * Return value : none
58 *******************************************************************************/
hardware_setup(void)59 void hardware_setup(void)
60 {
61   operating_frequency_set();
62   interrupts_configure();
63 }
64 
65 
66 
67 /*******************************************************************************
68 * Function name: operating_frequency_set
69 * Description  : Configures the clock settings for each of the device clocks
70 * Arguments    : none
71 * Return value : none
72 *******************************************************************************/
operating_frequency_set(void)73 void operating_frequency_set(void)
74 {
75 
76  /*
77   Clock Description              Frequency
78   ----------------------------------------
79   Input Clock Frequency............  24 MHz
80   Divisor..........................       2
81   PLL frequency (x16).............. 192 MHz
82   Internal Clock Frequency.........  96 MHz
83   Peripheral Clock Frequency.......  48 MHz
84   USB Clock Frequency..............  48 MHz
85   External Bus Clock Frequency.....  24 MHz */
86 
87   volatile unsigned int i;
88 
89   /* Protect off. */
90   SYSTEM.PRCR.WORD = 0xA50B;
91 
92   /* Uncomment if not using sub-clock */
93   //SYSTEM.SOSCCR.BYTE = 0x01;          /* stop sub-clock */
94   SYSTEM.SOSCCR.BYTE = 0x00;            /* Enable sub-clock for RTC */
95 
96   /* Wait 131,072 cycles * 12 MHz = 10.9 ms */
97   SYSTEM.MOSCWTCR.BYTE = 0x0D;
98 
99   /* x16 @PLL, 2 divisor */
100   SYSTEM.PLLCR.WORD = 0x0F01;
101 
102   /* EXTAL ON */
103   SYSTEM.MOSCCR.BYTE = 0x00;
104 
105   /* PLL ON */
106   SYSTEM.PLLCR2.BYTE = 0x00;
107 
108   for(i = 0;i< 0x168;i++)
109   {
110     /* Wait over 12ms */
111     __no_operation();
112   }
113 
114   /* Setup system clocks
115   SCKCR - System Clock Control Register
116   b31:b28 FCK[3:0]  0x02 = Flash clock: PLL/4 = (192 / 4) = 48 MHz
117   b27:b24 ICK[3:0]  0x01 = System clock: PLL/2 = (192 / 2) = 96 MHz
118   b23     PSTOP1    0x00 = BCLK pin output is enabled
119   b19:b16 BCK[3:0]  0x03 = BCLK: PLL/8 = 24 MHz
120   b11:b8  PCKB[3:0] 0x02 = Peripheral clock B: PLL/4 = 48 MHz
121   */
122   SYSTEM.SCKCR.LONG = 0x21031222;  /* ICK=PLL/2,BCK,FCK,PCK=PLL/4 */
123 
124   /* Setup IEBUS and USB clocks
125   SCKCR2 - System Clock Control Register 2
126   b7:b4 UCK[3:0]   0x03 = USB clock is PLL/4 = 48 MHz
127   b3:b0 IEBCK[3:0] 0x01 = IE Bus clock is PLL/2 = 96 MHz
128   */
129   SYSTEM.SCKCR2.WORD = 0x0031;
130 
131   /* ICLK, PCLKB, FCLK, BCLK, IECLK, and USBCLK all come from PLL circuit */
132   SYSTEM.SCKCR3.WORD = 0x0400;
133 
134   /* Protect on. */
135   SYSTEM.PRCR.WORD = 0xA500;
136 }
137 
138 
139 /***********************************************************************************************************************
140 * Function name: interrupts_configure
141 * Description  : Configures interrupts used
142 * Arguments    : none
143 * Return value : none
144 ***********************************************************************************************************************/
interrupts_configure(void)145 void interrupts_configure(void)
146 {
147     /* Protect off. */
148   SYSTEM.PRCR.WORD = 0xA50B;
149 
150   /* Enable the bus error interrupt to catch accesses to illegal/reserved areas
151      of memory. */
152 
153   /* Clear any pending interrupts */
154   IR(BSC,BUSERR) = 0;
155   /* Make this the highest priority interrupt (adjust as necessary for your
156      application) */
157   IPR(BSC,BUSERR) = 0x0F;
158   /* Enable the interrupt in the ICU */
159   IEN(BSC,BUSERR) = 1;
160   /* Enable illegal address interrupt in the BSC */
161   BSC.BEREN.BIT.IGAEN = 1;
162 }
163 
164 
165 /******************************************************************************
166 * Function name: buserr_isr
167 * Description  : Sample ISR for bus error (must do hardware setup first!)
168 *                By default, this demo code enables the Bus Error Interrupt.
169 *                This interrupt will fire if the user tries to access code
170 *                or data from one of the reserved areas in the memory map,
171 *                including the areas covered by disabled chip selects.
172 *                A nop statement is included here as a convenient place
173 *                to set a breakpoint during debugging and development, and
174 *                further handling should be added by the user for their
175 *                application.
176 * Arguments    : none
177 * Return value : none
178 ******************************************************************************/
179 #pragma vector = VECT_BSC_BUSERR
buserr_isr(void)180 __interrupt void buserr_isr(void)
181 {
182   /* To find the address that was accessed when the bus error occured, read
183      the register BSC.BERSR2.WORD.  The upper 13 bits of this register
184      contain the upper 13-bits of the offending address (in 512K byte units).
185   */
186 
187   /* Add your own code here to handle this interrupt */
188   __no_operation();
189 }
190