px-lib  0.9.2
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers

Description

Driver to perform analogue measurements.

File(s):

The driver must be configured by supplying a project specific "px_adc_cfg.h". "px_adc_cfg_template.h" can be copied, renamed and modified to supply compile time options.

Example:
#include "px_adc.h"
#include "px_board.h"
// Create ADC handle object
static px_adc_handle_t px_adc_handle;
int main(void)
{
uint16_t data;
// Initialise board
// Initialise ADC driver
// Open handle to ADC peripheral
px_adc_open(&px_adc_handle, PX_ADC_NR_1);
// Perform an ADC measurement on channel 0
data = px_adc_sample(&px_adc_handle, PX_ADC_CH0);
// Close ADC Handle
px_adc_close(px_adc_handle);
}

Data Structures

struct  px_adc_handle_t
 Define ADC handle. More...
 

Macros

#define PX_ADC_CFG_PER_COUNT   (PX_ADC_CFG_ADC1_EN)
 Number of enabled peripherals. More...
 
#define PX_ADC_CH_TO_BITMASK(ch)   ((uint32_t)1<<(ch))
 Convert ADC channel number into bit mask. More...
 
#define PX_ADC_CFG_ADC1_EN   1
 Enable/disable support for ADC1 peripheral. More...
 
#define PX_ADC_CFG_CLK   PX_ADC_CFG_CLK_PCLK
 Specify ADC clock source (see 'px_adc_cfg_clk_t') More...
 
#define PX_ADC_CFG_SAMPLE_TIME   PX_ADC_CFG_SAMPLE_TIME_7_5
 Specify ADC sampling time (see 'px_adc_cfg_sample_time_t') More...
 
#define PX_ADC_CFG_RES   PX_ADC_CFG_RES_12_BITS
 Specify ADC resolution (see 'px_adc_cfg_res_t') More...
 
#define PX_ADC_CFG_OVERSAMPLING   PX_ADC_CFG_OVERSAMPLING_NONE
 Specify ADC oversampling (see 'px_adc_cfg_oversampling_t') More...
 
#define PX_ADC_CFG_OVERSAMPLING_SHIFT   PX_ADC_CFG_OVERSAMPLING_SHIFT_NONE
 Specify ADC oversampling (see 'px_adc_cfg_oversampling_shift_t') More...
 
#define PX_ADC_CFG_ADC1_EN   1
 Enable/disable support for ADC1 peripheral. More...
 
#define PX_ADC_CFG_CLK   PX_ADC_CFG_CLK_PCLK
 Specify ADC clock source (see 'px_adc_cfg_clk_t') More...
 
#define PX_ADC_CFG_SAMPLE_TIME   PX_ADC_CFG_SAMPLE_TIME_160_5
 Specify ADC sampling time (see 'px_adc_cfg_sample_time_t') More...
 
#define PX_ADC_CFG_RES   PX_ADC_CFG_RES_12_BITS
 Specify ADC resolution (see 'px_adc_cfg_res_t') More...
 
#define PX_ADC_CFG_OVERSAMPLING   PX_ADC_CFG_OVERSAMPLING_NONE
 Specify ADC oversampling (see 'px_adc_cfg_oversampling_t') More...
 
#define PX_ADC_CFG_OVERSAMPLING_SHIFT   PX_ADC_CFG_OVERSAMPLING_SHIFT_NONE
 Specify ADC oversampling (see 'px_adc_cfg_oversampling_shift_t') More...
 

Enumerations

enum  px_adc_nr_t
 Specify ADC peripheral. More...
 
enum  px_adc_ch_t {
  PX_ADC_CH0 = 0, PX_ADC_CH1 = 1, PX_ADC_CH2 = 2, PX_ADC_CH3 = 3,
  PX_ADC_CH4 = 4, PX_ADC_CH5 = 5, PX_ADC_CH6 = 6, PX_ADC_CH7 = 7,
  PX_ADC_CH8 = 8, PX_ADC_CH9 = 9, PX_ADC_CH10 = 10, PX_ADC_CH11 = 11,
  PX_ADC_CH12 = 12, PX_ADC_CH13 = 13, PX_ADC_CH14 = 14, PX_ADC_CH15 = 15 ,
  PX_ADC_CH17 = 17, PX_ADC_CH18 = 18
}
 Specify ADC channel. More...
 
enum  px_adc_cfg_clk_t { PX_ADC_CFG_CLK_PCLK = 0, PX_ADC_CFG_CLK_PCLK_DIV2 = 1, PX_ADC_CFG_CLK_PCLK_DIV4 = 2 }
 Specify ADC peripheral clock source. More...
 
enum  px_adc_cfg_sample_time_t {
  PX_ADC_CFG_SAMPLE_TIME_1_5 = 0, PX_ADC_CFG_SAMPLE_TIME_3_5 = 1, PX_ADC_CFG_SAMPLE_TIME_7_5 = 2, PX_ADC_CFG_SAMPLE_TIME_12_5 = 3,
  PX_ADC_CFG_SAMPLE_TIME_19_5 = 4, PX_ADC_CFG_SAMPLE_TIME_39_5 = 5, PX_ADC_CFG_SAMPLE_TIME_79_5 = 6, PX_ADC_CFG_SAMPLE_TIME_160_5 = 7
}
 Specify ADC sampling time. More...
 
enum  px_adc_cfg_res_t { PX_ADC_CFG_RES_12_BITS = 0, PX_ADC_CFG_RES_10_BITS = 1, PX_ADC_CFG_RES_8_BITS = 2, PX_ADC_CFG_RES_6_BITS = 3 }
 Specify ADC resolution. More...
 
enum  px_adc_cfg_oversampling_t {
  PX_ADC_CFG_OVERSAMPLING_NONE = 0, PX_ADC_CFG_OVERSAMPLING_2 = 1, PX_ADC_CFG_OVERSAMPLING_4 = 2, PX_ADC_CFG_OVERSAMPLING_8 = 3,
  PX_ADC_CFG_OVERSAMPLING_16 = 4, PX_ADC_CFG_OVERSAMPLING_32 = 5, PX_ADC_CFG_OVERSAMPLING_64 = 6, PX_ADC_CFG_OVERSAMPLING_128 = 7,
  PX_ADC_CFG_OVERSAMPLING_256 = 8
}
 Specify ADC oversampling. More...
 
enum  px_adc_cfg_oversampling_shift_t {
  PX_ADC_CFG_OVERSAMPLING_SHIFT_NONE = 0, PX_ADC_CFG_OVERSAMPLING_SHIFT_1 = 1, PX_ADC_CFG_OVERSAMPLING_SHIFT_2 = 2, PX_ADC_CFG_OVERSAMPLING_SHIFT_3 = 3,
  PX_ADC_CFG_OVERSAMPLING_SHIFT_4 = 4, PX_ADC_CFG_OVERSAMPLING_SHIFT_5 = 5, PX_ADC_CFG_OVERSAMPLING_SHIFT_6 = 6, PX_ADC_CFG_OVERSAMPLING_SHIFT_7 = 7,
  PX_ADC_CFG_OVERSAMPLING_SHIFT_8 = 8
}
 Specify ADC oversampling. More...
 

Functions

void px_adc_init (void)
 Initialise ADC driver. More...
 
bool px_adc_open (px_adc_handle_t *handle, px_adc_nr_t adc_nr)
 Open ADC peripheral using predefined (default) parameters. More...
 
bool px_adc_close (px_adc_handle_t *handle)
 Close specified device. More...
 
uint16_t px_adc_sample (px_adc_handle_t *handle, px_adc_ch_t ch)
 Perform a single measurement on the specified ADC channel. More...
 

Data Structure Documentation

◆ px_adc_handle_t

struct px_adc_handle_t

Define ADC handle.

Definition at line 161 of file px_adc.h.

Data Fields
struct px_adc_per_s * adc_per ADC peripheral data.

Macro Definition Documentation

◆ PX_ADC_CFG_PER_COUNT

#define PX_ADC_CFG_PER_COUNT   (PX_ADC_CFG_ADC1_EN)

Number of enabled peripherals.

Definition at line 63 of file px_adc.h.

◆ PX_ADC_CH_TO_BITMASK

#define PX_ADC_CH_TO_BITMASK (   ch)    ((uint32_t)1<<(ch))

Convert ADC channel number into bit mask.

Definition at line 211 of file px_adc.h.

◆ PX_ADC_CFG_ADC1_EN [1/2]

#define PX_ADC_CFG_ADC1_EN   1

Enable/disable support for ADC1 peripheral.

Definition at line 33 of file px_adc_cfg_template.h.

◆ PX_ADC_CFG_CLK [1/2]

#define PX_ADC_CFG_CLK   PX_ADC_CFG_CLK_PCLK

Specify ADC clock source (see 'px_adc_cfg_clk_t')

Definition at line 36 of file px_adc_cfg_template.h.

◆ PX_ADC_CFG_SAMPLE_TIME [1/2]

#define PX_ADC_CFG_SAMPLE_TIME   PX_ADC_CFG_SAMPLE_TIME_7_5

Specify ADC sampling time (see 'px_adc_cfg_sample_time_t')

Definition at line 39 of file px_adc_cfg_template.h.

◆ PX_ADC_CFG_RES [1/2]

#define PX_ADC_CFG_RES   PX_ADC_CFG_RES_12_BITS

Specify ADC resolution (see 'px_adc_cfg_res_t')

Definition at line 42 of file px_adc_cfg_template.h.

◆ PX_ADC_CFG_OVERSAMPLING [1/2]

#define PX_ADC_CFG_OVERSAMPLING   PX_ADC_CFG_OVERSAMPLING_NONE

Specify ADC oversampling (see 'px_adc_cfg_oversampling_t')

Definition at line 45 of file px_adc_cfg_template.h.

◆ PX_ADC_CFG_OVERSAMPLING_SHIFT [1/2]

#define PX_ADC_CFG_OVERSAMPLING_SHIFT   PX_ADC_CFG_OVERSAMPLING_SHIFT_NONE

Specify ADC oversampling (see 'px_adc_cfg_oversampling_shift_t')

Definition at line 48 of file px_adc_cfg_template.h.

◆ PX_ADC_CFG_ADC1_EN [2/2]

#define PX_ADC_CFG_ADC1_EN   1

Enable/disable support for ADC1 peripheral.

Definition at line 33 of file px_adc_cfg.h.

◆ PX_ADC_CFG_CLK [2/2]

#define PX_ADC_CFG_CLK   PX_ADC_CFG_CLK_PCLK

Specify ADC clock source (see 'px_adc_cfg_clk_t')

Definition at line 36 of file px_adc_cfg.h.

◆ PX_ADC_CFG_SAMPLE_TIME [2/2]

#define PX_ADC_CFG_SAMPLE_TIME   PX_ADC_CFG_SAMPLE_TIME_160_5

Specify ADC sampling time (see 'px_adc_cfg_sample_time_t')

Definition at line 39 of file px_adc_cfg.h.

◆ PX_ADC_CFG_RES [2/2]

#define PX_ADC_CFG_RES   PX_ADC_CFG_RES_12_BITS

Specify ADC resolution (see 'px_adc_cfg_res_t')

Definition at line 42 of file px_adc_cfg.h.

◆ PX_ADC_CFG_OVERSAMPLING [2/2]

#define PX_ADC_CFG_OVERSAMPLING   PX_ADC_CFG_OVERSAMPLING_NONE

Specify ADC oversampling (see 'px_adc_cfg_oversampling_t')

Definition at line 45 of file px_adc_cfg.h.

◆ PX_ADC_CFG_OVERSAMPLING_SHIFT [2/2]

#define PX_ADC_CFG_OVERSAMPLING_SHIFT   PX_ADC_CFG_OVERSAMPLING_SHIFT_NONE

Specify ADC oversampling (see 'px_adc_cfg_oversampling_shift_t')

Definition at line 48 of file px_adc_cfg.h.

Enumeration Type Documentation

◆ px_adc_nr_t

Specify ADC peripheral.

Definition at line 73 of file px_adc.h.

◆ px_adc_ch_t

Specify ADC channel.

Enumerator
PX_ADC_CH0 

ADC Channel 0.

PX_ADC_CH1 

ADC Channel 1.

PX_ADC_CH2 

ADC Channel 2.

PX_ADC_CH3 

ADC Channel 3.

PX_ADC_CH4 

ADC Channel 4.

PX_ADC_CH5 

ADC Channel 5.

PX_ADC_CH6 

ADC Channel 6.

PX_ADC_CH7 

ADC Channel 7.

PX_ADC_CH8 

ADC Channel 8.

PX_ADC_CH9 

ADC Channel 9.

PX_ADC_CH10 

ADC Channel 10.

PX_ADC_CH11 

ADC Channel 11.

PX_ADC_CH12 

ADC Channel 12.

PX_ADC_CH13 

ADC Channel 13.

PX_ADC_CH14 

ADC Channel 14.

PX_ADC_CH15 

ADC Channel 15.

PX_ADC_CH17 

Internal Voltage Reference (Vrefint)

PX_ADC_CH18 

Temperature sensor (Tsense)

Definition at line 79 of file px_adc.h.

◆ px_adc_cfg_clk_t

Specify ADC peripheral clock source.

Enumerator
PX_ADC_CFG_CLK_PCLK 

Peripheral Clock.

PX_ADC_CFG_CLK_PCLK_DIV2 

Peripheral Clock / 2.

PX_ADC_CFG_CLK_PCLK_DIV4 

Peripheral Clock / 4.

Definition at line 103 of file px_adc.h.

◆ px_adc_cfg_sample_time_t

Specify ADC sampling time.

Enumerator
PX_ADC_CFG_SAMPLE_TIME_1_5 

1.5 ADC clock cycles

PX_ADC_CFG_SAMPLE_TIME_3_5 

3.5 ADC clock cycles

PX_ADC_CFG_SAMPLE_TIME_7_5 

7.5 ADC clock cycles

PX_ADC_CFG_SAMPLE_TIME_12_5 

12.5 ADC clock cycles

PX_ADC_CFG_SAMPLE_TIME_19_5 

19.5 ADC clock cycles

PX_ADC_CFG_SAMPLE_TIME_39_5 

39.5 ADC clock cycles

PX_ADC_CFG_SAMPLE_TIME_79_5 

79.5 ADC clock cycles

PX_ADC_CFG_SAMPLE_TIME_160_5 

160.5 ADC clock cycles

Definition at line 111 of file px_adc.h.

◆ px_adc_cfg_res_t

Specify ADC resolution.

Enumerator
PX_ADC_CFG_RES_12_BITS 

12-bit resolution

PX_ADC_CFG_RES_10_BITS 

10-bit resolution

PX_ADC_CFG_RES_8_BITS 

8-bit resolution

PX_ADC_CFG_RES_6_BITS 

6-bit resolution

Definition at line 124 of file px_adc.h.

◆ px_adc_cfg_oversampling_t

Specify ADC oversampling.

Enumerator
PX_ADC_CFG_OVERSAMPLING_NONE 

No oversampling.

PX_ADC_CFG_OVERSAMPLING_2 

2 x oversampling

PX_ADC_CFG_OVERSAMPLING_4 

4 x oversampling

PX_ADC_CFG_OVERSAMPLING_8 

8 x oversampling

PX_ADC_CFG_OVERSAMPLING_16 

16 x oversampling

PX_ADC_CFG_OVERSAMPLING_32 

32 x oversampling

PX_ADC_CFG_OVERSAMPLING_64 

64 x oversampling

PX_ADC_CFG_OVERSAMPLING_128 

128 x oversampling

PX_ADC_CFG_OVERSAMPLING_256 

256 x oversampling

Definition at line 133 of file px_adc.h.

◆ px_adc_cfg_oversampling_shift_t

Specify ADC oversampling.

Enumerator
PX_ADC_CFG_OVERSAMPLING_SHIFT_NONE 

No shift.

PX_ADC_CFG_OVERSAMPLING_SHIFT_1 

Shift 1-bit.

PX_ADC_CFG_OVERSAMPLING_SHIFT_2 

Shift 2-bits.

PX_ADC_CFG_OVERSAMPLING_SHIFT_3 

Shift 3-bits.

PX_ADC_CFG_OVERSAMPLING_SHIFT_4 

Shift 4-bits.

PX_ADC_CFG_OVERSAMPLING_SHIFT_5 

Shift 5-bits.

PX_ADC_CFG_OVERSAMPLING_SHIFT_6 

Shift 6-bits.

PX_ADC_CFG_OVERSAMPLING_SHIFT_7 

Shift 7-bits.

PX_ADC_CFG_OVERSAMPLING_SHIFT_8 

Shift 8-bits.

Definition at line 147 of file px_adc.h.

Function Documentation

◆ px_adc_init()

void px_adc_init ( void  )

Initialise ADC driver.

Definition at line 136 of file px_adc.c.

◆ px_adc_open()

bool px_adc_open ( px_adc_handle_t handle,
px_adc_nr_t  adc_nr 
)

Open ADC peripheral using predefined (default) parameters.

Parameters
handlePointer to handle data structure
adc_nrADC peripheral number
Return values
falseError: peripheral was not opened
trueSuccess: peripheral was opened

Definition at line 144 of file px_adc.c.

◆ px_adc_close()

bool px_adc_close ( px_adc_handle_t handle)

Close specified device.

Parameters
handlePointer to handle data structure
Return values
trueSuccess
falseSpecified device was already closed (or not opened)

Definition at line 176 of file px_adc.c.

◆ px_adc_sample()

uint16_t px_adc_sample ( px_adc_handle_t handle,
px_adc_ch_t  ch 
)

Perform a single measurement on the specified ADC channel.

Parameters
handlePointer to handle data structure
chSpecified ADC channel
Returns
uint16_t Raw ADC value

Definition at line 231 of file px_adc.c.