/******************************************************************************
 * @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>

typedef struct PHB
{
  uint32_t voltage;
  uint16_t phase;
}PHBs;


 /*************************************************************************************************************************
 *
 * @brief      This demo if gaven 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.
 *
 *             If you want put a variable at the specific place, you can write as:
 *             int variable __attribute__((section(".ARM.__at_0x10010000"))) = 2;
 *             it means put the variable into the place located at absolute address 0x10010000 in flash and it's value equal '2'.
 *            
 *             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.
 *
 *************************************************************************************************************************/


uint32_t UserFunc(uint8_t a, uint8_t b, int vtor, int* c, int d)  __attribute__((section(".ARM.__at_0x10010000")));
PHBs* GetMotorStatus(uint32_t *PHA, PHBs * PHB, int *PHC, uint32_t **Key, uint32_t Count) __attribute__((section(".ARM.__at_0x10010060")));
void testfunc(void)__attribute__((section(".ARM.__at_0x100100c0")));

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

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;
}
