1 /*******************************************************************************
\r
3 * This software is supplied by Renesas Electronics Corporation and is only
\r
4 * intended for use with Renesas products. No other uses are authorized. This
\r
5 * software is owned by Renesas Electronics Corporation and is protected under
\r
6 * all applicable laws, including copyright laws.
\r
7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
\r
8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
\r
9 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
\r
10 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
\r
11 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
\r
12 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
\r
13 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
\r
14 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
\r
15 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
\r
16 * Renesas reserves the right, without notice, to make changes to this software
\r
17 * and to discontinue the availability of this software. By using this software,
\r
18 * you agree to the additional terms and conditions found by accessing the
\r
20 * http://www.renesas.com/disclaimer
\r
22 * Copyright (C) 2014 Renesas Electronics Corporation. All rights reserved.
\r
23 *******************************************************************************/
\r
24 /*******************************************************************************
\r
25 * System Name : RZ/T1 Init program
\r
26 * File Name : r_ecm.c
\r
28 * Device : R7S9100xx
\r
29 * Abstract : API for ECM function
\r
30 * Tool-Chain : IAR Embedded Workbench Ver.7.20
\r
32 * H/W Platform : Renesas Starter Kit for RZ/T1(Preliminary)
\r
33 * Description : ECM API of RZ/T1
\r
34 * Limitation : LOCO operation is necessary for clearing ERROROUT# pin.
\r
35 *******************************************************************************/
\r
36 /*******************************************************************************
\r
37 * History : DD.MM.YYYY Version Description
\r
39 *******************************************************************************/
\r
41 /*******************************************************************************
\r
42 Includes <System Includes> , "Project Includes"
\r
43 *******************************************************************************/
\r
45 #include <Renesas/ior7s910017.h>
\r
46 #include "r_system.h"
\r
48 #include "r_reset.h"
\r
49 #include "r_icu_init.h"
\r
51 /*******************************************************************************
\r
53 *******************************************************************************/
\r
54 #define ECM_CMT0_CLOCK_PCLKD_32 (1)
\r
55 #define ECM_CMT0_CMI0_ENABLE (1)
\r
56 #define ECM_CMT0_CONST_15_us (0x22)
\r
57 #define ECM_CMT0_START (1)
\r
58 #define ECM_CMT0_STOP (0)
\r
60 #define ECM_CMT_REG_CLEAR (0x0000)
\r
62 /*******************************************************************************
\r
64 *******************************************************************************/
\r
68 /*******************************************************************************
\r
69 Imported global variables and functions (from other files)
\r
70 *******************************************************************************/
\r
73 /*******************************************************************************
\r
74 Exported global variables and functions (to be accessed by other files)
\r
75 *******************************************************************************/
\r
79 /*******************************************************************************
\r
80 Private variables and functions
\r
81 *******************************************************************************/
\r
82 static uint32_t *g_pcmd_reg_adrr[ECM_TYPE_MAX] =
\r
84 (uint32_t *) &ECMM.ECMMPCMD0.LONG,
\r
85 (uint32_t *) &ECMC.ECMCPCMD0.LONG,
\r
86 (uint32_t *) &ECM.ECMPCMD1.LONG
\r
89 /*******************************************************************************
\r
90 * Function Name : R_ECM_Init
\r
91 * Description : Initialize ECM function.
\r
92 * - Clear all error source
\r
93 * - Clear ERROROUT# pin output to in-active (High) level.
\r
95 * Return Value : none
\r
96 *******************************************************************************/
\r
97 void R_ECM_Init(void)
\r
99 volatile uint8_t result;
\r
101 /* Clear all error source (ECMESSTC0, ECMESSTC1, ECMESSTC2) */
\r
102 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMESSTC0.LONG), 0xDFFFFFF7);
\r
103 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMESSTC1.LONG), 0x000001FF);
\r
104 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMESSTC2.LONG), 0x70000000);
\r
106 /* Mask all error source (ECMEMK0, ECMEMK1, ECMEMK2) for clearing ERROROUT# */
\r
107 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMEMK0.LONG), 0xDFFFFFF7);
\r
108 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMEMK1.LONG), 0x000001FF);
\r
109 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMEMK2.LONG), 0x30000000);
\r
111 /* Mask ECM maskable, non-maskable interrupt and ECM reset of ECM compare match
\r
112 error (ECMMICFG2, ECMNMICFG2, ECMIRCFG2) */
\r
113 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMMICFG2.LONG), 0x00000000);
\r
114 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMNMICFG2.LONG), 0x00000000);
\r
115 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMIRCFG2.LONG), 0x00000000);
\r
117 /* Clear ERROROUT# pin output to in-active (High) level */
\r
118 result = R_ECM_Write_Reg8(ECM_MASTER, &(ECMM.ECMMECLR.BYTE), 0x01);
\r
119 result = R_ECM_Write_Reg8(ECM_CHECKER, &(ECMC.ECMCECLR.BYTE), 0x01);
\r
121 /* Wait 15us for ECM compare error stabilization */
\r
122 R_ECM_CompareError_Wait();
\r
124 /* Clear ECM compare error (ECMESSTC2) again */
\r
125 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMESSTC2.LONG), 0x10000000);
\r
127 /* Initialize the all error mask settings (ECMEMK0, ECMEMK1, ECMEMK2) */
\r
128 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMEMK0.LONG), 0x00000000);
\r
129 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMEMK1.LONG), 0x00000000);
\r
130 result = R_ECM_Write_Reg32(ECM_COMMON, &(ECM.ECMEMK2.LONG), 0x00000000);
\r
134 /*******************************************************************************
\r
135 End of function R_ECM_Init
\r
136 *******************************************************************************/
\r
138 /*******************************************************************************
\r
139 * Function Name : R_ECM_CompareError_Wait
\r
140 * Description : Wait about 15 us for ECM compare error stabilizeation by using CMT0
\r
142 * Return Value : none
\r
143 *******************************************************************************/
\r
144 void R_ECM_CompareError_Wait(void)
\r
146 /* Enables writing to the registers related to Reset and Low-Power function */
\r
147 r_rst_write_enable();
\r
149 /* Release from the CMT0 module-stop state */
\r
152 /* Disables writing to the registers related to Reset and Low-Power function */
\r
153 r_rst_write_disable();
\r
155 /* Set CMT0 to 100us interval operation */
\r
156 CMT0.CMCR.BIT.CKS = ECM_CMT0_CLOCK_PCLKD_32; // Count clock = PCLKD/32
\r
157 CMT0.CMCR.BIT.CMIE = ECM_CMT0_CMI0_ENABLE; // Enable CMI0 interrupt
\r
158 CMT0.CMCNT = ECM_CMT_REG_CLEAR; // Clear CMCNT counter
\r
159 CMT0.CMCOR = ECM_CMT0_CONST_15_us; // Set constant value for 15us
\r
162 /* Set IRQ21(CMI0) for polloing sequence */
\r
163 VIC.IEC0.BIT.IEC21 = ICU_IEC_MASK_SET; // Mask IRQ21 interrupt
\r
164 VIC.PLS0.BIT.PLS21 = ICU_TYPE_EDGE; // Set EDGE type interrupt
\r
165 VIC.PIC0.BIT.PIC21 = ICU_PIC_EDGE_CLEAR; // Clear interrupt detection edge
\r
167 /* Enable IRQ interrupt (Clear CPSR.I bit to 0) */
\r
168 asm("cpsie i"); // Clear CPSR.I bit to 0
\r
169 asm("isb"); // Ensuring Context-changing
\r
171 /* Start CMT0 count */
\r
172 CMT.CMSTR0.BIT.STR0 = ECM_CMT0_START;
\r
174 /* Wait for 15us (IRQ21 is generated) */
\r
175 while ( ! (VIC.RAIS0.BIT.RAI21) )
\r
180 /* Stop CMT0 count */
\r
181 CMT.CMSTR0.BIT.STR0 = ECM_CMT0_STOP;
\r
183 /* Initialize CMT0 settings and clear interrupt detection edge */
\r
184 CMT0.CMCR.WORD = ECM_CMT_REG_CLEAR;
\r
185 CMT0.CMCNT = ECM_CMT_REG_CLEAR;
\r
186 CMT0.CMCOR = ECM_CMT_REG_CLEAR;
\r
187 CMT.CMSTR0.WORD = ECM_CMT_REG_CLEAR;
\r
189 VIC.PIC0.BIT.PIC21 = ICU_PIC_EDGE_CLEAR; // Clear interrupt detection edge
\r
192 /* Disable IRQ interrupt (Set CPSR.I bit to 1) */
\r
196 /* Enables writing to the registers related to Reset and Low-Power function */
\r
197 r_rst_write_enable();
\r
199 /* Set CMT0 to module-stop state */
\r
202 /* Disables writing to the registers related to Reset and Low-Power function */
\r
203 r_rst_write_disable();
\r
207 /*******************************************************************************
\r
208 End of function R_ECM_CompareError_Wait
\r
209 *******************************************************************************/
\r
212 /*******************************************************************************
\r
213 * Function Name : R_ECM_Write_Reg8
\r
214 * Description : Writing the special sequence for 8-bit ECM protected register
\r
215 * Arguments : reg_type
\r
216 * The type of ECM register (ECM_MASETR, ECM_CHECKER, ECM_COMMON)
\r
218 * The address of ECM protected register
\r
220 * The 8-bit value of writing to protected register
\r
221 * Return Value : none
\r
222 *******************************************************************************/
\r
223 uint8_t R_ECM_Write_Reg8( uint8_t reg_type, volatile unsigned char *reg, uint8_t value)
\r
226 volatile uint8_t dummy_8;
\r
227 volatile uint32_t dummy_32;
\r
229 /* Special write sequence */
\r
230 *g_pcmd_reg_adrr[reg_type] = ECM_COMMAND_KEY; // Write fixed value
\r
231 dummy_32 = *g_pcmd_reg_adrr[reg_type];
\r
233 *reg = value; // Write expected value
\r
234 *reg = ~value; // Write inversed value of the expected value
\r
235 *reg = value; // Write expected value
\r
238 /* Check the ECMPS register whether special sequence is success or failure
\r
239 result = 0 : Special sequence is success.
\r
240 = 1 : Special sequence is failure. */
\r
241 result = ECM.ECMPS.BYTE;
\r
246 /*******************************************************************************
\r
247 End of function R_ECM_Write_Reg8
\r
248 *******************************************************************************/
\r
250 /*******************************************************************************
\r
251 * Function Name : R_ECM_Write_Reg32
\r
252 * Description : Writing the special sequence for 32-bit ECM protected register
\r
253 * Arguments : reg_type
\r
254 * The type of ECM register (ECM_MASETR, ECM_CHECKER, ECM_COMMON)
\r
256 * The address of ECM protected register
\r
258 * The 32-bit value of writing to protected register
\r
259 * Return Value : none
\r
260 *******************************************************************************/
\r
261 uint8_t R_ECM_Write_Reg32( uint8_t reg_type, volatile unsigned long *reg, uint32_t value)
\r
264 volatile uint32_t dummy_32;
\r
266 /* Special write sequence */
\r
267 *g_pcmd_reg_adrr[reg_type] = ECM_COMMAND_KEY; // Write fixed value
\r
268 dummy_32 = *g_pcmd_reg_adrr[reg_type];
\r
270 *reg = value; // Write expected value
\r
271 *reg = ~value; // Write inversed value of the expected value
\r
272 *reg = value; // Write expected value
\r
275 /* Check the ECMPS register whether special sequence is success or failure
\r
276 result = 0 : Special sequence is success.
\r
277 = 1 : Special sequence is failure. */
\r
278 result = ECM.ECMPS.BYTE;
\r
283 /*******************************************************************************
\r
284 End of function R_ECM_Write_Reg32
\r
285 *******************************************************************************/
\r