diff --git a/.github/workflows/upload_component.yml b/.github/workflows/upload_component.yml index 797441d87..8deef17e2 100644 --- a/.github/workflows/upload_component.yml +++ b/.github/workflows/upload_component.yml @@ -64,6 +64,7 @@ jobs: components/motor/esp_simplefoc; components/motor/servo; components/openai; + components/sensors/adc/mcp3201; components/sensors/battery_fuel_gauge/max17048; components/sensors/power_measure; components/sensors/gesture/apds9960; diff --git a/.gitlab/ci/build.yml b/.gitlab/ci/build.yml index 176af50fc..e20e0e46f 100644 --- a/.gitlab/ci/build.yml +++ b/.gitlab/ci/build.yml @@ -1303,6 +1303,14 @@ build_components_sensors_power_measure_test_apps: variables: EXAMPLE_DIR: components/sensors/power_measure/test_apps +build_components_sensors_adc_mcp3201_test_apps: + extends: + - .build_examples_template + - .rules:build:components_sensors_adc_mcp3201_test_apps + - .build_idf_active_release_version + variables: + EXAMPLE_DIR: components/sensors/adc/mcp3201/test_apps + build_components_sensors_battery_fuel_gauge_max17048_test_apps: extends: - .build_examples_template diff --git a/.gitlab/ci/rules.yml b/.gitlab/ci/rules.yml index b9b55738a..47a391199 100644 --- a/.gitlab/ci/rules.yml +++ b/.gitlab/ci/rules.yml @@ -202,6 +202,10 @@ - "components/sensors/power_measure/**/*" - "components/tools/cmake_utilities/package_manager.cmake" +.patterns-components_sensors_adc_mcp3201: &patterns-components_sensors_adc_mcp3201 + - "components/sensors/adc/mcp3201/**/*" + - "tools/cmake_utilities/package_manager.cmake" + .patterns-components_sensors_battery_fuel_gauge_max17048: &patterns-components_sensors_battery_fuel_gauge_max17048 - "components/sensors/battery_fuel_gauge/max17048/**/*" - "tools/cmake_utilities/package_manager.cmake" @@ -2020,6 +2024,17 @@ - <<: *if-dev-push changes: *patterns-components_openai +.rules:build:components_sensors_adc_mcp3201_test_apps: + rules: + - <<: *if-protected + - <<: *if-label-build + - <<: *if-label-target_test + - <<: *if-trigger-job + - <<: *if-dev-push + changes: *patterns-build_system + - <<: *if-dev-push + changes: *patterns-components_sensors_adc_mcp3201 + .rules:build:components_sensors_battery_fuel_gauge_max17048_test_apps: rules: - <<: *if-protected diff --git a/README.md b/README.md index 41b79a58f..11f2a6a87 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ The registered components in ESP-IoT-Solution are listed below: | [lis2dh12](https://components.espressif.com/components/espressif/lis2dh12) | [![Component Registry](https://components.espressif.com/components/espressif/lis2dh12/badge.svg)](https://components.espressif.com/components/espressif/lis2dh12) | | [max17048](https://components.espressif.com/components/espressif/max17048) | [![Component Registry](https://components.espressif.com/components/espressif/max17048/badge.svg)](https://components.espressif.com/components/espressif/max17048) | | [mcp23017](https://components.espressif.com/components/espressif/mcp23017) | [![Component Registry](https://components.espressif.com/components/espressif/mcp23017/badge.svg)](https://components.espressif.com/components/espressif/mcp23017) | +| [mcp3201](https://components.espressif.com/components/espressif/mcp3201) | [![Component Registry](https://components.espressif.com/components/espressif/mcp3201/badge.svg)](https://components.espressif.com/components/espressif/mcp3201) | | [mvh3004d](https://components.espressif.com/components/espressif/mvh3004d) | [![Component Registry](https://components.espressif.com/components/espressif/mvh3004d/badge.svg)](https://components.espressif.com/components/espressif/mvh3004d) | | [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver) | [![Component Registry](https://components.espressif.com/components/espressif/ntc_driver/badge.svg)](https://components.espressif.com/components/espressif/ntc_driver) | | [openai](https://components.espressif.com/components/espressif/openai) | [![Component Registry](https://components.espressif.com/components/espressif/openai/badge.svg)](https://components.espressif.com/components/espressif/openai) | diff --git a/README_CN.md b/README_CN.md index c5f4413e2..914e6d84b 100644 --- a/README_CN.md +++ b/README_CN.md @@ -115,6 +115,7 @@ ESP-IoT-Solution 中注册的组件如下: | [lis2dh12](https://components.espressif.com/components/espressif/lis2dh12) | [![Component Registry](https://components.espressif.com/components/espressif/lis2dh12/badge.svg)](https://components.espressif.com/components/espressif/lis2dh12) | | [max17048](https://components.espressif.com/components/espressif/max17048) | [![Component Registry](https://components.espressif.com/components/espressif/max17048/badge.svg)](https://components.espressif.com/components/espressif/max17048) | | [mcp23017](https://components.espressif.com/components/espressif/mcp23017) | [![Component Registry](https://components.espressif.com/components/espressif/mcp23017/badge.svg)](https://components.espressif.com/components/espressif/mcp23017) | +| [mcp3201](https://components.espressif.com/components/espressif/mcp3201) | [![Component Registry](https://components.espressif.com/components/espressif/mcp3201/badge.svg)](https://components.espressif.com/components/espressif/mcp3201) | | [mvh3004d](https://components.espressif.com/components/espressif/mvh3004d) | [![Component Registry](https://components.espressif.com/components/espressif/mvh3004d/badge.svg)](https://components.espressif.com/components/espressif/mvh3004d) | | [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver) | [![Component Registry](https://components.espressif.com/components/espressif/ntc_driver/badge.svg)](https://components.espressif.com/components/espressif/ntc_driver) | | [openai](https://components.espressif.com/components/espressif/openai) | [![Component Registry](https://components.espressif.com/components/espressif/openai/badge.svg)](https://components.espressif.com/components/espressif/openai) | diff --git a/components/.build-rules.yml b/components/.build-rules.yml index 7ff2cd3a7..617ef8d5a 100644 --- a/components/.build-rules.yml +++ b/components/.build-rules.yml @@ -70,6 +70,10 @@ components/openai/test_apps: enable: - if: INCLUDE_DEFAULT == 1 +components/sensors/adc/mcp3201: + enable: + - if: IDF_TARGET in ["esp32s3"] + components/sensors/radar/at581x/test_apps: enable: - if: INCLUDE_DEFAULT == 1 diff --git a/components/sensors/adc/mcp3201/CHANGELOG.md b/components/sensors/adc/mcp3201/CHANGELOG.md new file mode 100644 index 000000000..4bcb8374f --- /dev/null +++ b/components/sensors/adc/mcp3201/CHANGELOG.md @@ -0,0 +1,8 @@ +# ChangeLog + + +## v0.1.0 - 2025-1-16 + +### Enhancements: + +* Initial version. diff --git a/components/sensors/adc/mcp3201/CMakeLists.txt b/components/sensors/adc/mcp3201/CMakeLists.txt new file mode 100644 index 000000000..033bffe32 --- /dev/null +++ b/components/sensors/adc/mcp3201/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register(SRCS "mcp3201.c" + INCLUDE_DIRS include) + +include(package_manager) +cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR}) diff --git a/components/sensors/adc/mcp3201/README.md b/components/sensors/adc/mcp3201/README.md new file mode 100644 index 000000000..a4a23fd15 --- /dev/null +++ b/components/sensors/adc/mcp3201/README.md @@ -0,0 +1,40 @@ +# Component: MCP3201 + +MCP3201 device is a successive approximation 12-bit Analog-to-Digital (A/D) Converter with on-board sample and hold circuitry. The device provides a single pseudo-differential input.Differential Nonlinearity (DNL) is specified at ±1 LSB, and Integral Nonlinearity (INL) is offered in ±1 LSB (MCP3201-B) and ±2 LSB (MCP3201-C) versions. Communication with the device is done using a simple serial interface compatible with the SPI protocol. The device is capable of sample rates of up to 100 ksps at a clock rate of 1.6 MHz. The MCP3201 device operates over a broad voltage range (2.7V-5.5V).Low-current design permits operation with typical standby and active currents of only 500 nA and 300 μA, respectively. + +## Add component to your project + +Please use the component manager command `add-dependency` to add the `mcp3201` to your project's dependency, during the `CMake` step the component will be downloaded automatically + +``` +idf.py add-dependency "espressif/mcp3201=*" +``` + +## Example of MCP3201 usage + +```c +static spi_bus_handle_t spi_bus = NULL; +static mcp3201_handle_t mcp3201 = NULL; +int16_t data; + +// Step1: Init SPI bus +spi_config_t bus_conf = { + .miso_io_num = SPI_MISO_IO, + .mosi_io_num = -1, + .sclk_io_num = SPI_SCLK_IO, +}; +spi_bus = spi_bus_create(SPI2_HOST, &bus_conf); +TEST_ASSERT(spi_bus != NULL); + +// Step2: Init MCP3201 +spi_device_config_t dev_cfg = { + .mode = 0, + .clock_speed_hz = SPI_FREQ_HZ, + .cs_io_num = SPI_CS_IO, +}; +mcp3201 = mcp3201_create(spi_bus, &dev_cfg); + +// Step3: Read ADC value +mcp3201_get_data(mcp3201, &data); +ESP_LOGI(TAG, "MCP3201:%d,Convert:%.2f", data, 1.0f * data * (3.3f / 4096)); +``` diff --git a/components/sensors/adc/mcp3201/idf_component.yml b/components/sensors/adc/mcp3201/idf_component.yml new file mode 100644 index 000000000..c1c35d555 --- /dev/null +++ b/components/sensors/adc/mcp3201/idf_component.yml @@ -0,0 +1,12 @@ +version: 0.1.0 +description: SPI driver for MCP3201 adc sensor +url: https://github.com/espressif/esp-iot-solution/tree/master/components/sensor/adc/mcp3201 +repository: https://github.com/espressif/esp-iot-solution.git +documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/sensors/adc.html +issues: https://github.com/espressif/esp-iot-solution/issues +dependencies: + spi_bus: + public: true + override_path: "../../../spi_bus" + idf: '>=4.4' + cmake_utilities: "0.*" diff --git a/components/sensors/adc/mcp3201/include/mcp3201.h b/components/sensors/adc/mcp3201/include/mcp3201.h new file mode 100644 index 000000000..1a516c69c --- /dev/null +++ b/components/sensors/adc/mcp3201/include/mcp3201.h @@ -0,0 +1,53 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include "spi_bus.h" + +typedef void *mcp3201_handle_t; + +/** + * @brief Create and init sensor object and return a sensor handle + * + * @param spi_bus SPI bus handle + * @param dev_cfg SPI device configuration + * + * @return + * - sht3x handle_t + */ +mcp3201_handle_t mcp3201_create(spi_bus_handle_t spi_bus, spi_device_config_t *dev_cfg); + +/** + * @brief Delete and release a sensor object + * + * @param sensor Point to object handle of mcp3201 + * + * @return + * - ESP_OK Success + * - ESP_FAIL Fail + */ +esp_err_t mcp3201_delete(mcp3201_handle_t *sensor); + +/** + * @brief Get device data of MCP3201 + * + * @param sensor Point to object handle of mcp3201 + * @param data adc data + * + * @return + * - ESP_OK Success + * - ESP_FAIL Fail + */ +esp_err_t mcp3201_get_data(mcp3201_handle_t sensor, int16_t *data); + +#ifdef __cplusplus +} +#endif diff --git a/components/sensors/adc/mcp3201/license.txt b/components/sensors/adc/mcp3201/license.txt new file mode 100644 index 000000000..d64569567 --- /dev/null +++ b/components/sensors/adc/mcp3201/license.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/components/sensors/adc/mcp3201/mcp3201.c b/components/sensors/adc/mcp3201/mcp3201.c new file mode 100644 index 000000000..fc89e710c --- /dev/null +++ b/components/sensors/adc/mcp3201/mcp3201.c @@ -0,0 +1,51 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "mcp3201.h" + +typedef struct { + spi_bus_device_handle_t spi_dev; +} mcp3201_sensor_t; + +mcp3201_handle_t mcp3201_create(spi_bus_handle_t spi_bus, spi_device_config_t *dev_cfg) +{ + mcp3201_sensor_t *sens = (mcp3201_sensor_t *) calloc(1, sizeof(mcp3201_sensor_t)); + sens->spi_dev = spi_bus_device_create(spi_bus, dev_cfg); + if (sens->spi_dev == NULL) { + free(sens); + return NULL; + } + return (mcp3201_handle_t) sens; +} + +esp_err_t mcp3201_delete(mcp3201_handle_t *sensor) +{ + if (*sensor == NULL) { + return ESP_OK; + } + + mcp3201_sensor_t *sens = (mcp3201_sensor_t *)(*sensor); + spi_bus_device_delete(&sens->spi_dev); + free(sens); + *sensor = NULL; + return ESP_OK; +} + +esp_err_t mcp3201_get_data(mcp3201_handle_t sensor, int16_t *data) +{ + mcp3201_sensor_t *sens = (mcp3201_sensor_t *) sensor; + uint8_t rbuf[3] = {0}; + esp_err_t ret = spi_bus_transfer_bytes(sens->spi_dev, rbuf, NULL, 3); + + if (ret != ESP_OK) { + *data = -1; + return ret; + } + *data = ((rbuf[0] & 0x1F) << 7) + (rbuf[1] >> 1); + return ESP_OK; +} diff --git a/components/sensors/adc/mcp3201/test_apps/CMakeLists.txt b/components/sensors/adc/mcp3201/test_apps/CMakeLists.txt new file mode 100644 index 000000000..9b9c94459 --- /dev/null +++ b/components/sensors/adc/mcp3201/test_apps/CMakeLists.txt @@ -0,0 +1,8 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components" + "../../mcp3201") +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mcp3201_test) diff --git a/components/sensors/adc/mcp3201/test_apps/main/CMakeLists.txt b/components/sensors/adc/mcp3201/test_apps/main/CMakeLists.txt new file mode 100644 index 000000000..544fe17bd --- /dev/null +++ b/components/sensors/adc/mcp3201/test_apps/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRC_DIRS "." + INCLUDE_DIRS "." + REQUIRES unity test_utils driver mcp3201) diff --git a/components/sensors/adc/mcp3201/test_apps/main/mcp3201_test.c b/components/sensors/adc/mcp3201/test_apps/main/mcp3201_test.c new file mode 100644 index 000000000..0139b3ae8 --- /dev/null +++ b/components/sensors/adc/mcp3201/test_apps/main/mcp3201_test.c @@ -0,0 +1,110 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include "unity.h" +#include "mcp3201.h" + +#define TEST_MEMORY_LEAK_THRESHOLD (-500) + +static size_t before_free_8bit; +static size_t before_free_32bit; + +#define SPI_MISO_IO (gpio_num_t)(2) +#define SPI_SCLK_IO (gpio_num_t)(3) +#define SPI_CS_IO (gpio_num_t)(4) +#define SPI_FREQ_HZ (10 * 1000 * 1000) + +static const char *TAG = "MCP3201"; +static spi_bus_handle_t spi_bus = NULL; +static mcp3201_handle_t mcp3201 = NULL; + +TEST_CASE("mcp3201 init-deinit test", "[spi][sensor][mcp3201]") +{ + spi_config_t bus_conf = { + .miso_io_num = SPI_MISO_IO, + .mosi_io_num = -1, + .sclk_io_num = SPI_SCLK_IO, + }; + spi_bus = spi_bus_create(SPI2_HOST, &bus_conf); + TEST_ASSERT(spi_bus != NULL); + + spi_device_config_t dev_cfg = { + .mode = 0, + .clock_speed_hz = SPI_FREQ_HZ, + .cs_io_num = SPI_CS_IO, + }; + + mcp3201 = mcp3201_create(spi_bus, &dev_cfg); + mcp3201_delete(&mcp3201); + spi_bus_delete(&spi_bus); +} + +TEST_CASE("mcp3201 get data test", "[spi][sensor][mcp3201]") +{ + int16_t data; + spi_config_t bus_conf = { + .miso_io_num = SPI_MISO_IO, + .mosi_io_num = -1, + .sclk_io_num = SPI_SCLK_IO, + }; + spi_bus = spi_bus_create(SPI2_HOST, &bus_conf); + TEST_ASSERT(spi_bus != NULL); + + spi_device_config_t dev_cfg = { + .mode = 0, + .clock_speed_hz = SPI_FREQ_HZ, + .cs_io_num = SPI_CS_IO, + }; + + mcp3201 = mcp3201_create(spi_bus, &dev_cfg); + + for (int i = 0; i < 50; i++) { + mcp3201_get_data(mcp3201, &data); + ESP_LOGI(TAG, "MCP3201:%d,Convert:%.2f", data, 1.0f * data * (3.3f / 4096)); + vTaskDelay(pdMS_TO_TICKS(100)); + } + + mcp3201_delete(&mcp3201); + spi_bus_delete(&spi_bus); +} + +static void check_leak(size_t before_free, size_t after_free, const char *type) +{ + ssize_t delta = after_free - before_free; + printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); + TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); +} + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +void tearDown(void) +{ + size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + check_leak(before_free_8bit, after_free_8bit, "8BIT"); + check_leak(before_free_32bit, after_free_32bit, "32BIT"); +} + +void app_main(void) +{ + printf(" ____ ____ ______ _______ ______ _____ ____ __ \n"); + printf("|_ \\ / _|.' ___ ||_ __ \\ / ____ `. / ___ `. .' '. / | \n"); + printf(" | \\/ | / .' \\_| | |__) |`' __) ||_/___) || .--. |`| | \n"); + printf(" | |\\ /| | | | | ___/ _ |__ '. .'____.'| | | | | | \n"); + printf(" _| |_\\/_| | \\ `.___.'\\ _| |_ | \\____) |/ /_____ | `--' |_| |_ \n"); + printf("|_____||_____| `.____ .'|_____| \\______.'|_______| '.____.'|_____| \n"); + unity_run_menu(); +} diff --git a/components/sensors/adc/mcp3201/test_apps/sdkconfig.defaults b/components/sensors/adc/mcp3201/test_apps/sdkconfig.defaults new file mode 100644 index 000000000..62e98b0aa --- /dev/null +++ b/components/sensors/adc/mcp3201/test_apps/sdkconfig.defaults @@ -0,0 +1,4 @@ +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y +CONFIG_FREERTOS_HZ=1000 +CONFIG_ESP_TASK_WDT_EN=n +CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=4096 diff --git a/docs/en/sensors/adc.rst b/docs/en/sensors/adc.rst new file mode 100644 index 000000000..cce2ec8f8 --- /dev/null +++ b/docs/en/sensors/adc.rst @@ -0,0 +1,14 @@ +ADC Sensor +============ +:link_to_translation:`zh_CN:[中文]` + +ADC sensor is a device that converts analog voltage signals into digital signals, widely used in data acquisition and signal processing systems for precise monitoring and analysis of voltage variations. + +Adapted Products +--------------------- + ++---------+----------+-----+-----------+----------------------------------------------------------------------------+-----+ +| Name | Function | Bus | Vendor | Datasheet | HAL | ++=========+==========+=====+===========+============================================================================+=====+ +| MCP3201 | ADC | SPI | Microchip | `Datesheet `__ | | ++---------+----------+-----+-----------+----------------------------------------------------------------------------+-----+ diff --git a/docs/en/sensors/index.rst b/docs/en/sensors/index.rst index 67b0c0e94..4cf743d59 100644 --- a/docs/en/sensors/index.rst +++ b/docs/en/sensors/index.rst @@ -16,3 +16,4 @@ Sensors Power Monitor Power Measure Battery Fuel Gauge + ADC Sensor diff --git a/docs/zh_CN/sensors/adc.rst b/docs/zh_CN/sensors/adc.rst new file mode 100644 index 000000000..00a23c6b5 --- /dev/null +++ b/docs/zh_CN/sensors/adc.rst @@ -0,0 +1,14 @@ +ADC传感器 +=========== +:link_to_translation:`en:[English]` + +ADC 传感器是一种将模拟电压信号转换为数字信号的传感器,广泛应用于数据采集和信号处理系统中,实现对电压变化的精准监测和分析。 + +已适配列表 +---------- + ++---------+------+------+-----------+-------------------------------------------------------------------------+------------+ +| 名称 | 功能 | 总线 | 供应商 | 规格书 | 硬件抽象层 | ++=========+======+======+===========+=========================================================================+============+ +| MCP3201 | ADC | SPI | Microchip | `规格书 `__ | | ++---------+------+------+-----------+-------------------------------------------------------------------------+------------+ diff --git a/docs/zh_CN/sensors/index.rst b/docs/zh_CN/sensors/index.rst index c931df543..3116fa3e3 100644 --- a/docs/zh_CN/sensors/index.rst +++ b/docs/zh_CN/sensors/index.rst @@ -16,3 +16,4 @@ 功率监视器 功率测量 电池电量计 + ADC传感器