diff --git a/Code/Inc/config.h b/Code/Inc/config.h index 10cb23a..a91f9ca 100644 --- a/Code/Inc/config.h +++ b/Code/Inc/config.h @@ -24,9 +24,26 @@ #define INIT_PUPDR_MOTOR_PBA_MSK INIT_MODDER_MOTOR_PBA_MSK #define INIT_PUPDR_MOTOR_PBB_MSK INIT_MODDER_MOTOR_PBB_MSK +extern volatile uint32_t it_count; +extern uint32_t index_time; +extern uint32_t nbr_un; +extern uint32_t nbr_un_final; +extern volatile uint8_t finReception; + void motor_init(void); void init_Timer_RoueDroit(char sens, uint8_t vitesse); void init_Timer_RoueGauche(char sens, uint8_t vitesse); + +void InitGPIOUS(void); +void InitTimerUS(void); +void TIM2_IRQHandler(void); +void UltraSoundMgt(void); +void set_RoueGauche_Sens(int sens, uint8_t vitesse); +uint32_t ComputeDistance(uint32_t nbrUnFinal); + + + + typedef enum { STOP = 0, AVANCE = 1, diff --git a/Code/Src/config.c b/Code/Src/config.c index 810b6bc..35ab694 100644 --- a/Code/Src/config.c +++ b/Code/Src/config.c @@ -7,6 +7,12 @@ #include #include +uint32_t index_time = 0; +uint32_t nbr_un = 0; +uint32_t nbr_un_final = 0; +volatile uint8_t finReception = 0; +volatile uint32_t it_count = 0; + void motor_init(void) { // Init des clks sur portB et A @@ -132,3 +138,122 @@ void init_Timer_RoueGauche(char sens, uint8_t vitesse) TIM1->CR1 |= TIM_CR1_CEN; } + +/* + * INIT US : + * PA1 : TIM2_CH2 : AF01 + * PA5 : TIM2_CH1 / ETR : AF01 + * PA8 : TIM1_CH1 : AF01 + * PA12 : TIM1_ETR AF01 + */ + +void InitGPIOUS(void) { + // 1. Activer l'horloge GPIOA + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; + + // 2. Configuration des MODER + // Nettoyage complet des bits pour PA1, PA5, PA8, PA12 + GPIOA->MODER &= ~(GPIO_MODER_MODER1 | GPIO_MODER_MODER5 | GPIO_MODER_MODER8 | GPIO_MODER_MODER12); + + // PA1 en AF (0b10), PA5 en Output (0b01), PA12 en Output (0b01), PA8 en Input (0b00) + GPIOA->MODER |= (2 << GPIO_MODER_MODER1_Pos) | (1 << GPIO_MODER_MODER5_Pos) | (1 << GPIO_MODER_MODER12_Pos); + GPIOA->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED1_Msk |GPIO_OSPEEDR_OSPEED5_Msk | GPIO_OSPEEDR_OSPEED12_Msk); + GPIOA->OSPEEDR |= (2 << GPIO_OSPEEDR_OSPEED1_Pos | 2 << GPIO_OSPEEDR_OSPEED5_Pos | 2 << GPIO_OSPEEDR_OSPEED12_Pos); + + GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPD8_Msk); + GPIOA->PUPDR |= (2 << GPIO_PUPDR_PUPD8_Pos); + + // 3. Configuration AF pour PA1 (TIM2_CH2 est AF01) + GPIOA->AFR[0] &= ~GPIO_AFRL_AFSEL1; + GPIOA->AFR[0] |= (1 << GPIO_AFRL_AFSEL1_Pos); + +} + +void InitTimerUS(void) { + // 1. Horloge TIM2 + RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; + + // 2. Base de temps : Te = 100 µs @ 180 MHz + TIM2->PSC = 179; // 180 MHz / 180 = 1 MHz + TIM2->ARR = 99; // 1 MHz / 100 = 10 kHz + TIM2->CR1 |= TIM_CR1_ARPE; // preload ARR + + // 3. Canal 2 (PA1) en PWM mode 1 -> validation étape 1 + TIM2->CCMR1 &= ~TIM_CCMR1_CC2S; // CC2 en sortie + TIM2->CCMR1 &= ~TIM_CCMR1_OC2M; + TIM2->CCMR1 |= (6 << TIM_CCMR1_OC2M_Pos); // PWM mode 1 + TIM2->CCMR1 |= TIM_CCMR1_OC2PE; // preload CCR2 + TIM2->CCER |= TIM_CCER_CC2E; // activer sortie OC2 + TIM2->CCR2 = 50; // 50 % de 100 + + // 4. Interruption de débordement + TIM2->DIER |= TIM_DIER_UIE; + NVIC_EnableIRQ(TIM2_IRQn); + + // 6. Démarrage + TIM2->CR1 |= TIM_CR1_CEN; +} + +void UltraSoundMgt(void) { + if (index_time < 500) { // 100*10⁽-6) * 500 = 0.05 soit 50ms (période de l'US) + if (index_time == 0) { + GPIOA->ODR |= GPIO_ODR_ODR_12; // Trig = 1 + nbr_un = 0; + } else if (index_time == 1) { + GPIOA->ODR &= ~GPIO_ODR_ODR_12; // Trig = 0 + } else { + if (GPIOA->IDR & GPIO_IDR_IDR_8) // lecture Echo + nbr_un++; + } + index_time++; + } else { + nbr_un_final = nbr_un; + finReception = 1; + index_time = 0; + } +} + +void TIM2_IRQHandler(void) { + // On vérifie si c'est bien l'interruption de mise à jour (Update) du Timer 2 + if (TIM2->SR & TIM_SR_UIF) { + + // 1. Reset au niveau du périphérique (Timer 2) + TIM2->SR &= ~TIM_SR_UIF; + + // 2. Reset au niveau du processeur (NVIC) + NVIC_ClearPendingIRQ(TIM2_IRQn); + + // Exécution de ta machine d'état + UltraSoundMgt(); + } +} + + +/** + * Pilote le sens et la vitesse du moteur gauche + * @param sens : 'A' pour AVANT (PB4 actif), 'R' pour RECULER (PA10 actif) + * @param vitesse : valeur de 0 à 99 + */ +void set_RoueGauche_Sens(int sens, uint8_t vitesse) +{ + if (sens == AVANCE) // Marche avant + { + TIM3->CCR1 = vitesse; // PWM sur PB4 + TIM1->CCR3 = 0; // PA10 à 0 + } + else if (sens == RECUL) // Marche arrière + { + TIM3->CCR1 = 0; // PB4 à 0 + TIM1->CCR3 = vitesse; // PWM sur PA10 + } + else // Arrêt + { + TIM3->CCR1 = 0; + TIM1->CCR3 = 0; + } +} + +uint32_t ComputeDistance(uint32_t nbrUnFinal) { + return (nbrUnFinal * 100u) / 59u; // ≈ /58.8235 +} + diff --git a/Code/Src/main.c b/Code/Src/main.c index 2df3f24..c9737b8 100644 --- a/Code/Src/main.c +++ b/Code/Src/main.c @@ -21,13 +21,80 @@ #if !defined(__SOFT_FP__) && defined(__ARM_FP) #warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use." #endif - +void SystemClock_Config_180MHz(void); int main(void) { + SystemClock_Config_180MHz(); motor_init(); - - init_Timer_RoueDroit(AVANCE, 0); init_Timer_RoueGauche(AVANCE, 0); + + InitGPIOUS(); + InitTimerUS(); + + //init_Timer_RoueDroit(AVANCE, 0); /* Loop forever */ - for(;;); + while(1) + { + //set_RoueGauche_Sens(AVANCE, 50); + if (finReception == 1) { + uint32_t distance = ComputeDistance(nbr_un_final); + + + if (distance <= 5) { // ignore le plancher de bruit + set_RoueGauche_Sens(RECUL, 100); + } else { + set_RoueGauche_Sens(AVANCE, 0); + } + finReception = 0; + } + + } +} + + + + + +void SystemClock_Config_180MHz(void) { // Fonction faite par Keil (pas besoin de l'écrire) + // 1. Activer l'horloge du périphérique Power (PWR) + RCC->APB1ENR |= RCC_APB1ENR_PWREN; + + // 2. Configurer le régulateur de tension en mode "Scale 1" (obligatoire pour hautes fréq.) + PWR->CR |= PWR_CR_VOS; + + // 3. Activer le mode Over-Drive (Indispensable sur le F446 pour dépasser 168 MHz) + PWR->CR |= PWR_CR_ODEN; + while (!(PWR->CSR & PWR_CSR_ODRDY)); // Attendre que l'Over-Drive soit prêt + PWR->CR |= PWR_CR_ODSWEN; + while (!(PWR->CSR & PWR_CSR_ODSWRDY)); // Attendre le basculement effectif + + // 4. Configurer la mémoire Flash (5 Wait States requis pour 180 MHz à 3.3V) + FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | (5 << FLASH_ACR_LATENCY_Pos); + + // 5. Configurer les diviseurs des bus (Prescalers) + // HCLK = 180 MHz (DIV1), PCLK1 = 45 MHz (DIV4 max), PCLK2 = 90 MHz (DIV2 max) + RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2); + RCC->CFGR |= (RCC_CFGR_PPRE1_DIV4 | RCC_CFGR_PPRE2_DIV2); + + // 6. Configurer la PLL (Basée sur l'horloge interne HSI à 16 MHz) + // Formule : VCO_in = HSI / M = 16 / 8 = 2 MHz + // VCO_out = VCO_in * N = 2 * 180 = 360 MHz + // SYSCLK = VCO_out / P = 360 / 2 = 180 MHz + RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLM | RCC_PLLCFGR_PLLN | RCC_PLLCFGR_PLLP | RCC_PLLCFGR_PLLSRC); + RCC->PLLCFGR |= (8 << RCC_PLLCFGR_PLLM_Pos) | + (180 << RCC_PLLCFGR_PLLN_Pos) | + (0 << RCC_PLLCFGR_PLLP_Pos) | // 0 équivaut à un diviseur P = 2 + RCC_PLLCFGR_PLLSRC_HSI; + + // 7. Activer la PLL + RCC->CR |= RCC_CR_PLLON; + while (!(RCC->CR & RCC_CR_PLLRDY)); // Attendre le verrouillage de la PLL + + // 8. Basculer l'horloge système (SYSCLK) sur la PLL + RCC->CFGR &= ~RCC_CFGR_SW; + RCC->CFGR |= RCC_CFGR_SW_PLL; + while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); + + // 9. ✨ L'ASTUCE STM32F446 ✨ : Booster les horloges des Timers + RCC->DCKCFGR |= RCC_DCKCFGR_TIMPRE; } diff --git a/Datasheet/TP2 - radar US_presentation.pdf b/Datasheet/TP2 - radar US_presentation.pdf new file mode 100644 index 0000000..5210082 Binary files /dev/null and b/Datasheet/TP2 - radar US_presentation.pdf differ diff --git a/Datasheet/TP2 US.pdf b/Datasheet/TP2 US.pdf new file mode 100644 index 0000000..14f7888 Binary files /dev/null and b/Datasheet/TP2 US.pdf differ