/* --------------------------------------------------------------------------
 * 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 "kdrv_system.h"
//#include "kdrv_pinmux.h"
#include "kdrv_timer.h"
#include "kdrv_scu_ext.h"

#define PINMUX_SD_DATA2_GPIO26          3
#define PINMUX_SD_DATA3_GPIO27          3

uint32_t btest[10];
uint32_t Ftest[5];
uint32_t timerid;
osThreadId_t nonblocktid;
void test(cb_event_t argu, void* argu2)
{
    Ftest[2] = argu;
    Ftest[3] = *(uint32_t*)argu2;
    Ftest[4]++;
}
void test1(cb_event_t argu, void* argu2)
{
}
void test2(cb_event_t argu, void* argu2)
{
}
void test3(cb_event_t argu, void* argu2)
{
}
void test4(cb_event_t argu, void* argu2)
{
}
void test5(cb_event_t argu, void* argu2)
{
}
void myTIMERtest5(void *argument)
{
    uint32_t timerid;
    while(1){
    kdrv_timer_open(&timerid, NULL, NULL);
    kdrv_timer_set(&timerid, 3190000, TIMER_START);
    if( btest[0] < 0xFFFFFFF0)
        btest[5]++;
    }
}
void myTIMERtest4(void *argument)
{
    uint32_t timerid;
    while(1){
    kdrv_timer_open(&timerid, NULL, NULL);
    kdrv_timer_set(&timerid, 5000000, TIMER_START);
    if( btest[0] < 0xFFFFFFF0)
        btest[4]++;
    }
}
void myTIMERtest3(void *argument)
{
    uint32_t timerid;
    while(1){
    kdrv_timer_open(&timerid, NULL, NULL);
    kdrv_timer_set(&timerid, 3000000, TIMER_START);
    if( btest[0] < 0xFFFFFFF0)
        btest[3]++;
    }
}
void myTIMERtest2(void *argument)
{
    uint32_t timerid;
    while(1){
    kdrv_timer_open(&timerid, NULL, NULL);
    kdrv_timer_set(&timerid, 1000000, TIMER_START);
    if( btest[0] < 0xFFFFFFF0)
        btest[2]++;
    }
}
extern uint32_t perftimerid;
void myTIMERtest1(void *argument)
{
    uint32_t Cnt;
    masked_outw(GPIO_FTGPIO010_PA_BASE + 0x8/*REG_GPIO_PINOUT_OFFSET*/, (1 << 26), (1 << 26));
    masked_outw(GPIO_FTGPIO010_PA_BASE + 0x0/*REG_GPIO_DOUT_OFFSET*/, (1 << 26), (1 << 26));
    while(1)
    {
        kdrv_timer_delay_ms(10);
        if( btest[0] < 0xFFFFFFF0)
            btest[1]++;
        if(Cnt++%2)
            masked_outw(GPIO_FTGPIO010_PA_BASE + 0x0/*REG_GPIO_DOUT_OFFSET*/, (1 << 26), (1 << 26));
        else
            masked_outw(GPIO_FTGPIO010_PA_BASE + 0x0/*REG_GPIO_DOUT_OFFSET*/, 0, (1 << 26));
    }
}
void myTIMERtest(void *argument)
{
    uint32_t Cnt;
    uint32_t perftime, perftime2;
    masked_outw(GPIO_FTGPIO010_PA_BASE + 0x8/*REG_GPIO_PINOUT_OFFSET*/, (1 << 27), (1 << 27));
    masked_outw(GPIO_FTGPIO010_PA_BASE + 0x0/*REG_GPIO_DOUT_OFFSET*/, (1 << 27), (1 << 27));
    while(1)
    {
        kdrv_timer_delay_us(1000);
        kdrv_timer_perf_get_instant(&perftimerid, &perftime, &perftime2);
        Ftest[0] = perftime;
        Ftest[1] = perftime2;
        if( btest[0] < 0xFFFFFFF0)
            btest[0]++;
        if(Cnt++%2)
            masked_outw(GPIO_FTGPIO010_PA_BASE + 0x0/*REG_GPIO_DOUT_OFFSET*/, (1 << 27), (1 << 27));
        else
            masked_outw(GPIO_FTGPIO010_PA_BASE + 0x0/*REG_GPIO_DOUT_OFFSET*/, 0, (1 << 27));
    }
}

int main(void)
{
    kdrv_system_init();
    SystemCoreClockUpdate();            // System Initialization

    kdrv_timer_initialize();
    SET_MASKED_BITS(SCU_EXTREG_SD_DATA2_IOCTRL, PINMUX_SD_DATA2_GPIO26, 0, 2)
    SET_MASKED_BITS(SCU_EXTREG_SD_DATA3_IOCTRL, PINMUX_SD_DATA3_GPIO27, 0, 2)
    osKernelInitialize();               // Initialize CMSIS-RTOS
    kdrv_timer_perf_measure_start();


    osThreadNew(myTIMERtest, NULL, NULL);
    osThreadNew(myTIMERtest1, NULL, NULL);
    //osThreadNew(myTIMERtest2, NULL, NULL);
    //osThreadNew(myTIMERtest3, NULL, NULL);
    //osThreadNew(myTIMERtest4, NULL, NULL);

    osKernelStart();

    while(1) {
    }
}


