37
37
#define ADC_CAL_ADDRESS (0x1fff7a2a)
38
38
#define ADC_CAL1 ((uint16_t*)(ADC_CAL_ADDRESS + 2))
39
39
#define ADC_CAL2 ((uint16_t*)(ADC_CAL_ADDRESS + 4))
40
+ #define VREFIN_CAL ((uint16_t *)ADC_CAL_ADDRESS)
40
41
41
42
// correction factor for reference value
42
43
STATIC volatile float adc_refcor = 1.0f ;
43
44
44
- #define CORE_TEMP_V25 (943) /* (0.76v/3.3v)*(2^ADC resoultion) */
45
- #define CORE_TEMP_AVG_SLOPE (3) /* (2.5mv/3.3v)*(2^ADC resoultion) */
45
+ STATIC void set_adc_params (ADC_HandleTypeDef * AdcHandle ) {
46
+ AdcHandle -> Instance = ADC1 ;
47
+ AdcHandle -> Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4 ;
48
+ AdcHandle -> Init .Resolution = ADC_RESOLUTION_12B ;
49
+ AdcHandle -> Init .ScanConvMode = DISABLE ;
50
+ AdcHandle -> Init .ContinuousConvMode = DISABLE ;
51
+ AdcHandle -> Init .DiscontinuousConvMode = DISABLE ;
52
+ AdcHandle -> Init .NbrOfDiscConversion = 0 ;
53
+ AdcHandle -> Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
54
+ AdcHandle -> Init .ExternalTrigConv = ADC_SOFTWARE_START ;
55
+ AdcHandle -> Init .DataAlign = ADC_DATAALIGN_RIGHT ;
56
+ AdcHandle -> Init .NbrOfConversion = 1 ;
57
+ AdcHandle -> Init .DMAContinuousRequests = DISABLE ;
58
+ AdcHandle -> Init .EOCSelection = ADC_EOC_SINGLE_CONV ;
59
+ }
46
60
47
61
float common_hal_mcu_processor_get_temperature (void ) {
48
62
__HAL_RCC_ADC1_CLK_ENABLE ();
49
63
50
64
//HAL Implementation
51
65
ADC_HandleTypeDef AdcHandle ;
52
66
ADC_ChannelConfTypeDef sConfig ;
53
-
54
- AdcHandle .Instance = ADC1 ;
55
- AdcHandle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4 ;
56
- AdcHandle .Init .Resolution = ADC_RESOLUTION_12B ;
57
- AdcHandle .Init .ScanConvMode = DISABLE ;
58
- AdcHandle .Init .ContinuousConvMode = DISABLE ;
59
- AdcHandle .Init .DiscontinuousConvMode = DISABLE ;
60
- AdcHandle .Init .NbrOfDiscConversion = 0 ;
61
- AdcHandle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
62
- AdcHandle .Init .ExternalTrigConv = ADC_SOFTWARE_START ;
63
- AdcHandle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
64
- AdcHandle .Init .NbrOfConversion = 1 ;
65
- AdcHandle .Init .DMAContinuousRequests = DISABLE ;
66
- AdcHandle .Init .EOCSelection = ADC_EOC_SINGLE_CONV ;
67
+ set_adc_params (& AdcHandle );
67
68
HAL_ADC_Init (& AdcHandle );
68
69
69
70
ADC -> CCR |= ADC_CCR_TSVREFE ;
70
71
ADC -> CCR &= ~ADC_CCR_VBATE ; // If this somehow got turned on, it'll return bad values.
71
72
72
73
sConfig .Channel = ADC_CHANNEL_TEMPSENSOR ; //either 16 or 18, depending on chip
73
74
sConfig .Rank = 1 ;
74
- sConfig .SamplingTime = ADC_SAMPLETIME_480CYCLES ; //Taken from micropython
75
+ sConfig .SamplingTime = ADC_SAMPLETIME_480CYCLES ; // Temp sensor likes 10us minimum
75
76
HAL_ADC_ConfigChannel (& AdcHandle , & sConfig );
76
77
77
78
HAL_ADC_Start (& AdcHandle );
@@ -84,50 +85,35 @@ float common_hal_mcu_processor_get_temperature(void) {
84
85
//There's no F4 specific appnote for this but it works the same as the L1 in AN3964
85
86
float core_temp_avg_slope = (* ADC_CAL2 - * ADC_CAL1 ) / 80.0 ;
86
87
return (((float )value * adc_refcor - * ADC_CAL1 ) / core_temp_avg_slope ) + 30.0f ;
87
-
88
- // STATIC uint32_t adc_config_and_read_channel(ADC_HandleTypeDef *adcHandle, uint32_t channel) {
89
- // adc_config_channel(adcHandle, channel);
90
- // uint32_t raw_value = adc_read_channel(adcHandle);
91
-
92
- // #if defined(STM32F4) || defined(STM32F7)
93
- // // ST docs say that (at least on STM32F42x and STM32F43x), VBATE must
94
- // // be disabled when TSVREFE is enabled for TEMPSENSOR and VREFINT
95
- // // conversions to work. VBATE is enabled by the above call to read
96
- // // the channel, and here we disable VBATE so a subsequent call for
97
- // // TEMPSENSOR or VREFINT works correctly.
98
- // if (channel == ADC_CHANNEL_VBAT) {
99
- // ADC->CCR &= ~ADC_CCR_VBATE;
100
- // }
101
- // #endif
102
-
103
- // return raw_value;
104
-
105
- // int adc_read_core_temp(ADC_HandleTypeDef *adcHandle) {
106
- // int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR);
107
- // return ((raw_value - CORE_TEMP_V25) / CORE_TEMP_AVG_SLOPE) + 25;
108
- // }
109
-
110
- // #if MICROPY_PY_BUILTINS_FLOAT
111
- // // correction factor for reference value
112
- // STATIC volatile float adc_refcor = 1.0f;
113
-
114
- // float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
115
- // int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR);
116
- // float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 80.0;
117
- // return (((float)raw_value * adc_refcor - *ADC_CAL1) / core_temp_avg_slope) + 30.0f;
118
- // }
119
88
}
120
89
121
90
float common_hal_mcu_processor_get_voltage (void ) {
122
- // float adc_read_core_vref(ADC_HandleTypeDef *adcHandle) {
123
- // uint32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_VREFINT);
91
+ __HAL_RCC_ADC1_CLK_ENABLE ();
92
+
93
+ //HAL Implementation
94
+ ADC_HandleTypeDef AdcHandle ;
95
+ ADC_ChannelConfTypeDef sConfig ;
96
+ set_adc_params (& AdcHandle );
97
+ HAL_ADC_Init (& AdcHandle );
98
+
99
+ ADC -> CCR |= ADC_CCR_TSVREFE ;
100
+
101
+ sConfig .Channel = ADC_CHANNEL_VREFINT ;
102
+ sConfig .Rank = 1 ;
103
+ sConfig .SamplingTime = ADC_SAMPLETIME_480CYCLES ;
104
+ HAL_ADC_ConfigChannel (& AdcHandle , & sConfig );
105
+
106
+ HAL_ADC_Start (& AdcHandle );
107
+ if (HAL_ADC_PollForConversion (& AdcHandle ,1 ) != HAL_OK ) {
108
+ mp_raise_RuntimeError (translate ("Voltage read timed out" ));
109
+ }
110
+ uint32_t value = (uint32_t )HAL_ADC_GetValue (& AdcHandle );
111
+ HAL_ADC_Stop (& AdcHandle );
124
112
125
- // // update the reference correction factor
126
- // adc_refcor = ((float)(*VREFIN_CAL)) / ((float)raw_value );
113
+ //This value could be used to actively correct ADC values.
114
+ adc_refcor = ((float )(* VREFIN_CAL )) / ((float )value );
127
115
128
- // return (*VREFIN_CAL) * ADC_SCALE;
129
- // }
130
- return NAN ;
116
+ return adc_refcor * 3.3f ;
131
117
}
132
118
133
119
uint32_t common_hal_mcu_processor_get_frequency (void ) {
0 commit comments