zoukankan      html  css  js  c++  java
  • TI-RTOS 之 GPIO中断(按键)

    TI-RTOS GPIO中断(按键)

    前面已经用过LED, 定时器,这次来了解GPIO的中断是怎么用的,从CC1310+TI-RTOS的例程可以直接找到相应的例子程序,它的关键是在于要使能中断,也就是在引脚配置时要选择方向为输入(PIN_INPUT_EN), 中断选项为(PIN_IRQ_NEGEDGE)下降沿有效。代码如下:

    /*

     * 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

    };

    在初始化之后,还要注册中断回调函数,应用TI-RTOS就不能再去写中断函数了,而使用回调函数,这个回调函数就相当于中断函数的一个子函数了。它的写法是:

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

     * @fn      buttonCallbackFxn

     *

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

     *

     * @param   PIN_Handle handle, PIN_Id pinId

     *

     * @return  void

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

    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId)

    {

      uint_t ledState;

      

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

        CPUdelay(8000*50);

        

        // 延时之后再次读 IO, 仍然为低则按键有效

        if (!PIN_getInputValue(pinId))

        {

          // 不同的按键控制不同的灯

          if ( pinId == Board_BUTTON0 )

          {

            ledState = PIN_getOutputValue(Board_LED0);

            PIN_setOutputValue(ledPinHandle, Board_LED0, !ledState);

          }

          else if ( pinId == Board_BUTTON1 )

          {

            ledState = PIN_getOutputValue(Board_LED1);

            PIN_setOutputValue(ledPinHandle, Board_LED1, !ledState);

          }      

        }

    }

    这里将按键的触发和执行都写在回调函数了,看起来比较简单些,在实际应用中,可能是得到按键键值并传递到应用的任务或者菜单任务。

    最后是演示任务的整体代码:

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

    Filename:       keyDemo.c

    Editor:         Tome @ newbit

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

    Revision:       $Revision: 00001 $

    Description:    了解 TI-RTOS的使用之,IO中断【按键】

    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>

    /* 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;

    /*

     * 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)

    {

       

      

      // 这里不是为了初始化,而是为了拿到操作的句柄 (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)

      {    

        // 任务休眠 1 秒,  1000000us, 下面函数的单位是10us

        Task_sleep(100000);

      }

      

    }

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

     * @fn      buttonCallbackFxn

     *

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

     *

     * @param   PIN_Handle handle, PIN_Id pinId

     *

     * @return  void

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

    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId)

    {

      uint_t ledState;

      

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

        CPUdelay(8000*50);

        

        // 延时之后再次读 IO, 仍然为低则按键有效

        if (!PIN_getInputValue(pinId))

        {

          // 不同的按键控制不同的灯

          if ( pinId == Board_BUTTON0 )

          {

            ledState = PIN_getOutputValue(Board_LED0);

            PIN_setOutputValue(ledPinHandle, Board_LED0, !ledState);

          }

          else if ( pinId == Board_BUTTON1 )

          {

            ledState = PIN_getOutputValue(Board_LED1);

            PIN_setOutputValue(ledPinHandle, Board_LED1, !ledState);

          }      

        }

    }

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

    Copyright 2016 Newbit Studio. All rights reserved.

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

  • 相关阅读:
    innobackupex备份命令输出
    Percona XtraBackup原理详解
    MongoDB性能分析工具mongostat
    MongoDB查看当前连接数
    事务、拦截器
    HttpServletResponse和HttpServletRequest的简单实用
    Maven环境配置
    SQL Server 时间戳与时间格式互相转换
    虚拟机、云主机、VPS 三者之间的区别
    Elasticsearch 空值过滤
  • 原文地址:https://www.cnblogs.com/newbit/p/ti_rtos_int.html
Copyright © 2011-2022 走看看