/*
 * Copyright 2022 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "board_init.h"

/*
 * Definitions.
 */

/*
 * Variables.
 */
int app_cordic_adja = 1000, app_cordic_oppo = 10, app_cordic_atan = 0;

/*
 * Declerations.
 */
void app_cordic_init(void);

/*
 * Functions.
 */
int main(void)
{
    BOARD_Init();
    printf("cordic_atan example.\r\n");
    printf("press any key to start calculate...\r\n");
    app_cordic_init();

    while (1)
    {
        /* Calculate atan. */
        getchar();
        /* Set the adjacent side length value. */
        printf("Adjacent: %d, ", app_cordic_adja);
        CORDIC_SetData(BOARD_CORDIC_PORT, CORDIC_Data_AdjaSide, app_cordic_adja);
        app_cordic_adja += 1000;
        if (app_cordic_adja > 0xFFFFFF)
        {
            app_cordic_adja = 0;
        }

        /* Set the opposite side length value. */
        printf("Opposite: %d, ", app_cordic_oppo);
        CORDIC_SetData(BOARD_CORDIC_PORT, CORDIC_Data_OppoSide, app_cordic_oppo);
        app_cordic_oppo += app_cordic_adja/100;
        if (app_cordic_oppo > 0xFFFFFF)
        {
            app_cordic_oppo = 0;
        }

        /* Start calculate. */
        CORDIC_Start(BOARD_CORDIC_PORT);

        /* Wait for ready. */
        while (CORDIC_STATUS_BUSY == CORDIC_GetStatus(BOARD_CORDIC_PORT))
        {
        }

        /* Get the calculate result. */
        app_cordic_atan = CORDIC_GetResult(BOARD_CORDIC_PORT, CORDIC_Result_Atan);
        printf("Atan: %d\r\n", app_cordic_atan);
    }
}

void app_cordic_init(void)
{
    CORDIC_Init_Type cordic_init;

    cordic_init.DataFormat = CORDIC_DataFormat_None;
    cordic_init.CalcMode = CORDIC_CalcMode_Vectoring;
    cordic_init.StartMode = CORDIC_StartMode_WaitForStart;
    CORDIC_Init(BOARD_CORDIC_PORT, &cordic_init);
    CORDIC_Enable(BOARD_CORDIC_PORT, true);
}

/* EOF. */
