main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /* Kernel includes. */
  56. #include "FreeRTOS.h" /* Must come first. */
  57. #include "queue.h" /* RTOS queue related API prototypes. */
  58. #include "semphr.h" /* Semaphore related API prototypes. */
  59. #include "task.h" /* RTOS task related API prototypes. */
  60. #include "timers.h" /* Software timer related API prototypes. */
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include "nuclei_sdk_soc.h"
  64. /* The period of the example software timer, specified in milliseconds, and
  65. converted to ticks using the pdMS_TO_TICKS() macro. */
  66. #define mainSOFTWARE_TIMER_PERIOD_MS pdMS_TO_TICKS(500)
  67. #define TASKDLYMS pdMS_TO_TICKS(100)
  68. #define mainQUEUE_LENGTH (1)
  69. static void prvSetupHardware(void);
  70. extern void idle_task(void);
  71. static void vExampleTimerCallback(TimerHandle_t xTimer);
  72. /* The queue used by the queue send and queue receive tasks. */
  73. static QueueHandle_t xQueue = NULL;
  74. static TaskHandle_t StartTask1_Handler;
  75. static TaskHandle_t StartTask2_Handler;
  76. void prvSetupHardware(void)
  77. {
  78. }
  79. void start_task1(void* pvParameters);
  80. void start_task2(void* pvParameters);
  81. int main(void)
  82. {
  83. TimerHandle_t xExampleSoftwareTimer = NULL;
  84. /* Configure the system ready to run the demo. The clock configuration
  85. can be done here if it was not done before main() was called. */
  86. prvSetupHardware();
  87. xQueue = xQueueCreate(/* The number of items the queue can hold. */
  88. mainQUEUE_LENGTH,
  89. /* The size of each item the queue holds. */
  90. sizeof(uint32_t));
  91. if (xQueue == NULL) {
  92. printf("Unable to create xQueue due to low memory.\n");
  93. while (1);
  94. }
  95. xTaskCreate((TaskFunction_t)start_task1, (const char*)"start_task1",
  96. (uint16_t)256, (void*)NULL, (UBaseType_t)2,
  97. (TaskHandle_t*)&StartTask1_Handler);
  98. xTaskCreate((TaskFunction_t)start_task2, (const char*)"start_task2",
  99. (uint16_t)256, (void*)NULL, (UBaseType_t)1,
  100. (TaskHandle_t*)&StartTask2_Handler);
  101. xExampleSoftwareTimer =
  102. xTimerCreate((const char*)"ExTimer", mainSOFTWARE_TIMER_PERIOD_MS,
  103. pdTRUE, (void*)0, vExampleTimerCallback);
  104. xTimerStart(xExampleSoftwareTimer, 0);
  105. printf("Before StartScheduler\r\n");
  106. vTaskStartScheduler();
  107. printf("OS should never run to here\r\n");
  108. while (1);
  109. }
  110. void start_task1(void* pvParameters)
  111. {
  112. int cnt = 0;
  113. printf("Enter to task_1\r\n");
  114. while (1) {
  115. printf("task1 is running %d.....\r\n", cnt++);
  116. vTaskDelay(TASKDLYMS);
  117. }
  118. }
  119. void start_task2(void* pvParameters)
  120. {
  121. int cnt = 0;
  122. printf("Enter to task_2\r\n");
  123. while (1) {
  124. printf("task2 is running %d.....\r\n", cnt++);
  125. vTaskDelay(TASKDLYMS);
  126. }
  127. }
  128. static void vExampleTimerCallback(TimerHandle_t xTimer)
  129. {
  130. /* The timer has expired. Count the number of times this happens. The
  131. timer that calls this function is an auto re-load timer, so it will
  132. execute periodically. */
  133. static int cnt = 0;
  134. printf("timers Callback %d\r\n", cnt++);
  135. #ifdef CFG_SIMULATION
  136. if (cnt > 2) {
  137. // directly exit if in nuclei internally simulation
  138. SIMULATION_EXIT(0);
  139. }
  140. #endif
  141. }
  142. void vApplicationTickHook(void)
  143. {
  144. // BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  145. /* The RTOS tick hook function is enabled by setting configUSE_TICK_HOOK to
  146. 1 in FreeRTOSConfig.h.
  147. "Give" the semaphore on every 500th tick interrupt. */
  148. /* If xHigherPriorityTaskWoken is pdTRUE then a context switch should
  149. normally be performed before leaving the interrupt (because during the
  150. execution of the interrupt a task of equal or higher priority than the
  151. running task was unblocked). The syntax required to context switch from
  152. an interrupt is port dependent, so check the documentation of the port you
  153. are using.
  154. In this case, the function is running in the context of the tick interrupt,
  155. which will automatically check for the higher priority task to run anyway,
  156. so no further action is required. */
  157. }
  158. /*-----------------------------------------------------------*/
  159. void vApplicationMallocFailedHook(void)
  160. {
  161. /* The malloc failed hook is enabled by setting
  162. configUSE_MALLOC_FAILED_HOOK to 1 in FreeRTOSConfig.h.
  163. Called if a call to pvPortMalloc() fails because there is insufficient
  164. free memory available in the FreeRTOS heap. pvPortMalloc() is called
  165. internally by FreeRTOS API functions that create tasks, queues, software
  166. timers, and semaphores. The size of the FreeRTOS heap is set by the
  167. configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
  168. printf("malloc failed\n");
  169. while (1);
  170. }
  171. /*-----------------------------------------------------------*/
  172. void vApplicationStackOverflowHook(TaskHandle_t xTask, char* pcTaskName)
  173. {
  174. /* Run time stack overflow checking is performed if
  175. configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
  176. function is called if a stack overflow is detected. pxCurrentTCB can be
  177. inspected in the debugger if the task name passed into this function is
  178. corrupt. */
  179. printf("Stack Overflow\n");
  180. while (1);
  181. }
  182. /*-----------------------------------------------------------*/
  183. extern UBaseType_t uxCriticalNesting;
  184. void vApplicationIdleHook(void)
  185. {
  186. // volatile size_t xFreeStackSpace;
  187. /* The idle task hook is enabled by setting configUSE_IDLE_HOOK to 1 in
  188. FreeRTOSConfig.h.
  189. This function is called on each cycle of the idle task. In this case it
  190. does nothing useful, other than report the amount of FreeRTOS heap that
  191. remains unallocated. */
  192. /* By now, the kernel has allocated everything it is going to, so
  193. if there is a lot of heap remaining unallocated then
  194. the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
  195. reduced accordingly. */
  196. __WFI();
  197. }