• TI-RTOS 之 事件同步(Event, 类似semaphore)


    TI-RTOS 之 事件同步(Event, 类似semaphore)

    Event 是类似Semaphore的存在,官方如下描述:

    SYS/BIOS events are a means of communication between Tasks and other threads such as Hwis, Swis, and other Tasks, or between Tasks and other SYS/BIOS objects. Other SYS/BIOS objects include semaphores, mailboxes, message queues, etc. Only tasks can wait for events; whereas tasks, Hwis, Swis, or SYS/BIOS objects can post them.

    重点是它可用来处理任务间,任务与中断间同步。主要的API有以下:

     

    此前我们作GPIO中断演示时,曾在中断中为了消抖而非常勉强地使用一个函数 CPUdelay(), 这样可以实现我们的目标,不过CPU仍然要进行计算,而且是在中断之中,有可能会影响到其他Task的执行时间。同样也会增加系统的功耗。如果使用Event就能解决这类问题。

    思路 是这样, 用一个任务专门来处理按键,在没有按键时,任务处于Sleep状态,当按键按下时,它的中断会唤醒这个任务,唤醒的方法就是使用Event, 唤醒之后,可以再次Sleep一小段时间,用于消抖,然后再去读按键的状态,之后再进行处理。

    完整的演示代码如下:

    /**************************************************************************************************

    Filename:       keyDemo2.c

    Editor:         Tome @ newbit

    Revised:        $Date: 2016-8-11 11:20:02 +0800  $

    Revision:       $Revision: 00001 $

    Description:    了解 TI-RTOS的使用之,Event的使用,它用来同步任务,或者

                     Task - Hwis, Swis

    History:        

    Notes:          要了解到这部分的接口,可阅读TI文档

                    1. TI-RTOS 2.20  User's Guide.pdf

                    2. Bios User Guide.pdf

     硬件平台  CC1130_LAUNCHPAD Rev1.3

    **************************************************************************************************/

    /**************************************************************************************************

    // INCLUDES

    **************************************************************************************************/

    /* XDCtools Header files */

    #include <xdc/std.h>

    #include <xdc/runtime/System.h>

    /* BIOS Header files */

    #include <ti/sysbios/BIOS.h>

    #include <ti/sysbios/knl/Task.h>

    #include <ti/sysbios/knl/Event.h>

    /* TI-RTOS Header files */

    #include <ti/drivers/PIN.h>

    #include "Board.h"

    /**************************************************************************************************

    // TYPEDEF

    **************************************************************************************************/

    /**************************************************************************************************

    // CONSTANTS

    **************************************************************************************************/

    #define TASKSTACKSIZE     768

    /**************************************************************************************************

    // LOCAL VERIABLE

    **************************************************************************************************/

    Task_Struct keyTaskStruct;

    Char keyTaskStack[TASKSTACKSIZE];               // 本任务的栈空间,静态分配

    /* Global memory storage for a PIN_Config table */

    static PIN_State ledPinState;

    static PIN_State buttonPinState;

    PIN_Handle ledPinHandle;

    PIN_Handle buttonPinHandle;

    // 新建 Event, 它用来通知任务,按键已经按下

    Event_Struct evtStruct;

    Event_Handle evtHandle;

    /*

     * Initial LED pin configuration table

     *   - LEDs Board_LED0 is on.

     *   - LEDs Board_LED1 is off.

     */

    PIN_Config ledPinTable[] = {

        Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,

        Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW  | PIN_PUSHPULL | PIN_DRVSTR_MAX,

        PIN_TERMINATE

    };

    /*

     * Application button pin configuration table:

     *   - Buttons interrupts are configured to trigger on falling edge.

     */

    PIN_Config buttonPinTable[] = {

        Board_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,

        Board_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,

        PIN_TERMINATE

    };

    /**************************************************************************************************

    // FUNCTIONS DECLERATION

    **************************************************************************************************/

    Void keyFxn(UArg arg0, UArg arg1);

    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId);

    /**************************************************************************************************

    // FUNCTIONS

    **************************************************************************************************/

    /**************************************************************************************************

     * @fn      keyTaskAdd

     *

     * @brief   

     *

     * @param   void

     *

     * @return  void

     **************************************************************************************************/

    void keyTaskAdd(void)

    {

      Task_Params taskParams;

      

      

      /* Construct BIOS objects */

        Task_Params_init(&taskParams); // 创建任务所要的参数,都设置为默认值

        taskParams.stackSize = TASKSTACKSIZE; // 栈空间

        taskParams.stack = &keyTaskStack;     // 栈地址

        // bios 传递参数,建立控制灯的任务

        Task_construct(&keyTaskStruct, (Task_FuncPtr)keyFxn, &taskParams, NULL);

    }

    /*

     *  ======== keyFxn ========

     *  Task for this function is created statically. See keyTaskAdd().

     */

    Void keyFxn(UArg arg0, UArg arg1)

    {

      uint_t ledState;

      

      Event_Params evtParams;

      Event_Params_init(&evtParams);

      Event_construct(&evtStruct, &evtParams);

      

      evtHandle = Event_handle(&evtStruct);

      

      // 这里不是为了初始化,而是为了拿到操作的句柄 (handle)

      // 函数说明:Allocate one or more pins for a driver or an application.

      ledPinHandle = PIN_open(&ledPinState, ledPinTable);

      if(!ledPinHandle) {

        System_abort("Error initializing board LED pins ");

      }    

      

      // 得到按键IO的操作句柄

      buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);

      if(!buttonPinHandle) {

        System_abort("Error initializing button pins ");

      }    

      

      /* Setup callback for button pins */

      // 注册按键的中断回调函数

      if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {

        System_abort("Error registering button callback function");

      }

      

      //

      while(1)

      {    

        // 任务将在这里阻塞,直到有键按下

        Event_pend(evtHandle,Event_Id_00, Event_Id_NONE, BIOS_WAIT_FOREVER );

        

        // 任务休眠 20 ms 用于消抖

        /* Debounce logic, only toggle if the button is still pushed (low) */

        Task_sleep(20*100);

        

        // 读取按键的状态,并控制灯

        if ( !PIN_getInputValue(Board_BUTTON0))

        {

            ledState = PIN_getOutputValue(Board_LED0);

            PIN_setOutputValue(ledPinHandle, Board_LED0, !ledState);

        }

        if ( !PIN_getInputValue(Board_BUTTON1))

        {

            ledState = PIN_getOutputValue(Board_LED1);

            PIN_setOutputValue(ledPinHandle, Board_LED1, !ledState);

        }

        

      }

      

    }

    /**************************************************************************************************

     * @fn      buttonCallbackFxn

     *

     * @brief   按键中断的回调函数

     *

     * @param   PIN_Handle handle, PIN_Id pinId

     *

     * @return  void

     **************************************************************************************************/

    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId)

    {

      // 解除任务的阻塞

      Event_post(evtHandle, Event_Id_00 );

    }

    /**************************************************************************************************

    Copyright 2016 Newbit Studio. All rights reserved.

    **************************************************************************************************/

  • 相关阅读:
    其他功能
    FastApi内容总结
    请求与响应
    django工程搭建
    模板
    React Hooks 监听浏览器宽高
    使用Selenium+CefSharp撸一个BOSS自动打招呼和回复功能(一)
    前端处理后端传来json
    ros
    发布消息
  • 原文地址:https://www.cnblogs.com/newbit/p/ti_rtos_event.html
Copyright © 2020-2023  润新知