Thu, 06 Apr 2023 20:53:06 +0200
Set the new measured deep sleep current consumption. This is half of the Wemos D1 system.
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /* |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (c) 2020 Ruslan V. Uss <unclerus@gmail.com> |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Redistribution and use in source and binary forms, with or without |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * modification, are permitted provided that the following conditions are met: |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * 1. Redistributions of source code must retain the above copyright notice, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * this list of conditions and the following disclaimer. |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * this list of conditions and the following disclaimer in the documentation |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * and/or other materials provided with the distribution. |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * 3. Neither the name of the copyright holder nor the names of itscontributors |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * may be used to endorse or promote products derived from this software without |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * specific prior written permission. |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | */ |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | /** |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | * @file mcp342x.c |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | * |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | * ESP-IDF driver for 18-Bit, multi-channel delta-sigma |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | * ADC MCP3426/MCP3427/MCP3428 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | * |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | * Copyright (c) 2020 Ruslan V. Uss <unclerus@gmail.com> |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | * |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | * BSD Licensed as described in the file LICENSE |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | */ |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | #include <esp_idf_lib_helpers.h> |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | #include <esp_log.h> |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | #include <freertos/FreeRTOS.h> |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | #include <freertos/task.h> |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | #include "mcp342x.h" |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | #define I2C_FREQ_HZ 400000 // 400kHz |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | static const char *TAG = "mcp342x"; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | #define BV(x) (1 << (x)) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | #define BIT_RDY BV(7) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | #define BIT_MODE BV(4) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | #define POS_CHAN 5 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | #define POS_SR 2 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | #define POS_GAIN 0 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | #define MASK_VAL 3 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | #define SIGN12 0xfffff000 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | #define SIGN14 0xffffc000 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | #define SIGN16 0xffff0000 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | #define SIGN18 0xfffc0000 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | #define CHECK(x) do { esp_err_t __; if ((__ = x) != ESP_OK) return __; } while (0) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | #define CHECK_ARG(VAL) do { if (!(VAL)) return ESP_ERR_INVALID_ARG; } while (0) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | static const uint32_t sample_time[] = { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | [MCP342X_RES_12] = 4167, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | [MCP342X_RES_14] = 16667, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | [MCP342X_RES_16] = 66667, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | [MCP342X_RES_18] = 266667 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | }; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | static const float lsb[] = { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | [MCP342X_RES_12] = 0.001, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | [MCP342X_RES_14] = 0.00025, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | [MCP342X_RES_16] = 0.0000625, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | [MCP342X_RES_18] = 0.000015625 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | }; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | static const int gain_val[] = { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | [MCP342X_GAIN1] = 1, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | [MCP342X_GAIN2] = 2, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | [MCP342X_GAIN4] = 4, |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | [MCP342X_GAIN8] = 8 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | }; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | static void get_cfg(mcp342x_t *dev, uint8_t reg) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | dev->mode = (reg & BIT_MODE) ? MCP342X_CONTINUOUS : MCP342X_ONESHOT; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | dev->channel = (reg >> POS_CHAN) & MASK_VAL; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | dev->resolution = (reg >> POS_SR) & MASK_VAL; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | dev->gain = (reg >> POS_GAIN) & MASK_VAL; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | static inline uint8_t get_reg(mcp342x_t *dev) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
97 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
98 | return (dev->mode == MCP342X_CONTINUOUS ? BIT_MODE : 0) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | | ((dev->channel & MASK_VAL) << POS_CHAN) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
100 | | ((dev->resolution & MASK_VAL) << POS_SR) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
101 | | ((dev->gain & MASK_VAL) << POS_GAIN); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
102 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
104 | /////////////////////////////////////////////////////////////////////////////// |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | esp_err_t mcp342x_init_desc(mcp342x_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
108 | CHECK_ARG(dev && addr >= MCP342X_ADDR_MIN && addr <= MCP342X_ADDR_MAX); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | dev->i2c_dev.port = port; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
111 | dev->i2c_dev.addr = addr; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
112 | dev->i2c_dev.cfg.sda_io_num = sda_gpio; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
113 | dev->i2c_dev.cfg.scl_io_num = scl_gpio; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
114 | #if HELPER_TARGET_IS_ESP32 |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
115 | dev->i2c_dev.cfg.master.clk_speed = I2C_FREQ_HZ; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
116 | #endif |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
117 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
118 | return i2c_dev_create_mutex(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
119 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
120 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
121 | esp_err_t mcp342x_free_desc(mcp342x_t *dev) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
122 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
123 | CHECK_ARG(dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
124 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | return i2c_dev_delete_mutex(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
126 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
127 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
128 | esp_err_t mcp342x_get_data(mcp342x_t *dev, int32_t *data, bool *ready) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
129 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
130 | CHECK_ARG(dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
131 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
132 | uint8_t buf[4] = { 0 }; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
134 | I2C_DEV_TAKE_MUTEX(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
135 | I2C_DEV_CHECK(&dev->i2c_dev, i2c_dev_read(&dev->i2c_dev, NULL, 0, buf, sizeof(buf))); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
136 | I2C_DEV_GIVE_MUTEX(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
137 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
138 | uint8_t reg = buf[(buf[3] & (MCP342X_RES_18 << POS_SR)) == (MCP342X_RES_18 << POS_SR) ? 3 : 2]; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
139 | get_cfg(dev, reg); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
140 | if (ready) *ready = !(reg & BIT_RDY); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
141 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
142 | if (!data) return ESP_OK; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
143 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
144 | uint32_t r = 0; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
145 | switch (dev->resolution) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
146 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
147 | case MCP342X_RES_12: |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
148 | r = (buf[0] << 8) | buf[1]; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
149 | if (r & BV(11)) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
150 | r |= SIGN12; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
151 | break; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
152 | case MCP342X_RES_14: |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
153 | r = (buf[0] << 8) | buf[1]; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
154 | if (r & BV(13)) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
155 | r |= SIGN14; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
156 | break; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
157 | case MCP342X_RES_16: |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
158 | r = (buf[0] << 8) | buf[1]; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
159 | if (r & BV(15)) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
160 | r |= SIGN16; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
161 | break; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
162 | case MCP342X_RES_18: |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
163 | r = (buf[0] << 16) | (buf[1] << 8) | buf[2]; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
164 | if (r & BV(17)) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
165 | r |= SIGN18; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
166 | break; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
167 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
168 | *data = *((int32_t *)&r); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
169 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
170 | return ESP_OK; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
171 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
172 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
173 | esp_err_t mcp342x_get_voltage(mcp342x_t *dev, float *volts, bool *ready) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
174 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
175 | CHECK_ARG(volts); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
176 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
177 | int32_t raw; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
178 | CHECK(mcp342x_get_data(dev, &raw, ready)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
179 | *volts = lsb[dev->resolution] * raw / gain_val[dev->gain]; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
180 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
181 | return ESP_OK; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
182 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
183 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
184 | esp_err_t mcp342x_get_sample_time_us(mcp342x_t *dev, uint32_t *us) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
185 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
186 | CHECK_ARG(dev && us && dev->resolution <= MCP342X_RES_18); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
187 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
188 | *us = sample_time[dev->resolution]; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
189 | return ESP_OK; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
190 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
191 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
192 | esp_err_t mcp342x_set_config(mcp342x_t *dev) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
193 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
194 | CHECK_ARG(dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
195 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
196 | uint8_t r = get_reg(dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
197 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
198 | I2C_DEV_TAKE_MUTEX(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
199 | I2C_DEV_CHECK(&dev->i2c_dev, i2c_dev_write(&dev->i2c_dev, NULL, 0, &r, 1)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
200 | I2C_DEV_GIVE_MUTEX(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
201 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
202 | return ESP_OK; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
203 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
204 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
205 | esp_err_t mcp342x_get_config(mcp342x_t *dev) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
206 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
207 | return mcp342x_get_data(dev, NULL, NULL); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
208 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
209 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
210 | esp_err_t mcp342x_start_conversion(mcp342x_t *dev) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
211 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
212 | CHECK_ARG(dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
214 | uint8_t r = get_reg(dev) | BIT_RDY; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
215 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
216 | I2C_DEV_TAKE_MUTEX(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
217 | I2C_DEV_CHECK(&dev->i2c_dev, i2c_dev_write(&dev->i2c_dev, NULL, 0, &r, 1)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
218 | I2C_DEV_GIVE_MUTEX(&dev->i2c_dev); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
219 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
220 | return ESP_OK; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
221 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
222 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
223 | esp_err_t mcp342x_oneshot_conversion(mcp342x_t *dev, int32_t *data) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
224 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
225 | CHECK_ARG(dev && data); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
226 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
227 | dev->mode = MCP342X_ONESHOT; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
228 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
229 | uint32_t st; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
230 | CHECK(mcp342x_get_sample_time_us(dev, &st)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
231 | CHECK(mcp342x_start_conversion(dev)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
232 | vTaskDelay(pdMS_TO_TICKS(st / 1000 + 1)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
233 | bool ready; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
234 | CHECK(mcp342x_get_data(dev, data, &ready)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
235 | if (!ready) |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
236 | { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
237 | ESP_LOGE(TAG, "Data not ready"); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
238 | return ESP_FAIL; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
239 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
240 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
241 | return ESP_OK; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
242 | } |