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

#include "board_init.h"

/*
 * Definitions.
 */

/*
 * Variables.
 */
int app_cordic_angle = 0, app_cordic_sin = 0, app_cordic_cos = 0;

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

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

    while (1)
    {
        /* Calculate atan. */
        getchar();

        /* Set the angle value. */
        printf("Angle: %d, ", app_cordic_angle);
        CORDIC_SetData(BOARD_CORDIC_PORT, CORDIC_Data_Angle, app_cordic_angle);
        app_cordic_angle += 1000;
        if (app_cordic_angle > 0xFFFFFF)
        {
            app_cordic_angle = 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_sin = CORDIC_GetResult(BOARD_CORDIC_PORT, CORDIC_Result_Sin);
        app_cordic_cos = CORDIC_GetResult(BOARD_CORDIC_PORT, CORDIC_Result_Cos);
        printf("Sin: %d, Cos: %d\r\n", app_cordic_sin, app_cordic_cos);
    }
}

void app_cordic_init(void)
{
    CORDIC_Init_Type cordic_init;

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

/* EOF. */
