Exemplo n.º 1
0
/**
 * @brief Function for initializing SAADC.
 */
static ret_code_t saadc_init(void)
{
    ret_code_t err_code;
    static nrf_saadc_value_t saadc_value;

    saadc_channel.gain = NRF_SAADC_GAIN1_3;

   err_code = nrf_drv_saadc_init(NULL, saadc_handler);
   if (err_code != NRF_SUCCESS)
   {
       return NRF_ERROR_INTERNAL;
   }

    nrf_gpio_pin_set(m_csense.output_pin);

    err_code = nrf_drv_saadc_channel_init(0, &saadc_channel);
    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }

    err_code = nrf_drv_saadc_buffer_convert(&saadc_value, 1);
    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }

    nrf_saadc_disable();

    return NRF_SUCCESS;
}
Exemplo n.º 2
0
void nrf_drv_saadc_uninit(void)
{
    ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED);

    nrf_saadc_int_disable(NRF_SAADC_INT_ALL);
    nrf_drv_common_irq_disable(SAADC_IRQn);
    nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP);

    // Wait for ADC being stopped.
    uint32_t timeout = HW_TIMEOUT;

    while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0 && timeout > 0)
    {
        --timeout;
    }
    ASSERT(timeout > 0);

    nrf_saadc_disable();
    m_cb.adc_state = NRF_SAADC_STATE_IDLE;

    for (uint8_t channel = 0; channel < NRF_SAADC_CHANNEL_COUNT; ++channel)
    {
        if (m_cb.psel[channel].pselp != NRF_SAADC_INPUT_DISABLED)
        {
            (void)nrf_drv_saadc_channel_uninit(channel);
        }
    }

    m_cb.state = NRF_DRV_STATE_UNINITIALIZED;
}
Exemplo n.º 3
0
/**
 * @brief Function for handling conversion values.
 *
 * @param[in] val                Value received from ADC or COMP.
 */
static void conversion_handler(uint16_t val)
{
    nrf_drv_csense_evt_t event_struct;

#if USE_COMP == 0
    nrf_gpio_pin_set(m_csense.output_pin);
#endif //USE_COMP

    m_csense.analog_values[m_csense.cur_chann_idx] = val;

    event_struct.read_value = val;
    event_struct.analog_channel = m_csense.cur_chann_idx;

    m_csense.channels_to_read &= ~(1UL<<m_csense.cur_chann_idx);

    // decide if there will be more conversions
    if(m_csense.channels_to_read == 0)
    {
        m_csense.busy = false;
#if USE_COMP == 0 && defined(SAADC_PRESENT)
        nrf_saadc_disable();
#endif
    }

    m_csense.event_handler(&event_struct);

    if(m_csense.channels_to_read > 0)     // Start new conversion.
    {
        ret_code_t err_code;
        calculate_next_channel();
        err_code = nrf_drv_csense_sample();
        if(err_code != NRF_SUCCESS)
        {
            return;
        }
    }
}