Skip to content

Commit 093f66f

Browse files
JordanYateskartben
authored andcommitted
sensor: tmp108: fix sample_fetch API conformance
`sensor_sample_fetch` is documented as blocking until the fetch operation is complete. Update the implementation so that this is true. Since the fetch operation now blocks, there is no need for the data ready trigger. Signed-off-by: Jordan Yates <[email protected]>
1 parent faf912d commit 093f66f

File tree

3 files changed

+38
-81
lines changed

3 files changed

+38
-81
lines changed

drivers/sensor/ti/tmp108/tmp108.c

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,45 +100,57 @@ static int tmp108_sample_fetch(const struct device *dev,
100100
enum sensor_channel chan)
101101
{
102102
struct tmp108_data *drv_data = dev->data;
103+
uint16_t config, converting_mask;
103104
int result;
104105

105106
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_AMBIENT_TEMP) {
106107
return -ENOTSUP;
107108
}
108109

109-
/* If one shot mode is set, query chip for reading
110-
* should be finished 30 ms later
111-
*/
112-
if (drv_data->one_shot_mode == true) {
110+
if (!drv_data->one_shot_mode) {
111+
/* Read the latest temperature result */
112+
return ti_tmp108_read_temp(dev);
113+
}
113114

114-
result = tmp108_write_config(dev,
115-
TI_TMP108_MODE_MASK(dev),
116-
TI_TMP108_MODE_ONE_SHOT(dev));
115+
/* Trigger the conversion */
116+
result = tmp108_write_config(dev,
117+
TI_TMP108_MODE_MASK(dev),
118+
TI_TMP108_MODE_ONE_SHOT(dev));
119+
if (result < 0) {
120+
return result;
121+
}
117122

118-
if (result < 0) {
119-
return result;
120-
}
123+
/* Typical conversion time:
124+
* TMP108: 27ms
125+
* AS6212: 36ms
126+
* Maximum conversion time:
127+
* TMP108: 35ms
128+
* AS6212: 51ms
129+
*/
130+
const uint32_t conv_time_min = 25;
131+
const uint32_t conv_time_max = 100;
132+
const uint32_t poll_period = 5;
121133

122-
/* Schedule read to start in 30 ms if mode change was successful
123-
* the typical wakeup time given in the data sheet is 27
124-
*/
125-
result = k_work_schedule(&drv_data->scheduled_work,
126-
K_MSEC(TMP108_WAKEUP_TIME_IN_MS(dev)));
134+
k_sleep(K_MSEC(conv_time_min));
135+
converting_mask = TI_TMP108_CONF_M1(dev) | TI_TMP108_CONF_M0(dev);
127136

137+
for (int i = conv_time_min; i < conv_time_max; i += poll_period) {
138+
/* Read the config register */
139+
result = tmp108_reg_read(dev, TI_TMP108_REG_CONF, &config);
128140
if (result < 0) {
129141
return result;
130142
}
131-
132-
return 0;
133-
}
134-
135-
result = ti_tmp108_read_temp(dev);
136-
137-
if (result < 0) {
138-
return result;
143+
if ((config & converting_mask) == 0) {
144+
/* Conversion has finished */
145+
LOG_DBG("Conversion complete after %d ms", i);
146+
return ti_tmp108_read_temp(dev);
147+
}
148+
/* Wait before reading again */
149+
k_sleep(K_MSEC(poll_period));
139150
}
140151

141-
return 0;
152+
/* Conversion timed out */
153+
return -EAGAIN;
142154
}
143155

144156
static int tmp108_channel_get(const struct device *dev,
@@ -367,8 +379,6 @@ static int tmp108_init(const struct device *dev)
367379
return -ENODEV;
368380
}
369381

370-
drv_data->scheduled_work.work.handler = tmp108_trigger_handle_one_shot;
371-
372382
/* save this driver instance for passing to other functions */
373383
drv_data->tmp108_dev = dev;
374384

drivers/sensor/ti/tmp108/tmp108.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
.CONF_M0 = 0x8000, \
3232
.CONF_RST = 0x0080, \
3333
.TEMP_MULT = 15625, \
34-
.TEMP_DIV = 2, \
35-
.WAKEUP_TIME_IN_MS = 120 }
34+
.TEMP_DIV = 2 }
3635

3736
#define TI_TMP108_CONF {.CONF_HYS0 = 0x0010, \
3837
.CONF_HYS1 = 0x0020, \
@@ -44,8 +43,7 @@
4443
.CONF_CR1 = 0x4000, \
4544
.CONF_RST = 0x0022, \
4645
.TEMP_MULT = 15625, \
47-
.TEMP_DIV = 4, \
48-
.WAKEUP_TIME_IN_MS = 30 }
46+
.TEMP_DIV = 4 }
4947

5048
#define TI_TMP108_MODE_SHUTDOWN(x) 0
5149
#define TI_TMP108_MODE_ONE_SHOT(x) (TI_TMP108_CONF_M0(x) | TI_TMP108_CONF_SLEEP(x))
@@ -84,7 +82,6 @@
8482

8583
#define TMP108_TEMP_MULTIPLIER(x) TI_TMP108_GET_CONF(x, TEMP_MULT)
8684
#define TMP108_TEMP_DIVISOR(x) TI_TMP108_GET_CONF(x, TEMP_DIV)
87-
#define TMP108_WAKEUP_TIME_IN_MS(x) TI_TMP108_GET_CONF(x, WAKEUP_TIME_IN_MS)
8885
#define TMP108_CONF_RST(x) TI_TMP108_GET_CONF(x, CONF_RST)
8986

9087
#define TI_TMP108_CONF_NA 0x0000
@@ -101,7 +98,6 @@ struct tmp_108_reg_def {
10198
uint16_t CONF_HYS0; /** Temperature hysteresis config 2 bit */
10299
int32_t TEMP_MULT; /** Temperature multiplier */
103100
int32_t TEMP_DIV; /** Temperature divisor */
104-
uint16_t WAKEUP_TIME_IN_MS; /** Wake up and conversion time from one shot */
105101
uint16_t CONF_RST; /** default reset values on init */
106102
};
107103

@@ -120,14 +116,9 @@ struct tmp108_data {
120116

121117
bool one_shot_mode;
122118

123-
struct k_work_delayable scheduled_work;
124-
125119
const struct sensor_trigger *temp_alert_trigger;
126120
sensor_trigger_handler_t temp_alert_handler;
127121

128-
sensor_trigger_handler_t data_ready_handler;
129-
const struct sensor_trigger *data_ready_trigger;
130-
131122
struct gpio_callback temp_alert_gpio_cb;
132123
};
133124

drivers/sensor/ti/tmp108/tmp108_trigger.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,8 @@
1010

1111
#include "tmp108.h"
1212

13-
#define TMP108_ONE_SHOT_RETRY_TIME_IN_MS 10
14-
1513
LOG_MODULE_DECLARE(TMP108, CONFIG_SENSOR_LOG_LEVEL);
1614

17-
void tmp108_trigger_handle_one_shot(struct k_work *work)
18-
{
19-
struct k_work_delayable *delayable_work = k_work_delayable_from_work(work);
20-
struct tmp108_data *drv_data = CONTAINER_OF(delayable_work,
21-
struct tmp108_data,
22-
scheduled_work);
23-
24-
uint16_t config = 0;
25-
bool shutdown_mode = false;
26-
27-
tmp108_reg_read(drv_data->tmp108_dev, TI_TMP108_REG_CONF, &config);
28-
29-
/* check shutdown mode which indicates a one shot read was successful */
30-
shutdown_mode = (config & (TI_TMP108_CONF_M1(drv_data->tmp108_dev) |
31-
TI_TMP108_CONF_M0(drv_data->tmp108_dev))) == 0;
32-
33-
if (shutdown_mode == true) {
34-
ti_tmp108_read_temp(drv_data->tmp108_dev);
35-
} else {
36-
LOG_ERR("Temperature one shot mode read failed, retrying");
37-
/* Wait for typical wake up time, retry if the read fails
38-
* assuming the chip should wake up and take a reading after the typical
39-
* wake up time and call of this thread plus 10 ms time has passed
40-
*/
41-
k_work_reschedule(&drv_data->scheduled_work,
42-
K_MSEC(TMP108_ONE_SHOT_RETRY_TIME_IN_MS));
43-
return;
44-
}
45-
46-
/* Successful read, call set callbacks */
47-
if (drv_data->data_ready_handler) {
48-
drv_data->data_ready_handler(drv_data->tmp108_dev,
49-
drv_data->data_ready_trigger);
50-
}
51-
}
52-
5315
void tmp108_trigger_handle_alert(const struct device *gpio,
5416
struct gpio_callback *cb,
5517
gpio_port_pins_t pins)
@@ -72,12 +34,6 @@ int tmp_108_trigger_set(const struct device *dev,
7234
{
7335
struct tmp108_data *drv_data = dev->data;
7436

75-
if (trig->type == SENSOR_TRIG_DATA_READY) {
76-
drv_data->data_ready_handler = handler;
77-
drv_data->data_ready_trigger = trig;
78-
return 0;
79-
}
80-
8137
if (trig->type == SENSOR_TRIG_THRESHOLD) {
8238
drv_data->temp_alert_handler = handler;
8339
drv_data->temp_alert_trigger = trig;

0 commit comments

Comments
 (0)