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/CC1310: Code working using pthread calls but not rtos calls

Part Number: CC1310


Tool/software: TI-RTOS

Hello,

I have a code working using pthread call but I tried switching to rtos call structure and the code compiles with no errors/warnings but the code no longer works.

Below is the main function using pthread calls

#include <pthread.h>
#include <stdint.h>

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

/* RTOS header files */
#include <ti/sysbios/BIOS.h>

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

#include "get_data.h"

/* Stack size in bytes */
#define THREADSTACKSIZE    2048

int main(void)
{
    pthread_t           thread;
    pthread_attr_t      attrs;
    struct sched_param  priParam;
    int                 retc;
    int                 detachState;

    // Call driver init functions
    Board_initGeneral();

    // Set priority and stack size attributes
    pthread_attr_init(&attrs);
    priParam.sched_priority = 1;

    detachState = PTHREAD_CREATE_DETACHED;
    retc = pthread_attr_setdetachstate(&attrs, detachState);
    if (retc != 0) {
        // pthread_attr_setdetachstate() failed
        while (1);
    }

    pthread_attr_setschedparam(&attrs, &priParam);

    retc |= pthread_attr_setstacksize(&attrs, THREADSTACKSIZE);
    if (retc != 0) {
        // pthread_attr_setstacksize() failed
        while (1);
    }

    retc = pthread_create(&thread, &attrs, get_data_Thread, NULL); // This throws a warning...
    if (retc != 0) {
        // pthread_create() failed
        while (1);
    }

    BIOS_start();

    return (0);
}

And here is it using rtos commands

#include <stdint.h>

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

/* RTOS header files */
#include <ti/sysbios/BIOS.h>

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

#include "get_data.h"

#define TASK_STACK_SIZE 2048
uint8_t appTaskStack[TASK_STACK_SIZE];
Task_Struct task0;


int main() {

    Board_initGeneral();

    Task_Params taskParams;

    // Configure task
    Task_Params_init(&taskParams);
    taskParams.stack = appTaskStack;
    taskParams.stackSize = TASK_STACK_SIZE;
    taskParams.priority = 0;

    Task_construct(&task0, (Task_FuncPtr)get_data_Thread, &taskParams, NULL);

    BIOS_start();

    return 0;
}

Any help is greatly appreciated

  • Hello Matt,

    please show your get_data_Thread() and get_data_Task().
    How did you created your TI-RTOS project?
  • Hello,

    Here is my get_data_thread() but I do not have a task unless it is already in the code, (sorry I am still new to this)

    NOTE: I am using this code for both versions (with pthread and with RTOS)

    #include <stdint.h>
    #include <stdio.h>
    #include <stddef.h>
    #include <ti/sysbios/knl/Clock.h>
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/gpio/GPIOCC26XX.h>
    
    /* POSIX Header files */
    // #include <pthread.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    #include "get_data.h"
    
    // #include <ti/devices/DeviceFamily.h>
    // #include DeviceFamily_constructPath(startup_files/ccfg.c)
    
    // Defines
    #define FLOW_DELAY 250 // Time device waits for flow to stop (ms)
    #define FLOW_CONST 1.0
    
    char my_id[15] = "0bC3eF6hI9";
    
    // Global Variables
    int need_send_data = 0;
    int need_do_flow_calcs = 1;
    int do_reset = 0;
    unsigned long int num_rising_edges = 0;
    unsigned long int old_time = 0.0;
    double running_volume_removed = 0.0;
    double volume_removed_sending = 0.0; // DEBUG INFO
    
    void get_data_Thread(void *arg0)
    {
        // Call GPIO driver init function
        GPIO_init();
    
        // Set interrupt function
        GPIO_setCallback(Board_GPIO_BUTTON0, gpioButtonFxn0);
        GPIO_setCallback(Board_GPIO_BUTTON1, gpioButtonFxn1);
    
        // Enable interrupts
        GPIO_enableInt(Board_GPIO_BUTTON0);
        GPIO_enableInt(Board_GPIO_BUTTON1);
    
        int error_true = 0;
    
        while(1){
            if(!do_flow_calcs())
                error_true = 1;
    
            if (need_send_data)
                if(!send_data())
                    error_true = 1;
            if(error_true)
            {
                break;
            }
    
            if (do_reset)
                reset_calcs();
        }
    
    }
    
    void update_total_removed(double cur_flow)
    {
        long double ntime = mills();
    
        // We no longer need to know the dt
        // since the same dt is also used to
        // convert number of rising edges to
        // the frequency of the device
        // HERTZ2FLOW*(NumRisingEdges/dt)*dt
        running_volume_removed += cur_flow;
    
        old_time = ntime;
    }
    
    int do_flow_calcs(void)
    {
        if (num_rising_edges > 0)
            need_do_flow_calcs = 1;
    
        if(need_do_flow_calcs) // Check if we need to do a calculation (TRIGGER EVENT)
        {
            double cur_flow;
            do
            {
                old_time = mills();
                while(mills() - old_time < FLOW_DELAY)
                {
                    // DO NOTHING BUT READ THE FLOW!!!
                }
                cur_flow = read_flow(); // read in current flow rate
    
                update_total_removed(cur_flow);
            } while(cur_flow > 0); // do this until the tap is done being used
    
            need_do_flow_calcs = 0;
    
            need_send_data = 1; // Update the server
    
            return 1;
        }
        else
        {
            return 1;
        }
    }
    int send_data(void)
    {
        unsigned long int cur_time = mills();
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
    
        // double send_volume_removed = running_volume_removed;
        running_volume_removed = 0.0;
    
        while (mills() - cur_time < 2500)
        {
            // DO NOTHING!!!
            // Actual thing is to send volume update to other thread
        }
    
        need_send_data = 0;
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);
    
        return 1;
    }
    
    double hertz2flow(double hertz)
    {
        const double m = FLOW_CONST/60.0;
        return m*hertz; // Liters per second
    }
    
    double read_flow()
    {
        // Read in the sensor data
        double flow = get_flow_data();
        return hertz2flow(flow);
    }
    
    double get_flow_data(void)
    {
        double val = (double)num_rising_edges;
        num_rising_edges = 0;
        return val;
    }
    
    void reset_calcs(void)
    {
        running_volume_removed = 0.0;
        do_reset = 0;
        need_send_data = 1;
        volume_removed_sending = 0.0; // DEBUG INFO
        GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_OFF); // DEBUG INFO
    }
    
    unsigned long int mills(void)
    {
        const unsigned long int m_CLOCK_FREQ_micro = Clock_tickPeriod;
        unsigned long int temp = ((unsigned long int)Clock_getTicks()*m_CLOCK_FREQ_micro)/1000;
        return temp;
    }
    
    void gpioButtonFxn0(uint_least8_t index)
    {
        // We have flow !!!
        ++num_rising_edges;
    }
    
    void gpioButtonFxn1(uint_least8_t index)
    {
        // Need to reset the board
        do_reset = 1;
    }

  • Any reason you want to remove the POSIX calls?

    See this for examples without POSIX you can take a look at: e2e.ti.com/.../681486

    Also see training.ti.com/simplelink-academy-cc13x0-sdk
  • Hello,

    Thank you for the references.

    I do not have a preference to the style, as long as it accomplishes what I want in the end.
    We have a user who is going through the manual and it uses the rtos style.

    The end goal is to have one thread handle data collection while another thread handles
    sending the collected data to our gateway and that will go to a central server.

    The idea is to capture flow data. After flow has stopped for a period of time, hand over how much volume
    has passed the flow sensor to the other thread and send it to the server, and reset the volume value in the first thread.
  • Which manual are you referring to? I believe most examples under dev.ti.com/.../ should use POSIX now.
  • He found it in the Documentation inside the /ti directory for the simplex cc13xx sdk. There was a document folder that contained rtos information. It is also where I found a code template that I modeled my RTOS code on. If we should not be using this, that is fine. But what documentation should be used to accomplish my goals?
  • The only difference is how to create a task. I would assume it's easier to use posix since this is used in all examples. Hence the easiest would hopefully be to start from the empty example in the SDK and add your code to this according to your needs.