machine_adc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "py/nlr.h"
  30. #include "py/runtime.h"
  31. #include "modmachine.h"
  32. #include "mphalport.h"
  33. #ifdef MICROPYTHON_USING_MACHINE_ADC
  34. #include <rtthread.h>
  35. #include <drivers/adc.h>
  36. extern const mp_obj_type_t machine_adc_type;
  37. typedef struct _machine_adc_obj_t {
  38. mp_obj_base_t base;
  39. struct rt_adc_device *adc_device;
  40. uint8_t channel;
  41. uint8_t is_init;
  42. } machine_adc_obj_t;
  43. STATIC void error_check(bool status, const char *msg) {
  44. if (!status) {
  45. nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, msg));
  46. }
  47. }
  48. STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type,
  49. size_t n_args, size_t n_kw, const mp_obj_t *args) {
  50. // create ADC object from the given pin
  51. machine_adc_obj_t *self = m_new_obj(machine_adc_obj_t);
  52. struct rt_adc_device *adc_device = RT_NULL;
  53. char adc_dev_name[RT_NAME_MAX] = {0};
  54. rt_err_t result = RT_EOK;
  55. int dev_id = 0;
  56. // init machine adc object information
  57. self->channel = 0;
  58. self->is_init = RT_FALSE;
  59. self->base.type = &machine_adc_type;
  60. mp_arg_check_num(n_args, n_kw, 1, 2, true);
  61. dev_id = mp_obj_get_int(args[0]);
  62. rt_snprintf(adc_dev_name, sizeof(adc_dev_name), "adc%d", dev_id);
  63. adc_device = (struct rt_adc_device *) rt_device_find(adc_dev_name);
  64. if (adc_device == RT_NULL || adc_device->parent.type != RT_Device_Class_Miscellaneous) {
  65. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  66. "ADC(%s) don't exist", adc_dev_name));
  67. }
  68. self->adc_device = adc_device;
  69. if (n_args == 2) {
  70. self->channel = mp_obj_get_int(args[1]);
  71. result = rt_adc_enable(self->adc_device, self->channel);
  72. error_check(result == RT_EOK, "ADC enable error");
  73. self->is_init = RT_TRUE;
  74. }
  75. return MP_OBJ_FROM_PTR(self);
  76. }
  77. STATIC mp_obj_t machine_adc_init(size_t n_args, const mp_obj_t *args) {
  78. machine_adc_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  79. rt_err_t result = RT_EOK;
  80. result = rt_adc_enable(self->adc_device, mp_obj_get_int(args[1]));
  81. error_check(result == RT_EOK, "ADC enable error");
  82. self->channel = mp_obj_get_int(args[1]);
  83. self->is_init = RT_TRUE;
  84. return mp_const_none;
  85. }
  86. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_adc_init_obj, 2, 2, machine_adc_init);
  87. STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) {
  88. machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
  89. rt_err_t result = RT_EOK;
  90. if (self->is_init == RT_TRUE) {
  91. result = rt_adc_disable(self->adc_device, self->channel);
  92. error_check(result == RT_EOK, "ADC disable error");
  93. self->is_init = RT_FALSE;
  94. }
  95. return mp_const_none;
  96. }
  97. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit);
  98. STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
  99. machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
  100. int tval = 0;
  101. error_check(self->is_init == RT_TRUE, "ADC device uninitialized");
  102. tval = rt_adc_read(self->adc_device, self->channel);
  103. return MP_OBJ_NEW_SMALL_INT(tval);
  104. }
  105. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
  106. STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
  107. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_adc_init_obj) },
  108. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) },
  109. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) },
  110. };
  111. STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict,
  112. machine_adc_locals_dict_table);
  113. const mp_obj_type_t machine_adc_type = {
  114. { &mp_type_type },
  115. .name = MP_QSTR_ADC,
  116. .make_new = machine_adc_make_new,
  117. .locals_dict = (mp_obj_dict_t *) &machine_adc_locals_dict,
  118. };
  119. #endif // MICROPYTHON_USING_MACHINE_adc