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: How to read the Input state of my device

Part Number: CC2640R2F
Other Parts Discussed in Thread: CC2640

Tool/software: TI-RTOS

Hi everyone, I'm working with my CC2640 custom board with my project. I would like to read the Input State, then 0/1, of port serial. How can I to make this step?

I'm new in this work with TIRTOS

Thanks

  • You can try to refer to pin interrupt example at dev.ti.com/.../
  • Hi , I try Pin Interrupt with my exemple. This is my part of code, but I not see nothing on uart.

    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
    uint8_t buf[4];


    /* Debounce logic, only toggle if the button is still pushed (low) */
    CPUdelay(8000*50);
    if (!PIN_getInputValue(pinId)) {
    switch (pinId) {
    case Board_PIN_INPUT1:
    if (PIN_getInputValue(Board_PIN_INPUT1) != ing1)
    {
    ing1 = PIN_getInputValue(Board_PIN_INPUT1);
    buf[4] = ing1;
    UART_write(uart, buf, 4);
    }
    break;

    case Board_PIN_INPUT2:
    if (PIN_getInputValue(Board_PIN_INPUT2) != ing2)
    {
    ing2 = PIN_getInputValue(Board_PIN_INPUT2);
    buf[4] = ing2;
    UART_write(uart, buf, 4);
    }
    break;
    }
    }
    }
  • Do you set a breakpoint on UART_write and see if the breakpoint is hitted?
  • Also take a look at the uartecho example to see how the uart works:

    dev.ti.com/.../

    Siri
  • Yes, the breakpoint is hitted. The my goal is to read the value 1/0 of input on my serial. But in this moment I not read nothing. 

    This is my part of code now:

    int ing1 = 1;
    int ing2 = 1;
    
    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
    uint8_t buf[4];
    
    
    /* Debounce logic, only toggle if the button is still pushed (low) */
    CPUdelay(8000*50);
    if (!PIN_getInputValue(pinId)) {
    switch (pinId) {
    case Board_PIN_INPUT1:
    if (PIN_getInputValue(Board_PIN_INPUT1) != ing1) 
    {
    ing1 = PIN_getInputValue(Board_PIN_INPUT1);
    buf[4] = ing1;
    UART_write(uart, buf, 4);
    }
    break;
    
    case Board_PIN_INPUT2:
    if (PIN_getInputValue(Board_PIN_INPUT2) != ing2) 
    {
    ing2 = PIN_getInputValue(Board_PIN_INPUT2);
    buf[4] = ing2;
    UART_write(uart, buf, 4);
    }
    break;
    }
    }
    }

  • I suppose UART_write would output to UART in hex format so do you use a terminal tool that can display in hex format?
  • I don't see nothing in exadecimal and nothing in text mode
  • Can you use CCS watch window to see what inside buf[4] before UART_write?
  • I made a small example that hopefully will help you.

    In the example, you will get a callback on falling edge of Board_PIN_BUTTON0.

    In the callback the state of Board_PIN_BUTTON1 is checked. If it is 1 (button NOT being pushed), the UART output 1. If it is 0 (button being pushed) it outputs a 0- The code is below:

    #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/drivers/UART.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    /* Pin driver handles */
    static PIN_Handle buttonPinHandle;
    static UART_Handle uart;
    
    /* Global memory storage for a PIN_Config table */
    static PIN_State buttonPinState;
    
    /*
     * Application button pin configuration table:
     *   - Button interrupt are configured to trigger on falling edge.
     */
    PIN_Config buttonPinTable[] = {
        Board_PIN_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
        Board_PIN_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_DIS,
        PIN_TERMINATE
    };
    
    /*
     *  ======== buttonCallbackFxn ========
     */
    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
        char input;
    
        /* Debounce logic */
        CPUdelay(8000*50);
    
        if ((PIN_getInputValue(Board_PIN_BUTTON1)) == 0)
        {
            input = '0';
            UART_write(uart, &input, 1);
        }
        else
        {
            input = '1';
            UART_write(uart, &input, 1);
        }
    }
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        UART_Params uartParams;
    
        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);
        }
    
        UART_init();
    
        /* Create a UART with data processing off. */
        UART_Params_init(&uartParams);
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.readReturnMode = UART_RETURN_FULL;
        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.baudRate = 115200;
    
        uart = UART_open(Board_UART0, &uartParams);
    
        if (uart == NULL) {
            /* UART_open() failed */
            while (1);
        }
    
        /* Loop forever */
        while(1) {
            sleep(1000);
        }
    }
    

    Siri

  • Hi Siri, thanks for your help. But I have the problem. Instead of button, I using the relay and then I want to see the state input if 1 or 0. In this moment I have change the BOARD_PIN_Button with my Pinid. When I go on the serial, I not see nothing. I see 1 or 0 when I measure with the multimeter the PIN of relay. Why?
  • If you debug your code, can it read correct relay status from your GPI?
  • When I click on debug I see this error:

    Error connecting to the target:
    (Error -260 @ 0x0)
    An attempt to connect to the XDS110 failed.
    The cause may be one or more of: no XDS110 is connected, invalid
    firmware update, invalid XDS110 serial number, or faulty USB
    cable. The firmware and serial number may be updated using the
    xdsdfu utility found in the .../ccs_base/common/uscif/xds110

    The my device is connet with JTAG cable to SmartRF
  • Can Flash Programmer 2 recognize your board?
  • Try to restart your desktop and CCS to try debugging again.
  • Try to replace an USB cable.
  • Ok I Try, one question Yikai. I would like to pass the value of state input on my APP. I following the button_service for to make this. It's correct?
  • Yes, that’s correct.
  • For to pass the value 0 or 1 of my State Input is how the ButtonState?
  • I couldn't understand your latest question.
  • The my goal, is to see the Input state on my APP. I following the ButtonState how example. I create two new file (input_service.c/.h) and I modify the project-zero example. For the passage of 1/0 to my APP, is it okay as I changed my codes?
  • I see no probelm on this.
  • I try the my APP, but I don't see the state of my Input. On the serial of my device when I connected, not talk the input_state. Do you sure that If I following the button example state for my input_state it's works?
  • It works!! Thanks for all!
  • Yikai, one question. If I want to use project-zero BLE5, Why I not see the part of security on project-zero code?
  • What do you mean “I not see the part of security on project-zero code?”
  • I not see the BLE Bond Manager initialization on project-zero.c BLE5 Stack. Why?
  • I can see BLE5 project-zero enable bonding in ProjectZero_init by the following code which is the same as BLE project-zer0.

    GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof(uint8_t),
    &bonding);
  • Hi Yikai, I writing a small code for the interrupt of led with timer this is my code, but the timer don't work. Do you give me a some advice?

    #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);
    }
    }
  • I couldn’t understand your question.
  • I want the 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. The code is that I attached here
  • When you press button, is your buttonCallbackFxn triggered?
  • I want the timer in the second if (DIO20 == 0). See the photo?

     

    #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 Temporizzatore_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 relayPinHandle;
    
    /* Global memory storage for a PIN_Config table */
    static PIN_State buttonPinState;
    static PIN_State relayPinState;
    
    /*
     * Initial LED pin configuration table
     *   - LEDs Board_PIN_LED0 is on.
     *   - LEDs Board_PIN_LED1 is off.
     */
    PIN_Config relayPinTable[] = {
        Board_PIN_RELAY1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW  | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        Board_PIN_RELAY2 | 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_PULSANTE0  | PIN_INPUT_EN | PIN_IRQ_NEGEDGE,
        Board_PIN_BUTTON1    | PIN_INPUT_EN | PIN_IRQ_NEGEDGE,
        Board_PIN_BIS        | 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;
        int k;
    
        /* Debounce logic, only toggle if the button is still pushed (low) */
        CPUdelay(8000*50);
        if((PIN_getInputValue(Board_PIN_BIS)) == 1) {
        if (!PIN_getInputValue(pinId)) {
            /* Toggle LED based on the button pressed */
            switch (pinId) {
                case Board_PIN_PULSANTE0:
                    currVal =  PIN_getOutputValue(Board_PIN_RELAY1);
                    PIN_setOutputValue(relayPinHandle, Board_PIN_RELAY1, !currVal);
                    break;
    
                default:
                    /* Do nothing */
                    break;
            }
        }
    } else if((PIN_getInputValue(Board_PIN_BIS)) == 0) {
        if (!PIN_getInputValue(pinId)) {
                switch (pinId) {
                    case Board_PIN_PULSANTE0:
                        currVal = PIN_getOutputValue(Board_PIN_RELAY2);
                        Task_sleep(1000000/Clock_tickPeriod);
                        PIN_setOutputValue(relayPinHandle, Board_PIN_RELAY2, !currVal);
    
                        break;
    
                    default:
                        /* Do nothing */
                        break;
      }
     }
    }
    }
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        Temporizzatore_init();
    
        /* Open LED pins */
        relayPinHandle = PIN_open(&relayPinState, relayPinTable);
        if(!relayPinHandle) {
            /* 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);
        }
    }

     

  • Can you answer my previous question “When you press button, is your buttonCallbackFxn triggered?”
  • I suppose you can set breakpoint in buttonCallbackFxn to debug/trace why your code doesn’t work.
  • The code it works. I don't know because not function the timer in the second IF DIO20 == 0
  • Does it hit “Task_sleep(1000000/Clock_tickPeriod);” if you set breakpoint?
  • No, I should to put something for on/off led for 2 second.
  • Does your answer means it cannot detect DIO20 == 0?
  • No, I enter in the if DIO20==0 and it works. The my goal is that enter in the if and I press the button, the led is on and after 1 sec goes off
  • I suppose you should call PIN_setOutputValue to turn on LED inside “if DIO20==0” and turn it off 1 second later.
  • Isn't it better to enter a timer? A two-second clock?
  • I think it’s easier to call PIN_setOutputValue to turn on LED inside “if DIO20==0” and start a timer event to turn it off 1 second later.
  • Ok, How can I make this event to turn off 1 second later?
  • You can refer to siple_perpheral example whcih show you how to use Util_constructClock to setup SBP_PERIODIC_EVT.
  • I don't understand how to use this Util_costructClock. Can you show me how to insert in the second if (DIO20==0) this clock?
  • As I had suggested in my previous reply, simple_peripheral already shows you how to do it.
  • But isn't it better with a clock? I don't understand how to use in simple_pheripheral