diff --git a/arch/riscv32/bl808/src/pine64_ox64/nonsecure/main.c b/arch/riscv32/bl808/src/pine64_ox64/nonsecure/main.c index a0c6321d..89ae15e8 100644 --- a/arch/riscv32/bl808/src/pine64_ox64/nonsecure/main.c +++ b/arch/riscv32/bl808/src/pine64_ox64/nonsecure/main.c @@ -1,19 +1,12 @@ /* Copyright 2019 SiFive, Inc */ /* SPDX-License-Identifier: Apache-2.0 */ -#include -#include "semphr.h" -#include "board.h" +/* Kernel includes. */ +#include "FreeRTOS.h" #include "task.h" #include "queue.h" -#include -#include "version.h"> - -#include "tzc_sec_reg.h" -#include "rv_hart.h" -#include "rv_pmp.h" - +/* BL808 includes. */ #include "bflb_uart.h" #include "bflb_gpio.h" #include "bflb_clock.h" @@ -25,13 +18,19 @@ #include "bl808_ef_cfg.h" #include "bl808_uhs_phy.h" #include "board.h" - #include "mem.h" #ifdef CONFIG_BSP_SDH_SDCARD #include "sdh_sdcard.h" #endif +#include "tzc_sec_reg.h" +#include "rv_hart.h" +#include "rv_pmp.h" + +#include +#include "version.h"> + struct bflb_device_s *uart0; extern void bflb_uart_set_console(struct bflb_device_s *dev); @@ -41,17 +40,17 @@ extern pmp_info_t xPmpInfo; #define CONFIG_APPS_HELLO_WORLD /* Priorities used by the tasks. */ -#define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 ) -#define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 ) +#define mainQUEUE_RECEIVE_TASK_PRIORITY (tskIDLE_PRIORITY + 2) +#define mainQUEUE_SEND_TASK_PRIORITY (tskIDLE_PRIORITY + 1) #ifndef _RTL_ /* The 1s value is converted to ticks using the pdMS_TO_TICKS() macro. */ -#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS( 1000 ) +#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS(1000) #else -/* For RTL Simulation we reduce the waiting timing, otherwise the simulation +/* For RTL Simulation we reduce the waiting timing, otherwise the simulation * will take too more time */ /* The 10ms value is converted to ticks using the pdMS_TO_TICKS() macro. */ -#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS( 10 ) +#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS(10) #endif /* The maximum number items the queue can hold. The priority of the receiving task is above the priority of the sending task, so the receiving task will @@ -59,15 +58,15 @@ preempt the sending task and remove the queue items each time the sending task writes to the queue. Therefore the queue will never have more than one item in it at any time, and even with a queue length of 1, the sending task will never find the queue full. */ -#define mainQUEUE_LENGTH ( 1 ) +#define mainQUEUE_LENGTH (1) -#if( portUSING_MPU_WRAPPERS == 1 ) +#if (portUSING_MPU_WRAPPERS == 1) /** * @brief Calls the port specific code to raise the privilege. * * @return pdFALSE if privilege was raised, pdTRUE otherwise. */ -BaseType_t xPortRaisePrivilege( void ) FREERTOS_SYSTEM_CALL; +BaseType_t xPortRaisePrivilege(void) FREERTOS_SYSTEM_CALL; extern pmp_info_t xPmpInfo; #endif @@ -76,15 +75,14 @@ extern pmp_info_t xPmpInfo; * Functions: * - prvSetupHardware: Setup Hardware according CPU and Board. */ -static void prvSetupHardware( void ); +static void prvSetupHardware(void); /* * The tasks as described in the comments at the top of this file. */ -static void prvQueueReceiveTask( void *pvParameters ); -static void prvQueueSendTask( void *pvParameters ); -// void vTaskCode( void * pvParameters ) FREERTOS_SYSTEM_CALL; // PRIVILEGED_FUNCTION; -static void vTaskCode( void * pvParameters ) PRIVILEGED_FUNCTION; +static void prvQueueReceiveTask(void *pvParameters); +static void prvQueueSendTask(void *pvParameters); +static void vTaskCode(void *pvParameters) PRIVILEGED_FUNCTION; /** * @brief Implements the task which has Read Only access to the memory region @@ -92,7 +90,7 @@ static void vTaskCode( void * pvParameters ) PRIVILEGED_FUNCTION; * * @param pvParameters[in] Parameters as passed during task creation. */ -static void prvROAccessTask( void * pvParameters ); +static void prvROAccessTask(void *pvParameters); /** * @brief Implements the task which has Read Write access to the memory region @@ -100,73 +98,41 @@ static void prvROAccessTask( void * pvParameters ); * * @param pvParameters[in] Parameters as passed during task creation. */ -static void prvRWAccessTask( void * pvParameters ); +static void prvRWAccessTask(void *pvParameters); /** * @brief Size of the shared memory region. */ -#define SHARED_MEMORY_SIZE 16 +#define SHARED_MEMORY_SIZE 16 /** * @brief Memory region shared between two tasks. */ -__attribute__ ((aligned(128))) uint8_t ucSharedMemory[ SHARED_MEMORY_SIZE ]; +__attribute__((aligned(128))) uint8_t ucSharedMemory[SHARED_MEMORY_SIZE]; // static uint8_t ucSharedMemory1[ SHARED_MEMORY_SIZE ] __attribute__( ( aligned( 32 ) ) ); // static uint8_t ucSharedMemory2[ SHARED_MEMORY_SIZE ] __attribute__( ( aligned( 32 ) ) ); +static StackType_t xSendTaskStack[configMINIMAL_STACK_SIZE] __attribute__((aligned(128))); +static StackType_t xReceiveTaskStack[configMINIMAL_STACK_SIZE] __attribute__((aligned(128))); +static StackType_t xROAccessTaskStack[configMINIMAL_STACK_SIZE] __attribute__((aligned(128))); +static StackType_t xRWAccessTaskStack[configMINIMAL_STACK_SIZE] __attribute__((aligned(128))); + /*-----------------------------------------------------------*/ /* The queue used by both tasks. */ static QueueHandle_t xQueue = NULL; -void _putchar(char character){ - MPU_bflb_uart_putchar(uart0, character); +void _putchar(char character) +{ + MPU_bflb_uart_putchar(uart0, character); } -/* Task to be created. */ -// void vTaskCode( void * pvParameters ) FREERTOS_SYSTEM_CALL // PRIVILEGED_FUNCTION -// static void vTaskCode( void * pvParameters ) PRIVILEGED_FUNCTION -// { -// unsigned long ulCounter = 0; -// char pcWriteBuffer[200]; - -// ( void ) pvParameters; - -// /* Must not just run off the end of a task function, so delete this task. -// Note that because this task was created using xTaskCreate() the stack was -// allocated dynamically and I have not included any code to free it again. */ -// // vTaskDelete( NULL ); - -// /* The parameter value is expected to be 1 as 1 is passed in the -// pvParameters value in the call to xTaskCreate() below. */ -// // configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); - -// if (portIS_PRIVILEGED() == 1) { -// printf("%s task is running in priveleged mode\r\n", pcTaskGetName(NULL)); -// } else { -// printf("%s task is running in unpriveleged mode\r\n", pcTaskGetName(NULL)); -// } -// write( STDOUT_FILENO, "Test task\r\n", strlen( "Test task\r\n" ) ); - -// for( ;; ) -// { -// /* Task code goes here. */ -// vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); -// vTaskList( pcWriteBuffer ); -// printf("\033[2J\r\n"); -// printf( pcWriteBuffer ); -// printf("\r\n"); -// // printf("[ %s ] : %d\r\n", pcTaskGetName(NULL), ++ulCounter ); -// // write( STDOUT_FILENO, "Test task\r\n", strlen( "Test task\r\n" ) ); -// } -// } - /*-----------------------------------------------------------*/ -static void prvROAccessTask( void * pvParameters ) +static void prvROAccessTask(void *pvParameters) { - /* Unused parameters. */ - ( void ) pvParameters; + /* Unused parameters. */ + (void)pvParameters; if (portIS_PRIVILEGED() == 1) { printf("ROAccess task is running in priveleged mode\r\n"); @@ -175,20 +141,20 @@ static void prvROAccessTask( void * pvParameters ) } // ucSharedMemory[ 0 ] = 1; - - for( ; ; ) - { - /* Wait for a 3 seconds. */ - vTaskDelay( pdMS_TO_TICKS( 3 * 1000 ) ); + + for (;;) + { + /* Wait for a 3 seconds. */ + vTaskDelay(pdMS_TO_TICKS(3 * 1000)); // vTaskDelete(NULL); - } + } } /*-----------------------------------------------------------*/ -static void prvRWAccessTask( void * pvParameters ) +static void prvRWAccessTask(void *pvParameters) { - /* Unused parameters. */ - ( void ) pvParameters; + /* Unused parameters. */ + (void)pvParameters; if (portIS_PRIVILEGED() == 1) { printf("RWAccess task is running in priveleged mode\r\n"); @@ -201,16 +167,16 @@ static void prvRWAccessTask( void * pvParameters ) // printf("[ RWAccess ] : ucSharedMemory[0] = %d\r\n", ucSharedMemory[0]); // metal_tty_putc( '#' ); - for( ; ; ) - { - /* This task has RW access to ucSharedMemory and therefore can write to - * it. */ - // ucSharedMemory[ 0 ] = 0; + for (;;) + { + /* This task has RW access to ucSharedMemory and therefore can write to + * it. */ + // ucSharedMemory[ 0 ] = 0; - /* Wait for a 3 seconds. */ - vTaskDelay( pdMS_TO_TICKS( 3 * 1000 ) ); + /* Wait for a 3 seconds. */ + vTaskDelay(pdMS_TO_TICKS(3 * 1000)); // vTaskDelete(NULL); - } + } } /*-----------------------------------------------------------*/ @@ -221,28 +187,52 @@ static void prvRWAccessTask( void * pvParameters ) * * @returns None */ -static void clnSrvTask( void *pvParameters ) +static void clnSrvTask(void *pvParameters) { - const TickType_t xDelay = 1000 / portTICK_PERIOD_MS; + const TickType_t xDelay = 1000 / portTICK_PERIOD_MS; - (void)pvParameters; + (void)pvParameters; - do { - vTaskPrioritySet( xTaskGetIdleTaskHandle(), uxTaskPriorityGet(xTaskGetCurrentTaskHandle())); -// taskYIELD(); - vTaskDelay( xDelay ); - vTaskPrioritySet( xTaskGetIdleTaskHandle(), tskIDLE_PRIORITY); - vTaskDelay( xDelay ); - } while (1); + do { + vTaskPrioritySet(xTaskGetIdleTaskHandle(), uxTaskPriorityGet(xTaskGetCurrentTaskHandle())); + // taskYIELD(); + vTaskDelay(xDelay); + vTaskPrioritySet(xTaskGetIdleTaskHandle(), tskIDLE_PRIORITY); + vTaskDelay(xDelay); + } while (1); } #define BUFFER_SIZE 300 // static __attribute__ ((aligned(16))) StackType_t xISRStack[ configMINIMAL_STACK_SIZE + 1 ] = { 0 }; //__attribute__ ((section (".heap"))) ; -static __attribute__ ((aligned(16))) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 }; //__attribute__ ((section (".heap"))) ; -__attribute__ ((aligned(4))) uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute__ ((section (".heap"))); +static __attribute__((aligned(16))) StackType_t xISRStack[configISR_STACK_SIZE_WORDS] = {0}; //__attribute__ ((section (".heap"))) ; +__attribute__((aligned(4))) uint8_t ucHeap[configTOTAL_HEAP_SIZE] __attribute__((section(".heap"))); + +// void *aligned_malloc(size_t size, size_t alignment) +// { +// // Allocate additional space for saving the original address and for alignment +// uintptr_t raw_address = (uintptr_t)pvPortMalloc(size + alignment - 1 + sizeof(void *)); + +// if (!raw_address) +// { +// return NULL; // Memory allocation error +// } + +// // Find the nearest aligned address +// uintptr_t aligned_address = (raw_address + sizeof(void *) + alignment - 1) & ~(alignment - 1); + +// // Store the original address before the aligned address +// ((void **)aligned_address)[-1] = (void *)raw_address; + +// return (void *)aligned_address; +// } + +// void aligned_free(void *aligned_ptr) +// { +// // Restore the original address and release it +// void *raw_address = ((void **)aligned_ptr)[-1]; +// vPortFree(raw_address); +// } -//static StackType_t xSendTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); -//static StackType_t xReceiveTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); /** * @brief menuTask1 - . * @@ -250,73 +240,50 @@ __attribute__ ((aligned(4))) uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute * * @returns None */ - -void* aligned_malloc(size_t size, size_t alignment) { - // Allocate additional space for saving the original address and for alignment - uintptr_t raw_address = (uintptr_t)pvPortMalloc(size + alignment - 1 + sizeof(void*)); - - if (!raw_address) { - return NULL; // Memory allocation error - } - - // Find the nearest aligned address - uintptr_t aligned_address = (raw_address + sizeof(void*) + alignment - 1) & ~(alignment - 1); - - // Store the original address before the aligned address - ((void**)aligned_address)[-1] = (void*)raw_address; - - return (void*)aligned_address; -} - -void aligned_free(void* aligned_ptr) { - // Restore the original address and release it - void* raw_address = ((void**)aligned_ptr)[-1]; - vPortFree(raw_address); -} - static void menuTask(void *pvParameters) PRIVILEGED_FUNCTION { - /* Define a buffer that is large enough to hold the generated table. In most - * cases the buffer will be too large to allocate on the stack, hence in this - * example it is declared static. */ - ( void ) pvParameters; + /* Define a buffer that is large enough to hold the generated table. In most + * cases the buffer will be too large to allocate on the stack, hence in this + * example it is declared static. */ + (void)pvParameters; - static char cBuffer[BUFFER_SIZE]; - extern uint32_t __unprivileged_data_section_start__[]; - extern uint32_t __unprivileged_data_section_end__[]; - TaskHandle_t xHandle_TaskROAccess = NULL, xHandle_TaskRWAccess = NULL, xHandle_ReceiveTask = NULL, xHandle_SendTask = NULL; + static char cBuffer[BUFFER_SIZE]; + extern uint32_t __unprivileged_data_section_start__[]; + extern uint32_t __unprivileged_data_section_end__[]; + TaskHandle_t xHandle_TaskROAccess = NULL, xHandle_TaskRWAccess = NULL, xHandle_ReceiveTask = NULL, xHandle_SendTask = NULL; - static TaskParameters_t xROAccessTaskParameters = + static TaskParameters_t xROAccessTaskParameters = { .pvTaskCode = prvROAccessTask, .pcName = "ROAccess", - .usStackDepth = 128, //configMINIMAL_STACK_SIZE, + .usStackDepth = 128, // configMINIMAL_STACK_SIZE, .pvParameters = NULL, .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, .puxStackBuffer = NULL, // .puxStackBuffer = xROAccessTaskStack, }; - static TaskParameters_t xRWAccessTaskParameters = - { - .pvTaskCode = prvRWAccessTask, - .pcName = "RWAccess", - .usStackDepth = 128, //configMINIMAL_STACK_SIZE, - .pvParameters = NULL, - .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, + static TaskParameters_t xRWAccessTaskParameters = + { + .pvTaskCode = prvRWAccessTask, + .pcName = "RWAccess", + .usStackDepth = 128, // configMINIMAL_STACK_SIZE, + .pvParameters = NULL, + .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, .puxStackBuffer = NULL, - // .puxStackBuffer = xRWAccessTaskStack, + // .puxStackBuffer = xRWAccessTaskStack, }; - static TaskParameters_t xTaskRXDefinition = + static TaskParameters_t xTaskRXDefinition = { .pvTaskCode = prvQueueReceiveTask, .pcName = "Rx", .usStackDepth = configMINIMAL_STACK_SIZE, .pvParameters = NULL, - .uxPriority = mainQUEUE_RECEIVE_TASK_PRIORITY - 1 , + .uxPriority = mainQUEUE_RECEIVE_TASK_PRIORITY, .puxStackBuffer = NULL, + // .puxStackBuffer = xReceiveTaskStack, }; - static TaskParameters_t xTaskTXDefinition = + static TaskParameters_t xTaskTXDefinition = { .pvTaskCode = prvQueueSendTask, .pcName = "Tx", @@ -324,314 +291,314 @@ static void menuTask(void *pvParameters) PRIVILEGED_FUNCTION .pvParameters = NULL, .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, .puxStackBuffer = NULL, + // .puxStackBuffer = xSendTaskStack, }; + (void)pvParameters; + int32_t lResult; + extern uint32_t __unprivileged_data_section_start__[]; + extern uint32_t __unprivileged_data_section_end__[]; + extern uint32_t __tcm_code_start__[]; + extern uint32_t __tcm_code_end__[]; + extern uint32_t __tcm_data_end__[]; + extern uint32_t __tcm_data_start__[]; + extern uint32_t __ram_data_start__[]; + extern uint32_t privilege_status; - - (void)pvParameters; - int32_t lResult; - extern uint32_t __unprivileged_data_section_start__[]; - extern uint32_t __unprivileged_data_section_end__[]; - extern uint32_t __tcm_code_start__[]; - extern uint32_t __tcm_code_end__[]; - extern uint32_t __tcm_data_end__[]; - extern uint32_t __tcm_data_start__[]; - extern uint32_t __ram_data_start__[]; - - extern uint32_t privilege_status; - - int ch; - do - { - portDISABLE_INTERRUPTS(); - printf("\n+- Menu -----------------------------------------------------+\r\n"); + int ch; + do { + portDISABLE_INTERRUPTS(); + printf("\n+- Menu -----------------------------------------------------+\r\n"); #ifdef CONFIG_APPS_HELLO_WORLD - printf("| [1] - Run 2 unprvl apps & demo interaction between them |\r\n"); - printf("| [2] - Delete 2 unprvl apps |\r\n"); + printf("| [1] - Run 2 unprvl apps & demo interaction between them |\r\n"); + printf("| [2] - Delete 2 unprvl apps |\r\n"); #endif #ifdef CONFIG_APPS_HOTP - printf("| [3] - Run HOTP (Ported from OP-TEE, user TA) |\r\n"); + printf("| [3] - Run HOTP (Ported from OP-TEE, user TA) |\r\n"); #endif - printf("| [4] - Get FreeRTOS Task List |\r\n"); + printf("| [4] - Get FreeRTOS Task List |\r\n"); #ifdef CONFIG_APPS_TEST - printf("| [5] - Run Test Suite for GP TEE Client & Internal API |\r\n"); + printf("| [5] - Run Test Suite for GP TEE Client & Internal API |\r\n"); #endif #ifdef CONFIG_APPS_HW_SECURITY_EXCEPTION_EXAMPLE - printf("| [6] - Run H/W Security exception example (from non-secure) |\r\n"); - printf("| [7] - Run H/W Security exception example (from secure) |\r\n"); + printf("| [6] - Run H/W Security exception example (from non-secure) |\r\n"); + printf("| [7] - Run H/W Security exception example (from secure) |\r\n"); #endif #ifdef CONFIG_APPS_SPY - printf("| [8] - Run test & spy (trying to get protected data) unprvl |\r\n"); - printf("| [9] - Delete test & spy unprvl apps |\r\n"); + printf("| [8] - Run test & spy (trying to get protected data) unprvl |\r\n"); + printf("| [9] - Delete test & spy unprvl apps |\r\n"); #endif - printf("+------------------------------------------------------------+\r\n"); - portENABLE_INTERRUPTS(); - while (1) { - ch = bflb_uart_getchar(uart0); - if (ch == -1) { - // taskYIELD(); - // vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); - } else break; - } - printf("\n[%c]\r\n", (char) ch); - - switch (ch) { + printf("+------------------------------------------------------------+\r\n"); + portENABLE_INTERRUPTS(); + while (1) + { + ch = bflb_uart_getchar(uart0); + if (ch == -1) { + // taskYIELD(); + // vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); + } else + break; + } + printf("\n[%c]\r\n", (char)ch); + + switch (ch) { #ifdef CONFIG_APPS_HELLO_WORLD - case '1': - xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) ); - - if( xQueue != NULL ) - { - /* - * Prepare xRegions for Receive Task - */ - memset(&xTaskRXDefinition.xRegions, 0, sizeof(xTaskRXDefinition.xRegions)); - - // authorize access to data and bss - // Low address - // printf("xTaskRXDefinition.xRegions[0].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[0].pvBaseAddress); - // High address - - xTaskRXDefinition.xRegions[0].ulLengthInBytes = 0x10000; - xTaskRXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) 0xE0000000UL, - (size_t *) &xTaskRXDefinition.xRegions[0].pvBaseAddress, - xTaskRXDefinition.xRegions[0].ulLengthInBytes); - if(lResult) - printf("lResult R[0] = %d\r\n",lResult); - - xTaskRXDefinition.xRegions[1].ulLengthInBytes = 8192UL; - xTaskRXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_ONLY) | - (portPMP_REGION_READ_WRITE) | - (portPMP_REGION_EXECUTE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) 0x62020000UL, - (size_t *) &xTaskRXDefinition.xRegions[1].pvBaseAddress, - xTaskRXDefinition.xRegions[1].ulLengthInBytes); - if(lResult) - printf("lResult R[1] = %d\r\n",lResult); - - xTaskRXDefinition.xRegions[2].ulLengthInBytes = 0x10000; - xTaskRXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) __ram_data_start__, - (size_t *) &xTaskRXDefinition.xRegions[2].pvBaseAddress, - xTaskRXDefinition.xRegions[2].ulLengthInBytes); - if(lResult) - printf("lResult R[2] = %d\r\n",lResult); - - - // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) - // xTaskRXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskRXDefinition.usStackDepth * sizeof( StackType_t ) * 2); - xTaskRXDefinition.puxStackBuffer = ( StackType_t * )aligned_malloc(xTaskRXDefinition.usStackDepth * sizeof( StackType_t ),128); - // xTaskRXDefinition.puxStackBuffer = xReceiveTaskStack; - // portDISABLE_INTERRUPTS(); - // printf("xTaskRXDefinition.puxStackBuffer = %p\r\n", xTaskRXDefinition.puxStackBuffer); - // portENABLE_INTERRUPTS(); - xTaskCreateRestricted( &xTaskRXDefinition, - &xHandle_ReceiveTask); - - /* - * Prepare xRegions for Send Task - */ - memset(&xTaskTXDefinition.xRegions, 0, sizeof(xTaskTXDefinition.xRegions)); - - xTaskTXDefinition.xRegions[2].ulLengthInBytes = 0x10000; - xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) __ram_data_start__, - (size_t *) &xTaskTXDefinition.xRegions[2].pvBaseAddress, - xTaskTXDefinition.xRegions[2].ulLengthInBytes); - if(lResult) - printf("lResult R[2] = %d\r\n",lResult); - - // authorize access to data and bss - - // High address - - // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) - // xTaskTXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskTXDefinition.usStackDepth * sizeof( StackType_t ) ); - xTaskTXDefinition.puxStackBuffer = ( StackType_t * )aligned_malloc(xTaskTXDefinition.usStackDepth * sizeof( StackType_t ),128); - - // printf("xTaskTXDefinition.puxStackBuffer = %p\r\n", xTaskTXDefinition.puxStackBuffer); - - xTaskCreateRestricted( &xTaskTXDefinition, - &xHandle_SendTask); - - } - - break; + case '1': + xQueue = xQueueCreate(mainQUEUE_LENGTH, sizeof(uint32_t)); + + if (xQueue != NULL) { + /* + * Prepare xRegions for Receive Task + */ + memset(&xTaskRXDefinition.xRegions, 0, sizeof(xTaskRXDefinition.xRegions)); + + // authorize access to data and bss + // Low address + // printf("xTaskRXDefinition.xRegions[0].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[0].pvBaseAddress); + // High address + + xTaskRXDefinition.xRegions[0].ulLengthInBytes = 0x10000; + xTaskRXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)0xE0000000UL, + (size_t *)&xTaskRXDefinition.xRegions[0].pvBaseAddress, + xTaskRXDefinition.xRegions[0].ulLengthInBytes); + if (lResult) + printf("lResult R[0] = %d\r\n", lResult); + + xTaskRXDefinition.xRegions[1].ulLengthInBytes = 8192UL; + xTaskRXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_ONLY) | + (portPMP_REGION_READ_WRITE) | + (portPMP_REGION_EXECUTE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)0x62020000UL, + (size_t *)&xTaskRXDefinition.xRegions[1].pvBaseAddress, + xTaskRXDefinition.xRegions[1].ulLengthInBytes); + if (lResult) + printf("lResult R[1] = %d\r\n", lResult); + + xTaskRXDefinition.xRegions[2].ulLengthInBytes = 0x10000; + xTaskRXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)__ram_data_start__, + (size_t *)&xTaskRXDefinition.xRegions[2].pvBaseAddress, + xTaskRXDefinition.xRegions[2].ulLengthInBytes); + if (lResult) + printf("lResult R[2] = %d\r\n", lResult); + + // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) + // xTaskRXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskRXDefinition.usStackDepth * sizeof( StackType_t )); + // xTaskRXDefinition.puxStackBuffer = (StackType_t *)aligned_malloc(xTaskRXDefinition.usStackDepth * sizeof(StackType_t), 128); + xTaskRXDefinition.puxStackBuffer = xReceiveTaskStack; + // printf("xTaskRXDefinition.puxStackBuffer = %p\r\n", xTaskRXDefinition.puxStackBuffer); + xTaskCreateRestricted(&xTaskRXDefinition, &xHandle_ReceiveTask); + + /* + * Prepare xRegions for Send Task + */ + memset(&xTaskTXDefinition.xRegions, 0, sizeof(xTaskTXDefinition.xRegions)); + + xTaskTXDefinition.xRegions[2].ulLengthInBytes = 0x10000; + xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)__ram_data_start__, + (size_t *)&xTaskTXDefinition.xRegions[2].pvBaseAddress, + xTaskTXDefinition.xRegions[2].ulLengthInBytes); + if (lResult) + printf("lResult R[2] = %d\r\n", lResult); + + // authorize access to data and bss + + // High address + + // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) + // xTaskTXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskTXDefinition.usStackDepth * sizeof( StackType_t ) ); + // xTaskTXDefinition.puxStackBuffer = ( StackType_t * )aligned_malloc(xTaskTXDefinition.usStackDepth * sizeof( StackType_t ),128); + xTaskTXDefinition.puxStackBuffer = xSendTaskStack; + // printf("xTaskTXDefinition.puxStackBuffer = %p\r\n", xTaskTXDefinition.puxStackBuffer); + + xTaskCreateRestricted(&xTaskTXDefinition, &xHandle_SendTask); + } + + break; case '2': if (xHandle_ReceiveTask != NULL) vTaskDelete(xHandle_ReceiveTask); if (xHandle_SendTask != NULL) vTaskDelete(xHandle_SendTask); - if (xTaskRXDefinition.puxStackBuffer != NULL) - vPortFree(xTaskRXDefinition.puxStackBuffer); - if (xTaskTXDefinition.puxStackBuffer != NULL) - vPortFree(xTaskTXDefinition.puxStackBuffer); - break; + // if (xTaskRXDefinition.puxStackBuffer != NULL) { + // vPortFree(xTaskRXDefinition.puxStackBuffer); + // xTaskRXDefinition.puxStackBuffer = NULL; + // } + // if (xTaskTXDefinition.puxStackBuffer != NULL) + // { + // vPortFree(xTaskTXDefinition.puxStackBuffer); + // xTaskTXDefinition.puxStackBuffer = NULL; + // } + if (xQueue != NULL) + { + vQueueDelete(xQueue); + xQueue = NULL; + } + break; #endif - case '4': - /* Obtain the current tick count. */ - printf("\nAuto-reload timer callback executing = %d\r\n", xTaskGetTickCount()); - - /* Pass the buffer into vTaskList() to generate the table of information. */ - vTaskList(cBuffer); - portDISABLE_INTERRUPTS(); - printf("%s\n", cBuffer); - portENABLE_INTERRUPTS(); - break; + case '4': + /* Obtain the current tick count. */ + printf("\nAuto-reload timer callback executing = %d\r\n", xTaskGetTickCount()); + + /* Pass the buffer into vTaskList() to generate the table of information. */ + vTaskList(cBuffer); + portDISABLE_INTERRUPTS(); + printf("%s\n", cBuffer); + portENABLE_INTERRUPTS(); + break; #ifdef CONFIG_APPS_SPY - case '8': - /* - * Prepare xRegions for xROAccess Task - */ - memset(&xROAccessTaskParameters.xRegions, 0, sizeof(xROAccessTaskParameters.xRegions)); - - // authorize access to data and bss - // Low address - - xROAccessTaskParameters.xRegions[0].ulLengthInBytes = 0x10000; - xROAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) 0xE0000000UL, - (size_t *) &xROAccessTaskParameters.xRegions[0].pvBaseAddress, - xROAccessTaskParameters.xRegions[0].ulLengthInBytes); - if(lResult) - printf("lResult R[0] = %d\r\n",lResult); - - - xROAccessTaskParameters.xRegions[1].ulLengthInBytes = 8192UL; - xROAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_ONLY) | - (portPMP_REGION_READ_WRITE) | - (portPMP_REGION_EXECUTE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) 0x62020000UL, - (size_t *) &xROAccessTaskParameters.xRegions[1].pvBaseAddress, - xROAccessTaskParameters.xRegions[1].ulLengthInBytes); - if(lResult) - printf("lResult R[1] = %d\r\n",lResult); - - xROAccessTaskParameters.xRegions[2].ulLengthInBytes = 0x10000; - xROAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) __ram_data_start__, - (size_t *) &xROAccessTaskParameters.xRegions[2].pvBaseAddress, - xROAccessTaskParameters.xRegions[2].ulLengthInBytes); - if(lResult) - printf("lResult R[2] = %d\r\n",lResult); - - // xROAccessTaskParameters.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xROAccessTaskParameters.usStackDepth * sizeof( StackType_t ) ); - xROAccessTaskParameters.puxStackBuffer = ( StackType_t * )aligned_malloc(xROAccessTaskParameters.usStackDepth * sizeof( StackType_t ),128); - // xROAccessTaskParameters.puxStackBuffer = xROAccessTaskStack; - // printf("xROAccessTaskParameters.puxStackBuffer = %p\r\n", xROAccessTaskParameters.puxStackBuffer); - - - xTaskCreateRestricted( &xROAccessTaskParameters, - &xHandle_TaskROAccess); - - /* - * Prepare xRegions for xRWAccess Task - */ - memset(&xRWAccessTaskParameters.xRegions, 0, sizeof(xRWAccessTaskParameters.xRegions)); - - // authorize access to data and bss - // Low address - - xRWAccessTaskParameters.xRegions[0].ulLengthInBytes = 0x10000; - xRWAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) 0xE0000000UL, - (size_t *) &xRWAccessTaskParameters.xRegions[0].pvBaseAddress, - xRWAccessTaskParameters.xRegions[0].ulLengthInBytes); - if(lResult) - printf("lResult R[0] = %d\r\n",lResult); - - - xRWAccessTaskParameters.xRegions[1].ulLengthInBytes = 8192UL; - xRWAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_ONLY) | - (portPMP_REGION_READ_WRITE) | - (portPMP_REGION_EXECUTE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) 0x62020000UL, - (size_t *) &xRWAccessTaskParameters.xRegions[1].pvBaseAddress, - xRWAccessTaskParameters.xRegions[1].ulLengthInBytes); - if(lResult) - printf("lResult R[1] = %d\r\n",lResult); - - xRWAccessTaskParameters.xRegions[2].ulLengthInBytes = 0x10000; - xRWAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - lResult = napot_addr_modifier ( xPmpInfo.granularity, - (size_t) __ram_data_start__, - (size_t *) &xRWAccessTaskParameters.xRegions[2].pvBaseAddress, - xRWAccessTaskParameters.xRegions[2].ulLengthInBytes); - if(lResult) - printf("lResult R[2] = %d\r\n",lResult); - - // printf("xROAccessTaskParameters.xRegions[0].ulLengthInBytes = %d\r\n", xROAccessTaskParameters.xRegions[0].ulLengthInBytes); - - // xRWAccessTaskParameters.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xRWAccessTaskParameters.usStackDepth * sizeof( StackType_t ) ); - xRWAccessTaskParameters.puxStackBuffer = ( StackType_t * )aligned_malloc(xRWAccessTaskParameters.usStackDepth * sizeof( StackType_t ),128); - // xRWAccessTaskParameters.puxStackBuffer = xRWAccessTaskStack; - // printf("xRWAccessTaskParameters.puxStackBuffer = %p\r\n", xRWAccessTaskParameters.puxStackBuffer); - - xTaskCreateRestricted( &xRWAccessTaskParameters, - &xHandle_TaskRWAccess); - - // spyAppTask(); - break; + case '8': + /* + * Prepare xRegions for xROAccess Task + */ + memset(&xROAccessTaskParameters.xRegions, 0, sizeof(xROAccessTaskParameters.xRegions)); + + // authorize access to data and bss + // Low address + + xROAccessTaskParameters.xRegions[0].ulLengthInBytes = 0x10000; + xROAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)0xE0000000UL, + (size_t *)&xROAccessTaskParameters.xRegions[0].pvBaseAddress, + xROAccessTaskParameters.xRegions[0].ulLengthInBytes); + if (lResult) + printf("lResult R[0] = %d\r\n", lResult); + + xROAccessTaskParameters.xRegions[1].ulLengthInBytes = 8192UL; + xROAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_ONLY) | + (portPMP_REGION_READ_WRITE) | + (portPMP_REGION_EXECUTE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)0x62020000UL, + (size_t *)&xROAccessTaskParameters.xRegions[1].pvBaseAddress, + xROAccessTaskParameters.xRegions[1].ulLengthInBytes); + if (lResult) + printf("lResult R[1] = %d\r\n", lResult); + + xROAccessTaskParameters.xRegions[2].ulLengthInBytes = 0x10000; + xROAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)__ram_data_start__, + (size_t *)&xROAccessTaskParameters.xRegions[2].pvBaseAddress, + xROAccessTaskParameters.xRegions[2].ulLengthInBytes); + if (lResult) + printf("lResult R[2] = %d\r\n", lResult); + + // xROAccessTaskParameters.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xROAccessTaskParameters.usStackDepth * sizeof( StackType_t ) ); + // xROAccessTaskParameters.puxStackBuffer = ( StackType_t * )aligned_malloc(xROAccessTaskParameters.usStackDepth * sizeof( StackType_t ),128); + xROAccessTaskParameters.puxStackBuffer = xROAccessTaskStack; + // printf("xROAccessTaskParameters.puxStackBuffer = %p\r\n", xROAccessTaskParameters.puxStackBuffer); + + xTaskCreateRestricted(&xROAccessTaskParameters, &xHandle_TaskROAccess); + + /* + * Prepare xRegions for xRWAccess Task + */ + memset(&xRWAccessTaskParameters.xRegions, 0, sizeof(xRWAccessTaskParameters.xRegions)); + + // authorize access to data and bss + // Low address + + xRWAccessTaskParameters.xRegions[0].ulLengthInBytes = 0x10000; + xRWAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)0xE0000000UL, + (size_t *)&xRWAccessTaskParameters.xRegions[0].pvBaseAddress, + xRWAccessTaskParameters.xRegions[0].ulLengthInBytes); + if (lResult) + printf("lResult R[0] = %d\r\n", lResult); + + xRWAccessTaskParameters.xRegions[1].ulLengthInBytes = 8192UL; + xRWAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_ONLY) | + (portPMP_REGION_READ_WRITE) | + (portPMP_REGION_EXECUTE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)0x62020000UL, + (size_t *)&xRWAccessTaskParameters.xRegions[1].pvBaseAddress, + xRWAccessTaskParameters.xRegions[1].ulLengthInBytes); + if (lResult) + printf("lResult R[1] = %d\r\n", lResult); + + xRWAccessTaskParameters.xRegions[2].ulLengthInBytes = 0x10000; + xRWAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + lResult = napot_addr_modifier(xPmpInfo.granularity, + (size_t)__ram_data_start__, + (size_t *)&xRWAccessTaskParameters.xRegions[2].pvBaseAddress, + xRWAccessTaskParameters.xRegions[2].ulLengthInBytes); + if (lResult) + printf("lResult R[2] = %d\r\n", lResult); + + // printf("xROAccessTaskParameters.xRegions[0].ulLengthInBytes = %d\r\n", xROAccessTaskParameters.xRegions[0].ulLengthInBytes); + + // xRWAccessTaskParameters.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xRWAccessTaskParameters.usStackDepth * sizeof( StackType_t ) ); + // xRWAccessTaskParameters.puxStackBuffer = ( StackType_t * )aligned_malloc(xRWAccessTaskParameters.usStackDepth * sizeof( StackType_t ),128); + xRWAccessTaskParameters.puxStackBuffer = xRWAccessTaskStack; + // printf("xRWAccessTaskParameters.puxStackBuffer = %p\r\n", xRWAccessTaskParameters.puxStackBuffer); + + xTaskCreateRestricted(&xRWAccessTaskParameters, &xHandle_TaskRWAccess); + + // spyAppTask(); + break; case '9': if (xHandle_TaskROAccess != NULL) vTaskDelete(xHandle_TaskROAccess); if (xHandle_TaskRWAccess != NULL) vTaskDelete(xHandle_TaskRWAccess); - if (xROAccessTaskParameters.puxStackBuffer != NULL) - vPortFree(xROAccessTaskParameters.puxStackBuffer); - if (xRWAccessTaskParameters.puxStackBuffer != NULL) - vPortFree(xRWAccessTaskParameters.puxStackBuffer); - break; + // if (xROAccessTaskParameters.puxStackBuffer != NULL) { + // vPortFree(xROAccessTaskParameters.puxStackBuffer); + // xROAccessTaskParameters.puxStackBuffer = NULL; + // } + // if (xRWAccessTaskParameters.puxStackBuffer != NULL) { + // vPortFree(xRWAccessTaskParameters.puxStackBuffer); + // xRWAccessTaskParameters.puxStackBuffer != NULL; + // } + break; #endif - default: - break; - } - taskYIELD(); + default: + break; + } + taskYIELD(); vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); - } while (1); + } while (1); } - -static void ledBlinkTask( void *pvParameters ) PRIVILEGED_FUNCTION +static void ledBlinkTask(void *pvParameters) PRIVILEGED_FUNCTION { /* Remove compiler warning about unused parameter. */ - ( void ) pvParameters; - - struct bflb_device_s *gpio; //make instance of GPIO + (void)pvParameters; + + struct bflb_device_s *gpio; // make instance of GPIO - //Get gpio device handle, i.e.) define IC pin here where IC's GPIO = 26, pin silkscreen = 13 - //this is the GPIO device index that is referenced from 0, make sure to check the schematic + // Get gpio device handle, i.e.) define IC pin here where IC's GPIO = 26, pin silkscreen = 13 + // this is the GPIO device index that is referenced from 0, make sure to check the schematic gpio = bflb_device_get_by_name("gpio"); - //Set as gpio as output + // Set as gpio as output bflb_gpio_init(gpio, GPIO_PIN_26, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0); - //Turn ON pin + // Turn ON pin bflb_gpio_set(gpio, GPIO_PIN_26); if (portIS_PRIVILEGED() == 1) { @@ -640,8 +607,7 @@ static void ledBlinkTask( void *pvParameters ) PRIVILEGED_FUNCTION printf("ledBlinkTask task is running in unpriveleged mode\r\n"); } - - for( ;; ) + for (;;) { vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); bflb_gpio_reset(gpio, GPIO_PIN_26); @@ -651,33 +617,22 @@ static void ledBlinkTask( void *pvParameters ) PRIVILEGED_FUNCTION printf("[ledBlinkTask] Led ON\r\n"); } } - /*-----------------------------------------------------------*/ -int main( void ) -{ - // TaskHandle_t xHandle_ReceiveTask, xHandle_SendTask, xHandle_TaskListTasks, xHandle_TaskROAccess, xHandle_TaskRWAccess; - - // static StackType_t xTaskListTasksShowStack[ configMINIMAL_STACK_SIZE * 3] __attribute__( ( aligned( 32 ) ) ); - // static StackType_t xSendTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); - // static StackType_t xReceiveTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); - // static StackType_t xROAccessTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) ); - // static StackType_t xRWAccessTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) ); - - // const char * const pcMessage = "FreeRTOS-TEE Demo start\r\n"; - // const char * const pcMessageEnd = "FreeRTOS-TEE Demo end\r\n"; - const char * const pcMessageEndError = "FreeRTOS-TEE Demo end - Error no enough PMP entry\r\n"; - const char * const pcMessageGranularityError = "FreeRTOS-TEE Demo end - Error platform granularity no supported\r\n"; +int main(void) +{ + const char *const pcMessageEndError = "Error: no enough PMP entry\r\n"; + const char *const pcMessageGranularityError = "Error: platform granularity no supported\r\n"; extern uint32_t __unprivileged_data_section_start__[]; extern uint32_t __unprivileged_data_section_end__[]; - extern uint32_t __unprivileged_section_start__[]; - extern uint32_t __unprivileged_section_end__[]; + extern uint32_t __unprivileged_section_start__[]; + extern uint32_t __unprivileged_section_end__[]; // extern uint32_t __heap_start[]; // extern uint32_t __heap_end[]; - //extern unsigned long __privileged_data_start__[]; - //extern unsigned long __privileged_data_end__[]; + // extern unsigned long __privileged_data_start__[]; + // extern unsigned long __privileged_data_end__[]; extern unsigned long __privileged_functions_start__[]; extern unsigned long __privileged_functions_end__[]; // extern unsigned long metal_segment_stack_begin[]; @@ -685,12 +640,8 @@ int main( void ) volatile unsigned long *pul; prvSetupHardware(); - // __asm volatile ( "csrc mstatus, 0x8" ); - printf("\n\n\t-= mTower v" VERSION " =- " __DATE__ " " __TIME__"\r\n\n"); - // printf("ucSharedMemory = %p\r\n", ucSharedMemory); - __enable_irq(); - // printf("__ricsv_xlen = %d\r\n", __riscv_xlen); + printf("\n\n\t-= mTower v" VERSION " =- " __DATE__ " " __TIME__ "\r\n\n"); // pul = __unprivileged_data_section_start__; // printf("__unprivileged_data_section_start__ = %p\r\n", pul); @@ -703,10 +654,10 @@ int main( void ) extern uint32_t __tcm_code_start__[]; extern uint32_t __tcm_code_end__[]; - extern uint32_t __fsymtab_start[]; - extern uint32_t __fsymtab_end[]; - extern uint32_t __vsymtab_start[]; - extern uint32_t __vsymtab_end[]; + extern uint32_t __fsymtab_start[]; + extern uint32_t __fsymtab_end[]; + extern uint32_t __vsymtab_start[]; + extern uint32_t __vsymtab_end[]; extern uint32_t __usbh_class_info_end__[]; // printf("__fsymtab_start = %p\r\n", &__fsymtab_start); @@ -763,96 +714,80 @@ int main( void ) // printf("sizeof( StackType_t ) = %d\r\n", sizeof( StackType_t )); // printf("portTOTAL_NUM_CFG_REG = %d\r\n", portTOTAL_NUM_CFG_REG); - // printf("\r\nBefore hello\r\n"); - /* - * Call xPortFreeRTOSInit in order to set xISRTopStack - */ // printf("xISRStack = %p\r\n",(StackType_t)&( xISRStack[ ( (configMINIMAL_STACK_SIZE - 1) & ~portBYTE_ALIGNMENT_MASK ) ] )); // printf("xISRStack = %p\r\n",&( xISRStack )); - // printf("xISRStack = %p\r\n",(StackType_t)&( xISRStack[ ( (configMINIMAL_STACK_SIZE - 1) & ~portBYTE_ALIGNMENT_MASK ) ] )); - // __enable_irq(); - if ( 0 != xPortFreeRTOSInit((StackType_t)&( xISRStack[ ( (configMINIMAL_STACK_SIZE - 1) & ~portBYTE_ALIGNMENT_MASK ) ] ))) { - printf("ERROR"); - while(1); - // _exit(-1); + /* + * Call xPortFreeRTOSInit in order to set xISRTopStack + */ + if (0 != xPortFreeRTOSInit((StackType_t) & (xISRStack[((configMINIMAL_STACK_SIZE - 1) & ~portBYTE_ALIGNMENT_MASK)]))) { + printf("xPortFreeRTOSInit: ERROR"); + _exit(-1); } - + BaseType_t xReturned; TaskHandle_t xHandle = NULL; - // xReturned = xTaskCreate( - // vTaskCode, /* Function that implements the task. */ - // "PRVLGD", /* Text name for the task. */ - // 512, /* Stack size in words, not bytes. */ - // NULL, //( void * ) 1, /* Parameter passed into the task. */ - // mainQUEUE_SEND_TASK_PRIORITY | portPRIVILEGE_BIT,/* Priority at which the task is created. */ - // &xHandle ); /* Used to pass out the created task's handle. */ - - xTaskCreate(menuTask, /* The function that implements the task. */ - "menu", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ - 768, /* The size of the stack to allocate to the task. */ - (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ + xTaskCreate(menuTask, /* The function that implements the task. */ + "menu", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ + 512, /* The size of the stack to allocate to the task. */ + (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ mainQUEUE_SEND_TASK_PRIORITY | portPRIVILEGE_BIT, /* The priority assigned to the task. */ NULL); - __enable_irq(); - xTaskCreate(ledBlinkTask, /* The function that implements the task. */ - "ledBlink", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ - 128, /* The size of the stack to allocate to the task. */ - (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ + xTaskCreate(ledBlinkTask, /* The function that implements the task. */ + "ledBlink", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ + 128, /* The size of the stack to allocate to the task. */ + (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ mainQUEUE_SEND_TASK_PRIORITY | portPRIVILEGE_BIT, /* The priority assigned to the task. */ NULL); - __enable_irq(); -#if( portUSING_MPU_WRAPPERS == 1 ) - if (xPmpInfo.nb_pmp < 8) - { +#if (portUSING_MPU_WRAPPERS == 1) + if (xPmpInfo.nb_pmp < 8) { printf("%s", pcMessageEndError); _exit(0); - // } else if (xPmpInfo.granularity > 4) { + // } else if (xPmpInfo.granularity > 4) { } else if (xPmpInfo.granularity > 128) { - /* - * platfrom granularity > 4 bytes is not supported yet, some - * modifications are needed on FreeRTOS port to do so. - */ - printf("%s", pcMessageGranularityError); + /* + * platfrom granularity > 4 bytes is not supported yet, some + * modifications are needed on FreeRTOS port to do so. + */ + printf("%s", pcMessageGranularityError); _exit(0); - } + } + // printf("xPmpInfo.nb_pmp = %d\r\n", xPmpInfo.nb_pmp); // printf("xPmpInfo.granularity= %d\r\n", xPmpInfo.granularity); - if(0 == xPmpInfo.granularity) - { - init_pmp (&xPmpInfo); - } - __enable_irq(); + if (0 == xPmpInfo.granularity) { + init_pmp(&xPmpInfo); + } - /* Start the tasks and timer running. */ - vTaskStartScheduler(); + /* Start the tasks and timer running. */ + vTaskStartScheduler(); /* If all is well, the scheduler will now be running, and the following line will never be reached. If the following line does execute, then there was insufficient FreeRTOS heap memory available for the Idle and/or - timer tasks to be created. + timer tasks to be created. or task have stoppped the Scheduler */ - // vTaskDelete( xHandle_SendTask ); - // vTaskDelete( xHandle_ReceiveTask ); + // vTaskDelete( xHandle_SendTask ); + // vTaskDelete( xHandle_ReceiveTask ); #endif /* ( portUSING_MPU_WRAPPERS == 1 ) */ } - /*-----------------------------------------------------------*/ -static __attribute__( ( section( ".unprivileged_section" ) ) ) void prvQueueSendTask( void *pvParameters ) + +static __attribute__((section(".unprivileged_section"))) void prvQueueSendTask(void *pvParameters) { TickType_t xNextWakeTime; BaseType_t xReturned; unsigned long ulCounter = 0; /* Remove compiler warning about unused parameter. */ - ( void ) pvParameters; - ( void ) xReturned; + (void)pvParameters; + (void)xReturned; printf("prvQueueSendTask task: "); if (portIS_PRIVILEGED() == 1) { @@ -864,41 +799,40 @@ static __attribute__( ( section( ".unprivileged_section" ) ) ) void prvQueueSend /* Initialise xNextWakeTime - this only needs to be done once. */ xNextWakeTime = xTaskGetTickCount(); - for( ; ; ) + for (;;) { - printf("[ Tx ] : %d\r\n", ++ulCounter ); + printf("[ Tx ] : %d\r\n", ++ulCounter); /* Place this task in the blocked state until it is time to run again. */ - vTaskDelayUntil( &xNextWakeTime, mainQUEUE_TICK_COUNT_FOR_1S); + vTaskDelayUntil(&xNextWakeTime, mainQUEUE_TICK_COUNT_FOR_1S); // vTaskDelay( mainQUEUE_TICK_COUNT_FOR_1S * 500); - /* Send to the queue - causing the queue receive task to unblock and toggle the LED. 0 is used as the block time so the sending operation will not block - it shouldn't need to block as the queue should always be empty at this point in the code. */ - xReturned = xQueueSend( xQueue, &ulCounter, 0U ); - configASSERT( xReturned == pdPASS ); + xReturned = xQueueSend(xQueue, &ulCounter, 0U); + configASSERT(xReturned == pdPASS); } - /** + /** * SiFive CI/CD need to have a exit(0) status to pass */ -#if( portUSING_MPU_WRAPPERS == 1 ) +#if (portUSING_MPU_WRAPPERS == 1) /* We run into user mode, so need to be machine mode before to call vTaskEndScheduler */ // xPortRaisePrivilege(); #endif /* ( portUSING_MPU_WRAPPERS == 1 ) */ - // vTaskEndScheduler(); + // vTaskEndScheduler(); } /*-----------------------------------------------------------*/ -static __attribute__( ( section( ".unprivileged_section" ) ) ) void prvQueueReceiveTask( void *pvParameters ) +static __attribute__((section(".unprivileged_section"))) void prvQueueReceiveTask(void *pvParameters) { unsigned long ulReceivedValue; /* Remove compiler warning about unused parameter. */ - ( void ) pvParameters; - + (void)pvParameters; + printf("prvQueueReceiveTask task: "); if (portIS_PRIVILEGED() == 1) { printf("RX task is running in priveleged mode\r\n"); @@ -906,93 +840,94 @@ static __attribute__( ( section( ".unprivileged_section" ) ) ) void prvQueueRece printf("Rx task is running in unpriveleged mode\r\n"); } - for( ;; ) + for (;;) { /* Wait until something arrives in the queue - this task will block indefinitely provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. */ - xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY ); + xQueueReceive(xQueue, &ulReceivedValue, portMAX_DELAY); /* To get here something must have been received from the queue, but is it the expected value? If it is, toggle the LED. */ - printf("[ Rx ] : %d\r\n", ulReceivedValue ); + printf("[ Rx ] : %d\r\n", ulReceivedValue); } } /*-----------------------------------------------------------*/ -static void prvSetupHardware( void ) +static void prvSetupHardware(void) { - // const char * const pcWarningMsg = "At least one of LEDs is null.\r\n"; - board_init(); - uart0 = bflb_device_get_by_name("uart0"); + board_init(); + uart0 = bflb_device_get_by_name("uart0"); bflb_uart_set_console(bflb_device_get_by_name("uart0")); } /*-----------------------------------------------------------*/ -__attribute__((weak)) void vApplicationTickHook(void) -{ - //printf("vApplicationTickHook\r\n"); -} - __attribute__((weak)) void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) { - printf("vApplicationStackOverflowHook\r\n"); - - while (1) - ; + printf("vApplicationStackOverflowHook\r\n"); + _exit(-1); } +/*-----------------------------------------------------------*/ __attribute__((weak)) void vApplicationMallocFailedHook(void) { - const char * const pcErrorMsg = "ERROR malloc \r\n"; - printf("vApplicationMallocFailedHook\r\n"); + const char *const pcErrorMsg = "ERROR malloc \r\n"; + printf("vApplicationMallocFailedHook\r\n"); printf(pcErrorMsg); - while (1) - ; + _exit(-1); } +/*-----------------------------------------------------------*/ __attribute__((weak)) void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) { - /* If the buffers to be provided to the Idle task are declared inside this - function then they must be declared static - otherwise they will be allocated on - the stack and so not exists after this function exits. */ - static StaticTask_t xIdleTaskTCB; - static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE]; - - /* Pass out a pointer to the StaticTask_t structure in which the Idle task's - state will be stored. */ - *ppxIdleTaskTCBBuffer = &xIdleTaskTCB; - - /* Pass out the array that will be used as the Idle task's stack. */ - *ppxIdleTaskStackBuffer = uxIdleTaskStack; - - /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer. - Note that, as the array is necessarily of type StackType_t, - configMINIMAL_STACK_SIZE is specified in words, not bytes. */ - *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; + /* If the buffers to be provided to the Idle task are declared inside this + function then they must be declared static - otherwise they will be allocated on + the stack and so not exists after this function exits. */ + static StaticTask_t xIdleTaskTCB; + static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE]; + + /* Pass out a pointer to the StaticTask_t structure in which the Idle task's + state will be stored. */ + *ppxIdleTaskTCBBuffer = &xIdleTaskTCB; + + /* Pass out the array that will be used as the Idle task's stack. */ + *ppxIdleTaskStackBuffer = uxIdleTaskStack; + + /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer. + Note that, as the array is necessarily of type StackType_t, + configMINIMAL_STACK_SIZE is specified in words, not bytes. */ + *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; } +/*-----------------------------------------------------------*/ + +void vApplicationTickHook(void) +{ + /* The tests in the full demo expect some interaction with interrupts. */ + // printf("vApplicationTickHook\r\n"); +} +/*-----------------------------------------------------------*/ /* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the application must provide an implementation of vApplicationGetTimerTaskMemory() to provide the memory that is used by the Timer service task. */ __attribute__((weak)) void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize) { - /* If the buffers to be provided to the Timer task are declared inside this - function then they must be declared static - otherwise they will be allocated on - the stack and so not exists after this function exits. */ - static StaticTask_t xTimerTaskTCB; - static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH]; - - /* Pass out a pointer to the StaticTask_t structure in which the Timer - task's state will be stored. */ - *ppxTimerTaskTCBBuffer = &xTimerTaskTCB; - - /* Pass out the array that will be used as the Timer task's stack. */ - *ppxTimerTaskStackBuffer = uxTimerTaskStack; - - /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer. - Note that, as the array is necessarily of type StackType_t, - configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */ - *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; + /* If the buffers to be provided to the Timer task are declared inside this + function then they must be declared static - otherwise they will be allocated on + the stack and so not exists after this function exits. */ + static StaticTask_t xTimerTaskTCB; + static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH]; + + /* Pass out a pointer to the StaticTask_t structure in which the Timer + task's state will be stored. */ + *ppxTimerTaskTCBBuffer = &xTimerTaskTCB; + + /* Pass out the array that will be used as the Timer task's stack. */ + *ppxTimerTaskStackBuffer = uxTimerTaskStack; + + /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer. + Note that, as the array is necessarily of type StackType_t, + configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */ + *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; } diff --git a/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/FreeRTOSConfig.h b/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/FreeRTOSConfig.h index 5963545c..760f287a 100644 --- a/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/FreeRTOSConfig.h +++ b/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/FreeRTOSConfig.h @@ -86,7 +86,7 @@ */ #define configAPPLICATION_ALLOCATED_HEAP 1 -#define configTOTAL_HEAP_SIZE ( ( size_t ) 2400 * sizeof( size_t ) ) +#define configTOTAL_HEAP_SIZE ( ( size_t ) 2600 * sizeof( size_t ) ) #define configMAX_TASK_NAME_LEN ( 16 ) #define configUSE_TRACE_FACILITY 1 diff --git a/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/Makefile b/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/Makefile index bf61a921..7f2f57f9 100644 --- a/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/Makefile +++ b/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/Makefile @@ -83,7 +83,7 @@ mtower_ns$(EXEEXT): $(OBJS_NS) # $(Q) $(MAKE) -C ../../freedom-metal/src/ TOPDIR="$(TOPDIR)" libfe310_StdDriver_ns.a @echo "LD: mTower_ns$(EXEEXT)" # $(Q) $(CC) $(CFLAGS) -Wl,--section-start=.text=$(CONFIG_START_ADDRESS_BL33) -Tmetal.freertos.lds $(OBJS_NS) $(LIBPATHS) $(LIBS) -o $(OBJDIR)/bl33.elf - $(Q) $(CC) $(CFLAGS) -nostartfiles -Wl,--defsym,__stack_size=0x300 -Wl,--defsym,__heap_size=0x100 -Tmetal.freertos.lds $(OBJS_NS) $(LIBPATHS) $(LIBS) -o $(OBJDIR)/bl33.elf + $(Q) $(CC) $(CFLAGS) -nostartfiles -Wl,--defsym,__stack_size=0x1C0 -Wl,--defsym,__heap_size=0x100 -Tmetal.freertos.lds $(OBJS_NS) $(LIBPATHS) $(LIBS) -o $(OBJDIR)/bl33.elf $(Q) $(OBJCOPY) -S -O binary $(OBJDIR)/bl33.elf $(OBJDIR)/bl33.bin $(Q) $(OBJCOPY) -S -O ihex $(OBJDIR)/bl33.elf $(OBJDIR)/bl33.hex diff --git a/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/main_ns.c b/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/main_ns.c index cb0fd5cb..c246d64f 100644 --- a/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/main_ns.c +++ b/arch/riscv32/fe310/src/sparkfun_redboard/nonsecure/main_ns.c @@ -1,36 +1,6 @@ /* Copyright 2019 SiFive, Inc */ /* SPDX-License-Identifier: Apache-2.0 */ - - -/****************************************************************************** - * - * main() creates one queue, and two tasks. It then starts the - * scheduler. - * - * The Queue Send Task: - * The queue send task is implemented by the prvQueueSendTask() function in - * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly - * block for 1000 milliseconds, before sending the value 100 to the queue that - * was created within main(). Once the value is sent, the task loops - * back around to block for another 1000 milliseconds...and so on. - * - * The Queue Receive Task: - * The queue receive task is implemented by the prvQueueReceiveTask() function - * in this file. prvQueueReceiveTask() sits in a loop where it repeatedly - * blocks on attempts to read data from the queue that was created within - * blinky(). When data is received, the task checks the value of the - * data, and if the value equals the expected 100, writes 'Blink' to the UART - * (the UART is used in place of the LED to allow easy execution in QEMU). The - * 'block time' parameter passed to the queue receive function specifies that - * the task should be held in the Blocked state indefinitely to wait for data to - * be available on the queue. The queue receive task will only leave the - * Blocked state when the queue send task writes to the queue. As the queue - * send task writes to the queue every 1000 milliseconds, the queue receive - * task leaves the Blocked state every 1000 milliseconds, and therefore toggles - * the LED every 1 second. - */ - /* Standard includes. */ #include #include @@ -55,25 +25,23 @@ #include #include "version.h"> -extern struct metal_led *led0_red, *led0_green, *led0_blue; - extern pmp_info_t xPmpInfo; #define CONFIG_APPS_SPY #define CONFIG_APPS_HELLO_WORLD /* Priorities used by the tasks. */ -#define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 ) -#define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 ) +#define mainQUEUE_RECEIVE_TASK_PRIORITY (tskIDLE_PRIORITY + 2) +#define mainQUEUE_SEND_TASK_PRIORITY (tskIDLE_PRIORITY + 1) #ifndef _RTL_ /* The 1s value is converted to ticks using the pdMS_TO_TICKS() macro. */ -#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS( 1000 ) +#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS(1000) #else -/* For RTL Simulation we reduce the waiting timing, otherwise the simulation +/* For RTL Simulation we reduce the waiting timing, otherwise the simulation * will take too more time */ /* The 10ms value is converted to ticks using the pdMS_TO_TICKS() macro. */ -#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS( 10 ) +#define mainQUEUE_TICK_COUNT_FOR_1S pdMS_TO_TICKS(10) #endif /* The maximum number items the queue can hold. The priority of the receiving task is above the priority of the sending task, so the receiving task will @@ -81,15 +49,15 @@ preempt the sending task and remove the queue items each time the sending task writes to the queue. Therefore the queue will never have more than one item in it at any time, and even with a queue length of 1, the sending task will never find the queue full. */ -#define mainQUEUE_LENGTH ( 1 ) +#define mainQUEUE_LENGTH (1) -#if( portUSING_MPU_WRAPPERS == 1 ) +#if (portUSING_MPU_WRAPPERS == 1) /** * @brief Calls the port specific code to raise the privilege. * * @return pdFALSE if privilege was raised, pdTRUE otherwise. */ -// BaseType_t xPortRaisePrivilege( void ) FREERTOS_SYSTEM_CALL; +BaseType_t xPortRaisePrivilege(void) FREERTOS_SYSTEM_CALL; extern pmp_info_t xPmpInfo; #endif @@ -98,15 +66,14 @@ extern pmp_info_t xPmpInfo; * Functions: * - prvSetupHardware: Setup Hardware according CPU and Board. */ -static void prvSetupHardware( void ); +static void prvSetupHardware(void); /* * The tasks as described in the comments at the top of this file. */ -static void prvQueueReceiveTask( void *pvParameters ); -static void prvQueueSendTask( void *pvParameters ); -// void vTaskCode( void * pvParameters ) FREERTOS_SYSTEM_CALL; // PRIVILEGED_FUNCTION; -static void vTaskCode( void * pvParameters ) PRIVILEGED_FUNCTION; +static void prvQueueReceiveTask(void *pvParameters); +static void prvQueueSendTask(void *pvParameters); +static void vTaskCode(void *pvParameters) PRIVILEGED_FUNCTION; /** * @brief Implements the task which has Read Only access to the memory region @@ -114,7 +81,7 @@ static void vTaskCode( void * pvParameters ) PRIVILEGED_FUNCTION; * * @param pvParameters[in] Parameters as passed during task creation. */ -static void prvROAccessTask( void * pvParameters ); +static void prvROAccessTask(void *pvParameters); /** * @brief Implements the task which has Read Write access to the memory region @@ -122,112 +89,68 @@ static void prvROAccessTask( void * pvParameters ); * * @param pvParameters[in] Parameters as passed during task creation. */ -static void prvRWAccessTask( void * pvParameters ); +static void prvRWAccessTask(void *pvParameters); /** * @brief Size of the shared memory region. */ -#define SHARED_MEMORY_SIZE 16 +#define SHARED_MEMORY_SIZE 16 /** * @brief Memory region shared between two tasks. */ -static uint8_t ucSharedMemory[ SHARED_MEMORY_SIZE ] __attribute__( ( aligned( 32 ) ) ); +static uint8_t ucSharedMemory[SHARED_MEMORY_SIZE] __attribute__((aligned(32))); // static uint8_t ucSharedMemory1[ SHARED_MEMORY_SIZE ] __attribute__( ( aligned( 32 ) ) ); // static uint8_t ucSharedMemory2[ SHARED_MEMORY_SIZE ] __attribute__( ( aligned( 32 ) ) ); +// static StackType_t xSendTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); +// static StackType_t xReceiveTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); +// static StackType_t xROAccessTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); +// static StackType_t xRWAccessTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 128 ) ) ); + /*-----------------------------------------------------------*/ /* The queue used by both tasks. */ static QueueHandle_t xQueue = NULL; -struct metal_cpu *cpu0; -struct metal_interrupt *cpu_intr, *tmr_intr; -struct metal_led *led0_red, *led0_green, *led0_blue; - -#define LED_ERROR ((led0_red == NULL) || (led0_green == NULL) || (led0_blue == NULL)) - -void _putchar(char character){ - metal_tty_putc( character ); +void _putchar(char character) +{ + metal_tty_putc(character); } -/* Task to be created. */ -// void vTaskCode( void * pvParameters ) FREERTOS_SYSTEM_CALL // PRIVILEGED_FUNCTION -// static void vTaskCode( void * pvParameters ) PRIVILEGED_FUNCTION -// { -// unsigned long ulCounter = 0; -// char pcWriteBuffer[200]; - -// ( void ) pvParameters; - -// /* Must not just run off the end of a task function, so delete this task. -// Note that because this task was created using xTaskCreate() the stack was -// allocated dynamically and I have not included any code to free it again. */ -// // vTaskDelete( NULL ); - -// /* The parameter value is expected to be 1 as 1 is passed in the -// pvParameters value in the call to xTaskCreate() below. */ -// // configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); - -// if (portIS_PRIVILEGED() == 1) { -// printf("%s task is running in priveleged mode\r\n", pcTaskGetName(NULL)); -// } else { -// printf("%s task is running in unpriveleged mode\r\n", pcTaskGetName(NULL)); -// } -// write( STDOUT_FILENO, "Test task\r\n", strlen( "Test task\r\n" ) ); - -// for( ;; ) -// { -// /* Task code goes here. */ -// vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); -// vTaskList( pcWriteBuffer ); -// printf("\033[2J\r\n"); -// printf( pcWriteBuffer ); -// printf("\r\n"); -// // printf("[ %s ] : %d\r\n", pcTaskGetName(NULL), ++ulCounter ); -// // write( STDOUT_FILENO, "Test task\r\n", strlen( "Test task\r\n" ) ); -// } -// } - /*-----------------------------------------------------------*/ -static void prvROAccessTask( void * pvParameters ) +static void prvROAccessTask(void *pvParameters) { - /* Unused parameters. */ - ( void ) pvParameters; - - // if (portIS_PRIVILEGED() == 1) { - // // printf("ROAccess task is running in priveleged mode\r\n"); - // printf("ROAccess\r\n"); - // } else { - // printf("ROAccess task\r\n"); - // // printf("ROAccess task is running in unpriveleged mode\r\n"); - // } - - ucSharedMemory[ 0 ] = 1; - - for( ; ; ) - { - /* Wait for a 3 seconds. */ - vTaskDelay( pdMS_TO_TICKS( 3 * 1000 ) ); + /* Unused parameters. */ + (void)pvParameters; + + if (portIS_PRIVILEGED() == 1) { + // printf("ROAccess task is running in priveleged mode\r\n"); + } else { + // printf("ROAccess task is running in unpriveleged mode\r\n"); + } + + ucSharedMemory[0] = 1; + + for (;;) + { + /* Wait for a 3 seconds. */ + vTaskDelay(pdMS_TO_TICKS(3 * 1000)); // vTaskDelete(NULL); - } + } } /*-----------------------------------------------------------*/ -static void prvRWAccessTask( void * pvParameters ) +static void prvRWAccessTask(void *pvParameters) { - /* Unused parameters. */ - ( void ) pvParameters; + /* Unused parameters. */ + (void)pvParameters; if (portIS_PRIVILEGED() == 1) { - // write( STDOUT_FILENO, "RWAccess\r\n", strlen("RWAccess\r\n") ); // printf("RWAccess task is running in priveleged mode\r\n"); - // printf("RWAccess\r\n"); } else { - // write( STDOUT_FILENO, "RWAccess\r\n", strlen("RWAccess\r\n") ); - // printf("RWAccess\r\n"); - // printf("RWAccess task is running in unpriveleged mode\r\n"); + // printf("RWAccess task is running in unpriveleged mode\r\n"); } // printf("[ RWAccess ] : ucSharedMemory[0] = %d\r\n", ucSharedMemory[0]); @@ -235,16 +158,16 @@ static void prvRWAccessTask( void * pvParameters ) // printf("[ RWAccess ] : ucSharedMemory[0] = %d\r\n", ucSharedMemory[0]); // metal_tty_putc( '#' ); - for( ; ; ) - { - /* This task has RW access to ucSharedMemory and therefore can write to - * it. */ - // ucSharedMemory[ 0 ] = 0; + for (;;) + { + /* This task has RW access to ucSharedMemory and therefore can write to + * it. */ + // ucSharedMemory[ 0 ] = 0; - /* Wait for a 3 seconds. */ - vTaskDelay( pdMS_TO_TICKS( 3 * 1000 ) ); + /* Wait for a 3 seconds. */ + vTaskDelay(pdMS_TO_TICKS(3 * 1000)); // vTaskDelete(NULL); - } + } } /*-----------------------------------------------------------*/ @@ -255,22 +178,22 @@ static void prvRWAccessTask( void * pvParameters ) * * @returns None */ -static void clnSrvTask( void *pvParameters ) +static void clnSrvTask(void *pvParameters) { - const TickType_t xDelay = 1000 / portTICK_PERIOD_MS; + const TickType_t xDelay = 1000 / portTICK_PERIOD_MS; - (void)pvParameters; + (void)pvParameters; - do { - vTaskPrioritySet( xTaskGetIdleTaskHandle(), uxTaskPriorityGet(xTaskGetCurrentTaskHandle())); -// taskYIELD(); - vTaskDelay( xDelay ); - vTaskPrioritySet( xTaskGetIdleTaskHandle(), tskIDLE_PRIORITY); - vTaskDelay( xDelay ); - } while (1); + do { + vTaskPrioritySet(xTaskGetIdleTaskHandle(), uxTaskPriorityGet(xTaskGetCurrentTaskHandle())); + // taskYIELD(); + vTaskDelay(xDelay); + vTaskPrioritySet(xTaskGetIdleTaskHandle(), tskIDLE_PRIORITY); + vTaskDelay(xDelay); + } while (1); } -#define BUFFER_SIZE 200 +#define BUFFER_SIZE 240 /** * @brief menuTask1 - . @@ -281,37 +204,38 @@ static void clnSrvTask( void *pvParameters ) */ static void menuTask(void *pvParameters) PRIVILEGED_FUNCTION { - /* Define a buffer that is large enough to hold the generated table. In most - * cases the buffer will be too large to allocate on the stack, hence in this - * example it is declared static. */ + /* Define a buffer that is large enough to hold the generated table. In most + * cases the buffer will be too large to allocate on the stack, hence in this + * example it is declared static. */ + (void)pvParameters; - static char cBuffer[BUFFER_SIZE]; - extern uint32_t __unprivileged_data_section_start__[]; - extern uint32_t __unprivileged_data_section_end__[]; - TaskHandle_t xHandle_TaskROAccess = NULL, xHandle_TaskRWAccess = NULL, xHandle_ReceiveTask = NULL, xHandle_SendTask = NULL; + static char cBuffer[BUFFER_SIZE]; + extern uint32_t __unprivileged_data_section_start__[]; + extern uint32_t __unprivileged_data_section_end__[]; + TaskHandle_t xHandle_TaskROAccess = NULL, xHandle_TaskRWAccess = NULL, xHandle_ReceiveTask = NULL, xHandle_SendTask = NULL; - static TaskParameters_t xROAccessTaskParameters = + static TaskParameters_t xROAccessTaskParameters = { .pvTaskCode = prvROAccessTask, .pcName = "ROAccess", - .usStackDepth = 128, //configMINIMAL_STACK_SIZE, + .usStackDepth = 128, // configMINIMAL_STACK_SIZE, .pvParameters = NULL, .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, .puxStackBuffer = NULL, // .puxStackBuffer = xROAccessTaskStack, }; - static TaskParameters_t xRWAccessTaskParameters = - { - .pvTaskCode = prvRWAccessTask, - .pcName = "RWAccess", - .usStackDepth = 128, //configMINIMAL_STACK_SIZE, - .pvParameters = NULL, - .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, + static TaskParameters_t xRWAccessTaskParameters = + { + .pvTaskCode = prvRWAccessTask, + .pcName = "RWAccess", + .usStackDepth = 128, // configMINIMAL_STACK_SIZE, + .pvParameters = NULL, + .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, .puxStackBuffer = NULL, - // .puxStackBuffer = xRWAccessTaskStack, + // .puxStackBuffer = xRWAccessTaskStack, }; - static TaskParameters_t xTaskRXDefinition = + static TaskParameters_t xTaskRXDefinition = { .pvTaskCode = prvQueueReceiveTask, .pcName = "Rx", @@ -319,8 +243,9 @@ static void menuTask(void *pvParameters) PRIVILEGED_FUNCTION .pvParameters = NULL, .uxPriority = mainQUEUE_RECEIVE_TASK_PRIORITY, .puxStackBuffer = NULL, + // .puxStackBuffer = xReceiveTaskStack, }; - static TaskParameters_t xTaskTXDefinition = + static TaskParameters_t xTaskTXDefinition = { .pvTaskCode = prvQueueSendTask, .pcName = "Tx", @@ -328,343 +253,331 @@ static void menuTask(void *pvParameters) PRIVILEGED_FUNCTION .pvParameters = NULL, .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, .puxStackBuffer = NULL, + // .puxStackBuffer = xSendTaskStack, }; + (void)pvParameters; - - (void)pvParameters; - - int ch; - do - { - portDISABLE_INTERRUPTS(); - printf("\n+- Menu -----------------------------------------------------+\r\n"); + int ch; + do { + portDISABLE_INTERRUPTS(); + printf("\n+- Menu -----------------------------------------------------+\r\n"); #ifdef CONFIG_APPS_HELLO_WORLD - printf("| [1] - Run 2 unprvl apps & demo interaction between them |\r\n"); - printf("| [2] - Delete 2 unprvl apps |\r\n"); + printf("| [1] - Run 2 unprvl apps & demo interaction between them |\r\n"); + printf("| [2] - Delete 2 unprvl apps |\r\n"); #endif #ifdef CONFIG_APPS_HOTP - printf("| [3] - Run HOTP (Ported from OP-TEE, user TA) |\r\n"); + printf("| [3] - Run HOTP (Ported from OP-TEE, user TA) |\r\n"); #endif - printf("| [4] - Get FreeRTOS Task List |\r\n"); + printf("| [4] - Get FreeRTOS Task List |\r\n"); #ifdef CONFIG_APPS_TEST - printf("| [5] - Run Test Suite for GP TEE Client & Internal API |\r\n"); + printf("| [5] - Run Test Suite for GP TEE Client & Internal API |\r\n"); #endif #ifdef CONFIG_APPS_HW_SECURITY_EXCEPTION_EXAMPLE - printf("| [6] - Run H/W Security exception example (from non-secure) |\r\n"); - printf("| [7] - Run H/W Security exception example (from secure) |\r\n"); + printf("| [6] - Run H/W Security exception example (from non-secure) |\r\n"); + printf("| [7] - Run H/W Security exception example (from secure) |\r\n"); #endif #ifdef CONFIG_APPS_SPY - printf("| [8] - Run test & spy (trying to get protected data) unprvl |\r\n"); - printf("| [9] - Delete test & spy unprvl apps |\r\n"); + printf("| [8] - Run test & spy (trying to get protected data) unprvl |\r\n"); + printf("| [9] - Delete test & spy unprvl apps |\r\n"); #endif - printf("+------------------------------------------------------------+\r\n"); - portENABLE_INTERRUPTS(); - metal_tty_getc(&ch); - printf("\n[%c]\r\n", (char) ch); + printf("+------------------------------------------------------------+\r\n"); + portENABLE_INTERRUPTS(); + metal_tty_getc(&ch); + printf("\n[%c]\r\n", (char)ch); - switch (ch) { + switch (ch) { #ifdef CONFIG_APPS_HELLO_WORLD - case '1': - xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) ); - - if( xQueue != NULL ) - { - extern uint32_t __unprivileged_data_section_start__[]; - extern uint32_t __unprivileged_data_section_end__[]; - - /* - * Prepare xRegions for Receive Task - */ - memset(&xTaskRXDefinition.xRegions, 0, sizeof(xTaskRXDefinition.xRegions)); - - // authorize access to data and bss - // Low address - xTaskRXDefinition.xRegions[0].ulLengthInBytes = 4; - xTaskRXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NA4)); - addr_modifier ( xPmpInfo.granularity, - ( size_t ) __unprivileged_data_section_start__, - (size_t *) &xTaskRXDefinition.xRegions[0].pvBaseAddress); - // printf("xTaskRXDefinition.xRegions[0].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[0].pvBaseAddress); - // High address - xTaskRXDefinition.xRegions[1].ulLengthInBytes = 4; - xTaskRXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_TOR)); - - addr_modifier ( xPmpInfo.granularity, - ( size_t ) __unprivileged_data_section_end__, - (size_t *) &xTaskRXDefinition.xRegions[1].pvBaseAddress); - // printf("xTaskRXDefinition.xRegions[1].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[1].pvBaseAddress); + case '1': + xQueue = xQueueCreate(mainQUEUE_LENGTH, sizeof(uint32_t)); + + if (xQueue != NULL) { + extern uint32_t __unprivileged_data_section_start__[]; + extern uint32_t __unprivileged_data_section_end__[]; + + /* + * Prepare xRegions for Receive Task + */ + memset(&xTaskRXDefinition.xRegions, 0, sizeof(xTaskRXDefinition.xRegions)); + + // authorize access to data and bss + // Low address + xTaskRXDefinition.xRegions[0].ulLengthInBytes = 4; + xTaskRXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NA4)); + addr_modifier(xPmpInfo.granularity, + (size_t)__unprivileged_data_section_start__, + (size_t *)&xTaskRXDefinition.xRegions[0].pvBaseAddress); + // printf("xTaskRXDefinition.xRegions[0].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[0].pvBaseAddress); + // High address + xTaskRXDefinition.xRegions[1].ulLengthInBytes = 4; + xTaskRXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_TOR)); + + addr_modifier(xPmpInfo.granularity, + (size_t)__unprivileged_data_section_end__, + (size_t *)&xTaskRXDefinition.xRegions[1].pvBaseAddress); + // printf("xTaskRXDefinition.xRegions[1].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[1].pvBaseAddress); #ifdef METAL_SIFIVE_UART0 - // allow access to UART peripheral - xTaskRXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; - xTaskRXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - napot_addr_modifier ( xPmpInfo.granularity, - (size_t) METAL_SIFIVE_UART0_0_BASE_ADDRESS, - (size_t *) &xTaskRXDefinition.xRegions[2].pvBaseAddress, - xTaskRXDefinition.xRegions[2].ulLengthInBytes); + // allow access to UART peripheral + xTaskRXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; + xTaskRXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + napot_addr_modifier(xPmpInfo.granularity, + (size_t)METAL_SIFIVE_UART0_0_BASE_ADDRESS, + (size_t *)&xTaskRXDefinition.xRegions[2].pvBaseAddress, + xTaskRXDefinition.xRegions[2].ulLengthInBytes); #endif /* METAL_SIFIVE_UART0 */ - // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) - xTaskRXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskRXDefinition.usStackDepth * sizeof( StackType_t ) ); - // xTaskRXDefinition.puxStackBuffer = xReceiveTaskStack; - // portDISABLE_INTERRUPTS(); - // printf("xTaskRXDefinition.puxStackBuffer = %p\r\n", xTaskRXDefinition.puxStackBuffer); - // portENABLE_INTERRUPTS(); - xTaskCreateRestricted( &xTaskRXDefinition, - &xHandle_ReceiveTask); - - /* - * Prepare xRegions for Send Task - */ - memset(&xTaskTXDefinition.xRegions, 0, sizeof(xTaskTXDefinition.xRegions)); - - // authorize access to data and bss - // Low address - xTaskTXDefinition.xRegions[0].ulLengthInBytes = 4; - xTaskTXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NA4)); - addr_modifier ( xPmpInfo.granularity, - ( size_t ) __unprivileged_data_section_start__, - (size_t *) &xTaskTXDefinition.xRegions[0].pvBaseAddress); - - // High address - xTaskTXDefinition.xRegions[1].ulLengthInBytes = 4; - xTaskTXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_TOR)); - - addr_modifier ( xPmpInfo.granularity, - ( size_t ) __unprivileged_data_section_end__, - (size_t *) &xTaskTXDefinition.xRegions[1].pvBaseAddress); - -// #ifdef METAL_SIFIVE_GPIO0 -// // allow access to GPIO (Each peripheral are on 4Kb mapping area) -// xTaskTXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_GPIO0_0_SIZE; -// xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_NAPOT)); - -// napot_addr_modifier ( xPmpInfo.granularity, -// (size_t) METAL_SIFIVE_GPIO0_0_BASE_ADDRESS, -// (size_t *) &xTaskTXDefinition.xRegions[2].pvBaseAddress, -// xTaskTXDefinition.xRegions[2].ulLengthInBytes); -// #endif /* METAL_SIFIVE_GPIO0 */ + // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) + xTaskRXDefinition.puxStackBuffer = (StackType_t *)pvPortMalloc(xTaskRXDefinition.usStackDepth * sizeof(StackType_t)); + // xTaskRXDefinition.puxStackBuffer = (StackType_t *)aligned_malloc(xTaskRXDefinition.usStackDepth * sizeof(StackType_t), 128); + // xTaskRXDefinition.puxStackBuffer = xReceiveTaskStack; + // printf("xTaskRXDefinition.puxStackBuffer = %p\r\n", xTaskRXDefinition.puxStackBuffer); + xTaskCreateRestricted(&xTaskRXDefinition, &xHandle_ReceiveTask); + + /* + * Prepare xRegions for Send Task + */ + memset(&xTaskTXDefinition.xRegions, 0, sizeof(xTaskTXDefinition.xRegions)); + + // authorize access to data and bss + // Low address + xTaskTXDefinition.xRegions[0].ulLengthInBytes = 4; + xTaskTXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NA4)); + addr_modifier(xPmpInfo.granularity, + (size_t)__unprivileged_data_section_start__, + (size_t *)&xTaskTXDefinition.xRegions[0].pvBaseAddress); + + // High address + xTaskTXDefinition.xRegions[1].ulLengthInBytes = 4; + xTaskTXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_TOR)); + + addr_modifier(xPmpInfo.granularity, + (size_t)__unprivileged_data_section_end__, + (size_t *)&xTaskTXDefinition.xRegions[1].pvBaseAddress); + + // #ifdef METAL_SIFIVE_GPIO0 + // // allow access to GPIO (Each peripheral are on 4Kb mapping area) + // xTaskTXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_GPIO0_0_SIZE; + // xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + // (portPMP_REGION_ADDR_MATCH_NAPOT)); + + // napot_addr_modifier ( xPmpInfo.granularity, + // (size_t) METAL_SIFIVE_GPIO0_0_BASE_ADDRESS, + // (size_t *) &xTaskTXDefinition.xRegions[2].pvBaseAddress, + // xTaskTXDefinition.xRegions[2].ulLengthInBytes); + // #endif /* METAL_SIFIVE_GPIO0 */ #ifdef METAL_SIFIVE_UART0 - // allow access to UART peripheral - xTaskTXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; - xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - napot_addr_modifier ( xPmpInfo.granularity, - (size_t) METAL_SIFIVE_UART0_0_BASE_ADDRESS, - (size_t *) &xTaskTXDefinition.xRegions[2].pvBaseAddress, - xTaskTXDefinition.xRegions[2].ulLengthInBytes); + // allow access to UART peripheral + xTaskTXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; + xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + napot_addr_modifier(xPmpInfo.granularity, + (size_t)METAL_SIFIVE_UART0_0_BASE_ADDRESS, + (size_t *)&xTaskTXDefinition.xRegions[2].pvBaseAddress, + xTaskTXDefinition.xRegions[2].ulLengthInBytes); #endif /* METAL_SIFIVE_UART0 */ - // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) - xTaskTXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskTXDefinition.usStackDepth * sizeof( StackType_t ) ); - // xTaskTXDefinition.puxStackBuffer = xSendTaskStack; - - // printf("xTaskTXDefinition.puxStackBuffer = %p\r\n", xTaskTXDefinition.puxStackBuffer); + // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) + xTaskTXDefinition.puxStackBuffer = (StackType_t *)pvPortMalloc(xTaskTXDefinition.usStackDepth * sizeof(StackType_t)); + // xTaskTXDefinition.puxStackBuffer = xSendTaskStack; + // printf("xTaskTXDefinition.puxStackBuffer = %p\r\n", xTaskTXDefinition.puxStackBuffer); - xTaskCreateRestricted( &xTaskTXDefinition, - &xHandle_SendTask); + xTaskCreateRestricted(&xTaskTXDefinition, &xHandle_SendTask); + } - } - - break; + break; case '2': if (xHandle_ReceiveTask != NULL) vTaskDelete(xHandle_ReceiveTask); if (xHandle_SendTask != NULL) vTaskDelete(xHandle_SendTask); - if (xTaskRXDefinition.puxStackBuffer != NULL) + if (xTaskRXDefinition.puxStackBuffer != NULL) { vPortFree(xTaskRXDefinition.puxStackBuffer); + xTaskRXDefinition.puxStackBuffer = NULL; + } if (xTaskTXDefinition.puxStackBuffer != NULL) + { vPortFree(xTaskTXDefinition.puxStackBuffer); - break; + xTaskTXDefinition.puxStackBuffer = NULL; + } + if (xQueue != NULL) + { + vQueueDelete(xQueue); + xQueue = NULL; + } + break; #endif - case '4': - /* Obtain the current tick count. */ - printf("\nAuto-reload timer callback executing = %d\r\n", xTaskGetTickCount()); - - /* Pass the buffer into vTaskList() to generate the table of information. */ - vTaskList(cBuffer); - portDISABLE_INTERRUPTS(); - printf("%s\n", cBuffer); - portENABLE_INTERRUPTS(); - break; + case '4': + /* Obtain the current tick count. */ + printf("\nAuto-reload timer callback executing = %d\r\n", xTaskGetTickCount()); + + /* Pass the buffer into vTaskList() to generate the table of information. */ + vTaskList(cBuffer); + portDISABLE_INTERRUPTS(); + printf("%s\n", cBuffer); + portENABLE_INTERRUPTS(); + break; #ifdef CONFIG_APPS_SPY - case '8': - /* - * Prepare xRegions for xROAccess Task - */ - memset(&xROAccessTaskParameters.xRegions, 0, sizeof(xROAccessTaskParameters.xRegions)); - - // authorize access to data and bss - // Low address - xROAccessTaskParameters.xRegions[0].ulLengthInBytes = 4; - xROAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_ONLY) | - (portPMP_REGION_ADDR_MATCH_NA4)); - addr_modifier ( xPmpInfo.granularity, - // ( size_t ) __unprivileged_data_section_start__, - ( size_t ) ucSharedMemory, - (size_t *) &xROAccessTaskParameters.xRegions[0].pvBaseAddress); - // printf("xROAccessTaskParameters.xRegions[0].pvBaseAddress = %p\r\n", xROAccessTaskParameters.xRegions[0].ulLengthInBytes); - - // High address - xROAccessTaskParameters.xRegions[1].ulLengthInBytes = 4; - xROAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_TOR)); - - addr_modifier ( xPmpInfo.granularity, - ( size_t ) __unprivileged_data_section_end__, - (size_t *) &xROAccessTaskParameters.xRegions[1].pvBaseAddress); + case '8': + /* + * Prepare xRegions for xROAccess Task + */ + memset(&xROAccessTaskParameters.xRegions, 0, sizeof(xROAccessTaskParameters.xRegions)); + + // authorize access to data and bss + // Low address + xROAccessTaskParameters.xRegions[0].ulLengthInBytes = 4; + xROAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_ONLY) | + (portPMP_REGION_ADDR_MATCH_NA4)); + addr_modifier(xPmpInfo.granularity, + // ( size_t ) __unprivileged_data_section_start__, + (size_t)ucSharedMemory, + (size_t *)&xROAccessTaskParameters.xRegions[0].pvBaseAddress); + // printf("xROAccessTaskParameters.xRegions[0].pvBaseAddress = %p\r\n", xROAccessTaskParameters.xRegions[0].ulLengthInBytes); + + // High address + xROAccessTaskParameters.xRegions[1].ulLengthInBytes = 4; + xROAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_TOR)); + + addr_modifier(xPmpInfo.granularity, + (size_t)__unprivileged_data_section_end__, + (size_t *)&xROAccessTaskParameters.xRegions[1].pvBaseAddress); #ifdef METAL_SIFIVE_UART0 - // allow access to UART peripheral - xROAccessTaskParameters.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; - xROAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - napot_addr_modifier ( xPmpInfo.granularity, - (size_t) METAL_SIFIVE_UART0_0_BASE_ADDRESS, - (size_t *) &xROAccessTaskParameters.xRegions[2].pvBaseAddress, - xROAccessTaskParameters.xRegions[2].ulLengthInBytes); + // allow access to UART peripheral + xROAccessTaskParameters.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; + xROAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + napot_addr_modifier(xPmpInfo.granularity, + (size_t)METAL_SIFIVE_UART0_0_BASE_ADDRESS, + (size_t *)&xROAccessTaskParameters.xRegions[2].pvBaseAddress, + xROAccessTaskParameters.xRegions[2].ulLengthInBytes); #endif /* METAL_SIFIVE_UART0 */ - xROAccessTaskParameters.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xROAccessTaskParameters.usStackDepth * sizeof( StackType_t ) ); - // xROAccessTaskParameters.puxStackBuffer = xROAccessTaskStack; - // printf("xROAccessTaskParameters.puxStackBuffer = %p\r\n", xROAccessTaskParameters.puxStackBuffer); - - - xTaskCreateRestricted( &xROAccessTaskParameters, - &xHandle_TaskROAccess); - - /* - * Prepare xRegions for xRWAccess Task - */ - memset(&xRWAccessTaskParameters.xRegions, 0, sizeof(xRWAccessTaskParameters.xRegions)); - - // authorize access to data and bss - // Low address - // printf("xROAccessTaskParameters.xRegions[0].ulLengthInBytes = %d\r\n", xROAccessTaskParameters.xRegions[0].ulLengthInBytes); - xRWAccessTaskParameters.xRegions[0].ulLengthInBytes = 4; - xRWAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NA4)); - addr_modifier ( xPmpInfo.granularity, - // ( size_t ) __unprivileged_data_section_start__, - ( size_t ) ucSharedMemory, - (size_t *) &xRWAccessTaskParameters.xRegions[0].pvBaseAddress); - - // printf("xRWAccessTaskParameters.xRegions[0].pvBaseAddress = %p\r\n", xRWAccessTaskParameters.xRegions[0].pvBaseAddress); - // High address - xRWAccessTaskParameters.xRegions[1].ulLengthInBytes = 4; - xRWAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_TOR)); - - addr_modifier ( xPmpInfo.granularity, - ( size_t ) __unprivileged_data_section_end__, - (size_t *) &xRWAccessTaskParameters.xRegions[1].pvBaseAddress); + xROAccessTaskParameters.puxStackBuffer = (StackType_t *)pvPortMalloc(xROAccessTaskParameters.usStackDepth * sizeof(StackType_t)); + // xROAccessTaskParameters.puxStackBuffer = xROAccessTaskStack; + // printf("xROAccessTaskParameters.puxStackBuffer = %p\r\n", xROAccessTaskParameters.puxStackBuffer); + + xTaskCreateRestricted(&xROAccessTaskParameters, &xHandle_TaskROAccess); + + /* + * Prepare xRegions for xRWAccess Task + */ + memset(&xRWAccessTaskParameters.xRegions, 0, sizeof(xRWAccessTaskParameters.xRegions)); + + // authorize access to data and bss + // Low address + // printf("xROAccessTaskParameters.xRegions[0].ulLengthInBytes = %d\r\n", xROAccessTaskParameters.xRegions[0].ulLengthInBytes); + xRWAccessTaskParameters.xRegions[0].ulLengthInBytes = 4; + xRWAccessTaskParameters.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NA4)); + addr_modifier(xPmpInfo.granularity, + // ( size_t ) __unprivileged_data_section_start__, + (size_t)ucSharedMemory, + (size_t *)&xRWAccessTaskParameters.xRegions[0].pvBaseAddress); + + // printf("xRWAccessTaskParameters.xRegions[0].pvBaseAddress = %p\r\n", xRWAccessTaskParameters.xRegions[0].pvBaseAddress); + // High address + xRWAccessTaskParameters.xRegions[1].ulLengthInBytes = 4; + xRWAccessTaskParameters.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_TOR)); + + addr_modifier(xPmpInfo.granularity, + (size_t)__unprivileged_data_section_end__, + (size_t *)&xRWAccessTaskParameters.xRegions[1].pvBaseAddress); #ifdef METAL_SIFIVE_UART0 - // allow access to UART peripheral - xRWAccessTaskParameters.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; - xRWAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | - (portPMP_REGION_ADDR_MATCH_NAPOT)); - - napot_addr_modifier ( xPmpInfo.granularity, - (size_t) METAL_SIFIVE_UART0_0_BASE_ADDRESS, - (size_t *) &xRWAccessTaskParameters.xRegions[2].pvBaseAddress, - xRWAccessTaskParameters.xRegions[2].ulLengthInBytes); + // allow access to UART peripheral + xRWAccessTaskParameters.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; + xRWAccessTaskParameters.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | + (portPMP_REGION_ADDR_MATCH_NAPOT)); + + napot_addr_modifier(xPmpInfo.granularity, + (size_t)METAL_SIFIVE_UART0_0_BASE_ADDRESS, + (size_t *)&xRWAccessTaskParameters.xRegions[2].pvBaseAddress, + xRWAccessTaskParameters.xRegions[2].ulLengthInBytes); #endif /* METAL_SIFIVE_UART0 */ - xRWAccessTaskParameters.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xRWAccessTaskParameters.usStackDepth * sizeof( StackType_t ) ); - // xRWAccessTaskParameters.puxStackBuffer = xRWAccessTaskStack; - // printf("xRWAccessTaskParameters.puxStackBuffer = %p\r\n", xRWAccessTaskParameters.puxStackBuffer); + xRWAccessTaskParameters.puxStackBuffer = (StackType_t *)pvPortMalloc(xRWAccessTaskParameters.usStackDepth * sizeof(StackType_t)); + // xRWAccessTaskParameters.puxStackBuffer = xRWAccessTaskStack; + // printf("xRWAccessTaskParameters.puxStackBuffer = %p\r\n", xRWAccessTaskParameters.puxStackBuffer); - xTaskCreateRestricted( &xRWAccessTaskParameters, - &xHandle_TaskRWAccess); + xTaskCreateRestricted(&xRWAccessTaskParameters, &xHandle_TaskRWAccess); - // spyAppTask(); - break; + // spyAppTask(); + break; case '9': if (xHandle_TaskROAccess != NULL) vTaskDelete(xHandle_TaskROAccess); if (xHandle_TaskRWAccess != NULL) vTaskDelete(xHandle_TaskRWAccess); - if (xROAccessTaskParameters.puxStackBuffer != NULL) + if (xROAccessTaskParameters.puxStackBuffer != NULL) { vPortFree(xROAccessTaskParameters.puxStackBuffer); - if (xRWAccessTaskParameters.puxStackBuffer != NULL) + xROAccessTaskParameters.puxStackBuffer = NULL; + } + if (xRWAccessTaskParameters.puxStackBuffer != NULL) { vPortFree(xRWAccessTaskParameters.puxStackBuffer); - break; + xRWAccessTaskParameters.puxStackBuffer != NULL; + } + break; #endif - default: - break; - } - taskYIELD(); + default: + break; + } + taskYIELD(); vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); - } while (1); + } while (1); } - -static void ledBlinkTask( void *pvParameters ) PRIVILEGED_FUNCTION +static void ledBlinkTask(void *pvParameters) PRIVILEGED_FUNCTION { - unsigned long ulCounter = 0; - /* Remove compiler warning about unused parameter. */ - ( void ) pvParameters; + (void)pvParameters; - struct metal_gpio *led0; //make instance of GPIO + struct metal_gpio *led0; // make instance of GPIO - //Get gpio device handle, i.e.) define IC pin here where IC's GPIO = 5, pin silkscreen = 13 - //this is the GPIO device index that is referenced from 0, make sure to check the schematic - led0 = metal_gpio_get_device(0); + // Get gpio device handle, i.e.) define IC pin here where IC's GPIO = 5, pin silkscreen = 13 + // this is the GPIO device index that is referenced from 0, make sure to check the schematic + led0 = metal_gpio_get_device(0); - //Pins are set when initialized so we must disable it when we use it as an input/output - metal_gpio_disable_input(led0, 5); + // Pins are set when initialized so we must disable it when we use it as an input/output + metal_gpio_disable_input(led0, 5); - //Set as gpio as output - metal_gpio_enable_output(led0, 5); + // Set as gpio as output + metal_gpio_enable_output(led0, 5); - //Pins have more than one function, make sure we disconnect anything connected... - metal_gpio_disable_pinmux(led0, 5); + // Pins have more than one function, make sure we disconnect anything connected... + metal_gpio_disable_pinmux(led0, 5); - //Turn ON pin - metal_gpio_set_pin(led0, 5, 1); + // Turn ON pin + metal_gpio_set_pin(led0, 5, 1); - for( ;; ) + for (;;) { - metal_gpio_set_pin(led0, 5, ++ulCounter&0x01); vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); - + metal_gpio_set_pin(led0, 5, 0); + printf("[ledBlinkTask] Led OFF\r\n"); + vTaskDelay(mainQUEUE_TICK_COUNT_FOR_1S); + metal_gpio_set_pin(led0, 5, 1); + printf("[ledBlinkTask] Led ON\r\n"); } } - -// void print_stack_pointer() { -// void* p = NULL; -// printf("%p\n", (void*)&p); -// } /*-----------------------------------------------------------*/ -int main( void ) -{ - TaskHandle_t xHandle_ReceiveTask, xHandle_SendTask, xHandle_TaskListTasks, xHandle_TaskROAccess, xHandle_TaskRWAccess; - - // static StackType_t xTaskListTasksShowStack[ configMINIMAL_STACK_SIZE * 3] __attribute__( ( aligned( 32 ) ) ); - // static StackType_t xSendTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) ); - // static StackType_t xReceiveTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) ); - // static StackType_t xROAccessTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) ); - // static StackType_t xRWAccessTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) ); - - const char * const pcMessage = "FreeRTOS-TEE Demo start\r\n"; - const char * const pcMessageEnd = "FreeRTOS-TEE Demo end\r\n"; - const char * const pcMessageEndError = "FreeRTOS-TEE Demo end - Error no enough PMP entry\r\n"; - const char * const pcMessageGranularityError = "FreeRTOS-TEE Demo end - Error platform granularity no supported\r\n"; +int main(void) +{ + const char *const pcMessageEndError = "Error: no enough PMP entry\r\n"; + const char *const pcMessageGranularityError = "Error: platform granularity no supported\r\n"; extern uint32_t __unprivileged_data_section_start__[]; extern uint32_t __unprivileged_data_section_end__[]; @@ -680,9 +593,7 @@ int main( void ) prvSetupHardware(); - printf("\n\n\t-= mTower v" VERSION " =- " __DATE__ " " __TIME__"\r\n\n"); - - // printf("__ricsv_xlen = %d\r\n", __riscv_xlen); + printf("\n\n\t-= mTower v" VERSION " =- " __DATE__ " " __TIME__ "\r\n\n"); // pul = __unprivileged_data_section_start__; // printf("__unprivileged_data_section_start__ = %p\r\n", pul); @@ -721,326 +632,95 @@ int main( void ) BaseType_t xReturned; TaskHandle_t xHandle = NULL; - // char *buf = pvPortMalloc(8); - // if (buf != NULL) { - // printf("pvPortMalloc: *buf = %p\r\n", buf); - // } - - // xReturned = xTaskCreate( - // vTaskCode, /* Function that implements the task. */ - // "PRVLGD", /* Text name for the task. */ - // 512, /* Stack size in words, not bytes. */ - // NULL, //( void * ) 1, /* Parameter passed into the task. */ - // mainQUEUE_SEND_TASK_PRIORITY | portPRIVILEGE_BIT,/* Priority at which the task is created. */ - // &xHandle ); /* Used to pass out the created task's handle. */ - - xTaskCreate(menuTask, /* The function that implements the task. */ - "menu", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ - 768, /* The size of the stack to allocate to the task. */ - (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ + xTaskCreate(menuTask, /* The function that implements the task. */ + "menu", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ + 400, /* The size of the stack to allocate to the task. */ + (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ mainQUEUE_SEND_TASK_PRIORITY | portPRIVILEGE_BIT, /* The priority assigned to the task. */ NULL); - // xTaskCreate(ledBlinkTask, /* The function that implements the task. */ - // "ledBlink", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ - // 128, /* The size of the stack to allocate to the task. */ - // (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ - // mainQUEUE_SEND_TASK_PRIORITY | portPRIVILEGE_BIT, /* The priority assigned to the task. */ - // NULL); + xTaskCreate(ledBlinkTask, /* The function that implements the task. */ + "ledBlink", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ + 128, /* The size of the stack to allocate to the task. */ + (void *)NULL, /* The parameter passed to the task - just to check the functionality. */ + mainQUEUE_SEND_TASK_PRIORITY | portPRIVILEGE_BIT, /* The priority assigned to the task. */ + NULL); -#if( portUSING_MPU_WRAPPERS == 1 ) - if (xPmpInfo.nb_pmp < 8) - { - write( STDOUT_FILENO, pcMessageEndError, strlen( pcMessageEndError ) ); +#if (portUSING_MPU_WRAPPERS == 1) + if (xPmpInfo.nb_pmp < 8) { + printf("%s", pcMessageEndError); _exit(0); } else if (xPmpInfo.granularity > 4) { - /* - * platfrom granularity > 4 bytes is not supported yet, some - * modifications are needed on FreeRTOS port to do so. - */ - write( STDOUT_FILENO, pcMessageGranularityError, strlen( pcMessageGranularityError ) ); + /* + * platfrom granularity > 4 bytes is not supported yet, some + * modifications are needed on FreeRTOS port to do so. + */ + printf("%s", pcMessageGranularityError); _exit(0); - } + } // printf("xPmpInfo.nb_pmp = %d\r\n", xPmpInfo.nb_pmp); + // printf("xPmpInfo.granularity= %d\r\n", xPmpInfo.granularity); - /* Create the queue. */ - xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) ); - - if( xQueue != NULL ) - { - extern uint32_t __unprivileged_data_section_start__[]; - extern uint32_t __unprivileged_data_section_end__[]; - - // static TaskParameters_t xTaskListTasksShowDefinition = - // { - // .pvTaskCode = prvListTasksShowTask, - // .pcName = "UNPRVLGD", - // .usStackDepth = 224, //configMINIMAL_STACK_SIZE, - // .pvParameters = NULL, - // .uxPriority = mainQUEUE_RECEIVE_TASK_PRIORITY, - // .puxStackBuffer = NULL, - // }; - - // static TaskParameters_t xTaskRXDefinition = - // { - // .pvTaskCode = prvQueueReceiveTask, - // .pcName = "Rx", - // .usStackDepth = configMINIMAL_STACK_SIZE, - // .pvParameters = NULL, - // .uxPriority = mainQUEUE_RECEIVE_TASK_PRIORITY, - // .puxStackBuffer = NULL, - // }; - - // static TaskParameters_t xTaskTXDefinition = - // { - // .pvTaskCode = prvQueueSendTask, - // .pcName = "Tx", - // .usStackDepth = configMINIMAL_STACK_SIZE, - // .pvParameters = NULL, - // .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, - // .puxStackBuffer = NULL, - // }; - - // static TaskParameters_t xROAccessTaskParameters = - // { - // .pvTaskCode = prvROAccessTask, - // .pcName = "ROAccess", - // .usStackDepth = 224, //configMINIMAL_STACK_SIZE, - // .pvParameters = NULL, - // .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, - // .puxStackBuffer = NULL, - // // .puxStackBuffer = xROAccessTaskStack, - // }; - - // static TaskParameters_t xRWAccessTaskParameters = - // { - // .pvTaskCode = prvRWAccessTask, - // .pcName = "RWAccess", - // .usStackDepth = 128, //configMINIMAL_STACK_SIZE, - // .pvParameters = NULL, - // .uxPriority = mainQUEUE_SEND_TASK_PRIORITY, - // .puxStackBuffer = NULL, - // // .puxStackBuffer = xRWAccessTaskStack, - // }; - - // printf("xROAccessTaskParameters.pcName = %s\r\n", xROAccessTaskParameters.pcName); - // printf("xTaskTXDefinition.pcName = %s\r\n", xTaskTXDefinition.pcName); - - if(0 == xPmpInfo.granularity) - { - init_pmp (&xPmpInfo); - } - // printf("xPmpInfo.granularity = %d\r\n", xPmpInfo.granularity); -// /* -// * Prepare xRegions for Receive Task -// */ -// memset(&xTaskRXDefinition.xRegions, 0, sizeof(xTaskRXDefinition.xRegions)); - -// // authorize access to data and bss -// // Low address -// xTaskRXDefinition.xRegions[0].ulLengthInBytes = 4; -// xTaskRXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_NA4)); -// addr_modifier ( xPmpInfo.granularity, -// ( size_t ) __unprivileged_data_section_start__, -// (size_t *) &xTaskRXDefinition.xRegions[0].pvBaseAddress); -// // printf("xTaskRXDefinition.xRegions[0].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[0].pvBaseAddress); -// // High address -// xTaskRXDefinition.xRegions[1].ulLengthInBytes = 4; -// xTaskRXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_TOR)); - -// addr_modifier ( xPmpInfo.granularity, -// ( size_t ) __unprivileged_data_section_end__, -// (size_t *) &xTaskRXDefinition.xRegions[1].pvBaseAddress); -// // printf("xTaskRXDefinition.xRegions[1].pvBaseAddress = %p\r\n", xTaskRXDefinition.xRegions[1].pvBaseAddress); - -// #ifdef METAL_SIFIVE_UART0 -// // allow access to UART peripheral -// xTaskRXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; -// xTaskRXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_NAPOT)); - -// napot_addr_modifier ( xPmpInfo.granularity, -// (size_t) METAL_SIFIVE_UART0_0_BASE_ADDRESS, -// (size_t *) &xTaskRXDefinition.xRegions[2].pvBaseAddress, -// xTaskRXDefinition.xRegions[2].ulLengthInBytes); -// #endif /* METAL_SIFIVE_UART0 */ - -// // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) -// xTaskRXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskRXDefinition.usStackDepth * sizeof( StackType_t ) ); -// // xTaskRXDefinition.puxStackBuffer = xReceiveTaskStack; -// // portDISABLE_INTERRUPTS(); -// printf("xTaskRXDefinition.puxStackBuffer = %p\r\n", xTaskRXDefinition.puxStackBuffer); -// // portENABLE_INTERRUPTS(); -// xTaskCreateRestricted( &xTaskRXDefinition, -// &xHandle_ReceiveTask); - -// /* -// * Prepare xRegions for Send Task -// */ -// memset(&xTaskTXDefinition.xRegions, 0, sizeof(xTaskTXDefinition.xRegions)); - -// // authorize access to data and bss -// // Low address -// xTaskTXDefinition.xRegions[0].ulLengthInBytes = 4; -// xTaskTXDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_NA4)); -// addr_modifier ( xPmpInfo.granularity, -// ( size_t ) __unprivileged_data_section_start__, -// (size_t *) &xTaskTXDefinition.xRegions[0].pvBaseAddress); - -// // High address -// xTaskTXDefinition.xRegions[1].ulLengthInBytes = 4; -// xTaskTXDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_TOR)); - -// addr_modifier ( xPmpInfo.granularity, -// ( size_t ) __unprivileged_data_section_end__, -// (size_t *) &xTaskTXDefinition.xRegions[1].pvBaseAddress); - -// #ifdef METAL_SIFIVE_GPIO0 -// // allow access to GPIO (Each peripheral are on 4Kb mapping area) -// xTaskTXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_GPIO0_0_SIZE; -// xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_NAPOT)); - -// napot_addr_modifier ( xPmpInfo.granularity, -// (size_t) METAL_SIFIVE_GPIO0_0_BASE_ADDRESS, -// (size_t *) &xTaskTXDefinition.xRegions[2].pvBaseAddress, -// xTaskTXDefinition.xRegions[2].ulLengthInBytes); -// #endif /* METAL_SIFIVE_GPIO0 */ - -// // #ifdef METAL_SIFIVE_UART0 -// // // allow access to UART peripheral -// // xTaskTXDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; -// // xTaskTXDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | -// // (portPMP_REGION_ADDR_MATCH_NAPOT)); - -// // napot_addr_modifier ( xPmpInfo.granularity, -// // (size_t) METAL_SIFIVE_UART0_0_BASE_ADDRESS, -// // (size_t *) &xTaskTXDefinition.xRegions[2].pvBaseAddress, -// // xTaskTXDefinition.xRegions[2].ulLengthInBytes); -// // #endif /* METAL_SIFIVE_UART0 */ -// // allocate stack (It will take 2 PMP Slot - So it is not needed to put align the StackBuffer) -// xTaskTXDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskTXDefinition.usStackDepth * sizeof( StackType_t ) ); -// // xTaskTXDefinition.puxStackBuffer = xSendTaskStack; - -// printf("xTaskTXDefinition.puxStackBuffer = %p\r\n", xTaskTXDefinition.puxStackBuffer); - -// xTaskCreateRestricted( &xTaskTXDefinition, -// &xHandle_SendTask); - - /* - * Prepare xRegions for ListTasksShow Task - */ -// memset(&xTaskListTasksShowDefinition.xRegions, 0, sizeof(xTaskListTasksShowDefinition.xRegions)); - -// xTaskListTasksShowDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskListTasksShowDefinition.usStackDepth * sizeof( StackType_t ) ); - -// // xTaskListTasksShowDefinition.puxStackBuffer = xTaskListTasksShowStack; - -// xTaskListTasksShowDefinition.xRegions[0].ulLengthInBytes = xTaskListTasksShowDefinition.usStackDepth * sizeof( StackType_t ); -// xTaskListTasksShowDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_NAPOT)); - -// napot_addr_modifier ( xPmpInfo.granularity, -// (size_t) xTaskListTasksShowDefinition.puxStackBuffer, -// (size_t *) &xTaskListTasksShowDefinition.xRegions[0].pvBaseAddress, -// xTaskListTasksShowDefinition.xRegions[0].ulLengthInBytes); - - -// // authorize access to data and bss -// // Low address -// // xTaskListTasksShowDefinition.xRegions[0].ulLengthInBytes = 4; -// // xTaskListTasksShowDefinition.xRegions[0].ulParameters = ((portPMP_REGION_READ_WRITE) | -// // (portPMP_REGION_ADDR_MATCH_NA4)); -// // addr_modifier ( xPmpInfo.granularity, -// // ( size_t ) __unprivileged_data_section_start__, -// // (size_t *) &xTaskListTasksShowDefinition.xRegions[0].pvBaseAddress); - -// // High address -// xTaskListTasksShowDefinition.xRegions[1].ulLengthInBytes = 4; -// xTaskListTasksShowDefinition.xRegions[1].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_TOR)); - -// addr_modifier ( xPmpInfo.granularity, -// ( size_t ) __unprivileged_data_section_end__, -// (size_t *) &xTaskListTasksShowDefinition.xRegions[1].pvBaseAddress); - -// #ifdef METAL_SIFIVE_UART0 -// // allow access to UART peripheral -// xTaskListTasksShowDefinition.xRegions[2].ulLengthInBytes = METAL_SIFIVE_UART0_0_SIZE; -// xTaskListTasksShowDefinition.xRegions[2].ulParameters = ((portPMP_REGION_READ_WRITE) | -// (portPMP_REGION_ADDR_MATCH_NAPOT)); - -// napot_addr_modifier ( xPmpInfo.granularity, -// (size_t) METAL_SIFIVE_UART0_0_BASE_ADDRESS, -// (size_t *) &xTaskListTasksShowDefinition.xRegions[2].pvBaseAddress, -// xTaskListTasksShowDefinition.xRegions[2].ulLengthInBytes); -// #endif /* METAL_SIFIVE_UART0 */ - -// // xTaskListTasksShowDefinition.puxStackBuffer = ( StackType_t * ) pvPortMalloc( xTaskListTasksShowDefinition.usStackDepth * sizeof( StackType_t ) ); -// // // xTaskListTasksShowDefinition.puxStackBuffer = xTaskListTasksShowStack; - -// printf("xTaskListTasksShowDefinition.puxStackBuffer = %p\r\n", xTaskListTasksShowDefinition.puxStackBuffer); -// // // printf("Test = "); -// // // print_stack_pointer(); -// xTaskCreateRestricted( &xTaskListTasksShowDefinition, -// &xHandle_TaskListTasks); + if (0 == xPmpInfo.granularity) { + init_pmp(&xPmpInfo); + } - /* Start the tasks and timer running. */ - vTaskStartScheduler(); + /* Start the tasks and timer running. */ + vTaskStartScheduler(); /* If all is well, the scheduler will now be running, and the following line will never be reached. If the following line does execute, then there was insufficient FreeRTOS heap memory available for the Idle and/or - timer tasks to be created. + timer tasks to be created. or task have stoppped the Scheduler */ - // vTaskDelete( xHandle_SendTask ); - // vTaskDelete( xHandle_ReceiveTask ); - } + // vTaskDelete( xHandle_SendTask ); + // vTaskDelete( xHandle_ReceiveTask ); #endif /* ( portUSING_MPU_WRAPPERS == 1 ) */ } - /*-----------------------------------------------------------*/ -static void prvQueueSendTask( void *pvParameters ) +static void prvQueueSendTask(void *pvParameters) { TickType_t xNextWakeTime; BaseType_t xReturned; unsigned long ulCounter = 0; /* Remove compiler warning about unused parameter. */ - ( void ) pvParameters; - ( void ) xReturned; + (void)pvParameters; + (void)xReturned; + + printf("prvQueueSendTask task: "); + if (portIS_PRIVILEGED() == 1) { + printf("TX task is running in priveleged mode\r\n"); + } else { + printf("Tx task is running in unpriveleged mode\r\n"); + } /* Initialise xNextWakeTime - this only needs to be done once. */ xNextWakeTime = xTaskGetTickCount(); - for( ; ; ) + for (;;) { - printf("[ Tx ] : %d\r\n", ++ulCounter ); + printf("[ Tx ] : %d\r\n", ++ulCounter); /* Place this task in the blocked state until it is time to run again. */ - vTaskDelayUntil( &xNextWakeTime, mainQUEUE_TICK_COUNT_FOR_1S); + vTaskDelayUntil(&xNextWakeTime, mainQUEUE_TICK_COUNT_FOR_1S); // vTaskDelay( mainQUEUE_TICK_COUNT_FOR_1S * 500); - /* Send to the queue - causing the queue receive task to unblock and toggle the LED. 0 is used as the block time so the sending operation will not block - it shouldn't need to block as the queue should always be empty at this point in the code. */ - xReturned = xQueueSend( xQueue, &ulCounter, 0U ); - configASSERT( xReturned == pdPASS ); + xReturned = xQueueSend(xQueue, &ulCounter, 0U); + configASSERT(xReturned == pdPASS); } - /** + /** * SiFive CI/CD need to have a exit(0) status to pass */ -#if( portUSING_MPU_WRAPPERS == 1 ) +#if (portUSING_MPU_WRAPPERS == 1) /* We run into user mode, so need to be machine mode before to call vTaskEndScheduler */ // xPortRaisePrivilege(); #endif /* ( portUSING_MPU_WRAPPERS == 1 ) */ @@ -1048,61 +728,41 @@ static void prvQueueSendTask( void *pvParameters ) } /*-----------------------------------------------------------*/ -static void prvQueueReceiveTask( void *pvParameters ) +static void prvQueueReceiveTask(void *pvParameters) { unsigned long ulReceivedValue; /* Remove compiler warning about unused parameter. */ - ( void ) pvParameters; + (void)pvParameters; + + printf("prvQueueReceiveTask task: "); + if (portIS_PRIVILEGED() == 1) { + printf("RX task is running in priveleged mode\r\n"); + } else { + printf("Rx task is running in unpriveleged mode\r\n"); + } - for( ;; ) + for (;;) { /* Wait until something arrives in the queue - this task will block indefinitely provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. */ - xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY ); + xQueueReceive(xQueue, &ulReceivedValue, portMAX_DELAY); /* To get here something must have been received from the queue, but is it the expected value? If it is, toggle the LED. */ - printf("[ Rx ] : %d\r\n", ulReceivedValue ); + printf("[ Rx ] : %d\r\n", ulReceivedValue); } } /*-----------------------------------------------------------*/ -static void prvSetupHardware( void ) +static void prvSetupHardware(void) { - // const char * const pcWarningMsg = "At least one of LEDs is null.\r\n"; - - // // This demo will toggle LEDs colors so we define them here - // led0_red = metal_led_get_rgb("LD0", "red"); - // led0_green = metal_led_get_rgb("LD0", "green"); - // led0_blue = metal_led_get_rgb("LD0", "blue"); - // if ((led0_red == NULL) || (led0_green == NULL) || (led0_blue == NULL)) - // { - // printf(pcWarningMsg); - // // write( STDOUT_FILENO, pcWarningMsg, strlen( pcWarningMsg ) ); - // } - // else - // { - // // Enable each LED - // metal_led_enable(led0_red); - // metal_led_enable(led0_green); - // metal_led_enable(led0_blue); - - // // All Off - // metal_led_off(led0_red); - // metal_led_off(led0_green); - // metal_led_off(led0_blue); - // // metal_led_on(led0_red); - // // metal_led_on(led0_green); - // // metal_led_on(led0_blue); - // } } /*-----------------------------------------------------------*/ - -void vApplicationMallocFailedHook( void ) +void vApplicationMallocFailedHook(void) { /* vApplicationMallocFailedHook() will only be called if configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook @@ -1117,23 +777,16 @@ void vApplicationMallocFailedHook( void ) taskDISABLE_INTERRUPTS(); -#if( portUSING_MPU_WRAPPERS == 1 ) +#if (portUSING_MPU_WRAPPERS == 1) /* need to be machine mode */ // xPortRaisePrivilege(); #endif /* ( portUSING_MPU_WRAPPERS == 1 ) */ printf("ERROR malloc\r\n"); - - if ( led0_red != NULL ) - { - // Red light on - metal_led_off(led0_red); - } - _exit(1); } /*-----------------------------------------------------------*/ -void vApplicationIdleHook( void ) +void vApplicationIdleHook(void) { /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle @@ -1147,43 +800,32 @@ void vApplicationIdleHook( void ) } /*-----------------------------------------------------------*/ -void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName ) +void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName) { - ( void ) pcTaskName; - ( void ) pxTask; + (void)pcTaskName; + (void)pxTask; /* Run time stack overflow checking is performed if configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is called if a stack overflow is detected. */ taskDISABLE_INTERRUPTS(); - printf("ERROR Stack overflow on func: %s\r\n",pcTaskName); - - // if ( led0_red != NULL ) - // { - // // Red light on - // metal_led_off(led0_red); - // } + printf("ERROR Stack overflow on func: %s\r\n", pcTaskName); _exit(1); } /*-----------------------------------------------------------*/ -void vApplicationTickHook( void ) +void vApplicationTickHook(void) { /* The tests in the full demo expect some interaction with interrupts. */ + // printf("vApplicationTickHook\r\n"); } /*-----------------------------------------------------------*/ -void vAssertCalled( void ) +void vAssertCalled(void) { taskDISABLE_INTERRUPTS(); - if ( led0_red != NULL ) - { - // Red light on - metal_led_off(led0_red); - } - _exit(1); } diff --git a/configs/sparkfun_redboard/Make.defs b/configs/sparkfun_redboard/Make.defs index 72fd1ef3..1455d2cb 100644 --- a/configs/sparkfun_redboard/Make.defs +++ b/configs/sparkfun_redboard/Make.defs @@ -64,15 +64,9 @@ ARCHDEFINES += -DPACKAGE_URL=\"https://github.com/sifive/freedom-metal\" -DPACKA ARCHDEFINES += -DMTIME_RATE_HZ_DEF=32768 -DportUSING_MPU_WRAPPERS=1 # -I. -# -I/home/virtual-box/projects/SiFive/freedom-e-sdk/freedom-metal -# -I/home/virtual-box/projects/SiFive/freedom-e-sdk/bsp/sifive-hifive1-revb/install/include # --specs=nano.specs -DMTIME_RATE_HZ_DEF=32768 # -O0 -g -MT gloss/sys_read.o -MD -MP -MF -# $depbase.Tpo -c -o gloss/sys_read.o -# /home/virtual-box/projects/SiFive/freedom-e-sdk/freedom-metal/gloss/sys_read.c &&\ -#depbase=`echo gloss/sys_sbrk.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\ - CFLAGS = -std=gnu99 -pipe -Wl,--gc-sections CFLAGS += $(ARCHWARNINGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES)