ADC-Related AHI Functions and Explanations
ADC
Some parts related to AHI’s ADC (Analog-to-Digital Conversion) are implemented for the purpose of source code compatibility.
- This implementation is not intended to maintain full compatibility.
- Notes and considerations known at the time of writing are described here.
- Internal implementation details may change without notice.
Overview
The hardware specifications of the ADC vary depending on the model.
TWELITE BLUE | TWELITE RED | TWELITE GOLD | |
---|---|---|---|
Resolution (bits) | 10bit | 10bit | 12bit |
Full Scale | 2470mV | 2470mV | 3600mV |
Number of Channels (not supported by API) | 4 | 4 (2) | 4 (2) |
This library provides processing for using four channels (ADC0..3) and Vcc.
However, compatibility of conversion time or conversion data is not handled by the library; it is assumed that adjustments will be made in the application source.
For conversions up to 6 pins or for ADC combining multiple channels, the AHI library does not provide support; please use mwf::the_adc
directly.
Related: On-chip Temperature Sensor
Pins
PIO | Notes | |
---|---|---|
ADC0 | 15 | |
ADC1 | 14 | Shared with DIO8 |
ADC2 | 16 | |
ADC3 | 17 |
AHIcmpt_ADC.cpp
This library describes the differences from the AHI library for TWELITE BLUE/RED.
For API specifications, please also refer to the AHI library manual.
vAHI_ApConfigure()
void vAHI_ApConfigure(
bool_t bAPRegulator,
bool_t bIntEnable,
uint8 u8SampleSelect,
uint8 u8ClockDivRatio,
bool_t bRefSelect);
Initializes the ADC (mwf::the_adc
object construction and the_adc->init()
initialization).
- Specify
bAPRegulator
as TRUE to enable the ADC. If set to FALSE, the ADC is disabled. - Specify
bIntEnable
as TRUE to enable ADC interrupts. u8SampleSelect
andbRefSelect
are ignored.u8ClockDivRatio
is also currently not applied.- A stabilization wait process for the analog circuit using
bAHI_APRegulatorEnabled()
is required.
bAHI_APRegulatorEnabled()
bool_t bAHI_APRegulatorEnabled(void)
When the wake timer (FRWT) is not running (which is the default in the C library), this function performs a fixed delay process (300 µsec). In this case, it is recommended to call this function immediately after invoking vAHI_ApConfigure()
. Even if sufficient time has already passed, the fixed delay will still be executed.
When FRWT is enabled, the function performs the required waiting process based on the timer count value. If sufficient time has already passed, no additional waiting will be performed within this function.
vAHI_APRegisterCallback()
void vAHI_APRegisterCallback(PR_HWINT_APPCALLBACK prApCallback);
Registers an interrupt handler. This will be called when the ADC conversion is completed.
vAHI_AdcEnable()
void void vAHI_AdcEnable(
bool_t bContinuous,
bool_t bInputRange,
uint8 u8Source);
Configures the ADC conversion.
- If
bContinuous
is selected, continuous conversion is performed. However, since internal interrupts occur on each conversion, performance considerations are necessary. - Specifying
bInputRange
has no effect because the hardware does not support this feature. - During this call, the pin corresponding to
u8Source
is configured for ADC use. - If
bAHI_APRegulatorEnabled()
was not executed aftervAHI_ApConfigure()
and the waiting process was skipped, the waiting process will be performed within this call.
vAHI_AdcDisable()
void vAHI_AdcDisable(void);
Stops the ADC.
vAHI_AdcStartSample()
void vAHI_AdcStartSample(void);
Starts the ADC conversion.
- In principle, the initial state is set as input; however, strictly speaking,
the initial state is determined by the
BOARD_InitPins()
initialization in the TWENETmcu library’s pinmux.c.
bAHI_AdcPoll()
bool_t bAHI_AdcPoll(void);
Used for the polling wait process while(bAHI_AdcPoll());
to wait for ADC completion.
- After the ADC completion interrupt, this call returns FALSE only once.
u16AHI_AdcRead(), i16AHI_AdcRead_mv()
uint16 u16AHI_AdcRead(void);
int16 i16AHI_AdcRead_mv(void); // 非AHI独自関数
Reads the executed ADC value.
u16AHI_AdcRead()
returns the ADC value in 12-bit (0..4095). On error, it returns0xffff
.i16AHI_AdcRead_mv()
is a custom function not present in the AHI library. It returns the ADC value in mV, and on error, it returns-32768
.
s_adc_int_handler()
static void s_adc_int_handler(uint32_t a1, uint32_t a2);
A static function defined in AHIcmpt_ADC.cpp
that acts as an interrupt handler
when the ADC conversion completes, passing the interrupt to TWENET’s AppQApi.
- If a callback function is specified with
vAHI_APRegisterCallback()
, this handler will not be called.
Experimental Implementation
Batch Processing for Multiple Channels
// TWELITE GOLD only
// Uses the FSL driver feature to acquire ADC values from multiple channels in a single operation.
// Execute ADC (parameters are the same as vAHI_AdcEnable())
void vAHI_AdcEnableSeq(
bool_t bContinuous,
bool_t bInputRange,
uint32 u32MaskSource);
// Read ADC value
uint16 u16AHI_AdcReadSeq(
uint8 u8Source
);
// Read ADC value (in mV)
int16 i16AHI_AdcReadSeq_mv(
uint8 u8Source
);
sensor_driver, adc.c, adc.h
sensor_driver
is a set of processing functions used in existing TWEApps applications, providing a mechanism to abstract sensor processing. adc.c
and .h
describe the sequential processing for operating the on-chip ADC with sensor_driver
, including issuing a series of commands, waiting, and acquiring data. Additionally, TWENETmwx/sensors/legacy
contains implementations for several sensors other than ADC.
When migrating projects from TWELITE BLUE/RED to TWELITE GOLD, use the adjusted files stored in App_Twelite/Common
. However, since project contents may differ, modifications may be required as needed.
Source Name | Description |
---|---|
adc.c | ADC processing part (ADC value conversion adjusted for TWELITE GOLD’s range) |
adc.h | Definition part |
sensor_driver.c | Sensor processing abstraction part |
sensor_driver.h | Definition part |
Code Example
Please refer to the comments within the following code.
#include "sensor_driver.h"
#include "adc.h"
tsObjData_ADC sObjADC; // ADC management structure (data section)
tsSnsObj sADC; // ADC management structure (control section)
int16 a1,a2,ab; // Variables for storing results
...
// ADC initialization
vSnsObj_Init(&sADC);
vADC_Init(&sObjADC, &sADC, TRUE);
vADC_WaitInit(); // Wait for hardware initialization
...
// Specify the ports to measure with ADC (here: power supply voltage, ADC1, ADC2)
sObjADC.u8SourceMask = TEH_ADC_SRC_VOLT
| TEH_ADC_SRC_ADC_1 | TEH_ADC_SRC_ADC_2;
// Start ADC
vSnsObj_Process(&sADC, E_ORDER_KICK); // Start command
// Wait until processing of one ADC channel completes (=E_AHI_DEVICE_ANALOGUE interrupt)
// and call vSnsObj_Process() sequentially.
void cbToCoNet_vHwEvent(uint32 u32DeviceId, uint32 u32ItemBitmap) {
switch (u32DeviceId) {
case E_AHI_DEVICE_ANALOGUE:
// ADC completion interrupt
vSnsObj_Process(&sADC, E_ORDER_KICK);
if (bSnsObj_isComplete(&sADC)) {
// All channels have finished processing.
// The values are stored as follows:
a1=sObjADC.ai16Result[TEH_ADC_IDX_ADC_1]; // ADC1 [mV]
a2=sObjADC.ai16Result[TEH_ADC_IDX_ADC_2]; // ADC2 [mV]
ab=sObjADC.ai16Result[TEH_ADC_IDX_VOLT]; // Power supply voltage [mV]
// Return to the initial state before ADC start
vSnsObj_Process(&sADC, E_ORDER_KICK);
// For continuous execution, call E_ORDER_KICK again
vSnsObj_Process(&sADC, E_ORDER_KICK);
}
break;
default:
break;
}
}
Functions
vSnsObj_Init()
void vSnsObj_Init(tsSnsObj *pSnsObj)
Initializes the sensor management structure. Call this function right before vADC_Init()
.
vADC_Init()
void vADC_Init(tsObjData_ADC *pData, tsSnsObj *pSnsObj, bool_t bInitAPR)
Initializes the ADC. Prepare the tsObjData structure (for storing results) and the tsSnsObj structure (for ADC management) in advance.
- If bInitAPR is TRUE, the ADC hardware is initialized. Since hardware initialization takes some time, always execute vADC_WaitInit() to wait for initialization.
vSnsObj_Process()
void vSnsObj_Process(tsSnsObj *pObj, teEvent eEv)
Advances the ADC processing. Specifically, this function is called each time the conversion for one ADC port is completed.
During this process, the ADC value is retrieved, converted to mV, and stored in the tsSnsObj
structure.
This process handles events for the state transitions managed by the tsSnsObj
structure. Immediately after calling this process, call bSnsObj_isComplete()
to check whether processing is complete. To return to the initial state, execute this process again with E_ORDER_KICK
as the argument (in other words, to run the ADC again, execute E_ORDER_KICK
twice after completion).
tsObjData_ADC
structure
This structure contains the specified ADC channels and the resulting voltage values.
u8SourceMask
: A bitmap specifying the ports for ADC. The specified ports become ADC targets.TEH_ADC_SRC_VOLT
: Power supply voltageTEH_ADC_SRC_ADC_1-4
: ADC1,2,3,4
u8InputRangeMask
: Specifies the range (0-Vref or 0-2Vref) for the ADC target ports. Specified ports use 0-Vref; unspecified ports use 0-2Vref.ai16Result[]
: Structure for storing ADC values. Results are stored as mV values.TEH_ADC_IDX_VOLT
: Power supply voltageTEH_ADC_IDX_ADC_1-4
: ADC1,2,3,4