💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Introduction
  • Comparison mbed API vs EC API
  • Case study: mbed API
  • * mbed API: Class Digital In (DigitalIn.h)
  • * mbed HAL API: gpio_api.h
  • Tutorial: Create EC_API - for Digital In
  • Create Application API source file
  • Define Application API
  • Use Application API
  • Exercise: Create EC_API - for Digital Out
  • Download source file
  • Define Application API

Was this helpful?

  1. EC Course
  2. Tutorial

Tutorial: Creating Application API

PreviousTutorial: DocumentationNextTutorial: 7-Segment Display

Last updated 9 months ago

Was this helpful?

Introduction

In this tutorial, you will learn how to create user application API for a simple, user-friendly programming of MCU.

We will create our own application API (EC API) , in similar format as mbed API.

EC API is based on the EC_HAL API that is based on CMSIS-CORE.

Comparison mbed API vs EC API

mbed API example code

Example code for Digital In and Out using mbed

#include "mbed.h"

DigitalIn  button(USER_BUTTON);
DigitalOut led(LED1);

int main() {
    while(1) {
        if(!button)  led = 1;  //if(!button.read())
        else         led = 0;
    }
}

EC API example code

We are going to create EC API is similar form, such as

#include "EC_GPIO.h"

#define LED_PIN 	5
#define BUTTON_PIN 13

EC_DigitalIn button(GPIOC,BUTTON_PIN);
EC_DigitalOut led(GPIOA,LED_PIN);

	
int main(void) { 
	while(1){
		if(!button)	led=1;			//if(!button.read())
		else 				led=0;
	}
}

Case study: mbed API

Lets analyze how user API is structured in mbed. The application API is defined with C++ class and its methods. Each methods are based on HAL API, which is defined based on CMSIS-CORE.

For example, GPIO Digital In.

* mbed API: Class Digital In (DigitalIn.h)

* mbed HAL API: gpio_api.h

* mbed Microcontroller Library
 * Copyright (c) 2006-2020 ARM Limited

#ifndef MBED_DIGITALIN_H
#define MBED_DIGITALIN_H

#include "platform/platform.h"
#include "interfaces/InterfaceDigitalIn.h"
#include "hal/gpio_api.h"

namespace mbed {

class DigitalIn
{

public:
    
    DigitalIn(PinName pin) : gpio()
    {
        gpio_init_in(&gpio, pin);
    }

    DigitalIn(PinName pin, PinMode mode) : gpio()
    {
        gpio_init_in_ex(&gpio, pin, mode);
    }

    ~DigitalIn()
    {
        gpio_free(&gpio);
    }

    int read()
    {
        return gpio_read(&gpio);
    }

    void mode(PinMode pull);

    int is_connected()
    {
        return gpio_is_connected(&gpio);
    }

    operator int()
    {
        return read();
    }

protected:
#if !defined(DOXYGEN_ONLY)
    gpio_t gpio;
#endif //!defined(DOXYGEN_ONLY)
};


} 

#endif
/** \addtogroup hal */

#ifndef MBED_GPIO_API_H
#define MBED_GPIO_API_H

#include <stdint.h>
#include "device.h"
#include "pinmap.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    uint8_t pull_none : 1;
    uint8_t pull_down : 1;
    uint8_t pull_up : 1;
} gpio_capabilities_t;

uint32_t gpio_set(PinName pin);

int gpio_is_connected(const gpio_t *obj);

void gpio_init(gpio_t *obj, PinName pin);
void gpio_free(gpio_t *obj);
void gpio_mode(gpio_t *obj, PinMode mode);
void gpio_dir(gpio_t *obj, PinDirection direction);
void gpio_write(gpio_t *obj, int value);
int gpio_read(gpio_t *obj);
void gpio_init_in(gpio_t *gpio, PinName pin);
void gpio_init_in_ex(gpio_t *gpio, PinName pin, PinMode mode);
void gpio_init_out(gpio_t *gpio, PinName pin);
void gpio_init_out_ex(gpio_t *gpio, PinName pin, int value);
void gpio_init_inout(gpio_t *gpio, PinName pin, PinDirection direction, PinMode mode, int value);
void gpio_get_capabilities(gpio_t *gpio, gpio_capabilities_t *cap);
const PinMap *gpio_pinmap(void);

#ifdef __cplusplus
}
#endif

#endif

/** @}*/

mbed API: Class Digital In (DigitalIn.h)

DigitalIn header defines the application API designed in C++ class structure. After class construction/initiation, the methods are easy to be used by the user. Here, you don't need to specifically define and refer to the register pointer for specific digital in pins.

In Each methods, it calls the functions defined in mbed HAL_API.

mbed HAL API: gpio_api.h

Underneath the simple application API, it calls more complex, more lower level HAL API. For example, in class construction (initialization), it finds which GPIO to be applied from the Pinname, using the call back function. gpio_init_in(&gpio, pin);

Tutorial: Create EC_API - for Digital In

Lets borrow the DigitalIn class from mbed API. To eliminate any redundancy defintion of variables, we will use prefix 'EC_ ' for Class, Variable names.

Create Application API source file

Application API: EC_GPIO_API.h, EC_GPIO_API.cpp

First, create header and source file as EC_ GPIO ___API. h and EC_ GPIO ___API. cpp

we will use *.cpp, which is C++ source file

Define Application API

Use the following source code to start. ecGPIO.h is the file you have created in LAB:GPIO Dgital InOut.

Unlike mbed API, we are going to input the GPIO and the pin number for initialization.

In " EC_GPIO_API.cpp ", you can define each methods. For this tutorial, we will use only *.h header file

#include "stm32f411xe.h"
#include "ecGPIO.h"
#include "ecRCC.h"
#include <stdint.h>

#ifndef __EC_GPIO_API_H
#define __EC_GPIO_API_H

#define EC_DIN 		0

#define EC_PU 1
#define EC_PD 0
#define EC_NONE 0

#define EC_LOW 		0
#define EC_MEDIUM 1
#define EC_FAST 	2
#define EC_HIGH 	3

/* System CLOCK is HSI by default */

class EC_DigitalIn // declare class set the Application API name and declare class
{
public:
    EC_DigitalIn(GPIO_TypeDef *Port, int pin)
    // API initial seting
    {
			uint8_t mode=EC_DIN; // mode=0
			GPIO_init(Port, pin, mode);
			Port_t=Port;
			pin_t=pin;
			mode_t=mode;	
    }

    ~EC_DigitalIn()
    {
			 delete[] Port_t;
    }

    int read()
    {
				val_t = GPIO_read(Port_t, pin_t);
				return val_t;
    }
		
		void pupdr(int _pupd){
			GPIO_pudr(Port_t, pin_t, _pupd);
		}
    
    operator int()
    {
        return read();
    }

	private:
			GPIO_TypeDef *Port_t;
			int	pin_t;
			int mode_t;	
			int val_t;	
};
#include "stm32f411xe.h"

#ifndef __EC_GPIO_H
#define __EC_GPIO_H

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

#define HIGH 1
#define LOW  0

void GPIO_init(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_mode(GPIO_TypeDef* Port, int pin, int mode);
void GPIO_ospeed(GPIO_TypeDef* Port, int pin, int speed);
void GPIO_otype(GPIO_TypeDef* Port, int pin, int type);
void GPIO_pudr(GPIO_TypeDef* Port, int pin, int pudr);

#endif

Use Application API

Lets compare the simple code based on 'EC_HAL' vs EC API'.

/**
******************************************************************************
* @author  SSSLAB
* @Mod		 2021-8-12 by YKKIM  	
* @brief   Embedded Controller:  LAB Digital In/Out
*					 - Toggle LED LD2 by Button B1  pressing
* 
******************************************************************************
*/

#include "stm32f4xx.h"
#include "ecGPIO.h"
#include "ecRCC.h"

#define LED_PIN 	5
#define BUTTON_PIN 13

void setup(void);
	
int main(void) { 
	// Initialiization --------------------------------------------------------
	setup();
	
	// Inifinite Loop ----------------------------------------------------------
	while(1){
		if(GPIO_read(GPIOC, BUTTON_PIN) == 0)	GPIO_write(GPIOA, LED_PIN, HIGH);
		else 																	GPIO_write(GPIOA, LED_PIN, LOW);
	}
}


// Initialiization 
void setup(void)
{
	RCC_HSI_init();	
	GPIO_init(GPIOC, BUTTON_PIN, INPUT);  // calls RCC_GPIOC_enable()
	GPIO_init(GPIOA, LED_PIN, OUTPUT);    // calls RCC_GPIOA_enable()
}
/**
******************************************************************************
* @author  SSSLAB
* @Mod		 2021-8-12 by YKKIM  	
* @brief   Embedded Controller:  LAB Digital In/Out with API
*					 - Toggle LED LD2 by Button B1  pressing
* 
******************************************************************************
*/

#include "EC_GPIO.h"

#define LED_PIN 	5
#define BUTTON_PIN 13

EC_DigitalIn button(GPIOC,BUTTON_PIN);
EC_DigitalOut led(GPIOA,LED_PIN);

	
int main(void) { 
	// Initialiization --------------------------------------------------------

	// Inifinite Loop ----------------------------------------------------------
	while(1){
	
		if(!button)	led=1;
		else 				led=0;
	}
}

Exercise: Create EC_API - for Digital Out

Lets borrow the Digital Out class from mbed API. To eliminate any redundancy defintion of variables, we will use prefix 'EC_ ' for Class, Variable names.

Download source file

Rename the files as 'EC_GPIO_API.cpp ' and 'EC_GPIO_API.h'

Define Application API

Complete the definition of the class EC_DigitiOut.

You need to define the function in "EC_GPIO_API.cpp"

Then, run the following code.

#include "EC_GPIO_API.h"

#define LED_PIN 	5
#define BUTTON_PIN 13

EC_DigitalIn button(GPIOC,BUTTON_PIN);
EC_DigitalOut led(GPIOA,LED_PIN);

	
int main(void) { 
	// Initialiization --------------------------------------------------------

	// Inifinite Loop ----------------------------------------------------------
	while(1){
		if(button.read() == 0)	led.write(HIGH);
		else 										led.write(LOW);
		//if(!button)	led=1;
		//else 				led=0;
		

	}
}

Download the source files:

EC_GPIO_API_student.h, EC_GPIO_API_student.cpp
Structure of mbed-os