123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include "bsp_adc.h"
- #include "system.h"
- #include "exception.h"
- #define PIN_NOT_USED 0xFF
- #define CHANNEL_NOT_USED 0xFF
- typedef struct {
- uint32_t pin;
- uint32_t channel;
- }adc_config_t;
- static adc_config_t m_adc_config[CHANNEL_MAX] = {{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED}};
- static nrf_saadc_channel_config_t channelConfig[CHANNEL_MAX];
- static nrf_saadc_value_t s_bufferPool[CHANNEL_MAX] = {0};
- static uint32_t sample_in_buffer = 0;
- static bsp_adc_sample_notify_cb m_cb = NULL;
- static void adcCallbackFunc(nrf_drv_saadc_evt_t const *pEvent)
- {
- ret_code_t err_code;
- static int16_t adc_value[CHANNEL_MAX] = {0,0,0,0,0,0,0,0};
- uint8_t adc_sort[CHANNEL_MAX] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- uint8_t sort_index = 0;
- int i;
-
-
- if(pEvent->type == NRF_DRV_SAADC_EVT_DONE)
- {
-
- err_code=nrf_drv_saadc_buffer_convert(pEvent->data.done.p_buffer,sample_in_buffer);
- if(err_code == NRF_SUCCESS)
- {
-
- for(i=0;i<CHANNEL_MAX;i++)
- {
- if(channelConfig[i].pin_p != NRF_SAADC_INPUT_DISABLED){
- adc_sort[sort_index] = i;
- sort_index++;
- }
- }
-
- for(i=0;i<sample_in_buffer;i++)
- {
- adc_value[adc_sort[i]] = s_bufferPool[i];
- }
- if(m_cb != NULL)m_cb(adc_value);
- }
- }
- }
- static void ADC_Init(void)
- {
- int ret = 0;
- ret_code_t errCode;
-
- nrf_drv_saadc_config_t p_config = NRFX_SAADC_DEFAULT_CONFIG;
- p_config.interrupt_priority = ADC_IRQ_PRIORITY;
-
-
- nrf_drv_saadc_init(&p_config, adcCallbackFunc);
- if(errCode != NRF_SUCCESS)ret = -1;
-
- for(int i=0;i<CHANNEL_MAX;i++)
- {
- if(m_adc_config[i].pin != PIN_NOT_USED && m_adc_config[i].channel != CHANNEL_NOT_USED)
- {
-
- nrf_drv_saadc_channel_init(m_adc_config[i].channel, &channelConfig[m_adc_config[i].channel]);
- }
- }
-
- if(sample_in_buffer > 0)
- {
-
- nrf_drv_saadc_buffer_convert(s_bufferPool, sample_in_buffer);
- if(errCode != NRF_SUCCESS)ret = -1;
- }
-
-
- }
- uint32_t ADC_SetPinChannel(uint32_t pin, uint32_t channel,nrf_gpio_pin_pull_t pin_pull)
- {
- ret_code_t errCode;
-
-
- ADC_RemovePinChannel(pin, channel);
-
- for(int i=0;i<CHANNEL_MAX;i++)
- {
- if(m_adc_config[i].pin == PIN_NOT_USED && m_adc_config[i].channel == CHANNEL_NOT_USED && PIN_NOT_USED != pin)
- {
-
- ADC_Disable();
-
- m_adc_config[i].pin = pin;
- m_adc_config[i].channel = channel;
-
-
- nrf_gpio_pin_dir_t dir = nrf_gpio_pin_dir_get(m_adc_config[i].pin);
-
-
- nrf_gpio_cfg_input(m_adc_config[i].pin,pin_pull);
-
- channelConfig[m_adc_config[i].channel].resistor_p = NRF_SAADC_RESISTOR_DISABLED;
- channelConfig[m_adc_config[i].channel].resistor_n = NRF_SAADC_RESISTOR_DISABLED;
- channelConfig[m_adc_config[i].channel].gain = NRF_SAADC_GAIN1_6;
- channelConfig[m_adc_config[i].channel].reference = NRF_SAADC_REFERENCE_INTERNAL;
- channelConfig[m_adc_config[i].channel].acq_time = NRF_SAADC_ACQTIME_10US;
- channelConfig[m_adc_config[i].channel].mode = NRF_SAADC_MODE_SINGLE_ENDED;
- channelConfig[m_adc_config[i].channel].burst = NRF_SAADC_BURST_DISABLED;
- channelConfig[m_adc_config[i].channel].pin_p = (nrf_saadc_input_t)(m_adc_config[i].channel + 1);
- channelConfig[m_adc_config[i].channel].pin_n = NRF_SAADC_INPUT_DISABLED;
-
- errCode = nrf_drv_saadc_channel_init(m_adc_config[i].channel, &channelConfig[m_adc_config[i].channel]);
- if(errCode != NRF_SUCCESS)
- {
- channelConfig[m_adc_config[i].channel].pin_p = NRF_SAADC_INPUT_DISABLED;
- m_adc_config[i].pin = PIN_NOT_USED;
- m_adc_config[i].channel = CHANNEL_NOT_USED;
- nrf_gpio_pin_dir_set(m_adc_config[i].pin, dir);
-
- ADC_Enable();
-
- return ADC_ERR_INIT_CONGESTION;
- }
-
- sample_in_buffer++;
-
- ADC_Enable();
-
- return ADC_OP_SUCCESS;
- }
- }
- return ADC_ERR_INIT_CONGESTION;
- }
- uint32_t ADC_RemovePinChannel(uint32_t pin, uint32_t channel)
- {
- for(int i=0;i<CHANNEL_MAX;i++)
- {
- if(m_adc_config[i].pin == pin && m_adc_config[i].channel == channel)
- {
- nrf_drv_saadc_channel_uninit(m_adc_config[i].channel);
- channelConfig[m_adc_config[i].channel].pin_p = NRF_SAADC_INPUT_DISABLED;
- m_adc_config[i].pin = PIN_NOT_USED;
- m_adc_config[i].channel = CHANNEL_NOT_USED;
- if(sample_in_buffer != 0)sample_in_buffer--;
- return ADC_OP_SUCCESS;
- }
- }
- return ADC_ERR_REMOVE_PIN_CHANNEL;
- }
- uint32_t ADC_Sample(bsp_adc_sample_notify_cb cb)
- {
- uint32_t errCode;
- if(cb != NULL)
- {
-
- if(sample_in_buffer == 0)return ADC_ERR_READ_NO_CHANNEL;
- ADC_Disable();
- ADC_Enable();
- errCode = nrf_drv_saadc_sample();
- if(errCode != NRF_SUCCESS)return ADC_ERR_TRIGGER_FAIL;
- m_cb = cb;
- return ADC_OP_SUCCESS;
- }
-
- return ADC_ERR_TRIGGER_FAIL;
- }
- void ADC_Enable(void)
- {
- ADC_Init();
- }
- void ADC_Disable(void)
- {
- nrf_drv_saadc_uninit();
- }
|