adc.c 30.3 KB
Newer Older
1
/*
2
 * This file is part of the MicroPython project, http://micropython.org/
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 */

D
Dave Hylands 已提交
27 28 29
#include <stdio.h>
#include <string.h>

30 31
#include "py/runtime.h"
#include "py/binary.h"
32
#include "py/mphal.h"
D
Dave Hylands 已提交
33 34
#include "adc.h"
#include "pin.h"
35
#include "timer.h"
D
Dave Hylands 已提交
36

37 38
#if MICROPY_HW_ENABLE_ADC

39 40 41 42 43 44 45 46 47 48 49 50 51
/// \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
D
Dave Hylands 已提交
52 53

/* ADC defintions */
54

55 56
#if defined(STM32H7)
#define ADCx                    (ADC3)
57 58
#define PIN_ADC_MASK            PIN_ADC3
#define pin_adc_table           pin_adc3
59
#else
D
Dave Hylands 已提交
60
#define ADCx                    (ADC1)
61 62
#define PIN_ADC_MASK            PIN_ADC1
#define pin_adc_table           pin_adc1
63
#endif
64

65
#define ADCx_CLK_ENABLE         __HAL_RCC_ADC1_CLK_ENABLE
D
Dave Hylands 已提交
66
#define ADC_NUM_CHANNELS        (19)
67

68 69 70 71
#if defined(STM32F0)

#define ADC_FIRST_GPIO_CHANNEL  (0)
#define ADC_LAST_GPIO_CHANNEL   (15)
72
#define ADC_SCALE_V             (3.3f)
73 74 75
#define ADC_CAL_ADDRESS         (0x1ffff7ba)
#define ADC_CAL1                ((uint16_t*)0x1ffff7b8)
#define ADC_CAL2                ((uint16_t*)0x1ffff7c2)
76
#define ADC_CAL_BITS            (12)
77 78

#elif defined(STM32F4)
79 80 81

#define ADC_FIRST_GPIO_CHANNEL  (0)
#define ADC_LAST_GPIO_CHANNEL   (15)
82
#define ADC_SCALE_V             (3.3f)
83
#define ADC_CAL_ADDRESS         (0x1fff7a2a)
84 85
#define ADC_CAL1                ((uint16_t*)(ADC_CAL_ADDRESS + 2))
#define ADC_CAL2                ((uint16_t*)(ADC_CAL_ADDRESS + 4))
86
#define ADC_CAL_BITS            (12)
87

88
#elif defined(STM32F7)
89

90 91
#define ADC_FIRST_GPIO_CHANNEL  (0)
#define ADC_LAST_GPIO_CHANNEL   (15)
92
#define ADC_SCALE_V             (3.3f)
93 94 95 96
#if defined(STM32F722xx) || defined(STM32F723xx) || \
    defined(STM32F732xx) || defined(STM32F733xx)
#define ADC_CAL_ADDRESS         (0x1ff07a2a)
#else
97
#define ADC_CAL_ADDRESS         (0x1ff0f44a)
98 99
#endif

100 101
#define ADC_CAL1                ((uint16_t*)(ADC_CAL_ADDRESS + 2))
#define ADC_CAL2                ((uint16_t*)(ADC_CAL_ADDRESS + 4))
102
#define ADC_CAL_BITS            (12)
103

104 105 106 107
#elif defined(STM32H7)

#define ADC_FIRST_GPIO_CHANNEL  (0)
#define ADC_LAST_GPIO_CHANNEL   (16)
108
#define ADC_SCALE_V             (3.3f)
109 110 111
#define ADC_CAL_ADDRESS         (0x1FF1E860)
#define ADC_CAL1                ((uint16_t*)(0x1FF1E820))
#define ADC_CAL2                ((uint16_t*)(0x1FF1E840))
112
#define ADC_CAL_BITS            (16)
113 114
#define ADC_CHANNEL_VBAT        ADC_CHANNEL_VBAT_DIV4

115
#elif defined(STM32L4)
116

117 118
#define ADC_FIRST_GPIO_CHANNEL  (1)
#define ADC_LAST_GPIO_CHANNEL   (16)
119
#define ADC_SCALE_V             (3.0f)
120
#define ADC_CAL_ADDRESS         (0x1fff75aa)
121 122
#define ADC_CAL1                ((uint16_t*)(ADC_CAL_ADDRESS - 2))
#define ADC_CAL2                ((uint16_t*)(ADC_CAL_ADDRESS + 0x20))
123
#define ADC_CAL_BITS            (12)
124

125
#else
126

127
#error Unsupported processor
128

129
#endif
D
Dave Hylands 已提交
130

131 132 133
#if defined(STM32F091xC)
#define VBAT_DIV (2)
#elif defined(STM32F405xx) || defined(STM32F415xx) || \
D
Dave Hylands 已提交
134
    defined(STM32F407xx) || defined(STM32F417xx) || \
135 136
    defined(STM32F401xC) || defined(STM32F401xE) || \
    defined(STM32F411xE)
D
Dave Hylands 已提交
137 138
#define VBAT_DIV (2)
#elif defined(STM32F427xx) || defined(STM32F429xx) || \
139
      defined(STM32F437xx) || defined(STM32F439xx) || \
C
Chris Mason 已提交
140
      defined(STM32F446xx) || defined(STM32F413xx) || \
141 142
      defined(STM32F722xx) || defined(STM32F723xx) || \
      defined(STM32F732xx) || defined(STM32F733xx) || \
143 144
      defined(STM32F746xx) || defined(STM32F765xx) || \
      defined(STM32F767xx) || defined(STM32F769xx)
D
Dave Hylands 已提交
145
#define VBAT_DIV (4)
146 147
#elif defined(STM32H743xx)
#define VBAT_DIV (4)
148 149
#elif defined(STM32L432xx) || defined(STM32L475xx) || \
      defined(STM32L476xx) || defined(STM32L496xx)
150
#define VBAT_DIV (3)
151 152
#else
#error Unsupported processor
D
Dave Hylands 已提交
153 154
#endif

155 156 157
// Timeout for waiting for end-of-conversion, in ms
#define EOC_TIMEOUT (10)

D
Dave Hylands 已提交
158 159 160 161
/* Core temperature sensor definitions */
#define CORE_TEMP_V25          (943)  /* (0.76v/3.3v)*(2^ADC resoultion) */
#define CORE_TEMP_AVG_SLOPE    (3)    /* (2.5mv/3.3v)*(2^ADC resoultion) */

162
// scale and calibration values for VBAT and VREF
163
#define ADC_SCALE (ADC_SCALE_V / ((1 << ADC_CAL_BITS) - 1))
164 165
#define VREFIN_CAL ((uint16_t *)ADC_CAL_ADDRESS)

D
Dave Hylands 已提交
166 167 168 169 170 171 172
typedef struct _pyb_obj_adc_t {
    mp_obj_base_t base;
    mp_obj_t pin_name;
    int channel;
    ADC_HandleTypeDef handle;
} pyb_obj_adc_t;

173 174
// convert user-facing channel number into internal channel number
static inline uint32_t adc_get_internal_channel(uint32_t channel) {
175
    #if defined(STM32F4) || defined(STM32F7)
176 177 178 179 180 181 182 183 184
    // 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;
    }
    #endif
    return channel;
}

185
STATIC bool is_adcx_channel(int channel) {
186 187 188
#if defined(STM32F411xE)
    // 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;
189
#elif defined(STM32F0) || defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
190
    return IS_ADC_CHANNEL(channel);
191
#elif defined(STM32L4)
192 193 194 195 196 197 198 199 200 201
    ADC_HandleTypeDef handle;
    handle.Instance = ADCx;
    return IS_ADC_CHANNEL(&handle, channel);
#else
    #error Unsupported processor
#endif
}

STATIC void adc_wait_for_eoc_or_timeout(int32_t timeout) {
    uint32_t tickstart = HAL_GetTick();
202
#if defined(STM32F4) || defined(STM32F7)
203
    while ((ADCx->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) {
204
#elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4)
205 206 207 208 209 210 211 212 213 214 215
    while (READ_BIT(ADCx->ISR, ADC_FLAG_EOC) != ADC_FLAG_EOC) {
#else
    #error Unsupported processor
#endif
        if (((HAL_GetTick() - tickstart ) > timeout)) {
            break; // timeout
        }
    }
}

STATIC void adcx_clock_enable(void) {
216
#if defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
217
    ADCx_CLK_ENABLE();
218 219 220
#elif defined(STM32H7)
    __HAL_RCC_ADC3_CLK_ENABLE();
    __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP);
221
#elif defined(STM32L4)
222 223 224 225 226 227
    __HAL_RCC_ADC_CLK_ENABLE();
#else
    #error Unsupported processor
#endif
}

228 229 230 231 232 233 234
STATIC void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
    adcx_clock_enable();

    adch->Instance                   = ADCx;
    adch->Init.Resolution            = resolution;
    adch->Init.ContinuousConvMode    = DISABLE;
    adch->Init.DiscontinuousConvMode = DISABLE;
235
    #if !defined(STM32F0)
236 237
    adch->Init.NbrOfDiscConversion   = 0;
    adch->Init.NbrOfConversion       = 1;
238
    #endif
239 240 241
    adch->Init.EOCSelection          = ADC_EOC_SINGLE_CONV;
    adch->Init.ExternalTrigConv      = ADC_SOFTWARE_START;
    adch->Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE;
242
    #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
243 244
    adch->Init.ClockPrescaler        = ADC_CLOCK_SYNC_PCLK_DIV2;
    adch->Init.ScanConvMode          = DISABLE;
245 246 247 248 249 250 251 252 253 254 255
    adch->Init.DataAlign             = ADC_DATAALIGN_RIGHT;
    adch->Init.DMAContinuousRequests = DISABLE;
    #elif defined(STM32H7)
    adch->Init.ClockPrescaler        = ADC_CLOCK_SYNC_PCLK_DIV4;
    adch->Init.BoostMode             = ENABLE;
    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;
    adch->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
256 257 258 259 260 261
    #elif defined(STM32L4)
    adch->Init.ClockPrescaler        = ADC_CLOCK_ASYNC_DIV1;
    adch->Init.ScanConvMode          = ADC_SCAN_DISABLE;
    adch->Init.LowPowerAutoWait      = DISABLE;
    adch->Init.Overrun               = ADC_OVR_DATA_PRESERVED;
    adch->Init.OversamplingMode      = DISABLE;
262 263
    adch->Init.DataAlign             = ADC_DATAALIGN_RIGHT;
    adch->Init.DMAContinuousRequests = DISABLE;
264 265 266 267
    #else
    #error Unsupported processor
    #endif

268 269 270 271
    #if defined(STM32F0)
    adch->Init.SamplingTimeCommon = ADC_SAMPLETIME_71CYCLES_5;
    #endif

272
    HAL_ADC_Init(adch);
273 274 275 276

    #if defined(STM32H7)
    HAL_ADCEx_Calibration_Start(adch, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
    #endif
277 278 279
    #if defined(STM32L4)
    HAL_ADCEx_Calibration_Start(adch, ADC_SINGLE_ENDED);
    #endif
280 281
}

282
STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
283
    if (!is_adcx_channel(adc_obj->channel)) {
D
Dave Hylands 已提交
284 285 286
        return;
    }

287
    if (ADC_FIRST_GPIO_CHANNEL <= adc_obj->channel && adc_obj->channel <= ADC_LAST_GPIO_CHANNEL) {
288
        // Channels 0-16 correspond to real pins. Configure the GPIO pin in ADC mode.
289
        const pin_obj_t *pin = pin_adc_table[adc_obj->channel];
290
        mp_hal_pin_config(pin, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0);
D
Dave Hylands 已提交
291 292
    }

293
    adcx_init_periph(&adc_obj->handle, ADC_RESOLUTION_12B);
294

295
#if defined(STM32L4) && defined(ADC_DUALMODE_REGSIMULT_INJECSIMULT)
296 297
    ADC_MultiModeTypeDef multimode;
    multimode.Mode = ADC_MODE_INDEPENDENT;
298
    if (HAL_ADCEx_MultiModeConfigChannel(&adc_obj->handle, &multimode) != HAL_OK)
299 300 301 302
    {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Can not set multimode on ADC1 channel: %d", adc_obj->channel));
    }
#endif
303
}
D
Dave Hylands 已提交
304

305
STATIC void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel) {
D
Dave Hylands 已提交
306 307
    ADC_ChannelConfTypeDef sConfig;

308
    sConfig.Channel = channel;
D
Dave Hylands 已提交
309
    sConfig.Rank = 1;
310
#if defined(STM32F0)
311
    sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
312
#elif defined(STM32F4) || defined(STM32F7)
D
Dave Hylands 已提交
313
    sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
314
#elif defined(STM32H7)
315 316 317 318 319 320 321
    if (channel == ADC_CHANNEL_VREFINT
        || channel == ADC_CHANNEL_TEMPSENSOR
        || channel == ADC_CHANNEL_VBAT) {
        sConfig.SamplingTime = ADC_SAMPLETIME_387CYCLES_5;
    } else {
        sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
    }
322 323 324 325
    sConfig.SingleDiff = ADC_SINGLE_ENDED;
    sConfig.OffsetNumber = ADC_OFFSET_NONE;
    sConfig.OffsetRightShift = DISABLE;
    sConfig.OffsetSignedSaturation = DISABLE;
326
#elif defined(STM32L4)
327 328 329 330 331 332 333
    if (channel == ADC_CHANNEL_VREFINT
        || channel == ADC_CHANNEL_TEMPSENSOR
        || channel == ADC_CHANNEL_VBAT) {
        sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
    } else {
        sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
    }
334 335
    sConfig.SingleDiff = ADC_SINGLE_ENDED;
    sConfig.OffsetNumber = ADC_OFFSET_NONE;
336
    sConfig.Offset = 0;
337 338 339
#else
    #error Unsupported processor
#endif
D
Dave Hylands 已提交
340

341 342 343 344 345 346
    #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

347
    HAL_ADC_ConfigChannel(adc_handle, &sConfig);
D
Dave Hylands 已提交
348 349
}

350
STATIC uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
D
Dave Hylands 已提交
351
    HAL_ADC_Start(adcHandle);
352 353
    adc_wait_for_eoc_or_timeout(EOC_TIMEOUT);
    uint32_t value = ADCx->DR;
D
Dave Hylands 已提交
354
    HAL_ADC_Stop(adcHandle);
355 356
    return value;
}
D
Dave Hylands 已提交
357

358 359
STATIC uint32_t adc_config_and_read_channel(ADC_HandleTypeDef *adcHandle, uint32_t channel) {
    adc_config_channel(adcHandle, channel);
360 361 362 363 364 365 366 367 368 369 370 371 372 373
    uint32_t raw_value = adc_read_channel(adcHandle);

    #if defined(STM32F4) || defined(STM32F7)
    // 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.
    if (channel == ADC_CHANNEL_VBAT) {
        ADC->CCR &= ~ADC_CCR_VBATE;
    }
    #endif

    return raw_value;
D
Dave Hylands 已提交
374 375 376
}

/******************************************************************************/
377
/* MicroPython bindings : adc object (single channel)                         */
D
Dave Hylands 已提交
378

379
STATIC void adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
380
    pyb_obj_adc_t *self = MP_OBJ_TO_PTR(self_in);
381 382
    mp_print_str(print, "<ADC on ");
    mp_obj_print_helper(print, self->pin_name, PRINT_STR);
383
    mp_printf(print, " channel=%u>", self->channel);
D
Dave Hylands 已提交
384 385
}

386 387 388
/// \classmethod \constructor(pin)
/// Create an ADC object associated with the given pin.
/// This allows you to then read analog values on that pin.
389
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) {
390
    // check number of arguments
D
Damien George 已提交
391
    mp_arg_check_num(n_args, n_kw, 1, 1, false);
D
Dave Hylands 已提交
392

393 394
    // 1st argument is the pin name
    mp_obj_t pin_obj = args[0];
D
Dave Hylands 已提交
395 396 397

    uint32_t channel;

398
    if (mp_obj_is_int(pin_obj)) {
399
        channel = adc_get_internal_channel(mp_obj_get_int(pin_obj));
D
Dave Hylands 已提交
400
    } else {
401
        const pin_obj_t *pin = pin_find(pin_obj);
402
        if ((pin->adc_num & PIN_ADC_MASK) == 0) {
D
Dave Hylands 已提交
403
            // No ADC1 function on that pin
404
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %q does not have ADC capabilities", pin->name));
D
Dave Hylands 已提交
405 406 407 408
        }
        channel = pin->adc_channel;
    }

409
    if (!is_adcx_channel(channel)) {
410
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "not a valid ADC Channel: %d", channel));
D
Dave Hylands 已提交
411
    }
412 413 414 415


    if (ADC_FIRST_GPIO_CHANNEL <= channel && channel <= ADC_LAST_GPIO_CHANNEL) {
        // these channels correspond to physical GPIO ports so make sure they exist
416
        if (pin_adc_table[channel] == NULL) {
417 418 419
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
                "channel %d not available on this board", channel));
        }
D
Dave Hylands 已提交
420 421 422 423
    }

    pyb_obj_adc_t *o = m_new_obj(pyb_obj_adc_t);
    memset(o, 0, sizeof(*o));
424
    o->base.type = &pyb_adc_type;
D
Dave Hylands 已提交
425 426 427 428
    o->pin_name = pin_obj;
    o->channel = channel;
    adc_init_single(o);

429
    return MP_OBJ_FROM_PTR(o);
D
Dave Hylands 已提交
430 431
}

432 433 434
/// \method read()
/// Read the value on the analog pin and return it.  The returned value
/// will be between 0 and 4095.
435
STATIC mp_obj_t adc_read(mp_obj_t self_in) {
436
    pyb_obj_adc_t *self = MP_OBJ_TO_PTR(self_in);
437
    return mp_obj_new_int(adc_config_and_read_channel(&self->handle, self->channel));
438 439 440
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);

441
/// \method read_timed(buf, timer)
442
///
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
/// 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:
466 467 468 469 470 471 472 473 474
///
///     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.
475
STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) {
476
    pyb_obj_adc_t *self = MP_OBJ_TO_PTR(self_in);
477

478 479
    mp_buffer_info_t bufinfo;
    mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
480
    size_t typesize = mp_binary_get_size('@', bufinfo.typecode, NULL);
481

482 483 484 485 486 487 488 489 490 491 492 493
    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);
    }
494

495
    // configure the ADC channel
496
    adc_config_channel(&self->handle, self->channel);
497 498 499 500

    // This uses the timer in polling mode to do the sampling
    // TODO use DMA

501 502
    uint nelems = bufinfo.len / typesize;
    for (uint index = 0; index < nelems; index++) {
503
        // Wait for the timer to trigger so we sample at the correct frequency
504
        while (__HAL_TIM_GET_FLAG(tim, TIM_FLAG_UPDATE) == RESET) {
505
        }
506
        __HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE);
507 508 509 510 511 512

        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
513
#if defined(STM32F4) || defined(STM32F7)
514
            ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
515
#elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4)
516 517 518 519
            SET_BIT(ADCx->CR, ADC_CR_ADSTART);
#else
            #error Unsupported processor
#endif
520 521 522
        }

        // wait for sample to complete
523
        adc_wait_for_eoc_or_timeout(EOC_TIMEOUT);
524 525 526 527 528

        // read value
        uint value = ADCx->DR;

        // store value in buffer
529 530 531 532
        if (typesize == 1) {
            value >>= 4;
        }
        mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value);
533 534
    }

535 536 537
    // turn the ADC off
    HAL_ADC_Stop(&self->handle);

538 539 540 541 542 543
    #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
544 545 546 547 548

    return mp_obj_new_int(bufinfo.len);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(adc_read_timed_obj, adc_read_timed);

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
// 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) {
        mp_raise_ValueError("need at least 1 ADC");
    }
    if (nadcs != nbufs) {
        mp_raise_ValueError("length of ADC and buffer lists differ");
    }

    // 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);
574
    void *bufptrs[nbufs];
575 576 577 578 579 580
    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)) {
            mp_raise_ValueError("size and type of buffers must match");
        }
581
        bufptrs[array_index] = bufinfo_curr.buf;
582 583 584 585 586 587 588
    }

    // 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
589
    pyb_obj_adc_t *adc0 = MP_OBJ_TO_PTR(adc_array[0]);
590 591 592
    adc_config_channel(&adc0->handle, adc0->channel);
    HAL_ADC_Start(&adc0->handle);
    // Wait for sample to complete and discard
593
    adc_wait_for_eoc_or_timeout(EOC_TIMEOUT);
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    // Read (and discard) value
    uint value = ADCx->DR;

    // 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++) {
618
            pyb_obj_adc_t *adc = MP_OBJ_TO_PTR(adc_array[array_index]);
619 620 621 622 623 624
            // 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
            #if defined(STM32F4) || defined(STM32F7)
            ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
625
            #elif defined(STM32F0) || defined(STM32H7) || defined(STM32L4)
626 627 628 629 630
            SET_BIT(ADCx->CR, ADC_CR_ADSTART);
            #else
            #error Unsupported processor
            #endif
            // wait for sample to complete
631
            adc_wait_for_eoc_or_timeout(EOC_TIMEOUT);
632 633 634 635 636 637 638 639

            // read value
            value = ADCx->DR;

            // store values in buffer
            if (typesize == 1) {
                value >>= 4;
            }
640
            mp_binary_set_val_array_from_int(bufinfo.typecode, bufptrs[array_index], elem_index, value);
641 642 643 644
        }
    }

    // Turn the ADC off
645
    adc0 = MP_OBJ_TO_PTR(adc_array[0]);
646 647 648 649 650 651 652
    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));

653 654 655
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) },
656
    { MP_ROM_QSTR(MP_QSTR_read_timed_multi), MP_ROM_PTR(&adc_read_timed_multi_obj) },
657 658
};

659 660
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);

661 662 663 664 665
const mp_obj_type_t pyb_adc_type = {
    { &mp_type_type },
    .name = MP_QSTR_ADC,
    .print = adc_print,
    .make_new = adc_make_new,
666
    .locals_dict = (mp_obj_dict_t*)&adc_locals_dict,
667
};
D
Dave Hylands 已提交
668 669 670 671

/******************************************************************************/
/* adc all object                                                             */

672
typedef struct _pyb_adc_all_obj_t {
D
Dave Hylands 已提交
673 674
    mp_obj_base_t base;
    ADC_HandleTypeDef handle;
675
} pyb_adc_all_obj_t;
D
Dave Hylands 已提交
676

677
void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution, uint32_t en_mask) {
D
Dave Hylands 已提交
678 679

    switch (resolution) {
680
        #if !defined(STM32H7)
681
        case 6:  resolution = ADC_RESOLUTION_6B;  break;
682
        #endif
683 684 685
        case 8:  resolution = ADC_RESOLUTION_8B;  break;
        case 10: resolution = ADC_RESOLUTION_10B; break;
        case 12: resolution = ADC_RESOLUTION_12B; break;
686 687 688
        #if defined(STM32H7)
        case 16: resolution = ADC_RESOLUTION_16B; break;
        #endif
D
Dave Hylands 已提交
689
        default:
690
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
D
Dave Hylands 已提交
691 692 693
                "resolution %d not supported", resolution));
    }

694
    for (uint32_t channel = ADC_FIRST_GPIO_CHANNEL; channel <= ADC_LAST_GPIO_CHANNEL; ++channel) {
695 696 697 698
        // only initialise those channels that are selected with the en_mask
        if (en_mask & (1 << channel)) {
            // Channels 0-16 correspond to real pins. Configure the GPIO pin in
            // ADC mode.
699
            const pin_obj_t *pin = pin_adc_table[channel];
700
            if (pin) {
701
                mp_hal_pin_config(pin, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0);
702 703
            }
        }
D
Dave Hylands 已提交
704 705
    }

706
    adcx_init_periph(&adc_all->handle, resolution);
D
Dave Hylands 已提交
707 708
}

D
Dave Hylands 已提交
709
int adc_get_resolution(ADC_HandleTypeDef *adcHandle) {
710
    uint32_t res_reg = ADC_GET_RESOLUTION(adcHandle);
D
Dave Hylands 已提交
711 712

    switch (res_reg) {
713
       #if !defined(STM32H7)
714
        case ADC_RESOLUTION_6B:  return 6;
715
        #endif
716 717
        case ADC_RESOLUTION_8B:  return 8;
        case ADC_RESOLUTION_10B: return 10;
718 719 720
        #if defined(STM32H7)
        case ADC_RESOLUTION_16B: return 16;
        #endif
D
Dave Hylands 已提交
721 722 723 724
    }
    return 12;
}

725 726 727 728 729
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));
}
D
Dave Hylands 已提交
730

731 732
int adc_read_core_temp(ADC_HandleTypeDef *adcHandle) {
    int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR);
D
Dave Hylands 已提交
733 734 735
    return ((raw_value - CORE_TEMP_V25) / CORE_TEMP_AVG_SLOPE) + 25;
}

D
Dave Hylands 已提交
736
#if MICROPY_PY_BUILTINS_FLOAT
737 738 739
// correction factor for reference value
STATIC volatile float adc_refcor = 1.0f;

740
float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
741
    int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR);
742 743 744 745
    float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 80.0;
    return (((float)raw_value * adc_refcor - *ADC_CAL1) / core_temp_avg_slope) + 30.0f;
}

746
float adc_read_core_vbat(ADC_HandleTypeDef *adcHandle) {
747
    uint32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_VBAT);
748
    return raw_value * VBAT_DIV * ADC_SCALE * adc_refcor;
D
Dave Hylands 已提交
749 750
}

751
float adc_read_core_vref(ADC_HandleTypeDef *adcHandle) {
752
    uint32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_VREFINT);
D
Dave Hylands 已提交
753

754 755 756 757
    // update the reference correction factor
    adc_refcor = ((float)(*VREFIN_CAL)) / ((float)raw_value);

    return (*VREFIN_CAL) * ADC_SCALE;
D
Dave Hylands 已提交
758
}
D
Dave Hylands 已提交
759
#endif
D
Dave Hylands 已提交
760 761

/******************************************************************************/
762
/* MicroPython bindings : adc_all object                                      */
D
Dave Hylands 已提交
763

764
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) {
765
    // check number of arguments
766
    mp_arg_check_num(n_args, n_kw, 1, 2, false);
767 768 769 770

    // make ADCAll object
    pyb_adc_all_obj_t *o = m_new_obj(pyb_adc_all_obj_t);
    o->base.type = &pyb_adc_all_type;
771 772 773 774 775 776
    mp_int_t res = mp_obj_get_int(args[0]);
    uint32_t en_mask = 0xffffffff;
    if (n_args > 1) {
        en_mask =  mp_obj_get_int(args[1]);
    }
    adc_init_all(o, res, en_mask);
777

778
    return MP_OBJ_FROM_PTR(o);
D
Dave Hylands 已提交
779 780
}

781
STATIC mp_obj_t adc_all_read_channel(mp_obj_t self_in, mp_obj_t channel) {
782
    pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
783
    uint32_t chan = adc_get_internal_channel(mp_obj_get_int(channel));
D
Dave Hylands 已提交
784 785 786
    uint32_t data = adc_config_and_read_channel(&self->handle, chan);
    return mp_obj_new_int(data);
}
787
STATIC MP_DEFINE_CONST_FUN_OBJ_2(adc_all_read_channel_obj, adc_all_read_channel);
D
Dave Hylands 已提交
788

789
STATIC mp_obj_t adc_all_read_core_temp(mp_obj_t self_in) {
790
    pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
791 792 793 794
    #if MICROPY_PY_BUILTINS_FLOAT
    float data = adc_read_core_temp_float(&self->handle);
    return mp_obj_new_float(data);
    #else
D
Dave Hylands 已提交
795 796
    int data  = adc_read_core_temp(&self->handle);
    return mp_obj_new_int(data);
797
    #endif
D
Dave Hylands 已提交
798
}
799
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_temp_obj, adc_all_read_core_temp);
D
Dave Hylands 已提交
800

D
Dave Hylands 已提交
801
#if MICROPY_PY_BUILTINS_FLOAT
802
STATIC mp_obj_t adc_all_read_core_vbat(mp_obj_t self_in) {
803
    pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
D
Dave Hylands 已提交
804 805 806
    float data = adc_read_core_vbat(&self->handle);
    return mp_obj_new_float(data);
}
807
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_vbat_obj, adc_all_read_core_vbat);
D
Dave Hylands 已提交
808

809
STATIC mp_obj_t adc_all_read_core_vref(mp_obj_t self_in) {
810
    pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
D
Dave Hylands 已提交
811 812 813
    float data  = adc_read_core_vref(&self->handle);
    return mp_obj_new_float(data);
}
814
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_vref_obj, adc_all_read_core_vref);
815 816

STATIC mp_obj_t adc_all_read_vref(mp_obj_t self_in) {
817
    pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in);
818
    adc_read_core_vref(&self->handle);
819
    return mp_obj_new_float(ADC_SCALE_V * adc_refcor);
820 821
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_vref_obj, adc_all_read_vref);
D
Dave Hylands 已提交
822
#endif
D
Dave Hylands 已提交
823

824 825 826
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) },
D
Dave Hylands 已提交
827
#if MICROPY_PY_BUILTINS_FLOAT
828 829 830
    { 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) },
D
Dave Hylands 已提交
831
#endif
D
Dave Hylands 已提交
832 833
};

834 835
STATIC MP_DEFINE_CONST_DICT(adc_all_locals_dict, adc_all_locals_dict_table);

836
const mp_obj_type_t pyb_adc_all_type = {
D
Dave Hylands 已提交
837
    { &mp_type_type },
838 839
    .name = MP_QSTR_ADCAll,
    .make_new = adc_all_make_new,
840
    .locals_dict = (mp_obj_dict_t*)&adc_all_locals_dict,
D
Dave Hylands 已提交
841
};
842 843

#endif // MICROPY_HW_ENABLE_ADC