I'm using SDK17.1 and pca10056 DK board. I want to use app_sched_event_put for a very simple purpose. I start a one second clock whose timeout handler increments a static local uint32_t counter. The timeout handler then uses app_sched_event_put to transfer the uint32_t one second counter's value, not it's address, to the app_sched_event_put's handler. Unfortunately, the handler's void *p_event_data looks like an address even after doing a (uint32_t)p_event_data cast.
Code sample followed by some RTTViewer output.
static bool main_loop_started = false;
// Scheduler settings
#define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE
#define SCHED_QUEUE_SIZE 16
APP_TIMER_DEF(m_our_app_timer_id);
#define TIMER_INTERVAL APP_TIMER_TICKS(1000) // every 1000 msecs
void timer_timeout_action(void *p_event_data, uint16_t event_size)
{
uint32_t loop_count = (uint32_t)p_event_data;
NRF_LOG_INFO(" timeout_action: p_event_data = %d loop_count = %d", p_event_data, loop_count);
}
static void timer_timeout_handler(void * p_context)
{
static uint32_t loop_count = 0;
++loop_count;
if (main_loop_started)
{
app_sched_event_put((void *)loop_count, 0, timer_timeout_action);
NRF_LOG_INFO("timeout_handler: &loop_count = %d loop_count = %d", &loop_count, loop_count);
}
}
static void timers_init(void)
{
ret_code_t err_code;
// Make sure the LF clock is running
err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
nrfx_clock_lfclk_start();
// Initialize timer module
err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
// Create timers
err_code = app_timer_create(&m_our_app_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler);
APP_ERROR_CHECK(err_code);
}
static void application_timers_start(void)
{
ret_code_t err_code;
err_code = app_timer_start(m_our_app_timer_id, TIMER_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
}
static void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(get_Nordic_rtc_counter);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
static void power_management_init(void)
{
ret_code_t err_code;
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
static void idle_state_handler(void)
{
if (NRF_LOG_PROCESS() == false)
{
nrf_pwr_mgmt_run();
}
}
int main(void)
{
ret_code_t err_code;
// Initialize
APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
log_init();
timers_init();
application_timers_start();
power_management_init();
NRF_LOG_INFO("DevKit APP Schedular practice started.\n");
// Enter main loop.
main_loop_started = true;
for (;;)
{
app_sched_execute();
idle_state_handler();
}
}
00> [00:00:43.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 43 00> [00:00:43.000,061] <info> app: timeout_action: p_event_data = 536874048 loop_count = 536874048 00> [00:00:44.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 44 00> [00:00:44.000,061] <info> app: timeout_action: p_event_data = 536874056 loop_count = 536874056 00> [00:00:45.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 45 00> [00:00:45.000,061] <info> app: timeout_action: p_event_data = 536874064 loop_count = 536874064 00> [00:00:46.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 46 00> [00:00:46.000,061] <info> app: timeout_action: p_event_data = 536874072 loop_count = 536874072 00> [00:00:47.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 47 00> [00:00:47.000,061] <info> app: timeout_action: p_event_data = 536874080 loop_count = 536874080 00> [00:00:48.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 48 00> [00:00:48.000,061] <info> app: timeout_action: p_event_data = 536874088 loop_count = 536874088 00> [00:00:49.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 49 00> [00:00:49.000,061] <info> app: timeout_action: p_event_data = 536874096 loop_count = 536874096 00> [00:00:50.000,000] <info> app: timeout_handler: &loop_count = 536873844 loop_count = 50 00> [00:00:50.000,061] <info> app: timeout_action: p_event_data = 536874104 loop_count = 536874104
As you can see from the RTTViewer output, the timer_timeout_action's void *p_event_data isn't loop_count's value.
Can I use the app_sched_event_put's p_event_data pointer's memory slot to hold a single uint32_t value and pass it as such to the handler?
I'm motivated to do this because if app_sched_execute starts a process that takes longer than a second, I could wind up with two events in the queue pointing to the same value, (the increment of the increment), instead of two different values, (the current increment and the previous increment).
I would like to add that providing a sizeof(uint32_t) as app_sched_event_put's 2nd parameter doesn't change the outcome.
