|
| 1 | +/**************************************************************************** |
| 2 | + * boards/xtensa/esp32/common/src/esp32_board_adc.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <syslog.h> |
| 30 | + |
| 31 | +#include <nuttx/analog/adc.h> |
| 32 | + |
| 33 | +#include "espressif/esp_adc.h" |
| 34 | + |
| 35 | +#include "esp32_board_adc.h" |
| 36 | + |
| 37 | +/**************************************************************************** |
| 38 | + * Pre-processor Definitions |
| 39 | + ****************************************************************************/ |
| 40 | + |
| 41 | +/* The number of channels for each ADC */ |
| 42 | + |
| 43 | +#define ADC_1_MAX_CHANNELS 8 |
| 44 | +#define ADC_2_MAX_CHANNELS 10 |
| 45 | + |
| 46 | +/**************************************************************************** |
| 47 | + * Private Data |
| 48 | + ****************************************************************************/ |
| 49 | + |
| 50 | +/* Select channels to be used for each ADC. |
| 51 | + * |
| 52 | + * GPIOs are fixed for each channel and configured in the lower-half driver. |
| 53 | + * |
| 54 | + * ADC 1 |
| 55 | + * Channel: 0 1 2 3 4 5 6 7 |
| 56 | + * GPIO: 36 37 38 39 32 33 34 35 |
| 57 | + * |
| 58 | + * ADC 2 |
| 59 | + * Channel: 0 1 2 3 4 5 6 7 8 9 |
| 60 | + * GPIO: 4 0 2 15 13 12 14 27 25 26 |
| 61 | + * |
| 62 | + * On the chanlist arrays below, channels are added +1. Do not change. |
| 63 | + * Important: if using more than 8 channels, edit CONFIG_ADC_FIFOSIZE. |
| 64 | + */ |
| 65 | + |
| 66 | +#ifdef CONFIG_ESPRESSIF_ADC_1 |
| 67 | +static const uint8_t g_chanlist_adc1[ADC_1_MAX_CHANNELS] = |
| 68 | +{ |
| 69 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH0 |
| 70 | + 1, |
| 71 | +#endif |
| 72 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH1 |
| 73 | + 2, |
| 74 | +#endif |
| 75 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH2 |
| 76 | + 3, |
| 77 | +#endif |
| 78 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH3 |
| 79 | + 4, |
| 80 | +#endif |
| 81 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH4 |
| 82 | + 5, |
| 83 | +#endif |
| 84 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH5 |
| 85 | + 6, |
| 86 | +#endif |
| 87 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH6 |
| 88 | + 7, |
| 89 | +#endif |
| 90 | +#ifdef CONFIG_ESPRESSIF_ADC_1_CH7 |
| 91 | + 8, |
| 92 | +#endif |
| 93 | +}; |
| 94 | +#endif |
| 95 | + |
| 96 | +#ifdef CONFIG_ESPRESSIF_ADC_2 |
| 97 | +static const uint8_t g_chanlist_adc2[ADC_2_MAX_CHANNELS] = |
| 98 | +{ |
| 99 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH0 |
| 100 | + 1, |
| 101 | +#endif |
| 102 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH1 |
| 103 | + 2, |
| 104 | +#endif |
| 105 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH2 |
| 106 | + 3, |
| 107 | +#endif |
| 108 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH3 |
| 109 | + 4, |
| 110 | +#endif |
| 111 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH4 |
| 112 | + 5, |
| 113 | +#endif |
| 114 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH5 |
| 115 | + 6, |
| 116 | +#endif |
| 117 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH6 |
| 118 | + 7, |
| 119 | +#endif |
| 120 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH7 |
| 121 | + 8, |
| 122 | +#endif |
| 123 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH8 |
| 124 | + 9, |
| 125 | +#endif |
| 126 | +#ifdef CONFIG_ESPRESSIF_ADC_2_CH9 |
| 127 | + 10 |
| 128 | +#endif |
| 129 | +}; |
| 130 | +#endif |
| 131 | + |
| 132 | +/**************************************************************************** |
| 133 | + * Private Functions |
| 134 | + ****************************************************************************/ |
| 135 | + |
| 136 | +/**************************************************************************** |
| 137 | + * Name: board_adc_register |
| 138 | + * |
| 139 | + * Description: |
| 140 | + * This function registers the ADC driver for the specified ADC channel. |
| 141 | + * It initializes the ADC hardware, creates the device name, and registers |
| 142 | + * the ADC device with the system. |
| 143 | + * |
| 144 | + * Input Parameters: |
| 145 | + * adc_num - The ADC channel number to register. |
| 146 | + * |
| 147 | + * Returned Value: |
| 148 | + * Returns zero (OK) on successful registration; a negated errno value is |
| 149 | + * returned to indicate the nature of any failure. |
| 150 | + * |
| 151 | + ****************************************************************************/ |
| 152 | + |
| 153 | +static int board_adc_register(int adc_num) |
| 154 | +{ |
| 155 | + int ret; |
| 156 | + char devname[12]; |
| 157 | + struct adc_dev_s *adcdev; |
| 158 | + |
| 159 | + adcdev = kmm_malloc(sizeof(struct adc_dev_s)); |
| 160 | + if (adcdev == NULL) |
| 161 | + { |
| 162 | + syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n"); |
| 163 | + return -ENOMEM; |
| 164 | + } |
| 165 | + |
| 166 | + memset(adcdev, 0, sizeof(struct adc_dev_s)); |
| 167 | + |
| 168 | + switch (adc_num) |
| 169 | + { |
| 170 | + case 1: |
| 171 | +#ifdef CONFIG_ESPRESSIF_ADC_1 |
| 172 | + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc1); |
| 173 | + break; |
| 174 | +#endif |
| 175 | + case 2: |
| 176 | +#ifdef CONFIG_ESPRESSIF_ADC_2 |
| 177 | + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc2); |
| 178 | + break; |
| 179 | +#endif |
| 180 | + default: |
| 181 | + syslog(LOG_ERR, "ERROR: Unsupported ADC number: %d\n", adc_num); |
| 182 | + return ERROR; |
| 183 | + } |
| 184 | + |
| 185 | + if (adcdev == NULL) |
| 186 | + { |
| 187 | + syslog(LOG_ERR, "ERROR: Failed to initialize ADC %d\n", adc_num); |
| 188 | + return -ENODEV; |
| 189 | + } |
| 190 | + |
| 191 | + /* Register the ADC driver at "/dev/adcx" */ |
| 192 | + |
| 193 | + snprintf(devname, sizeof(devname), "/dev/adc%d", adc_num - 1); |
| 194 | + ret = adc_register(devname, adcdev); |
| 195 | + if (ret < 0) |
| 196 | + { |
| 197 | + kmm_free(adcdev); |
| 198 | + syslog(LOG_ERR, "ERROR: adc_register %s failed: %d\n", devname, ret); |
| 199 | + return ret; |
| 200 | + } |
| 201 | + |
| 202 | + return ret; |
| 203 | +} |
| 204 | + |
| 205 | +/**************************************************************************** |
| 206 | + * Public Functions |
| 207 | + ****************************************************************************/ |
| 208 | + |
| 209 | +/**************************************************************************** |
| 210 | + * Name: board_adc_init |
| 211 | + * |
| 212 | + * Description: |
| 213 | + * Initialize and configuree the ADC driver for the board. |
| 214 | + * It registers the ADC channels specified in the configuration and ensures |
| 215 | + * that the ADC hardware is properly set up for use. |
| 216 | + * |
| 217 | + * Input Parameters: |
| 218 | + * None. |
| 219 | + * |
| 220 | + * Returned Value: |
| 221 | + * Returns zero (OK) on successful initialization and registration of the |
| 222 | + * ADC channels; a negated errno value is returned to indicate the nature |
| 223 | + * of any failure. |
| 224 | + * |
| 225 | + ****************************************************************************/ |
| 226 | + |
| 227 | +int board_adc_init(void) |
| 228 | +{ |
| 229 | + int ret; |
| 230 | + |
| 231 | +#ifdef CONFIG_ESPRESSIF_ADC_1 |
| 232 | + ret = board_adc_register(1); |
| 233 | + if (ret != OK) |
| 234 | + { |
| 235 | + return ret; |
| 236 | + } |
| 237 | +#endif |
| 238 | + |
| 239 | +#ifdef CONFIG_ESPRESSIF_ADC_2 |
| 240 | + ret = board_adc_register(2); |
| 241 | + if (ret != OK) |
| 242 | + { |
| 243 | + return ret; |
| 244 | + } |
| 245 | +#endif |
| 246 | + |
| 247 | + return ret; |
| 248 | +} |
0 commit comments