From 277a18fd56676cbb78f4b03a79c9299965b49dd6 Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 18 Nov 2025 10:45:06 +0100 Subject: [PATCH] Pilotes avec couche service girouette --- Pilotes/Include/DriverGPIO.h | 14 +++++ Pilotes/Include/Girouette.h | 4 ++ Pilotes/Include/MyTimer.h | 12 +++++ Pilotes/Include/Nucleo.h | 8 +++ Pilotes/Include/PWM.h | 4 ++ Pilotes/Include/Timer.h | 12 +++++ Pilotes/Include/test.h | Bin 0 -> 1024 bytes Pilotes/Source/ADC.c | Bin 0 -> 1024 bytes Pilotes/Source/DriverGPIO.c | 71 ++++++++++++++++++++++++ Pilotes/Source/Girouette.c | 30 +++++++++++ Pilotes/Source/MyTimer.c | 39 ++++++++++++++ Pilotes/Source/Nucleo.c | 25 +++++++++ Pilotes/Source/PWM.c | 67 +++++++++++++++++++++++ Pilotes/Source/Timer.c | 102 +++++++++++++++++++++++++++++++++++ 14 files changed, 388 insertions(+) create mode 100644 Pilotes/Include/DriverGPIO.h create mode 100644 Pilotes/Include/Girouette.h create mode 100644 Pilotes/Include/MyTimer.h create mode 100644 Pilotes/Include/Nucleo.h create mode 100644 Pilotes/Include/PWM.h create mode 100644 Pilotes/Include/Timer.h create mode 100644 Pilotes/Include/test.h create mode 100644 Pilotes/Source/ADC.c create mode 100644 Pilotes/Source/DriverGPIO.c create mode 100644 Pilotes/Source/Girouette.c create mode 100644 Pilotes/Source/MyTimer.c create mode 100644 Pilotes/Source/Nucleo.c create mode 100644 Pilotes/Source/PWM.c create mode 100644 Pilotes/Source/Timer.c diff --git a/Pilotes/Include/DriverGPIO.h b/Pilotes/Include/DriverGPIO.h new file mode 100644 index 0000000..3748ebf --- /dev/null +++ b/Pilotes/Include/DriverGPIO.h @@ -0,0 +1,14 @@ +#include "stm32f10x.h" +#define In_Floating 0x4 +#define In_PullDown 0x8 +#define In_PullUp 0x8 +#define In_Analog 0x0 +#define Out_Ppull 0x3 +#define Out_OD 0x7 +#define AltOut_Ppull 0xB +#define AltOut_OD 0xF +extern void MyGPIO_Init(GPIO_TypeDef * GPIO, char pin, char conf ); +extern int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin); // renvoie 0 ou autre chose different de 0 +extern void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin); +extern void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin); +extern void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin); \ No newline at end of file diff --git a/Pilotes/Include/Girouette.h b/Pilotes/Include/Girouette.h new file mode 100644 index 0000000..c5f8961 --- /dev/null +++ b/Pilotes/Include/Girouette.h @@ -0,0 +1,4 @@ +#include "stm32f10x.h" +extern void configEncoder(TIM_TypeDef * Timer); +extern void configChannel(); +extern int returnAngle (TIM_TypeDef * Timer); \ No newline at end of file diff --git a/Pilotes/Include/MyTimer.h b/Pilotes/Include/MyTimer.h new file mode 100644 index 0000000..012aa0d --- /dev/null +++ b/Pilotes/Include/MyTimer.h @@ -0,0 +1,12 @@ +#include "stm32f10x.h" + +//TIMERS start +#define MyTimer_Base_Start(Timer) (Timer->CR1 |= TIM_CR1_CEN) +#define MyTimer_Base_Stop(Timer) (Timer -> CR1 =(0x0)) +// IT +extern volatile int g_tick_count; // Declara que a variável existe em outro arquivo +void Test(void); +void ConfigureIT(); +// PWM +void ConfigurePWM(); +void ConfigureTimers(); \ No newline at end of file diff --git a/Pilotes/Include/Nucleo.h b/Pilotes/Include/Nucleo.h new file mode 100644 index 0000000..49fb743 --- /dev/null +++ b/Pilotes/Include/Nucleo.h @@ -0,0 +1,8 @@ +#include "stm32f10x.h" +// Config +extern void ConfigHorloge(void); +extern void ConfigBroche(void); +// Gestion des IO Spesifiques +extern int BoutonAppuye(void); +extern void AllumerLED(void); +extern void EteindreLED(void); diff --git a/Pilotes/Include/PWM.h b/Pilotes/Include/PWM.h new file mode 100644 index 0000000..049b49f --- /dev/null +++ b/Pilotes/Include/PWM.h @@ -0,0 +1,4 @@ +#include "stm32f10x.h" +// Config +extern void MyTimer_PWM(TIM_TypeDef *Timer, char Channel); +extern void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent); diff --git a/Pilotes/Include/Timer.h b/Pilotes/Include/Timer.h new file mode 100644 index 0000000..1a8de79 --- /dev/null +++ b/Pilotes/Include/Timer.h @@ -0,0 +1,12 @@ +#include "stm32f10x.h" +// Config de timer +extern void MyTimer_Base_Init(TIM_TypeDef *Timer , unsigned short ValARR , unsigned short ValPSC ); +extern void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void)); +// Fonctions d'interruption +extern void TIM2_IRQHandler(void); +extern void TIM3_IRQHandler(void); +extern void TIM4_IRQHandler(void); +extern void TIM1_CC_IRQnHandler(void); +extern void TIM1_UP_IRQnHandler(void); +// Enable timers +void EnableTimer(TIM_TypeDef *Timer); diff --git a/Pilotes/Include/test.h b/Pilotes/Include/test.h new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/Pilotes/Source/ADC.c b/Pilotes/Source/ADC.c new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/Pilotes/Source/DriverGPIO.c b/Pilotes/Source/DriverGPIO.c new file mode 100644 index 0000000..37cd7dc --- /dev/null +++ b/Pilotes/Source/DriverGPIO.c @@ -0,0 +1,71 @@ +#include "stm32f10x.h" + +#define In_Floating 0x4 +#define In_PullDown 0x8 +#define In_PullUp 0x8 +#define In_Analog 0x0 +#define Out_Ppull 0x3 +#define Out_OD 0x7 +#define AltOut_Ppull 0xB +#define AltOut_OD 0xF + +void MyGPIO_Init(GPIO_TypeDef * GPIO, char pin, char conf ){ +int shift_pin; +//Start clock for relevant GPIO +if(GPIO == GPIOA){ +RCC -> APB2ENR |= RCC_APB2ENR_IOPAEN; +} +else if(GPIO == GPIOB){ +RCC -> APB2ENR |= RCC_APB2ENR_IOPBEN; +} +else if(GPIO == GPIOC){ +RCC -> APB2ENR |= RCC_APB2ENR_IOPCEN; +} +else if(GPIO == GPIOD){ +RCC -> APB2ENR |= RCC_APB2ENR_IOPDEN; +} +if(pin < 8){//CRL zone +shift_pin = pin*4; +GPIO -> CRL &= ~(0xF << shift_pin); +//PullUp and PullDown have the same conf number, so we need to change the ODR to diferenciate them both +if(conf == In_PullUp){ +GPIO -> CRL |= ( In_PullUp << shift_pin); +GPIO -> ODR |= (1< CRL |= ( In_PullDown << shift_pin); +GPIO -> ODR &= ~(1< CRL |= ( conf << shift_pin); +} +} +else{//CRH zone +shift_pin = (pin-8)*4; +GPIO -> CRH &= ~(0xF << shift_pin); +if(conf == In_PullUp){ +GPIO -> CRH |= ( In_PullUp << shift_pin); +GPIO -> ODR |= (1< CRH |= ( In_PullDown << shift_pin); +GPIO -> ODR &= ~(1< CRH |= ( conf << shift_pin); +} +} +} +int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin){ + +return(GPIO -> IDR & (1 << GPIO_Pin)); +} +void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin){ +GPIO -> BSRR = (1< BSRR = (1<<(GPIO_Pin+16));//1 on reset zone +} +void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin){ +GPIO -> ODR = GPIO -> ODR ^ (0x1 << GPIO_Pin); +} diff --git a/Pilotes/Source/Girouette.c b/Pilotes/Source/Girouette.c new file mode 100644 index 0000000..18ebe8b --- /dev/null +++ b/Pilotes/Source/Girouette.c @@ -0,0 +1,30 @@ +#include "stm32f10x.h" +#include "MyTimer.h" +#include "Nucleo.h" +#include "Timer.h" +#include "DriverGPIO.h" +#include "Girouette.h" + +#define POSITIONS 4*360 + +void configEncoder(TIM_TypeDef * Timer){ + Timer -> CCMR1 |= TIM_CCMR1_CC1S; // TI1FP1 mapped on TI1 + Timer -> CCMR1 |= TIM_CCMR1_CC2S; // TI1FP2 mapped on TI2 + Timer -> CCER &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP); // TI1FP1 output non-inverted + Timer -> CCMR1 &= ~(TIM_CCMR1_IC1F); // Input capture 1 filter, no filter + Timer -> CCER &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP); // TI1FP2 output non-inverted + Timer -> CCMR2 &= ~(TIM_CCMR1_IC2F); // Input capture 2 filter, no filter + Timer -> SMCR &= ~TIM_SMCR_SMS; // Reset SMS-bits + Timer -> SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;// SMS = "011" + Timer -> CR1 |= TIM_CR1_CEN; // Enable counter + Timer -> ARR = 0xFD20; // Setting ARR as 1440*45 +} +void configChannel(){ + MyGPIO_Init(GPIOA,0,In_Floating ); // GPIOA pin 0 in mode floating TIM2_CH1 + MyGPIO_Init(GPIOA,1,In_Floating ); // GPIOA pin 1 in mode floating TIM2_CH2 + MyGPIO_Init(GPIOA,8,In_Floating ); // GPIOA pin 7 in mode floating Index +} + +int returnAngle (TIM_TypeDef * Timer){ + return(Timer -> CNT / POSITIONS * 360); +} diff --git a/Pilotes/Source/MyTimer.c b/Pilotes/Source/MyTimer.c new file mode 100644 index 0000000..bf5be8a --- /dev/null +++ b/Pilotes/Source/MyTimer.c @@ -0,0 +1,39 @@ +#include "stm32f10x.h" +#include "Timer.h" +#include "MyTimer.h" +#include "PWM.h" +#include "DriverGPIO.h" +// Variables +#define ARR_TIM1 0xFFAD +#define PSC_TIM1 0xFF +#define ARR_TIM2 0xFFAD +#define PSC_TIM2 0x0225 +#define ARR_TIM3 0x2CF +#define PSC_TIM3 0x0 + +volatile int g_tick_count; +void Test(void){ + // Signal + g_tick_count++; + MyGPIO_Toggle(GPIOA, 8); +} + +void ConfigureTimers(){ +// Config ARR & PSC +//MyTimer_Base_Init(TIM2, ARR_TIM2, PSC_TIM2); +MyTimer_Base_Init(TIM1, ARR_TIM1, PSC_TIM1); +MyTimer_Base_Init(TIM3, ARR_TIM2, PSC_TIM2); +// Enable timer clock +EnableTimer(TIM1); +EnableTimer(TIM2); +EnableTimer(TIM3); +} +void ConfigureIT(){ // Activate general interuption with a function and priority +//MyTimer_ActiveIT(TIM2, 4, Test); //start interruption with priority 4 +//MyTimer_ActiveIT(TIM1, 4, Test); //start interruption with priority 4 +MyTimer_ActiveIT(TIM3, 4, Test); //start interruption with priority 4 +} +void ConfigurePWM(){ // Set dutycycle with timer +MyTimer_PWM(TIM1, 1); +MyTimer_Set_DutyCycle(TIM1, 1, 20.0); +} diff --git a/Pilotes/Source/Nucleo.c b/Pilotes/Source/Nucleo.c new file mode 100644 index 0000000..c719043 --- /dev/null +++ b/Pilotes/Source/Nucleo.c @@ -0,0 +1,25 @@ +#include "stm32f10x.h" +#include "../Include/Nucleo.h" +#include "DriverGPIO.h" + +void ConfigHorloge(void) { // Peut-être redondant ?? +RCC->APB2ENR |= (0x01 << 2) | (0x01 << 3) | (0x01 << 4) | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_TIM1EN; +}; + +void ConfigBroche(void){ // + //Mettre Broche 5 GPIOA à output push-pull + MyGPIO_Init(GPIOA, 5, Out_Ppull); + //Mettre broche 8 sur GPIOA à output open drain + MyGPIO_Init(GPIOA, 8, Out_OD); +}; + +int BoutonAppuye(void){ // Peut être modifié avec ChercherEtat + return(MyGPIO_Read(GPIOA, 9)); +} +void AllumerLED(void){ + MyGPIO_Set(GPIOA, 8); +} +void EteindreLED(void){ + MyGPIO_Reset(GPIOA, 8); +} + diff --git a/Pilotes/Source/PWM.c b/Pilotes/Source/PWM.c new file mode 100644 index 0000000..a66a5b2 --- /dev/null +++ b/Pilotes/Source/PWM.c @@ -0,0 +1,67 @@ +#include "stm32f10x.h" +#include "../Include/PWM.h" + + +void MyTimer_PWM(TIM_TypeDef *Timer, char Channel) { // Activer PWM sur un output + // preload + Timer->CR1 |= TIM_CR1_ARPE; + + switch (Channel) { + case 1: + // Config o channel 1 in "PWM Mode 1" and enable preload of CCR1 + Timer->CCMR1 &= ~TIM_CCMR1_OC1M; // clean bit modes + Timer->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1; // Mode PWM 1 + Timer->CCMR1 |= TIM_CCMR1_OC1PE; // enable preload + + // enable exit 1 (Output enable) + Timer->CCER |= TIM_CCER_CC1E; + break; + case 2: + Timer->CCMR1 &= ~TIM_CCMR1_OC2M; + Timer->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1; + Timer->CCMR1 |= TIM_CCMR1_OC2PE; + Timer->CCER |= TIM_CCER_CC2E; + break; + case 3: + Timer->CCMR2 &= ~TIM_CCMR2_OC3M; + Timer->CCMR2 |= TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1; + Timer->CCMR2 |= TIM_CCMR2_OC3PE; + Timer->CCER |= TIM_CCER_CC3E; + break; + case 4: + Timer->CCMR2 &= ~TIM_CCMR2_OC4M; + Timer->CCMR2 |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1; + Timer->CCMR2 |= TIM_CCMR2_OC4PE; + Timer->CCER |= TIM_CCER_CC4E; + break; + } + + // special case(specific timers) + if (Timer == TIM1 || Timer == TIM8) { + Timer->BDTR |= TIM_BDTR_MOE; + } +} +void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent) { + unsigned short ccr_value; + + // Percentages between 0 and 100 + if (DutyCycle_Percent > 100.0) DutyCycle_Percent = 100.0; + if (DutyCycle_Percent < 0.0) DutyCycle_Percent = 0.0; + // calcule of crr + ccr_value = (unsigned short)((DutyCycle_Percent / 100.0) * (Timer->ARR)); + // Assigner le valaur pour le registre de comparison pour le channel qui est pertient + switch (Channel) { + case 1: + Timer->CCR1 = ccr_value; + break; + case 2: + Timer->CCR2 = ccr_value; + break; + case 3: + Timer->CCR3 = ccr_value; + break; + case 4: + Timer->CCR4 = ccr_value; + break; + } +} diff --git a/Pilotes/Source/Timer.c b/Pilotes/Source/Timer.c new file mode 100644 index 0000000..b17f122 --- /dev/null +++ b/Pilotes/Source/Timer.c @@ -0,0 +1,102 @@ +#include "stm32f10x.h" +#include "../Include/Timer.h" + +//REMEMBER TO ENALBLE TIMERS +//EXAMPLES + //RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN; // Enable TIM2 + //RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable TIM1 + +void MyTimer_Base_Init( TIM_TypeDef * Timer , unsigned short ValARR , unsigned short ValPSC ) { // Configuration du timer +Timer -> PSC=(ValPSC); +Timer-> ARR = (ValARR); +Timer->EGR |= TIM_EGR_UG; +}; + + +static void (*p_IT_functions[4])(void); // Pour créer l'array des fonctions + +void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void)) { + //Enable interruption requisition + Timer->DIER |= TIM_DIER_UIE; // Update interrupt enable + + //Id the interruption timer routine + IRQn_Type IRQn; + + int timer_index = -1; // Indice pour notre array des pointeurs + if (Timer == TIM2) { + IRQn = TIM2_IRQn; + timer_index = 0; + } else if (Timer == TIM3) { + IRQn = TIM3_IRQn; + timer_index = 1; + } else if (Timer == TIM4) { + IRQn = TIM4_IRQn; + timer_index = 2; + } + // Keep the pointer of the valid timer function + if (timer_index != -1) { + p_IT_functions[timer_index] = IT_function; // index the function + } else { + return; // Timer invalid + } + // set interruption priority + NVIC_SetPriority(IRQn, Prio); + // Enable routine + NVIC_EnableIRQ(IRQn); +}; + + +void TIM2_IRQHandler(void) { + // Clean flag + TIM2->SR &= ~TIM_SR_UIF; // Drapeau d'interuption + //Call function + if (p_IT_functions[0] != 0) { + p_IT_functions[0](); // Execute fonction + } +}; +void TIM3_IRQHandler(void) { + // Clean flag + TIM3->SR &= ~TIM_SR_UIF; + //Call function + if (p_IT_functions[1] != 0) { + p_IT_functions[1](); // Execute function + } +}; +void TIM4_IRQHandler(void) { + // Clean flag + TIM4->SR &= ~TIM_SR_UIF; + //Call function + if (p_IT_functions[2] != 0) { + p_IT_functions[2](); // Execute function + } +}; +// IT PWM +void TIM1_CC_IRQHandler(void) { + // Clean flag + TIM1 -> DIER &= ~TIM_DIER_CC1IE; + //Set bit + GPIOA -> ODR |= (0x1 << 8); +}; + +void TIM1_UP_IRQHandler(void) { + // Clean flag + TIM1-> DIER &= ~TIM_DIER_TIE; + //Reset bit + GPIOA -> ODR &= ~(0x1 << 8); +}; +void EnableTimer(TIM_TypeDef *Timer){ + if(Timer == TIM2){ + RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN; + } + else if(Timer == TIM3){ + RCC -> APB1ENR |= RCC_APB1ENR_TIM3EN; + } + else if(Timer == TIM4){ + RCC -> APB1ENR |= RCC_APB1ENR_TIM4EN; + } + else if(Timer == TIM1){ + RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; + } + else{ + } +}