main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdlib.h>
  14. #include "ch.h"
  15. #include "hal.h"
  16. float ff1(float par);
  17. float ff2(float par1, float par2, float par3, float par4);
  18. /*===========================================================================*/
  19. /* Configurable settings. */
  20. /*===========================================================================*/
  21. #ifndef RANDOMIZE
  22. #define RANDOMIZE FALSE
  23. #endif
  24. #ifndef ITERATIONS
  25. #define ITERATIONS 100
  26. #endif
  27. /*===========================================================================*/
  28. /* Test related code. */
  29. /*===========================================================================*/
  30. static bool saturated;
  31. /*
  32. * Test worker thread.
  33. */
  34. static THD_WORKING_AREA(waWorkerThread, 128);
  35. static THD_FUNCTION(WorkerThread, arg) {
  36. (void)arg;
  37. while(1) {
  38. float f1, f2, f3, f4, f5;
  39. f1 = ff1(3.0f);
  40. f2 = ff1(4.0f);
  41. f3 = ff1(5.0f);
  42. f5 = f1 + f2 + f3;
  43. f4 = ff1(6.0f);
  44. f5 = ff2(f5, f4, f5, f4);
  45. if (f5 != 324.0f)
  46. chSysHalt("float corrupion #1");
  47. }
  48. }
  49. /*
  50. * Test periodic thread.
  51. */
  52. static THD_WORKING_AREA(waPeriodicThread, 128);
  53. static THD_FUNCTION(PeriodicThread, arg) {
  54. (void)arg;
  55. while(1) {
  56. float f1, f2, f3, f4, f5;
  57. f1 = ff1(4.0f);
  58. f2 = ff1(5.0f);
  59. f3 = ff1(6.0f);
  60. f5 = f1 + f2 + f3;
  61. f4 = ff1(7.0f);
  62. f5 = ff2(f5, f4, f5, f4);
  63. if (f5 != 484.0f)
  64. chSysHalt("float corrupion #2");
  65. chThdSleepSeconds(1);
  66. }
  67. }
  68. /*
  69. * GPT2 callback.
  70. */
  71. static void gpt4cb(GPTDriver *gptp) {
  72. float f1, f2, f3, f4, f5;
  73. (void)gptp;
  74. f1 = ff1(2.0f);
  75. f2 = ff1(3.0f);
  76. f3 = ff1(4.0f);
  77. f5 = f1 + f2 + f3;
  78. f4 = ff1(5.0f);
  79. f5 = ff2(f5, f4, f5, f4);
  80. if (f5 != 196.0f)
  81. chSysHalt("float corrupion #3");
  82. }
  83. /*
  84. * GPT3 callback.
  85. */
  86. static void gpt3cb(GPTDriver *gptp) {
  87. float f1, f2, f3, f4, f5;
  88. (void)gptp;
  89. f1 = ff1(1.0f);
  90. f2 = ff1(2.0f);
  91. f3 = ff1(3.0f);
  92. f5 = f1 + f2 + f3;
  93. f4 = ff1(4.0f);
  94. f5 = ff2(f5, f4, f5, f4);
  95. if (f5 != 100.0f)
  96. chSysHalt("float corrupion #4");
  97. }
  98. /*
  99. * GPT4 configuration.
  100. */
  101. static const GPTConfig gpt4cfg = {
  102. 1000000, /* 1MHz timer clock.*/
  103. gpt4cb, /* Timer callback.*/
  104. 0,
  105. 0
  106. };
  107. /*
  108. * GPT3 configuration.
  109. */
  110. static const GPTConfig gpt3cfg = {
  111. 1000000, /* 1MHz timer clock.*/
  112. gpt3cb, /* Timer callback.*/
  113. 0,
  114. 0
  115. };
  116. CH_FAST_IRQ_HANDLER(STM32_TIM1_UP_HANDLER) {
  117. float f1, f2, f3, f4, f5;
  118. TIM1->SR = 0;
  119. f1 = ff1(3.0f);
  120. f2 = ff1(4.0f);
  121. f3 = ff1(5.0f);
  122. f5 = f1 + f2 + f3;
  123. f4 = ff1(4.0f);
  124. f5 = ff2(f5, f4, f5, f4);
  125. if (f5 != 256.0f) {
  126. chSysHalt("float corrupion #5");
  127. }
  128. }
  129. /*===========================================================================*/
  130. /* Generic demo code. */
  131. /*===========================================================================*/
  132. CH_FAST_IRQ_HANDLER(Vector184) {
  133. while (1)
  134. ;
  135. }
  136. static void print(char *p) {
  137. while (*p) {
  138. streamPut(&SD2, *p++);
  139. }
  140. }
  141. static void println(char *p) {
  142. while (*p) {
  143. streamPut(&SD2, *p++);
  144. }
  145. streamWrite(&SD2, (uint8_t *)"\r\n", 2);
  146. }
  147. static void printn(uint32_t n) {
  148. char buf[16], *p;
  149. if (!n)
  150. streamPut(&SD2, '0');
  151. else {
  152. p = buf;
  153. while (n)
  154. *p++ = (n % 10) + '0', n /= 10;
  155. while (p > buf)
  156. streamPut(&SD2, *--p);
  157. }
  158. }
  159. /*
  160. * Application entry point.
  161. */
  162. int main(void) {
  163. unsigned i;
  164. gptcnt_t interval, threshold, worst;
  165. /* Enables FPU exceptions.*/
  166. nvicEnableVector(FPU_IRQn, 1);
  167. /*
  168. * System initializations.
  169. * - HAL initialization, this also initializes the configured device drivers
  170. * and performs the board-specific initializations.
  171. * - Kernel initialization, the main() function becomes a thread and the
  172. * RTOS is active.
  173. */
  174. halInit();
  175. chSysInit();
  176. /*
  177. * Prepares the Serial driver 2 and GPT drivers 4 and 3.
  178. */
  179. sdStart(&SD2, NULL); /* Default is 38400-8-N-1.*/
  180. palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
  181. palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
  182. gptStart(&GPTD4, &gpt4cfg);
  183. gptStart(&GPTD3, &gpt3cfg);
  184. /*
  185. * Enabling TIM1 as a fast interrupts source.
  186. */
  187. rccEnableTIM1(false);
  188. nvicEnableVector(STM32_TIM1_UP_NUMBER, 0);
  189. TIM1->ARR = 10000;
  190. TIM1->PSC = 0;
  191. TIM1->CNT = 0;
  192. TIM1->DIER = TIM_DIER_UIE;
  193. TIM1->CR1 = TIM_CR1_CEN;
  194. /*
  195. * Initializes the worker threads.
  196. */
  197. chThdCreateStatic(waWorkerThread, sizeof waWorkerThread,
  198. NORMALPRIO - 20, WorkerThread, NULL);
  199. chThdCreateStatic(waPeriodicThread, sizeof waPeriodicThread,
  200. NORMALPRIO - 10, PeriodicThread, NULL);
  201. /*
  202. * Test procedure.
  203. */
  204. println("");
  205. println("*** ChibiOS/RT IRQ-STORM-FPU long duration test");
  206. println("***");
  207. print("*** Kernel: ");
  208. println(CH_KERNEL_VERSION);
  209. print("*** Compiled: ");
  210. println(__DATE__ " - " __TIME__);
  211. #ifdef PORT_COMPILER_NAME
  212. print("*** Compiler: ");
  213. println(PORT_COMPILER_NAME);
  214. #endif
  215. print("*** Architecture: ");
  216. println(PORT_ARCHITECTURE_NAME);
  217. #ifdef PORT_CORE_VARIANT_NAME
  218. print("*** Core Variant: ");
  219. println(PORT_CORE_VARIANT_NAME);
  220. #endif
  221. #ifdef PORT_INFO
  222. print("*** Port Info: ");
  223. println(PORT_INFO);
  224. #endif
  225. #ifdef PLATFORM_NAME
  226. print("*** Platform: ");
  227. println(PLATFORM_NAME);
  228. #endif
  229. #ifdef BOARD_NAME
  230. print("*** Test Board: ");
  231. println(BOARD_NAME);
  232. #endif
  233. println("***");
  234. print("*** System Clock: ");
  235. printn(STM32_SYSCLK);
  236. println("");
  237. print("*** Iterations: ");
  238. printn(ITERATIONS);
  239. println("");
  240. print("*** Randomize: ");
  241. printn(RANDOMIZE);
  242. println("");
  243. println("");
  244. worst = 0;
  245. for (i = 1; i <= ITERATIONS; i++){
  246. print("Iteration ");
  247. printn(i);
  248. println("");
  249. saturated = FALSE;
  250. threshold = 0;
  251. for (interval = 2000; interval >= 10; interval -= interval / 10) {
  252. gptStartContinuous(&GPTD4, interval - 1); /* Slightly out of phase.*/
  253. gptStartContinuous(&GPTD3, interval + 1); /* Slightly out of phase.*/
  254. chThdSleepMilliseconds(1000);
  255. gptStopTimer(&GPTD4);
  256. gptStopTimer(&GPTD3);
  257. if (!saturated)
  258. print(".");
  259. else {
  260. print("#");
  261. if (threshold == 0)
  262. threshold = interval;
  263. }
  264. }
  265. /* Gives the worker threads a chance to empty the mailboxes before next
  266. cycle.*/
  267. chThdSleepMilliseconds(20);
  268. println("");
  269. print("Saturated at ");
  270. printn(threshold);
  271. println(" uS");
  272. println("");
  273. if (threshold > worst)
  274. worst = threshold;
  275. }
  276. gptStopTimer(&GPTD4);
  277. gptStopTimer(&GPTD3);
  278. print("Worst case at ");
  279. printn(worst);
  280. println(" uS");
  281. println("");
  282. println("Test Complete");
  283. /*
  284. * Normal main() thread activity, nothing in this test.
  285. */
  286. while (true) {
  287. chThdSleepMilliseconds(5000);
  288. }
  289. }