|
3 | 3 | *
|
4 | 4 | * The MIT License (MIT)
|
5 | 5 | *
|
6 |
| - * Copyright (c) 2017 Dan Halbert for Adafruit Industries |
7 | 6 | * Copyright (c) 2019 Lucian Copeland for Adafruit Industries
|
8 | 7 | *
|
9 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
34 | 33 |
|
35 | 34 | #define STM32_UUID ((uint32_t *)0x1FFF7A10)
|
36 | 35 |
|
| 36 | +//Factory calibration locations |
| 37 | +#define ADC_CAL_ADDRESS (0x1fff7a2a) |
| 38 | +#define ADC_CAL1 ((uint16_t*)(ADC_CAL_ADDRESS + 2)) |
| 39 | +#define ADC_CAL2 ((uint16_t*)(ADC_CAL_ADDRESS + 4)) |
| 40 | + |
| 41 | +// correction factor for reference value |
| 42 | +STATIC volatile float adc_refcor = 1.0f; |
| 43 | + |
| 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) */ |
| 46 | + |
37 | 47 | float common_hal_mcu_processor_get_temperature(void) {
|
38 |
| - return NAN; |
| 48 | + __HAL_RCC_ADC1_CLK_ENABLE(); |
| 49 | + |
| 50 | + //HAL Implementation |
| 51 | + ADC_HandleTypeDef AdcHandle; |
| 52 | + 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 | + HAL_ADC_Init(&AdcHandle); |
| 68 | + |
| 69 | + ADC->CCR |= ADC_CCR_TSVREFE; |
| 70 | + ADC->CCR &= ~ADC_CCR_VBATE; // If this somehow got turned on, it'll return bad values. |
| 71 | + |
| 72 | + sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; //either 16 or 18, depending on chip |
| 73 | + sConfig.Rank = 1; |
| 74 | + sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; //Taken from micropython |
| 75 | + HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); |
| 76 | + |
| 77 | + HAL_ADC_Start(&AdcHandle); |
| 78 | + if (HAL_ADC_PollForConversion(&AdcHandle,1) != HAL_OK) { |
| 79 | + mp_raise_RuntimeError(translate("Temperature read timed out")); |
| 80 | + } |
| 81 | + uint32_t value = (uint32_t)HAL_ADC_GetValue(&AdcHandle); |
| 82 | + HAL_ADC_Stop(&AdcHandle); |
| 83 | + |
| 84 | + //There's no F4 specific appnote for this but it works the same as the L1 in AN3964 |
| 85 | + float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 80.0; |
| 86 | + 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 | + // } |
39 | 119 | }
|
40 | 120 |
|
41 | 121 | 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); |
| 124 | + |
| 125 | +// // update the reference correction factor |
| 126 | +// adc_refcor = ((float)(*VREFIN_CAL)) / ((float)raw_value); |
| 127 | + |
| 128 | +// return (*VREFIN_CAL) * ADC_SCALE; |
| 129 | +// } |
42 | 130 | return NAN;
|
43 | 131 | }
|
44 | 132 |
|
|
0 commit comments