adc_hal.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The HAL layer for ADC (common part)
  15. #include "hal/adc_hal.h"
  16. #include "hal/adc_hal_conf.h"
  17. #include "hal/adc_types.h"
  18. void adc_hal_digi_deinit(void)
  19. {
  20. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  21. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  22. }
  23. void adc_hal_digi_controller_config(const adc_digi_config_t *cfg)
  24. {
  25. /* Single channel mode or multi channel mode. */
  26. adc_ll_digi_set_convert_mode(cfg->conv_mode);
  27. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  28. adc_ll_set_controller(ADC_NUM_1, ADC_CTRL_DIG);
  29. if (cfg->adc1_pattern_len) {
  30. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  31. adc_ll_digi_set_pattern_table_len(ADC_NUM_1, cfg->adc1_pattern_len);
  32. for (uint32_t i = 0; i < cfg->adc1_pattern_len; i++) {
  33. adc_ll_digi_set_pattern_table(ADC_NUM_1, i, cfg->adc1_pattern[i]);
  34. }
  35. }
  36. }
  37. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  38. adc_ll_set_controller(ADC_NUM_2, ADC_CTRL_DIG);
  39. if (cfg->adc2_pattern_len) {
  40. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  41. adc_ll_digi_set_pattern_table_len(ADC_NUM_2, cfg->adc2_pattern_len);
  42. for (uint32_t i = 0; i < cfg->adc2_pattern_len; i++) {
  43. adc_ll_digi_set_pattern_table(ADC_NUM_2, i, cfg->adc2_pattern[i]);
  44. }
  45. }
  46. }
  47. adc_ll_digi_set_output_format(cfg->format);
  48. if (cfg->conv_limit_en) {
  49. adc_ll_digi_set_convert_limit_num(cfg->conv_limit_num);
  50. adc_ll_digi_convert_limit_enable();
  51. } else {
  52. adc_ll_digi_convert_limit_disable();
  53. }
  54. adc_ll_digi_set_data_source(ADC_I2S_DATA_SRC_ADC);
  55. }
  56. int adc_hal_hall_convert(void)
  57. {
  58. int Sens_Vp0;
  59. int Sens_Vn0;
  60. int Sens_Vp1;
  61. int Sens_Vn1;
  62. int hall_value;
  63. // convert for 4 times with different phase and outputs
  64. adc_ll_hall_phase_disable(); // hall phase
  65. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_0, &Sens_Vp0 );
  66. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_3, &Sens_Vn0 );
  67. adc_ll_hall_phase_enable();
  68. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_0, &Sens_Vp1 );
  69. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_3, &Sens_Vn1 );
  70. hall_value = (Sens_Vp1 - Sens_Vp0) - (Sens_Vn1 - Sens_Vn0);
  71. return hall_value;
  72. }