I will use ADC / Button function over SoftDevice.
below by source.
the thread is repeated not go beyond the conn_params_init.
#define ADC_SAMPLING_INTERVAL APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER)
#define BUTTON_DETECTION_DELAY APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)
{
timers_init();
gpiote_init();
buttons_init();
ble_stack_init();
gap_params_init();
services_init();
advertising_init();
conn_params_init();
sec_params_init();
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
adc_init();
application_timers_start();
advertising_start();
}
static void application_timers_start(void)
{
uint32_t err_code;
//ADC timer start
err_code = app_timer_start(m_adc_sampling_timer_id, ADC_SAMPLING_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
}
static void adc_sampling_timeout_handler(void * p_context)
{
uint32_t p_is_running = 0;
sd_clock_hfclk_request();
while(! p_is_running) { //wait for the hfclk to be available
sd_clock_hfclk_is_running((&p_is_running));
}
NRF_ADC->TASKS_START = 1; //Start ADC sampling
}
static void timers_init(void)
{
uint32_t err_code;
// Initialize timer module
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);
err_code = app_timer_create(&m_adc_sampling_timer_id,
APP_TIMER_MODE_REPEATED,
adc_sampling_timeout_handler);
APP_ERROR_CHECK(err_code);
}
static void adc_init(void)
{
/* Enable interrupt on ADC sample ready event*/
NRF_ADC->INTENSET = ADC_INTENSET_END_Msk;
sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW);
sd_nvic_EnableIRQ(ADC_IRQn);
NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) /* Bits 17..16 : ADC external reference pin selection. */
| (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) /*!< Use analog input 2 as analog input. */
| (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) /*!< Use internal 1.2V bandgap voltage as reference for conversion. */
| (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) /*!< Analog input specified by PSEL with no prescaling used as input for the conversion. */
| (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos); /*!< 8bit ADC resolution. */
/* Enable ADC*/
NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
AD_SDA_STANDARD0_NODRIVE1();
AD_SCL_OUTPUT();
AD_SDA_INPUT();
}
void ADC_IRQHandler(void)
{
uint8_t adc_result;
/* Clear dataready event */
NRF_ADC->EVENTS_END = 0;
ADC_Value = (NRF_ADC->RESULT);
NRF_ADC->TASKS_STOP = 1;
//Release the external crystal
sd_clock_hfclk_release();
}
static void gpiote_init(void)
{
APP_GPIOTE_INIT(APP_GPIOTE_MAX_USERS);
}
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
if (button_action == APP_BUTTON_PUSH)
{
switch (pin_no)
{
case BT_ONE: Zero_Point(); break;
case BT_TWO: break;
default: APP_ERROR_HANDLER(pin_no); break;
}
}
}
static void buttons_init(void)
{
static app_button_cfg_t buttons[] =
{
{BT_ONE, false, BUTTON_PULL, button_event_handler},
{BT_TWO, false, BUTTON_PULL, button_event_handler},
};
APP_BUTTON_INIT(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY, false);
}
#EDIT MY Question
I'm sure It will not work when I use the two functions at the same time. {
APP_BUTTON_INIT(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY, false);
app_timer_create(&m_adc_sampling_timer_id,
APP_TIMER_MODE_REPEATED,
adc_sampling_timeout_handler);
}
How should I fix it?