Skip to content

Commit

Permalink
Merge branch 'feature/add_spi_adc_sensor_mcp3201' into 'master'
Browse files Browse the repository at this point in the history
add adc sensor: mcp3201

See merge request ae_group/esp-iot-solution!913
  • Loading branch information
YanKE01 committed Jan 16, 2025
2 parents eaa7262 + d31f899 commit 87637c7
Show file tree
Hide file tree
Showing 21 changed files with 556 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/upload_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions .gitlab/ci/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions .gitlab/ci/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
4 changes: 4 additions & 0 deletions components/.build-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions components/sensors/adc/mcp3201/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ChangeLog


## v0.1.0 - 2025-1-16

### Enhancements:

* Initial version.
5 changes: 5 additions & 0 deletions components/sensors/adc/mcp3201/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
idf_component_register(SRCS "mcp3201.c"
INCLUDE_DIRS include)

include(package_manager)
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})
40 changes: 40 additions & 0 deletions components/sensors/adc/mcp3201/README.md
Original file line number Diff line number Diff line change
@@ -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));
```
12 changes: 12 additions & 0 deletions components/sensors/adc/mcp3201/idf_component.yml
Original file line number Diff line number Diff line change
@@ -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.*"
53 changes: 53 additions & 0 deletions components/sensors/adc/mcp3201/include/mcp3201.h
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 87637c7

Please sign in to comment.