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.
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#defineEC_DIN0#defineEC_PU1#defineEC_PD0#defineEC_NONE0#defineEC_LOW0#defineEC_MEDIUM1#defineEC_FAST2#defineEC_HIGH3/* System CLOCK is HSI by default */classEC_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=0GPIO_init(Port, pin, mode);Port_t=Port;pin_t=pin;mode_t=mode; }~EC_DigitalIn() {delete[]Port_t; }intread() {val_t=GPIO_read(Port_t,pin_t);returnval_t; }voidpupdr(int _pupd){GPIO_pudr(Port_t,pin_t, _pupd); }operator int() {returnread(); }private: GPIO_TypeDef *Port_t;intpin_t;intmode_t; intval_t; };
/********************************************************************************* @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"#defineLED_PIN5#defineBUTTON_PIN13EC_DigitalInbutton(GPIOC,BUTTON_PIN);EC_DigitalOutled(GPIOA,LED_PIN);intmain(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.