From 9ed6e01eeb4dfed082741d5ecfd3d2105287d483 Mon Sep 17 00:00:00 2001 From: Robin M Date: Sat, 18 Mar 2023 21:34:40 +0100 Subject: [PATCH] Changes on MyGPIO_Init function --- drivers/Driver_GPIO.c | 62 +++++++++++++++++++++++++++++-------------- projet_1/src/main.c | 48 +++++++++++++++------------------ projet_1/tp.uvoptx | 26 +++++++++++++++++- projet_1/tp.uvprojx | 24 +++++++++++++++++ 4 files changed, 113 insertions(+), 47 deletions(-) diff --git a/drivers/Driver_GPIO.c b/drivers/Driver_GPIO.c index a4bf322..d41d350 100644 --- a/drivers/Driver_GPIO.c +++ b/drivers/Driver_GPIO.c @@ -1,29 +1,41 @@ #include "Driver_GPIO.h" #include "stm32f10x.h" +void test() { + int a = 20; +} + void MyGPIO_Init(MyGPIO_Struct_TypeDef * GPIOStructPtr) { - // variable pour stocker la configuration du GPIO - uint32_t tmpreg = 0; - - // configurer le mode de la broche GPIO - if ((GPIOStructPtr->GPIO_Pin & 0x8) != 0) { // si la broche a une résistance de pull-up ou pull-down activée - tmpreg |= ((uint32_t)GPIOStructPtr->GPIO_Conf & (uint32_t)0x0F) << ((uint32_t)(((uint32_t)GPIOStructPtr->GPIO_Pin & (uint32_t)0x07) * 4)); - } else { // sinon - tmpreg |= ((uint32_t)GPIOStructPtr->GPIO_Conf & (uint32_t)0x0F) << ((uint32_t)(((uint32_t)GPIOStructPtr->GPIO_Pin & (uint32_t)0x07) * 4) + 2); + // Activation des horloges des ports A et C + RCC->APB2ENR |= (RCC_APB2ENR_IOPCEN); + RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN); + + // On calcule la position de la broche à configurer en utilisant le numéro de broche contenu dans la structure GPIOStructPtr + uint32_t GPIO_Pin = 1 << GPIOStructPtr->GPIO_Pin; + + // On vérifie si la broche à configurer se trouve dans les 8 premières broches (CRL) ou les 8 dernières broches (CRH) + if (GPIOStructPtr->GPIO_Pin <= 7) { // Si la broche est dans les 8 premières broches (CRL) + // On réinitialise les 4 bits de configuration de la broche en utilisant un masque qui est décalé vers la gauche de (GPIOStructPtr->GPIO_Pin * 4) bits pour cibler la broche à configurer + GPIOStructPtr->GPIO->CRL &= ~(0xF << (GPIOStructPtr->GPIO_Pin * 4)); + // On configure la broche selon la configuration souhaitée en utilisant un masque qui est décalé vers la gauche de (GPIOStructPtr->GPIO_Pin * 4) bits pour cibler la broche à configurer + GPIOStructPtr->GPIO->CRL |= (GPIOStructPtr->GPIO_Conf << (GPIOStructPtr->GPIO_Pin * 4)); + } + else { // Si la broche est dans les 8 dernières broches (CRH) + // On réinitialise les 4 bits de configuration de la broche en utilisant un masque qui est décalé vers la gauche de ((GPIOStructPtr->GPIO_Pin - 8) * 4) bits pour cibler la broche à configurer + GPIOStructPtr->GPIO->CRH &= ~(0xF << ((GPIOStructPtr->GPIO_Pin - 8) * 4)); + // On configure la broche selon la configuration souhaitée en utilisant un masque qui est décalé vers la gauche de ((GPIOStructPtr->GPIO_Pin - 8) * 4) bits pour cibler la broche à configurer + GPIOStructPtr->GPIO->CRH |= (GPIOStructPtr->GPIO_Conf << ((GPIOStructPtr->GPIO_Pin - 8) * 4)); } - - // configurer la vitesse de la broche GPIO - tmpreg |= (uint32_t)0x2 << ((uint32_t)(((uint32_t)GPIOStructPtr->GPIO_Pin & (uint32_t)0x07) * 4) + 2); - - // configurer le GPIO mode alternate function (AF) - if ((GPIOStructPtr->GPIO_Conf & 0x8) != 0) { // si la broche est configurée en alternate function - tmpreg &= ~(((uint32_t)0x0F) << ((uint32_t)(((uint32_t)GPIOStructPtr->GPIO_Pin & (uint32_t)0x07) * 4))); - tmpreg |= ((uint32_t)GPIOStructPtr->GPIO_Conf & (uint32_t)0x0F) << ((uint32_t)(((uint32_t)GPIOStructPtr->GPIO_Pin & (uint32_t)0x07) * 4)); + + // Si la broche est en entrée, on la configure en input mode avec pull-up/pull-down désactivé + if (GPIOStructPtr->GPIO_Conf == In_Floating || GPIOStructPtr->GPIO_Conf == In_PullDown || GPIOStructPtr->GPIO_Conf == In_PullUp) { + // On met la broche en input mode en utilisant le registre BSRR de la GPIO + GPIOStructPtr->GPIO->BSRR = GPIO_Pin << 16; + } + else { // Si la broche est en sortie, on la configure en output mode et on met la broche à 0 (niveau bas) + // On met la broche en output mode en utilisant le registre BSRR de la GPIO + GPIOStructPtr->GPIO->BSRR = GPIO_Pin; } - - // configurer la broche GPIO - GPIOStructPtr->GPIO->CRL &= ~(0xF << ((GPIOStructPtr->GPIO_Pin & 0x7) * 4)); // effacer les bits de configuration actuels - GPIOStructPtr->GPIO->CRL |= tmpreg; // écrire la nouvelle configuration } @@ -46,6 +58,16 @@ void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin) { GPIO->BSRR |= (uint32_t)(1 << GPIO_Pin); // Set la pin correspondante en écrivant un 1 dans le bit correspondant de BSRR } +void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin) +{ + // Calcul de la valeur du masque à appliquer pour réinitialiser la broche + uint32_t mask = 1 << GPIO_Pin; // on décale le bit 1 de GPIO_Pin positions vers la gauche + + // Réinitialisation de la broche GPIO en niveau bas + GPIO->BRR = mask; // on met à 1 tous les bits correspondant aux broches à réinitialiser, ce qui les mettra à 0 (niveau bas) +} + + void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin) { GPIO->ODR ^= (1 << GPIO_Pin); // On fait un XOR pour toggle la pin correspondante } \ No newline at end of file diff --git a/projet_1/src/main.c b/projet_1/src/main.c index 6f326a2..772bb70 100644 --- a/projet_1/src/main.c +++ b/projet_1/src/main.c @@ -1,33 +1,29 @@ #include "stm32f10x.h" +#include "Driver_GPIO.h" -int main(void) -{ - // Voir ~p113 et ~p170 du manual +int main(void) { - // Activation des horloges des ports A et C - RCC->APB2ENR |= (RCC_APB2ENR_IOPCEN); - RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN); + // Configure la broche PA5 en sortie + MyGPIO_Struct_TypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Pin = 5; + GPIO_InitStructure.GPIO_Conf = Out_Ppull; + GPIO_InitStructure.GPIO = GPIOA; + MyGPIO_Init(&GPIO_InitStructure); - // Configuration de PC13 en input pull-up - GPIOC->CRH &= ~(GPIO_CRH_CNF13) | (GPIO_CRH_MODE13); // Mise à 0 de CNF13 et MODE13 pour fonctionnement en Input pull-down voir p160 - GPIOC->CRH |= GPIO_CRH_CNF13_0; // Mise à 1 de CNF1 pour fonctionnement input pull up/down - GPIOC->ODR |= GPIO_ODR_ODR13; //Pull up -> ODR13 à 0 + // Configure la broche PC13 en entrée avec une résistance de pull-up + GPIO_InitStructure.GPIO_Pin = 13; + GPIO_InitStructure.GPIO_Conf = In_PullUp; + GPIO_InitStructure.GPIO = GPIOC; + MyGPIO_Init(&GPIO_InitStructure); - // Configuration de PA5 en sortie push-pull - GPIOA->CRL &= ~(GPIO_CRL_MODE5); - GPIOA->CRL &= ~(GPIO_CRL_CNF5); - GPIOA->CRL |= GPIO_CRL_MODE5_0; - - while (1) - { - // Vérification de l'état du bouton - if ((GPIOC->IDR & GPIO_IDR_IDR13) == 0) - { - // Inversion de l'état de la broche PA5 - GPIOA->ODR ^= GPIO_ODR_ODR5; - - // Attente pour éviter les rebonds du bouton - for (int i = 0; i < 1000000; i++); + while(1) { + // Vérifie si le bouton est pressé + if(MyGPIO_Read(GPIOC, 13) == 0) { + // Toggle la LED + MyGPIO_Toggle(GPIOA, 5); + + // Delay pour éviter les rebonds du au ressort du bouton + for(int i = 0; i < 1000000; i++); } } -} +} \ No newline at end of file diff --git a/projet_1/tp.uvoptx b/projet_1/tp.uvoptx index 67d47a0..ff90d18 100644 --- a/projet_1/tp.uvoptx +++ b/projet_1/tp.uvoptx @@ -384,10 +384,34 @@ drivers - 0 + 1 0 0 0 + + 2 + 2 + 1 + 0 + 0 + 0 + ..\drivers\Driver_GPIO.c + Driver_GPIO.c + 0 + 0 + + + 2 + 3 + 5 + 0 + 0 + 0 + ..\drivers\Driver_GPIO.h + Driver_GPIO.h + 0 + 0 + diff --git a/projet_1/tp.uvprojx b/projet_1/tp.uvprojx index 47d0651..d71be3f 100644 --- a/projet_1/tp.uvprojx +++ b/projet_1/tp.uvprojx @@ -393,6 +393,18 @@ drivers + + + Driver_GPIO.c + 1 + ..\drivers\Driver_GPIO.c + + + Driver_GPIO.h + 5 + ..\drivers\Driver_GPIO.h + + ::CMSIS @@ -789,6 +801,18 @@ drivers + + + Driver_GPIO.c + 1 + ..\drivers\Driver_GPIO.c + + + Driver_GPIO.h + 5 + ..\drivers\Driver_GPIO.h + + ::CMSIS