67
67
#define ADC_CAL_ADDRESS (0x1ffff7ba)
68
68
#define ADC_CAL1 ((uint16_t*)0x1ffff7b8)
69
69
#define ADC_CAL2 ((uint16_t*)0x1ffff7c2)
70
+ #define ADC_CAL_BITS (12)
70
71
71
72
#elif defined(STM32F4 )
72
73
76
77
#define ADC_CAL_ADDRESS (0x1fff7a2a)
77
78
#define ADC_CAL1 ((uint16_t*)(ADC_CAL_ADDRESS + 2))
78
79
#define ADC_CAL2 ((uint16_t*)(ADC_CAL_ADDRESS + 4))
80
+ #define ADC_CAL_BITS (12)
79
81
80
82
#elif defined(STM32F7 )
81
83
91
93
92
94
#define ADC_CAL1 ((uint16_t*)(ADC_CAL_ADDRESS + 2))
93
95
#define ADC_CAL2 ((uint16_t*)(ADC_CAL_ADDRESS + 4))
96
+ #define ADC_CAL_BITS (12)
94
97
95
98
#elif defined(STM32H7 )
96
99
100
103
#define ADC_CAL_ADDRESS (0x1FF1E860)
101
104
#define ADC_CAL1 ((uint16_t*)(0x1FF1E820))
102
105
#define ADC_CAL2 ((uint16_t*)(0x1FF1E840))
106
+ #define ADC_CAL_BITS (16)
103
107
#define ADC_CHANNEL_VBAT ADC_CHANNEL_VBAT_DIV4
104
108
105
109
#elif defined(STM32L4 )
110
114
#define ADC_CAL_A
10000
DDRESS (0x1fff75aa)
111
115
#define ADC_CAL1 ((uint16_t*)(ADC_CAL_ADDRESS - 2))
112
116
#define ADC_CAL2 ((uint16_t*)(ADC_CAL_ADDRESS + 0x20))
117
+ #define ADC_CAL_BITS (12)
113
118
114
119
#else
115
120
149
154
#define CORE_TEMP_AVG_SLOPE (3) /* (2.5mv/3.3v)*(2^ADC resoultion) */
150
155
151
156
// scale and calibration values for VBAT and VREF
152
- #define ADC_SCALE (ADC_SCALE_V / 4095 )
157
+ #define ADC_SCALE (ADC_SCALE_V / ((1 << ADC_CAL_BITS) - 1) )
153
158
#define VREFIN_CAL ((uint16_t *)ADC_CAL_ADDRESS)
154
159
155
160
typedef struct _pyb_obj_adc_t {
@@ -699,13 +704,14 @@ int adc_get_resolution(ADC_HandleTypeDef *adcHandle) {
699
704
return 12 ;
700
705
}
701
706
702
- int adc_read_core_temp (ADC_HandleTypeDef * adcHandle ) {
703
- int32_t raw_value = adc_config_and_read_channel (adcHandle , ADC_CHANNEL_TEMPSENSOR );
704
-
705
- // Note: constants assume 12-bit resolution, so we scale the raw value to
706
- // be 12-bits.
707
- raw_value <<= (12 - adc_get_resolution (adcHandle ));
707
+ STATIC uint32_t adc_config_and_read_ref (ADC_HandleTypeDef * adcHandle , uint32_t channel ) {
708
+ uint32_t raw_value = adc_config_and_read_channel (adcHandle , channel );
709
+ // Scale raw reading to the number of bits used by the calibration constants
710
+ return raw_value << (ADC_CAL_BITS - adc_get_resolution (adcHandle ));
711
+ }
708
712
713
+ int adc_read_core_temp (ADC_HandleTypeDef * adcHandle ) {
714
+ int32_t raw_value = adc_config_and_read_ref (adcHandle , ADC_CHANNEL_TEMPSENSOR );
709
715
return ((raw_value - CORE_TEMP_V25 ) / CORE_TEMP_AVG_SLOPE ) + 25 ;
710
716
}
711
717
@@ -714,31 +720,18 @@ int adc_read_core_temp(ADC_HandleTypeDef *adcHandle) {
714
720
STATIC volatile float adc_refcor = 1.0f ;
715
721
716
722
float adc_read_core_temp_float (ADC_HandleTypeDef * adcHandle ) {
717
- int32_t raw_value = adc_config_and_read_channel (adcHandle , ADC_CHANNEL_TEMPSENSOR );
718
-
719
- // constants assume 12-bit resolution so we scale the raw value to 12-bits
720
- raw_value <<= (12 - adc_get_resolution (adcHandle ));
721
-
723
+ int32_t raw_value = adc_config_and_read_ref (adcHandle , ADC_CHANNEL_TEMPSENSOR );
722
724
float core_temp_avg_slope = (* ADC_CAL2 - * ADC_CAL1 ) / 80.0 ;
723
725
return (((float )raw_value * adc_refcor - * ADC_CAL1 ) / core_temp_avg_slope ) + 30.0f ;
724
726
}
725
727
726
728
float adc_read_core_vbat (ADC_HandleTypeDef * adcHandle ) {
727
- uint32_t raw_value = adc_config_and_read_channel (adcHandle , ADC_CHANNEL_VBAT );
728
-
729
- // Note: constants assume 12-bit resolution, so we scale the raw value to
730
- // be 12-bits.
731
- raw_value <<= (12 - adc_get_resolution (adcHandle ));
732
-
729
+ uint32_t raw_value = adc_config_and_read_ref (adcHandle , ADC_CHANNEL_VBAT );
733
730
return raw_value * VBAT_DIV * ADC_SCALE * adc_refcor ;
734
731
}
735
732
736
733
float adc_read_core_vref (ADC_HandleTypeDef * adcHandle ) {
737
- uint32_t raw_value = adc_config_and_read_channel (adcHandle , ADC_CHANNEL_VREFINT );
738
-
739
- // Note: constants assume 12-bit resolution, so we scale the raw value to
740
- // be 12-bits.
741
- raw_value <<= (12 - adc_get_resolution (adcHandle ));
734
+ uint32_t raw_value = adc_config_and_read_ref (adcHandle , ADC_CHANNEL_VREFINT );
742
735
743
736
// update the reference correction factor
744
737
adc_refcor = ((float )(* VREFIN_CAL )) / ((float )raw_value );
0 commit comments