]> begriffs open source - cmsis-freertos/blob - Demo/RISC-V_RV64_PolarFire_SoftConsole/main.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Demo / RISC-V_RV64_PolarFire_SoftConsole / main.c
1 /*
2  * FreeRTOS V202111.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 /******************************************************************************
29  * This project provides two demo applications.  A simple blinky style project,
30  * and a more comprehensive test and demo application.  The
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
32  * select between the two.  The simply blinky demo is implemented and described
33  * in main_blinky.c.  The more comprehensive test and demo application is
34  * implemented and described in main_full.c.
35  *
36  * This file implements the code that is not demo specific, including the
37  * hardware setup and standard FreeRTOS hook functions.
38  *
39  * When running on the PolarFire SoC hardware:
40  * When executing correctly the yellow LED will toggle every three seconds.  If
41  * the yellow LED toggles every 500ms then one of the self-monitoring test tasks
42  * discovered a potential issue.  If the red led toggles rapidly then a hardware
43  * exception occurred.
44  *
45  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
46  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
47  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
48  *
49  */
50
51 /* FreeRTOS kernel includes. */
52 #include <FreeRTOS.h>
53 #include <task.h>
54
55 /* PolarFire HAL includes. */
56 #include "mpfs_hal/mss_hal.h"
57 #include "drivers/mss/mss_gpio/mss_gpio.h"
58 #include "drivers/mss/mss_mmuart/mss_uart.h"
59
60 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
61 or 0 to run the more comprehensive test and demo application. */
62 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY  0
63
64 /*-----------------------------------------------------------*/
65
66 /*
67  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
68  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
69  */
70 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
71     extern void main_blinky( void );
72 #else
73     extern void main_full( void );
74 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
75
76 /*
77  * Prototypes for the standard FreeRTOS callback/hook functions implemented
78  * within this file.  See https://www.freertos.org/a00016.html
79  */
80 void vApplicationMallocFailedHook( void );
81 void vApplicationIdleHook( void );
82 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
83 void vApplicationTickHook( void );
84
85 /*
86  * Setup the hardware to run this demo.
87  */
88 static void prvSetupHardware( void );
89
90 /*-----------------------------------------------------------*/
91
92 /* Main function for the HART0(E51 processor). Application code running on
93  * HART0 is placed here. */
94 void e51( void )
95 {
96     prvSetupHardware();
97
98     /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
99      * of this file. */
100     #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
101     {
102         main_blinky();
103     }
104     #else
105     {
106         main_full();
107     }
108     #endif
109 }
110 /*-----------------------------------------------------------*/
111
112 static void prvSetupHardware( void )
113 {
114     /* Configure UART0. */
115     SYSREG->SUBBLK_CLOCK_CR |= SUBBLK_CLOCK_CR_MMUART0_MASK;
116     SYSREG->SOFT_RESET_CR   &= ~SOFT_RESET_CR_MMUART0_MASK;
117     MSS_UART_init( &( g_mss_uart0_lo ),
118                    MSS_UART_115200_BAUD,
119                    MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );
120
121     /* Configure the LED. */
122     mss_config_clk_rst( MSS_PERIPH_GPIO2, ( uint8_t )MPFS_HAL_FIRST_HART, PERIPHERAL_ON );
123     MSS_GPIO_config( GPIO2_LO, MSS_GPIO_16, MSS_GPIO_OUTPUT_MODE ); /* Red Led (LED1). */
124     MSS_GPIO_config( GPIO2_LO, MSS_GPIO_18, MSS_GPIO_OUTPUT_MODE ); /* Yellow Led (LED3). */
125 }
126 /*-----------------------------------------------------------*/
127
128 void vApplicationMallocFailedHook( void )
129 {
130     /* vApplicationMallocFailedHook() will only be called if
131      * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
132      * function that will get called if a call to pvPortMalloc() fails.
133      * pvPortMalloc() is called internally by the kernel whenever a task, queue,
134      * timer or semaphore is created.  It is also called by various parts of the
135      * demo application.  If heap_1.c or heap_2.c are used, then the size of the
136      * heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
137      * FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
138      * to query the size of free heap space that remains (although it does not
139      * provide information on how the remaining heap might be fragmented). */
140     taskDISABLE_INTERRUPTS();
141     for( ;; );
142 }
143 /*-----------------------------------------------------------*/
144
145 void vApplicationIdleHook( void )
146 {
147     /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
148      * to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
149      * task.  It is essential that code added to this hook function never attempts
150      * to block in any way (for example, call xQueueReceive() with a block time
151      * specified, or call vTaskDelay()).  If the application makes use of the
152      * vTaskDelete() API function (as this demo application does) then it is also
153      * important that vApplicationIdleHook() is permitted to return to its calling
154      * function, because it is the responsibility of the idle task to clean up
155      * memory allocated by the kernel to any task that has since been deleted. */
156 }
157 /*-----------------------------------------------------------*/
158
159 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
160 {
161     ( void ) pcTaskName;
162     ( void ) pxTask;
163
164     /* Run time stack overflow checking is performed if
165      * configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
166      * function is called if a stack overflow is detected. */
167     taskDISABLE_INTERRUPTS();
168     for( ;; );
169 }
170 /*-----------------------------------------------------------*/
171
172 void vApplicationTickHook( void )
173 {
174     /* The tests in the full demo expect some interaction with interrupts. */
175     #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
176     {
177         extern void vFullDemoTickHook( void );
178         vFullDemoTickHook();
179     }
180     #endif
181 }
182 /*-----------------------------------------------------------*/
183
184 void vToggleLED( void )
185 {
186 static volatile uint8_t value = 0u;
187
188     value = ( value == 0u ) ? 1u : 0u;
189     MSS_GPIO_set_output( GPIO2_LO, MSS_GPIO_18, value );
190 }
191 /*-----------------------------------------------------------*/
192
193 void vAssertCalled( void )
194 {
195 volatile uint32_t ul;
196 const uint32_t ulNullLoopDelay = 0x1ffffUL;
197 static volatile uint8_t value = 0u;
198
199     taskDISABLE_INTERRUPTS();
200
201     /* Flash the red LED to indicate that assert was hit - interrupts are off
202      * here to prevent any further tick interrupts or context switches, so the
203      * delay is implemented as a crude loop instead of a peripheral timer. */
204     for( ;; )
205     {
206         for( ul = 0; ul < ulNullLoopDelay; ul++ )
207         {
208             __asm volatile( "nop" );
209         }
210
211         value = ( value == 0u ) ? 1u : 0u;
212         MSS_GPIO_set_output( GPIO2_LO, MSS_GPIO_16, value );
213     }
214 }
215 /*-----------------------------------------------------------*/