1 /*
2 * Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 
7 /***********************************************************************************************************************
8  * Includes   <System Includes> , "Project Includes"
9  **********************************************************************************************************************/
10 #include "bsp_api.h"
11 
12 #if (2 == BSP_CFG_RTOS)                // FreeRTOS
13  #include "FreeRTOS.h"
14 #endif
15 
16 /***********************************************************************************************************************
17  * Macro definitions
18  **********************************************************************************************************************/
19 #if defined(__ICCARM__)
20  #define WEAK_ERROR_ATTRIBUTE
21  #define WEAK_INIT_ATTRIBUTE
22  #pragma weak fsp_error_log                            = fsp_error_log_internal
23  #pragma weak bsp_init                                 = bsp_init_internal
24 #elif defined(__GNUC__)
25 
26  #define WEAK_ERROR_ATTRIBUTE    __attribute__((weak, alias("fsp_error_log_internal")))
27 
28  #define WEAK_INIT_ATTRIBUTE     __attribute__((weak, alias("bsp_init_internal")))
29 #endif
30 
31 #define FSP_SECTION_VERSION      ".version"
32 
33 /***********************************************************************************************************************
34  * Typedef definitions
35  **********************************************************************************************************************/
36 
37 /***********************************************************************************************************************
38  * Exported global variables (to be accessed by other files)
39  **********************************************************************************************************************/
40 
41 /* System clock frequency information for CKIO */
42 #if (1 == BSP_FEATURE_CGC_SCKCR_TYPE)
43 const uint32_t g_bsp_system_clock_select_ckio[][2] =
44 {
45     {BSP_PRV_CKIO_FREQ_100_MHZ,       BSP_PRV_CKIO_FREQ_75_MHZ       }, // CKIO = 000b
46     {BSP_PRV_CKIO_FREQ_66_7_MHZ,      BSP_PRV_CKIO_FREQ_50_MHZ       }, // CKIO = 001b
47     {BSP_PRV_CKIO_FREQ_50_MHZ,        BSP_PRV_CKIO_FREQ_37_5_MHZ     }, // CKIO = 010b
48     {BSP_PRV_CKIO_FREQ_40_MHZ,        BSP_PRV_CKIO_FREQ_30_MHZ       }, // CKIO = 011b
49     {BSP_PRV_CKIO_FREQ_33_3_MHZ,      BSP_PRV_CKIO_FREQ_25_MHZ       }, // CKIO = 100b
50     {BSP_PRV_CKIO_FREQ_28_6_MHZ,      BSP_PRV_CKIO_FREQ_21_4_MHZ     }, // CKIO = 101b
51     {BSP_PRV_CKIO_FREQ_25_MHZ,        BSP_PRV_CKIO_FREQ_18_75_MHZ    }, // CKIO = 110b
52     {BSP_PRV_CKIO_FREQ_NOT_SUPPORTED, BSP_PRV_CKIO_FREQ_NOT_SUPPORTED}, // CKIO = 111b
53 };
54 #elif (2 == BSP_FEATURE_CGC_SCKCR_TYPE)
55 const uint32_t g_bsp_system_clock_select_ckio[] =
56 {
57     BSP_PRV_CKIO_FREQ_125_MHZ,         // CKIO = 000b
58     BSP_PRV_CKIO_FREQ_83_3_MHZ,        // CKIO = 001b
59     BSP_PRV_CKIO_FREQ_62_5_MHZ,        // CKIO = 010b
60     BSP_PRV_CKIO_FREQ_50_MHZ,          // CKIO = 011b
61     BSP_PRV_CKIO_FREQ_41_7_MHZ,        // CKIO = 100b
62     BSP_PRV_CKIO_FREQ_35_7_MHZ,        // CKIO = 101b
63     BSP_PRV_CKIO_FREQ_31_25_MHZ,       // CKIO = 110b
64     BSP_PRV_CKIO_FREQ_NOT_SUPPORTED,   // CKIO = 111b
65 };
66 #endif
67 
68 /* System clock frequency information for XSPI_CLK */
69 const uint32_t g_bsp_system_clock_select_xspi_clk[][2] =
70 {
71     {BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED, BSP_PRV_XSPI_CLK_FREQ_150_MHZ      }, // FSELXSPIn = 000b
72     {BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED, BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED}, // FSELXSPIn = 001b
73     {BSP_PRV_XSPI_CLK_FREQ_133_3_MHZ,     BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED}, // FSELXSPIn = 010b
74     {BSP_PRV_XSPI_CLK_FREQ_100_MHZ,       BSP_PRV_XSPI_CLK_FREQ_75_MHZ       }, // FSELXSPIn = 011b
75     {BSP_PRV_XSPI_CLK_FREQ_50_MHZ,        BSP_PRV_XSPI_CLK_FREQ_37_5_MHZ     }, // FSELXSPIn = 100b
76     {BSP_PRV_XSPI_CLK_FREQ_25_MHZ,        BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED}, // FSELXSPIn = 101b
77     {BSP_PRV_XSPI_CLK_FREQ_12_5_MHZ,      BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED}, // FSELXSPIn = 110b
78     {BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED, BSP_PRV_XSPI_CLK_FREQ_NOT_SUPPORTED}, // FSELXSPIn = 111b
79 };
80 
81 /* System clock frequency information for SPI_CLK */
82 #if (1 == BSP_FEATURE_CGC_PCLKSPI_CLOCK_FREQ_TYPE)
83 const uint32_t g_bsp_system_clock_select_spi_clk[] =
84 {
85     BSP_PRV_PCLKSPI_FREQ_75_MHZ,       // SPInASYNCSEL = 0b
86     BSP_PRV_PCLKSPI_FREQ_96_MHZ,       // SPInASYNCSEL = 1b
87 };
88 #elif (2 == BSP_FEATURE_CGC_PCLKSCI_CLOCK_FREQ_TYPE)
89 const uint32_t g_bsp_system_clock_select_spi_clk[] =
90 {
91     BSP_PRV_PCLKSPI_FREQ_75_MHZ,       // SPInASYNCSEL = 00b
92     BSP_PRV_PCLKSPI_FREQ_80_MHZ,       // SPInASYNCSEL = 01b
93     BSP_PRV_PCLKSPI_FREQ_96_MHZ,       // SPInASYNCSEL = 10b
94     BSP_PRV_PCLKSPI_FREQ_100_MHZ,      // SPInASYNCSEL = 11b
95 };
96 #endif
97 
98 /* System clock frequency information for SCI_CLK */
99 #if (1 == BSP_FEATURE_CGC_PCLKSCI_CLOCK_FREQ_TYPE)
100 const uint32_t g_bsp_system_clock_select_sci_clk[] =
101 {
102     BSP_PRV_PCLKSCI_FREQ_75_MHZ,       // SCInASYNCSEL = 0b
103     BSP_PRV_PCLKSCI_FREQ_96_MHZ,       // SCInASYNCSEL = 1b
104 };
105 #elif (2 == BSP_FEATURE_CGC_PCLKSCI_CLOCK_FREQ_TYPE)
106 const uint32_t g_bsp_system_clock_select_sci_clk[] =
107 {
108     BSP_PRV_PCLKSCI_FREQ_75_MHZ,       // SCInASYNCSEL = 00b
109     BSP_PRV_PCLKSCI_FREQ_80_MHZ,       // SCInASYNCSEL = 01b
110     BSP_PRV_PCLKSCI_FREQ_96_MHZ,       // SCInASYNCSEL = 10b
111     BSP_PRV_PCLKSCI_FREQ_100_MHZ,      // SCInASYNCSEL = 11b
112 };
113 #endif
114 
115 /***********************************************************************************************************************
116  * Private global variables and functions
117  **********************************************************************************************************************/
118 
119 /* FSP pack version structure. */
120 static BSP_DONT_REMOVE const fsp_pack_version_t g_fsp_version BSP_PLACE_IN_SECTION (FSP_SECTION_VERSION) =
121 {
122     .version_id_b =
123     {
124         .minor = FSP_VERSION_MINOR,
125         .major = FSP_VERSION_MAJOR,
126         .build = FSP_VERSION_BUILD,
127         .patch = FSP_VERSION_PATCH
128     }
129 };
130 
131 /* Public FSP version name. */
132 static BSP_DONT_REMOVE const uint8_t g_fsp_version_string[] BSP_PLACE_IN_SECTION(FSP_SECTION_VERSION) =
133     FSP_VERSION_STRING;
134 
135 /* Unique FSP version ID. */
136 static BSP_DONT_REMOVE const uint8_t g_fsp_version_build_string[] BSP_PLACE_IN_SECTION(FSP_SECTION_VERSION) =
137     FSP_VERSION_BUILD_STRING;
138 
139 /***********************************************************************************************************************
140  * Private function prototypes
141  **********************************************************************************************************************/
142 
143 /** Prototype of initialization function called before main.  This prototype sets the weak association of this
144  * function to an internal example implementation. If this function is defined in the application code, the
145  * application code version is used. */
146 
147 void bsp_init(void * p_args) WEAK_INIT_ATTRIBUTE;
148 
149 void bsp_init_internal(void * p_args); /// Default initialization function
150 
151 #if ((1 == BSP_CFG_ERROR_LOG) || (1 == BSP_CFG_ASSERT))
152 
153 /** Prototype of function called before errors are returned in FSP code if BSP_CFG_ERROR_LOG is set to 1.  This
154  * prototype sets the weak association of this function to an internal example implementation. */
155 
156 void fsp_error_log(fsp_err_t err, const char * file, int32_t line) WEAK_ERROR_ATTRIBUTE;
157 
158 void fsp_error_log_internal(fsp_err_t err, const char * file, int32_t line); /// Default error logger function
159 
160 #endif
161 
162 /*******************************************************************************************************************//**
163  * @addtogroup BSP_MCU
164  * @{
165  **********************************************************************************************************************/
166 
167 /*******************************************************************************************************************//**
168  * Get the FSP version based on compile time macros.
169  *
170  * @param[out] p_version        Memory address to return version information to.
171  *
172  * @retval FSP_SUCCESS          Version information stored.
173  * @retval FSP_ERR_ASSERTION    The parameter p_version is NULL.
174  **********************************************************************************************************************/
R_FSP_VersionGet(fsp_pack_version_t * const p_version)175 fsp_err_t R_FSP_VersionGet (fsp_pack_version_t * const p_version)
176 {
177 #if BSP_CFG_PARAM_CHECKING_ENABLE
178 
179     /** Verify parameters are valid */
180     FSP_ASSERT(NULL != p_version);
181 #endif
182 
183     *p_version = g_fsp_version;
184 
185     return FSP_SUCCESS;
186 }
187 
188 #if ((1 == BSP_CFG_ERROR_LOG) || (1 == BSP_CFG_ASSERT))
189 
190 /*******************************************************************************************************************//**
191  * Default error logger function, used only if fsp_error_log is not defined in the user application.
192  *
193  * @param[in]  err     The error code encountered.
194  * @param[in]  file    The file name in which the error code was encountered.
195  * @param[in]  line    The line number at which the error code was encountered.
196  **********************************************************************************************************************/
fsp_error_log_internal(fsp_err_t err,const char * file,int32_t line)197 void fsp_error_log_internal (fsp_err_t err, const char * file, int32_t line)
198 {
199     /** Do nothing. Do not generate any 'unused' warnings. */
200     FSP_PARAMETER_NOT_USED(err);
201     FSP_PARAMETER_NOT_USED(file);
202     FSP_PARAMETER_NOT_USED(line);
203 }
204 
205 #endif
206 
207 /** @} (end addtogroup BSP_MCU) */
208 
209 /*******************************************************************************************************************//**
210  * Default initialization function, used only if bsp_init is not defined in the user application.
211  **********************************************************************************************************************/
bsp_init_internal(void * p_args)212 void bsp_init_internal (void * p_args)
213 {
214     /* Do nothing. */
215     FSP_PARAMETER_NOT_USED(p_args);
216 }
217 
218 #if defined(__ARMCC_VERSION)
219 
220 /*******************************************************************************************************************//**
221  * Default implementation of assert for AC6.
222  **********************************************************************************************************************/
223 __attribute__((weak, noreturn))
__aeabi_assert(const char * expr,const char * file,int line)224 void __aeabi_assert (const char * expr, const char * file, int line) {
225     FSP_PARAMETER_NOT_USED(expr);
226     FSP_PARAMETER_NOT_USED(file);
227     FSP_PARAMETER_NOT_USED(line);
228     __BKPT(0);
229     while (1)
230     {
231         /* Do nothing. */
232     }
233 }
234 
235 #endif
236 
237 /*******************************************************************************************************************//**
238  * Allocate memory dynamically to heap.
239  *
240  * @param[in]   size    Size of memory allocated.
241  *
242  * @return      Returns a void pointer to the allocated area. If no memory is allocated, returns NULL.
243  **********************************************************************************************************************/
bsp_prv_malloc(size_t size)244 void * bsp_prv_malloc (size_t size)
245 {
246 #if (2 == BSP_CFG_RTOS) && configSUPPORT_DYNAMIC_ALLOCATION
247 
248     /* Use FreeRTOS heap */
249     return pvPortMalloc(size);
250 #else
251 
252     /* If RTOS dynamic allocation is disabled or RTOS is not in use, allocate heap data to the main heap. */
253     return malloc(size);
254 #endif
255 }
256 
257 /*******************************************************************************************************************//**
258  * Free dynamically allocated memory on heap.
259  *
260  * @param[in]   ptr     Pointer to memory to be released
261  **********************************************************************************************************************/
bsp_prv_free(void * ptr)262 void bsp_prv_free (void * ptr)
263 {
264 #if (2 == BSP_CFG_RTOS) && configSUPPORT_DYNAMIC_ALLOCATION
265 
266     /* Use FreeRTOS heap */
267     vPortFree(ptr);
268 #else
269 
270     /* If RTOS dynamic allocation is disabled or RTOS is not in use, use free(). */
271     free(ptr);
272 #endif
273 }
274