/**
  ******************************************************************************
  * @file    USART/Printf/main.c
  * @author  IOT Development Team
  * @version V1.0.0
  * @date    10-January-2020
  * @brief   Main program body
  ******************************************************************************

  */

/* Includes ------------------------------------------------------------------*/
#include "apm32f10x.h"
#include <stdio.h>
#include <stdarg.h>
#include "platform_config.h"
#include "sc_math.h"
/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup USART_Printf
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_ConfigStruct_T USART_configStruct;
float a,b,s,x,y,z,ans[100],X[100]= {0,0,0,};
float PI=3.14159;
u8 databuff;
u32 m=0;


/* Private function prototypes -----------------------------------------------*/

#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/* Private functions ---------------------------------------------------------*/

void Uartx_SendByte(USART_T* USARTx,int data)
{
    if (data=='\n')
    {
        while (!(USARTx->STS & (1<<6)));

        USARTx->DATA='\r';
    }

    while (!(USARTx->STS & (0x01<<6)));

    USARTx->DATA=data;
}

void Uartx_SendString(USART_T* USARTx,char *pt)
{
    while (*pt)
    {
        Uartx_SendByte(USARTx,*pt++);
    }
}


void Uartx_Printf(USART_T* USARTx,char *fmt,...)
{
    __va_list ap;
    char string[256];

    va_start(ap,fmt);
    vsprintf(string,fmt,ap);
    Uartx_SendString(USARTx,string);
    va_end(ap);
}

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_apm32f10x_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_apm32f10x.c file
       */
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_FPU);
    RCM->CFG |= 0x08000000;
    /* USARTx configured as follow:
          - BaudRate = 115200 baud
          - Word Length = 8 Bits
          - One Stop Bit
          - No parity
          - Hardware flow control disabled (RTS and CTS signals)
          - Receive and transmit enabled
    */
    USART_configStruct.baudRate = 115200;
    USART_configStruct.wordLength = USART_WORD_LEN_8B;
    USART_configStruct.stopBits = USART_STOP_BIT_1;
    USART_configStruct.parity = USART_PARITY_NONE;
    USART_configStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
    USART_configStruct.mode = USART_MODE_TX_RX;



#if defined (APM32_MINI)
    APM_MINI_COMInit(COM1, &USART_configStruct);
#elif defined(APM32_DISCOVERY)
    APM_DISCOVERY_COMInit(COM1, &USART_InitStructure);
#elif defined (APM32F103V_EVAL)
    APM_EVAL_COMInit(COM1, &USART_InitStructure);
#endif

    databuff=0;

    for (s=0; s<100; s++)
    {
        X[databuff]=s;
        databuff++;
    }

    databuff=0;
    s=3;

#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"SIN:");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"SIN:");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"SIN:");
#endif

    for (s=0; s<100; s++)
    {
        ans[databuff]=0x00;
        databuff++;
    }



    databuff=0;

    for (s=-PI; s<0; s=s+0.1)
    {
        ans[databuff]= sc_math_sin(s);
        databuff++;
    }

    ans[databuff]= sc_math_sin(0);
    databuff++;

    for (s=PI; s>0; s=s-0.1)
    {
        ans[databuff]= sc_math_sin(s);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }

#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"COS:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"COS:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"COS:\n");
#endif

    for (s=0; s<100; s++)
    {
        ans[databuff]=0x00;
        databuff++;
    }


    databuff=0;

    for (s=-PI; s<0; s=s+0.1)
    {
        ans[databuff]= sc_math_cos(s);
        databuff++;
    }

    ans[databuff]= sc_math_cos(0);
    databuff++;

    for (s=PI; s>0; s=s-0.1)
    {
        ans[databuff]= sc_math_cos(s);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }

#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"TAN:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"TAN:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"TAN:\n");
#endif

    for (s=0; s<100; s++)
    {
        ans[databuff]=0x00;
        databuff++;
    }

    databuff=0;

    for (s=-PI; s<0; s=s+0.1)
    {
        ans[databuff]= sc_math_tan(s);
        databuff++;
    }

    ans[databuff]= sc_math_tan(0);
    databuff++;

    for (s=PI; s>0; s=s-0.1)
    {
        ans[databuff]= sc_math_tan(s);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }


#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"ATAN:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"ATAN:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"ATAN:\n");
#endif

    for (s=0; s<100; s++)
    {
        ans[databuff]=0x00;
        databuff++;
    }

    databuff=0;

    for (s=-PI; s<0; s=s+0.1)
    {
        ans[databuff]= sc_math_atan(s);
        databuff++;
    }

    ans[databuff]= sc_math_atan(0);
    databuff++;

    for (s=PI; s>0; s=s-0.1)
    {
        ans[databuff]= sc_math_atan(s);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }


#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"ASIN:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"ASIN:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"ASIN:\n");
#endif

    for (s=0; s<100; s++)
    {
        ans[databuff]=0x00;
        databuff++;
    }

    databuff=0;

    for (s=-1; s<=1; s=s+0.1)
    {
        ans[databuff]= sc_math_asin(s);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }

#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"ACOS:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"ACOS:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"ACOS:\n");
#endif

    for (s=0; s<100; s++)
    {
        ans[databuff]=0x00;
        databuff++;
    }

    databuff=0;

    for (s=-1; s<=1; s=s+0.1)
    {
        ans[databuff]= sc_math_acos(s);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }



#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"ATAN2:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"ATAN2:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"ATAN2:\n");
#endif

    for (s=0; s<100; s++)
    {
        ans[databuff]=0x00;
        databuff++;
    }

    databuff=0;


    for (s=-PI; s<0; s=s+0.1)
    {
        a=s;
        b=s+0.1;
        ans[databuff]= sc_math_atan2(b,a);
        databuff++;
    }

    a=s;
    b=s+0.1;
    ans[databuff]= sc_math_atan2(0,0);
    databuff++;


    for (s=PI; s>0; s=s-0.1)
    {
        a=s;
        b=s-0.1;
        ans[databuff]= sc_math_atan2(b,a);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }


#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"invsqrt:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"invsqrt:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"invsqrt:\n");
#endif
    databuff=0;

    for (s=0; s<100; s++)
    {
        ans[databuff]= sc_math_invsqrt(databuff);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }

#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"mac:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"mac:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"mac:\n");
#endif

    databuff=0;

    for (s=0; s<100; s++)
    {
        x=s;
        y=s+1;
        z=s+2;
        ans[databuff]=sc_math_mac(x,y,z);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%f\n",ans[databuff]);
#endif
        databuff++;
    }



#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"sum_N:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"sum_N:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"sum_N:\n");
#endif
    databuff=0;

    for (s=0; s<100; s++)
    {
        X[databuff]=s;
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
        ans[databuff]=sc_math_sum_N(X,databuff);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }






#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"sub_N:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"sub_N:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"sub_N:\n");
#endif

    databuff=0;

    for (s=0; s<100; s++)
    {
        X[databuff]=100-s;
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
        ans[databuff]= sc_math_sub_N(X,databuff);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }


#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"prdct:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"prdct:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"prdct:\n");
#endif

    databuff=0;

    for (s=0; s<100; s++)
    {
        X[databuff]=s+1;
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
        ans[databuff]= sc_math_prdct(X,databuff);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }

#if defined (APM32_MINI)
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"\n");
    Uartx_Printf(MINI_COM1,"sumsq:\n");
#elif defined(APM32_DISCOVERY)
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"\n");
    Uartx_Printf(DISCOVERY_COM1,"sumsq:\n");
#elif defined (APM32F103V_EVAL)
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"\n");
    Uartx_Printf(EVAL_COM1,"sumsq:\n");
#endif
    databuff=0;

    for (s=0; s<100; s++)
    {
        X[databuff]=100-s;
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
        ans[databuff]= sc_math_sumsq(X,databuff);
        databuff++;
    }

    databuff=0;

    for (s=0; s<100; s++)
    {
#if defined (APM32_MINI)
        Uartx_Printf(MINI_COM1,"%5.11f\n",ans[databuff]);
#elif defined(APM32_DISCOVERY)
        Uartx_Printf(DISCOVERY_COM1,"%5.11f\n",ans[databuff]);
#elif defined (APM32F103V_EVAL)
        Uartx_Printf(EVAL_COM1,"%5.11f\n",ans[databuff]);
#endif
        databuff++;
    }

    while (1)
    {
    }
}

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART */
#if defined (APM32_MINI)
    USART_TxData(MINI_COM1, (uint8_t) ch);

    /* Loop until the end of transmission */
    while (USART_ReadFlag(MINI_COM1, USART_FLAG_TXC) == RESET)
    {}

#elif defined(APM32_DISCOVERY)
    USART_SendData(DISCOVERY_COM1, (uint8_t) ch);

    /* Loop until the end of transmission */
    while (USART_GetFlagStatus(DISCOVERY_COM1, USART_FLAG_TC) == RESET)
    {}

#endif
    return ch;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}

#endif

/**
  * @}
  */

/**
  * @}
  */

