/********************************************************************* * INCLUDES */ #include "bsp_adc.h" #include "system.h" #include "exception.h" /********************************************************************* * DEFINITIONS */ #define CHANNEL_MAX 8 #define PIN_NOT_USED 0xFF #define CHANNEL_NOT_USED 0xFF #define WAIT_TIME_VALUE 10000 // 等待超时最大值 /********************************************************************* * STRUCTION */ typedef struct { uint32_t pin; uint32_t channel; }adc_config_t; typedef enum{ ChargeADC_IDIE=0, ChargeADC_SET, ChargeADC_IRQ, }_ChargeADC_STATE; /********************************************************************* * LOCAL VARIABLES */ 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 volatile bool adc_SampleOk = true; // adc采集完成标志 static volatile _ChargeADC_STATE Chargeadc_state = ChargeADC_IDIE; // 充电引脚adc采集完成标志 //定义SAADC采样数据缓存 //定义SAADC采样缓存数组大小 //只有采样结果存满该缓存之后,才会产生SAADC采样完成事件 static uint32_t sample_in_buffer = 0; static bsp_adc_read_cb m_cb = NULL; static uint32_t m_cb_channel = CHANNEL_NOT_USED; static uint32_t m_cb_count = 0; /********************************************************************* * LOCAL FUNCTIONS */ uint8_t bsp_Get_ChargePinADC(int16_t *adc){ uint8_t i =0; uint8_t adc_sort[CHANNEL_MAX] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; uint8_t sort_index = 0; if(ChargeADC_IRQ != Chargeadc_state )return 0; else{ for(i=0;i> ChargeADC_IDIE\r\n"); Chargeadc_state = ChargeADC_IDIE; return 1; } } /** @brief ADC中断处理回调函数 @param 无 @return 无 */ static void adcCallbackFunc(nrf_drv_saadc_evt_t const *pEvent) { int i; ret_code_t err_code; uint8_t adc_sort[CHANNEL_MAX] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; uint8_t sort_index = 0; 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) { adc_SampleOk = true; if(ChargeADC_SET == Chargeadc_state){ Chargeadc_state= ChargeADC_IRQ; DEBUG_LOG(" >> ChargeADC_IRQ\r\n"); } //读取回调 if(m_cb != NULL && m_cb_count != 0) { //预先排序,采集时填充顺序为,通道编号小的先填充。 for(i=0;i 0) { // 缓冲配置 errCode = nrf_drv_saadc_buffer_convert(s_bufferPool, sample_in_buffer); if(errCode != NRF_SUCCESS)ret = -1; } if(ret == -1) { Process_Start(0,"bsp_adc_init",bsp_adc_init_process); } } static void cb_adcWakeup(uint32_t t) { ADC_Enable(); } static void cb_adcSleep(uint32_t t) { ADC_Disable(); } /********************************************************************* * PUBLIC FUNCTIONS */ /** @brief 初始化ADC引脚和通道 @param pin -[in] 需要初始化的adc引脚 @param channel -[in] 需要初始化的adc通道 @return 错误代码 */ 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> ChargeADC_SET\r\n"); Chargeadc_state =ChargeADC_SET; return ADC_OP_SUCCESS; } /** @brief ADC读取 @param channel -[in] 需要读取的通道 @param p_adc_value -[out] 返回读取的通道值 @return 错误代码 */ uint32_t ADC_Read(uint32_t channel, int16_t *p_adc_value) { uint32_t errCode; uint32_t wait_time_out = WAIT_TIME_VALUE; uint8_t adc_sort[CHANNEL_MAX] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; uint8_t sort_index = 0; int i; adc_SampleOk = false; if(Chargeadc_state != ChargeADC_IDIE)return ADC_ERR_READ_BUSY; //没有配置通道返回失败 if(sample_in_buffer == 0)return ADC_ERR_READ_NO_CHANNEL; errCode = nrf_drv_saadc_sample(); if(errCode != NRF_SUCCESS)return ADC_ERR_READ_FAIL; //等待采集 while(!adc_SampleOk) { if(wait_time_out--){ // nrf_pwr_mgmt_run(); } else{ return ADC_ERR_READ_TIMEOUT; } } //预先排序,采集时填充顺序为,通道编号小的先填充。 for(i=0;i