middle service目录结构
└── plateform
├── fm33lc0xx
├── include
│ ├── adc_service.h
│ ├── flash_service.h
│ ├── freq_service.h
│ ├── fs_service.h
│ ├── gpio_service.h
│ ├── i2c_service.h
│ ├── memory_service.h
│ ├── rtc_service.h
│ ├── serial_service.h
│ ├── spi_service.h
│ ├── timer_service.h
│ └── wdt_service.h
├── stm32f030xx
└── stm32g0xx
├── include
├── libs
│ └── lib_stm32g0xx.a
└── src
├── adc
├── filesystem
├── flash
├── freq
├── gpio
│ └── gpio_service.c
├── i2c
├── memory
├── misc
├── rtc
├── serial
├── spi
├── timer
└── wdt
1、目录介绍
此目录主要存放各厂商驱动封装函数,通过对驱动接口的封装,实现某单一功能。
fm33lc0xx 存放复旦微fm33lc0xx驱动封装函数
stm32f030xx 存放意法半导体stm32f030xx驱动封装函数
stm32g0xx 存放意法半导体stm32g0xx驱动封装函数
include 存放所有驱动封装接口,供middle层调用
2、GPIO引脚输入输出接口封装
通过对驱动层GPIO接口封装,实现GPIO输入输出。
输入:
浮空输入
上拉输入
输出:
推挽输出
开漏输出
/* gpio转换(stm32g070rb为例)
* pin: gpio端口 pin值
* GPIOA0~15 0~15
* GPIOB0~15 16~31
* GPIOC0~15 32~47
* GPIOD0~9 48~57
*/
static GPIO_TypeDef *get_gpio_port(unsigned int pin)
{
GPIO_TypeDef *GPIOx = NULL;
int index = pin >> 4;
if(0 == index)
{
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
GPIOx = GPIOA;
}
else if(1 == index)
{
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
GPIOx = GPIOB;
}
else if(2 == index)
{
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
GPIOx = GPIOC;
}
else if(3 == index)
{
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOD);
GPIOx = GPIOD;
}
return GPIOx;
}
/// 设置gpio为输出引脚类型
int gpio_as_output_service(int pin, GPIO_PULL_SERVICE Pull_choice)
{
GPIO_TypeDef *GPIOx = get_gpio_port(pin);
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
if(GPIOx == NULL)
{
return MID_ERROR_NULL_POINTER;
}
//用到的GPIO引脚 设为输入
GPIO_InitStruct.Pin = get_gpio_pin(pin);;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = Pull_choice;
LL_GPIO_Init(GPIOx, &GPIO_InitStruct);
return MID_ERROR_OK;
}
/// 设置gpio为输入引脚类型
int gpio_as_input_service(int pin, GPIO_PULL_SERVICE Pull_choice)
{
GPIO_TypeDef *GPIOx = get_gpio_port(pin);
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
if(GPIOx == NULL)
{
return MID_ERROR_NULL_POINTER;
}
//用到的GPIO引脚 设为输入
GPIO_InitStruct.Pin = get_gpio_pin(pin);
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = Pull_choice;
LL_GPIO_Init(GPIOx, &GPIO_InitStruct);
return MID_ERROR_OK;
}
/// 设置gpio为模拟输入
int gpio_as_analog_input_service(int pin, GPIO_PULL_SERVICE Pull_choice)
{
GPIO_TypeDef *GPIOx = get_gpio_port(pin);
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
if(GPIOx == NULL)
{
return MID_ERROR_NULL_POINTER;
}
//用到的GPIO引脚 设为输入
GPIO_InitStruct.Pin = get_gpio_pin(pin);
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = Pull_choice;
LL_GPIO_Init(GPIOx, &GPIO_InitStruct);
return MID_ERROR_OK;
}
获取GPIO电平状态:
/// 获取gpio的电平状态
int gpio_get_pin_service(int pin)
{
unsigned int mode = 0;
int level = 0;
mode = LL_GPIO_GetPinMode(get_gpio_port(pin), get_gpio_pin(pin));
if(mode == LL_GPIO_MODE_INPUT)
{
level = (LL_GPIO_ReadInputPort(get_gpio_port(pin)) >> (pin % 16)) & 0x1;
}
else if(mode == LL_GPIO_MODE_OUTPUT)
{
level = (LL_GPIO_ReadOutputPort(get_gpio_port(pin)) >> (pin % 16)) & 0x1;
}
return level;
}
设置GPIO电平状态
/// 设置gpio为高电平
int gpio_set_pin_service(int pin)
{
LL_GPIO_SetOutputPin(get_gpio_port(pin), get_gpio_pin(pin));
return MID_ERROR_OK;
}
/// 设置gpio为低电平
int gpio_clear_pin_service(int pin)
{
LL_GPIO_ResetOutputPin(get_gpio_port(pin), get_gpio_pin(pin));
return MID_ERROR_OK;
}
/// 设置gpio为level电平
int gpio_set_pin_level_service(int pin, int level)
{
if(level == GPIO_LEVEL_HIGH)
{
LL_GPIO_SetOutputPin(get_gpio_port(pin), get_gpio_pin(pin));
}
else if(level == GPIO_LEVEL_LOW)
{
LL_GPIO_ResetOutputPin(get_gpio_port(pin), get_gpio_pin(pin));
}
return MID_ERROR_OK;
}