diff options
Diffstat (limited to 'lib/chibios/demos/STM32/RT-STM32L476-DISCOVERY-SB_HOST_STATIC/main.c')
-rw-r--r-- | lib/chibios/demos/STM32/RT-STM32L476-DISCOVERY-SB_HOST_STATIC/main.c | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/lib/chibios/demos/STM32/RT-STM32L476-DISCOVERY-SB_HOST_STATIC/main.c b/lib/chibios/demos/STM32/RT-STM32L476-DISCOVERY-SB_HOST_STATIC/main.c new file mode 100644 index 000000000..9d5313b69 --- /dev/null +++ b/lib/chibios/demos/STM32/RT-STM32L476-DISCOVERY-SB_HOST_STATIC/main.c | |||
@@ -0,0 +1,200 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #include "ch.h" | ||
18 | #include "hal.h" | ||
19 | #include "rt_test_root.h" | ||
20 | #include "oslib_test_root.h" | ||
21 | |||
22 | #include "chprintf.h" | ||
23 | #include "sb.h" | ||
24 | |||
25 | /* Static memory areas used by sandboxes.*/ | ||
26 | extern uint32_t __flash6_base__, __flash6_end__, | ||
27 | __flash7_base__, __flash7_end__, | ||
28 | __ram6_base__, __ram6_end__, | ||
29 | __ram7_base__, __ram7_end__; | ||
30 | |||
31 | /* Sandbox objects.*/ | ||
32 | sb_class_t sbx1, sbx2; | ||
33 | |||
34 | /* Sandbox configurations.*/ | ||
35 | static const sb_config_t sb_config1 = { | ||
36 | .code_region = 0U, | ||
37 | .data_region = 1U, | ||
38 | .regions = { | ||
39 | {(uint32_t)&__flash6_base__, (uint32_t)&__flash6_end__, false}, | ||
40 | {(uint32_t)&__ram6_base__, (uint32_t)&__ram6_end__, true} | ||
41 | }, | ||
42 | .stdin_stream = (SandboxStream *)&SD2, | ||
43 | .stdout_stream = (SandboxStream *)&SD2, | ||
44 | .stderr_stream = (SandboxStream *)&SD2 | ||
45 | }; | ||
46 | |||
47 | static const sb_config_t sb_config2 = { | ||
48 | .code_region = 0U, | ||
49 | .data_region = 1U, | ||
50 | .regions = { | ||
51 | {(uint32_t)&__flash7_base__, (uint32_t)&__flash7_end__, false}, | ||
52 | {(uint32_t)&__ram7_base__, (uint32_t)&__ram7_end__, true} | ||
53 | }, | ||
54 | .stdin_stream = (SandboxStream *)&SD2, | ||
55 | .stdout_stream = (SandboxStream *)&SD2, | ||
56 | .stderr_stream = (SandboxStream *)&SD2 | ||
57 | }; | ||
58 | |||
59 | /* | ||
60 | * LEDs blinker thread, times are in milliseconds. | ||
61 | */ | ||
62 | static THD_WORKING_AREA(waThread1, 128); | ||
63 | static THD_FUNCTION(Thread1, arg) { | ||
64 | unsigned i = 1U; | ||
65 | |||
66 | (void)arg; | ||
67 | chRegSetThreadName("blinker"); | ||
68 | while (true) { | ||
69 | palClearLine(LINE_LED_GREEN); | ||
70 | chThdSleepMilliseconds(50); | ||
71 | palClearLine(LINE_LED_RED); | ||
72 | chThdSleepMilliseconds(200); | ||
73 | (void) sbSendMessage(&sbx1, (msg_t)i); | ||
74 | palSetLine(LINE_LED_GREEN); | ||
75 | chThdSleepMilliseconds(50); | ||
76 | palSetLine(LINE_LED_RED); | ||
77 | chThdSleepMilliseconds(200); | ||
78 | (void) sbSendMessage(&sbx2, (msg_t)i); | ||
79 | i++; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | /* | ||
84 | * Unprivileged thread1. | ||
85 | */ | ||
86 | static THD_WORKING_AREA(waUnprivileged1, 128); | ||
87 | static THD_FUNCTION(Unprivileged1, arg) { | ||
88 | |||
89 | (void)arg; | ||
90 | chRegSetThreadName("unprivileged1"); | ||
91 | |||
92 | /* Sandbox object initialization.*/ | ||
93 | sbObjectInit(&sbx1); | ||
94 | |||
95 | /* Static MPU setup for the sandbox, both regions are used because in this | ||
96 | demo it requires both a flash and a RAM regions.*/ | ||
97 | mpuConfigureRegion(MPU_REGION_0, | ||
98 | sb_config1.regions[0].base, | ||
99 | MPU_RASR_ATTR_AP_RO_RO | | ||
100 | MPU_RASR_ATTR_CACHEABLE_WT_NWA | | ||
101 | MPU_RASR_SIZE_32K | | ||
102 | MPU_RASR_ENABLE); | ||
103 | mpuConfigureRegion(MPU_REGION_1, | ||
104 | sb_config1.regions[1].base, | ||
105 | MPU_RASR_ATTR_AP_RW_RW | | ||
106 | MPU_RASR_ATTR_CACHEABLE_WB_WA | | ||
107 | MPU_RASR_SIZE_4K | | ||
108 | MPU_RASR_ENABLE); | ||
109 | |||
110 | /* This thread goes in the sandbox and is trapped there, it cannot | ||
111 | return, only invoke the sandbox API.*/ | ||
112 | sbStart(&sbx1, &sb_config1); | ||
113 | |||
114 | /* Function sbStart() only fails if the sandbox cannot be started | ||
115 | because code header problems.*/ | ||
116 | } | ||
117 | |||
118 | /* | ||
119 | * Unprivileged thread2. | ||
120 | */ | ||
121 | static THD_WORKING_AREA(waUnprivileged2, 128); | ||
122 | static THD_FUNCTION(Unprivileged2, arg) { | ||
123 | |||
124 | (void)arg; | ||
125 | chRegSetThreadName("unprivileged2"); | ||
126 | |||
127 | /* Sandbox object initialization.*/ | ||
128 | sbObjectInit(&sbx2); | ||
129 | |||
130 | /* Static MPU setup for the sandbox, both regions are used because in this | ||
131 | demo it requires both a flash and a RAM regions.*/ | ||
132 | mpuConfigureRegion(MPU_REGION_2, | ||
133 | sb_config2.regions[0].base, | ||
134 | MPU_RASR_ATTR_AP_RO_RO | | ||
135 | MPU_RASR_ATTR_CACHEABLE_WT_NWA | | ||
136 | MPU_RASR_SIZE_32K | | ||
137 | MPU_RASR_ENABLE); | ||
138 | mpuConfigureRegion(MPU_REGION_3, | ||
139 | sb_config2.regions[1].base, | ||
140 | MPU_RASR_ATTR_AP_RW_RW | | ||
141 | MPU_RASR_ATTR_CACHEABLE_WB_WA | | ||
142 | MPU_RASR_SIZE_4K | | ||
143 | MPU_RASR_ENABLE); | ||
144 | |||
145 | /* This thread goes in the sandbox and is trapped there, it cannot | ||
146 | return, only invoke the sandbox API.*/ | ||
147 | sbStart(&sbx2, &sb_config2); | ||
148 | |||
149 | /* Function sbStart() only fails if the sandbox cannot be started | ||
150 | because code header problems.*/ | ||
151 | } | ||
152 | |||
153 | /* | ||
154 | * Application entry point. | ||
155 | */ | ||
156 | int main(void) { | ||
157 | thread_t *tp1, *tp2; | ||
158 | // msg_t msg; | ||
159 | |||
160 | /* | ||
161 | * System initializations. | ||
162 | * - HAL initialization, this also initializes the configured device drivers | ||
163 | * and performs the board-specific initializations. | ||
164 | * - Kernel initialization, the main() function becomes a thread and the | ||
165 | * RTOS is active. | ||
166 | */ | ||
167 | halInit(); | ||
168 | chSysInit(); | ||
169 | |||
170 | /* Activating the serial driver 2 using the driver default configuration.*/ | ||
171 | sdStart(&SD2, NULL); | ||
172 | |||
173 | /* Creating the blinker thread.*/ | ||
174 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+10, | ||
175 | Thread1, NULL); | ||
176 | |||
177 | /* Creating the unprivileged threads.*/ | ||
178 | chprintf((BaseSequentialStream *)&SD2, "Starting unprivileged thread 1\r\n"); | ||
179 | tp1 = chThdCreateStatic(waUnprivileged1, sizeof(waUnprivileged1), | ||
180 | NORMALPRIO - 10U, Unprivileged1, NULL); | ||
181 | chprintf((BaseSequentialStream *)&SD2, "Starting unprivileged thread 2\r\n"); | ||
182 | tp2 = chThdCreateStatic(waUnprivileged2, sizeof(waUnprivileged2), | ||
183 | NORMALPRIO - 10U, Unprivileged2, NULL); | ||
184 | |||
185 | /* Waiting for the unprivileged threads to exit or fail.*/ | ||
186 | (void)tp1; | ||
187 | (void)tp2; | ||
188 | // msg = chThdWait(tp1); | ||
189 | // chprintf((BaseSequentialStream *)&SD2, "Exit code 0x%x\r\n", msg); | ||
190 | |||
191 | /* Normal main() thread activity, in this demo it does nothing except | ||
192 | sleeping in a loop and check the button state.*/ | ||
193 | while (true) { | ||
194 | if (palReadLine(LINE_JOY_CENTER)) { | ||
195 | test_execute((BaseSequentialStream *)&SD2, &rt_test_suite); | ||
196 | test_execute((BaseSequentialStream *)&SD2, &oslib_test_suite); | ||
197 | } | ||
198 | chThdSleepMilliseconds(500); | ||
199 | } | ||
200 | } | ||