@@ -859,10 +859,10 @@ static inline uint16_t adc_ref_internal(const struct device *dev)
859
859
}
860
860
861
861
/**
862
- * @brief Convert a raw ADC value to millivolts.
862
+ * @brief Conversion from raw ADC units to a specific output unit
863
863
*
864
864
* This function performs the necessary conversion to transform a raw
865
- * ADC measurement to a voltage in millivolts .
865
+ * ADC measurement to a physical voltage .
866
866
*
867
867
* @param ref_mv the reference voltage used for the measurement, in
868
868
* millivolts. This may be from adc_ref_internal() or a known
@@ -875,15 +875,21 @@ static inline uint16_t adc_ref_internal(const struct device *dev)
875
875
* resolution in struct adc_sequence.
876
876
*
877
877
* @param valp pointer to the raw measurement value on input, and the
878
- * corresponding millivolt value on successful conversion. If
878
+ * corresponding output value on successful conversion. If
879
879
* conversion fails the stored value is left unchanged.
880
880
*
881
881
* @retval 0 on successful conversion
882
882
* @retval -EINVAL if the gain is not reversible
883
883
*/
884
- static inline int adc_raw_to_millivolts (int32_t ref_mv ,
885
- enum adc_gain gain ,
886
- uint8_t resolution ,
884
+ typedef int (* adc_raw_to_x_fn )(int32_t ref_mv , enum adc_gain gain , uint8_t resolution ,
885
+ int32_t * valp );
886
+
887
+ /**
888
+ * @brief Convert a raw ADC value to millivolts.
889
+ *
890
+ * @see adc_raw_to_x_fn
891
+ */
892
+ static inline int adc_raw_to_millivolts (int32_t ref_mv , enum adc_gain gain , uint8_t resolution ,
887
893
int32_t * valp )
888
894
{
889
895
int32_t adc_mv = * valp * ref_mv ;
@@ -897,20 +903,43 @@ static inline int adc_raw_to_millivolts(int32_t ref_mv,
897
903
}
898
904
899
905
/**
900
- * @brief Convert a raw ADC value to millivolts using information stored
901
- * in a struct adc_dt_spec.
906
+ * @brief Convert a raw ADC value to microvolts.
907
+ *
908
+ * @see adc_raw_to_x_fn
909
+ */
910
+ static inline int adc_raw_to_microvolts (int32_t ref_mv , enum adc_gain gain , uint8_t resolution ,
911
+ int32_t * valp )
912
+ {
913
+ int64_t adc_uv = (int64_t )* valp * ref_mv * 1000 ;
914
+ int ret = adc_gain_invert_64 (gain , & adc_uv );
915
+
916
+ if (ret == 0 ) {
917
+ * valp = (int32_t )(adc_uv >> resolution );
918
+ }
919
+
920
+ return ret ;
921
+ }
922
+
923
+ /**
924
+ * @brief Convert a raw ADC value to an arbitrary output unit
902
925
*
926
+ * @param[in] conv_func Function that converts to the final output unit.
903
927
* @param[in] spec ADC specification from Devicetree.
928
+ * @param[in] channel_cfg Channel configuration used for sampling. This can be
929
+ * either the configuration from @a spec, or an alternate sampling configuration
930
+ * based on @a spec, for example a different gain value.
904
931
* @param[in,out] valp Pointer to the raw measurement value on input, and the
905
- * corresponding millivolt value on successful conversion. If conversion fails
932
+ * corresponding output value on successful conversion. If conversion fails
906
933
* the stored value is left unchanged.
907
934
*
908
- * @return A value from adc_raw_to_millivolts() or -ENOTSUP if information from
935
+ * @return A value from adc_raw_to_x_fn or -ENOTSUP if information from
909
936
* Devicetree is not valid.
910
- * @see adc_raw_to_millivolts()
937
+ * @see adc_raw_to_x_fn
911
938
*/
912
- static inline int adc_raw_to_millivolts_dt (const struct adc_dt_spec * spec ,
913
- int32_t * valp )
939
+ static inline int adc_raw_to_x_dt_chan (adc_raw_to_x_fn conv_func ,
940
+ const struct adc_dt_spec * spec ,
941
+ const struct adc_channel_cfg * channel_cfg ,
942
+ int32_t * valp )
914
943
{
915
944
int32_t vref_mv ;
916
945
uint8_t resolution ;
@@ -919,7 +948,7 @@ static inline int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec,
919
948
return - ENOTSUP ;
920
949
}
921
950
922
- if (spec -> channel_cfg . reference == ADC_REF_INTERNAL ) {
951
+ if (channel_cfg -> reference == ADC_REF_INTERNAL ) {
923
952
vref_mv = (int32_t )adc_ref_internal (spec -> dev );
924
953
} else {
925
954
vref_mv = spec -> vref_mv ;
@@ -931,12 +960,48 @@ static inline int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec,
931
960
* For differential channels, one bit less needs to be specified
932
961
* for resolution to achieve correct conversion.
933
962
*/
934
- if (spec -> channel_cfg . differential ) {
963
+ if (channel_cfg -> differential ) {
935
964
resolution -= 1U ;
936
965
}
937
966
938
- return adc_raw_to_millivolts (vref_mv , spec -> channel_cfg .gain ,
939
- resolution , valp );
967
+ return conv_func (vref_mv , channel_cfg -> gain , resolution , valp );
968
+ }
969
+
970
+
971
+ /**
972
+ * @brief Convert a raw ADC value to millivolts using information stored
973
+ * in a struct adc_dt_spec.
974
+ *
975
+ * @param[in] spec ADC specification from Devicetree.
976
+ * @param[in,out] valp Pointer to the raw measurement value on input, and the
977
+ * corresponding millivolt value on successful conversion. If conversion fails
978
+ * the stored value is left unchanged.
979
+ *
980
+ * @return A value from adc_raw_to_millivolts() or -ENOTSUP if information from
981
+ * Devicetree is not valid.
982
+ * @see adc_raw_to_millivolts()
983
+ */
984
+ static inline int adc_raw_to_millivolts_dt (const struct adc_dt_spec * spec , int32_t * valp )
985
+ {
986
+ return adc_raw_to_x_dt_chan (adc_raw_to_millivolts , spec , & spec -> channel_cfg , valp );
987
+ }
988
+
989
+ /**
990
+ * @brief Convert a raw ADC value to microvolts using information stored
991
+ * in a struct adc_dt_spec.
992
+ *
993
+ * @param[in] spec ADC specification from Devicetree.
994
+ * @param[in,out] valp Pointer to the raw measurement value on input, and the
995
+ * corresponding microvolt value on successful conversion. If conversion fails
996
+ * the stored value is left unchanged.
997
+ *
998
+ * @return A value from adc_raw_to_microvolts() or -ENOTSUP if information from
999
+ * Devicetree is not valid.
1000
+ * @see adc_raw_to_microvolts()
1001
+ */
1002
+ static inline int adc_raw_to_microvolts_dt (const struct adc_dt_spec * spec , int32_t * valp )
1003
+ {
1004
+ return adc_raw_to_x_dt_chan (adc_raw_to_microvolts , spec , & spec -> channel_cfg , valp );
940
1005
}
941
1006
942
1007
/**
0 commit comments