今天下午在给nRF52832写程序时,莫名遇到了这个错误
错误id是一个很奇怪的数。
原代码如下:
static void timers_init(void)
{
uint32_t timer_err_code;
// Initialize timer module.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
//创建应用定时器
app_timer_create(&m_adc_sampling_timer_id,
APP_TIMER_MODE_REPEATED,
saadc_sampling_timeout_handler);
APP_ERROR_CHECK(timer_err_code);
}
修改后代码如下:
static void timers_init(void)
{
uint32_t timer_err_code;
// Initialize timer module.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
//创建应用定时器
timer_err_code=app_timer_create(&m_adc_sampling_timer_id,
APP_TIMER_MODE_REPEATED,
saadc_sampling_timeout_handler);
APP_ERROR_CHECK(timer_err_code);
}
经过排查发现,错误id,即变量timer_err_code没有赋值就传递给APP_ERROR_CHECK这个宏,结果导致宏调用了错误处理函数,并进一步导致了程序卡死。
APP_ERROR_CHECK宏的作用:Macro for calling error handler function if supplied error code any other than NRF_SUCCESS
注意
1 变量声明要尽可能靠近第一次使用处,避免一次性声明一组没有马上使用的变量。
2 函数的返回值要清楚,明了,防止使用者误用,理解错误或忽视错误返回码。