1 /***********************************************************************************************************************
2  * Copyright [2015-2017] Renesas Electronics Corporation and/or its licensors. All Rights Reserved.
3  *
4  * This file is part of Renesas SynergyTM Software Package (SSP)
5  *
6  * The contents of this file (the "contents") are proprietary and confidential to Renesas Electronics Corporation
7  * and/or its licensors ("Renesas") and subject to statutory and contractual protections.
8  *
9  * This file is subject to a Renesas SSP license agreement. Unless otherwise agreed in an SSP license agreement with
10  * Renesas: 1) you may not use, copy, modify, distribute, display, or perform the contents; 2) you may not use any name
11  * or mark of Renesas for advertising or publicity purposes or in connection with your use of the contents; 3) RENESAS
12  * MAKES NO WARRANTY OR REPRESENTATIONS ABOUT THE SUITABILITY OF THE CONTENTS FOR ANY PURPOSE; THE CONTENTS ARE PROVIDED
13  * "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT; AND 4) RENESAS SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, OR
15  * CONSEQUENTIAL DAMAGES, INCLUDING DAMAGES RESULTING FROM LOSS OF USE, DATA, OR PROJECTS, WHETHER IN AN ACTION OF
16  * CONTRACT OR TORT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE CONTENTS. Third-party contents
17  * included in this file may be subject to different terms.
18  **********************************************************************************************************************/
19 
20 /**********************************************************************************************************************
21  * File Name    : hw_flash_common.c
22  * Description  : Common functions used by both the Code and Data Low Power Flash modules
23  **********************************************************************************************************************/
24 
25 /******************************************************************************
26  * Includes   <System Includes> , “Project Includes”
27  ******************************************************************************/
28 #include "bsp_api.h"
29 #include "r_flash_lp.h"
30 #include "../hw_flash_lp_private.h"
31 #include "r_flash_cfg.h"
32 #include "hw_flash_common.h"
33 #include "hw_dataflash.h"
34 #include "hw_codeflash.h"
35 #include "hw_codeflash_extra.h"
36 
37 /*******************************************************************************************************************//**
38  * @addtogroup FLASH
39  * @{
40  **********************************************************************************************************************/
41 /*******************************************************************************************************************//**
42  * @} (end FLASH)
43  **********************************************************************************************************************/
44 
45 /******************************************************************************
46  * Macro definitions
47  ******************************************************************************/
48 #if defined(__ICCARM__)
49 #define BSP_ATTRIBUTE_STACKLESS __stackless
50 #elif defined(__GNUC__)
51 /*LDRA_INSPECTED 293 s - This is an allowed exception to LDRA standard 293 S "Non ANSI/ISO construct used. "*/
52 #define BSP_ATTRIBUTE_STACKLESS __attribute__((naked))
53 #endif
54 
55 /******************************************************************************
56  * Private global variables and functions
57  ******************************************************************************/
58 
59 BSP_ATTRIBUTE_STACKLESS static void HW_FLASH_LP_delay_loop (uint32_t n) PLACE_IN_RAM_SECTION;
60 
61 // Roughly 4 cycles per loop
62 #define DELAY_LOOP_CYCLES 4U
HW_FLASH_LP_delay_loop(uint32_t n)63 BSP_ATTRIBUTE_STACKLESS static void HW_FLASH_LP_delay_loop (uint32_t n)
64 {
65     SSP_PARAMETER_NOT_USED(n);      /// prevent compiler 'unused' warning
66     __asm volatile ("label1:\n"
67 #if defined(__ICCARM__)
68                     "   subs r0, #1         \n"     ///< 1 cycle
69 #elif defined(__GNUC__)
70                     "   sub r0, r0, #1      \n"     ///< 1 cycle
71 #endif
72                     "cmp r0, #0\n"              //  1 cycle
73 /* CM0 has a different instruction set */
74 #if defined(__CORE_CM0PLUS_H_GENERIC) || defined(__CORE_ARMV8MBL_H_GENERIC)
75                     "   bne label1   \n"     ///< 2 cycles
76 #else
77                     "   bne.n label1 \n"     ///< 2 cycles
78 #endif
79                     "bx lr\n");                 // ~2 cycles
80 }
81 
82 
83 /*******************************************************************************
84  * Outline      : Function that specifies the execution time
85  * Header       : none
86  * Function Name: r_flash_delay_us
87  * Description  : The number of loops is calculated based on the execution time (micro secs)
88  *              : and the sytem clock (ICLK) frequency, and the intrinsic function
89  *              : that specifies the number of loops is called.
90  * Arguments    : us  : Execution time
91  *             : mhz : ICLK frequency (in Mhz)
92  * Return Value : none
93  *******************************************************************************/
HW_FLASH_LP_delay_us(uint32_t us,uint32_t mhz)94 void HW_FLASH_LP_delay_us (uint32_t us, uint32_t mhz)
95 {
96     uint32_t loop_cnt;
97 
98     // @12 MHz, one loop is 332 ns. A delay of 5 us would require 15 loops. 15 * 332 = 4980 ns or ~ 5us
99     /* Calculation of a loop count */
100     loop_cnt = ((us * mhz) / DELAY_LOOP_CYCLES);
101 
102     if (loop_cnt > 0U)
103     {
104         HW_FLASH_LP_delay_loop(loop_cnt);
105     }
106 }
107