STM32L例程02--GPIO输出控制LED状态切换(基于cola_os)

一、实验目的

1.实现GPIO口的输出。

2.控制LED状态切换,500ms闪烁一次。

一、实验步骤

1.CubeMax配置

配置PB1为输出,修改Lable为LED,导出工程。

2.LED驱动创建

stm32_led.c

#include "config.h"
#include "stm32l0xx_hal.h"
#include "stm32_led.h"
#include "gpio.h"
#include "cola_os.h"
#include "cola_device.h"


#ifdef USING_GPIO_TOGGLE

#define LED_GREEN_ON                  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET)
#define LED_GREEN_OFF                 HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET)
#define LED_GREEN_TOGGLE              HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin)


static cola_device_t led_dev;

static void led_gpio_init(void)
{
    MX_GPIO_Init();
}

static int led_ctrl(cola_device_t *dev, int cmd, void *args)
{
    if(LED_TOGGLE == cmd)
    {
        LED_GREEN_TOGGLE;
    }
    else  if(LED_OFF == cmd)
    {
        LED_GREEN_OFF;
    }
    else
    {
        LED_GREEN_ON;
    }
    return 1;
}


static struct cola_device_ops ops =
{
    .control = led_ctrl,
};

static void led_register(void)
{
    led_gpio_init();
    led_dev.dops = &ops;
    led_dev.name = "led";
    cola_device_register(&led_dev);
}

device_initcall(led_register);
#endif

3.gpio输出应用

app_gpio_toggle.c

#include "cola_device.h"
#include "cola_os.h"
#include "example.h"


#ifdef APP_GPIO_TOGGLE
static task_t timer_500ms;

static cola_device_t *app_led_dev;

//led每500ms状态改变一次
static void timer_500ms_cb(uint32_t event)
{
    cola_device_ctrl(app_led_dev,LED_TOGGLE,0);
}


void app_gpio_toggle_init(void)
{
    app_led_dev = cola_device_find("led");
    assert(app_led_dev);
    cola_timer_create(&timer_500ms,timer_500ms_cb);
    cola_timer_start(&timer_500ms,TIMER_ALWAYS,500);
}

#endif

三、代码下载

gitHub:https://github.com/sckuck-bit/stm32l0_demo

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页