Files
voiture_Keyestudio_4WS_STM3…/Code/Src/main.c

101 lines
3.5 KiB
C

/**
******************************************************************************
* @file : main.c
* @author : Auto-generated by STM32CubeIDE
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include <stdint.h>
#include <config.h>
#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_RoueGauche(AVANCE, 0);
InitGPIOUS();
InitTimerUS();
//init_Timer_RoueDroit(AVANCE, 0);
/* Loop forever */
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;
}