/* -------------------------------------------------------------------------- 
 * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *      Name:    main.c
 *      Purpose: RTX for Kneron
 *
 *---------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "cmsis_os2.h"                    // ARM::CMSIS:RTOS2:Keil RTX5
#include "project.h"
#include "kdrv_system.h"
#include "kmdw_console.h"
#include "kdrv_cmsis_core.h"

#include "kdrv_pwm.h"

extern void delay_ms(unsigned int msec); // FIXME...

#define PWM_Duty_Check_holdtime 1000 //1sec

extern void delay_ms_enable(void);

#include "rtc.h"

struct rtc_time_s mytime;


void sample_pwm_test(pwmtimer pwm_channel)
{
    char buf[256];
    unsigned int period, duty, polarity;

    DSG("[PWM%d Test]",pwm_channel);
    DSG("Please input the period(10ns)");
    kmdw_console_echo_gets(buf, sizeof(buf));
    period = atoi(buf);
    DSG("Please input duty(10ns)");
    kmdw_console_echo_gets(buf, sizeof(buf));
    duty = atoi(buf);
    DSG("Please input polarity(0:low, 1:high)");
    kmdw_console_echo_gets(buf, sizeof(buf));
    polarity = atoi(buf);
    DSG("period = %d, duty = %d, polarity = %d", period, duty, polarity);

    kdrv_pwm_config((pwmtimer)pwm_channel, (pwmpolarity)polarity, duty, period, 0);
    kdrv_pwm_enable((pwmtimer)pwm_channel);
    DSG("setting done!");

}

void sample_pwm_adjust_duty_test(pwmtimer pwm_channel, uint16_t duty)
{
    //char buf[256];
    unsigned int period, polarity;

    DSG("[PWM%d Test]",pwm_channel);
    period = 1000;
    polarity = 0;
    DSG("period = %d, duty = %d, polarity = %d", period, duty, polarity);

    kdrv_pwm_config((pwmtimer)pwm_channel, (pwmpolarity)polarity, duty, period, 0);
    kdrv_pwm_enable((pwmtimer)pwm_channel);
    DSG("setting done!");

}

void myPWMtest(void)
{
    char buf[256];
    uint16_t duty=0;

    //TEST 1
    DSG("************************************");
    sample_pwm_test(PWMTIMER1);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));
    kdrv_pwm_disable(PWMTIMER1);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));

    DSG("************************************");
    sample_pwm_test(PWMTIMER2);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));
    kdrv_pwm_disable(PWMTIMER2);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));

    DSG("************************************");
    sample_pwm_test(PWMTIMER3);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));
    kdrv_pwm_disable(PWMTIMER3);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));

    DSG("************************************");
    sample_pwm_test(PWMTIMER4);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));
    kdrv_pwm_disable(PWMTIMER4);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));

    DSG("************************************");
    sample_pwm_test(PWMTIMER5);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));
    kdrv_pwm_disable(PWMTIMER5);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));

    DSG("************************************");
    sample_pwm_test(PWMTIMER6);
    DSG("Enter to go next step!");
    kmdw_console_echo_gets(buf, sizeof(buf));
    kdrv_pwm_disable(PWMTIMER6);
    DSG("Enter to go to next test!");
    kmdw_console_echo_gets(buf, sizeof(buf));

    //TEST 2
    DSG("************************************");
    DSG("PWM duty increasing test!");
    for(duty=50; duty <= 1000; duty+=50)
    {
        sample_pwm_adjust_duty_test(PWMTIMER6, duty);
        kdrv_pwmtimer_delay_ms(PWM_Duty_Check_holdtime);
    }

    //TEST 3
    DSG("************************************");
    DSG("PWM duty decreasing test!");
    for(duty=1000; duty >= 50; duty-=50)
    {
        sample_pwm_adjust_duty_test(PWMTIMER6, duty);
        kdrv_pwmtimer_delay_ms(PWM_Duty_Check_holdtime);
    }
}


/**
 * @brief main, main dispatch function
 */
int main(void) 
{
    kdrv_system_init();

    kdrv_uart_initialize();
    kdrv_uart_console_init(MSG_PORT, MSG_PORT_BAUDRATE, NULL);   // for log

    SystemCoreClockUpdate();            // System Initialization

    myPWMtest();

    while(1);
}


