|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Michael Hope |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT wch_adc |
| 8 | + |
| 9 | +#include <ch32fun.h> |
| 10 | + |
| 11 | +#include <zephyr/drivers/adc.h> |
| 12 | +#include <zephyr/drivers/pinctrl.h> |
| 13 | +#include <zephyr/drivers/clock_control.h> |
| 14 | + |
| 15 | +struct adc_ch32v00x_data { |
| 16 | +}; |
| 17 | + |
| 18 | +struct adc_ch32v00x_config { |
| 19 | + ADC_TypeDef *regs; |
| 20 | + const struct pinctrl_dev_config *pin_cfg; |
| 21 | + const struct device *clock_dev; |
| 22 | + uint8_t clock_id; |
| 23 | +}; |
| 24 | + |
| 25 | +static int adc_ch32v00x_channel_setup(const struct device *dev, |
| 26 | + const struct adc_channel_cfg *channel_cfg) |
| 27 | +{ |
| 28 | + if (channel_cfg->gain != ADC_GAIN_1) { |
| 29 | + return -EINVAL; |
| 30 | + } |
| 31 | + if (channel_cfg->reference != ADC_REF_INTERNAL) { |
| 32 | + return -EINVAL; |
| 33 | + } |
| 34 | + if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) { |
| 35 | + return -EINVAL; |
| 36 | + } |
| 37 | + if (channel_cfg->differential) { |
| 38 | + return -EINVAL; |
| 39 | + } |
| 40 | + if (channel_cfg->channel_id >= 10) { |
| 41 | + return -EINVAL; |
| 42 | + } |
| 43 | + |
| 44 | + return 0; |
| 45 | +} |
| 46 | + |
| 47 | +static int adc_ch32v00x_read(const struct device *dev, const struct adc_sequence *sequence) |
| 48 | +{ |
| 49 | + const struct adc_ch32v00x_config *config = dev->config; |
| 50 | + ADC_TypeDef *regs = config->regs; |
| 51 | + uint32_t channels = sequence->channels; |
| 52 | + int rsqr = 2; |
| 53 | + int sequence_id = 0; |
| 54 | + int total_channels = 0; |
| 55 | + int i; |
| 56 | + uint16_t *samples = sequence->buffer; |
| 57 | + |
| 58 | + if (sequence->options != NULL) { |
| 59 | + return -ENOTSUP; |
| 60 | + } |
| 61 | + if (sequence->resolution != 10) { |
| 62 | + return -EINVAL; |
| 63 | + } |
| 64 | + if (sequence->oversampling != 0) { |
| 65 | + return -ENOTSUP; |
| 66 | + } |
| 67 | + if (sequence->channels >= (1 << 10)) { |
| 68 | + return -EINVAL; |
| 69 | + } |
| 70 | + |
| 71 | + if (sequence->calibrate) { |
| 72 | + regs->CTLR2 |= ADC_RSTCAL; |
| 73 | + while ((regs->CTLR2 & ADC_RSTCAL) != 0) { |
| 74 | + } |
| 75 | + regs->CTLR2 |= ADC_CAL; |
| 76 | + while ((ADC1->CTLR2 & ADC_CAL) != 0) { |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /* |
| 81 | + * Build the sample sequence. The channel IDs are packed 5 bits at a time starting in RSQR3 |
| 82 | + * and working down in memory to RSQR1. |
| 83 | + */ |
| 84 | + regs->RSQR1 = 0; |
| 85 | + regs->RSQR2 = 0; |
| 86 | + regs->RSQR3 = 0; |
| 87 | + |
| 88 | + for (i = 0; channels != 0; i++, channels >>= 1) { |
| 89 | + if ((channels & 1) != 0) { |
| 90 | + total_channels++; |
| 91 | + (®s->RSQR1)[rsqr] |= i << sequence_id; |
| 92 | + sequence_id += ADC_SQ2_0; |
| 93 | + if (sequence_id >= 32) { |
| 94 | + /* Move on to the next RSQRn register, i.e. RSQR(n-1) */ |
| 95 | + sequence_id = 0; |
| 96 | + rsqr--; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + if (total_channels == 0) { |
| 101 | + return 0; |
| 102 | + } |
| 103 | + if (sequence->buffer_size < total_channels * sizeof(*samples)) { |
| 104 | + return -ENOMEM; |
| 105 | + } |
| 106 | + |
| 107 | + /* Set the number of channels to read. Note that '0' means 'one channel'. */ |
| 108 | + regs->RSQR1 |= (total_channels - 1) * ADC_L_0; |
| 109 | + regs->CTLR2 |= ADC_SWSTART; |
| 110 | + for (i = 0; i < total_channels; i++) { |
| 111 | + while ((regs->STATR & ADC_EOC) == 0) { |
| 112 | + } |
| 113 | + *samples++ = regs->RDATAR; |
| 114 | + } |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +static int adc_ch32v00x_init(const struct device *dev) |
| 120 | +{ |
| 121 | + const struct adc_ch32v00x_config *config = dev->config; |
| 122 | + ADC_TypeDef *regs = config->regs; |
| 123 | + int err; |
| 124 | + |
| 125 | + clock_control_on(config->clock_dev, (clock_control_subsys_t)(uintptr_t)config->clock_id); |
| 126 | + |
| 127 | + err = pinctrl_apply_state(config->pin_cfg, PINCTRL_STATE_DEFAULT); |
| 128 | + if (err != 0) { |
| 129 | + return err; |
| 130 | + } |
| 131 | + |
| 132 | + /* |
| 133 | + * The default sampling time is 3 cycles and shows coupling between channels. Use 15 cycles |
| 134 | + * instead. Arbitrary. |
| 135 | + */ |
| 136 | + regs->SAMPTR2 = ADC_SMP0_1 | ADC_SMP1_1 | ADC_SMP2_1 | ADC_SMP3_1 | ADC_SMP4_1 | |
| 137 | + ADC_SMP5_1 | ADC_SMP6_1 | ADC_SMP7_1 | ADC_SMP8_1 | ADC_SMP9_1; |
| 138 | + |
| 139 | + regs->CTLR2 = ADC_ADON | ADC_EXTSEL; |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
| 144 | +#define ADC_CH32V00X_DEVICE(n) \ |
| 145 | + PINCTRL_DT_INST_DEFINE(n); \ |
| 146 | + \ |
| 147 | + static const struct adc_driver_api adc_ch32v00x_api_##n = { \ |
| 148 | + .channel_setup = adc_ch32v00x_channel_setup, \ |
| 149 | + .read = adc_ch32v00x_read, \ |
| 150 | + .ref_internal = DT_INST_PROP(n, vref_mv), \ |
| 151 | + }; \ |
| 152 | + \ |
| 153 | + static const struct adc_ch32v00x_config adc_ch32v00x_config_##n = { \ |
| 154 | + .regs = (ADC_TypeDef *)DT_INST_REG_ADDR(n), \ |
| 155 | + .pin_cfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \ |
| 156 | + .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \ |
| 157 | + .clock_id = DT_INST_CLOCKS_CELL(n, id), \ |
| 158 | + }; \ |
| 159 | + \ |
| 160 | + DEVICE_DT_INST_DEFINE(n, adc_ch32v00x_init, NULL, NULL, &adc_ch32v00x_config_##n, \ |
| 161 | + POST_KERNEL, CONFIG_ADC_INIT_PRIORITY, &adc_ch32v00x_api_##n); |
| 162 | + |
| 163 | +DT_INST_FOREACH_STATUS_OKAY(ADC_CH32V00X_DEVICE) |
0 commit comments