1 // See LICENSE for license details.
\r
3 #include <FreeRTOS.h>
\r
8 #include "platform.h"
\r
10 #include "plic/plic_driver.h"
\r
11 #include "encoding.h"
\r
13 #include "stdatomic.h"
\r
18 * FreeRTOS hook for when malloc fails, enable in FreeRTOSConfig.
\r
20 void vApplicationMallocFailedHook( void );
\r
23 * FreeRTOS hook for when FreeRtos is idling, enable in FreeRTOSConfig.
\r
25 void vApplicationIdleHook( void );
\r
28 * FreeRTOS hook for when a stack overflow occurs, enable in FreeRTOSConfig.
\r
30 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
34 void vRegTest1Task( void *pvParameters );
\r
35 void vRegTest2Task( void *pvParameters );
\r
38 const char * const pcStartMessage = "FreeRTOS demo\r\n";
\r
39 volatile uint32_t ulRegTest1LoopCounter = 0, ulRegTest2LoopCounter = 0;
\r
40 static void prvCheckTask( void *pvParameters );
\r
41 plic_instance_t g_plic;
\r
42 uint32_t bitbang_mask = 0;
\r
46 #ifdef HAS_BOARD_BUTTONS
\r
47 GPIO_REG(GPIO_OUTPUT_EN) &= ~((0x1 << BUTTON_0_OFFSET) | (0x1 << BUTTON_1_OFFSET) | (0x1 << BUTTON_2_OFFSET));
\r
48 GPIO_REG(GPIO_PULLUP_EN) &= ~((0x1 << BUTTON_0_OFFSET) | (0x1 << BUTTON_1_OFFSET) | (0x1 << BUTTON_2_OFFSET));
\r
49 GPIO_REG(GPIO_INPUT_EN) |= ((0x1 << BUTTON_0_OFFSET) | (0x1 << BUTTON_1_OFFSET) | (0x1 << BUTTON_2_OFFSET));
\r
52 GPIO_REG(GPIO_INPUT_EN) &= ~((0x1<< RED_LED_OFFSET) | (0x1<< GREEN_LED_OFFSET) | (0x1 << BLUE_LED_OFFSET)) ;
\r
53 GPIO_REG(GPIO_OUTPUT_EN) |= ((0x1<< RED_LED_OFFSET)| (0x1<< GREEN_LED_OFFSET) | (0x1 << BLUE_LED_OFFSET)) ;
\r
54 GPIO_REG(GPIO_OUTPUT_VAL) |= (0x1 << BLUE_LED_OFFSET) ;
\r
55 GPIO_REG(GPIO_OUTPUT_VAL) &= ~((0x1<< RED_LED_OFFSET) | (0x1<< GREEN_LED_OFFSET)) ;
\r
57 /* For Bit-banging with Atomics demo. */
\r
58 #ifdef _SIFIVE_HIFIVE1_H
\r
59 bitbang_mask = (1 << PIN_19_OFFSET);
\r
61 #ifdef _SIFIVE_COREPLEXIP_ARTY_H
\r
62 bitbang_mask = (0x1 << JA_0_OFFSET);
\r
66 GPIO_REG(GPIO_OUTPUT_EN) |= bitbang_mask;
\r
68 // xTaskCreate( vRegTest1Task, "RegTest1", 1000, NULL, tskIDLE_PRIORITY, NULL );
\r
69 // xTaskCreate( vRegTest2Task, "RegTest2", 1000, NULL, tskIDLE_PRIORITY, NULL );
\r
70 xTaskCreate( prvCheckTask, "Check", 1000, NULL, configMAX_PRIORITIES - 1, NULL );
\r
72 vTaskStartScheduler();
\r
74 /*-----------------------------------------------------------*/
\r
76 static void prvCheckTask( void *pvParameters )
\r
78 const char *pcMessage = "PASS\r\n";
\r
79 const TickType_t xCheckPeriod = pdMS_TO_TICKS( 3000UL );
\r
80 uint32_t ulLastRegTest1LoopCounter = 0, ulLastRegTest2LoopCounter = 0;
\r
81 volatile uintptr_t mstatus;
\r
83 volatile uint32_t ulx;
\r
84 __asm volatile ("csrr %0, mstatus" : "=r"(mstatus));
\r
85 portENABLE_INTERRUPTS();
\r
86 __asm volatile ("csrr %0, mstatus" : "=r"(mstatus));
\r
89 // for( ulx = 0; ulx < 0xffff; ulx++ ) __asm volatile( "NOP" );
\r
90 // vPortSetupTimerInterrupt();
\r
91 vTaskDelay( xCheckPeriod );
\r
92 write( STDOUT_FILENO, "Blip\r\n", strlen( "Blip\r\n" ) );
\r
97 vTaskDelay( xCheckPeriod );
\r
99 if( ulLastRegTest1LoopCounter == ulRegTest1LoopCounter )
\r
101 /* The RegTest1 loop counter is no longer incrementing, indicating
\r
102 the task failed its self check. */
\r
103 pcMessage = "FAIL: RegTest1\r\n";
\r
107 ulLastRegTest1LoopCounter = ulRegTest1LoopCounter;
\r
110 if( ulLastRegTest2LoopCounter == ulRegTest2LoopCounter )
\r
112 /* The RegTest1 loop counter is no longer incrementing, indicating
\r
113 the task failed its self check. */
\r
114 pcMessage = "FAIL: RegTest2\r\n";
\r
118 ulLastRegTest2LoopCounter = ulRegTest2LoopCounter;
\r
121 vUARTWriteString( pcMessage );
\r
125 /*-----------------------------------------------------------*/
\r
127 void vApplicationMallocFailedHook( void )
\r
129 /* vApplicationMallocFailedHook() will only be called if
\r
130 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
131 function that will get called if a call to pvPortMalloc() fails.
\r
132 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
133 timer or semaphore is created. It is also called by various parts of the
\r
134 demo application. If heap_1.c or heap_2.c are used, then the size of the
\r
135 heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
136 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
137 to query the size of free heap space that remains (although it does not
\r
138 provide information on how the remaining heap might be fragmented). */
\r
139 taskDISABLE_INTERRUPTS();
\r
142 /*-----------------------------------------------------------*/
\r
144 void vApplicationIdleHook( void )
\r
146 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
147 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
148 task. It is essential that code added to this hook function never attempts
\r
149 to block in any way (for example, call xQueueReceive() with a block time
\r
150 specified, or call vTaskDelay()). If the application makes use of the
\r
151 vTaskDelete() API function (as this demo application does) then it is also
\r
152 important that vApplicationIdleHook() is permitted to return to its calling
\r
153 function, because it is the responsibility of the idle task to clean up
\r
154 memory allocated by the kernel to any task that has since been deleted. */
\r
156 /*-----------------------------------------------------------*/
\r
158 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
160 ( void ) pcTaskName;
\r
163 /* Run time stack overflow checking is performed if
\r
164 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
165 function is called if a stack overflow is detected. */
\r
166 taskDISABLE_INTERRUPTS();
\r
169 /*-----------------------------------------------------------*/
\r
171 #define mainINTERRUPT_BIT_SET 0x80000000UL
\r
172 #define mainENVIRONMENT_CALL 11UL
\r
173 #define mainEXTERNAL_INTERRRUPT ( mainINTERRUPT_BIT_SET | 11UL )
\r
174 #define mainTIMER_INTERRUPT ( mainINTERRUPT_BIT_SET | 7UL )
\r
175 #define mainSOFTWARE_INTERRUPT ( mainINTERRUPT_BIT_SET | 3UL )
\r
177 extern void Timer_IRQHandler( void );
\r
179 uint32_t ulPortTrapHandler( uint32_t mcause, uint32_t mepc )
\r
181 if( mcause == mainENVIRONMENT_CALL )
\r
183 vTaskSwitchContext();
\r
185 /* Ensure not to return to the instruction that generated the exception. */
\r
188 else if( mcause == mainEXTERNAL_INTERRRUPT )
\r
192 else if( mcause == mainTIMER_INTERRUPT )
\r
194 Timer_IRQHandler();
\r
196 else if( mcause == mainSOFTWARE_INTERRUPT )
\r