/*
 * FILENAME: inmain.c
 *
 * Copyright 2011 By InterNiche Technologies Inc. All rights reserved
 *
 * NicheStack main() function
 *
 * MODULE: MCB1700
 *
 * ROUTINES: c_entry()
 *
 * PORTABLE: NO
 */

#include "FreeRTOS.h"
#include "task.h"
#include "embtcp.h"

extern int32_t system_init(void);
extern int32_t nichestack_init(uint32_t);
extern void    embTCP_tick_hook(void);

extern void example_init(void);


/* FUNCTION: main()
 *
 * Top-level NicheTask/SuperLoop function
 * 
 * PARAMS: none
 *
 * RETURN: int                program termination code
 */
int
main(void)
{
   int32_t  err;
   char  buf[32];

   /*
    * Perform any required system initialization
    */ 
   if ((err = system_init()) < 0)
   {
      sprintf(&buf[0], "system_init error %d", err);
      panic(buf);
   }

   /*
    * Perform any OS-specific initialization
    */
   TK_INIT_OS();

   /*
    * Perform Stack initialization
    */
   err = nichestack_init(0);
   if (err < 0)
   {
      sprintf(&buf[0], "nichestack_init error %d", err);
      panic(buf);
   }

   /*
    * Perform example app initialization
    */
/*
   example_init();
*/

   /*
    * Pass control to the OS
    */
   TK_START_OS();

   /*
    * We shouldn't return here, but just in case ...
    */
   panic("exit");
   return (1);
}



/*----------------- FreeRTOS Application Hooks ----------------------------*/

/* FUNCTION: vApplicationTickHook()
 *
 * Called by the FreeRTOS timer interrupt function.
 */
void
vApplicationTickHook(void)
{
   /* DO NOT remove this function call if you are using embTCP. */
   embTCP_tick_hook();        /* increment the embTCP 'cticks' counter */
}


#if (configUSE_IDLE_HOOK == 1)

/* FUNCTION: vApplicationIdleHook()
 *
 * Called from the "idle" task.
 *
 * PARAMS: none
 *
 * RETURN: none
 */
void
vApplicationIdleHook(void)
{
   const unsigned long ulMSToSleep = 5;

   /* Sleep to reduce CPU load, but don't sleep indefinitely in case
    * there are tasks waiting to be terminated by the idle task.
    */
   Sleep(ulMSToSleep);
}

#endif

#if (configUSE_MALLOC_FAILED_HOOK == 1)

/* FUNCITON: vApplicationMallocFailedHook()
 *
 * Called by FreeRTOS when a memory allocation request fails.
 *
 * PARAMS: none
 *
 * RETURN: none
 */
void
vApplicationMallocFailedHook(void)
{
   panic("malloc");
}

#endif

#if (configCHECK_FOR_STACK_OVERFLOW > 0)

/* FUNCTION: vApplicationStackOverflowHook()
 *
 * Called if the OS detects a task stack overflow.
 *
 * PARAM1: xTaskHandle        current FreeRTOS task
 * PARAM2: char *             name of current task
 *
 * RETURN: none
 */
void
vApplicationStackOverflowHook(xTaskHandle curTCB, char *name)
{
   panic("stack overflow");
}

#endif

/* FUNCTION: vAssertCalled()
 *
 * Called if "configAssert(x)" is defined in FreeRTOSConfig.h.
 *
 * PARAMS: none
 *
 * RETURN: none
 */
void
vAssertCalled(void)
{
   panic("assert");
}

