Projet_Voilier/drivers/GPIO.c
2025-11-14 13:52:38 +01:00

58 lines
No EOL
1.4 KiB
C

#include "stm32f10x.h"
#include "GPIO.h"
void MyGPIO_Init(GPIO_TypeDef *GPIO, char pin, char conf){
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) {
if (conf == 0xF) {
GPIO->CRL&=~(0xF<<4*pin);
GPIO->CRL|=(0x8<<4*pin);
GPIO->ODR|=(1<<pin);
}
else if (conf == 0x8) {
GPIO->CRL&=~(0xF<<4*pin);
GPIO->CRL|=(conf<<4*pin);
GPIO->ODR&=~(1<<pin);
}
else {
GPIO->CRL&=~(0xF<<4*pin);
GPIO->CRL|=(conf<<4*pin);
}
}
else {
if (conf == 0xF) {
GPIO->CRH&=~(0xF<<4*(pin-8));
GPIO->CRH|=(0x8<<4*(pin-8));
GPIO->ODR|=(1<<pin);
}
else if (conf == 0x8) {
GPIO->CRH&=~(0xF<<4*(pin-8));
GPIO->CRH|=(conf<<4*(pin-8));
GPIO->ODR&=~(1<<pin);
}
else {
GPIO->CRH&=~(0xF<<4*(pin-8));
GPIO->CRH|=(conf<<4*(pin-8));
}
}
}
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<<GPIO_Pin);
}
void MyGPIO_Reset(GPIO_TypeDef *GPIO, char GPIO_Pin){
GPIO->BSRR=(1<<(GPIO_Pin+16));
}
void MyGPIO_Toggle(GPIO_TypeDef *GPIO, char GPIO_Pin){
if (((GPIO->ODR>>GPIO_Pin)&1) == 1) MyGPIO_Reset(GPIO, GPIO_Pin);
else MyGPIO_Set(GPIO, GPIO_Pin);
}