#include <stdint.h>
#define GPIO
#define RCC_BASE 0x40021000U
#define GPIOA_BASE 0x50000000U
#define LED_RED 5
#define LED_BLUE 4
#define Push_Button 3
#define RCC_IOPENR (*(volatile uint32_t *)(RCC_BASE + 0x2C))
#define GPIOA_MODER (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
#define GPIOA_OTYPER (*(volatile uint32_t *)(GPIOA_BASE + 0x04))
#define GPIOA_OSPEEDR (*(volatile uint32_t *)(GPIOA_BASE + 0x08))
#define GPIOA_PUPDR (*(volatile uint32_t *)(GPIOA_BASE + 0x0C))
#define GPIOA_IDR (*(volatile uint32_t *)(GPIOA_BASE + 0x10))
#define GPIOA_ODR (*(volatile uint32_t *)(GPIOA_BASE + 0x14))
#define GPIOA_BSRR (*(volatile uint32_t *)(GPIOA_BASE + 0x18))
#define GPIO_PIN_0 0U
#define GPIO_PIN_1 1U
#define GPIO_PIN_2 2U
#define GPIO_PIN_3 3U
#define GPIO_PIN_4 4U
#define GPIO_PIN_5 5U
#define GPIO_PIN_6 6U
#define GPIO_PIN_7 7U
#define GPIO_PIN_8 8U
#define GPIO_PIN_9 9U
#define GPIO_PIN_10 10U
#define GPIO_PIN_11 11U
#define GPIO_PIN_12 12U
#define GPIO_PIN_13 13U
#define GPIO_PIN_14 14U
#define GPIO_PIN_15 15U
typedef enum {
Input = 0U,
Output,
Analog,
Alternative
} GPIO_MODE_t;
typedef enum {
Push_Pull = 0U,
Open_Drain
} GPIO_OTYPE_t;
typedef enum{
Low_Speed = 0U,
Medium_Speed,
High_Speed,
Very_High_Speed
}
GPIO_OSPEED_t;
typedef enum{
No_Pull = 0U,
Pull_Up,
Pull_Down,
Reserved
}
GPIO_PUPD_t;
typedef enum {
GPIO_LOW = 0U,
GPIO_HIGH
} GPIO_Value_t;
void delay(void){
for (volatile uint32_t i = 0; i < 500000U; i++);
}
void GPIO_Init(uint8_t pin, GPIO_MODE_t mode, GPIO_OTYPE_t outputType, GPIO_OSPEED_t outputSpeed, GPIO_PUPD_t pullUpDown)
{
GPIOA_MODER &= ~(3UL << (pin * 2));
GPIOA_MODER |= ((uint32_t)(mode << (pin*2))); // Setiing Mode(Output,Input,Alternate,Analog)
GPIOA_OTYPER &= ~(1UL << pin);
GPIOA_OTYPER |= (uint32_t)(outputType << pin); // Setting PullUpDown or Open drain
GPIOA_OSPEEDR &= ~(3UL << (pin * 2));
GPIOA_OSPEEDR |= ((uint32_t)(outputSpeed << (pin*2))); // Setting Speed of GPIO pin
GPIOA_PUPDR &= ~(3UL << (pin * 2));
GPIOA_PUPDR |= ((uint32_t)(pullUpDown << (pin*2))); //Setting GPIO as Pull Up or Down
}
void GPIO_Write(uint8_t pin, GPIO_Value_t value){
if(value){
GPIOA_BSRR = (1UL << pin); //Set the pin or On the LED
// port->BSRR |= (1UL << pin);
}
else{
GPIOA_BSRR = (1UL << (pin + 16U)); // Reset the pin or Off the LED
// port->BSRR |= (1UL << (pin + 16));
}
}
GPIO_Value_t GPIO_Read(uint8_t pin)
{
if (GPIOA->IDR & (1U << pin))
{
return GPIO_HIGH;
}
else
{
return GPIO_LOW;
}
}
int main(void)
{
GPIO_Init(LED_RED,Output,Push_Pull,Low_Speed,No_Pull);
GPIO_Init(LED_BLUE,Output,Push_Pull,Low_Speed,No_Pull);
GPIO_Init(Push_Button,Output,Push_Pull,Low_Speed,Pull_Down);
while (1)
{
GPIO_Value_t readbutton = GPIO_Read(Push_Button);
if(readbutton == 1){
GPIO_Write(LED_RED,GPIO_HIGH);
GPIO_Write(LED_BLUE,GPIO_LOW);
}
else{
GPIO_Write(LED_RED,GPIO_LOW);
GPIO_Write(LED_BLUE,GPIO_HIGH);
}
}
}Loading
st-nucleo-l031k6
st-nucleo-l031k6