/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif
#include "func.h"


uint32_t    SPINTestFlag, *argv_1;
PHBs        * MotorStatus, * argvPHB;
int         * argv;
int         val = 10;


/*************************************************************************************************************************
*
* @brief      In this demo, A example how to access the function in the different flash partition of the main processing
*             thread is given. This project is the demo code of main processing thread, the user function interface demo
*             can be found in folder named "Flash_User_FuncWrap". You can load the code in "Flash_User_FuncWrap" first into
*             flash, then load this project code, if every thing is done, this main thread use the function pointer to find
*             the user function interface address and execute it.
*
*                                      ------------------
*                                     |                  |
*                                     |                  |
*                                     |------------------|
*                                     |  User function   |<------
*                                     |     Interface    |       |
*                                     |------------------|       |
*                                     |                  |       |
*                                     |                  |       |
*                                     |                  |       |
*                                     |                  |       |
*                                     |                  |       |
*                                     |                  |       |
*                                     |                  |       |
*                                     |------------------|       |
*                                     |    Main thread   |-------    At a moment, the main thread call the function in the
*                                     |                  |           space dose not belong to the main thread.
*                                      ------------------
*
*************************************************************************************************************************/

int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);

    CLOCK_InitWithRCO(200000000UL);
#endif

    Delay_Init();
    /*
     * Init the UART
     *
     * 1.Set the GPIO34/35 as UART FUNC
     *
     * 2.Enable the UART CLK
     *
     * 3.Set the rest para
     */
    GPIO_SetPinChannel(GPIO_34, GPIO34_UART_TXD);
    GPIO_SetPinChannel(GPIO_35, GPIO35_UART_RXD);
    CLOCK_EnableModule(UART_MODULE);
    UART_Init(UART, 38400);

    printf("start test...\n");

    printf("Init global value %d...\n", ThirdFuncLib->ptestfunc());

    argv = &val;
    SPINTestFlag = ThirdFuncLib->pUserFunc(1, 2, 3, argv, 4);
    printf("Spintrol test result : %d \n", SPINTestFlag);

    argv = &val;
    SPINTestFlag = ThirdFuncLib->pUserFunc(1, 2, 3, argv, 4);
    printf("Spintrol test result : %d \n", SPINTestFlag);

    argv_1 = (uint32_t *)&val;
    argvPHB = malloc(sizeof(PHBs));
    memset(argvPHB, 0x0, sizeof(PHBs));
    MotorStatus = ThirdFuncLib->pGetMotorStatus(argv_1, argvPHB, argv, &argv_1, 3);
    printf("Motor test result : %d , %d\n", MotorStatus->phase, MotorStatus->voltage);

    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    while (1)
    {

    }
}
