mwf_periph_adc - ADC
mwf_periph_adc - ADC
This is a peripheral object that summarizes the procedures for using the Analog-to-Digital Converter (ADC).
Code Example
The following example explicitly specifies the mwf::
namespace. To omit this, please write using namespace mwf;
.
- include
#include "mwf_periph_adc.hpp"
- Initialization Procedure
// Create the the_adc class object
mwf::the_adc->global_init_adc_manager();
// Specify ADC input pins
mwf::pin::conf_adc_input(14);
mwf::pin::conf_adc_input(15);
// Initialization
mwf::the_adc->init();
- ADC Start (One-shot)
// Specify channels
mwf::the_adc->enable(
(1ul << mwf::adc::CH_0)
| (1ul << mwf::adc::CH_1)
| (1ul << mwf::adc::CH_VCC));
// Start ADC
mwf::the_adc->start(); // One-shot
while(!mwf::the_adc->available()) {} // Wait for completion by polling
// Get values
int16_t v_ch0 = mwf::the_adc->get_value_mv(mwf::adc::CH_0);
int16_t v_ch1 = mwf::the_adc->get_value_mv(mwf::adc::CH_1);
int16_t v_vcc = mwf::the_adc->get_value_mv(mwf::adc::CH_VCC);
- ADC Start (Continuous)
// Specify channels
mwf::the_adc->enable(
(1ul << mwf::adc::CH_0)
| (1ul << mwf::adc::CH_1)
| (1ul << mwf::adc::CH_VCC));
// Start ADC
mwf::the_adc->start(true); // Continuous
// Inside the application loop.
void loop() {
if (mwf::the_adc->available()) {
// Get values (to be executed periodically)
int16_t v_ch0 = mwf::the_adc->get_value_mv(mwf::adc::CH_0);
int16_t v_ch1 = mwf::the_adc->get_value_mv(mwf::adc::CH_1);
int16_t v_vcc = mwf::the_adc->get_value_mv(mwf::adc::CH_VCC);
...
}
}
- Temperature Measurement
int32_t i32temp;
int16_t i16volt;
// Executes the process of turning on the internal sensor, waiting for stabilization, and performing ADC measurement (one time only).
mwf::the_adc->temp_capture(i32temp, i16volt, 0);
// i32temp is the temperature in degrees Celsius multiplied by 128. i16volt is in millivolts.
Serial << format("%dC %dmV", i32temp >> 7, i16volt);
class mwf::periph::adc
This section describes the main definitions for the the_adc
class object.
Constant Definitions
static const uint8_t CH_MAX = 7;
static const uint8_t CH_0 = 0; // ADC0
static const uint8_t CH_1 = 1; // ADC1
static const uint8_t CH_2 = 2; // ADC2
static const uint8_t CH_3 = 3; // ADC3
static const uint8_t CH_4 = 4; // ADC4
static const uint8_t CH_5 = 5; // ADC5
static const uint8_t CH_VCC = 6; // VCC
static const uint8_t CH_TEMP = 7; // Internal temperature sensor (the acquisition method differs from normal ADCs)
These are the configuration definitions for the ADC channels.
struct config
struct config {
uint8_t prescale;
};
This is a structure for setting configurations. It is passed as a parameter to init()
.
prescale
: (Current version only supportsDEFAULT_PRESCALE=6
) A prescale value that determines the ADC conversion time. It sets(1ul << .prescale)
toadc_config_t::clockDividerNumber
defined in the fsl library and calls::ADC_Init()
.
global_init_adc_manager()
global_deinit_adc_manager()
static void global_init_adc_manager();
static void global_deinit_adc_manager();
These functions create and destroy the the_adc
class object.
set_pin_as_adc()
static void set_pin_as_adc(uint8_t pin)
This function sets the specified pin number pin
as an ADC input.
init()
deinit()
void init(bool b_wait_init = true);
void deinit();
Initializes the ADC.
Setting b_wait_init
to FALSE
omits the ADC stabilization wait time (300ms). For details on handling the wait time, please refer to is_periph_enabled()
.
To initialize or re-initialize the ADC for internal temperature sensor acquisition, call init_for_temp_volt()
.
is_periph_enabled()
force_periph_enabled()
get_init_freerun_tick()
bool is_periph_enabled()
void force_periph_enabled()
uint32_t get_init_freerun_tick()
is_periph_enabled()
returns true
when the ADC has been initialized and the necessary waiting period has elapsed.
- If the FRWT (Free Running Wake Timer) provided by
the_wtimer
is running,is_periph_enabled()
will returnfalse
until the appropriate time has passed. You can get the tick count value at the timeinit()
was called by callingget_init_freerun_tick()
. - If the FRWT is not running, the first call to
is_periph_enabled()
will incur a wait time of approximately 300 microseconds. To avoid this waiting process, you can callforce_periph_enabled()
immediately after callinginit()
. This forces the internal state to be treated as if the waiting period has already passed.
enable()
void enable(uint32_t chmask);
This function sets the ADC to an operational state. chmask
is a bitmask of the channels to be converted. For example, if you want to target ADC0, ADC1, and VCC, you would specify (1ul << CH_0) | (1ul << CH_1) | (1ul << CH_VCC)
.
start()
stop()
void start(bool b_cont = false);
void stop();
After calling enable()
, you can start the ADC by calling start()
. If b_cont
is set to true
, it will perform continuous conversion. Do not call start()
if the ADC is already running.
To stop the conversion during continuous mode, call stop()
.
When the conversion is complete, a read of the_adc->available()
will return true
.
available()
bool available();
Returns true
after the conversion is complete. After true
is read, it returns false
again.
is_started()
bool is_started();
Returns true
if the ADC is currently running due to a call to start()
.
get_value()
uint16_t get_value(uint8_t ch);
Gets the 12-bit AD converted value for the channel specified by ch
. Call this after the AD conversion is complete.
get_value_mv()
int16_t get_value_mv(uint8_t ch);
Gets the AD converted value in millivolts (mv) for the channel specified by ch
. Call this after the AD conversion is complete.
global_init_device()
global_deinit_device()
static void global_init_device();
static void global_deinit_device();
Performs the procedures for hardware initialization and termination of use.
Note: This is called internally from constructors, destructors, etc., and does not need to be explicitly called from the user application.
register_callback()
typedef void (*PFN_ADC_CALLBACK)(uint32_t, uint32_t);
void register_callback(PFN_ADC_CALLBACK pfn)
Specifies a callback function from within an interrupt handler.
The first parameter of the callback function is kADC_ConvSeqAInterruptFlag
, and the second parameter is a bitmask of the channels that were converted.
Temperature Sensor
temp_capture()
bool temp_capture(
int32_t& temp128th,
int16_t& volt_mv,
uint8_t times_adc_scaler = 0,
bool b_power_on_temp_sensor = true)
Acquires the value of the on-chip temperature sensor. It also secondarily measures the supply voltage.
temp128th
: Specifies the variable to store the temperature measurement result. The value is 128 times the value in degrees Celsius. The integer part can be calculated withtemp128th >> 7
, and the first decimal place with(10 * temp128th) >> 7
.volt_mv
: Specifies the variable to store the voltage measurement result. The value is in millivolts [mV].times_adc_scaler
: Specifies a scaler value corresponding to the number of ADC repetitions. A value from 0 to 3 can be specified; 0 performs 1 AD conversion, 1 performs 2, 2 performs 4, and 3 performs 8, after which the values are averaged.- The return value is
true
on success andfalse
on failure.
The following processes are performed implicitly:
- If the temperature sensor is not ON, it is set to ON and the necessary waiting process is performed (see
temp_power_on()
). - The temperature sensor is turned OFF after execution.
- It will fail if the ADC has not been initialized (
init()
). - If the device is not available after ADC initialization, it performs a waiting process (see
is_periph_enabled()
).
temp_get_capt_tick()
uint32_t temp_get_capt_tick()
This function returns the FRWT counter value from the last time the temperature was acquired.
temp_power_on()
, temp_power_off()
void temp_power_on()
void temp_power_off()
These functions explicitly turn the temperature sensor ON/OFF.
If FRWT is enabled, you can shorten the waiting time by calling temp_power_on()
in advance, as the function determines if the wait is complete based on the counter value.
temp_computation()
int32_t temp_computation(
uint16_t adcout_vbat_lsb_sum8,
uint16_t tsens_adcout_T_sum8,
uint8_t nb_samples_actual = 1)
This is for internal use. It calculates the temperature from the ADC measurement values.
get_ctrl0_adc_reg_context()
class ctrl0_adc_reg;
ctrl0_adc_reg get_ctrl0_adc_reg_context(uint8_t mode, uint8_t tsamp)
// Example
if (auto rc = get_ctrl0_adc_reg_context(
0x0
, 0x14
)) {
; // This scope enables the (0x0, 0x14) setting. The original value is restored upon exiting the scope.
}
This is for internal use. It temporarily changes the ADC configuration parameters.
class mwf::periph::adc
(sys_ev_handler
)
on_sleep()
Performs the ADC stop process.
on_wakeup()
If the ADC was initialized before sleep, it performs the initialization procedure (init()
). You must execute enable()
and start()
again.
Others
About Operation in Continuous Mode
The conversion cycle is determined by the hardware. In the current version, the prescaler value is fixed.
Usage with AHI and mwx
The mwf::the_adc
is used internally by the AHI and mwx libraries, so caution is required when using it directly.
- If you are using the AHI library and also
mwf::the_adc
, please avoid calling any ADC-related procedures directly (e.g.,vAHI_ApConfigure()
,vAHI_AdcEnable()
,vAHI_AdcStartSample()
inadc.c
that comes with App_Tweline). - If you are using the mwx library and also
mwf::the_adc
, please do not perform operations on theAnalogue
class object (e.g.,Analogue.setup()
).