浏览代码

【添加】machine.ADC 模块

Signed-off-by: chenyong <1521761801@qq.com>
chenyong 6 年之前
父节点
当前提交
f2faac3b57
共有 5 个文件被更改,包括 190 次插入1 次删除
  1. 1 0
      port/genhdr/qstrdefs.generated.h
  2. 146 0
      port/machine_adc.c
  3. 35 0
      port/machine_adc.h
  4. 4 1
      port/modmachine.c
  5. 4 0
      port/mpconfigport.h

+ 1 - 0
port/genhdr/qstrdefs.generated.h

@@ -697,5 +697,6 @@ QDEF(MP_QSTR_ffimod, (const byte*)"\xca\x06" "ffimod")
 QDEF(MP_QSTR_ffifunc, (const byte*)"\x92\x07" "ffifunc")
 QDEF(MP_QSTR_fficallback, (const byte*)"\xc5\x0b" "fficallback")
 QDEF(MP_QSTR_ffivar, (const byte*)"\x49\x06" "ffivar")
+QDEF(MP_QSTR_ADC, (const byte*)"\x63\x03" "ADC")
 
 // This file was automatically generated by makeqstrdata.py

+ 146 - 0
port/machine_adc.c

@@ -0,0 +1,146 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "py/nlr.h"
+#include "py/runtime.h"
+#include "modmachine.h"
+#include "mphalport.h"
+
+#ifdef MICROPYTHON_USING_MACHINE_ADC
+
+#include <rtthread.h>
+#include <drivers/adc.h>
+
+extern const mp_obj_type_t machine_adc_type;
+
+typedef struct _machine_adc_obj_t {
+    mp_obj_base_t base;
+    struct rt_adc_device *adc_device;
+    uint8_t channel;
+    uint8_t is_init;
+} machine_adc_obj_t;
+
+STATIC void error_check(bool status, const char *msg) {
+    if (!status) {
+        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, msg));
+    }
+}
+
+STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type,
+                                  size_t n_args, size_t n_kw, const mp_obj_t *args) {
+    // create ADC object from the given pin
+    machine_adc_obj_t *self = m_new_obj(machine_adc_obj_t);
+    struct rt_adc_device *adc_device = RT_NULL;
+    char adc_dev_name[RT_NAME_MAX] = {0};
+    rt_err_t result = RT_EOK;
+    int dev_id = 0;
+
+    // init machine adc object information
+    self->channel = 0;
+    self->is_init = RT_FALSE;
+    self->base.type = &machine_adc_type;
+
+    mp_arg_check_num(n_args, n_kw, 1, 2, true);
+
+    dev_id = mp_obj_get_int(args[0]);
+    rt_snprintf(adc_dev_name, sizeof(adc_dev_name), "adc%d", dev_id);
+    adc_device = (struct rt_adc_device *) rt_device_find(adc_dev_name);
+    if (adc_device == RT_NULL || adc_device->parent.type != RT_Device_Class_Miscellaneous) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, 
+                                                "ADC(%s) don't exist", adc_dev_name));
+    }
+    self->adc_device = adc_device;
+
+    if (n_args == 2) {
+        self->channel = mp_obj_get_int(args[1]);
+        result = rt_adc_enable(self->adc_device, self->channel);
+        error_check(result == RT_EOK, "ADC enable error");
+        self->is_init = RT_TRUE;
+    }
+
+    return MP_OBJ_FROM_PTR(self);
+}
+
+STATIC mp_obj_t machine_adc_init(size_t n_args, const mp_obj_t *args) {
+    machine_adc_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+    rt_err_t result = RT_EOK;
+
+    result = rt_adc_enable(self->adc_device, mp_obj_get_int(args[1]));
+    error_check(result == RT_EOK, "ADC enable error");
+    self->channel = mp_obj_get_int(args[1]);
+    self->is_init = RT_TRUE;
+
+    return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_adc_init_obj, 2, 2, machine_adc_init);
+
+STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) {
+    machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
+    rt_err_t result = RT_EOK;
+
+    if (self->is_init == RT_TRUE) {
+        result = rt_adc_disable(self->adc_device, self->channel);
+        error_check(result == RT_EOK, "ADC disable error");
+        self->is_init = RT_FALSE;
+    }
+
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit);
+
+STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
+    machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
+    int tval = 0;
+
+    error_check(self->is_init == RT_TRUE, "ADC device uninitialized");
+
+    tval = rt_adc_read(self->adc_device, self->channel);
+    return MP_OBJ_NEW_SMALL_INT(tval);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
+
+STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
+    { MP_ROM_QSTR(MP_QSTR_init),    MP_ROM_PTR(&machine_adc_init_obj) },
+    { MP_ROM_QSTR(MP_QSTR_deinit),  MP_ROM_PTR(&machine_adc_deinit_obj) },
+    { MP_ROM_QSTR(MP_QSTR_read),    MP_ROM_PTR(&machine_adc_read_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict,
+                            machine_adc_locals_dict_table);
+
+const mp_obj_type_t machine_adc_type = {
+    { &mp_type_type },
+    .name = MP_QSTR_ADC,
+    .make_new = machine_adc_make_new,
+    .locals_dict = (mp_obj_dict_t *) &machine_adc_locals_dict,
+};
+
+#endif // MICROPYTHON_USING_MACHINE_adc

+ 35 - 0
port/machine_adc.h

@@ -0,0 +1,35 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_MACHINE_ADC_H
+#define MICROPY_INCLUDED_MACHINE_ADC_H
+
+#include "py/obj.h"
+#include <rtthread.h>
+
+extern const mp_obj_type_t machine_adc_type;
+
+#endif // MICROPY_INCLUDED_MACHINE_ADC_H

+ 4 - 1
port/modmachine.c

@@ -38,6 +38,7 @@
 #include "extmod/machine_spi.h"
 #include "modmachine.h"
 #include "machine_uart.h"
+#include "machine_adc.h"
 
 #include <rthw.h>
 
@@ -204,7 +205,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
 #if MICROPY_PY_MACHINE_UART
     { MP_ROM_QSTR(MP_QSTR_UART),                MP_ROM_PTR(&machine_uart_type ) },
 #endif
-
+#if MICROPY_PY_MACHINE_ADC
+    { MP_ROM_QSTR(MP_QSTR_ADC),                 MP_ROM_PTR(&machine_adc_type) },
+#endif
 };
 
 STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);

+ 4 - 0
port/mpconfigport.h

@@ -143,6 +143,10 @@
 #define MICROPY_PY_MACHINE_UART      (1)
 #endif
 
+#ifdef MICROPYTHON_USING_MACHINE_ADC
+#define MICROPY_PY_MACHINE_ADC       (1)
+#endif
+
 /*****************************************************************************/
 /* System Module                                                             */