This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/CC2640R2F: ON/OFF Led of CC2640 with timer

Part Number: CC2640R2F
Other Parts Discussed in Thread: CC2640

Tool/software: TI-RTOS

I'm implementing a small example with my cc2640 and I miss a little to complete it. This is an interrupt with the leds, but with a variation. Through an input (so if 1/0) the leds must light up in a certain way. In practice if
the input of the DIO20 is 1 ... I click the button and the LED turns on / off each time the button is pressed. If the input is 0, I click the button and the LED should light up for 1 second and it should go off by itself. This is my code, but it does not work yet.

/*
 *  ======== pinInterrupt.c ========
 */
#include <unistd.h>

/* Driver Header files */
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerCC26XX.h>
#include <ti/drivers/PIN.h>
#include <ti/drivers/pin/PINCC26XX.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/BIOS.h>

/* Example/Board Header files */
#include "Board.h"

#define TIMER_TASK_STACK_SIZE 512
Task_Struct tasktimer0Struct;
Char tasktimer0Stack[TIMER_TASK_STACK_SIZE];

static Task_Params timerParams;
static void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId);

void timer_init()
{

    Task_Params_init(&timerParams);
    timerParams.arg0 = 1000000 / Clock_tickPeriod;
    timerParams.stackSize = TIMER_TASK_STACK_SIZE;
    timerParams.stack = &tasktimer0Stack;
    Task_construct(&tasktimer0Struct, (Task_FuncPtr)buttonCallbackFxn, &timerParams, NULL);

}


/* Pin driver handles */
static PIN_Handle buttonPinHandle;
static PIN_Handle ledPinHandle;

/* Global memory storage for a PIN_Config table */
static PIN_State buttonPinState;
static PIN_State ledPinState;

/*
 * Initial LED pin configuration table
 *   - LEDs Board_PIN_LED0 is on.
 *   - LEDs Board_PIN_LED1 is off.
 */
PIN_Config relayPinTable[] = {
    Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW  | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    Board_PIN_LED2 | 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_PIN_BUTTON0    | PIN_INPUT_EN | PIN_IRQ_NEGEDGE,
    Board_PIN_BUTTON1    | PIN_INPUT_EN | PIN_IRQ_NEGEDGE,
    Board_PIN_DIO20      | PIN_INPUT_EN | PIN_IRQ_NEGEDGE,
    PIN_TERMINATE
};

/*
 *  ======== buttonCallbackFxn ========
 *  Pin interrupt Callback function board buttons configured in the pinTable.
 *  If Board_PIN_LED3 and Board_PIN_LED4 are defined, then we'll add them to the PIN
 *  callback function.
 */
void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
    uint32_t currVal = 0;

    /* Debounce logic, only toggle if the button is still pushed (low) */
    CPUdelay(8000*50);
    if((PIN_getInputValue(Board_PIN_DIO20)) == 1) {
    if (!PIN_getInputValue(pinId)) {
        /* Toggle LED based on the button pressed */
        switch (pinId) {
            case Board_PIN_BUTTON0:
                currVal =  PIN_getOutputValue(Board_PIN_LED1);
                PIN_setOutputValue(relayPinHandle, Board_PIN_LED1, !currVal);
                break;

            default:
                /* Do nothing */
                break;
        }
    }
} else if((PIN_getInputValue(Board_PIN_DIO20)) == 0) {
    if (!PIN_getInputValue(pinId)) {
            switch (pinId) {
                case Board_PIN_BUTTON0:
                    currVal = PIN_getOutputValue(Board_PIN_LED1);
                    Task_sleep(1000000/Clock_tickPeriod);
                    PIN_setOutputValue(relayPinHandle, Board_PIN_LED1, !currVal);

                    break;

                default:
                    /* Do nothing */
                    break;
  }
 }
}
}
/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
    Timer_init();

    /* Open LED pins */
    ledPinHandle = PIN_open(&ledPinState, ledPinTable);
    if(!ledPinHandle) {
        /* Error initializing board LED pins */
        while(1);
    }

    buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
    if(!buttonPinHandle) {
        /* Error initializing button pins */
        while(1);
    }

    /* Setup callback for button pins */
    if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {
        /* Error registering button callback function */
        while(1);
    }

  /* Loop forever */
    while(1) {
        sleep(1000);
    }
}