#include <at32f403a_407_clock.h>
#include <SystemDelayInterface.h>
#include <stdio.h>
#include "cmsis_os.h"
#include "CmsisRtosThreadUtils.h"
#include "delay_sec.h"
#define EXTEND_SRAM 0xFE

extern void MAIN_INIT();
extern void MAIN_START();

__WEAK void MAIN_INIT()
{
    /**
     * MAIN_INIT() not defined
     */
    asm("nop");
}

__WEAK void MAIN_START()
{
    /**
    * MAIN_START() not defined
    */
    asm("nop");
}


/**
  * @brief  to extend sram size
  * @param  none
  * @retval none
  */
void extend_sram(void)
{
    /* check if ram has been set to expectant size, if not, change eopb0 */
    if (((USD->eopb0) & 0xFF) != EXTEND_SRAM) {
        flash_unlock();
        /* erase user system data bytes */
        flash_user_system_data_erase();

        /* change sram size */
        flash_user_system_data_program((uint32_t) &USD->eopb0, EXTEND_SRAM);
        /* system reset */
        nvic_system_reset();
    }
}

/**
 * Stack Overflow
 */
_Noreturn void stop()
{
    while (1) {
        asm("nop");
    }
}

#define STOP stop();

void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName)
{
    STOP
}


int main(void)
{
    system_clock_config();

    delay_init();

    osKernelInitialize();

    MAIN_INIT();

    MAIN_START();

    osKernelStart();


    STOP
}