PDL for FM0+  Version1.0
Peripheral Driverl Library for FM0+
C:/pdl_v10/library/driver/adc/adc.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
00003 *
00004 * This software is owned and published by:
00005 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA  94088-3453 ("Spansion").
00006 *
00007 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
00008 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
00009 *
00010 * This software contains source code for use with Spansion
00011 * components. This software is licensed by Spansion to be adapted only
00012 * for use in systems utilizing Spansion components. Spansion shall not be
00013 * responsible for misuse or illegal use of this software for devices not
00014 * supported herein.  Spansion is providing this software "AS IS" and will
00015 * not be responsible for issues arising from incorrect user implementation
00016 * of the software.
00017 *
00018 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
00019 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
00020 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
00021 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
00022 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
00023 * WARRANTY OF NONINFRINGEMENT.
00024 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
00025 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
00026 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
00027 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
00028 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
00029 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
00030 * SAVINGS OR PROFITS,
00031 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
00032 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
00033 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
00034 * FROM, THE SOFTWARE.
00035 *
00036 * This software may be replicated in part or whole for the licensed use,
00037 * with the restriction that this Disclaimer and Copyright notice must be
00038 * included with each copy of this software, whether used in part or whole,
00039 * at all times.
00040 */
00041 /******************************************************************************/
00052 #ifndef __ADC_H__
00053 #define __ADC_H__
00054 
00055 /******************************************************************************/
00056 /* Include files                                                              */
00057 /******************************************************************************/
00058 #include "mcu.h"
00059 #include "pdl_user.h"
00060 
00061 #if (defined(PDL_PERIPHERAL_ADC_ACTIVE))
00062 
00063 /* C binding of definitions if building with C++ compiler */
00064 #ifdef __cplusplus
00065 extern "C"
00066 {
00067 #endif
00068 
00138 
00139 /******************************************************************************
00140  * Global definitions
00141  ******************************************************************************/
00142 #define stc_adcn_t   FM0P_ADC_TypeDef
00143 #define ADC0       (*((volatile stc_adcn_t *) FM0P_ADC0_BASE))
00144 #define ADC1       (*((volatile stc_adcn_t *) FM0P_ADC1_BASE))
00145 #define ADC2       (*((volatile stc_adcn_t *) FM0P_ADC2_BASE))
00146 
00147 #define PDL_ADC_READY_WAIT_COUNT 1000000u        ///< Time-out counter value for ADC ready bit polling
00148 
00149 #define ADC_INSTANCE_COUNT                                \
00150         (uint8_t)(PDL_PERIPHERAL_ENABLE_ADC0 == PDL_ON) + \
00151         (uint8_t)(PDL_PERIPHERAL_ENABLE_ADC1 == PDL_ON) + \
00152         (uint8_t)(PDL_PERIPHERAL_ENABLE_ADC2 == PDL_ON)
00153 
00154 #define ADC0_SCAN_FIFO_ADDR     (uint32_t)(&FM0P_ADC0->SCFD)
00155 #define ADC1_SCAN_FIFO_ADDR     (uint32_t)(&FM0P_ADC1->SCFD)
00156 #define ADC2_SCAN_FIFO_ADDR     (uint32_t)(&FM0P_ADC2->SCFD)
00157 
00158 /******************************************************************************
00159  * Global type definitions
00160  ******************************************************************************/
00161 typedef void (*func_ptr_adc_parg32_t)(volatile uint32_t* pu32Argument);
00162 
00167 typedef enum en_adc_scan_mode
00168 {
00169     ScanSingleConversion  = 0,        
00170     ScanRepeatConversion  = 1         
00171 } en_adc_scan_mode_t;
00172 
00177 typedef enum en_adc_sample_time_n
00178 {
00179     Value1        = 0,    
00180     Value4        = 1,    
00181     Value8        = 2,    
00182     Value16       = 3,    
00183     Value32       = 4,    
00184     Value64       = 5,    
00185     Value128      = 6,    
00186     Value256      = 7,    
00187 } en_adc_sample_time_n_t;
00188 
00193 typedef enum en_adc_fifo_status
00194 {
00195     AdcFifoEmpty     = 0,    
00196     AdcFifoFilled    = 1,    
00197     AdcFifoFull      = 2,    
00198     AdcFifoOverrun   = 3,    
00199     AdcFifoError     = 4     
00200 } en_adc_fifo_status_t;
00201 
00207 typedef enum en_adc_timer_select
00208 {
00209     AdcNoTimer =  0,   
00210     AdcMft     =  1,   
00211     AdcBt0     =  2,   
00212     AdcBt1     =  3,   
00213     AdcBt2     =  4,   
00214     AdcBt3     =  5,   
00215     AdcBt4     =  6,   
00216     AdcBt5     =  7,   
00217     AdcBt6     =  8,   
00218     AdcBt7     =  9,   
00219 
00220 } en_adc_timer_select_t;
00221 
00226 typedef struct stc_ad_channel_list
00227 {
00228     union unAdcChannels
00229     {
00230         uint32_t u32AD_CHn;
00231         struct stcChannelBits
00232         {
00233             uint32_t AD_CH_0  : 1 ;
00234             uint32_t AD_CH_1  : 1 ;
00235             uint32_t AD_CH_2  : 1 ;
00236             uint32_t AD_CH_3  : 1 ;
00237             uint32_t AD_CH_4  : 1 ;
00238             uint32_t AD_CH_5  : 1 ;
00239             uint32_t AD_CH_6  : 1 ;
00240             uint32_t AD_CH_7  : 1 ;
00241             uint32_t AD_CH_8  : 1 ;
00242             uint32_t AD_CH_9  : 1 ;
00243             uint32_t AD_CH_10 : 1 ;
00244             uint32_t AD_CH_11 : 1 ;
00245             uint32_t AD_CH_12 : 1 ;
00246             uint32_t AD_CH_13 : 1 ;
00247             uint32_t AD_CH_14 : 1 ;
00248             uint32_t AD_CH_15 : 1 ;
00249             uint32_t AD_CH_16 : 1 ;
00250             uint32_t AD_CH_17 : 1 ;
00251             uint32_t AD_CH_18 : 1 ;
00252             uint32_t AD_CH_19 : 1 ;
00253             uint32_t AD_CH_20 : 1 ;
00254             uint32_t AD_CH_21 : 1 ;
00255             uint32_t AD_CH_22 : 1 ;
00256             uint32_t AD_CH_23 : 1 ;
00257             uint32_t AD_CH_24 : 1 ;
00258             uint32_t AD_CH_25 : 1 ;
00259             uint32_t AD_CH_26 : 1 ;
00260             uint32_t AD_CH_27 : 1 ;
00261             uint32_t AD_CH_28 : 1 ;
00262             uint32_t AD_CH_29 : 1 ;
00263             uint32_t AD_CH_30 : 1 ;
00264             uint32_t AD_CH_31 : 1 ;
00265         };
00266     };
00267 } stc_ad_channel_list_t ;
00268 
00273 typedef struct stc_adc_scan
00274 {
00275     en_adc_scan_mode_t            enScanMode;                     
00276     stc_ad_channel_list_t         u32ScanCannelSelect;            
00277     boolean_t                     bScanTimerStartEnable;          
00278     en_adc_timer_select_t         enScanTimerTrigger;             
00279     uint8_t                       u8ScanFifoDepth;                
00280 
00281 }stc_adc_scan_t;
00282 
00287 typedef struct stc_adc_prio
00288 {
00289     boolean_t                     bPrioExtTrigStartEnable;        
00290     boolean_t                     bPrioTimerStartEnable;          
00291     en_adc_timer_select_t         enPrioTimerTrigger;             
00292     uint8_t                       u8PrioFifoDepth;                
00293     uint8_t                       u8PrioLevel1AnalogChSel;        
00294     uint8_t                       u8PrioLevel2AnalogChSel;        
00295 
00296 }stc_adc_prio_t;
00297 
00302 typedef struct stc_adc_comapre
00303 {
00304     uint16_t                      u16CompareValue;                
00305     boolean_t                     bCompareAllChannels;            
00306     uint8_t                       u8CompareChannel;               
00307     boolean_t                     bCompIrqEqualGreater;           
00308 
00309 }stc_adc_comapre_t;
00310 
00315 typedef struct stc_adc_range_compare
00316 {
00317     uint16_t                      u16UpperLimitRangeValue;        
00318     uint16_t                      u16LowerLimitRangeValue;        
00319     uint8_t                       u8RangeCountValue;              
00320     boolean_t                     bWithinRange;                   
00321     boolean_t                     bRangeCompareAllChannels;       
00322     uint8_t                       u8RangeComapreChannel;          
00323 
00324 }stc_adc_range_compare_t;
00325 
00330 typedef struct stc_adc_config
00331 {
00332     boolean_t                     bLsbAlignment;                  
00333     stc_ad_channel_list_t         u32SamplingTimeSelect;          
00334     en_adc_sample_time_n_t        enSamplingTimeN0;               
00335     uint8_t                       u8SamplingTime0;                
00336     en_adc_sample_time_n_t        enSamplingTimeN1;               
00337     uint8_t                       u8SamplingTime1;                
00338     uint8_t                       u8SamplingMultiplier;           
00339     uint8_t                       u8EnableTime;                   
00340 
00341     boolean_t                     bScanInit;                      
00342     stc_adc_scan_t                stcScanInit;                    
00343 
00344     boolean_t                     bPrioInit;                      
00345     stc_adc_prio_t                stcPrioInit;                    
00346 
00347     boolean_t                     bComparisonEnable;              
00348     stc_adc_comapre_t             stcComparisonInit;              
00349 
00350     boolean_t                     bRangeComparisonEnable;         
00351     stc_adc_range_compare_t       stcRangeComparisonInit;         
00352 
00353 } stc_adc_config_t;
00354 
00359 typedef struct stc_adc_int_sel
00360 {
00361     boolean_t                     bScanIrq;                 
00362     boolean_t                     bPrioIrq;                 
00363     boolean_t                     bFifoOverrunIrq;          
00364     boolean_t                     bComparisonIrq;           
00365     boolean_t                     bRangeComparisonIrq;      
00366 
00367 }stc_adc_int_sel_t;
00368 
00373 typedef enum en_scan_start_cause
00374 {
00375     ScanStartBySw        = 0,  
00376     ScanStartByTimer     = 1,  
00377 
00378 }en_scan_start_cause_t;
00379 
00384 typedef struct stc_scan_fifo_info
00385 {
00386     uint16_t                u16Data;      
00387     boolean_t               bInValid;     
00388     en_scan_start_cause_t   enStartCause; 
00389     uint8_t                 u8Ch;         
00390 
00391 }stc_scan_fifo_info_t;
00392 
00397 typedef enum en_prio_start_cause
00398 {
00399     PrioStartBySw         = 0,   
00400     PrioStartByTimer      = 1,   
00401     PrioStartByExtTrig    = 2,   
00402 
00403 }en_prio_start_cause_t;
00404 
00409 typedef struct stc_prio_fifo_info
00410 {
00411     uint16_t                u16Data;       
00412     boolean_t               bInValid;      
00413     en_prio_start_cause_t   enStartCause;  
00414     uint8_t                 u8Ch;          
00415 
00416 }stc_prio_fifo_info_t;
00417 
00422 typedef struct stc_adc_int_callback
00423 {
00424     func_ptr_adc_parg32_t         pfnScanCallback;              
00425     func_ptr_adc_parg32_t         pfnPrioCallback;              
00426     func_ptr_t                    pfnScanErrorCallback;         
00427     func_ptr_t                    pfnPrioErrorCallback;         
00428     func_ptr_t                    pfnComparisonCallback;        
00429     func_ptr_t                    pfnRangeComparisonCallback;   
00430 
00431 }stc_adc_int_callback_t;
00432 
00437 typedef enum en_adc_status
00438 {
00439     ScanFifoEmptyStatus         = 0,  
00440     ScanFifoFullStatus          = 1,  
00441     PrioFifoEmptyStatus         = 2,  
00442     PrioFifoFullStatus          = 3,  
00443     Prio2PendingStatus          = 4,  
00444     PrioStatus                  = 5,  
00445     ScanStatus                  = 6,  
00446     RangeThresholdExcessFlag    = 7,  
00447 
00448 }en_adc_status_t;
00449 
00454 typedef enum en_adc_int
00455 {
00456     AdcScanInt                = 0,   
00457     AdcPrioInt                = 1,   
00458     AdcScanFifoOverrunInt     = 2,   
00459     AdcPrioFifoOverrunInt     = 3,   
00460     AdcComparisonInt          = 4,   
00461     AdcRangeComparisonInt     = 5,   
00462 
00463 }en_adc_int_t;
00464 
00466 typedef enum en_adc_instance_index
00467 {
00468   #if (PDL_PERIPHERAL_ENABLE_ADC0 == PDL_ON)
00469     AdcInstanceIndexAdc0,
00470   #endif
00471   #if (PDL_PERIPHERAL_ENABLE_ADC1 == PDL_ON)
00472     AdcInstanceIndexAdc1,
00473   #endif
00474   #if (PDL_PERIPHERAL_ENABLE_ADC2 == PDL_ON)
00475     AdcInstanceIndexAdc2,
00476   #endif
00477     AdcInstanceIndexMax
00478 } en_adc_instance_index_t;
00479 
00481 typedef struct stc_adc_intern_data
00482 {
00483   func_ptr_adc_parg32_t   pfnScanCallback;              
00484   func_ptr_adc_parg32_t   pfnPrioCallback;              
00485   func_ptr_t              pfnScanErrorCallback;                 
00486   func_ptr_t              pfnPrioErrorCallback;                 
00487   func_ptr_t              pfnComparisonCallback;                
00488   func_ptr_t              pfnRangeComparisonCallback;           
00489 } stc_adc_intern_data_t;
00490 
00492 typedef struct stc_adc_instance_data
00493 {
00494   volatile stc_adcn_t*  pstcInstance;  
00495   stc_adc_intern_data_t stcInternData; 
00496 } stc_adc_instance_data_t;
00497 
00498 /******************************************************************************/
00499 /* Global variable declarations ('extern', definition in C source)            */
00500 /******************************************************************************/
00501 
00503 extern stc_adc_instance_data_t m_astcAdcInstanceDataLut[ADC_INSTANCE_COUNT];
00504 
00505 /******************************************************************************/
00506 /* Global function prototypes ('extern', definition in C source)              */
00507 /******************************************************************************/
00508 /* Init/De-Init */
00509 en_result_t               Adc_Init( volatile stc_adcn_t* pstcAdc,
00510                                     stc_adc_config_t*    pstcConfig );
00511 en_result_t               Adc_DeInit( volatile stc_adcn_t* pstcAdc );
00512 
00513 #if (PDL_INTERRUPT_ENABLE_ADC0 == PDL_ON) || \
00514     (PDL_INTERRUPT_ENABLE_ADC1 == PDL_ON) || \
00515     (PDL_INTERRUPT_ENABLE_ADC2 == PDL_ON)
00516 /* Int Enable/Disable */
00517 en_result_t               Adc_EnableInt(volatile stc_adcn_t* pstcAdc,
00518                                         stc_adc_int_sel_t* pstcIntSel,
00519                                         stc_adc_int_callback_t* pstcIntCallback);
00520 en_result_t               Adc_DisableInt(volatile stc_adcn_t* pstcAdc,
00521                                          stc_adc_int_sel_t* pstcIntSel);
00522 #endif
00523 /* Int Flag get/clear */
00524 boolean_t                 Adc_GetIntFlag(volatile stc_adcn_t* pstcAdc,
00525                                          en_adc_int_t enInt);
00526 en_result_t               Adc_ClrIntFlag(volatile stc_adcn_t* pstcAdc,
00527                                          en_adc_int_t enInt);
00528 
00529 /* Function enable/disable */
00530 en_result_t               Adc_Enable( volatile stc_adcn_t* pstcAdc );
00531 en_result_t               Adc_Disable( volatile stc_adcn_t* pstcAdc );
00532 en_result_t               Adc_SwTriggerScan( volatile stc_adcn_t* pstcAdc );
00533 en_result_t               Adc_StopScanRepeat( volatile stc_adcn_t* pstcAdc );
00534 en_result_t               Adc_SwTriggerPrio( volatile stc_adcn_t* pstcAdc );
00535 en_result_t               Adc_ForceStop( volatile stc_adcn_t* pstcAdc );
00536 /* Status */
00537 boolean_t                 Adc_GetStatus(volatile stc_adcn_t* pstcAdc,
00538                                         en_adc_status_t enStatus);
00539 /* FIFO data get/clear */
00540 uint32_t                  Adc_GetScanFifo(volatile stc_adcn_t* pstcAdc);
00541 en_result_t               Adc_ClrScanFifo(volatile stc_adcn_t* pstcAdc);
00542 en_result_t               Adc_ExtractScanFifo(volatile stc_adcn_t* pstcAdc,
00543                                               uint32_t u32FifoData,
00544                                               stc_scan_fifo_info_t* pstcFifo);
00545 uint32_t                  Adc_GetPrioFifo(volatile stc_adcn_t* pstcAdc);
00546 en_result_t               Adc_ClrPrioFifo(volatile stc_adcn_t* pstcAdc);
00547 en_result_t               Adc_ExtractPrioFifo(volatile stc_adcn_t* pstcAdc,
00548                                               uint32_t u32FifoData,
00549                                               stc_prio_fifo_info_t* pstcFifo);
00550 /* ADC IRQ */
00551 void                      AdcIrqHandler( volatile stc_adcn_t*   pstcAdc,
00552                                          stc_adc_intern_data_t* pstcAdcInternData );
00554 #ifdef __cplusplus
00555 }
00556 #endif
00557 
00558 #endif // #if (defined(PDL_PERIPHERAL_ADC_ACTIVE))
00559 
00560 #endif /* __ADC_H__ */
00561 /******************************************************************************/
00562 /* EOF (not truncated)                                                        */
00563 /******************************************************************************/