• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  *******************************************************************************
3  *
4  * @file   platform_gr55xx.c
5  *
6  * @brief  Platform Initialization Routines.
7  *
8  *******************************************************************************
9 
10  * @attention
11   #####Copyright (c) 2019 GOODIX
12   All rights reserved.
13 
14     Redistribution and use in source and binary forms, with or without
15     modification, are permitted provided that the following conditions are met:
16   * Redistributions of source code must retain the above copyright
17     notice, this list of conditions and the following disclaimer.
18   * Redistributions in binary form must reproduce the above copyright
19     notice, this list of conditions and the following disclaimer in the
20     documentation and/or other materials provided with the distribution.
21   * Neither the name of GOODIX nor the names of its contributors may be used
22     to endorse or promote products derived from this software without
23     specific prior written permission.
24 
25   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28   ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
29   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35   POSSIBILITY OF SUCH DAMAGE.
36  *******************************************************************************
37  */
38 
39 /*
40  * INCLUDE FILES
41  *******************************************************************************
42  */
43 #include "gr55xx_hal_cortex.h"
44 #include "gr55xx.h"
45 #include "gr55xx_sys.h"
46 #include "gr55xx_ll_pwr.h"
47 #include "hal_flash.h"
48 #include "platform_sdk.h"
49 #include "pmu_calibration.h"
50 #include "boards.h"
51 #include "custom_config.h"
52 #include "patch.h"
53 #include "patch_tab.h"
54 #include "gr55xx_ll_gpio.h"
55 #include "gr55xx_rom_symbol.h"
56 
57 // NOTE: SVC #0 is reserved for freertos, DO NOT USE IT!
58 #define SVC_TABLE_NUM_MAX   4
59 
60 #define PMU_CALIBRATION_DURATION_MS (30000)
61 
62 #define FLASH_CS        (LL_GPIO_PIN_2)      /* XQSPI flash CS        */
63 #define FLASH_CLK       (LL_GPIO_PIN_4)      /* XQSPI flash CLK       */
64 #define FLASH_IO_0      (LL_GPIO_PIN_7)      /* XQSPI flash IO0       */
65 #define FLASH_IO_1      (LL_GPIO_PIN_6)      /* XQSPI flash IO1       */
66 #define FLASH_IO_2      (LL_GPIO_PIN_5)      /* XQSPI flash IO2 (WP)  */
67 #define FLASH_IO_3      (LL_GPIO_PIN_3)      /* XQSPI flash IO3 (HOLD) */
HAL_EXFLASH_IO_PULL_SET(uint32_t PIN,uint32_t PULL)68 static inline void HAL_EXFLASH_IO_PULL_SET(uint32_t PIN, uint32_t PULL)
69 {
70     ll_gpio_set_pin_pull(GPIO1, PIN, PULL);
71 }
72 
73 static uint32_t SVC_TABLE_USER_SPACE[SVC_TABLE_NUM_MAX] __attribute__((section("SVC_TABLE")));
74 
75 #if (CFG_LCP_SUPPORT && (CHIP_TYPE == 0))
76 static uint8_t lcp_buf[280] __attribute__((section (".ARM.__at_0x00820000"), zero_init));
77 #endif
78 
79 #ifdef GR5515_E
80 static uint8_t s_nvds_cache[4096];
81 #endif
82 
nvds_setup(void)83 static void nvds_setup(void)
84 {
85 #ifdef GR5515_E
86     g_nvds_buf = (uint8_t *)&s_nvds_cache;
87 #endif
88 
89 #ifdef NVDS_START_ADDR
90     uint8_t err_code = nvds_init(NVDS_START_ADDR, NVDS_NUM_SECTOR);
91 #else
92     uint8_t err_code = nvds_init(0, NVDS_NUM_SECTOR);
93 #endif
94 
95     switch (err_code) {
96         case NVDS_FAIL:
97         case NVDS_STORAGE_ACCESS_FAILED: {
98             uint32_t start_addr  = nvds_get_start_addr();
99             uint32_t sector_size = hal_flash_sector_size();
100             if (hal_flash_erase(start_addr, NVDS_NUM_SECTOR * sector_size)) {
101                 err_code = nvds_init(start_addr, NVDS_NUM_SECTOR);
102                 if (NVDS_SUCCESS == err_code) {
103                     break;
104                 }
105             }
106             /* Flash fault, cannot startup.
107              * Output log via UART or Dump an error code to flash. */
108             while (1) {}
109         }
110         case NVDS_SUCCESS:
111             break;
112         default:
113             /* Illegal NVDS Parameters.
114              * Please check the start address and number of sectors. */
115             while (1) {}
116     }
117 }
118 
ble_sdk_env_init(void)119 void ble_sdk_env_init(void)
120 {
121     // register the msg handler for patch
122     uint16_t msg_cnt = sizeof(msg_tab) / sizeof(msg_tab_item_t);
123     reg_msg_patch_tab(msg_tab, msg_cnt);
124 
125 #if CFG_MAX_CONNECTIONS
126     ble_con_env_init();
127 #endif
128 
129 #if CFG_MAX_SCAN
130     ble_scan_env_init();
131 #endif
132 
133 #if CFG_MAX_ADVS
134     ble_adv_env_init();
135 #endif
136 }
137 
BLE_power_check(void)138 static void BLE_power_check(void)
139 {
140     if ((AON->PWR_RET01 & AON_PWR_REG01_PWR_EN_PD_COMM_TIMER) ||
141             (AON->PWR_RET01 & AON_PWR_REG01_PWR_EN_PD_COMM_CORE)) {
142         ll_pwr_enable_comm_core_reset();
143         ll_pwr_enable_comm_timer_reset();
144         ll_pwr_disable_comm_core_power();
145         ll_pwr_disable_comm_timer_power();
146         /* Reserve System Cold Fully Reset Method. */
147     }
148 }
149 
system_calibration(void)150 static void system_calibration(void)
151 {
152     system_pmu_calibration_init(PMU_CALIBRATION_DURATION_MS);
153 
154     system_pmu_deinit();
155     SystemCoreSetClock((mcu_clock_type_t)SYSTEM_CLOCK);
156     system_pmu_init((mcu_clock_type_t)SYSTEM_CLOCK);
157 
158     // recover the default setting by temperature, should be called in the end
159     pmu_calibration_handler(NULL);
160 
161     /* RTC calibration function */
162 #if !CFG_LPCLK_INTERNAL_EN
163     rtc_calibration();
164 #endif
165 
166     /* rng calibration */
167 #ifndef GR5515_E
168     rng_calibration();
169 #endif
170 }
171 
exflash_io_pull_config(void)172 static void exflash_io_pull_config(void)
173 {
174     /* XQSPI IO configuration needs to match Flash.
175        The default configuration can match most Flash */
176     HAL_EXFLASH_IO_PULL_SET(FLASH_CS,   LL_GPIO_PULL_UP);
177     HAL_EXFLASH_IO_PULL_SET(FLASH_CLK,  LL_GPIO_PULL_NO);
178     HAL_EXFLASH_IO_PULL_SET(FLASH_IO_0, LL_GPIO_PULL_UP); /* MOSI */
179     HAL_EXFLASH_IO_PULL_SET(FLASH_IO_1, LL_GPIO_PULL_UP); /* MISO */
180     HAL_EXFLASH_IO_PULL_SET(FLASH_IO_2, LL_GPIO_PULL_UP); /* WP   */
181     HAL_EXFLASH_IO_PULL_SET(FLASH_IO_3, LL_GPIO_PULL_UP); /* HOLD */
182 }
183 
platform_init(void)184 void platform_init(void)
185 {
186     /* if BLE not fully power off, reset and power off it manually */
187     BLE_power_check();
188 
189     /* Clear All Wakeup Event When Cold Boot */
190     ll_pwr_clear_wakeup_event(LL_PWR_WKUP_EVENT_ALL);
191     for (uint8_t i = 0; i < MAX_NUMS_IRQn; i++) {
192         NVIC_ClearPendingIRQ((IRQn_Type)(i));
193     }
194 
195 #ifdef EXFLASH_WAKEUP_DELAY
196     warm_boot_set_exflash_readid_delay(EXFLASH_WAKEUP_DELAY * ITEM_5);
197     run_mode_t run_mode = (run_mode_t)(SYSTEM_CLOCK);
198     uint16_t osc_time = ble_wakeup_osc_time_get(run_mode) + (EXFLASH_WAKEUP_DELAY * ITEM_5);
199     ble_wakeup_osc_time_set(run_mode, osc_time);
200 #endif
201 
202     /* enable protection. */
203 #ifndef GR5515_E
204     platform_init_push();
205 #endif
206 
207     /* set sram power state. */
208     mem_pwr_mgmt_init();
209 
210     if (!hal_flash_init()) {
211         /* Flash fault, cannot startup.
212          * Output log via UART or Dump an error code to flash. */
213         while (1) {}
214     }
215 
216 #if (defined(GR5515_E) && defined(ROM_RUN_IN_FLASH)) || !defined(GR5515_E)
217     platform_flash_enable_quad();
218 #endif
219 
220     platform_flash_protection(FLASH_PROTECT_PRIORITY);
221 
222     /* nvds module init process. */
223     nvds_setup();
224 
225     /* To choose the System clock source and set the accuracy of OSC. */
226 #if CFG_LPCLK_INTERNAL_EN
227     platform_clock_init_rng((mcu_clock_type_t)SYSTEM_CLOCK, RNG_OSC_CLK2, CFG_LF_ACCURACY_PPM, 0);
228 #else
229     platform_clock_init((mcu_clock_type_t)SYSTEM_CLOCK, RTC_OSC_CLK, CFG_LF_ACCURACY_PPM, 0);
230 #endif
231 
232     /* Register the SVC Table. */
233     svc_table_register(SVC_TABLE_USER_SPACE);
234 
235 #if ENCRYPT_ENABLE
236     fpb_register_patch_init_func(fpb_encrypt_mode_patch_enable);
237 #else
238     fpb_register_patch_init_func(fpb_patch_enable);
239 #endif
240 
241     /* platform init process. */
242     platform_sdk_init();
243 
244 #if ENCRYPT_ENABLE
245     dfu_cmd_handler_replace_for_encrypt();
246 #endif
247 
248     system_calibration();
249 
250 #if (CFG_LCP_SUPPORT && (CHIP_TYPE == 0))
251     gdx_lcp_buf_init((uint32_t)lcp_buf);
252 #endif
253 
254     exflash_io_pull_config();
255 
256     /* disable protection. */
257 #ifndef GR5515_E
258     platform_init_pop();
259 #endif
260 
261     return;
262 }
263 
264 #if defined ( __GNUC__ )
__main(void)265 void __main(void)
266 {
267     __asm("ldr    r1, =__etext\n");
268     __asm("ldr    r2, =__data_start__\n");
269     __asm("ldr    r3, =__data_end__\n");
270     __asm(".L_loop1:\n");
271     __asm("cmp    r2, r3\n");
272     __asm("ittt   lt\n");
273     __asm("ldrlt  r0, [r1], #4\n");
274     __asm("strlt  r0, [r2], #4\n");
275     __asm("blt    .L_loop1\n");
276     __asm("ldr    r1, =__bss_start__\n");
277     __asm("ldr    r2, =__bss_end__\n");
278     __asm("movs   r0, 0\n");
279     __asm(".L_loop3:\n");
280     __asm("cmp    r1, r2\n");
281     __asm("itt    lt\n");
282     __asm("strlt  r0, [r1], #4\n");
283     __asm("blt    .L_loop3\n");
284     system_platform_init();
285     main();
286 }
287 #endif
288 
289 #if defined ( __CC_ARM )
290 //lint -e{10}
291 //lint -e{10,144}
$Sub$$main(void)292 void $Sub$$main(void)
293 {
294     system_platform_init();
295     $Super$$main();
296 }
297 #endif
298 
299 #if defined ( __ICCARM__ )
300 
__main(void)301 void __main(void)
302 {
303     __iar_program_start();
304 }
305 
__low_level_init(void)306 int __low_level_init(void)
307 {
308     // call IAR table copy function.
309     __iar_data_init3();
310     system_platform_init();
311     return 0;
312 }
313 #endif
314