Tutorial: Creating Application API

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.

Structure of mbed-os

Comparison mbed API vs EC API

mbed API example code

Example code for Digital In and Out using mbed

EC API example code

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

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 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

Use Application API

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

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

Download the source files: EC_GPIO_API_student.h, EC_GPIO_API_student.cpp

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.

Last updated

Was this helpful?