1 /*
2  * SPDX-FileCopyrightText: 2020 Amazon.com, Inc. or its affiliates
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
7  */
8 /*
9  * FreeRTOS Kernel V10.4.3
10  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
13  * this software and associated documentation files (the "Software"), to deal in
14  * the Software without restriction, including without limitation the rights to
15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16  * the Software, and to permit persons to whom the Software is furnished to do so,
17  * subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
25  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
26  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  *
29  * https://www.FreeRTOS.org
30  * https://github.com/FreeRTOS
31  *
32  * 1 tab == 4 spaces!
33  */
34 
35 /*-----------------------------------------------------------------------
36  * Implementation of functions defined in portable.h for the RISC-V port.
37  *----------------------------------------------------------------------*/
38 
39 #include "sdkconfig.h"
40 #include <string.h>
41 #include "soc/soc_caps.h"
42 #include "soc/periph_defs.h"
43 #include "soc/system_reg.h"
44 #include "hal/systimer_hal.h"
45 #include "hal/systimer_ll.h"
46 #include "riscv/rvruntime-frames.h"
47 #include "riscv/riscv_interrupts.h"
48 #include "riscv/interrupt.h"
49 #include "esp_private/crosscore_int.h"
50 #include "esp_attr.h"
51 #include "esp_system.h"
52 #include "esp_intr_alloc.h"
53 #include "esp_log.h"
54 #include "FreeRTOS.h"       /* This pulls in portmacro.h */
55 #include "task.h"
56 #include "portmacro.h"
57 #include "esp_memory_utils.h"
58 
59 /* ---------------------------------------------------- Variables ------------------------------------------------------
60  *
61  * ------------------------------------------------------------------------------------------------------------------ */
62 
63 static const char *TAG = "cpu_start";
64 
65 /* ---------------------------------------------- Port Implementations -------------------------------------------------
66  *
67  * ------------------------------------------------------------------------------------------------------------------ */
68 
69 // --------------------- Interrupts ------------------------
70 
xPortInIsrContext(void)71 BaseType_t xPortInIsrContext(void)
72 {
73     return (BaseType_t)rt_interrupt_get_nest();
74 }
75 
xPortInterruptedFromISRContext(void)76 BaseType_t IRAM_ATTR xPortInterruptedFromISRContext(void)
77 {
78     /* For single core, this can be the same as xPortInIsrContext() because reading it is atomic */
79     return (BaseType_t)rt_interrupt_get_nest();
80 }
81 
82 // ----------------------- System --------------------------
83 
xPortGetTickRateHz(void)84 uint32_t xPortGetTickRateHz(void)
85 {
86     return (uint32_t)configTICK_RATE_HZ;
87 }
88 
89 #define STACK_WATCH_AREA_SIZE 32
90 #define STACK_WATCH_POINT_NUMBER (SOC_CPU_WATCHPOINTS_NUM - 1)
91 
vPortSetStackWatchpoint(void * pxStackStart)92 void vPortSetStackWatchpoint(void *pxStackStart)
93 {
94     uint32_t addr = (uint32_t)pxStackStart;
95     addr = (addr + (STACK_WATCH_AREA_SIZE - 1)) & (~(STACK_WATCH_AREA_SIZE - 1));
96     esp_cpu_set_watchpoint(STACK_WATCH_POINT_NUMBER, (char *)addr, STACK_WATCH_AREA_SIZE, ESP_CPU_WATCHPOINT_STORE);
97 }
98 
99 
100 
101 /* ---------------------------------------------- Misc Implementations -------------------------------------------------
102  *
103  * ------------------------------------------------------------------------------------------------------------------ */
104 
105 // --------------------- App Start-up ----------------------
106 
107 /* [refactor-todo]: See if we can include this through a header */
108 extern void esp_startup_start_app_common(void);
109 
esp_startup_start_app(void)110 void esp_startup_start_app(void)
111 {
112     esp_startup_start_app_common();
113 
114     ESP_LOGI(TAG, "Starting scheduler.");
115     vTaskStartScheduler();
116 }
117