/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. 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"spc1168.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/*Used for test*/
typedef struct PHB{
  uint32_t voltage;
  uint16_t phase;
}PHBs;

#define                   USERFUNCADDR               0x10010001            /*The loading address of Spintrol function in "Flash_User_FuncWrap"*/
#define                   GETMOTORSTATUS             0x10010061            /*The loading address of GetMotorStatus function in "Flash_User_FuncWrap"*/
#define                   JUMPFUNCADDR               0x100100c1            /*The loading address of testfunc function in "Flash_User_FuncWrap"*/

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

/*Jump to the code dosen't belong to the same partition of code being excuted*/
uint32_t (* JumpUserFunc) (uint8_t a, uint8_t b, int vtor, int* c, int d) \
                  = (uint32_t(*)(uint8_t a, uint8_t b, int vtor, int* c, int d)) USERFUNCADDR;

PHBs* (* JumoMotorStatus) (uint32_t *PHA, PHBs * PHB, int *PHC, uint32_t **Key, uint32_t Count) \
                  = (PHBs* (*) (uint32_t *PHA, PHBs * PHB, int *PHC, uint32_t **Key, uint32_t Count)) GETMOTORSTATUS;

void (* JUMPFUNC)() = (void (*)())JUMPFUNCADDR;


 /*************************************************************************************************************************
 *
 * @brief      In this demo, A example how to access the function in the different flash partition of the main processing
 *             thread is gaven. 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 excute 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();
  FLASH_SetTiming(200000000);

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  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");

  JUMPFUNC();

  argv = &val;
  SPINTestFlag = JumpUserFunc(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 = JumoMotorStatus(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)
  {

  }
}
