/*
 * tim.c
 *
 *  Created on: 2024年6月22日
 *      Author: psx
 */

#include "tim.h"
#include "timeSliceOffset.h"
#include "pwm.h"
#include "mppt_control.h"
#include "task.h"

void TIM3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));


void TIM3_Init(uint16_t delay_ms)
{
    /* 分频系数 */
    uint16_t psc = (SystemCoreClock / 10000) - 1;
    /* 周期数 */
    uint16_t arr = delay_ms * 10 - 1;

    TIM3_Int_Init(arr, psc);
}

void TIM3_Int_Init(uint16_t arr, uint16_t psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_PB1PeriphClockCmd(RCC_PB1Periph_TIM3, ENABLE); //使能TIM3时钟

    TIM_TimeBaseStructure.TIM_Period = arr;   //指定下次更新事件时要加载到活动自动重新加载寄存器中的周期值。
    TIM_TimeBaseStructure.TIM_Prescaler = psc; //指定用于划分TIM时钟的预分频器值。
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //时钟分频因子
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM计数模式，向上计数模式
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位

    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE ); //使能TIM3中断，允许更新中断

    //初始化TIM NVIC，设置中断优先级分组
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;           //TIM3中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置抢占优先级0
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;        //设置响应优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           //使能通道1中断
    NVIC_Init(&NVIC_InitStructure); //初始化NVIC

    TIM_Cmd(TIM3, ENABLE); //TIM3使能
}

void TIM3_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { //检查TIM3中断是否发生。
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);    //清除TIM3的中断挂起位。
        test();
    }
}

void TIM2_Init(uint16_t delay_ms)
{
    /* 分频系数 */
    uint16_t psc = (SystemCoreClock / 10000) - 1;
    /* 周期值 */
    uint16_t arr = delay_ms * 10 - 1;

    TIM2_Int_Init(arr, psc);
}

void TIM2_Int_Init(uint16_t arr, uint16_t psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_PB1PeriphClockCmd(RCC_PB1Periph_TIM2, ENABLE); //使能TIM2时钟

    TIM_TimeBaseStructure.TIM_Period = arr;   //指定下次更新事件时要加载到活动自动重新加载寄存器中的周期值。
    TIM_TimeBaseStructure.TIM_Prescaler = psc; //指定用于划分TIM时钟的预分频器值。
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //时钟分频因子
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM计数模式，向上计数模式
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2中断，允许更新中断

    //初始化TIM NVIC，设置中断优先级分组
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;           //TIM2中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置抢占优先级0
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;        //设置响应优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           //使能通道1中断
    NVIC_Init(&NVIC_InitStructure); //初始化NVIC

    TIM_Cmd(TIM2, ENABLE); //TIM2使能
}

void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {    //检查TIM2中断是否发生。
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);         //清除TIM1的中断挂起位。
        TimeSliceOffset_Produce();
        if (outputAgainFlag == 1) {
            outputAgainFlag = 0;
        }
    }
}

//SysTick初始化配置          1MS进入一次中断
static void SysTick_Config(uint32_t ticks)
{
    SysTick->SR = 0;
    SysTick->CNT = 0;
    SysTick->CMP = ticks;
    SysTick->CTLR =0xF;

    NVIC_SetPriority(SysTicK_IRQn, 15);
    NVIC_EnableIRQ(SysTicK_IRQn);
}

void Systick_Init(void)
{
    SysTick_Config((SystemCoreClock / 1000) - 1);
}

void SysTick_Handler(void)
{
    SysTick->SR = 0;
    TimeSliceOffset_Produce();
}

void TIM1_Init(uint16_t delay_ms)
{
    /* 分频系数 */
    uint16_t psc = (SystemCoreClock / 10000) - 1;
    /*  */
    uint16_t arr = delay_ms * 10 - 1;
//    uint16_t arr = 10 - 1;

    TIM1_Int_Init(arr, psc);
}

void TIM1_Int_Init(uint16_t arr, uint16_t psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_PB2PeriphClockCmd(RCC_PB2Periph_TIM1, ENABLE); //使能TIM1时钟

    TIM_TimeBaseStructure.TIM_Period = arr;   //指定下次更新事件时要加载到活动自动重新加载寄存器中的周期值。
    TIM_TimeBaseStructure.TIM_Prescaler = psc; //指定用于划分TIM时钟的预分频器值。
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //时钟分频因子
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM计数模式，向上计数模式
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //使能TIM1中断，允许更新中断

    //初始化TIM NVIC，设置中断优先级分组
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;           //TIM1中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   //设置抢占优先级0
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;          //设置响应优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;             //使能通道中断
    NVIC_Init(&NVIC_InitStructure); //初始化NVIC

    TIM_Cmd(TIM1, ENABLE); //TIM1使能
}

void TIM1_UP_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) {    //检查TIM1中断是否发生。
        TIM_ClearITPendingBit(TIM1, TIM_IT_Update);         //清除TIM1的中断挂起位。
        printf("in tim1 irq \n");
    }
}

