This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to set up the scheduler on nRF51822

Hello Team,

I want to use scheduler to schedule two tasks i.e task_1 and task_2. The task_1 and task_2 are as follow:

void task_1() {
    printf("Task 1 ran");
}

void task_2() {
    printf("Task 2 ran");
}

Now I want to use the scheduler to schedule this task i.e to schedule task_1 after every 100ms and task_2 after every 200ms.

I am totally new to this topic and have tried to read the scheduler library from Nordic info-center but i am still not clear about how to implement this.

I have also referred the HID example but was not able to implement the above describe task.

If possible if you can provide a example it will be very helpful.

Thanks and Regards,

Juzer.T

Parents Reply Children
  • Tools:
    software: keil version 5
    SDK version: SDK8 (available with keil u5, i.e default Nordic SDK available with keil software for nRF51822)

    STEPS:
    I have used the UART example from pack installer and have added the following code to impliment my logic of printing "Task 1 ran" on completing the 15sec timer.

    #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
    #define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */
    #define SCHED_MAX_EVENT_DATA_SIZE sizeof(app_timer_event_t)
    #define SCHED_QUEUE_SIZE 10

    APP_TIMER_DEF(m_timer_id);

    //void * p_context; /**< General purpose pointer. Will be passed to the timeout handler when the timer expires. */
    static void timer_timeout_handler(void * p_context)

    {
    //Do something
    printf("Task 1 ran\n");
    }

    void function2(void)
    {
    //Stop timer if called
    app_timer_stop(m_timer_id);
    printf("fuction1 running timer stopped.\n");
    }

    void function1(void)
    {
    //Start 15s timer if called
    app_timer_start(m_timer_id, APP_TIMER_TICKS(15000, APP_TIMER_PRESCALER),NULL);
    printf("fuction1 running timer started.\n");
    }

    int main(void)
    {
    APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, true);

    uint8_t err_code = app_timer_create(&m_timer_id,
    APP_TIMER_MODE_SINGLE_SHOT,
    timer_timeout_handler);

    APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
    printf("function1.\n");
    function1();

    while (true)
    {
    app_sched_execute();
    }
    }

    my code compiled successfully and Now when I run the code, on serial monitor I got the following message:
    "function1.
    function1 running timer started."

    That's it
    My timer_timeout_handler is not executed and not printing "Task 1 ran" on serial monitor

    Now what I was thinking that when my timer of 15sec gets over it calls timer_timeout_handler and thus after 15sec it should print "Task 1 ran" but it is not.

    what I am doing wrong can you please explain.

  • Hi,

    You need to start the low freqeuncy 32KHz clock source before starting your app timer, the timer will not run without it. To start the timer you can add the following line at the beginning of main()

                   NRF_CLOCK->TASKS_LFCLKSTART = 1;

  • Hi Vidar Berg,

    I tried adding the above line to my code but still it is not working. Is their any thing else that i may be missing as i have tried a lot but still it is not working.

    Is their any issue with the sdk means i am using old sdk i.e sdk8 is this causing any issue?

  • Hi,

    Please try to use nRF5 SDK 12.3.0 instead. It's the latest version that includes support for the nRF51 series (SDKs and SoftDevices). You may also want to check the return values from your function calls to detect any errors.

    e.g.,

    uint32_t err_code = app_timer_create(&m_timer_id,
                                                                           APP_TIMER_MODE_SINGLE_SHOT,
                                                                          timer_timeout_handler);

    APP_ERROR_CHECK(err_code);

    Also, in case you have not seen it, here is a tutorial explaining how to use the app scheduler: https://devzone.nordicsemi.com/nordic/short-range-guides/b/software-development-kit/posts/scheduler-tutorial

Related