luat_adc_air105.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_adc.h"
  22. #include "luat_base.h"
  23. #include "core_adc.h"
  24. #include "platform_define.h"
  25. #define LUAT_LOG_TAG "luat.adc"
  26. #include "luat_log.h"
  27. int luat_adc_open(int ch, void *args)
  28. {
  29. switch (ch)
  30. {
  31. case 0:
  32. return 0;
  33. case 1:
  34. GPIO_Iomux(GPIOC_00, 2);
  35. GPIO_PullConfig(GPIOC_00, 0, 0);
  36. break;
  37. case 2:
  38. GPIO_Iomux(GPIOC_01, 2);
  39. GPIO_PullConfig(GPIOC_01, 0, 0);
  40. break;
  41. case 3:
  42. return -1;
  43. case 4:
  44. GPIO_Iomux(GPIOC_03, 2);
  45. GPIO_PullConfig(GPIOC_03, 0, 0);
  46. break;
  47. case 5:
  48. GPIO_Iomux(GPIOC_04, 2);
  49. GPIO_PullConfig(GPIOC_04, 0, 0);
  50. break;
  51. case 6:
  52. GPIO_Iomux(GPIOC_05, 2);
  53. GPIO_PullConfig(GPIOC_05, 0, 0);
  54. break;
  55. default:
  56. return -1;
  57. }
  58. ADC_ChannelOnOff(ch, 1);
  59. return 0;
  60. }
  61. int luat_adc_global_config(int tp, int val)
  62. {
  63. switch(tp)
  64. {
  65. case ADC_SET_GLOBAL_RANGE:
  66. ADC_IntelResistance(val);
  67. return 0;
  68. }
  69. return -1;
  70. }
  71. int luat_adc_read(int ch, int *val, int *val2)
  72. {
  73. int voltage = 0;
  74. voltage = ADC_GetChannelValue(ch, val2);
  75. *val = voltage;
  76. return 0;
  77. }
  78. int luat_adc_close(int ch){
  79. switch (ch)
  80. {
  81. case 1:
  82. GPIO_PullConfig(GPIOC_00, 1, 1);
  83. GPIO_Iomux(GPIOC_00, 1);
  84. break;
  85. case 2:
  86. GPIO_PullConfig(GPIOC_01, 1, 1);
  87. GPIO_Iomux(GPIOC_01, 1);
  88. break;
  89. case 4:
  90. GPIO_PullConfig(GPIOC_03, 1, 1);
  91. GPIO_Iomux(GPIOC_03, 1);
  92. break;
  93. case 5:
  94. GPIO_PullConfig(GPIOC_04, 1, 1);
  95. GPIO_Iomux(GPIOC_04, 1);
  96. break;
  97. case 6:
  98. GPIO_PullConfig(GPIOC_05, 1, 1);
  99. GPIO_Iomux(GPIOC_05, 1);
  100. break;
  101. default:
  102. return -1;
  103. }
  104. if (ch)
  105. ADC_ChannelOnOff(ch, 0);
  106. return 0;
  107. }