58 行
无行尾
2.6 KiB
C
58 行
无行尾
2.6 KiB
C
#include "Driver_GPIO.h"
|
|
#include "stm32f10x.h"
|
|
|
|
void MyGPIO_Init(MyGPIO_Struct_TypeDef * GPIOStructPtr) {
|
|
// Activation de l'horloge correspondante au port GPIO
|
|
if (GPIOStructPtr->GPIO == GPIOA) {
|
|
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
|
|
} else if (GPIOStructPtr->GPIO == GPIOB) {
|
|
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
|
|
} else if (GPIOStructPtr->GPIO == GPIOC) {
|
|
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
|
|
} else if (GPIOStructPtr->GPIO == GPIOD) {
|
|
RCC->APB2ENR |= RCC_APB2ENR_IOPDEN;
|
|
}
|
|
|
|
// Configuration de la broche GPIO
|
|
if (GPIOStructPtr->GPIO_Pin < 8) { // CRL Configure les GPIO de 0 à 7
|
|
GPIOStructPtr->GPIO->CRL &= ~(0xF << (4 * GPIOStructPtr->GPIO_Pin)); // Efface les 4 bits correspondant à la broche GPIO, on calcule la position à partir du début en multipliant par 4 (eg. 4 * GPIO5 = 20), on fais un masque avec 0b1111 (0xF) ensuite pour mettre à 0 les bits présents
|
|
GPIOStructPtr->GPIO->CRL |= (GPIOStructPtr->GPIO_Conf << (4 * GPIOStructPtr->GPIO_Pin)); // Ajoute la configuration pour la broche GPIO
|
|
} else { // CRH Configure les GPIO de 8 à 15
|
|
GPIOStructPtr->GPIO->CRL &= ~(0xF << (4 * (GPIOStructPtr->GPIO_Pin - 8)));
|
|
GPIOStructPtr->GPIO->CRL |= (GPIOStructPtr->GPIO_Conf << (4 * (GPIOStructPtr->GPIO_Pin - 8))); // On soutrait de 8 car la première broche de CRH est la 8 (8 - 8 == 0)
|
|
}
|
|
|
|
// Configuration d'une résistance de pull-up ou pull-down si nécessaire
|
|
if (GPIOStructPtr->GPIO_Conf == In_PullUp) {
|
|
GPIOStructPtr->GPIO->ODR |= (1 << GPIOStructPtr->GPIO_Pin);
|
|
} else if (GPIOStructPtr->GPIO_Conf == In_PullDown) {
|
|
GPIOStructPtr->GPIO->ODR &= ~(1 << GPIOStructPtr->GPIO_Pin);
|
|
}
|
|
}
|
|
|
|
int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin) {
|
|
int bitstatus;
|
|
|
|
// Vérifier si le pin est haut ou bas en lisant l'état du bit correspondant dans le registre IDR
|
|
if ((GPIO->IDR & (1 << GPIO_Pin)) != 0) {
|
|
bitstatus = 1; // Si le bit est à 1, le pin est haut
|
|
} else {
|
|
bitstatus = 0; // Si le bit est à 0, le pin est bas
|
|
}
|
|
|
|
// Renvoyer l'état du GPIO (0 ou 1)
|
|
return bitstatus;
|
|
}
|
|
|
|
void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin) {
|
|
GPIO->BSRR |= (1 << GPIO_Pin); // Set le pin correspondante en écrivant un 1 dans le bit correspondant de BSRR
|
|
}
|
|
|
|
void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin)
|
|
{
|
|
GPIO->BRR = (1 << GPIO_Pin); // Réinitialisation de la broche GPIO en niveau bas /!\ Logique inverse : mettre un 1 pour reset à 0
|
|
}
|
|
|
|
void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin) {
|
|
GPIO->ODR ^= (1 << GPIO_Pin); // On fait un XOR pour toggle la pin correspondante
|
|
} |