esp_err_t adc_init(adc_config_t *config) { ADC_CHECK(config, "config error", ESP_ERR_INVALID_ARG); ADC_CHECK(NULL == adc_handle, "adc has been initialized", ESP_FAIL); uint8_t vdd33_const; esp_phy_init_data_t *phy_init_data; phy_init_data = (esp_phy_init_data_t *)esp_phy_get_init_data(); vdd33_const = phy_init_data->params[107]; ADC_CHECK((config->mode == ADC_READ_TOUT_MODE) ? (vdd33_const < 255) : true, "To read the external voltage on TOUT(ADC) pin, vdd33_const need less than 255", ESP_FAIL); ADC_CHECK((config->mode == ADC_READ_VDD_MODE) ? (vdd33_const == 255) : true, "When adc measuring system voltage, vdd33_const must be set to 255,", ESP_FAIL); ADC_CHECK(config->mode <= ADC_READ_MAX_MODE, "adc mode err", ESP_FAIL); adc_handle = heap_caps_malloc(sizeof(adc_handle_t), MALLOC_CAP_8BIT); ADC_CHECK(adc_handle, "adc handle malloc error", ESP_ERR_NO_MEM); memcpy(&adc_handle->config, config, sizeof(adc_config_t)); adc_handle->adc_mux = xSemaphoreCreateMutex(); if (NULL == adc_handle->adc_mux) { adc_deinit(); ADC_CHECK(false, "Semaphore create fail", ESP_ERR_NO_MEM); } return ESP_OK; }
key_state_t key_state_get(void) { key_state_t ret = KEY_NO_KEY; int16_t res; adc_init(ADC_CHAN_ADC1, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((res = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){;} adc_deinit(); if (res>0x0370) { ret = KEY_NO_KEY; } else if (res>0x0280) { ret = KEY_DOWN; } else if (res>0x0180) { ret = KEY_LEFT; } else if (res>0x00C0) { ret = KEY_RIGHT; } else { ret = KEY_UP; } if (!(ENTER_PORT & (1<<ENTER_PIN))) { ret |= KEY_ENTER; } return ret; }
/* run this demo, please connect the sensor board. */ int main(void) { printf("ADC demo started.\n"); HAL_Status status = HAL_ERROR; adc_init(); while (1) { #ifdef ADC_IT_MODE #ifdef ADC_TEST_FIFO status = HAL_ADC_FifoConfigChannel(ADC_INCH, ADC_SELECT_ENABLE); #else status =HAL_ADC_ConfigChannel(ADC_INCH, ADC_SELECT_ENABLE, ADC_IRQ_MODE, 0, 0); #endif if (status != HAL_OK) { printf("ADC config error %d\n", status); break; } #else uint32_t ad_value = 0; status = HAL_ADC_Conv_Polling(ADC_INCH, &ad_value, 10000); if (status != HAL_OK) { printf("ADC poll error %d\n", status); } printf("poll mode: adc value = %d\n", ad_value); #endif OS_MSleep(500); } adc_deinit(); printf("ADC demo over.\n"); return 0; }
/** * @brief Deinitialize dedicated current measurement HW * (GPIO, ADC) of a given spring. * warning 'spring' index starts at 1, not 0. * @return 0 on success, standard error otherwise. * @param[in] spring: selected spring ([1..SPRING_COUNT]) */ int spwrm_deinit(uint8_t spring) { int ret; uint8_t adc, chan; uint32_t spin; CHECK_SPRING(spring); dbg_verbose("%s(): De-initializing %s...\n", __func__, spwrm_get_name(spring)); adc = spwrm_get_adc_device(spring); chan = spwrm_get_adc_channel(spring); spin = spwrm_get_sign_pin(spring); /* Unconfigure GPIO ADC channel pin */ stm32_unconfiggpio(spin); /* Shutdown ADC */ ret = adc_deinit(adc, chan); if (ret) { dbg_error("%s(): %s de-initialization failed!!! (%d)\n", __func__, spwrm_get_name(spring), ret); } else { dbg_verbose("%s(): %s de-initialization done.\n", __func__, spwrm_get_name(spring)); } return ret; }
void deinit(void) { cli(); timer_deinit(); pwm_deinit(); adc_deinit(); io_deinit(); }
/** \brief Read current voltage * \return EOF on error */ double voltage_get() { int16_t result; /* Init ADC and measure */ adc_init(ADC_CHAN_ADC30, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((result = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } adc_deinit(); return 1.1 * (1024.0 / (double)result); }
int16_t temp_get(temp_unit_t unit) { int16_t res; int16_t temp; /* Return if temp sensor driver not initialized */ if (temp_initialized == false) { return EOF; } /* Power up sensor */ TEMP_PORT |= (1 << TEMP_BIT_PWR); /* Init ADC and measure */ adc_init(ADC_CHAN_ADC4, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((res = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } #if MEASURE_ADC2 /* Measure external voltage supply, routed to ADC2 through a 470K/100K divider*/ /* AVCC is 3.3 volts if using external supply, else Vbat which will be lower */ /* Convert result to millivolts assuming AVCC is 3.3 volts, on battery it will be lower! */ adc_init(ADC_CHAN_ADC2, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((ADC2_reading = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } ADC2_reading = (ADC2_reading*((470+100)*3300UL))/(100*1024UL); #endif adc_deinit(); /* Re-init the adc for buttons. */ key_init(); /* Power down sensor */ TEMP_PORT &= ~(1 << TEMP_BIT_PWR); /* Get corresponding temperature from table */ if (unit == TEMP_UNIT_CELCIUS) { temp = find_temp(res, temp_table_celcius, sizeof(temp_table_celcius)/sizeof(int)) + TEMP_ZERO_OFFSET_CELCIUS; } else /*unit == TEMP_UNIT_FAHRENHEIT*/{ temp = find_temp(res, temp_table_fahrenheit, sizeof(temp_table_fahrenheit)/sizeof(int)) + TEMP_ZERO_OFFSET_FAHRENHEIT; } return temp; }
static int16_t supply_voltage_read(void) { int16_t adc_res; // Read band gap voltage with Avcc as ref. Use result to calulate Vcc adc_init(ADC_CHAN_VBG, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); // wait for band gap voltage to stabilize delay_us(150); // read ADC adc_conversion_start(); while ((adc_res = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){;} adc_deinit(); // Return battery voltage in mV return (int16_t)((1100L*1024L)/(long)adc_res); }
/** \brief Read current light's raw value * \return EOF on error */ int16_t sensor_light_get_raw() { int16_t result; if (!light_initialized) { light_init(); } /* Power up sensor */ LIGHT_PORT |= (1 << LIGHT_BIT); /* Init ADC and measure */ adc_init(ADC_CHAN_ADC4, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((result = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } adc_deinit(); /* Power down sensor */ LIGHT_PORT &= ~(1 << LIGHT_BIT); return result; }