main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * FreeRTOS V202104.00
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /******************************************************************************
  28. * This project provides two demo applications. A simple blinky style project,
  29. * and a more comprehensive test and demo application. The
  30. * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
  31. * select between the two. The simply blinky demo is implemented and described
  32. * in main_blinky.c. The more comprehensive test and demo application is
  33. * implemented and described in main_full.c.
  34. *
  35. * This file implements the code that is not demo specific, including the
  36. * hardware setup, standard FreeRTOS hook functions, and the ISR hander called
  37. * by the RTOS after interrupt entry (including nesting) has been taken care of.
  38. *
  39. * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
  40. * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
  41. * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
  42. *
  43. */
  44. /* Scheduler include files. */
  45. #include "FreeRTOS.h"
  46. #include "task.h"
  47. #include "semphr.h"
  48. /* Standard demo includes. */
  49. #include "partest.h"
  50. #include "TimerDemo.h"
  51. #include "QueueOverwrite.h"
  52. #include "EventGroupsDemo.h"
  53. /* Library includes. */
  54. #include "board.h"
  55. #include "chip.h"
  56. #ifdef ADC_TOUCH
  57. #include "touch.h"
  58. #endif
  59. #ifdef ADC_KEY
  60. #include "keypad.h"
  61. #endif
  62. //#define mainCREATE_SIMPLE_BLINKY_DEMO
  63. #ifdef VG_ONLY
  64. #define mainCREATE_OPENVG_DEMO
  65. #else
  66. #define mainCREATE_LVGL_DEMO
  67. #endif
  68. #if defined(mainCREATE_LVGL_DEMO)
  69. extern int main_lvgl(void);
  70. #elif defined(mainCREATE_OPENVG_DEMO)
  71. extern void main_openvg( void );
  72. #elif defined(mainCREATE_SIMPLE_BLINKY_DEMO)
  73. extern void main_blinky( void );
  74. #endif
  75. /*-----------------------------------------------------------*/
  76. /*
  77. * Configure the hardware as necessary to run this demo.
  78. */
  79. static void prvSetupHardware( void );
  80. /* Prototypes for the standard FreeRTOS callback/hook functions implemented
  81. within this file. */
  82. void vApplicationMallocFailedHook( size_t size );
  83. void vApplicationIdleHook( void );
  84. void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
  85. void vApplicationTickHook( void );
  86. /* Prototype for the IRQ handler called by the generic Cortex-A5 RTOS port
  87. layer. */
  88. void vApplicationIRQHandler( void );
  89. static void prvBoardLateInitTask( void *pvParameters )
  90. {
  91. ark_lcd_enable(1);
  92. #ifdef SDMMC_SUPPORT
  93. mmcsd_core_init();
  94. mmc_init();
  95. #endif
  96. #ifdef ADC_TOUCH
  97. adc_channel_enable(ADC_CH_TP);
  98. /* tp enable */
  99. vSysctlConfigure(SYS_ANA1_CFG, 26, 1, 1);
  100. if (LoadTouchConfigure()) {
  101. AdjustTouch();
  102. }
  103. #endif
  104. #ifdef ADC_KEY
  105. KeypadInit();
  106. #endif
  107. #ifdef AUDIO_REPLAY
  108. extern int sound_init(void);
  109. sound_init();
  110. #endif
  111. #ifdef USB_SUPPORT
  112. extern int ark_usb_init(void);
  113. ark_usb_init();
  114. #endif
  115. /* you'd better init the rtc driver at last */
  116. #ifdef RTC_SUPPORT
  117. rtc_init();
  118. SystemTime_t tm;
  119. iGetLocalTime(&tm);
  120. printf("Time: %d-%.2d-%.2d %.2d:%.2d:%.2d.\n", tm.tm_year, tm.tm_mon,
  121. tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  122. #endif
  123. for (;;) {
  124. vTaskDelay(portMAX_DELAY);
  125. }
  126. }
  127. /*-----------------------------------------------------------*/
  128. int main( void )
  129. {
  130. /* Configure the hardware ready to run the demo. */
  131. prvSetupHardware();
  132. /* lateinit the hardware which needs call task funcs */
  133. xTaskCreate(prvBoardLateInitTask, "lateinit", configMINIMAL_STACK_SIZE * 10, NULL,
  134. configMAX_PRIORITIES - 1, NULL);
  135. #if defined(mainCREATE_LVGL_DEMO) && LCD_INTERFACE_TYPE != LCD_INTERFACE_CPU
  136. main_lvgl();
  137. #elif defined(mainCREATE_OPENVG_DEMO)
  138. main_openvg();
  139. #elif defined(mainCREATE_SIMPLE_BLINKY_DEMO)
  140. main_blinky();
  141. #endif
  142. /* Start the tasks and timer running. */
  143. vTaskStartScheduler();
  144. /* If all is well, the scheduler will now be running, and the following
  145. line will never be reached. If the following line does execute, then
  146. there was insufficient FreeRTOS heap memory available for the idle and/or
  147. timer tasks to be created. See the memory management section on the
  148. FreeRTOS web site for more details. */
  149. for( ;; );
  150. }
  151. /*-----------------------------------------------------------*/
  152. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  153. #pragma data_alignment=16*1024
  154. static uint32_t MMUTable[4*1024];
  155. #else
  156. static uint32_t MMUTable[4*1024] __attribute__((aligned(16*1024)));
  157. #endif
  158. static void prvSetupHardware( void )
  159. {
  160. /* Disable watchdog */
  161. //WDT_Disable( WDT );
  162. /* Setup pinctrl */
  163. vPinctrlSetup();
  164. /* Setup clock */
  165. vClkInit();
  166. /* Configure a timer for delay */
  167. vInitialiseTimerForDelay();
  168. /* Initialize interrupt contorller */
  169. AIC_Initialize();
  170. /* Initialize Debug Uart */
  171. vDebugConsoleInitialise();
  172. printf("\r\ncpu_clk=%d, ddr_clk=%d, ahb_clk=%d, apb_clk=%d.\n", ulClkGetRate(CLK_CPU),
  173. ulClkGetRate(CLK_DDR), ulClkGetRate(CLK_AHB), ulClkGetRate(CLK_APB));
  174. /* Configure ports used by LEDs. */
  175. //vParTestInitialise();
  176. /* enable cache */
  177. MMU_Initialize(MMUTable);
  178. CP15_EnableMMU();
  179. CP15_EnableDcache();
  180. CP15_EnableIcache();
  181. dma_init();
  182. spi_init();
  183. i2c_init();
  184. wdt_init();
  185. jpegdec_init();
  186. itu_init();
  187. pxp_init();
  188. VideoDisplayBufInit();
  189. adc_init();
  190. #if LCD_INTERFACE_TYPE == LCD_INTERFACE_CPU
  191. Cpulcd_Init();
  192. #else
  193. lcd_init();
  194. #endif
  195. #ifdef ADC_TOUCH
  196. TouchInit();
  197. #endif
  198. }
  199. /*-----------------------------------------------------------*/
  200. void vApplicationMallocFailedHook( size_t size )
  201. {
  202. /* Called if a call to pvPortMalloc() fails because there is insufficient
  203. free memory available in the FreeRTOS heap. pvPortMalloc() is called
  204. internally by FreeRTOS API functions that create tasks, queues, software
  205. timers, and semaphores. The size of the FreeRTOS heap is set by the
  206. configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
  207. HeapStats_t heapStat;
  208. vPortGetHeapStats(&heapStat);
  209. printf("ERROR! Malloc %d bytes fail.\n", size);
  210. printf("xAvailableHeapSpaceInBytes %d.\n", heapStat.xAvailableHeapSpaceInBytes);
  211. printf("xSizeOfLargestFreeBlockInBytes %d.\n", heapStat.xSizeOfLargestFreeBlockInBytes);
  212. configASSERT( ( volatile void * ) NULL );
  213. }
  214. /*-----------------------------------------------------------*/
  215. void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
  216. {
  217. ( void ) pcTaskName;
  218. ( void ) pxTask;
  219. /* Run time stack overflow checking is performed if
  220. configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
  221. function is called if a stack overflow is detected. */
  222. printf("ERROR! task %s stack over flow.\n", pcTaskName);
  223. /* Force an assert. */
  224. configASSERT( ( volatile void * ) NULL );
  225. }
  226. /*-----------------------------------------------------------*/
  227. void vApplicationIdleHook( void )
  228. {
  229. static uint32_t idletick = 0;
  230. volatile size_t xFreeHeapSpace;
  231. /* This is just a trivial example of an idle hook. It is called on each
  232. cycle of the idle task. It must *NOT* attempt to block. In this case the
  233. idle task just queries the amount of FreeRTOS heap that remains. See the
  234. memory management section on the http://www.FreeRTOS.org web site for memory
  235. management options. If there is a lot of heap memory free then the
  236. configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
  237. RAM. */
  238. xFreeHeapSpace = xPortGetFreeHeapSize();
  239. /* Remove compiler warning about xFreeHeapSpace being set but never used. */
  240. ( void ) xFreeHeapSpace;
  241. if (xTaskGetTickCount() - idletick > configTICK_RATE_HZ * 10) {
  242. printf("Memory free: %d bytes.\n", xFreeHeapSpace);
  243. idletick = xTaskGetTickCount();
  244. }
  245. }
  246. /*-----------------------------------------------------------*/
  247. void vAssertCalled( const char * pcFile, unsigned long ulLine )
  248. {
  249. volatile unsigned long ul = 0;
  250. printf("vAssertCalled %s %d.\n", pcFile, ulLine);
  251. taskENTER_CRITICAL();
  252. {
  253. /* Set ul to a non-zero value using the debugger to step out of this
  254. function. */
  255. while( ul == 0 )
  256. {
  257. portNOP();
  258. }
  259. }
  260. taskEXIT_CRITICAL();
  261. }
  262. /*-----------------------------------------------------------*/
  263. void vApplicationTickHook( void )
  264. {
  265. }
  266. /*-----------------------------------------------------------*/
  267. /* The function called by the RTOS port layer after it has managed interrupt
  268. entry. */
  269. void vApplicationIRQHandler( void )
  270. {
  271. AIC_IrqHandler();
  272. __DSB();
  273. __ISB();
  274. }
  275. /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
  276. implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
  277. used by the Idle task. */
  278. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
  279. {
  280. /* If the buffers to be provided to the Idle task are declared inside this
  281. function then they must be declared static - otherwise they will be allocated on
  282. the stack and so not exists after this function exits. */
  283. static StaticTask_t xIdleTaskTCB;
  284. static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
  285. /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
  286. state will be stored. */
  287. *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
  288. /* Pass out the array that will be used as the Idle task's stack. */
  289. *ppxIdleTaskStackBuffer = uxIdleTaskStack;
  290. /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
  291. Note that, as the array is necessarily of type StackType_t,
  292. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  293. *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  294. }
  295. /*-----------------------------------------------------------*/
  296. /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
  297. application must provide an implementation of vApplicationGetTimerTaskMemory()
  298. to provide the memory that is used by the Timer service task. */
  299. void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
  300. {
  301. /* If the buffers to be provided to the Timer task are declared inside this
  302. function then they must be declared static - otherwise they will be allocated on
  303. the stack and so not exists after this function exits. */
  304. static StaticTask_t xTimerTaskTCB;
  305. static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
  306. /* Pass out a pointer to the StaticTask_t structure in which the Timer
  307. task's state will be stored. */
  308. *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
  309. /* Pass out the array that will be used as the Timer task's stack. */
  310. *ppxTimerTaskStackBuffer = uxTimerTaskStack;
  311. /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
  312. Note that, as the array is necessarily of type StackType_t,
  313. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  314. *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
  315. }