/*
 * outputs.c
 *
 *  Created on: Mar 16, 2022
 *      Author: v0stap
 */

#include "main.h"

void TIM16_Init(void) {
	RCC->APB2ENR |= RCC_APB2ENR_TIM16EN;
	TIM16->PSC = 480;
	TIM16->ARR = 70287;
	TIM16->CR1 |= 1 << 0 | 1 << 7;
	TIM16->CR2 = 0;
	TIM16->SMCR = 0;
	TIM16->DIER = 1 << 0;
	TIM16->CCMR1 = 0;
	TIM16->CCMR2 = 0;
	TIM16->CCER = 0x1111;
	TIM16->SR = 0;
	NVIC_EnableIRQ(TIM16_IRQn);
}

void TIM17_Init(void) {
	RCC->APB2ENR |= RCC_APB2ENR_TIM17EN;
	TIM17->PSC = 479;
	TIM17->ARR = 10000;
	TIM17->CR1 |= 1 << 0 | 1 << 7;
	TIM17->CR2 = 0;
	TIM17->SMCR = 0;
	TIM17->DIER = 1 << 0;
	TIM17->CCMR1 = 0;
	TIM17->CCMR2 = 0;
	TIM17->CCER = 0x1111;
	TIM17->SR = 0;
	NVIC_EnableIRQ(TIM17_IRQn);
}

void VSS_Set(uint8_t vss) {
	if (vss < 3) {
		TIM16->CR1 &= ~(1 << 0);
	} else {
		TIM16->CR1 |= 1 << 0;
		TIM16->ARR = 70287 / vss;
	}
}


void TIM16_IRQHandler(void) {
	TIM16->SR &= ~(1 << 0);
	//TIM2->CNT = 600;
	HAL_GPIO_TogglePin(GPIOB, GPO1_Pin);
}
void TIM17_IRQHandler(void) {
	TIM17->SR &= ~(1 << 0);
	//TIM2->CNT = 600;
	MAZDA_Send_Data();
}

