1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2021 Philipp Ebensberger
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
27 #include <stdint.h>
28 #include "py/obj.h"
29 #include "py/runtime.h"
30 #include "py/mphal.h"
31
32 #include "fsl_adc.h"
33 #include "fsl_gpio.h"
34 #include "fsl_iomuxc.h"
35
36 #include "modmachine.h"
37
38 typedef struct _machine_adc_obj_t {
39 mp_obj_base_t base;
40 ADC_Type *adc;
41 uint8_t channel;
42 uint8_t channel_group;
43 uint16_t resolution;
44 } machine_adc_obj_t;
45
46 STATIC ADC_Type *const adc_bases[] = ADC_BASE_PTRS;
47
adc_obj_print(const mp_print_t * print,mp_obj_t o,mp_print_kind_t kind)48 STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
49 (void)kind;
50 machine_adc_obj_t *self = MP_OBJ_TO_PTR(o);
51
52 // Get ADC adc id
53 for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) {
54 if (adc_bases[i] == self->adc) {
55 mp_printf(print, "ADC(%u, channel=%u)", i, self->channel);
56 break;
57 }
58 }
59 }
60
adc_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)61 STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
62 mp_arg_check_num(n_args, n_kw, 1, 1, false);
63
64 // Unpack and check parameter
65 const machine_pin_obj_t *pin = pin_find(args[0]);
66
67 if (pin->adc_list_len == 0) {
68 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Pin(%q) does not have ADC capabilities"), pin->name);
69 }
70
71 // Extract arguments
72 ADC_Type *adc_instance = pin->adc_list[0].instance; // NOTE: we only use the first ADC assignment - multiple assignments are not supported for now
73 uint8_t channel = pin->adc_list[0].channel;
74
75 // Configure ADC peripheral channel
76 adc_channel_config_t channel_config = {
77 .channelNumber = (uint32_t)channel,
78 .enableInterruptOnConversionCompleted = false,
79 };
80 ADC_SetChannelConfig(adc_instance, 0UL, &channel_config); // NOTE: we always choose channel group '0' since we only perform software triggered conversion
81
82 // Create ADC Instance
83 machine_adc_obj_t *o = m_new_obj(machine_adc_obj_t);
84 o->base.type = &machine_adc_type;
85 o->adc = adc_instance;
86 o->channel = (uint8_t)channel;
87 o->channel_group = 0;
88 o->resolution = 4096; // NOTE: currently only 12bit resolution supported
89
90 return MP_OBJ_FROM_PTR(o);
91 }
92
93 // read_u16()
machine_adc_read_u16(mp_obj_t self_in)94 STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
95 machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
96
97 // Initiate conversion
98 adc_channel_config_t channel_config = {
99 .channelNumber = self->channel,
100 .enableInterruptOnConversionCompleted = false,
101 };
102 ADC_SetChannelConfig(self->adc, (uint32_t)self->channel_group, &channel_config);
103
104 // Wait for conversion to finish
105 while (!ADC_GetChannelStatusFlags(self->adc, (uint32_t)self->channel_group)) {
106 // do nothing
107 }
108
109 // Measure input voltage
110 uint32_t value = ADC_GetChannelConversionValue(self->adc, (uint32_t)self->channel_group);
111 return MP_OBJ_NEW_SMALL_INT(value * 65535 / self->resolution);
112 }
113 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
114
115 STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = {
116 { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
117 };
118
119 STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);
120
121 const mp_obj_type_t machine_adc_type = {
122 {&mp_type_type},
123 .name = MP_QSTR_ADC,
124 .print = adc_obj_print,
125 .make_new = adc_obj_make_new,
126 .locals_dict = (mp_obj_dict_t *)&adc_locals_dict,
127 };
128
machine_adc_init(void)129 void machine_adc_init(void) {
130 for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) {
131 ADC_Type *adc_instance = adc_bases[i];
132
133 // Configure ADC perpheral
134 adc_config_t config;
135 ADC_GetDefaultConfig(&config);
136 ADC_Init(adc_instance, &config);
137
138 // Perform calibration
139 status_t calib_state = ADC_DoAutoCalibration(adc_instance);
140
141 if (calib_state == kStatus_Fail) {
142 mp_printf(&mp_plat_print, "Calibration for ADC Instance %d failed", i);
143 }
144 }
145 }
146