ccadc.c File Reference


Detailed Description

CC-ADC module.

Application note:
AVR352: Using the CC-ADC
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
4810
Date
2008-11-04 12:22:27 +0100 (ti, 04 nov 2008)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file ccadc.c.

#include <ioavr.h>
#include "config.h"
#include "ccadc.h"

Include dependency graph for ccadc.c:

Go to the source code of this file.

Data Structures

union  CCADC_conversionFlag_union

Functions

__interrupt void Ccadc_Acc_ISR (void)
 CC-ADC Accumulated current conversion complete interrupt.
int32_t CCADC_GetAccResult (void)
 Returns result of an CC-ADC Accumulated Conversion.
int32_t CCADC_GetIccResult (void)
 Returns result of an CC-ADC Instantaneous Conversion.
uint8_t CCADC_GetMode (void)
 Provide information whether CC-ADC is in RCC mode of IC/ACC mode.
__interrupt void Ccadc_Icc_ISR (void)
 CC-ADC instant current conversion complete interrupt.
void CCADC_Init (CCADC_AccConvPeriod_t accConversionTime, CCADC_RccConvPeriod_t rccSamplingInterval, uint16_t regularCurrentLevel)
 Configuration of the CC-ADC and Regular Current Level.
bool CCADC_isAccResultReady (void)
 Used to determine if a new CCADC accumulated current result is ready.
bool CCADC_isIccResultReady (void)
 Used to determine if a new CCADC instant current result is ready.
static void CCADC_PolarityManager (void)
 Controls when to alter the CC-ADC input polarity.
__interrupt void Ccadc_Rcc_ISR (void)
 CC-ADC Regular current threshold passed interrupt.
void CCADC_SetMode (CCADC_modes_t mode)
 Selects mode of operation for the CC-ADC.

Variables

volatile int32_t CCADC_accResult
volatile int16_t CCADC_iccResult
union CCADC_conversionFlag_union CCADC_statusFlags


Function Documentation

__interrupt void Ccadc_Acc_ISR ( void   ) 

CC-ADC Accumulated current conversion complete interrupt.

Definition at line 327 of file ccadc.c.

References CCADC_conversionFlag_union::accResultReady, CCADC_accResult, CCADC_PolarityManager(), and CCADC_statusFlags.

00328 {
00329     if( CADCSRA & (1<<CADPOL) ){
00330         CCADC_accResult = - CADAC;
00331     }else{
00332         CCADC_accResult = CADAC;
00333     }
00334     CCADC_PolarityManager();
00335     CCADC_statusFlags.accResultReady = true;
00336 }

Here is the call graph for this function:

int32_t CCADC_GetAccResult ( void   ) 

Returns result of an CC-ADC Accumulated Conversion.

Returns the 18 bit signed result of an Accumulated Conversion as signed 32-bit. The result is scaled so to use a 24.8 signed fixed point format before it is returned.

Note:
The Bandgap voltage refence must be enabled and calibrated to used the CC-ADC.
Return values:
result which is returned in a 24.8 signed fixed point format

Definition at line 201 of file ccadc.c.

References CCADC_accResult.

Referenced by main().

00202 {
00203     int32_t result;
00204     uint8_t interruptState = __save_interrupt();
00205 
00206     __disable_interrupt();
00207     result = CCADC_accResult;
00208     __restore_interrupt(interruptState);
00209 
00210     return result;
00211 }

int32_t CCADC_GetIccResult ( void   ) 

Returns result of an CC-ADC Instantaneous Conversion.

Returns the 13 bit signed result of an Instantaneous Conversion as signed 16-bit (since there are no standard datatypes for 13 bit).

Note:
The Bandgap voltage refence must be enabled and calibrated to used the CC-ADC.
Return values:
current (in mA)

Definition at line 224 of file ccadc.c.

References CCADC_iccResult.

Referenced by main().

00225 {
00226     int16_t result;
00227     uint8_t interruptState = __save_interrupt();
00228     
00229     __disable_interrupt();
00230     result = CCADC_iccResult;
00231     __restore_interrupt(interruptState);
00232     
00233     // Scale the 13-bit Icc result to 18-bit like the Acc result.
00234     return (int32_t)(result * (1<<5));
00235 }

uint8_t CCADC_GetMode ( void   ) 

Provide information whether CC-ADC is in RCC mode of IC/ACC mode.

Note:
The Bandgap voltage refence must be enabled and calibrated to used the CC-ADC.
Return values:
mode [CCADC_RCC, CCADC_IAC (for ICC, ACC or IAC) ]

Definition at line 176 of file ccadc.c.

References CCADC_IAC, and CCADC_RCC.

00177 {
00179     while( CADCSRA & (1<<CADUB) ){
00180     }
00181     
00182     if( CADCSRA & (1<<CADSE) ){
00183         return CCADC_RCC;
00184     }else{
00185         return CCADC_IAC;
00186     }
00187 }

__interrupt void Ccadc_Icc_ISR ( void   ) 

CC-ADC instant current conversion complete interrupt.

Definition at line 304 of file ccadc.c.

References CCADC_iccResult, CCADC_statusFlags, and CCADC_conversionFlag_union::iccResultReady.

00305 {
00306     if( CADCSRA & (1<<CADPOL) ){
00307         CCADC_iccResult = -CADIC;
00308     }else{
00309         CCADC_iccResult = CADIC;
00310     }
00311     CCADC_statusFlags.iccResultReady = true;
00312 }

void CCADC_Init ( CCADC_AccConvPeriod_t  accConversionTime,
CCADC_RccConvPeriod_t  rccSamplingInterval,
uint16_t  regularCurrentLevel 
)

Configuration of the CC-ADC and Regular Current Level.

Note that the CC-ADC is not started, only configured. The CC-ADC is started by the CCADC_Mode_Select() function.

Note:
The Bandgap voltage refence must be enabled and calibrated to used the CC-ADC.
Parameters:
accConversionTime [ACCT_128, ACCT_256, ACCT_512, ACCT_1024]
rccSamplingInterval [RSI_256, RSI_512, RSI_1024, RSI_2048]
regularCurrentLevel Given in milliAmps

Definition at line 88 of file ccadc.c.

References CCADC_POLARITY_POS, RCC_SCALE, and RCC_STEP_SIZE_mA_SCALED.

Referenced by main().

00089 { 
00090     // Set Regular current detection level.
00091     CADRC = ((regularCurrentLevel<<RCC_SCALE) / RCC_STEP_SIZE_mA_SCALED);
00092 
00093     /* Wait until CC-ADC is ready for configuration change. (wait for
00094      * synchronization between clock domains to be completed since previous change).
00095      */
00096     while( CADCSRA & (1<<CADUB) ){
00097     }
00098     
00099     /* Write CC-ADC settings:
00100      * - Clear CC-ADC Conversion Time and Regular Current Sampling Interval.\n
00101      * - select new CC-ADC Conversion Time and Current Sampling Interval.\n
00102      * - and set the polarity bit accoring the initial state.
00103      */
00104     CADCSRA = (CADCSRA & ~( (1<<CADAS1)|(1<<CADAS0)|(1<<CADSI1)|(1<<CADSI0)|(1<<CADPOL) )) |
00105               ((uint8_t)accConversionTime | (uint8_t)rccSamplingInterval) |
00106               CCADC_POLARITY_POS;
00107 }

bool CCADC_isAccResultReady ( void   ) 

Used to determine if a new CCADC accumulated current result is ready.

Since the internal status flag is also accessed by the interrupt, it is protected by disabling the global interrupt while accessing it.

Return values:
bool Returns true is new result is ready, otherwise false.

Definition at line 266 of file ccadc.c.

References CCADC_conversionFlag_union::accResultReady, and CCADC_statusFlags.

00267 {
00268     uint8_t interruptState = __save_interrupt();
00269     __disable_interrupt();
00270 
00271     bool flagState = CCADC_statusFlags.accResultReady;
00272     CCADC_statusFlags.accResultReady = false;
00273 
00274     __restore_interrupt( interruptState );
00275     
00276     return flagState;
00277 }

bool CCADC_isIccResultReady ( void   ) 

Used to determine if a new CCADC instant current result is ready.

Since the internal status flag is also accessed by the interrupt, it is protected by disabling the global interrupt while accessing it.

Return values:
bool Returns true is new result is ready, otherwise false.

Definition at line 287 of file ccadc.c.

References CCADC_statusFlags, and CCADC_conversionFlag_union::iccResultReady.

00288 {
00289     uint8_t interruptState = __save_interrupt();
00290     __disable_interrupt();
00291 
00292     bool flagState = CCADC_statusFlags.iccResultReady;
00293     CCADC_statusFlags.iccResultReady = false;
00294 
00295     __restore_interrupt( interruptState );
00296     
00297     return flagState;
00298 }

static void CCADC_PolarityManager ( void   )  [static]

Controls when to alter the CC-ADC input polarity.

Changes the CC-ADC input polarity every CONVERSION_COUNT conversion. Since this should be done as soon after the conversion has completed as possible this function is embedded in the ACC conversion complete interrupt rutine. It should not be called from elsewere, and is therefore static/local file scope only.

Definition at line 247 of file ccadc.c.

References CONVERSION_COUNT.

Referenced by Ccadc_Acc_ISR().

00248 {
00249     static uint8_t polarityCounter = CONVERSION_COUNT;
00250     
00251     polarityCounter--;
00252     if(polarityCounter == 0){
00253         CADCSRA ^= (1<<CADPOL);
00254         polarityCounter = CONVERSION_COUNT;
00255     }
00256 }

__interrupt void Ccadc_Rcc_ISR ( void   ) 

CC-ADC Regular current threshold passed interrupt.

Definition at line 318 of file ccadc.c.

References CCADC_statusFlags, and CCADC_conversionFlag_union::regularCurrentDetected.

00319 {
00320     CCADC_statusFlags.regularCurrentDetected = true;
00321 }

void CCADC_SetMode ( CCADC_modes_t  mode  ) 

Selects mode of operation for the CC-ADC.

Select between

  • CCADC_DISABLE (To disable the CC-ADC, and disable and clear interrupts).
  • CCADC_IAC (CC_ADC is enabled and both Instantaneous and Accumulated Conversions generate interrupts).
  • CCADC_IC (CC_ADC is enabled and Instantaneous Conversions generate interrupts).
  • CCADC_ACC (CC_ADC is enabled and Instantaneous Conversions generate interrupts).
  • CCADC_RCC (Regular Current Condition is enabled - Note that accumulator is cleared)

Note:
The enabling and disabling does _not_ modify the configuration of CC-ADC. It only enable and disable the mode and interrupts.

The Bandgap voltage refence must be enabled and calibrated to used the CC-ADC

Parameters:
mode Mode of operation.

Definition at line 127 of file ccadc.c.

References CCADC_ACC, CCADC_DISABLE, CCADC_IAC, CCADC_ICC, and CCADC_RCC.

Referenced by main().

00128 {
00130     while( CADCSRA & (1<<CADUB) ){
00131     };
00132     
00133     switch( mode ){
00134         case CCADC_DISABLE:
00135         // Disable CC-ADC and disable interrupts (and clear pending interrupts).
00136         CADCSRA = CADCSRA & ~( (1<<CADEN) | (1<<CADSE) );
00137         CADCSRB = (0<<CADACIE) | (0<<CADRCIE) | (0<<CADICIE) | (1<<CADACIF) | (1<<CADRCIF) | (1<<CADICIF);
00138         break;
00139         
00140         case CCADC_IAC:
00141         // Enable CC-ADC with both ICC and ACC interrupts running (and clear pending interrupts).
00142         CADCSRA = ( CADCSRA & ~(1<<CADSE) ) | (1<<CADEN);
00143         CADCSRB = (1<<CADACIE) | (0<<CADRCIE) | (1<<CADICIE) | (1<<CADACIF) | (1<<CADRCIF) | (1<<CADICIF);
00144         break;
00145         
00146         case CCADC_ICC:
00147         // Enable CC-ADC with ICC interrupt running  (and clear pending interrupts).
00148         CADCSRA = ( CADCSRA & ~(1<<CADSE) ) | (1<<CADEN);
00149         CADCSRB = (0<<CADACIE) | (0<<CADRCIE) | (1<<CADICIE) | (1<<CADACIF) | (1<<CADRCIF) | (1<<CADICIF);
00150         break;
00151         
00152         case CCADC_ACC:
00153         // Enable CC-ADC with ACC interrupt running  (and clear pending interrupts).
00154         CADCSRA = ( CADCSRA & ~(1<<CADSE) ) | (1<<CADEN);
00155         CADCSRB = (1<<CADACIE) | (0<<CADRCIE) | (0<<CADICIE) | (1<<CADACIF) | (1<<CADRCIF) | (1<<CADICIF);
00156         break;
00157         
00158         case CCADC_RCC:
00159         // Enable CC-ADC with RCC and ICC interrupts running (ICC due to RCC unable to wake up device from power save).
00160         CADCSRA = CADCSRA | (1<<CADSE) | (1<<CADEN);
00161         CADCSRB = (0<<CADACIE) | (1<<CADRCIE) | (1<<CADICIE) | (1<<CADACIF) | (1<<CADRCIF) | (1<<CADICIF);  
00162         break;
00163         
00164         default:
00165         break;
00166     }
00167 }


Variable Documentation

volatile int32_t CCADC_accResult

Definition at line 56 of file ccadc.c.

Referenced by Ccadc_Acc_ISR(), and CCADC_GetAccResult().

volatile int16_t CCADC_iccResult

Definition at line 57 of file ccadc.c.

Referenced by CCADC_GetIccResult(), and Ccadc_Icc_ISR().


Generated on Thu Nov 6 12:50:57 2008 for AVR352 - Using the CC-ADC by  doxygen 1.5.6