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