|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2013, 2014 Damien P. George |
| 7 | + * Copyright (c) 2015 Daniel Campora |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <stdio.h> |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "mpconfig.h" |
| 32 | +#include MICROPY_HAL_H |
| 33 | +#include "py/nlr.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "py/binary.h" |
| 36 | +#include "py/gc.h" |
| 37 | +#include "bufhelper.h" |
| 38 | +#include "inc/hw_types.h" |
| 39 | +#include "inc/hw_adc.h" |
| 40 | +#include "inc/hw_ints.h" |
| 41 | +#include "inc/hw_memmap.h" |
| 42 | +#include "rom_map.h" |
| 43 | +#include "interrupt.h" |
| 44 | +#include "pin.h" |
| 45 | +#include "prcm.h" |
| 46 | +#include "adc.h" |
| 47 | +#include "pybadc.h" |
| 48 | +#include "pybpin.h" |
| 49 | +#include "pins.h" |
| 50 | +#include "mpexception.h" |
| 51 | + |
| 52 | + |
| 53 | +/// \moduleref pyb |
| 54 | +/// \class ADC - analog to digital conversion: read analog values on a pin |
| 55 | +/// |
| 56 | +/// Usage: |
| 57 | +/// |
| 58 | +/// adc = pyb.ADC(channel) # create an adc object on the given channel (0 to 3) |
| 59 | +/// adc.read() # read channel value |
| 60 | +/// |
| 61 | +/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits. |
| 62 | + |
| 63 | +typedef struct _pyb_obj_adc_t { |
| 64 | + mp_obj_base_t base; |
| 65 | + byte channel; |
| 66 | + byte num; |
| 67 | +} pyb_obj_adc_t; |
| 68 | + |
| 69 | + |
| 70 | +/******************************************************************************/ |
| 71 | +/* Micro Python bindings : adc object */ |
| 72 | + |
| 73 | +STATIC void adc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 74 | + pyb_obj_adc_t *self = self_in; |
| 75 | + print(env, "<ADC, channel=%u>", self->num); |
| 76 | +} |
| 77 | + |
| 78 | +/// \classmethod \constructor(channel) |
| 79 | +/// Create an ADC object associated with the given channel. |
| 80 | +/// This allows you to then read analog values on that pin. |
| 81 | +STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
| 82 | + // check number of arguments |
| 83 | + mp_arg_check_num(n_args, n_kw, 1, 1, false); |
| 84 | + |
| 85 | + // the first argument is the channel number |
| 86 | + uint num = mp_obj_get_int(args[0]); |
| 87 | + const pin_obj_t *pin; |
| 88 | + uint channel; |
| 89 | + switch (num) { |
| 90 | + case 0: |
| 91 | + channel = ADC_CH_0; |
| 92 | + pin = &pin_GPIO2; |
| 93 | + break; |
| 94 | + case 1: |
| 95 | + channel = ADC_CH_1; |
| 96 | + pin = &pin_GPIO3; |
| 97 | + break; |
| 98 | + case 2: |
| 99 | + channel = ADC_CH_2; |
| 100 | + pin = &pin_GPIO4; |
| 101 | + break; |
| 102 | + case 3: |
| 103 | + channel = ADC_CH_3; |
| 104 | + pin = &pin_GPIO5; |
| 105 | + break; |
| 106 | + default: |
| 107 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + // disable the callback before re-configuring |
| 112 | + pyb_obj_adc_t *self = m_new_obj(pyb_obj_adc_t); |
| 113 | + self->base.type = &pyb_adc_type; |
| 114 | + self->channel = channel; |
| 115 | + self->num = num; |
| 116 | + |
| 117 | + // configure the pin in analog mode |
| 118 | + pin_config (pin, 0, 0, PIN_TYPE_ANALOG, PIN_STRENGTH_2MA); |
| 119 | + |
| 120 | + // enable the ADC channel |
| 121 | + MAP_ADCChannelEnable(ADC_BASE, channel); |
| 122 | + // enable and configure the timer |
| 123 | + MAP_ADCTimerConfig(ADC_BASE, (1 << 17) - 1); |
| 124 | + MAP_ADCTimerEnable(ADC_BASE); |
| 125 | + // enable the ADC peripheral |
| 126 | + MAP_ADCEnable(ADC_BASE); |
| 127 | + |
| 128 | + return self; |
| 129 | +} |
| 130 | + |
| 131 | +/// \method read() |
| 132 | +/// Read the value on the analog pin and return it. The returned value |
| 133 | +/// will be between 0 and 4095. |
| 134 | +STATIC mp_obj_t adc_read(mp_obj_t self_in) { |
| 135 | + pyb_obj_adc_t *self = self_in; |
| 136 | + uint32_t sample; |
| 137 | + |
| 138 | + // wait until a new value is available |
| 139 | + while (!MAP_ADCFIFOLvlGet(ADC_BASE, self->channel)); |
| 140 | + // read the sample |
| 141 | + sample = MAP_ADCFIFORead(ADC_BASE, self->channel); |
| 142 | + // the 12 bit sampled value is stored in bits [13:2] |
| 143 | + return MP_OBJ_NEW_SMALL_INT((sample & 0x3FFF) >> 2); |
| 144 | +} |
| 145 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read); |
| 146 | + |
| 147 | +/// \method enable() |
| 148 | +/// Enable the adc channel |
| 149 | +STATIC mp_obj_t adc_enable(mp_obj_t self_in) { |
| 150 | + pyb_obj_adc_t *self = self_in; |
| 151 | + |
| 152 | + MAP_ADCChannelEnable(ADC_BASE, self->channel); |
| 153 | + return mp_const_none; |
| 154 | +} |
| 155 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_enable_obj, adc_enable); |
| 156 | + |
| 157 | +/// \method disable() |
| 158 | +/// Disable the adc channel |
| 159 | +STATIC mp_obj_t adc_disable(mp_obj_t self_in) { |
| 160 | + pyb_obj_adc_t *self = self_in; |
| 161 | + |
| 162 | + MAP_ADCChannelDisable(ADC_BASE, self->channel); |
| 163 | + return mp_const_none; |
| 164 | +} |
| 165 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_disable_obj, adc_disable); |
| 166 | + |
| 167 | +STATIC const mp_map_elem_t adc_locals_dict_table[] = { |
| 168 | + { MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&adc_disable_obj }, |
| 169 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj }, |
| 170 | + { MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&adc_enable_obj }, |
| 171 | + { MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&adc_disable_obj }, |
| 172 | +}; |
| 173 | + |
| 174 | +STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table); |
| 175 | + |
| 176 | +const mp_obj_type_t pyb_adc_type = { |
| 177 | + { &mp_type_type }, |
| 178 | + .name = MP_QSTR_ADC, |
| 179 | + .print = adc_print, |
| 180 | + .make_new = adc_make_new, |
| 181 | + .locals_dict = (mp_obj_t)&adc_locals_dict, |
| 182 | +}; |
| 183 | + |
0 commit comments