samples: shields: lmp90100_evb: rtd: convert to devicetree configuration
Convert the sample for the lmp90100_evb shield to use devicetree for configuring the ADC channel. Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
parent
df83fffc7c
commit
d856ed73f4
2 changed files with 40 additions and 30 deletions
|
@ -1,9 +1,29 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2019 Vestas Wind Systems A/S
|
* Copyright (c) 2019-2024 Vestas Wind Systems A/S
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
&lmp90100_lmp90100_evb {
|
/ {
|
||||||
rtd-current = <1000>;
|
zephyr,user {
|
||||||
|
io-channels = <&lmp90100_lmp90100_evb 0>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
&lmp90100_lmp90100_evb {
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
|
||||||
|
rtd-current = <1000>;
|
||||||
|
|
||||||
|
channel@0 {
|
||||||
|
reg = <0>;
|
||||||
|
zephyr,gain = "ADC_GAIN_1";
|
||||||
|
zephyr,reference = "ADC_REF_EXTERNAL1";
|
||||||
|
zephyr,acquisition-time = <ADC_ACQ_TIME(ADC_ACQ_TIME_TICKS, 0)>;
|
||||||
|
zephyr,resolution = <24>;
|
||||||
|
zephyr,differential;
|
||||||
|
zephyr,input-positive = <0>;
|
||||||
|
zephyr,input-negative = <1>;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2019 Vestas Wind Systems A/S
|
* Copyright (c) 2019-2024 Vestas Wind Systems A/S
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
@ -17,11 +17,8 @@ LOG_MODULE_REGISTER(main);
|
||||||
/* Nominal RTD (PT100) resistance in ohms */
|
/* Nominal RTD (PT100) resistance in ohms */
|
||||||
#define RTD_NOMINAL_RESISTANCE 100
|
#define RTD_NOMINAL_RESISTANCE 100
|
||||||
|
|
||||||
/* ADC resolution in bits */
|
|
||||||
#define ADC_RESOLUTION 24U
|
|
||||||
|
|
||||||
/* ADC maximum value (taking sign bit into consideration) */
|
/* ADC maximum value (taking sign bit into consideration) */
|
||||||
#define ADC_MAX BIT_MASK(ADC_RESOLUTION - 1)
|
#define ADC_MAX(resolution) BIT_MASK(resolution - 1)
|
||||||
|
|
||||||
/* Bottom resistor value in ohms */
|
/* Bottom resistor value in ohms */
|
||||||
#define BOTTOM_RESISTANCE 2000
|
#define BOTTOM_RESISTANCE 2000
|
||||||
|
@ -42,46 +39,39 @@ static double rtd_temperature(int nom, double resistance)
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
const struct device *const lmp90100 = DEVICE_DT_GET_ONE(ti_lmp90100);
|
const struct adc_dt_spec ch_cfg = ADC_DT_SPEC_GET(DT_PATH(zephyr_user));
|
||||||
|
double adc_max = ADC_MAX(ch_cfg.resolution);
|
||||||
double resistance;
|
double resistance;
|
||||||
int32_t buffer;
|
int32_t buffer;
|
||||||
int err;
|
int err;
|
||||||
const struct adc_channel_cfg ch_cfg = {
|
struct adc_sequence seq = {
|
||||||
.channel_id = 0,
|
|
||||||
.differential = 1,
|
|
||||||
.input_positive = 0,
|
|
||||||
.input_negative = 1,
|
|
||||||
.reference = ADC_REF_EXTERNAL1,
|
|
||||||
.gain = ADC_GAIN_1,
|
|
||||||
.acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_TICKS, 0)
|
|
||||||
};
|
|
||||||
const struct adc_sequence seq = {
|
|
||||||
.options = NULL,
|
|
||||||
.channels = BIT(0),
|
|
||||||
.buffer = &buffer,
|
.buffer = &buffer,
|
||||||
.buffer_size = sizeof(buffer),
|
.buffer_size = sizeof(buffer),
|
||||||
.resolution = ADC_RESOLUTION,
|
|
||||||
.oversampling = 0,
|
|
||||||
.calibrate = 0
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!device_is_ready(lmp90100)) {
|
if (!adc_is_ready_dt(&ch_cfg)) {
|
||||||
LOG_ERR("LMP90100 device not ready");
|
LOG_ERR("LMP90100 device not ready");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = adc_channel_setup(lmp90100, &ch_cfg);
|
err = adc_channel_setup_dt(&ch_cfg);
|
||||||
if (err) {
|
if (err != 0) {
|
||||||
LOG_ERR("failed to setup ADC channel (err %d)", err);
|
LOG_ERR("failed to setup ADC channel (err %d)", err);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = adc_sequence_init_dt(&ch_cfg, &seq);
|
||||||
|
if (err != 0) {
|
||||||
|
LOG_ERR("failed to initialize ADC sequence (err %d)", err);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
err = adc_read(lmp90100, &seq);
|
err = adc_read_dt(&ch_cfg, &seq);
|
||||||
if (err) {
|
if (err != 0) {
|
||||||
LOG_ERR("failed to read ADC (err %d)", err);
|
LOG_ERR("failed to read ADC (err %d)", err);
|
||||||
} else {
|
} else {
|
||||||
resistance = (buffer / (double)ADC_MAX) * BOTTOM_RESISTANCE;
|
resistance = (buffer / adc_max) * BOTTOM_RESISTANCE;
|
||||||
printf("R: %.02f ohm\n", resistance);
|
printf("R: %.02f ohm\n", resistance);
|
||||||
printf("T: %.02f degC\n",
|
printf("T: %.02f degC\n",
|
||||||
rtd_temperature(RTD_NOMINAL_RESISTANCE,
|
rtd_temperature(RTD_NOMINAL_RESISTANCE,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue