/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.1
 * @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>

#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"



/*************************************************************************************************************************
*
* @brief      This demo if given for how to write a function interface defined by user, which can used by main thread in
*             "Flash_M_Access_User" folder, this code give a example of how to load the right function into specific address.
*
*             __attribute__((section(".ARM.__at_0x10010000"))) means put the function named 'UserFunc' into the code section
*             named '.ARM.__at_0x10010000', located at absolute address 0x10010000 in flash.
*
*             There is a key point, you must keep the space between one function to another function is enough to place the
*             right function content. For example, the function named 'UserFunc' have a size of 0x70bytes, and you still put
*             GetMotorStatus() at 0x10010060, then you will get an error of overlap.
*
*************************************************************************************************************************/

#if defined ( __CC_ARM )
#define         _PRE_Define
#if defined(SPC1158)
#define         _FUNC_Absolute_Addr_            __attribute__((section(".ARM.__at_0x10008000")))
#else
#define         _FUNC_Absolute_Addr_            __attribute__((section(".ARM.__at_0x10010000")))
#endif
#else
#define         _PRE_Define                     __root
#if defined(SPC1158)
#define         _FUNC_Absolute_Addr_            @ (0x10008000)
#else
#define         _FUNC_Absolute_Addr_            @ (0x10010000)
#endif

#endif

uint32_t gu32Number;

_PRE_Define const FUNCLIB ThirdFuncLib _FUNC_Absolute_Addr_ =
{
    UserFunc,
    GetMotorStatus,
    testfunc,
};

uint32_t testfunc(void)
{
    gu32Number = 10;
    return gu32Number;
}
  
uint32_t UserFunc(uint8_t a, uint8_t b, int vtor, int* c, int d)
{
    d = a + b;
    vtor += d;
    c = &d;
    gu32Number++;
    return *c + vtor + gu32Number;
}

PHBs* GetMotorStatus(uint32_t *PHA, PHBs * PHB, int *PHC, uint32_t **Key, uint32_t Count)
{
    PHB->voltage = *PHA + *PHC;
    *Key = &Count;
    PHB->phase = **Key;
    return PHB;
}
