adc_hal.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_init(void)
  19. {
  20. adc_hal_init();
  21. adc_ll_digi_set_clk_div(SOC_ADC_DIGI_SAR_CLK_DIV_DEFAULT);
  22. }
  23. void adc_hal_digi_deinit(void)
  24. {
  25. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  26. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  27. adc_hal_deinit();
  28. }
  29. void adc_hal_digi_controller_config(const adc_digi_config_t *cfg)
  30. {
  31. /* If enable digital controller, adc xpd should always on. */
  32. adc_ll_set_power_manage(ADC_POWER_SW_ON);
  33. /* Single channel mode or multi channel mode. */
  34. adc_ll_digi_set_convert_mode(cfg->conv_mode);
  35. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  36. adc_ll_set_controller(ADC_NUM_1, ADC_CTRL_DIG);
  37. if (cfg->adc1_pattern_len) {
  38. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  39. adc_ll_digi_set_pattern_table_len(ADC_NUM_1, cfg->adc1_pattern_len);
  40. for (int i = 0; i < cfg->adc1_pattern_len; i++) {
  41. adc_ll_digi_set_pattern_table(ADC_NUM_1, i, cfg->adc1_pattern[i]);
  42. }
  43. }
  44. }
  45. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  46. adc_ll_set_controller(ADC_NUM_2, ADC_CTRL_DIG);
  47. if (cfg->adc2_pattern_len) {
  48. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  49. adc_ll_digi_set_pattern_table_len(ADC_NUM_2, cfg->adc2_pattern_len);
  50. for (int i = 0; i < cfg->adc2_pattern_len; i++) {
  51. adc_ll_digi_set_pattern_table(ADC_NUM_2, i, cfg->adc2_pattern[i]);
  52. }
  53. }
  54. }
  55. adc_ll_digi_set_output_format(cfg->format);
  56. if (cfg->conv_limit_en) {
  57. adc_ll_digi_set_convert_limit_num(cfg->conv_limit_num);
  58. adc_ll_digi_convert_limit_enable();
  59. } else {
  60. adc_ll_digi_convert_limit_disable();
  61. }
  62. adc_ll_digi_set_data_source(ADC_I2S_DATA_SRC_ADC);
  63. }
  64. int adc_hal_hall_convert(void)
  65. {
  66. int Sens_Vp0;
  67. int Sens_Vn0;
  68. int Sens_Vp1;
  69. int Sens_Vn1;
  70. int hall_value;
  71. // convert for 4 times with different phase and outputs
  72. adc_ll_hall_phase_disable(); // hall phase
  73. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_0, &Sens_Vp0 );
  74. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_3, &Sens_Vn0 );
  75. adc_ll_hall_phase_enable();
  76. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_0, &Sens_Vp1 );
  77. adc_hal_convert( ADC_NUM_1, ADC_CHANNEL_3, &Sens_Vn1 );
  78. hall_value = (Sens_Vp1 - Sens_Vp0) - (Sens_Vn1 - Sens_Vn0);
  79. return hall_value;
  80. }