hello world: 基于setjmp协程的闪灯程序

usr_app.h:

struct usrapp_t
{
    struct vsfsm_ljmp_t ljmp;
    struct vsfsm_t sm;
    uint8_t stack[256];
    bool toggle;
};

extern struct usrapp_t usrapp;
void usrapp_srt_init(struct usrapp_t *app);

usrapp.c:

#include "vsf.h"
#include "usrapp.h"

void usrapp_led_thread(struct vsfsm_ljmp_t *ljmp);
struct usrapp_t usrapp =
{
    .ljmp.thread        = usrapp_led_thread,
    .ljmp.user_data     = &usrapp,
    .ljmp.stack         = usrapp.stack + sizeof(usrapp.stack),
};

#define USRAPP_EVT_ON1S                 VSFSM_EVT_USER
void usrapp_led_thread(struct vsfsm_ljmp_t *ljmp)
{
    struct usrapp_t *app = (struct usrapp_t *)ljmp->user_data;

    vsfhal_gpio_init(0);
    vsfhal_gpio_config_pin(0, 1, GPIO_OUTPP);
    vsftimer_create(ljmp->sm, 1000, -1, USRAPP_EVT_ON1S);

    while (1)
    {
        if (app->toggle)
            vsfhal_gpio_set(0, 1 << 1);
        else
            vsfhal_gpio_clear(0, 1 << 1);
        app->toggle = !app->toggle;
        vsfsm_ljmp_wfe(ljmp, USRAPP_EVT_ON1S);
    }
}

void usrapp_srt_init(struct usrapp_t *app)
{
    vsfsm_ljmp_init(&app->sm, &app->ljmp);
}

setjmp为选修,就自己体会了。

你可能感兴趣的:(hello world: 基于setjmp协程的闪灯程序)