2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-24 13:49:23 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
2015-10-30 19:03:58 -04:00
|
|
|
#include "py/mphal.h"
|
2014-03-24 13:49:23 -04:00
|
|
|
#include "adc.h"
|
|
|
|
#include "pin.h"
|
2014-04-15 14:52:56 -04:00
|
|
|
#include "timer.h"
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2018-02-21 22:22:45 -05:00
|
|
|
#if MICROPY_HW_ENABLE_ADC
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class ADC - analog to digital conversion: read analog values on a pin
|
|
|
|
///
|
|
|
|
/// Usage:
|
|
|
|
///
|
|
|
|
/// adc = pyb.ADC(pin) # create an analog object from a pin
|
|
|
|
/// val = adc.read() # read an analog value
|
|
|
|
///
|
|
|
|
/// adc = pyb.ADCAll(resolution) # creale an ADCAll object
|
|
|
|
/// val = adc.read_channel(channel) # read the given channel
|
|
|
|
/// val = adc.read_core_temp() # read MCU temperature
|
|
|
|
/// val = adc.read_core_vbat() # read MCU VBAT
|
|
|
|
/// val = adc.read_core_vref() # read MCU VREF
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2022-07-16 18:43:33 -04:00
|
|
|
/* ADC definitions */
|
2014-03-24 13:49:23 -04:00
|
|
|
#define ADCx (ADC1)
|
2019-02-05 21:35:28 -05:00
|
|
|
#define PIN_ADC_MASK PIN_ADC1
|
|
|
|
#define pin_adc_table pin_adc1
|
|
|
|
|
2021-09-15 09:08:16 -04:00
|
|
|
#if defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
|
|
|
|
defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
|
|
|
|
#define ADCALLx (ADC2)
|
|
|
|
#define pin_adcall_table pin_adc2
|
|
|
|
#elif defined(STM32H7)
|
2021-07-01 10:15:31 -04:00
|
|
|
// On the H7 ADC3 is used for ADCAll to be able to read internal
|
|
|
|
// channels. For all other GPIO channels, ADC12 is used instead.
|
|
|
|
#define ADCALLx (ADC3)
|
|
|
|
#define pin_adcall_table pin_adc3
|
|
|
|
#else
|
|
|
|
// Use ADC1 for ADCAll instance by default for all other MCUs.
|
|
|
|
#define ADCALLx (ADC1)
|
|
|
|
#define pin_adcall_table pin_adc1
|
|
|
|
#endif
|
|
|
|
|
2018-02-12 23:37:35 -05:00
|
|
|
#define ADCx_CLK_ENABLE __HAL_RCC_ADC1_CLK_ENABLE
|
2016-08-23 18:13:03 -04:00
|
|
|
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
|
2018-09-20 09:50:54 -04:00
|
|
|
#define ADC_SCALE_V (3.3f)
|
2018-05-28 04:10:53 -04:00
|
|
|
#define ADC_CAL_ADDRESS (0x1ffff7ba)
|
2020-02-26 23:36:53 -05:00
|
|
|
#define ADC_CAL1 ((uint16_t *)0x1ffff7b8)
|
|
|
|
#define ADC_CAL2 ((uint16_t *)0x1ffff7c2)
|
2018-12-11 20:50:37 -05:00
|
|
|
#define ADC_CAL_BITS (12)
|
2018-05-28 04:10:53 -04:00
|
|
|
|
|
|
|
#elif defined(STM32F4)
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2018-09-20 09:50:54 -04:00
|
|
|
#define ADC_SCALE_V (3.3f)
|
2016-11-29 20:04:21 -05:00
|
|
|
#define ADC_CAL_ADDRESS (0x1fff7a2a)
|
2020-02-26 23:36:53 -05:00
|
|
|
#define ADC_CAL1 ((uint16_t *)(ADC_CAL_ADDRESS + 2))
|
|
|
|
#define ADC_CAL2 ((uint16_t *)(ADC_CAL_ADDRESS + 4))
|
2018-12-11 20:50:37 -05:00
|
|
|
#define ADC_CAL_BITS (12)
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2018-03-16 19:42:50 -04:00
|
|
|
#elif defined(STM32F7)
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2018-09-20 09:50:54 -04:00
|
|
|
#define ADC_SCALE_V (3.3f)
|
2018-01-31 21:11:32 -05:00
|
|
|
#if defined(STM32F722xx) || defined(STM32F723xx) || \
|
|
|
|
defined(STM32F732xx) || defined(STM32F733xx)
|
|
|
|
#define ADC_CAL_ADDRESS (0x1ff07a2a)
|
|
|
|
#else
|
2016-11-29 20:04:21 -05:00
|
|
|
#define ADC_CAL_ADDRESS (0x1ff0f44a)
|
2018-01-31 21:11:32 -05:00
|
|
|
#endif
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
#define ADC_CAL1 ((uint16_t *)(ADC_CAL_ADDRESS + 2))
|
|
|
|
#define ADC_CAL2 ((uint16_t *)(ADC_CAL_ADDRESS + 4))
|
2018-12-11 20:50:37 -05:00
|
|
|
#define ADC_CAL_BITS (12)
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32G0) || defined(STM32G4) || defined(STM32H5)
|
2021-01-26 08:49:56 -05:00
|
|
|
|
|
|
|
#define ADC_SCALE_V (((float)VREFINT_CAL_VREF) / 1000.0f)
|
|
|
|
#define ADC_CAL_ADDRESS VREFINT_CAL_ADDR
|
|
|
|
#define ADC_CAL1 TEMPSENSOR_CAL1_ADDR
|
|
|
|
#define ADC_CAL2 TEMPSENSOR_CAL2_ADDR
|
2022-02-12 15:36:58 -05:00
|
|
|
#define ADC_CAL_BITS (12) // UM2319/UM2570, __HAL_ADC_CALC_TEMPERATURE: 'corresponds to a resolution of 12 bits'
|
2021-01-26 08:49:56 -05:00
|
|
|
|
2018-03-31 21:20:21 -04:00
|
|
|
#elif defined(STM32H7)
|
|
|
|
|
2018-09-20 09:50:54 -04:00
|
|
|
#define ADC_SCALE_V (3.3f)
|
2018-03-31 21:20:21 -04:00
|
|
|
#define ADC_CAL_ADDRESS (0x1FF1E860)
|
2020-02-26 23:36:53 -05:00
|
|
|
#define ADC_CAL1 ((uint16_t *)(0x1FF1E820))
|
|
|
|
#define ADC_CAL2 ((uint16_t *)(0x1FF1E840))
|
2018-12-11 20:50:37 -05:00
|
|
|
#define ADC_CAL_BITS (16)
|
2018-03-31 21:20:21 -04:00
|
|
|
|
2022-09-19 04:56:31 -04:00
|
|
|
#elif defined(STM32L1)
|
|
|
|
|
|
|
|
#define ADC_SCALE_V (VREFINT_CAL_VREF / 1000.0f)
|
|
|
|
#define ADC_CAL_ADDRESS (VREFINT_CAL_ADDR)
|
|
|
|
#define ADC_CAL1 (TEMPSENSOR_CAL1_ADDR)
|
|
|
|
#define ADC_CAL2 (TEMPSENSOR_CAL2_ADDR)
|
|
|
|
#define ADC_CAL_BITS (12)
|
|
|
|
|
2020-06-01 07:29:11 -04:00
|
|
|
#elif defined(STM32L4) || defined(STM32WB)
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2020-06-01 07:29:11 -04:00
|
|
|
#define ADC_SCALE_V (VREFINT_CAL_VREF / 1000.0f)
|
|
|
|
#define ADC_CAL_ADDRESS (VREFINT_CAL_ADDR)
|
|
|
|
#define ADC_CAL1 (TEMPSENSOR_CAL1_ADDR)
|
|
|
|
#define ADC_CAL2 (TEMPSENSOR_CAL2_ADDR)
|
2018-12-11 20:50:37 -05:00
|
|
|
#define ADC_CAL_BITS (12)
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2016-08-23 18:13:03 -04:00
|
|
|
#else
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2016-08-23 18:13:03 -04:00
|
|
|
#error Unsupported processor
|
2016-11-29 20:04:21 -05:00
|
|
|
|
2016-08-23 18:13:03 -04:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F091xC)
|
|
|
|
#define VBAT_DIV (2)
|
|
|
|
#elif defined(STM32F405xx) || defined(STM32F415xx) || \
|
2020-02-26 23:36:53 -05:00
|
|
|
defined(STM32F407xx) || defined(STM32F417xx) || \
|
|
|
|
defined(STM32F401xC) || defined(STM32F401xE)
|
2014-03-24 13:49:23 -04:00
|
|
|
#define VBAT_DIV (2)
|
2020-05-14 09:56:26 -04:00
|
|
|
#elif defined(STM32F411xE) || defined(STM32F412Zx) || \
|
|
|
|
defined(STM32F413xx) || defined(STM32F427xx) || \
|
|
|
|
defined(STM32F429xx) || defined(STM32F437xx) || \
|
2021-11-28 21:25:53 -05:00
|
|
|
defined(STM32F439xx) || defined(STM32F446xx) || \
|
|
|
|
defined(STM32F479xx)
|
2019-05-20 00:37:28 -04:00
|
|
|
#define VBAT_DIV (4)
|
|
|
|
#elif defined(STM32F722xx) || defined(STM32F723xx) || \
|
2020-02-26 23:36:53 -05:00
|
|
|
defined(STM32F732xx) || defined(STM32F733xx) || \
|
2022-12-14 01:12:34 -05:00
|
|
|
defined(STM32F745xx) || defined(STM32F746xx) || \
|
|
|
|
defined(STM32F756xx) || defined(STM32F765xx) || \
|
2020-02-26 23:36:53 -05:00
|
|
|
defined(STM32F767xx) || defined(STM32F769xx)
|
2014-03-24 13:49:23 -04:00
|
|
|
#define VBAT_DIV (4)
|
2022-02-12 15:36:58 -05:00
|
|
|
#elif defined(STM32G0) || defined(STM32G4)
|
2021-01-26 08:49:56 -05:00
|
|
|
#define VBAT_DIV (3)
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32H5)
|
|
|
|
#define VBAT_DIV (4)
|
2022-12-20 01:00:59 -05:00
|
|
|
#elif defined(STM32H723xx) || defined(STM32H733xx) || \
|
|
|
|
defined(STM32H743xx) || defined(STM32H747xx) || \
|
2021-09-15 09:08:16 -04:00
|
|
|
defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || \
|
|
|
|
defined(STM32H7B3xx) || defined(STM32H7B3xxQ) || \
|
2021-09-09 17:39:09 -04:00
|
|
|
defined(STM32H750xx)
|
2018-03-31 21:20:21 -04:00
|
|
|
#define VBAT_DIV (4)
|
2019-05-20 08:00:41 -04:00
|
|
|
#elif defined(STM32L432xx) || \
|
2020-02-26 23:36:53 -05:00
|
|
|
defined(STM32L451xx) || defined(STM32L452xx) || \
|
|
|
|
defined(STM32L462xx) || defined(STM32L475xx) || \
|
2020-06-01 07:29:11 -04:00
|
|
|
defined(STM32L476xx) || defined(STM32L496xx) || \
|
2023-05-16 21:44:23 -04:00
|
|
|
defined(STM32L4A6xx) || \
|
2020-06-01 07:29:11 -04:00
|
|
|
defined(STM32WB55xx)
|
2016-03-22 06:28:35 -04:00
|
|
|
#define VBAT_DIV (3)
|
2022-09-19 04:56:31 -04:00
|
|
|
#elif defined(STM32L152xE)
|
|
|
|
// STM32L152xE does not have vbat.
|
2015-07-28 14:13:33 -04:00
|
|
|
#else
|
|
|
|
#error Unsupported processor
|
2014-03-24 13:49:23 -04:00
|
|
|
#endif
|
|
|
|
|
2018-04-11 00:28:06 -04:00
|
|
|
// Timeout for waiting for end-of-conversion, in ms
|
|
|
|
#define EOC_TIMEOUT (10)
|
|
|
|
|
2014-03-24 13:49:23 -04:00
|
|
|
/* Core temperature sensor definitions */
|
2022-07-16 18:43:33 -04:00
|
|
|
#define CORE_TEMP_V25 (943) /* (0.76v/3.3v)*(2^ADC resolution) */
|
|
|
|
#define CORE_TEMP_AVG_SLOPE (3) /* (2.5mv/3.3v)*(2^ADC resolution) */
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2016-11-29 20:04:21 -05:00
|
|
|
// scale and calibration values for VBAT and VREF
|
2018-12-11 20:50:37 -05:00
|
|
|
#define ADC_SCALE (ADC_SCALE_V / ((1 << ADC_CAL_BITS) - 1))
|
2016-11-29 20:04:21 -05:00
|
|
|
#define VREFIN_CAL ((uint16_t *)ADC_CAL_ADDRESS)
|
|
|
|
|
2019-10-11 16:19:33 -04:00
|
|
|
#ifndef __HAL_ADC_IS_CHANNEL_INTERNAL
|
2022-09-19 04:56:31 -04:00
|
|
|
#if defined(STM32L1)
|
|
|
|
#define __HAL_ADC_IS_CHANNEL_INTERNAL(channel) \
|
|
|
|
(channel == ADC_CHANNEL_VREFINT \
|
|
|
|
|| channel == ADC_CHANNEL_TEMPSENSOR)
|
|
|
|
#else
|
2019-10-11 16:19:33 -04:00
|
|
|
#define __HAL_ADC_IS_CHANNEL_INTERNAL(channel) \
|
|
|
|
(channel == ADC_CHANNEL_VBAT \
|
2020-02-26 23:36:53 -05:00
|
|
|
|| channel == ADC_CHANNEL_VREFINT \
|
|
|
|
|| channel == ADC_CHANNEL_TEMPSENSOR)
|
2019-10-11 16:19:33 -04:00
|
|
|
#endif
|
2022-09-19 04:56:31 -04:00
|
|
|
#endif
|
2019-10-11 16:19:33 -04:00
|
|
|
|
2014-03-24 13:49:23 -04:00
|
|
|
typedef struct _pyb_obj_adc_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t pin_name;
|
2021-06-30 22:04:48 -04:00
|
|
|
uint32_t channel;
|
2014-03-24 13:49:23 -04:00
|
|
|
ADC_HandleTypeDef handle;
|
|
|
|
} pyb_obj_adc_t;
|
|
|
|
|
2016-11-29 20:20:23 -05:00
|
|
|
// convert user-facing channel number into internal channel number
|
|
|
|
static inline uint32_t adc_get_internal_channel(uint32_t channel) {
|
2018-03-16 19:42:50 -04:00
|
|
|
#if defined(STM32F4) || defined(STM32F7)
|
2016-11-29 20:20:23 -05:00
|
|
|
// on F4 and F7 MCUs we want channel 16 to always be the TEMPSENSOR
|
|
|
|
// (on some MCUs ADC_CHANNEL_TEMPSENSOR=16, on others it doesn't)
|
|
|
|
if (channel == 16) {
|
|
|
|
channel = ADC_CHANNEL_TEMPSENSOR;
|
|
|
|
}
|
2023-07-07 22:04:20 -04:00
|
|
|
#elif defined(STM32G4)
|
|
|
|
if (channel == 16) {
|
|
|
|
channel = ADC_CHANNEL_TEMPSENSOR_ADC1;
|
|
|
|
} else if (channel == 17) {
|
|
|
|
channel = ADC_CHANNEL_VBAT;
|
|
|
|
} else if (channel == 18) {
|
|
|
|
channel = ADC_CHANNEL_VREFINT;
|
|
|
|
}
|
2022-10-22 03:56:56 -04:00
|
|
|
#elif defined(STM32L4)
|
|
|
|
if (channel == 0) {
|
|
|
|
channel = ADC_CHANNEL_VREFINT;
|
|
|
|
} else if (channel == 17) {
|
|
|
|
channel = ADC_CHANNEL_TEMPSENSOR;
|
|
|
|
} else if (channel == 18) {
|
|
|
|
channel = ADC_CHANNEL_VBAT;
|
|
|
|
}
|
2016-11-29 20:20:23 -05:00
|
|
|
#endif
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2016-03-22 06:28:35 -04:00
|
|
|
STATIC bool is_adcx_channel(int channel) {
|
2020-02-26 23:36:53 -05:00
|
|
|
#if defined(STM32F411xE)
|
2018-04-10 23:16:54 -04:00
|
|
|
// The HAL has an incorrect IS_ADC_CHANNEL macro for the F411 so we check for temp
|
|
|
|
return IS_ADC_CHANNEL(channel) || channel == ADC_CHANNEL_TEMPSENSOR;
|
2020-02-26 23:36:53 -05:00
|
|
|
#elif defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
|
2016-03-22 06:28:35 -04:00
|
|
|
return IS_ADC_CHANNEL(channel);
|
2022-09-19 04:56:31 -04:00
|
|
|
#elif defined(STM32L1)
|
|
|
|
// The HAL of STM32L1 defines some channels those may not be available on package
|
|
|
|
return __HAL_ADC_IS_CHANNEL_INTERNAL(channel)
|
|
|
|
|| (channel < MP_ARRAY_SIZE(pin_adcall_table) && pin_adcall_table[channel]);
|
2022-02-12 15:36:58 -05:00
|
|
|
#elif defined(STM32G0) || defined(STM32H7)
|
2019-10-11 16:19:33 -04:00
|
|
|
return __HAL_ADC_IS_CHANNEL_INTERNAL(channel)
|
2020-02-26 23:36:53 -05:00
|
|
|
|| IS_ADC_CHANNEL(__HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel));
|
2022-01-31 12:12:48 -05:00
|
|
|
#elif defined(STM32G4) || defined(STM32L4) || defined(STM32WB)
|
2020-06-01 07:29:11 -04:00
|
|
|
ADC_HandleTypeDef handle;
|
|
|
|
handle.Instance = ADCx;
|
|
|
|
return __HAL_ADC_IS_CHANNEL_INTERNAL(channel)
|
|
|
|
|| IS_ADC_CHANNEL(&handle, __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel));
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32H5)
|
|
|
|
// The first argument to the IS_ADC_CHANNEL macro is unused.
|
|
|
|
return __HAL_ADC_IS_CHANNEL_INTERNAL(channel)
|
|
|
|
|| IS_ADC_CHANNEL(NULL, __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel));
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2016-03-22 06:28:35 -04:00
|
|
|
#error Unsupported processor
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2016-03-22 06:28:35 -04:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:44:52 -04:00
|
|
|
STATIC void adc_wait_for_eoc_or_timeout(ADC_HandleTypeDef *adcHandle, int32_t timeout) {
|
2016-03-22 06:28:35 -04:00
|
|
|
uint32_t tickstart = HAL_GetTick();
|
2022-09-19 04:56:31 -04:00
|
|
|
#if defined(STM32F4) || defined(STM32F7) || defined(STM32L1)
|
2021-06-16 17:44:52 -04:00
|
|
|
while ((adcHandle->Instance->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) {
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H5) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
|
2021-06-16 17:44:52 -04:00
|
|
|
while (READ_BIT(adcHandle->Instance->ISR, ADC_FLAG_EOC) != ADC_FLAG_EOC) {
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2016-03-22 06:28:35 -04:00
|
|
|
#error Unsupported processor
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2020-03-26 09:35:04 -04:00
|
|
|
if (((HAL_GetTick() - tickstart) > timeout)) {
|
2016-03-22 06:28:35 -04:00
|
|
|
break; // timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:44:52 -04:00
|
|
|
STATIC void adcx_clock_enable(ADC_HandleTypeDef *adch) {
|
2022-09-19 04:56:31 -04:00
|
|
|
#if defined(STM32F0) || defined(STM32F4) || defined(STM32F7) || defined(STM32L1)
|
2016-03-22 06:28:35 -04:00
|
|
|
ADCx_CLK_ENABLE();
|
2021-09-15 09:08:16 -04:00
|
|
|
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H7B3xx) || defined(STM32H7B3xxQ)
|
|
|
|
__HAL_RCC_ADC12_CLK_ENABLE();
|
|
|
|
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP);
|
2022-02-12 15:36:58 -05:00
|
|
|
#elif defined(STM32G0)
|
|
|
|
__HAL_RCC_ADC_CLK_ENABLE();
|
2021-01-26 08:49:56 -05:00
|
|
|
#elif defined(STM32G4)
|
|
|
|
__HAL_RCC_ADC12_CLK_ENABLE();
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32H5)
|
|
|
|
__HAL_RCC_ADC_CLK_ENABLE();
|
2020-02-26 23:36:53 -05:00
|
|
|
#elif defined(STM32H7)
|
2021-06-16 17:44:52 -04:00
|
|
|
if (adch->Instance == ADC3) {
|
|
|
|
__HAL_RCC_ADC3_CLK_ENABLE();
|
|
|
|
} else {
|
|
|
|
__HAL_RCC_ADC12_CLK_ENABLE();
|
|
|
|
}
|
2018-03-31 21:20:21 -04:00
|
|
|
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP);
|
2020-06-01 07:29:11 -04:00
|
|
|
#elif defined(STM32L4) || defined(STM32WB)
|
|
|
|
if (__HAL_RCC_GET_ADC_SOURCE() == RCC_ADCCLKSOURCE_NONE) {
|
|
|
|
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
|
|
|
|
}
|
2016-03-22 06:28:35 -04:00
|
|
|
__HAL_RCC_ADC_CLK_ENABLE();
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2016-03-22 06:28:35 -04:00
|
|
|
#error Unsupported processor
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2016-03-22 06:28:35 -04:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:46:13 -04:00
|
|
|
STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
|
2021-06-16 17:44:52 -04:00
|
|
|
adcx_clock_enable(adch);
|
2018-04-11 00:46:13 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.Resolution = resolution;
|
|
|
|
adch->Init.ContinuousConvMode = DISABLE;
|
2018-04-11 00:46:13 -04:00
|
|
|
adch->Init.DiscontinuousConvMode = DISABLE;
|
2022-02-12 15:36:58 -05:00
|
|
|
#if !defined(STM32F0) && !defined(STM32G0)
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.NbrOfDiscConversion = 0;
|
2022-02-12 15:36:58 -05:00
|
|
|
#endif
|
|
|
|
#if !defined(STM32F0)
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.NbrOfConversion = 1;
|
2018-05-28 04:10:53 -04:00
|
|
|
#endif
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
|
|
|
adch->Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
|
|
adch->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
2019-09-27 00:27:51 -04:00
|
|
|
#if defined(STM32F0)
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; // 12MHz
|
|
|
|
adch->Init.ScanConvMode = DISABLE;
|
|
|
|
adch->Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
2019-09-27 00:27:51 -04:00
|
|
|
adch->Init.DMAContinuousRequests = DISABLE;
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.SamplingTimeCommon = ADC_SAMPLETIME_55CYCLES_5; // ~4uS
|
2019-09-27 00:27:51 -04:00
|
|
|
#elif defined(STM32F4) || defined(STM32F7)
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
|
|
|
adch->Init.ScanConvMode = DISABLE;
|
|
|
|
adch->Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
2018-03-31 21:20:21 -04:00
|
|
|
adch->Init.DMAContinuousRequests = DISABLE;
|
|
|
|
#elif defined(STM32H7)
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
|
|
|
adch->Init.ScanConvMode = DISABLE;
|
|
|
|
adch->Init.LowPowerAutoWait = DISABLE;
|
|
|
|
adch->Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
|
|
|
|
adch->Init.OversamplingMode = DISABLE;
|
|
|
|
adch->Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
|
2018-03-31 21:20:21 -04:00
|
|
|
adch->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
|
2022-09-19 04:56:31 -04:00
|
|
|
#elif defined(STM32L1)
|
|
|
|
adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
|
|
|
|
adch->Init.ScanConvMode = ADC_SCAN_DISABLE;
|
|
|
|
adch->Init.LowPowerAutoWait = DISABLE;
|
|
|
|
adch->Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
|
|
adch->Init.DMAContinuousRequests = DISABLE;
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32G0) || defined(STM32G4) || defined(STM32H5) || defined(STM32L4) || defined(STM32WB)
|
2023-07-07 21:46:01 -04:00
|
|
|
#if defined(STM32G4)
|
|
|
|
adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV16;
|
|
|
|
#else
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
|
2023-07-07 21:46:01 -04:00
|
|
|
#endif
|
2020-02-26 23:36:53 -05:00
|
|
|
adch->Init.ScanConvMode = ADC_SCAN_DISABLE;
|
|
|
|
adch->Init.LowPowerAutoWait = DISABLE;
|
|
|
|
adch->Init.Overrun = ADC_OVR_DATA_PRESERVED;
|
|
|
|
adch->Init.OversamplingMode = DISABLE;
|
|
|
|
adch->Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
2018-03-31 21:20:21 -04:00
|
|
|
adch->Init.DMAContinuousRequests = DISABLE;
|
2018-04-11 00:46:13 -04:00
|
|
|
#else
|
|
|
|
#error Unsupported processor
|
|
|
|
#endif
|
|
|
|
|
|
|
|
HAL_ADC_Init(adch);
|
2018-03-31 21:20:21 -04:00
|
|
|
|
|
|
|
#if defined(STM32H7)
|
|
|
|
HAL_ADCEx_Calibration_Start(adch, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
|
|
|
|
#endif
|
2022-02-12 15:36:58 -05:00
|
|
|
#if defined(STM32G0)
|
|
|
|
HAL_ADCEx_Calibration_Start(adch);
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32G4) || defined(STM32H5) || defined(STM32L4) || defined(STM32WB)
|
2018-11-16 13:37:31 -05:00
|
|
|
HAL_ADCEx_Calibration_Start(adch, ADC_SINGLE_ENDED);
|
|
|
|
#endif
|
2018-04-11 00:46:13 -04:00
|
|
|
}
|
|
|
|
|
2014-06-16 12:41:58 -04:00
|
|
|
STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
|
2021-06-16 17:44:52 -04:00
|
|
|
adc_obj->handle.Instance = ADCx;
|
2018-04-11 00:46:13 -04:00
|
|
|
adcx_init_periph(&adc_obj->handle, ADC_RESOLUTION_12B);
|
2016-07-19 05:35:33 -04:00
|
|
|
|
2021-01-26 08:49:56 -05:00
|
|
|
#if (defined(STM32G4) || defined(STM32L4)) && defined(ADC_DUALMODE_REGSIMULT_INJECSIMULT)
|
2016-07-19 05:35:33 -04:00
|
|
|
ADC_MultiModeTypeDef multimode;
|
|
|
|
multimode.Mode = ADC_MODE_INDEPENDENT;
|
2020-02-26 23:36:53 -05:00
|
|
|
if (HAL_ADCEx_MultiModeConfigChannel(&adc_obj->handle, &multimode) != HAL_OK) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can not set multimode on ADC1 channel: %d"), adc_obj->channel);
|
2016-07-19 05:35:33 -04:00
|
|
|
}
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2014-06-16 12:41:58 -04:00
|
|
|
}
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2016-07-19 05:35:33 -04:00
|
|
|
STATIC void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel) {
|
2014-03-24 13:49:23 -04:00
|
|
|
ADC_ChannelConfTypeDef sConfig;
|
|
|
|
|
2022-02-12 15:36:58 -05:00
|
|
|
#if defined(STM32G0) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
|
2019-10-11 16:19:33 -04:00
|
|
|
sConfig.Rank = ADC_REGULAR_RANK_1;
|
|
|
|
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel) == 0) {
|
|
|
|
channel = __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel);
|
|
|
|
}
|
|
|
|
#else
|
2014-03-24 13:49:23 -04:00
|
|
|
sConfig.Rank = 1;
|
2019-10-11 16:19:33 -04:00
|
|
|
#endif
|
|
|
|
sConfig.Channel = channel;
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
#if defined(STM32F0)
|
2019-09-27 00:27:51 -04:00
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
|
2020-02-26 23:36:53 -05:00
|
|
|
#elif defined(STM32F4) || defined(STM32F7)
|
2021-06-16 17:44:52 -04:00
|
|
|
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
|
|
|
|
} else {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
|
|
|
|
}
|
2020-02-26 23:36:53 -05:00
|
|
|
#elif defined(STM32H7)
|
2019-10-11 16:36:58 -04:00
|
|
|
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) {
|
2019-10-11 16:38:50 -04:00
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_810CYCLES_5;
|
2018-12-11 20:51:26 -05:00
|
|
|
} else {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
|
|
|
|
}
|
2018-03-31 21:20:21 -04:00
|
|
|
sConfig.SingleDiff = ADC_SINGLE_ENDED;
|
|
|
|
sConfig.OffsetNumber = ADC_OFFSET_NONE;
|
|
|
|
sConfig.OffsetRightShift = DISABLE;
|
|
|
|
sConfig.OffsetSignedSaturation = DISABLE;
|
2022-09-19 04:56:31 -04:00
|
|
|
#elif defined(STM32L1)
|
|
|
|
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES;
|
|
|
|
} else {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES;
|
|
|
|
}
|
2022-02-12 15:36:58 -05:00
|
|
|
#elif defined(STM32G0)
|
|
|
|
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
|
|
|
|
} else {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
|
|
|
|
}
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32G4) || defined(STM32H5) || defined(STM32L4) || defined(STM32WB)
|
2019-10-11 16:36:58 -04:00
|
|
|
if (__HAL_ADC_IS_CHANNEL_INTERNAL(channel)) {
|
2018-09-20 09:51:33 -04:00
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
|
|
|
|
} else {
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
|
|
|
|
}
|
2016-07-19 05:35:33 -04:00
|
|
|
sConfig.SingleDiff = ADC_SINGLE_ENDED;
|
|
|
|
sConfig.OffsetNumber = ADC_OFFSET_NONE;
|
2018-05-28 04:10:53 -04:00
|
|
|
sConfig.Offset = 0;
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2016-03-22 06:28:35 -04:00
|
|
|
#error Unsupported processor
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2018-08-03 23:33:02 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
// On the STM32F0 we must select only one channel at a time to sample, so clear all
|
|
|
|
// channels before calling HAL_ADC_ConfigChannel, which will select the desired one.
|
|
|
|
adc_handle->Instance->CHSELR = 0;
|
|
|
|
#endif
|
|
|
|
|
2016-07-19 05:35:33 -04:00
|
|
|
HAL_ADC_ConfigChannel(adc_handle, &sConfig);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2014-06-16 12:41:58 -04:00
|
|
|
STATIC uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
|
2023-07-07 22:14:00 -04:00
|
|
|
uint32_t value;
|
|
|
|
#if defined(STM32G4)
|
|
|
|
// For STM32G4 there is errata 2.7.7, "Wrong ADC result if conversion done late after
|
|
|
|
// calibration or previous conversion". According to the errata, this can be avoided
|
|
|
|
// by performing two consecutive ADC conversions and keeping the second result.
|
|
|
|
for (uint8_t i = 0; i < 2; i++)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
HAL_ADC_Start(adcHandle);
|
|
|
|
adc_wait_for_eoc_or_timeout(adcHandle, EOC_TIMEOUT);
|
|
|
|
value = adcHandle->Instance->DR;
|
|
|
|
}
|
2014-03-24 13:49:23 -04:00
|
|
|
HAL_ADC_Stop(adcHandle);
|
2018-04-11 00:28:06 -04:00
|
|
|
return value;
|
|
|
|
}
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2018-04-11 00:28:06 -04:00
|
|
|
STATIC uint32_t adc_config_and_read_channel(ADC_HandleTypeDef *adcHandle, uint32_t channel) {
|
|
|
|
adc_config_channel(adcHandle, channel);
|
2018-08-03 23:25:43 -04:00
|
|
|
uint32_t raw_value = adc_read_channel(adcHandle);
|
|
|
|
|
|
|
|
// ST docs say that (at least on STM32F42x and STM32F43x), VBATE must
|
|
|
|
// be disabled when TSVREFE is enabled for TEMPSENSOR and VREFINT
|
|
|
|
// conversions to work. VBATE is enabled by the above call to read
|
|
|
|
// the channel, and here we disable VBATE so a subsequent call for
|
|
|
|
// TEMPSENSOR or VREFINT works correctly.
|
2020-12-16 20:23:09 -05:00
|
|
|
// It's also good to disable the VBAT switch to prevent battery drain,
|
|
|
|
// so disable it for all MCUs.
|
|
|
|
adc_deselect_vbat(adcHandle->Instance, channel);
|
2018-08-03 23:25:43 -04:00
|
|
|
|
|
|
|
return raw_value;
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 03:22:17 -04:00
|
|
|
/* MicroPython bindings : adc object (single channel) */
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_obj_adc_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "<ADC on ");
|
|
|
|
mp_obj_print_helper(print, self->pin_name, PRINT_STR);
|
2018-05-04 01:52:03 -04:00
|
|
|
mp_printf(print, " channel=%u>", self->channel);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \classmethod \constructor(pin)
|
|
|
|
/// Create an ADC object associated with the given pin.
|
|
|
|
/// This allows you to then read analog values on that pin.
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-03-24 15:27:13 -04:00
|
|
|
// check number of arguments
|
2014-04-19 19:16:30 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2014-03-24 15:27:13 -04:00
|
|
|
// 1st argument is the pin name
|
|
|
|
mp_obj_t pin_obj = args[0];
|
2014-03-24 13:49:23 -04:00
|
|
|
|
|
|
|
uint32_t channel;
|
|
|
|
|
2019-01-30 06:05:48 -05:00
|
|
|
if (mp_obj_is_int(pin_obj)) {
|
2016-11-29 20:20:23 -05:00
|
|
|
channel = adc_get_internal_channel(mp_obj_get_int(pin_obj));
|
2014-03-24 13:49:23 -04:00
|
|
|
} else {
|
2014-04-18 17:38:09 -04:00
|
|
|
const pin_obj_t *pin = pin_find(pin_obj);
|
2019-02-05 21:35:28 -05:00
|
|
|
if ((pin->adc_num & PIN_ADC_MASK) == 0) {
|
2021-06-30 22:04:48 -04:00
|
|
|
// No ADC function on the given pin.
|
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Pin(%q) doesn't have ADC capabilities"), pin->name);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
channel = pin->adc_channel;
|
|
|
|
}
|
|
|
|
|
2016-03-22 06:28:35 -04:00
|
|
|
if (!is_adcx_channel(channel)) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("not a valid ADC Channel: %d"), channel);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
2016-08-23 18:13:03 -04:00
|
|
|
|
2021-06-30 22:04:48 -04:00
|
|
|
// If this channel corresponds to a pin then configure the pin in ADC mode.
|
|
|
|
if (channel < MP_ARRAY_SIZE(pin_adc_table)) {
|
|
|
|
const pin_obj_t *pin = pin_adc_table[channel];
|
|
|
|
if (pin != NULL) {
|
|
|
|
mp_hal_pin_config(pin, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0);
|
2016-08-23 18:13:03 -04:00
|
|
|
}
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pyb_obj_adc_t *o = m_new_obj(pyb_obj_adc_t);
|
|
|
|
memset(o, 0, sizeof(*o));
|
2014-03-24 15:27:13 -04:00
|
|
|
o->base.type = &pyb_adc_type;
|
2014-03-24 13:49:23 -04:00
|
|
|
o->pin_name = pin_obj;
|
|
|
|
o->channel = channel;
|
|
|
|
adc_init_single(o);
|
|
|
|
|
2018-07-08 09:25:11 -04:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method read()
|
|
|
|
/// Read the value on the analog pin and return it. The returned value
|
|
|
|
/// will be between 0 and 4095.
|
2014-03-24 15:27:13 -04:00
|
|
|
STATIC mp_obj_t adc_read(mp_obj_t self_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_obj_adc_t *self = MP_OBJ_TO_PTR(self_in);
|
2018-04-11 00:28:06 -04:00
|
|
|
return mp_obj_new_int(adc_config_and_read_channel(&self->handle, self->channel));
|
2014-03-24 15:27:13 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
|
|
|
|
|
2015-07-22 14:37:21 -04:00
|
|
|
/// \method read_timed(buf, timer)
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
2015-07-22 14:37:21 -04:00
|
|
|
/// Read analog values into `buf` at a rate set by the `timer` object.
|
|
|
|
///
|
|
|
|
/// `buf` can be bytearray or array.array for example. The ADC values have
|
|
|
|
/// 12-bit resolution and are stored directly into `buf` if its element size is
|
|
|
|
/// 16 bits or greater. If `buf` has only 8-bit elements (eg a bytearray) then
|
|
|
|
/// the sample resolution will be reduced to 8 bits.
|
|
|
|
///
|
|
|
|
/// `timer` should be a Timer object, and a sample is read each time the timer
|
|
|
|
/// triggers. The timer must already be initialised and running at the desired
|
|
|
|
/// sampling frequency.
|
|
|
|
///
|
|
|
|
/// To support previous behaviour of this function, `timer` can also be an
|
|
|
|
/// integer which specifies the frequency (in Hz) to sample at. In this case
|
|
|
|
/// Timer(6) will be automatically configured to run at the given frequency.
|
|
|
|
///
|
|
|
|
/// Example using a Timer object (preferred way):
|
|
|
|
///
|
|
|
|
/// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
|
|
|
|
/// tim = pyb.Timer(6, freq=10) # create a timer running at 10Hz
|
|
|
|
/// buf = bytearray(100) # creat a buffer to store the samples
|
|
|
|
/// adc.read_timed(buf, tim) # sample 100 values, taking 10s
|
|
|
|
///
|
|
|
|
/// Example using an integer for the frequency:
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
|
|
|
|
/// buf = bytearray(100) # create a buffer of 100 bytes
|
|
|
|
/// adc.read_timed(buf, 10) # read analog values into buf at 10Hz
|
|
|
|
/// # this will take 10 seconds to finish
|
|
|
|
/// for val in buf: # loop over all values
|
|
|
|
/// print(val) # print the value out
|
|
|
|
///
|
|
|
|
/// This function does not allocate any memory.
|
2014-04-15 14:52:56 -04:00
|
|
|
STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_obj_adc_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-15 14:52:56 -04:00
|
|
|
|
2014-04-18 18:28:56 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
|
2015-05-10 07:04:38 -04:00
|
|
|
size_t typesize = mp_binary_get_size('@', bufinfo.typecode, NULL);
|
2014-04-15 14:52:56 -04:00
|
|
|
|
2015-07-22 14:37:21 -04:00
|
|
|
TIM_HandleTypeDef *tim;
|
|
|
|
#if defined(TIM6)
|
|
|
|
if (mp_obj_is_integer(freq_in)) {
|
|
|
|
// freq in Hz given so init TIM6 (legacy behaviour)
|
|
|
|
tim = timer_tim6_init(mp_obj_get_int(freq_in));
|
|
|
|
HAL_TIM_Base_Start(tim);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// use the supplied timer object as the sampling time base
|
|
|
|
tim = pyb_timer_get_handle(freq_in);
|
|
|
|
}
|
2014-04-15 14:52:56 -04:00
|
|
|
|
2015-03-23 18:36:51 -04:00
|
|
|
// configure the ADC channel
|
2016-07-19 05:35:33 -04:00
|
|
|
adc_config_channel(&self->handle, self->channel);
|
2015-03-23 18:36:51 -04:00
|
|
|
|
|
|
|
// This uses the timer in polling mode to do the sampling
|
|
|
|
// TODO use DMA
|
|
|
|
|
2015-03-13 18:11:50 -04:00
|
|
|
uint nelems = bufinfo.len / typesize;
|
|
|
|
for (uint index = 0; index < nelems; index++) {
|
2015-03-23 18:36:51 -04:00
|
|
|
// Wait for the timer to trigger so we sample at the correct frequency
|
2015-07-22 14:37:21 -04:00
|
|
|
while (__HAL_TIM_GET_FLAG(tim, TIM_FLAG_UPDATE) == RESET) {
|
2014-04-15 14:52:56 -04:00
|
|
|
}
|
2015-07-22 14:37:21 -04:00
|
|
|
__HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE);
|
2015-03-23 18:36:51 -04:00
|
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
// for the first sample we need to turn the ADC on
|
|
|
|
HAL_ADC_Start(&self->handle);
|
|
|
|
} else {
|
|
|
|
// for subsequent samples we can just set the "start sample" bit
|
2022-09-19 04:56:31 -04:00
|
|
|
#if defined(STM32F4) || defined(STM32F7) || defined(STM32L1)
|
2021-06-16 17:44:52 -04:00
|
|
|
self->handle.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H5) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
|
2021-06-16 17:44:52 -04:00
|
|
|
SET_BIT(self->handle.Instance->CR, ADC_CR_ADSTART);
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2016-03-22 06:28:35 -04:00
|
|
|
#error Unsupported processor
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2015-03-23 18:36:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for sample to complete
|
2021-06-16 17:44:52 -04:00
|
|
|
adc_wait_for_eoc_or_timeout(&self->handle, EOC_TIMEOUT);
|
2015-03-23 18:36:51 -04:00
|
|
|
|
|
|
|
// read value
|
2021-06-16 17:44:52 -04:00
|
|
|
uint value = self->handle.Instance->DR;
|
2015-03-23 18:36:51 -04:00
|
|
|
|
|
|
|
// store value in buffer
|
2014-04-18 18:28:56 -04:00
|
|
|
if (typesize == 1) {
|
|
|
|
value >>= 4;
|
|
|
|
}
|
|
|
|
mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value);
|
2014-04-15 14:52:56 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 18:36:51 -04:00
|
|
|
// turn the ADC off
|
|
|
|
HAL_ADC_Stop(&self->handle);
|
|
|
|
|
2015-07-22 14:37:21 -04:00
|
|
|
#if defined(TIM6)
|
|
|
|
if (mp_obj_is_integer(freq_in)) {
|
|
|
|
// stop timer if we initialised TIM6 in this function (legacy behaviour)
|
|
|
|
HAL_TIM_Base_Stop(tim);
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-15 14:52:56 -04:00
|
|
|
|
|
|
|
return mp_obj_new_int(bufinfo.len);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(adc_read_timed_obj, adc_read_timed);
|
|
|
|
|
2018-03-04 04:07:40 -05:00
|
|
|
// read_timed_multi((adcx, adcy, ...), (bufx, bufy, ...), timer)
|
|
|
|
//
|
|
|
|
// Read analog values from multiple ADC's into buffers at a rate set by the
|
|
|
|
// timer. The ADC values have 12-bit resolution and are stored directly into
|
|
|
|
// the corresponding buffer if its element size is 16 bits or greater, otherwise
|
|
|
|
// the sample resolution will be reduced to 8 bits.
|
|
|
|
//
|
|
|
|
// This function should not allocate any heap memory.
|
|
|
|
STATIC mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_in, mp_obj_t tim_in) {
|
|
|
|
size_t nadcs, nbufs;
|
|
|
|
mp_obj_t *adc_array, *buf_array;
|
|
|
|
mp_obj_get_array(adc_array_in, &nadcs, &adc_array);
|
|
|
|
mp_obj_get_array(buf_array_in, &nbufs, &buf_array);
|
|
|
|
|
|
|
|
if (nadcs < 1) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("need at least 1 ADC"));
|
2018-03-04 04:07:40 -05:00
|
|
|
}
|
|
|
|
if (nadcs != nbufs) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("length of ADC and buffer lists differ"));
|
2018-03-04 04:07:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get buf for first ADC, get word size, check other buffers match in type
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf_array[0], &bufinfo, MP_BUFFER_WRITE);
|
|
|
|
size_t typesize = mp_binary_get_size('@', bufinfo.typecode, NULL);
|
2018-04-11 00:08:23 -04:00
|
|
|
void *bufptrs[nbufs];
|
2018-03-04 04:07:40 -05:00
|
|
|
for (uint array_index = 0; array_index < nbufs; array_index++) {
|
|
|
|
mp_buffer_info_t bufinfo_curr;
|
|
|
|
mp_get_buffer_raise(buf_array[array_index], &bufinfo_curr, MP_BUFFER_WRITE);
|
|
|
|
if ((bufinfo.len != bufinfo_curr.len) || (bufinfo.typecode != bufinfo_curr.typecode)) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("size and type of buffers must match"));
|
2018-03-04 04:07:40 -05:00
|
|
|
}
|
2018-04-11 00:08:23 -04:00
|
|
|
bufptrs[array_index] = bufinfo_curr.buf;
|
2018-03-04 04:07:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the supplied timer object as the sampling time base
|
|
|
|
TIM_HandleTypeDef *tim;
|
|
|
|
tim = pyb_timer_get_handle(tim_in);
|
|
|
|
|
|
|
|
// Start adc; this is slow so wait for it to start
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_obj_adc_t *adc0 = MP_OBJ_TO_PTR(adc_array[0]);
|
2018-03-04 04:07:40 -05:00
|
|
|
adc_config_channel(&adc0->handle, adc0->channel);
|
|
|
|
HAL_ADC_Start(&adc0->handle);
|
|
|
|
// Wait for sample to complete and discard
|
2021-06-16 17:44:52 -04:00
|
|
|
adc_wait_for_eoc_or_timeout(&adc0->handle, EOC_TIMEOUT);
|
2018-03-04 04:07:40 -05:00
|
|
|
// Read (and discard) value
|
2021-06-16 17:44:52 -04:00
|
|
|
uint value = adc0->handle.Instance->DR;
|
2018-03-04 04:07:40 -05:00
|
|
|
|
|
|
|
// Ensure first sample is on a timer tick
|
|
|
|
__HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE);
|
|
|
|
while (__HAL_TIM_GET_FLAG(tim, TIM_FLAG_UPDATE) == RESET) {
|
|
|
|
}
|
|
|
|
__HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE);
|
|
|
|
|
|
|
|
// Overrun check: assume success
|
|
|
|
bool success = true;
|
|
|
|
size_t nelems = bufinfo.len / typesize;
|
|
|
|
for (size_t elem_index = 0; elem_index < nelems; elem_index++) {
|
|
|
|
if (__HAL_TIM_GET_FLAG(tim, TIM_FLAG_UPDATE) != RESET) {
|
|
|
|
// Timer has already triggered
|
|
|
|
success = false;
|
|
|
|
} else {
|
|
|
|
// Wait for the timer to trigger so we sample at the correct frequency
|
|
|
|
while (__HAL_TIM_GET_FLAG(tim, TIM_FLAG_UPDATE) == RESET) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE);
|
|
|
|
|
|
|
|
for (size_t array_index = 0; array_index < nadcs; array_index++) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_obj_adc_t *adc = MP_OBJ_TO_PTR(adc_array[array_index]);
|
2018-03-04 04:07:40 -05:00
|
|
|
// configure the ADC channel
|
|
|
|
adc_config_channel(&adc->handle, adc->channel);
|
|
|
|
// for the first sample we need to turn the ADC on
|
|
|
|
// ADC is started: set the "start sample" bit
|
2022-09-19 04:56:31 -04:00
|
|
|
#if defined(STM32F4) || defined(STM32F7) || defined(STM32L1)
|
2021-06-16 17:44:52 -04:00
|
|
|
adc->handle.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
|
2023-03-22 01:38:49 -04:00
|
|
|
#elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H5) || defined(STM32H7) || defined(STM32L4) || defined(STM32WB)
|
2021-06-16 17:44:52 -04:00
|
|
|
SET_BIT(adc->handle.Instance->CR, ADC_CR_ADSTART);
|
2018-03-04 04:07:40 -05:00
|
|
|
#else
|
|
|
|
#error Unsupported processor
|
|
|
|
#endif
|
|
|
|
// wait for sample to complete
|
2021-06-16 17:44:52 -04:00
|
|
|
adc_wait_for_eoc_or_timeout(&adc->handle, EOC_TIMEOUT);
|
2018-03-04 04:07:40 -05:00
|
|
|
|
|
|
|
// read value
|
2021-06-16 17:44:52 -04:00
|
|
|
value = adc->handle.Instance->DR;
|
2018-03-04 04:07:40 -05:00
|
|
|
|
|
|
|
// store values in buffer
|
|
|
|
if (typesize == 1) {
|
|
|
|
value >>= 4;
|
|
|
|
}
|
2018-04-11 00:08:23 -04:00
|
|
|
mp_binary_set_val_array_from_int(bufinfo.typecode, bufptrs[array_index], elem_index, value);
|
2018-03-04 04:07:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn the ADC off
|
2018-07-08 09:25:11 -04:00
|
|
|
adc0 = MP_OBJ_TO_PTR(adc_array[0]);
|
2018-03-04 04:07:40 -05:00
|
|
|
HAL_ADC_Stop(&adc0->handle);
|
|
|
|
|
|
|
|
return mp_obj_new_bool(success);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(adc_read_timed_multi_fun_obj, adc_read_timed_multi);
|
|
|
|
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(adc_read_timed_multi_obj, MP_ROM_PTR(&adc_read_timed_multi_fun_obj));
|
|
|
|
|
2017-05-06 03:03:40 -04:00
|
|
|
STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&adc_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_timed), MP_ROM_PTR(&adc_read_timed_obj) },
|
2018-03-04 04:07:40 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_timed_multi), MP_ROM_PTR(&adc_read_timed_multi_obj) },
|
2014-03-24 15:27:13 -04:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pyb_adc_type,
|
|
|
|
MP_QSTR_ADC,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, adc_make_new,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, adc_print,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &adc_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2014-03-24 13:49:23 -04:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* adc all object */
|
|
|
|
|
2014-04-18 17:38:09 -04:00
|
|
|
typedef struct _pyb_adc_all_obj_t {
|
2014-03-24 13:49:23 -04:00
|
|
|
mp_obj_base_t base;
|
|
|
|
ADC_HandleTypeDef handle;
|
2014-04-18 17:38:09 -04:00
|
|
|
} pyb_adc_all_obj_t;
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2022-09-19 04:56:31 -04:00
|
|
|
float adc_read_core_vref(ADC_HandleTypeDef *adcHandle);
|
|
|
|
|
2016-11-27 20:06:10 -05:00
|
|
|
void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution, uint32_t en_mask) {
|
2014-03-24 13:49:23 -04:00
|
|
|
|
|
|
|
switch (resolution) {
|
2018-03-31 21:20:21 -04:00
|
|
|
#if !defined(STM32H7)
|
2020-02-26 23:36:53 -05:00
|
|
|
case 6:
|
|
|
|
resolution = ADC_RESOLUTION_6B;
|
|
|
|
break;
|
2018-03-31 21:20:21 -04:00
|
|
|
#endif
|
2020-02-26 23:36:53 -05:00
|
|
|
case 8:
|
|
|
|
resolution = ADC_RESOLUTION_8B;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
resolution = ADC_RESOLUTION_10B;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
resolution = ADC_RESOLUTION_12B;
|
|
|
|
break;
|
2018-12-11 20:51:46 -05:00
|
|
|
#if defined(STM32H7)
|
2020-02-26 23:36:53 -05:00
|
|
|
case 16:
|
|
|
|
resolution = ADC_RESOLUTION_16B;
|
|
|
|
break;
|
2018-12-11 20:51:46 -05:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
default:
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("resolution %d not supported"), resolution);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2021-06-30 22:04:48 -04:00
|
|
|
for (uint32_t channel = 0; channel < MP_ARRAY_SIZE(pin_adcall_table); ++channel) {
|
2016-11-27 20:06:10 -05:00
|
|
|
// only initialise those channels that are selected with the en_mask
|
|
|
|
if (en_mask & (1 << channel)) {
|
2021-06-30 22:04:48 -04:00
|
|
|
// If this channel corresponds to a pin then configure the pin in ADC mode.
|
2021-07-01 10:15:31 -04:00
|
|
|
const pin_obj_t *pin = pin_adcall_table[channel];
|
2016-11-27 20:06:10 -05:00
|
|
|
if (pin) {
|
2018-05-01 22:06:23 -04:00
|
|
|
mp_hal_pin_config(pin, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0);
|
2016-11-27 20:06:10 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2021-07-01 10:15:31 -04:00
|
|
|
adc_all->handle.Instance = ADCALLx;
|
2018-04-11 00:46:13 -04:00
|
|
|
adcx_init_periph(&adc_all->handle, resolution);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2014-03-25 02:23:09 -04:00
|
|
|
int adc_get_resolution(ADC_HandleTypeDef *adcHandle) {
|
2022-09-19 04:56:31 -04:00
|
|
|
#if defined(STM32L1)
|
|
|
|
uint32_t res_reg = adcHandle->Instance->CR1 & ADC_CR1_RES_Msk;
|
|
|
|
#else
|
2018-02-12 23:37:35 -05:00
|
|
|
uint32_t res_reg = ADC_GET_RESOLUTION(adcHandle);
|
2022-09-19 04:56:31 -04:00
|
|
|
#endif
|
2014-03-25 02:23:09 -04:00
|
|
|
|
|
|
|
switch (res_reg) {
|
2019-10-11 16:27:13 -04:00
|
|
|
#if !defined(STM32H7)
|
2020-02-26 23:36:53 -05:00
|
|
|
case ADC_RESOLUTION_6B:
|
|
|
|
return 6;
|
2018-03-31 21:20:21 -04:00
|
|
|
#endif
|
2020-02-26 23:36:53 -05:00
|
|
|
case ADC_RESOLUTION_8B:
|
|
|
|
return 8;
|
|
|
|
case ADC_RESOLUTION_10B:
|
|
|
|
return 10;
|
2018-12-11 20:51:46 -05:00
|
|
|
#if defined(STM32H7)
|
2020-02-26 23:36:53 -05:00
|
|
|
case ADC_RESOLUTION_16B:
|
|
|
|
return 16;
|
2018-12-11 20:51:46 -05:00
|
|
|
#endif
|
2014-03-25 02:23:09 -04:00
|
|
|
}
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:50:37 -05:00
|
|
|
STATIC uint32_t adc_config_and_read_ref(ADC_HandleTypeDef *adcHandle, uint32_t channel) {
|
|
|
|
uint32_t raw_value = adc_config_and_read_channel(adcHandle, channel);
|
|
|
|
// Scale raw reading to the number of bits used by the calibration constants
|
|
|
|
return raw_value << (ADC_CAL_BITS - adc_get_resolution(adcHandle));
|
|
|
|
}
|
2014-03-25 02:23:09 -04:00
|
|
|
|
2018-12-11 20:50:37 -05:00
|
|
|
int adc_read_core_temp(ADC_HandleTypeDef *adcHandle) {
|
2021-01-26 08:49:56 -05:00
|
|
|
#if defined(STM32G4)
|
|
|
|
int32_t raw_value = 0;
|
|
|
|
if (adcHandle->Instance == ADC1) {
|
|
|
|
raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR_ADC1);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2018-12-11 20:50:37 -05:00
|
|
|
int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR);
|
2021-01-26 08:49:56 -05:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
return ((raw_value - CORE_TEMP_V25) / CORE_TEMP_AVG_SLOPE) + 25;
|
|
|
|
}
|
|
|
|
|
2014-09-26 12:04:05 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2016-11-29 20:04:21 -05:00
|
|
|
// correction factor for reference value
|
|
|
|
STATIC volatile float adc_refcor = 1.0f;
|
|
|
|
|
2016-11-29 20:05:53 -05:00
|
|
|
float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
|
2023-07-07 21:59:35 -04:00
|
|
|
#if defined(STM32G4) || defined(STM32L1) || defined(STM32L4)
|
|
|
|
// Update the reference correction factor before reading tempsensor
|
|
|
|
// because TS_CAL1 and TS_CAL2 of STM32G4,L1/L4 are at VDDA=3.0V
|
|
|
|
adc_read_core_vref(adcHandle);
|
|
|
|
#endif
|
|
|
|
|
2021-01-26 08:49:56 -05:00
|
|
|
#if defined(STM32G4)
|
|
|
|
int32_t raw_value = 0;
|
|
|
|
if (adcHandle->Instance == ADC1) {
|
|
|
|
raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR_ADC1);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-07 21:59:35 -04:00
|
|
|
float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 100.0f;
|
2021-01-26 08:49:56 -05:00
|
|
|
#else
|
2018-12-11 20:50:37 -05:00
|
|
|
int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR);
|
2020-03-31 08:48:08 -04:00
|
|
|
float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 80.0f;
|
2023-07-07 21:59:35 -04:00
|
|
|
#endif
|
2016-11-29 20:05:53 -05:00
|
|
|
return (((float)raw_value * adc_refcor - *ADC_CAL1) / core_temp_avg_slope) + 30.0f;
|
|
|
|
}
|
|
|
|
|
2014-03-24 15:27:13 -04:00
|
|
|
float adc_read_core_vbat(ADC_HandleTypeDef *adcHandle) {
|
2023-07-07 23:01:01 -04:00
|
|
|
#if defined(STM32G4) || defined(STM32L4)
|
|
|
|
// Update the reference correction factor before reading tempsensor
|
|
|
|
// because VREFINT of STM32G4,L4 is at VDDA=3.0V
|
|
|
|
adc_read_core_vref(adcHandle);
|
|
|
|
#endif
|
|
|
|
|
2022-09-19 04:56:31 -04:00
|
|
|
#if defined(STM32L152xE)
|
|
|
|
mp_raise_NotImplementedError(MP_ERROR_TEXT("read_core_vbat not supported"));
|
|
|
|
#else
|
2018-12-11 20:50:37 -05:00
|
|
|
uint32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_VBAT);
|
2016-11-29 20:04:21 -05:00
|
|
|
return raw_value * VBAT_DIV * ADC_SCALE * adc_refcor;
|
2022-09-19 04:56:31 -04:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2014-03-24 15:27:13 -04:00
|
|
|
float adc_read_core_vref(ADC_HandleTypeDef *adcHandle) {
|
2018-12-11 20:50:37 -05:00
|
|
|
uint32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_VREFINT);
|
2014-03-25 02:23:09 -04:00
|
|
|
|
2016-11-29 20:04:21 -05:00
|
|
|
// update the reference correction factor
|
|
|
|
adc_refcor = ((float)(*VREFIN_CAL)) / ((float)raw_value);
|
|
|
|
|
|
|
|
return (*VREFIN_CAL) * ADC_SCALE;
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
2014-09-26 12:04:05 -04:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 03:22:17 -04:00
|
|
|
/* MicroPython bindings : adc_all object */
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t adc_all_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-04-18 17:38:09 -04:00
|
|
|
// check number of arguments
|
2016-11-27 20:06:10 -05:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 2, false);
|
2014-04-18 17:38:09 -04:00
|
|
|
|
|
|
|
// make ADCAll object
|
2022-04-22 03:09:15 -04:00
|
|
|
pyb_adc_all_obj_t *o = mp_obj_malloc(pyb_adc_all_obj_t, &pyb_adc_all_type);
|
2016-11-27 20:06:10 -05:00
|
|
|
mp_int_t res = mp_obj_get_int(args[0]);
|
|
|
|
uint32_t en_mask = 0xffffffff;
|
|
|
|
if (n_args > 1) {
|
2020-02-26 23:36:53 -05:00
|
|
|
en_mask = mp_obj_get_int(args[1]);
|
2016-11-27 20:06:10 -05:00
|
|
|
}
|
|
|
|
adc_init_all(o, res, en_mask);
|
2014-04-18 17:38:09 -04:00
|
|
|
|
2018-07-08 09:25:11 -04:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
|
|
|
|
2014-03-24 15:27:13 -04:00
|
|
|
STATIC mp_obj_t adc_all_read_channel(mp_obj_t self_in, mp_obj_t channel) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-11-29 20:20:23 -05:00
|
|
|
uint32_t chan = adc_get_internal_channel(mp_obj_get_int(channel));
|
2022-08-28 02:40:00 -04:00
|
|
|
if (!is_adcx_channel(chan)) {
|
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("not a valid ADC Channel: %d"), chan);
|
|
|
|
}
|
2014-03-24 13:49:23 -04:00
|
|
|
uint32_t data = adc_config_and_read_channel(&self->handle, chan);
|
|
|
|
return mp_obj_new_int(data);
|
|
|
|
}
|
2014-04-18 17:38:09 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(adc_all_read_channel_obj, adc_all_read_channel);
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2014-03-24 15:27:13 -04:00
|
|
|
STATIC mp_obj_t adc_all_read_core_temp(mp_obj_t self_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-11-29 20:05:53 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
float data = adc_read_core_temp_float(&self->handle);
|
|
|
|
return mp_obj_new_float(data);
|
|
|
|
#else
|
2020-02-26 23:36:53 -05:00
|
|
|
int data = adc_read_core_temp(&self->handle);
|
2014-03-24 13:49:23 -04:00
|
|
|
return mp_obj_new_int(data);
|
2016-11-29 20:05:53 -05:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
}
|
2014-04-18 17:38:09 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_temp_obj, adc_all_read_core_temp);
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2014-09-26 12:04:05 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-24 15:27:13 -04:00
|
|
|
STATIC mp_obj_t adc_all_read_core_vbat(mp_obj_t self_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-03-24 13:49:23 -04:00
|
|
|
float data = adc_read_core_vbat(&self->handle);
|
|
|
|
return mp_obj_new_float(data);
|
|
|
|
}
|
2014-04-18 17:38:09 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_vbat_obj, adc_all_read_core_vbat);
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2014-03-24 15:27:13 -04:00
|
|
|
STATIC mp_obj_t adc_all_read_core_vref(mp_obj_t self_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2020-02-26 23:36:53 -05:00
|
|
|
float data = adc_read_core_vref(&self->handle);
|
2014-03-24 13:49:23 -04:00
|
|
|
return mp_obj_new_float(data);
|
|
|
|
}
|
2014-03-24 15:27:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_vref_obj, adc_all_read_core_vref);
|
2016-11-29 20:06:48 -05:00
|
|
|
|
|
|
|
STATIC mp_obj_t adc_all_read_vref(mp_obj_t self_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-11-29 20:06:48 -05:00
|
|
|
adc_read_core_vref(&self->handle);
|
2018-09-20 09:50:54 -04:00
|
|
|
return mp_obj_new_float(ADC_SCALE_V * adc_refcor);
|
2016-11-29 20:06:48 -05:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_vref_obj, adc_all_read_vref);
|
2014-09-26 12:04:05 -04:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2017-05-06 03:03:40 -04:00
|
|
|
STATIC const mp_rom_map_elem_t adc_all_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_channel), MP_ROM_PTR(&adc_all_read_channel_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_core_temp), MP_ROM_PTR(&adc_all_read_core_temp_obj) },
|
2020-02-26 23:36:53 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2017-05-06 03:03:40 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_core_vbat), MP_ROM_PTR(&adc_all_read_core_vbat_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_core_vref), MP_ROM_PTR(&adc_all_read_core_vref_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read_vref), MP_ROM_PTR(&adc_all_read_vref_obj) },
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2014-03-24 13:49:23 -04:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(adc_all_locals_dict, adc_all_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pyb_adc_all_type,
|
|
|
|
MP_QSTR_ADCAll,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, adc_all_make_new,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &adc_all_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2018-02-21 22:22:45 -05:00
|
|
|
|
|
|
|
#endif // MICROPY_HW_ENABLE_ADC
|