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.h.
#include <ioavr.h>
#include "config.h"


Go to the source code of this file.
Defines | |
| #define | ACC_SCALE (17) |
| Scaled version of ACC_STEP_SIZE (value is 10944 and can thus be considered a 14bit unsigned value). | |
| #define | ACC_STEP_SIZE_mA (ACC_STEP_SIZE_uV/SHUNT_RESISTANCE) |
| 2^x scaling; used to get best possible resolution when multiplied by a 18bit signed (CCADC result) without overflowing. | |
| #define | ACC_STEP_SIZE_mA_SCALED ( (int32_t)(ACC_STEP_SIZE_mA * (1ul<<ACC_SCALE) ) ) |
| #define | ACC_STEP_SIZE_uV (0.839233) |
| ACC steps size in mA (with a given SHUNT_RESISTANCE). | |
| #define | ACC_STEP_SIZE_uV_SCALED ( (int16_t)(ACC_STEP_SIZE_uV * (1ul<<ACC_SCALE) ) ) |
| Scaled version of ACC_STEP_SIZE (value is 10944 and can thus be considered a 14bit unsigned value). | |
| #define | CCADC_POLARITY_NEG (1<<CADPOL) |
| #define | CCADC_POLARITY_POS (0<<CADPOL) |
| #define | CONVERSION_COUNT 10 |
| Number of conversions to run. | |
| #define | ICC_SCALE (16) |
| Scaled version of ICC_STEP_SIZE. | |
| #define | ICC_STEP_SIZE_mA (ICC_STEP_SIZE_uV/SHUNT_RESISTANCE) |
| 2^x scaling; used to get best possible resolution with out overflowing 32 bit signed int when multiplied by 13 bit CC-ADC result: 16-bits | |
| #define | ICC_STEP_SIZE_mA_SCALED ( (int32_t)( ICC_STEP_SIZE_mA*(1ul<<ICC_SCALE) ) ) |
| 0.839233 uV per CC-ADC tick in Accumulated Current Conversion mode | |
| #define | ICC_STEP_SIZE_uV (26.855) |
| ICC steps size in mA (with a given SHUNT_RESISTANCE). | |
| #define | RCC_SCALE 8 |
| Same as RCC_STEP_SIZE_mA, but scaled by 2^8 to ease fixed point computations. | |
| #define | RCC_STEP_SIZE_mA RCC_STEP_SIZE_uV/SHUNT_RESISTANCE |
| 2^x scaling | |
| #define | RCC_STEP_SIZE_mA_SCALED ( (uint16_t)(RCC_STEP_SIZE_mA * (1<< RCC_SCALE))) |
| #define | RCC_STEP_SIZE_uV 26.8549 |
| Regular Current Condition level computation. | |
| #define | SHUNT_RESISTANCE (10) |
| < Shunt resistance in mOhm | |
Typedefs | |
| typedef enum CCADC_AccConversionPeriod_enum | CCADC_AccConvPeriod_t |
| CC-ADC Accumulated Current Conversion Periods - in milliseconds. | |
| typedef enum CCADC_modes_enum | CCADC_modes_t |
| CC-ADC operation modes. | |
| typedef enum CCADC_RccConversionPeriod_enum | CCADC_RccConvPeriod_t |
| CC-ADC Regular Current Sampling Intervals - in milliseconds. | |
Enumerations | |
| enum | CCADC_AccConversionPeriod_enum { ACCT_128 = ( (0<<CADAS1)|(0<<CADAS0) ), ACCT_256 = ( (0<<CADAS1)|(1<<CADAS0) ), ACCT_512 = ( (1<<CADAS1)|(0<<CADAS0) ), ACCT_1024 = ( (1<<CADAS1)|(1<<CADAS0) ) } |
| CC-ADC Accumulated Current Conversion Periods - in milliseconds. More... | |
| enum | CCADC_modes_enum { CCADC_DISABLE = 0, CCADC_IAC = 1, CCADC_ICC = 2, CCADC_ACC = 3, CCADC_RCC = 4 } |
| CC-ADC operation modes. More... | |
| enum | CCADC_RccConversionPeriod_enum { RCCI_256 = ( (0<<CADSI1)|(0<<CADSI0) ), RCCI_512 = ( (0<<CADSI1)|(1<<CADSI0) ), RCCI_1024 = ( (1<<CADSI1)|(0<<CADSI0) ), RCCI_2048 = ( (1<<CADSI1)|(1<<CADSI0) ) } |
| CC-ADC Regular Current Sampling Intervals - in milliseconds. More... | |
Functions | |
| 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. | |
| 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. | |
| void | CCADC_SetMode (CCADC_modes_t mode) |
| Selects mode of operation for the CC-ADC. | |
| #define ACC_SCALE (17) |
| #define ACC_STEP_SIZE_mA (ACC_STEP_SIZE_uV/SHUNT_RESISTANCE) |
| #define ACC_STEP_SIZE_mA_SCALED ( (int32_t)(ACC_STEP_SIZE_mA * (1ul<<ACC_SCALE) ) ) |
| #define ACC_STEP_SIZE_uV (0.839233) |
| #define ACC_STEP_SIZE_uV_SCALED ( (int16_t)(ACC_STEP_SIZE_uV * (1ul<<ACC_SCALE) ) ) |
| #define CCADC_POLARITY_POS (0<<CADPOL) |
| #define CONVERSION_COUNT 10 |
Number of conversions to run.
Definition at line 122 of file ccadc.h.
Referenced by CCADC_PolarityManager().
| #define ICC_STEP_SIZE_mA (ICC_STEP_SIZE_uV/SHUNT_RESISTANCE) |
| #define ICC_STEP_SIZE_mA_SCALED ( (int32_t)( ICC_STEP_SIZE_mA*(1ul<<ICC_SCALE) ) ) |
| #define ICC_STEP_SIZE_uV (26.855) |
| #define RCC_SCALE 8 |
Same as RCC_STEP_SIZE_mA, but scaled by 2^8 to ease fixed point computations.
Definition at line 113 of file ccadc.h.
Referenced by CCADC_Init().
| #define RCC_STEP_SIZE_mA RCC_STEP_SIZE_uV/SHUNT_RESISTANCE |
| #define RCC_STEP_SIZE_mA_SCALED ( (uint16_t)(RCC_STEP_SIZE_mA * (1<< RCC_SCALE))) |
| #define RCC_STEP_SIZE_uV 26.8549 |
| #define SHUNT_RESISTANCE (10) |
| typedef enum CCADC_AccConversionPeriod_enum CCADC_AccConvPeriod_t |
CC-ADC Accumulated Current Conversion Periods - in milliseconds.
| typedef enum CCADC_modes_enum CCADC_modes_t |
CC-ADC operation modes.
| typedef enum CCADC_RccConversionPeriod_enum CCADC_RccConvPeriod_t |
CC-ADC Regular Current Sampling Intervals - in milliseconds.
CC-ADC Accumulated Current Conversion Periods - in milliseconds.
| ACCT_128 | Equals to 16K (16384) ULP cycles. |
| ACCT_256 | Equals to 32K (32768) ULP cycles. |
| ACCT_512 | Equals to 64K (65536) ULP cycles. |
| ACCT_1024 | Equals to 128K (131072) ULP cycles. |
Definition at line 92 of file ccadc.h.
00092 { 00093 ACCT_128 = ( (0<<CADAS1)|(0<<CADAS0) ), 00094 ACCT_256 = ( (0<<CADAS1)|(1<<CADAS0) ), 00095 ACCT_512 = ( (1<<CADAS1)|(0<<CADAS0) ), 00096 ACCT_1024 = ( (1<<CADAS1)|(1<<CADAS0) ) 00097 }CCADC_AccConvPeriod_t;
| enum CCADC_modes_enum |
CC-ADC operation modes.
Definition at line 83 of file ccadc.h.
00083 { 00084 CCADC_DISABLE = 0, 00085 CCADC_IAC = 1, 00086 CCADC_ICC = 2, 00087 CCADC_ACC = 3, 00088 CCADC_RCC = 4 00089 } CCADC_modes_t;
CC-ADC Regular Current Sampling Intervals - in milliseconds.
| RCCI_256 | Equals to 32K (32768) ULP cycles. |
| RCCI_512 | Equals to 64K (65536) ULP cycles. |
| RCCI_1024 | Equals to 128K (131072) ULP cycles. |
| RCCI_2048 | Equals to 256K (262144) ULP cycles. |
Definition at line 100 of file ccadc.h.
00100 { 00101 RCCI_256 = ( (0<<CADSI1)|(0<<CADSI0) ), 00102 RCCI_512 = ( (0<<CADSI1)|(1<<CADSI0) ), 00103 RCCI_1024 = ( (1<<CADSI1)|(0<<CADSI0) ), 00104 RCCI_2048 = ( (1<<CADSI1)|(1<<CADSI0) ) 00105 }CCADC_RccConvPeriod_t;
| 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.
| 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).
| 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.
| 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 }
| 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.
| 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.
| 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.
| 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 }
| void CCADC_SetMode | ( | CCADC_modes_t | mode | ) |
Selects mode of operation for the CC-ADC.
Select between
The Bandgap voltage refence must be enabled and calibrated to used the CC-ADC
| 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 }
1.5.6