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

Basic questions about Nordic's style of programming.

Hello all,

In the proccess of migrating from arduino to SES IDE to work with the nrf52832 SOC and coming accros some of the examples in the SDK I'm strugglling to understand a few things I didn't came across before like in this loop:

 while (true)
{
    for (uint8_t i = 0; i < 40; ++i)
    {
        value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);

        ready_flag = false;
        /* Set the duty cycle - keep trying until PWM is ready... */
        while (app_pwm_channel_duty_set(&PWM1, 0, value) == NRF_ERROR_BUSY);

        /* ... or wait for callback. */
        while (!ready_flag);
       // APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
        nrf_delay_ms(25);
    }
}

what does the operations ? and : do?

and in other examples the main loop function seem empty with only a function to reduce power I guess putting the IC to sleep like:__WFE(); and code still manages to work for example in the SAADC example...

any explanation for that?

also I see those alot:

NRF_LOG_FLUSH(); info center def:

"Use the NRF_LOG_FLUSH() macro to ensure that all logs have been processed."

APP_ERROR_CHECK();

What are they necessary for?

Thanks for your time.

and:

Parents
  • To answer your first question, I suggest you look up what a "ternary operator" is. These are widely used in many other languages outside of C and are allowed to be used in C as well.

    The APP_ERROR_CHECK(); is a macro that basically shortcuts to a global error handling function. Be careful when using it without setting a break point inside of the default function since it contains a lot of useful information about what the error is. However, it is not a good idea to use it when you have non-debug code since you should be handling those error codes at run time.

    __WFE(); stands for Wait For Event. So yes, the chip is "asleep" until an event wakes it up. This is how such low power is achieved, the chips sleeps most of the time.

    Lastly, NRF_LOG_FLUSH(); I'm pretty sure just means that before the chip is locked up in an endless loop from APP_ERROR_CHECK();, make sure the log with the error information is passed to the developer so they know what happened. Otherwise, the lock up happens before all of the log is sent and you get either one character, or nothing, which isn't very helpful.

  • Great stuff here thanks there is the page for the "Ternary operator" for the lazy like me:www.sitesbay.com/.../c-ternary-operator

    So from what I understand this can only be used with arithmetic expression! no Boolean and that basically replaces if and else statements for efficiency and line saving.

    Another question in the WFE function: what kind of events exactly trigger it to wake up? (My guess is you can set it but what comes default?)

    Also how comes in other examples where the main loop in the main function run continue sly? or they just run once?

Reply Children
No Data
Related