Skip to content

Commit

Permalink
Merge pull request #6 from gfurtadoalmeida/fast-simple-read
Browse files Browse the repository at this point in the history
Split read and sample functions.
  • Loading branch information
gfurtadoalmeida authored Jan 12, 2025
2 parents fddf279 + bede8bc commit 0ba813e
Show file tree
Hide file tree
Showing 14 changed files with 935 additions and 356 deletions.
30 changes: 28 additions & 2 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,35 @@ jobs:
shell: pwsh
run: docker pull ${{env.ESP_DOCKER_IMAGE}}

- name: Build with Sonar
- name: Build Push with Sonar
if: ${{github.event_name == 'push'}}
shell: pwsh
run: docker run --rm --env SONARCLOUD_ORGANIZATION=${{secrets.SONARCLOUD_ORGANIZATION}} --env SONARCLOUD_TOKEN=${{secrets.SONARCLOUD_TOKEN}} --env LC_ALL='C.UTF-8' -v ${{github.workspace}}:/project -w /project ${{env.ESP_DOCKER_IMAGE}} idf.py build
run: |
docker run `
--rm `
--env EDS_ORG=${{secrets.SONARCLOUD_ORGANIZATION}} `
--env EDS_TOKEN=${{secrets.SONARCLOUD_TOKEN}} `
--env EDS_BRANCH=${{github.ref_name}} `
--env LC_ALL='C.UTF-8' `
-v ${{github.workspace}}:/project `
-w /project ${{env.ESP_DOCKER_IMAGE}} `
idf.py build
- name: Build PR with Sonar
if: ${{github.event_name == 'pull_request'}}
shell: pwsh
run: |
docker run `
--rm `
--env EDS_ORG=${{secrets.SONARCLOUD_ORGANIZATION}} `
--env EDS_TOKEN=${{secrets.SONARCLOUD_TOKEN}} `
--env EDS_PR_KEY=${{github.event.pull_request.number}} `
--env EDS_PR_BRANCH=${{github.head_ref}} `
--env EDS_PR_BASE=${{github.base_ref}} `
--env LC_ALL='C.UTF-8' `
-v ${{github.workspace}}:/project `
-w /project ${{env.ESP_DOCKER_IMAGE}} `
idf.py build
- name: Build Test
shell: pwsh
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
with:
allowUpdates: true
artifacts: "${{env.ZIP_NAME}}"
generateReleaseNotes: true
makeLatest: true
name: ESP32 Driver MCP320X ${{github.ref_name}}
removeArtifacts: false
Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ESP32 driver for Microchip [MCP3204](https://www.microchip.com/en-us/product/MCP

* ESP-IDF: [v5.3](https://docs.espressif.com/projects/esp-idf/en/v5.3/esp32/index.html)
* Written in **C** using just the [ESP-IDF Framework](https://github.com/espressif/esp-idf).
* Testable: 20+ tests.
* Testable: 30+ tests.

## Documentation

Expand Down Expand Up @@ -39,23 +39,23 @@ void app_main(void)
.mosi_io_num = GPIO_NUM_23,
.miso_io_num = GPIO_NUM_19,
.sclk_io_num = GPIO_NUM_18,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.data4_io_num = -1,
.data5_io_num = -1,
.data6_io_num = -1,
.data7_io_num = -1,
.quadwp_io_num = GPIO_NUM_NC,
.quadhd_io_num = GPIO_NUM_NC,
.data4_io_num = GPIO_NUM_NC,
.data5_io_num = GPIO_NUM_NC,
.data6_io_num = GPIO_NUM_NC,
.data7_io_num = GPIO_NUM_NC,
.max_transfer_sz = 3, // 24 bits.
.flags = SPICOMMON_BUSFLAG_MASTER,
.isr_cpu_id = INTR_CPU_ID_AUTO,
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
.intr_flags = ESP_INTR_FLAG_LEVEL3};

mcp320x_config_t mcp320x_cfg = {
.host = SPI3_HOST,
.device_model = MCP3204_MODEL,
.clock_speed_hz = 1 * 1000 * 1000, // 1 Mhz.
.reference_voltage = 5000, // 5V
.cs_io_num = GPIO_NUM_5};
.cs_io_num = GPIO_NUM_22};

// Bus initialization is up to the developer.
spi_bus_initialize(mcp320x_cfg.host, &bus_cfg, 0);
Expand All @@ -71,11 +71,11 @@ void app_main(void)
for (size_t i = 0; i < 10; i++)
{
// Read voltage, sampling 1000 times.
mcp320x_read_voltage(mcp320x_handle,
MCP320X_CHANNEL_0,
MCP320X_READ_MODE_SINGLE,
1000,
&voltage);
mcp320x_sample_voltage(mcp320x_handle,
MCP320X_CHANNEL_0,
MCP320X_READ_MODE_SINGLE,
1000,
&voltage);

ESP_LOGI("mcp320x", "Voltage: %d mV", voltage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ extern "C"

// Constants

#define MCP320X_RESOLUTION 4096 /** @brief ADC resolution = 12 bits = 2^12 = 4096 steps */
#define MCP320X_CLOCK_MIN_HZ 10000 /** @brief Minimum recommended clock speed for a reliable reading = 10Khz. */
#define MCP320X_CLOCK_MAX_HZ 2000000 /** @brief Maximum clock speed supported = 2Mhz at 5V. */
#define MCP320X_REF_VOLTAGE_MIN 250 /** @brief Minimum reference voltage, in mV = 250mV. */
#define MCP320X_REF_VOLTAGE_MAX 7000 /** @brief Maximum reference voltage, in mV = 7000mV. The max safe voltage is 5000mV. */
#define MCP320X_RESOLUTION 4096 /** @brief ADC resolution = 12 bits = 2^12 = 4096 steps */
#define MCP320X_CLOCK_MIN_HZ (10 * 1000) /** @brief Minimum recommended clock speed for a reliable reading = 10Khz. */
#define MCP320X_CLOCK_MAX_HZ (2 * 1000 * 1000) /** @brief Maximum clock speed supported = 2Mhz at 5V. */
#define MCP320X_REF_VOLTAGE_MIN 250 /** @brief Minimum reference voltage, in mV = 250mV. */
#define MCP320X_REF_VOLTAGE_MAX 7000 /** @brief Maximum reference voltage, in mV = 7000mV. The max safe voltage is 5000mV. */

// Result codes

Expand Down Expand Up @@ -129,24 +129,20 @@ extern "C"
* @param[out] frequency_hz Pointer to where the frequency in Hertz will be stored.
* @return MCP320X_OK when success, otherwise any MCP320X_ERR* code.
*/
mcp320x_err_t mcp320x_get_actual_freq(mcp320x_t *handle,
uint32_t *frequency_hz);
mcp320x_err_t mcp320x_get_actual_freq(mcp320x_t *handle, uint32_t *frequency_hz);

/**
* @brief Read a digital code from 0 to 4096 (MCP320X_RESOLUTION).
* @note For high \p sample_count it's recommended to aquire the SPI bus using the @ref mcp320x_acquire function.
* @note This function is not thread safe when multiple tasks access the same SPI device.
* @param[in] handle MCP320X handle.
* @param[in] channel Channel to read from.
* @param[in] read_mode Read mode.
* @param[in] sample_count How many samples to take.
* @param[out] value Pointer to where the value will be stored.
* @return MCP320X_OK when success, otherwise any MCP320X_ERR* code.
*/
mcp320x_err_t mcp320x_read(mcp320x_t *handle,
mcp320x_channel_t channel,
mcp320x_read_mode_t read_mode,
uint16_t sample_count,
uint16_t *value);

/**
Expand All @@ -155,16 +151,48 @@ extern "C"
* @param[in] handle MCP320X handle.
* @param[in] channel Channel to read from.
* @param[in] read_mode Read mode.
* @param[in] sample_count How many samples to take.
* @param[out] voltage Pointer to where the value will be stored.
* @return MCP320X_OK when success, otherwise any MCP320X_ERR* code.
*/
mcp320x_err_t mcp320x_read_voltage(mcp320x_t *handle,
mcp320x_channel_t channel,
mcp320x_read_mode_t read_mode,
uint16_t sample_count,
uint16_t *voltage);

/**
* @brief Sample a channel, returning a digital code from 0 to 4096 (MCP320X_RESOLUTION).
* @note For high \p sample_count it's recommended to aquire the SPI bus using the @ref mcp320x_acquire function.
* @note This function is not thread safe when multiple tasks access the same SPI device.
* @param[in] handle MCP320X handle.
* @param[in] channel Channel to read from.
* @param[in] read_mode Read mode.
* @param[in] sample_count How many samples to take.
* @param[out] value Pointer to where the value will be stored.
* @return MCP320X_OK when success, otherwise any MCP320X_ERR* code.
*/
mcp320x_err_t mcp320x_sample(mcp320x_t *handle,
mcp320x_channel_t channel,
mcp320x_read_mode_t read_mode,
uint16_t sample_count,
uint16_t *value);

/**
* @brief Sample a channel, returning a voltage, in millivolts.
* @note For high \p sample_count it's recommended to aquire the SPI bus using the @ref mcp320x_acquire function.
* @note This function is not thread safe when multiple tasks access the same SPI device.
* @param[in] handle MCP320X handle.
* @param[in] channel Channel to read from.
* @param[in] read_mode Read mode.
* @param[in] sample_count How many samples to take.
* @param[out] voltage Pointer to where the value will be stored.
* @return MCP320X_OK when success, otherwise any MCP320X_ERR* code.
*/
mcp320x_err_t mcp320x_sample_voltage(mcp320x_t *handle,
mcp320x_channel_t channel,
mcp320x_read_mode_t read_mode,
uint16_t sample_count,
uint16_t *voltage);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 0ba813e

Please sign in to comment.