/*
 * config.c
 *
 *  Created on: 28 mai 2026
 *      Author: innotex
 */
#include <config.h>
#include <stdint.h>

void motor_init(void)
{
	// Init des clks sur portB et A
	RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN);

	GPIOA->MODER &= ~INIT_MODDER_MOTOR_PBA_MSK;
	GPIOA->MODER |= INIT_MODDER_MOTOR_PBA;
	GPIOB->MODER &= ~INIT_MODDER_MOTOR_PBB_MSK;
	GPIOB->MODER |= INIT_MODDER_MOTOR_PBB;

	GPIOA->OSPEEDR &= ~INIT_OSPEEDR_MOTOR_PBA_MSK;
	GPIOA->OSPEEDR |= INIT_OSPEEDR_MOTOR_PBA;
	GPIOB->OSPEEDR &= ~INIT_OSPEEDR_MOTOR_PBB_MSK;
	GPIOB->OSPEEDR |= INIT_OSPEEDR_MOTOR_PBB;

	GPIOA->PUPDR &= ~INIT_PUPDR_MOTOR_PBA_MSK;
	GPIOA->PUPDR &= ~INIT_PUPDR_MOTOR_PBB_MSK;

	GPIOA->ODR &= ~(GPIO_ODR_ODR_10);
	GPIOB->ODR &= ~(GPIO_ODR_ODR_10 | GPIO_ODR_ODR_5 | GPIO_ODR_ODR_4);
}

void init_Timer_RoueDroit(char sens, uint8_t vitesse)
{
	RCC->APB1ENR |= (RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN);

	GPIOB->MODER &= ~(GPIO_MODER_MODER5 | GPIO_MODER_MODER10);

	GPIOB->MODER |=  (GPIO_MODER_MODER5_1 | GPIO_MODER_MODER10_1); // PB5 et PB10 configuré en alterned function

	// Configuration du TIM3 CH2 sur PB5 sur AF02.
	GPIOB->AFR[0] &= ~GPIO_AFRL_AFSEL5; // Nettoyage des 4 bits
	GPIOB->AFR[0] |= (2 << GPIO_AFRL_AFSEL5_Pos); // Application de AF2

	// PB10 -> AF01 (TIM2). En binaire AF01 s'écrit "0001". On active donc le bit 0.
	GPIOB->AFR[1] &= ~GPIO_AFRH_AFSEL10; // Nettoyage des 4 bits
	GPIOB->AFR[1] |= (1 << GPIO_AFRH_AFSEL10_Pos); // Application de AF1

	TIM2->PSC = 35; //PSC = f_horloge/f_tick - 1
	TIM2->ARR = 99;      // résolution - 1

	TIM3->PSC = 35; //PSC = f_horloge/f_tick - 1
	TIM3->ARR = 99;      // résolution - 1

	TIM2->CR1 &= ~TIM_CR1_CMS; // Edge-aligned mode compteur compte de à -> 99 puis retourne à 0
	TIM2->CR1 &= ~TIM_CR1_DIR; // Counter used as upcounter

	TIM3->CR1 &= ~TIM_CR1_CMS; // Edge-aligned mode compteur compte de à -> 99 puis retourne à 0
	TIM3->CR1 &= ~TIM_CR1_DIR; // Counter used as upcounter
	TIM2->CR1 |= TIM_CR1_ARPE;
	TIM3->CR1 |= TIM_CR1_ARPE;

	TIM2->CCMR2 &= ~TIM_CCMR2_CC3S;

	TIM3->CCMR1 &= ~TIM_CCMR1_CC1S;

	TIM2->CCMR2 &= ~TIM_CCMR2_OC3M;
	TIM2->CCMR2 |= (6 << TIM_CCMR2_OC3M_Pos);

	TIM3->CCMR1 &= ~TIM_CCMR1_OC2M;
	TIM3->CCMR1 |= (6 << TIM_CCMR1_OC2M_Pos);

	// Activation du Preload pour le Canal 2 du Timer 3
	TIM3->CCMR1 |= TIM_CCMR1_OC2PE;

	// Activation du Preload pour le Canal 3 du Timer 2
	TIM2->CCMR2 |= TIM_CCMR2_OC3PE;


	TIM2->CCER  |= TIM_CCER_CC3E;     // Activer la sortie du Canal 3
	TIM2->CCR3   = vitesse;                 // Vitesse initiale à 0
	TIM2->CR1   |= TIM_CR1_CEN;       // Démarrer le Timer 2

	TIM3->CCER  |= TIM_CCER_CC2E;     // Activer la sortie du Canal 2 (Capture/Compare 2 Enable)
	TIM3->CCR2   = 0;                 // Vitesse initiale à 0
	TIM3->CR1   |= TIM_CR1_CEN;       // Démarrer le Timer 3 (Counter ENable)

}

void init_Timer_RoueGauche(char sens, uint8_t vitesse)
{
    // Activation des horloges
    RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN);
    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

    // Configuration des MODER
    GPIOB->MODER &= ~GPIO_MODER_MODER4;
    GPIOB->MODER |=  GPIO_MODER_MODER4_1; // PB4 en AF
    GPIOA->MODER &= ~GPIO_MODER_MODER10;
    GPIOA->MODER |=  GPIO_MODER_MODER10_1; // PA10 en AF

    // Configuration des AFR
    // PB4 -> TIM3 CH1 (AF02)
    GPIOB->AFR[0] &= ~GPIO_AFRL_AFSEL4;
    GPIOB->AFR[0] |= (2 << GPIO_AFRL_AFSEL4_Pos);

    // PA10 -> TIM1 CH3 (AF01)
    GPIOA->AFR[1] &= ~GPIO_AFRH_AFSEL10;
    GPIOA->AFR[1] |= (1 << GPIO_AFRH_AFSEL10_Pos);

    // Paramétrage Timers
    TIM3->PSC = 35; TIM3->ARR = 99; TIM3->CR1 |= TIM_CR1_ARPE;
    TIM1->PSC = 35; TIM1->ARR = 99; TIM1->CR1 |= TIM_CR1_ARPE;

    // --- TIM3 CH1 (PB4) ---
    TIM3->CCMR1 &= ~TIM_CCMR1_OC1M;
    TIM3->CCMR1 |= (6 << TIM_CCMR1_OC1M_Pos) | TIM_CCMR1_OC1PE;
    TIM3->CCER  |= TIM_CCER_CC1E; // Activer le Canal 1

    // --- TIM1 CH3 (PA10) ---
    TIM1->CCMR2 &= ~TIM_CCMR2_OC3M;
    TIM1->CCMR2 |= (6 << TIM_CCMR2_OC3M_Pos) | TIM_CCMR2_OC3PE;
    TIM1->CCER  |= TIM_CCER_CC3E; // Activer le Canal 3
    TIM1->BDTR  |= TIM_BDTR_MOE;  // OBLIGATOIRE POUR TIM1

    // Initialisation vitesses
    TIM3->CCR1 = 0; // Utilise CCR1 pour canal 1
    TIM1->CCR3 = vitesse; // Utilise CCR3 pour canal 3

    // Démarrage
    TIM3->CR1 |= TIM_CR1_CEN;
    TIM1->CR1 |= TIM_CR1_CEN;
}

