led.h
#ifndef __LED_H__
#define __LED_H__
#define RCC_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDER;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)
typedef enum{
INPUT,
OUTPUT,
ALT,
ANALOG
}gpio_moder_t;
typedef enum{
PUSH_PULL,
OPEN_DRAIN
}gpio_type_t;
typedef enum{
LOW,
MEDIUM,
HIGHT,
HIGHTER
}gpio_speeder_t;
typedef enum{
NO,
PULL_UP,
PULL_DOWN,
RESERVED
}gpio_puper_t;
typedef enum{
GPIO_RESET_T,
GPIO_SET_T
}gpio_status_t;
typedef struct{
gpio_moder_t moder;
gpio_type_t typer;
gpio_speeder_t speeder;
gpio_puper_t puper;
}gpio_init_t;
#define GPIO_PIN_0 0
#define GPIO_PIN_8 8
#define GPIO_PIN_10 10
void hal_gpio_init(gpio_t*gpiox, gpio_init_t* init, unsigned int pin_t);
void hal_gpio_write(gpio_t* gpiox,gpio_status_t status,unsigned int pin_t);
#endif
led.h
#include "led.h"
void hal_gpio_init(gpio_t*gpiox, gpio_init_t* init, unsigned int pin_t)
{
if(gpiox == GPIOE)
{
RCC_AHB4_ENSETR |= (0x1 << 4);
}
else if(gpiox == GPIOF)
{
RCC_AHB4_ENSETR |= (0x1 << 5);
}
gpiox->MODER &= ~(0x3 << (pin_t*2));
gpiox->MODER |= (init->moder << (pin_t*2));
gpiox->OTYPER &= ~(0x1 << pin_t);
gpiox->OTYPER |= (init->typer << pin_t);
gpiox->OSPEEDER &= ~(0x3 << (pin_t*2));
gpiox->OSPEEDER |= (init->speeder << (pin_t*2));
gpiox->PUPDR &= ~(0x3 << (pin_t*2));
gpiox->PUPDR |= (init->puper << (pin_t*2));
}
void hal_gpio_write(gpio_t* gpiox,gpio_status_t status,unsigned int pin_t)
{
gpiox->ODR &= ~(0x1 << pin_t);
gpiox->ODR |= (status << pin_t);
}
main.c
#include "led.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
gpio_init_t led1_init = {OUTPUT, PUSH_PULL, LOW, NO};
hal_gpio_init(GPIOE, &led1_init, GPIO_PIN_10);
gpio_init_t led2_init = {OUTPUT, PUSH_PULL, LOW, NO};
hal_gpio_init(GPIOF, &led2_init, GPIO_PIN_10);
gpio_init_t led3_init = {OUTPUT, PUSH_PULL, LOW, NO};
hal_gpio_init(GPIOE, &led3_init, GPIO_PIN_8);
while(1)
{
hal_gpio_write(GPIOE, GPIO_SET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOE, GPIO_RESET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOF, GPIO_SET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOF, GPIO_RESET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOE, GPIO_SET_T, GPIO_PIN_8);
delay_ms(500);
hal_gpio_write(GPIOE, GPIO_RESET_T, GPIO_PIN_8);
delay_ms(500);
}
return 0;
}