hsk-libs-dev  270
High Speed Karlsruhe XC878 library collection
Macros | Functions | Variables
hsk_adc.c File Reference

HSK Analog Digital Conversion implementation. More...

#include <Infineon/XC878.h>
#include "hsk_adc.h"
#include <string.h>
#include "../hsk_isr/hsk_isr.h"
Include dependency graph for hsk_adc.c:

Macros

#define ADC_CLK_12MHz   0
 Conversion clock prescaler setting for 12MHz. More...
 
#define ADC_CLK_8MHz   1
 Conversion clock prescaler setting for 8MHz. More...
 
#define ADC_CLK_6MHz   2
 Conversion clock prescaler setting for 6MHz. More...
 
#define ADC_CLK_750kHz   3
 Conversion clock prescaler setting for 750kHz. More...
 
#define ADC_CHANNELS   8
 Number of availbale ADC channels. More...
 
#define ADC_QUEUE   4
 Number of queue slots. More...
 
#define BIT_CHNR   0
 ADC_RESRxL Channel Number bits. More...
 
#define CNT_CHNR   3
 CHNR bit count. More...
 
#define BIT_RESULT   6
 ADC_RESRxLH Conversion Result bits. More...
 
#define CNT_RESULT   10
 RESULT bit count. More...
 
#define BIT_DW   6
 ADC_GLOBCTR Data Width bit. More...
 
#define BIT_CTC   4
 ADC_GLOBCTR Conversion Time Control bits. More...
 
#define CNT_CTC   2
 CTC bit count. More...
 
#define BIT_ASEN_SEQUENTIAL   6
 ADC_PRAR Arbitration Slot Sequential Enable bit. More...
 
#define BIT_ASEN_PARALLEL   7
 ADC_PRAR Arbitration Slot Parallel Enable bit. More...
 
#define BIT_IEN   4
 RCRx Interrupt Enable bit. More...
 
#define BIT_WFR   6
 RCRx Wait-for-Read Mode. More...
 
#define BIT_VFCTR   7
 RCRx Valid Flag Control bit. More...
 
#define BIT_ENGT   0
 QMR0 Enable Gate bit. More...
 
#define BIT_ANON   7
 ADC_GLOBCTR Analog Part Switched On bit. More...
 
#define BIT_IMODE   4
 SYSCON0 Interrupt Structure 2 Mode Select bit. More...
 
#define BIT_ADC_DIS   0
 PMCON1 ADC Disable Request bit. More...
 
#define BIT_FILL   0
 QSR0 bits Filling Level. More...
 
#define CNT_FILL   2
 Filling Level bit count. More...
 
#define BIT_EMPTY   5
 QSR0 bit Queue Empty. More...
 
#define BIT_REQCHNR   0
 ADC_QINR0 Request Channel Number bits. More...
 
#define CNT_REQCHNR   3
 REQCHNR bit count. More...
 

Functions

void hsk_adc_isr10 (void)
 Write the 10bit conversion result to the targeted memory address. More...
 
void hsk_adc_isr8 (void)
 Write the 8bit conversion result to the targeted memory address. More...
 
void hsk_adc_init (ubyte resolution, uword convTime)
 Initialize the AD conversion. More...
 
void hsk_adc_enable (void)
 Turns on ADC conversion, if previously deactivated. More...
 
void hsk_adc_disable (void)
 Turns off ADC conversion unit to converse power. More...
 
void hsk_adc_open10 (const hsk_adc_channel channel, uword *const target)
 Open the given ADC channel in 10 bit mode. More...
 
void hsk_adc_open8 (const hsk_adc_channel channel, ubyte *const target)
 Open the given ADC channel in 8 bit mode. More...
 
void hsk_adc_close (const hsk_adc_channel channel)
 Close the given ADC channel. More...
 
bool hsk_adc_service (void)
 A maintenance function that takes care of keeping AD conversions going. More...
 
bool hsk_adc_request (const hsk_adc_channel channel)
 Requests an ADC for a specific channel. More...
 
void hsk_adc_isr_warmup10 (void)
 Special ISR for warming up 10 bit conversions. More...
 
void hsk_adc_warmup10 (void)
 Warm up 10 bit AD conversion. More...
 

Variables

static hsk_adc_channel nextChannel = 8
 Holds the channel of the next conversion that will be requested. More...
 
union {
   uword *   ptr10
 Pointer type used for 10 bit conversions. More...
 
   ubyte *   ptr8
 Pointer type used for 8 bit conversions. More...
 
targets [8]
 An array of target addresses to write conversion results into. More...
 

Detailed Description

HSK Analog Digital Conversion implementation.

This file implements the functions defined in hsk_adc.h.

To be able to use all 8 channels the ADC is kept in sequential mode.

In order to reduce processing time this library uses the convention that all functions terminate with ADC register page 6. Page 6 contains the ADC queue request and status registers.

Author
kami

Macro Definition Documentation

§ ADC_CHANNELS

#define ADC_CHANNELS   8

Number of availbale ADC channels.

§ ADC_CLK_12MHz

#define ADC_CLK_12MHz   0

Conversion clock prescaler setting for 12MHz.

§ ADC_CLK_6MHz

#define ADC_CLK_6MHz   2

Conversion clock prescaler setting for 6MHz.

§ ADC_CLK_750kHz

#define ADC_CLK_750kHz   3

Conversion clock prescaler setting for 750kHz.

§ ADC_CLK_8MHz

#define ADC_CLK_8MHz   1

Conversion clock prescaler setting for 8MHz.

§ ADC_QUEUE

#define ADC_QUEUE   4

Number of queue slots.

§ BIT_ADC_DIS

#define BIT_ADC_DIS   0

PMCON1 ADC Disable Request bit.

§ BIT_ANON

#define BIT_ANON   7

ADC_GLOBCTR Analog Part Switched On bit.

§ BIT_ASEN_PARALLEL

#define BIT_ASEN_PARALLEL   7

ADC_PRAR Arbitration Slot Parallel Enable bit.

§ BIT_ASEN_SEQUENTIAL

#define BIT_ASEN_SEQUENTIAL   6

ADC_PRAR Arbitration Slot Sequential Enable bit.

§ BIT_CHNR

#define BIT_CHNR   0

ADC_RESRxL Channel Number bits.

§ BIT_CTC

#define BIT_CTC   4

ADC_GLOBCTR Conversion Time Control bits.

§ BIT_DW

#define BIT_DW   6

ADC_GLOBCTR Data Width bit.

§ BIT_EMPTY

#define BIT_EMPTY   5

QSR0 bit Queue Empty.

§ BIT_ENGT

#define BIT_ENGT   0

QMR0 Enable Gate bit.

§ BIT_FILL

#define BIT_FILL   0

QSR0 bits Filling Level.

§ BIT_IEN

#define BIT_IEN   4

RCRx Interrupt Enable bit.

§ BIT_IMODE

#define BIT_IMODE   4

SYSCON0 Interrupt Structure 2 Mode Select bit.

§ BIT_REQCHNR

#define BIT_REQCHNR   0

ADC_QINR0 Request Channel Number bits.

§ BIT_RESULT

#define BIT_RESULT   6

ADC_RESRxLH Conversion Result bits.

§ BIT_VFCTR

#define BIT_VFCTR   7

RCRx Valid Flag Control bit.

§ BIT_WFR

#define BIT_WFR   6

RCRx Wait-for-Read Mode.

§ CNT_CHNR

#define CNT_CHNR   3

CHNR bit count.

§ CNT_CTC

#define CNT_CTC   2

CTC bit count.

§ CNT_FILL

#define CNT_FILL   2

Filling Level bit count.

§ CNT_REQCHNR

#define CNT_REQCHNR   3

REQCHNR bit count.

§ CNT_RESULT

#define CNT_RESULT   10

RESULT bit count.

Function Documentation

§ hsk_adc_close()

void hsk_adc_close ( const hsk_adc_channel  channel)

Close the given ADC channel.

Stopp ADC if no more channels were left.

Parameters
channelThe channel id

§ hsk_adc_disable()

void hsk_adc_disable ( void  )

Turns off ADC conversion unit to converse power.

§ hsk_adc_enable()

void hsk_adc_enable ( void  )

Turns on ADC conversion, if previously deactivated.

§ hsk_adc_init()

void hsk_adc_init ( ubyte  resolution,
uword  convTime 
)

Initialize the AD conversion.

The shortest possible conversion time is 1.25µs, the longest is 714.75µs. The given value will be rounded down.

Note if hsk_adc_service() is not called in intervals shorter than convTime, there will be a waiting period between conversions. This prevents locking up of the controler with erratic interrupts.

There is a 4 entry queue, for starting conversions, so it suffices to average the interval below convTime.

All already open channels will be closed upon calling this function.

Parameters
resolutionThe conversion resolution, any of ADC_RESOLUTION_*
convTimeThe desired conversion time in µs
Here is the call graph for this function:

§ hsk_adc_isr10()

void hsk_adc_isr10 ( void  )
private

Write the 10bit conversion result to the targeted memory address.

§ hsk_adc_isr8()

void hsk_adc_isr8 ( void  )
private

Write the 8bit conversion result to the targeted memory address.

§ hsk_adc_isr_warmup10()

void hsk_adc_isr_warmup10 ( void  )
private

Special ISR for warming up 10 bit conversions.

This is used as the ISR by hsk_adc_warmup() after the warmup countdowns have been initialized. After all warmup countdowns have returned to zero The original ISR will be put back in control.

Here is the call graph for this function:

§ hsk_adc_open10()

void hsk_adc_open10 ( const hsk_adc_channel  channel,
uword *const  target 
)

Open the given ADC channel in 10 bit mode.

Parameters
channelThe channel id
targetA pointer where to store conversion results

§ hsk_adc_open8()

void hsk_adc_open8 ( const hsk_adc_channel  channel,
ubyte *const  target 
)

Open the given ADC channel in 8 bit mode.

Parameters
channelThe channel id
targetA pointer where to store conversion results

§ hsk_adc_request()

bool hsk_adc_request ( const hsk_adc_channel  channel)

Requests an ADC for a specific channel.

This function is an alternative to hsk_adc_service(). Make requests in time before the updated value is required.

This function uses the same queue as hsk_adc_service(), if the queue is full it fails silently.

Parameters
channelThe channel id
Return values
0The queue is full
1A conversion request has been added to the queue

§ hsk_adc_service()

bool hsk_adc_service ( void  )

A maintenance function that takes care of keeping AD conversions going.

This has to be called repeatedly.

There is a queue of up to 4 conversion jobs. One call of this function only adds one job to the queue.

Return values
0No conversion request had been queued, either the queue is full or no channels have been configured
1A conversion request has been added to the queue
Here is the call graph for this function:

§ hsk_adc_warmup10()

void hsk_adc_warmup10 ( void  )

Warm up 10 bit AD conversion.

I.e. make sure all conversion targets have been initialized with a conversion result. This is a blocking function only intended for single use during the boot procedure.

This function will not terminate unless interrupts are enabled.

Note
This function only works in 10 bit mode, because in 8 bit mode it is impossible to initialize targets with an invalid value.
Here is the call graph for this function:

Variable Documentation

§ nextChannel

hsk_adc_channel nextChannel = 8
static

Holds the channel of the next conversion that will be requested.

§ ptr10

uword* ptr10

Pointer type used for 10 bit conversions.

§ ptr8

ubyte* ptr8

Pointer type used for 8 bit conversions.

§ targets

targets
static

An array of target addresses to write conversion results into.