/*
 * FreeRTOS Kernel V11.1.0
 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

/*
 * This is a simple main that will start the FreeRTOS-Kernel and run a periodic task
 * that only delays if compiled with the template port, this project will do nothing.
 * For more information on getting started please look here:
 * https://freertos.org/FreeRTOS-quick-start-guide.html
 */

/* FreeRTOS includes. */
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <timers.h>
#include <semphr.h>
#include <debug_log.h>

/* Standard includes. */
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <semphr.h>

SemaphoreHandle_t xSemaphore = NULL;

/* stub functions for system calls */

void _exit(int status) { while (1); }                      // Infinite loop for _exit

int _kill(int pid, int sig) { return -1; }

int _getpid(void) { return 1; }

int _write(int file, char *ptr, int len) { return 0; }

int _close(int file) { return -1; }

int _fstat(int file, struct stat *st) { return 0; }

int _isatty(int file) { return 1; }

int _lseek(int file, int ptr, int dir) { return 0; }

int _read(int file, char *ptr, int len) { return 0; }
 
caddr_t _sbrk_r(struct _reent *r, int incr) {
    // In bare-metal systems, we can simply adjust a static "heap_end" pointer
    //char *prev_heap_end = heap_end;
    //heap_end += incr;  // Increase the heap size by the requested increment

    //return (caddr_t)prev_heap_end;  // Return the previous heap end
    return NULL;
}

/*-----------------------------------------------------------*/

static void exampleTask( void * parameters );

/*-----------------------------------------------------------*/

static void exampleTask( void * parameters )
{
    /* Unused parameters. */
    ( void ) parameters;

    for( ; ; )
    {
        /* Example Task Code */
        vTaskDelay( 100 ); /* delay 100 ticks */
    }
}
/*-----------------------------------------------------------*/


StaticTask_t exampleTaskTCB;
StackType_t exampleTaskStack[configMINIMAL_STACK_SIZE];

#if 0
void polling_task(void *pvParameters)
{
    (void) pvParameters; // Prevent compiler warning about unused parameter

    /* Dump system state in every 2 sec */
    for (;;) {
        //dumpSystemInfo();
        vTaskDelay(pdMS_TO_TICKS(30000)); // Delay for 30 s
    }
}
#endif
void main( void )
{
    //char *dst_ptr = 0xFFFF0000; copy vector table  and system control register bit 13 = 1 ftlr , 


    //( void ) printf( "Example FreeRTOS Project\n" );

#if 1
    char *src_ptr = 0x200000;
    char *dst_ptr = 0x000000;
    memcpy(dst_ptr ,src_ptr , 32);
    //__asm ("cpsie i");
    //__asm ("cpsie f");
    //__asm volatile ( "CPSIE i   \n" ::: "memory" );
#endif
    //rtos_printf("Example FreeRTOS Project\n");
#if 0
    //__asm ("bkpt ");
    xSemaphore = xSemaphoreCreateMutex();
    if( xSemaphore == NULL )
    {
      rtos_printf("semaphore creation failed\n");
       /* The semaphore creation failed */
      return;
    }
#endif
#if 1
    ( void ) xTaskCreateStatic( exampleTask,
                                "example",
                                configMINIMAL_STACK_SIZE,
                                NULL,
                                configMAX_PRIORITIES - 1U,
                                exampleTaskStack,
                                &(exampleTaskTCB));
    /* Start the scheduler. */
    vTaskStartScheduler();
#endif

#if 0
    if (xTaskCreate(polling_task, "Polling Task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS) {
                goto exit;
    }
    rtos_printf("Example FreeRTOS Project end \n");
    /* Start the scheduler. */
    vTaskStartScheduler();

exit:
    rtos_printf("Initializtion failed!\n");
#endif
    for( ; ; )
    {
        /* Should not reach here. */
    }
}
/*-----------------------------------------------------------*/

#if ( configCHECK_FOR_STACK_OVERFLOW > 0 )

    void vApplicationStackOverflowHook( TaskHandle_t xTask,
                                        char * pcTaskName )
    {
        /* Check pcTaskName for the name of the offending task,
         * or pxCurrentTCB if pcTaskName has itself been corrupted. */
        ( void ) xTask;
        ( void ) pcTaskName;
    }

#endif /* #if ( configCHECK_FOR_STACK_OVERFLOW > 0 ) */
/*-----------------------------------------------------------*/
void vApplicationIRQHandler(void) {
    // Your custom interrupt handler code
}