Skip to content

Commit 1d205e9

Browse files
linguini1xiaoxiang781216
authored andcommitted
sensors/mcp9600: Converted MCP9600 legacy driver to UORB driver as per suggestions on PR #15525.
1 parent e5a7f55 commit 1d205e9

File tree

8 files changed

+501
-367
lines changed

8 files changed

+501
-367
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
=======
12
MCP9600
23
=======
34

45
Contributed by Matteo Golin.
56

6-
The MCP9600 is a thermocouple EMF to temperature converter made by Microchip. It is also sold as a `breakout board module
7-
by Adafruit <https://learn.adafruit.com/adafruit-mcp9600-i2c-thermocouple-amplifier>`_.
7+
The MCP9600 is a thermocouple EMF to temperature converter made by Microchip. It
8+
is also sold as a `breakout board module by Adafruit
9+
<https://learn.adafruit.com/adafruit-mcp9600-i2c-thermocouple-amplifier>`_.
810

911
Application Programming Interface
1012
==================================
@@ -15,26 +17,63 @@ The header file for the MCP9600 driver interface can be included using:
1517
1618
#include <nuttx/sensors/mcp9600.h>
1719
18-
The MCP9600 registration function allows the driver to be registered as a POSIX
19-
character driver.
20+
The MCP9600 registration function allows the driver to be registered as a
21+
:doc:`UORB </components/drivers/special/sensors/sensors_uorb>` sensor.
22+
23+
The MCP9600 measures three types of temperature:
24+
* Hot junction temperature
25+
* Cold junction temperature
26+
* Temperature delta
27+
28+
Registering this sensor will create three UORB temperature topics, each with
29+
their own unique device number. You must specify the unique device numbers for
30+
each topic in the registration function:
31+
32+
.. code-block:: c
33+
34+
/* Registers sensor_temp1, sensor_temp2 and sensor_temp 3, where 1 is the
35+
* hot junction topic, 2 is the cold junction topic and 3 is the delta
36+
*/
2037
21-
The standard POSIX `read()` operation will return the device information in
22-
plain-text, which is useful when debugging/testing the driver using `cat` from
23-
the shell.
38+
int err;
39+
err = mcp9600_register(i2c_master, 0x60, 1, 2, 3);
40+
if (err < 0) {
41+
syslog(LOG_ERR, "Could not register MCP9600: %d\n", err);
42+
}
2443
25-
The `write()` operation is not implemented for this sensor.
2644
27-
Specific operations the sensor offers can be performed via the POSIX `ioctl`
28-
operation. The supported commands are:
45+
This sensor offers some additional control commands for features that are not
46+
accessible with the standard UORB interface.
2947

48+
* :c:macro:`SNIOC_SET_THERMO`
3049
* :c:macro:`SNIOC_WHO_AM_I`
3150
* :c:macro:`SNIOC_READ_RAW_DATA`
3251
* :c:macro:`SNIOC_CHECK_STATUS_REG`
3352
* :c:macro:`SNIOC_CONFIGURE`
3453
* :c:macro:`SNIOC_WRITECONF`
35-
* :c:macro:`SNIOC_READTEMP`
36-
* :c:macro:`SNIOC_SHUTDOWN`
37-
* :c:macro:`SNIOC_START`
54+
55+
``SNIOC_SET_THERMO``
56+
--------------------
57+
58+
This command configures the thermocouple type of the MCP9600. The device
59+
supports the following thermocouple types:
60+
61+
* K
62+
* J
63+
* T
64+
* N
65+
* E
66+
* S
67+
* B
68+
* R
69+
70+
.. code-block:: c
71+
72+
int err;
73+
err = orb_ioctl(sensor, SNIOC_SET_THERMO, SENSOR_THERMO_TYPE_J);
74+
if (err < 0) {
75+
syslog(LOG_ERR, "Failed to set thermocouple type: %d\n", err);
76+
}
3877
3978
``SNIOC_WHO_AM_I``
4079
------------------
@@ -46,7 +85,7 @@ type ``struct mcp9600_devinfo_s *``.
4685
.. code-block:: c
4786
4887
struct mcp9600_devinfo_s devinfo;
49-
err = ioctl(sensor, SNIOC_WHO_AM_I, &devinfo);
88+
err = orb_ioctl(sensor, SNIOC_WHO_AM_I, &devinfo);
5089
5190
uint8_t revision_minor = MCP9600_REV_MINOR(devinfo.revision);
5291
uint8_t revision_major = MCP9600_REV_MAJOR(devinfo.revision);
@@ -64,7 +103,7 @@ configured resolution; consult the data sheet.
64103
.. code-block:: c
65104
66105
int32_t raw;
67-
err = ioctl(sensor, SNIOC_READ_RAW_DATA, &raw);
106+
err = orb_ioctl(sensor, SNIOC_READ_RAW_DATA, &raw);
68107
69108
``SNIOC_CHECK_STATUS_REG``
70109
--------------------------
@@ -75,7 +114,7 @@ this command must be a pointer to type ``struct mcp9600_status_s``.
75114
.. code-block:: c
76115
77116
struct mcp9600_status_s status;
78-
err = ioctl(sensor, SNIOC_CHECK_STATUS_REG, &status);
117+
err = orb_ioctl(sensor, SNIOC_CHECK_STATUS_REG, &status);
79118
80119
``SNIOC_CONFIGURE``
81120
-------------------
@@ -93,7 +132,7 @@ mcp9600_devconf_s``.
93132
.resolution = MCP9600_ADC_RES_18,
94133
/* More fields ... */
95134
};
96-
err = ioctl(sensor, SNIOC_CONFIGURE, &conf);
135+
err = orb_ioctl(sensor, SNIOC_CONFIGURE, &conf);
97136
98137
``SNIOC_WRITECONF``
99138
-------------------
@@ -111,36 +150,4 @@ mcp9600_alertconf_s``.
111150
.limit = 40 / 0.25,
112151
/* More fields ... */
113152
};
114-
err = ioctl(sensor, SNIOC_WRITECONF, &conf);
115-
116-
``SNIOC_READTEMP``
117-
------------------
118-
119-
This command lets you read the three different types of temperature that the
120-
MCP9600 can measure. The argument to this command must be a pointer to type
121-
``struct mcp9600_temp_s``.
122-
123-
.. code-block:: c
124-
125-
struct mcp9600_temp_s temps;
126-
err = ioctl(sensor, SNIOC_READTEMP, &temps);
127-
128-
printf("Temperature: %d C\n", temps.hot_junc);
129-
130-
``SNIOC_SHUTDOWN``
131-
------------------
132-
133-
This command shuts down the sensor. It takes no arguments.
134-
135-
.. code-block:: c
136-
137-
err = ioctl(sensor, SNIOC_SHUTDOWN, NULL);
138-
139-
``SNIOC_START``
140-
---------------
141-
142-
This command starts the sensor in normal mode. It takes no arguments.
143-
144-
.. code-block:: c
145-
146-
err = ioctl(sensor, SNIOC_START, NULL);
153+
err = orb_ioctl(sensor, SNIOC_WRITECONF, &conf);

boards/arm/rp2040/common/src/rp2040_common_bringup.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,9 @@ int rp2040_common_bringup(void)
549549
#endif
550550

551551
#ifdef CONFIG_SENSORS_MCP9600
552-
/* Try to register MCP9600 device as /dev/thermo0 at I2C0. */
552+
/* Try to register MCP9600 device as /dev/therm0 at I2C0. */
553553

554-
ret = mcp9600_register("/dev/thermo0", rp2040_i2cbus_initialize(0), 0x60);
554+
ret = mcp9600_register(rp2040_i2cbus_initialize(0), 0x60, 1, 2, 3);
555555
if (ret < 0)
556556
{
557557
syslog(LOG_ERR, "ERROR: couldn't initialize MCP9600: %d\n", ret);

drivers/sensors/Kconfig

+15
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,21 @@ config MCP9600_I2C_FREQUENCY
965965
range 10000 100000
966966
depends on SENSORS_MCP9600
967967

968+
config SENSORS_MCP9600_POLL
969+
bool "MCP9600 polling"
970+
default y
971+
depends on SENSORS_MCP9600
972+
---help---
973+
Enable the worker thread for polling the MCP9600 and collecting
974+
measurements
975+
976+
config MCP9600_THREAD_STACKSIZE
977+
int "MCP9600 stack size"
978+
default 1024
979+
depends on SENSORS_MCP9600
980+
---help---
981+
Stack size of the worker thread polling the MCP9600 for measurements
982+
968983
endif # SENSORS_MCP9600
969984

970985
config SENSORS_MCP9844

drivers/sensors/Make.defs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ ifeq ($(CONFIG_SENSORS_MB7040),y)
225225
endif
226226

227227
ifeq ($(CONFIG_SENSORS_MCP9600),y)
228-
CSRCS += mcp9600.c
228+
CSRCS += mcp9600_uorb.c
229229
endif
230230

231231
ifeq ($(CONFIG_SENSORS_MCP9844),y)

0 commit comments

Comments
 (0)