Пример #1
0
void transport_control_timeslot_begin(uint64_t global_timer_value)
{
    uint32_t aa;
    uint32_t error_code = rbc_mesh_access_address_get(&aa);
    APP_ERROR_CHECK(error_code);

    radio_init(aa);

    step_timer_index = 0xFF;

    global_time = global_timer_value;

    order_search();
    transport_control_step();

}
Пример #2
0
/**
* @brief Radio signal callback handler taking care of all signals in searching 
*   mode
*/
static nrf_radio_signal_callback_return_param_t* radio_signal_callback(uint8_t sig)
{
    g_ret_param.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_NONE;
    g_is_in_callback = true;
    static uint32_t requested_extend_time = 0;
    static uint32_t successful_extensions = 0;
    static uint64_t last_rtc_value = 0;
    //static uint8_t noise_val = 0x5F;
    SET_PIN(PIN_SYNC_TIME);

    static uint64_t time_now = 0;

    switch (sig)
    {
        case NRF_RADIO_CALLBACK_SIGNAL_TYPE_START:
        {
            NVIC_ClearPendingIRQ(SWI0_IRQn);
            g_is_in_timeslot = true;

            event_fifo_flush();
            timer_init();
            SET_PIN(2);
            successful_extensions = 0;

            g_negotiate_timeslot_length = g_timeslot_length;

            g_timeslot_length = g_next_timeslot_length;

            g_timeslot_end_timer = 
                timer_order_cb_sync_exec(g_timeslot_length - TIMESLOT_END_SAFETY_MARGIN_US, 
                    end_timer_handler);


            /* attempt to extend our time right away */
            timeslot_extend(g_negotiate_timeslot_length);

#if USE_SWI_FOR_PROCESSING
            NVIC_EnableIRQ(SWI0_IRQn);
            NVIC_SetPriority(SWI0_IRQn, 3);
#endif

            /* sample RTC timer for trickle timing */
            uint32_t rtc_time = NRF_RTC0->COUNTER;

            /*First time the offset should be added*/
            if(last_rtc_value == 0)
            {
                last_rtc_value = g_start_time_ref;
            }

            /* Calculate delta rtc time */
            uint64_t delta_rtc_time;
            if(last_rtc_value > rtc_time)
            {
                delta_rtc_time = 0xFFFFFF - last_rtc_value + rtc_time;
            }
            else
            {
                delta_rtc_time = rtc_time - last_rtc_value;
            }
            /* Store last rtc time */
            last_rtc_value = rtc_time;


            /* scale to become us */
            time_now += ((delta_rtc_time << 15) / 1000);

            transport_control_timeslot_begin(time_now);
            break;
        }
        case NRF_RADIO_CALLBACK_SIGNAL_TYPE_RADIO:
            /* send to radio control module */
            TICK_PIN(PIN_RADIO_SIGNAL);
            radio_event_handler();
            break;

        case NRF_RADIO_CALLBACK_SIGNAL_TYPE_TIMER0:
            /* send to timer control module */
            TICK_PIN(PIN_TIMER_SIGNAL);
            timer_event_handler();
            break;

        case NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_SUCCEEDED:
            g_timeslot_length += requested_extend_time;
            requested_extend_time = 0;
            ++successful_extensions;
            g_ret_param.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_NONE;

            timer_abort(g_timeslot_end_timer);

            g_timeslot_end_timer = 
                timer_order_cb_sync_exec(g_timeslot_length - TIMESLOT_END_SAFETY_MARGIN_US, 
                    end_timer_handler);

            TICK_PIN(1);
            if (g_timeslot_length + g_negotiate_timeslot_length < TIMESLOT_MAX_LENGTH)
            {
                timeslot_extend(g_negotiate_timeslot_length);   
            }
            else
            {
                /* done extending, check for new trickle event */
                transport_control_step();
            }

            break;

        case NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_FAILED:
            g_negotiate_timeslot_length >>= 2;
            TICK_PIN(1);
            if (g_negotiate_timeslot_length > 1000)
            {
                timeslot_extend(g_negotiate_timeslot_length);
            }
            else
            {
                /* done extending, check for new trickle event */
                transport_control_step();
            }
            break;

        default:
            APP_ERROR_CHECK(NRF_ERROR_INVALID_STATE);
    }


    g_is_in_callback = false;
    if (g_ret_param.callback_action == NRF_RADIO_SIGNAL_CALLBACK_ACTION_EXTEND)
    {
        requested_extend_time = g_ret_param.params.extend.length_us;
    }
    else if (g_ret_param.callback_action == NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END)
    {
        CLEAR_PIN(2);
        g_is_in_timeslot = false;
        event_fifo_flush();
    }
    else
    {
        requested_extend_time = 0;
    }

    CLEAR_PIN(PIN_SYNC_TIME);
    return &g_ret_param;
}