2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* 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 17:49:23 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 21:06:20 +00:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2014-03-24 17:49:23 +00:00
|
|
|
#include "adc.h"
|
|
|
|
#include "pin.h"
|
2014-04-17 18:03:27 +01:00
|
|
|
#include "genhdr/pins.h"
|
2014-04-15 19:52:56 +01:00
|
|
|
#include "timer.h"
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-04-29 22:55:34 +01: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 17:49:23 +00:00
|
|
|
|
|
|
|
/* ADC defintions */
|
|
|
|
#define ADCx (ADC1)
|
|
|
|
#define ADCx_CLK_ENABLE __ADC1_CLK_ENABLE
|
|
|
|
#define ADC_NUM_CHANNELS (19)
|
|
|
|
#define ADC_NUM_GPIO_CHANNELS (16)
|
|
|
|
|
|
|
|
#if defined(STM32F405xx) || defined(STM32F415xx) || \
|
|
|
|
defined(STM32F407xx) || defined(STM32F417xx) || \
|
2015-04-18 22:15:59 +01:00
|
|
|
defined(STM32F401xC) || defined(STM32F401xE) || \
|
|
|
|
defined(STM32F411xE)
|
2014-03-24 17:49:23 +00:00
|
|
|
#define VBAT_DIV (2)
|
|
|
|
#elif defined(STM32F427xx) || defined(STM32F429xx) || \
|
2015-07-28 19:13:33 +01:00
|
|
|
defined(STM32F437xx) || defined(STM32F439xx) || \
|
|
|
|
defined(STM32F746xx)
|
2014-03-24 17:49:23 +00:00
|
|
|
#define VBAT_DIV (4)
|
2015-07-28 19:13:33 +01:00
|
|
|
#else
|
|
|
|
#error Unsupported processor
|
2014-03-24 17:49:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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) */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-06-16 17:41:58 +01:00
|
|
|
STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
|
2014-03-24 17:49:23 +00:00
|
|
|
if (!IS_ADC_CHANNEL(adc_obj->channel)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adc_obj->channel < ADC_NUM_GPIO_CHANNELS) {
|
|
|
|
// Channels 0-16 correspond to real pins. Configure the GPIO pin in
|
|
|
|
// ADC mode.
|
|
|
|
const pin_obj_t *pin = pin_adc1[adc_obj->channel];
|
2015-08-03 00:05:16 +01:00
|
|
|
mp_hal_gpio_clock_enable(pin->gpio);
|
2014-03-24 17:49:23 +00:00
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.Pin = pin->pin_mask;
|
|
|
|
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
|
|
|
|
GPIO_InitStructure.Pull = GPIO_NOPULL;
|
|
|
|
HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADCx_CLK_ENABLE();
|
|
|
|
|
|
|
|
ADC_HandleTypeDef *adcHandle = &adc_obj->handle;
|
|
|
|
adcHandle->Instance = ADCx;
|
|
|
|
adcHandle->Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
|
|
|
adcHandle->Init.Resolution = ADC_RESOLUTION12b;
|
|
|
|
adcHandle->Init.ScanConvMode = DISABLE;
|
|
|
|
adcHandle->Init.ContinuousConvMode = DISABLE;
|
|
|
|
adcHandle->Init.DiscontinuousConvMode = DISABLE;
|
|
|
|
adcHandle->Init.NbrOfDiscConversion = 0;
|
|
|
|
adcHandle->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
|
|
|
adcHandle->Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
|
|
|
adcHandle->Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
|
|
adcHandle->Init.NbrOfConversion = 1;
|
|
|
|
adcHandle->Init.DMAContinuousRequests = DISABLE;
|
|
|
|
adcHandle->Init.EOCSelection = DISABLE;
|
|
|
|
|
|
|
|
HAL_ADC_Init(adcHandle);
|
2014-06-16 17:41:58 +01:00
|
|
|
}
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-06-16 17:41:58 +01:00
|
|
|
STATIC void adc_config_channel(pyb_obj_adc_t *adc_obj) {
|
2014-03-24 17:49:23 +00:00
|
|
|
ADC_ChannelConfTypeDef sConfig;
|
|
|
|
|
|
|
|
sConfig.Channel = adc_obj->channel;
|
|
|
|
sConfig.Rank = 1;
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
|
|
|
|
sConfig.Offset = 0;
|
|
|
|
|
2014-06-16 17:41:58 +01:00
|
|
|
HAL_ADC_ConfigChannel(&adc_obj->handle, &sConfig);
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 17:41:58 +01:00
|
|
|
STATIC uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
|
2014-03-24 17:49:23 +00:00
|
|
|
uint32_t rawValue = 0;
|
|
|
|
|
|
|
|
HAL_ADC_Start(adcHandle);
|
2014-03-24 19:27:13 +00:00
|
|
|
if (HAL_ADC_PollForConversion(adcHandle, 10) == HAL_OK && HAL_ADC_GetState(adcHandle) == HAL_ADC_STATE_EOC_REG) {
|
2014-03-24 17:49:23 +00:00
|
|
|
rawValue = HAL_ADC_GetValue(adcHandle);
|
|
|
|
}
|
|
|
|
HAL_ADC_Stop(adcHandle);
|
|
|
|
|
|
|
|
return rawValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings : adc object (single channel) */
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-03-24 17:49:23 +00:00
|
|
|
pyb_obj_adc_t *self = self_in;
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_print_str(print, "<ADC on ");
|
|
|
|
mp_obj_print_helper(print, self->pin_name, PRINT_STR);
|
|
|
|
mp_printf(print, " channel=%lu>", self->channel);
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \classmethod \constructor(pin)
|
|
|
|
/// Create an ADC object associated with the given pin.
|
|
|
|
/// This allows you to then read analog values on that pin.
|
2014-08-30 00:35:11 +01:00
|
|
|
STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2014-03-24 19:27:13 +00:00
|
|
|
// check number of arguments
|
2014-04-20 00:16:30 +01:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
// 1st argument is the pin name
|
|
|
|
mp_obj_t pin_obj = args[0];
|
2014-03-24 17:49:23 +00:00
|
|
|
|
|
|
|
uint32_t channel;
|
|
|
|
|
|
|
|
if (MP_OBJ_IS_INT(pin_obj)) {
|
|
|
|
channel = mp_obj_get_int(pin_obj);
|
|
|
|
} else {
|
2014-04-18 22:38:09 +01:00
|
|
|
const pin_obj_t *pin = pin_find(pin_obj);
|
2014-03-24 17:49:23 +00:00
|
|
|
if ((pin->adc_num & PIN_ADC1) == 0) {
|
|
|
|
// No ADC1 function on that pin
|
2015-04-16 18:40:06 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %q does not have ADC capabilities", pin->name));
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
|
|
|
channel = pin->adc_channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_ADC_CHANNEL(channel)) {
|
2014-04-18 22:38:09 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "not a valid ADC Channel: %d", channel));
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
|
|
|
if (pin_adc1[channel] == NULL) {
|
2014-04-18 22:38:09 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "channel %d not available on this board", channel));
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pyb_obj_adc_t *o = m_new_obj(pyb_obj_adc_t);
|
|
|
|
memset(o, 0, sizeof(*o));
|
2014-03-24 19:27:13 +00:00
|
|
|
o->base.type = &pyb_adc_type;
|
2014-03-24 17:49:23 +00:00
|
|
|
o->pin_name = pin_obj;
|
|
|
|
o->channel = channel;
|
|
|
|
adc_init_single(o);
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-04-29 22:55:34 +01: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 19:27:13 +00:00
|
|
|
STATIC mp_obj_t adc_read(mp_obj_t self_in) {
|
|
|
|
pyb_obj_adc_t *self = self_in;
|
|
|
|
|
2014-06-16 17:41:58 +01:00
|
|
|
adc_config_channel(self);
|
2014-03-24 19:27:13 +00:00
|
|
|
uint32_t data = adc_read_channel(&self->handle);
|
|
|
|
return mp_obj_new_int(data);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
|
|
|
|
|
2015-07-22 19:37:21 +01:00
|
|
|
/// \method read_timed(buf, timer)
|
2014-04-29 22:55:34 +01:00
|
|
|
///
|
2015-07-22 19:37:21 +01: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 22:55:34 +01: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 19:52:56 +01:00
|
|
|
STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) {
|
|
|
|
pyb_obj_adc_t *self = self_in;
|
|
|
|
|
2014-04-18 23:28:56 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
|
2015-05-10 12:04:38 +01:00
|
|
|
size_t typesize = mp_binary_get_size('@', bufinfo.typecode, NULL);
|
2014-04-15 19:52:56 +01:00
|
|
|
|
2015-07-22 19:37:21 +01: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 19:52:56 +01:00
|
|
|
|
2015-03-23 22:36:51 +00:00
|
|
|
// configure the ADC channel
|
2014-06-16 17:41:58 +01:00
|
|
|
adc_config_channel(self);
|
2015-03-23 22:36:51 +00:00
|
|
|
|
|
|
|
// This uses the timer in polling mode to do the sampling
|
|
|
|
// TODO use DMA
|
|
|
|
|
2015-03-13 22:11:50 +00:00
|
|
|
uint nelems = bufinfo.len / typesize;
|
|
|
|
for (uint index = 0; index < nelems; index++) {
|
2015-03-23 22:36:51 +00:00
|
|
|
// Wait for the timer to trigger so we sample at the correct frequency
|
2015-07-22 19:37:21 +01:00
|
|
|
while (__HAL_TIM_GET_FLAG(tim, TIM_FLAG_UPDATE) == RESET) {
|
2014-04-15 19:52:56 +01:00
|
|
|
}
|
2015-07-22 19:37:21 +01:00
|
|
|
__HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE);
|
2015-03-23 22:36:51 +00: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
|
|
|
|
ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for sample to complete
|
|
|
|
uint32_t tickstart = HAL_GetTick();
|
|
|
|
while ((ADCx->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) {
|
|
|
|
#define READ_TIMED_TIMEOUT (10) // in ms
|
|
|
|
if (((HAL_GetTick() - tickstart ) > READ_TIMED_TIMEOUT)) {
|
|
|
|
break; // timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read value
|
|
|
|
uint value = ADCx->DR;
|
|
|
|
|
|
|
|
// store value in buffer
|
2014-04-18 23:28:56 +01:00
|
|
|
if (typesize == 1) {
|
|
|
|
value >>= 4;
|
|
|
|
}
|
|
|
|
mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value);
|
2014-04-15 19:52:56 +01:00
|
|
|
}
|
|
|
|
|
2015-03-23 22:36:51 +00:00
|
|
|
// turn the ADC off
|
|
|
|
HAL_ADC_Stop(&self->handle);
|
|
|
|
|
2015-07-22 19:37:21 +01: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 19:52:56 +01:00
|
|
|
|
|
|
|
return mp_obj_new_int(bufinfo.len);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(adc_read_timed_obj, adc_read_timed);
|
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC const mp_map_elem_t adc_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj},
|
2014-04-15 19:52:56 +01:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read_timed), (mp_obj_t)&adc_read_timed_obj},
|
2014-03-24 19:27:13 +00:00
|
|
|
};
|
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);
|
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
const mp_obj_type_t pyb_adc_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ADC,
|
|
|
|
.print = adc_print,
|
|
|
|
.make_new = adc_make_new,
|
2014-03-26 21:47:19 +00:00
|
|
|
.locals_dict = (mp_obj_t)&adc_locals_dict,
|
2014-03-24 19:27:13 +00:00
|
|
|
};
|
2014-03-24 17:49:23 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* adc all object */
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
typedef struct _pyb_adc_all_obj_t {
|
2014-03-24 17:49:23 +00:00
|
|
|
mp_obj_base_t base;
|
|
|
|
ADC_HandleTypeDef handle;
|
2014-04-18 22:38:09 +01:00
|
|
|
} pyb_adc_all_obj_t;
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution) {
|
2014-03-24 17:49:23 +00:00
|
|
|
|
|
|
|
switch (resolution) {
|
2014-03-25 06:23:09 +00:00
|
|
|
case 6: resolution = ADC_RESOLUTION6b; break;
|
|
|
|
case 8: resolution = ADC_RESOLUTION8b; break;
|
|
|
|
case 10: resolution = ADC_RESOLUTION10b; break;
|
|
|
|
case 12: resolution = ADC_RESOLUTION12b; break;
|
2014-03-24 17:49:23 +00:00
|
|
|
default:
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
2014-03-24 17:49:23 +00:00
|
|
|
"resolution %d not supported", resolution));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t channel = 0; channel < ADC_NUM_GPIO_CHANNELS; channel++) {
|
|
|
|
// Channels 0-16 correspond to real pins. Configure the GPIO pin in
|
|
|
|
// ADC mode.
|
|
|
|
const pin_obj_t *pin = pin_adc1[channel];
|
2015-08-03 00:05:16 +01:00
|
|
|
mp_hal_gpio_clock_enable(pin->gpio);
|
2014-03-24 17:49:23 +00:00
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.Pin = pin->pin_mask;
|
|
|
|
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
|
|
|
|
GPIO_InitStructure.Pull = GPIO_NOPULL;
|
|
|
|
HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADCx_CLK_ENABLE();
|
|
|
|
|
|
|
|
ADC_HandleTypeDef *adcHandle = &adc_all->handle;
|
|
|
|
adcHandle->Instance = ADCx;
|
|
|
|
adcHandle->Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
|
|
|
adcHandle->Init.Resolution = resolution;
|
|
|
|
adcHandle->Init.ScanConvMode = DISABLE;
|
|
|
|
adcHandle->Init.ContinuousConvMode = DISABLE;
|
|
|
|
adcHandle->Init.DiscontinuousConvMode = DISABLE;
|
|
|
|
adcHandle->Init.NbrOfDiscConversion = 0;
|
|
|
|
adcHandle->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
|
|
|
adcHandle->Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
|
|
|
adcHandle->Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
|
|
adcHandle->Init.NbrOfConversion = 1;
|
|
|
|
adcHandle->Init.DMAContinuousRequests = DISABLE;
|
|
|
|
adcHandle->Init.EOCSelection = DISABLE;
|
|
|
|
|
|
|
|
HAL_ADC_Init(adcHandle);
|
|
|
|
}
|
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
uint32_t adc_config_and_read_channel(ADC_HandleTypeDef *adcHandle, uint32_t channel) {
|
2014-03-24 17:49:23 +00:00
|
|
|
ADC_ChannelConfTypeDef sConfig;
|
|
|
|
sConfig.Channel = channel;
|
|
|
|
sConfig.Rank = 1;
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
|
|
|
|
sConfig.Offset = 0;
|
|
|
|
HAL_ADC_ConfigChannel(adcHandle, &sConfig);
|
|
|
|
|
|
|
|
return adc_read_channel(adcHandle);
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:23:09 +00:00
|
|
|
int adc_get_resolution(ADC_HandleTypeDef *adcHandle) {
|
|
|
|
uint32_t res_reg = __HAL_ADC_GET_RESOLUTION(adcHandle);
|
|
|
|
|
|
|
|
switch (res_reg) {
|
|
|
|
case ADC_RESOLUTION6b: return 6;
|
|
|
|
case ADC_RESOLUTION8b: return 8;
|
|
|
|
case ADC_RESOLUTION10b: return 10;
|
|
|
|
}
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
int adc_read_core_temp(ADC_HandleTypeDef *adcHandle) {
|
2014-03-24 17:49:23 +00:00
|
|
|
int32_t raw_value = adc_config_and_read_channel(adcHandle, ADC_CHANNEL_TEMPSENSOR);
|
2014-03-25 06:23:09 +00:00
|
|
|
|
|
|
|
// Note: constants assume 12-bit resolution, so we scale the raw value to
|
|
|
|
// be 12-bits.
|
|
|
|
raw_value <<= (12 - adc_get_resolution(adcHandle));
|
|
|
|
|
2014-03-24 17:49:23 +00:00
|
|
|
return ((raw_value - CORE_TEMP_V25) / CORE_TEMP_AVG_SLOPE) + 25;
|
|
|
|
}
|
|
|
|
|
2014-09-26 17:04:05 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-24 19:27:13 +00:00
|
|
|
float adc_read_core_vbat(ADC_HandleTypeDef *adcHandle) {
|
2014-03-24 17:49:23 +00:00
|
|
|
uint32_t raw_value = adc_config_and_read_channel(adcHandle, ADC_CHANNEL_VBAT);
|
2014-03-25 06:23:09 +00:00
|
|
|
|
|
|
|
// Note: constants assume 12-bit resolution, so we scale the raw value to
|
|
|
|
// be 12-bits.
|
|
|
|
raw_value <<= (12 - adc_get_resolution(adcHandle));
|
|
|
|
|
2015-02-11 00:25:22 +00:00
|
|
|
// multiplier is 3.3/4095
|
|
|
|
return raw_value * VBAT_DIV * 0.8058608058608059e-3f;
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
float adc_read_core_vref(ADC_HandleTypeDef *adcHandle) {
|
2014-03-24 17:49:23 +00:00
|
|
|
uint32_t raw_value = adc_config_and_read_channel(adcHandle, ADC_CHANNEL_VREFINT);
|
2014-03-25 06:23:09 +00:00
|
|
|
|
|
|
|
// Note: constants assume 12-bit resolution, so we scale the raw value to
|
|
|
|
// be 12-bits.
|
|
|
|
raw_value <<= (12 - adc_get_resolution(adcHandle));
|
|
|
|
|
2015-02-11 00:25:22 +00:00
|
|
|
// multiplier is 3.3/4095
|
|
|
|
return raw_value * 0.8058608058608059e-3f;
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
2014-09-26 17:04:05 +01:00
|
|
|
#endif
|
2014-03-24 17:49:23 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings : adc_all object */
|
|
|
|
|
2014-08-30 00:35:11 +01:00
|
|
|
STATIC mp_obj_t adc_all_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2014-04-18 22:38:09 +01:00
|
|
|
// check number of arguments
|
2014-04-20 00:16:30 +01:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
2014-04-18 22:38:09 +01:00
|
|
|
|
|
|
|
// make ADCAll object
|
|
|
|
pyb_adc_all_obj_t *o = m_new_obj(pyb_adc_all_obj_t);
|
|
|
|
o->base.type = &pyb_adc_all_type;
|
|
|
|
adc_init_all(o, mp_obj_get_int(args[0])); // args[0] is the resolution
|
|
|
|
|
|
|
|
return o;
|
2014-03-24 17:49:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
STATIC mp_obj_t adc_all_read_channel(mp_obj_t self_in, mp_obj_t channel) {
|
2014-04-18 22:38:09 +01:00
|
|
|
pyb_adc_all_obj_t *self = self_in;
|
2014-03-24 17:49:23 +00:00
|
|
|
uint32_t chan = mp_obj_get_int(channel);
|
|
|
|
uint32_t data = adc_config_and_read_channel(&self->handle, chan);
|
|
|
|
return mp_obj_new_int(data);
|
|
|
|
}
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(adc_all_read_channel_obj, adc_all_read_channel);
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
STATIC mp_obj_t adc_all_read_core_temp(mp_obj_t self_in) {
|
2014-04-18 22:38:09 +01:00
|
|
|
pyb_adc_all_obj_t *self = self_in;
|
2014-03-24 17:49:23 +00:00
|
|
|
int data = adc_read_core_temp(&self->handle);
|
|
|
|
return mp_obj_new_int(data);
|
|
|
|
}
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_temp_obj, adc_all_read_core_temp);
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-09-26 17:04:05 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-24 19:27:13 +00:00
|
|
|
STATIC mp_obj_t adc_all_read_core_vbat(mp_obj_t self_in) {
|
2014-04-18 22:38:09 +01:00
|
|
|
pyb_adc_all_obj_t *self = self_in;
|
2014-03-24 17:49:23 +00:00
|
|
|
float data = adc_read_core_vbat(&self->handle);
|
|
|
|
return mp_obj_new_float(data);
|
|
|
|
}
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_vbat_obj, adc_all_read_core_vbat);
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-03-24 19:27:13 +00:00
|
|
|
STATIC mp_obj_t adc_all_read_core_vref(mp_obj_t self_in) {
|
2014-04-18 22:38:09 +01:00
|
|
|
pyb_adc_all_obj_t *self = self_in;
|
2014-03-24 17:49:23 +00:00
|
|
|
float data = adc_read_core_vref(&self->handle);
|
|
|
|
return mp_obj_new_float(data);
|
|
|
|
}
|
2014-03-24 19:27:13 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_all_read_core_vref_obj, adc_all_read_core_vref);
|
2014-09-26 17:04:05 +01:00
|
|
|
#endif
|
2014-03-24 17:49:23 +00:00
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC const mp_map_elem_t adc_all_locals_dict_table[] = {
|
2014-04-18 22:38:09 +01:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read_channel), (mp_obj_t)&adc_all_read_channel_obj},
|
2014-03-26 21:47:19 +00:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read_core_temp), (mp_obj_t)&adc_all_read_core_temp_obj},
|
2014-09-26 17:04:05 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-26 21:47:19 +00:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read_core_vbat), (mp_obj_t)&adc_all_read_core_vbat_obj},
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read_core_vref), (mp_obj_t)&adc_all_read_core_vref_obj},
|
2014-09-26 17:04:05 +01:00
|
|
|
#endif
|
2014-03-24 17:49:23 +00:00
|
|
|
};
|
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(adc_all_locals_dict, adc_all_locals_dict_table);
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
const mp_obj_type_t pyb_adc_all_type = {
|
2014-03-24 17:49:23 +00:00
|
|
|
{ &mp_type_type },
|
2014-04-18 22:38:09 +01:00
|
|
|
.name = MP_QSTR_ADCAll,
|
|
|
|
.make_new = adc_all_make_new,
|
2014-03-26 21:47:19 +00:00
|
|
|
.locals_dict = (mp_obj_t)&adc_all_locals_dict,
|
2014-03-24 17:49:23 +00:00
|
|
|
};
|