Documentation

See Class Github for the example documentation

Embedded Controller HAL Library

Written by: Your Name

Program: C/C++

IDE/Compiler: Keil uVision 5

OS: WIn10

MCU: STM32F411RE, Nucleo-64

Table of Contents

GPIO Digital InOut

Header File

#include "ecGPIO.h"

#include "stm32f411xe.h"
#include "ecRCC.h"

#ifndef __ECGPIO_H
#define __ECGPIO_H

// MODER
#define INPUT  		0x00
#define OUTPUT 		0x01
#define AF     		0x02
#define ANALOG 		0x03

// IDR & ODR
#define HIGH 		1
#define LOW  		0

// OSPEED
#define LOW_SPEED		0x00
#define MID_SPEED		0x01
#define FAST_SPEED		0x02
#define HIGH_SPEED		0x03

// OTYPER
#define PUSH_PULL 		0	// Push-pull
#define OPEN_DRAIN 		1 	// Open-Drain

// PUDR
#define NO_PUPD			0x00 	// No pull-up, pull-down
#define PULL_UP			0x01 	// Pull-up
#define PULL_DOWN 		0x02 	// Pull-down	
#define RESERVED 		0x03 	// Reserved

// PIN
#define LED_PIN 		5
#define BUTTON_PIN 		13

#ifdef __cplusplus
 extern "C" {
#endif /* __cplusplus */
	 
void GPIO_init(GPIO_TypeDef *Port, int pin, int mode);
void GPIO_mode(GPIO_TypeDef* Port, int pin, int mode);
void GPIO_write(GPIO_TypeDef *Port, int pin, int output);
int  GPIO_read(GPIO_TypeDef *Port, int pin);
void GPIO_ospeed(GPIO_TypeDef* Port, int pin, int speed);
void GPIO_otype(GPIO_TypeDef* Port, int pin, int type);
void GPIO_pupdr(GPIO_TypeDef* Port, int pin, int pupd);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif

GPIO_init()

Initializes GPIO pins with default setting and Enables GPIO Clock. Mode: In/Out/AF/Analog

void GPIO_init(GPIO_TypeDef *Port, int pin, int mode);

Parameters

  • Port: Port Number, GPIOA~GPIOH

  • pin: pin number (int) 0~15

  • mode: INPUT(0), OUTPUT(1), AF(02), ANALOG (03)

Example code

GPIO_init(GPIOA, 5, OUTPUT);
GPIO_init(GPIOC, 13, INPUT); //GPIO_init(GPIOC, 13, 0);

GPIO_mode()

Configures GPIO pin modes: In/Out/AF/Analog

void GPIO_init(GPIO_TypeDef *Port, int pin, int mode);

Parameters

  • Port: Port Number, GPIOA~GPIOH

  • pin: pin number (int) 0~15

  • mode: INPUT (0), OUTPUT (1), AF(02), ANALOG (03)

Example code

GPIO_mode(GPIOA, 5, OUTPUT);

GPIO_write()

Write the data to GPIO pin: High, Low

write(GPIO_TypeDef *Port, int pin, int output);

Parameters

  • Port: Port Number, GPIOA~GPIOH

  • pin: pin number (int) 0~15

  • output: LOW(0), HIGH(1)

Example code

GPIO_write(GPIOA, 5, 1);  // 1: High

GPIO_read()

Read the data from GPIO pin

int  GPIO_read(GPIO_TypeDef *Port, int pin);

Parameters

  • Port: Port Number, GPIOA~GPIOH

  • pin: pin number (int) 0~15

Example code

GPIO_read(GPIOC, 13);

GPIO_ospeed()

Configures output speed of GPIO pin : Low, Mid, Fast, High

void GPIO_ospeed(GPIO_TypeDef* Port, int pin, int speed);

Parameters

  • Port: Port Number, GPIOA~GPIOH

  • pin: pin number (int) 0~15

  • speed: LOW_SPEED(0), MID_SPEED(1), FAST_SPEED(2) , HIGH_SPEED(3)

Example code

GPIO_ospeed(GPIOA, 5, 2);  // 2: FAST_SPEED

GPIO_otype()

Configures output type of GPIO pin: Push-Pull / Open-Drain

void GPIO_otype(GPIO_TypeDef* Port, int pin, int type);

Parameters

  • Port: Port Number, GPIOA~GPIOH

  • pin: pin number (int) 0~15

  • type: PUSH_PULL(0), OPEN_DRAIN(1)

Example code

GPIO_otype(GPIOA, 5, 0);  // 0: Push-Pull

GPIO_pupdr()

Configures Pull-up/Pull-down mode of GPIO pin: No Pull-up, Pull-down/ Pull-up/ Pull-down/ Reserved

void GPIO_pupdr(GPIO_TypeDef* Port, int pin, int pupd);

Parameters

  • Port: Port Number, GPIOA~GPIOH

  • pin: pin number (int) 0~15

  • pupd: NO_PUPD(0), PULL_UP(1), PULL_DOWN(2), RESERVED(3)

Example code

GPIO_pupdr(GPIOA, 5, 0);  // 0: No Pull-up, Pull-down

Class or Header name

Function Name

Parameters

  • p1

  • p2

Example code

Last updated