|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2025 Moddable Tech, Inc. |
| 3 | + * |
| 4 | + * This file is part of the Moddable SDK Runtime. |
| 5 | + * |
| 6 | + * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +/* |
| 22 | + Analog - |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "xsmc.h" // xs bindings for microcontroller |
| 26 | +#include "xsHost.h" // esp platform support |
| 27 | +#include "mc.xs.h" // for xsID_* values |
| 28 | +#include "mc.devicetree.h" |
| 29 | + |
| 30 | +#include <zephyr/device.h> |
| 31 | +#include <zephyr/devicetree.h> |
| 32 | + |
| 33 | +#include "builtinCommon.h" |
| 34 | + |
| 35 | +#if kModZephyrAnalogBusCount |
| 36 | + |
| 37 | +struct AnalogRecord { |
| 38 | + xsSlot obj; |
| 39 | + uint32_t pin; |
| 40 | + uint8_t channel; |
| 41 | + uint8_t channel_idx; |
| 42 | +// struct adc_sequence sequence; |
| 43 | +// const struct device *port; |
| 44 | +}; |
| 45 | +typedef struct AnalogRecord AnalogRecord; |
| 46 | +typedef struct AnalogRecord *Analog; |
| 47 | + |
| 48 | +#if !DT_NODE_EXISTS(DT_PATH(zephyr_user)) || \ |
| 49 | + !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channels) |
| 50 | +#error "No suitable devicetree overlay specified" |
| 51 | +#endif |
| 52 | + |
| 53 | +#define DT_SPEC_AND_COMMA(node_id, prop, idx) \ |
| 54 | + ADC_DT_SPEC_GET_BY_IDX(node_id, idx), |
| 55 | + |
| 56 | +static const struct adc_dt_spec adc_channels[] = { |
| 57 | + DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA) |
| 58 | +}; |
| 59 | + |
| 60 | +static int adc_channel_idx(int channel) { |
| 61 | + int i; |
| 62 | + for (i = 0; i < (sizeof(adc_channels)/sizeof(struct adc_dt_spec)); i++) { |
| 63 | + if (adc_channels[i].channel_id == channel) |
| 64 | + return i; |
| 65 | + } |
| 66 | + return -1; |
| 67 | +} |
| 68 | + |
| 69 | +void xs_analog_constructor_(xsMachine *the) |
| 70 | +{ |
| 71 | + Analog analog; |
| 72 | + uint32_t channel; |
| 73 | + xsSlot tmp; |
| 74 | + int err; |
| 75 | + int idx; |
| 76 | + |
| 77 | + xsmcVars(2); |
| 78 | + |
| 79 | + if (!xsmcHas(xsArg(0), xsID_port)) |
| 80 | + xsRangeError("port required"); |
| 81 | + if (!xsmcHas(xsArg(0), xsID_channel)) |
| 82 | + xsRangeError("channel required"); |
| 83 | + |
| 84 | + xsmcGet(tmp, xsArg(0), xsID_port); |
| 85 | + const struct modZephyrAnalog *port = modZephyrGetAnalog(xsmcToString(tmp)); |
| 86 | + if (NULL == port) |
| 87 | + xsRangeError("bad_port"); |
| 88 | + |
| 89 | + xsmcGet(xsVar(0), xsArg(0), xsID_channel); |
| 90 | + channel = builtinGetPin(the, &xsVar(0)); // |
| 91 | + |
| 92 | + idx = adc_channel_idx(channel); |
| 93 | + if (-1 == idx) |
| 94 | + xsRangeError("bad_channel"); |
| 95 | + if (!adc_is_ready_dt(&adc_channels[idx])) |
| 96 | + xsRangeError("adc controller not ready"); |
| 97 | + |
| 98 | + err = adc_channel_setup_dt(&adc_channels[idx]); |
| 99 | + if (err < 0) |
| 100 | + xsRangeError("could not set up adc channel"); |
| 101 | + |
| 102 | + builtinInitializeTarget(the); |
| 103 | + if (kIOFormatNumber != builtinInitializeFormat(the, kIOFormatNumber)) |
| 104 | + xsRangeError("invalid format"); |
| 105 | + |
| 106 | + analog = c_malloc(sizeof(AnalogRecord)); |
| 107 | + if (!analog) |
| 108 | + xsRangeError("no memory"); |
| 109 | + |
| 110 | + analog->obj = xsThis; |
| 111 | + xsRemember(analog->obj); |
| 112 | + xsmcSetHostData(xsThis, analog); |
| 113 | + analog->channel = channel; |
| 114 | + analog->channel_idx = idx; |
| 115 | +} |
| 116 | + |
| 117 | +void xs_analog_destructor_(void *data) |
| 118 | +{ |
| 119 | + Analog analog = data; |
| 120 | + if (!analog) |
| 121 | + return; |
| 122 | + |
| 123 | + c_free(analog); |
| 124 | +} |
| 125 | + |
| 126 | +void xs_analog_close_(xsMachine *the) |
| 127 | +{ |
| 128 | + Analog analog = xsmcGetHostData(xsThis);; |
| 129 | + if (analog && xsmcGetHostDataValidate(xsThis, xs_analog_destructor_)) { |
| 130 | + xsForget(analog->obj); |
| 131 | + xs_analog_destructor_(analog); |
| 132 | + xsmcSetHostData(xsThis, NULL); |
| 133 | + xsmcSetHostDestructor(xsThis, NULL); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +void xs_analog_read_(xsMachine *the) |
| 138 | +{ |
| 139 | + Analog analog = xsmcGetHostDataValidate(xsThis, xs_analog_destructor_); |
| 140 | + int err; |
| 141 | + uint16_t value; |
| 142 | + struct adc_sequence sequence = { |
| 143 | + .buffer = &value, |
| 144 | + .buffer_size = sizeof(value), |
| 145 | + }; |
| 146 | + |
| 147 | + (void)adc_sequence_init_dt(&adc_channels[analog->channel_idx], &sequence); |
| 148 | + err = adc_read_dt(&adc_channels[analog->channel_idx], &sequence); |
| 149 | + if (err < 0) |
| 150 | + xsUnknownError("Analog read error"); |
| 151 | + |
| 152 | + xsmcSetInteger(xsResult, value); |
| 153 | +} |
| 154 | + |
| 155 | +void xs_analog_get_resolution_(xsMachine *the) |
| 156 | +{ |
| 157 | + Analog analog = xsmcGetHostDataValidate(xsThis, xs_analog_destructor_); |
| 158 | + |
| 159 | + xsmcSetInteger(xsResult, adc_channels[analog->channel_idx].resolution); |
| 160 | +} |
| 161 | + |
| 162 | +#else // !kModZephyrAnalogBusCount |
| 163 | + |
| 164 | +void xs_analog_constructor_(xsMachine *the) |
| 165 | +{ |
| 166 | + xsUnknownError("no Analog"); |
| 167 | +} |
| 168 | + |
| 169 | +void xs_analog_destructor_(void *) {} |
| 170 | +void xs_analog_close_(xsMachine *the) {} |
| 171 | +void xs_analog_read_(xsMachine *the) {} |
| 172 | +void xs_analog_get_resolution_(xsMachine *the) {} |
| 173 | + |
| 174 | +#endif |
0 commit comments