///////////////////////////////////////////////////////////////////////////////
//                                                                            /
// IAR 8051 C/C++ Compiler V7.30B/W32                   19/May/2010  09:38:40 /
// Copyright 2004-2007 IAR Systems. All rights reserved.                      /
//                                                                            /
//    Core               =  plain                                             /
//    Code model         =  banked                                            /
//    Data model         =  large                                             /
//    Calling convention =  xdata reentrant                                   /
//    Constant location  =  data                                              /
//    Dptr setup         =  1,16                                              /
//    Source file        =  C:\Texas Instruments\ZStack-1.4.3-1.2.1\Component /
//                          s\hal\target\CC2430EB\hal_adc.c                   /
//    Command line       =  -f "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Proje /
//                          cts\zstack\Samples\Location\CC2430DB\..\..\..\Too /
//                          ls\CC2430DB\f8wCoord.cfg" (-DCPU32MHZ             /
//                          -DFORCE_MAC_NEAR -DROOT=__near_func               /
//                          -DMAC_CFG_APP_PENDING_QUEUE=TRUE                  /
//                          -DZDO_COORDINATOR -DRTR_NWK -DBLINK_LEDS          /
//                          "-DCONST=const __code" -DGENERIC=__generic) -f    /
//                          "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Projects /
//                          \zstack\Samples\Location\CC2430DB\..\..\..\Tools\ /
//                          CC2430DB\f8wConfig.cfg" (-DSECURE=1               /
//                          -DDEFAULT_CHANLIST=0x04000000                     /
//                          -DZDAPP_CONFIG_PAN_ID=0x00FF                      /
//                          -DNWK_START_DELAY=100 -DEXTENDED_JOINING_RANDOM_M /
//                          ASK=0x007F -DBEACON_REQUEST_DELAY=100             /
//                          -DBEACON_REQ_DELAY_MASK=0x00FF                    /
//                          -DROUTE_EXPIRY_TIME=30 -DAPSC_ACK_WAIT_DURATION_P /
//                          OLLED=3000 -DNWK_INDIRECT_MSG_TIMEOUT=7           /
//                          -DMAX_RREQ_ENTRIES=10 -DAPSC_MAX_FRAME_RETRIES=3  /
//                          -DNWK_MAX_DATA_RETRIES=2                          /
//                          -DMAX_POLL_FAILURE_RETRIES=2 -DMAX_BCAST=9        /
//                          -DAPS_MAX_GROUPS=16 -DMAX_RTG_ENTRIES=20          /
//                          -DNWK_MAX_BINDING_ENTRIES=10                      /
//                          -DMAX_BINDING_CLUSTER_IDS=5                       /
//                          "-DDEFAULT_KEY={0x02, 0x03, 0x05, 0x07, 0x09,     /
//                          0x0B, 0x0D, 0x0F, 0x00, 0x02, 0x04, 0x06, 0x08,   /
//                          0x0A, 0x0C, 0x0D}" -DRFD_RCVC_ALWAYS_ON=FALSE     /
//                          -DPOLL_RATE=1000 -DQUEUED_POLL_RATE=100           /
//                          -DRESPONSE_POLL_RATE=100) -DREJOIN_POLL_RATE=440  /
//                          "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Componen /
//                          ts\hal\target\CC2430EB\hal_adc.c" -D CC2430EB -D  /
//                          ZTOOL_P1 -D LOCATION_DONGLE -D MT_TASK -D         /
//                          xNV_RESTORE -D INT_HEAP_LEN=4608 -D               /
//                          HAL_UART_BIG_TX_BUF=1 -D                          /
//                          SPI_MGR_DEFAULT_MAX_TX_BUFF=1024 -D               /
//                          SPI_MGR_DEFAULT_OVERFLOW=1 -lC "C:\Texas          /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\LocDongle\List\" -lA      /
//                          "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Projects /
//                          \zstack\Samples\Location\CC2430DB\LocDongle\List\ /
//                          " --diag_suppress Pe001,Pa010 --diag_remark       /
//                          pe550 -o "C:\Texas Instruments\ZStack-1.4.3-1.2.1 /
//                          \Projects\zstack\Samples\Location\CC2430DB\LocDon /
//                          gle\Obj\" -e --require_prototypes -z9             /
//                          --no_code_motion --debug --core=plain             /
//                          --dptr=16,1 --data_model=large                    /
//                          --code_model=banked --calling_convention=xdata_re /
//                          entrant --place_constants=data --nr_virtual_regs  /
//                          8 -I "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Pro /
//                          jects\zstack\Samples\Location\CC2430DB\" -I       /
//                          "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Projects /
//                          \zstack\Samples\Location\CC2430DB\..\SOURCE\" -I  /
//                          "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Projects /
//                          \zstack\Samples\Location\CC2430DB\..\..\..\ZMAIN\ /
//                          TI2430DB\" -I "C:\Texas                           /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\WXL\INCLUDE\" /
//                           -I "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Proj /
//                          ects\zstack\Samples\Location\CC2430DB\..\..\..\.. /
//                          \..\COMPONENTS\MT\" -I "C:\Texas                  /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \HAL\INCLUDE\" -I "C:\Texas                       /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \HAL\TARGET\CC2430EB\" -I "C:\Texas               /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \OSAL\MCU\CCSOC\" -I "C:\Texas                    /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \OSAL\INCLUDE\" -I "C:\Texas                      /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \STACK\AF\" -I "C:\Texas                          /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \STACK\NWK\" -I "C:\Texas                         /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \STACK\SEC\" -I "C:\Texas                         /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \STACK\SYS\" -I "C:\Texas                         /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \STACK\ZDO\" -I "C:\Texas                         /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \ZMAC\F8W\" -I "C:\Texas                          /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \ZMAC\" -I "C:\Texas Instruments\ZStack-1.4.3-1.2 /
//                          .1\Projects\zstack\Samples\Location\CC2430DB\..\. /
//                          .\..\..\..\COMPONENTS\SERVICES\SADDR\" -I         /
//                          "C:\Texas Instruments\ZStack-1.4.3-1.2.1\Projects /
//                          \zstack\Samples\Location\CC2430DB\..\..\..\..\..\ /
//                          COMPONENTS\SERVICES\SDATA\" -I "C:\Texas          /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \MAC\INCLUDE\" -I "C:\Texas                       /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \MAC\HIGH_LEVEL\" -I "C:\Texas                    /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \MAC\LOW_LEVEL\SRF03\" -I "C:\Texas               /
//                          Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Sa /
//                          mples\Location\CC2430DB\..\..\..\..\..\COMPONENTS /
//                          \MAC\LOW_LEVEL\SRF03\SINGLE_CHIP\" -I             /
//                          "C:\Program Files\IAR Systems\Embedded Workbench  /
//                          4.0 Evaluation version\8051\INC\" -I "C:\Program  /
//                          Files\IAR Systems\Embedded Workbench 4.0          /
//                          Evaluation version\8051\INC\CLIB\"                /
//    List file          =  C:\Texas Instruments\ZStack-1.4.3-1.2.1\Projects\ /
//                          zstack\Samples\Location\CC2430DB\LocDongle\List\h /
//                          al_adc.s51                                        /
//                                                                            /
//                                                                            /
///////////////////////////////////////////////////////////////////////////////

        NAME hal_adc

        RTMODEL "__calling_convention", "xdata_reentrant"
        RTMODEL "__code_model", "banked"
        RTMODEL "__core", "plain"
        RTMODEL "__data_model", "large"
        RTMODEL "__dptr_size", "16"
        RTMODEL "__extended_stack", "disabled"
        RTMODEL "__location_for_constants", "data"
        RTMODEL "__number_of_dptrs", "1"
        RTMODEL "__rt_version", "1"

        RSEG DOVERLAY:DATA:NOROOT(0)
        RSEG IOVERLAY:IDATA:NOROOT(0)
        RSEG ISTACK:IDATA:NOROOT(0)
        RSEG PSTACK:XDATA:NOROOT(0)
        RSEG XSTACK:XDATA:NOROOT(0)

        EXTERN ?ALLOC_XSTACK8
        EXTERN ?BANKED_ENTER_XDATA
        EXTERN ?BANKED_LEAVE_XDATA
        EXTERN ?BDISPATCH
        EXTERN ?BRET
        EXTERN ?DEALLOC_XSTACK8
        EXTERN ?SS_SHR
        EXTERN ?UC_SWITCH_DENSE
        EXTERN ?V0
        EXTERN ?XSP

        PUBLIC ??HalAdcCheckVdd?relay
        FUNCTION ??HalAdcCheckVdd?relay,0203H
        PUBLIC ??HalAdcInit?relay
        FUNCTION ??HalAdcInit?relay,0203H
        PUBLIC ??HalAdcRead?relay
        FUNCTION ??HalAdcRead?relay,0203H
        PUBWEAK ADCCFG
        PUBWEAK ADCCON1
        PUBWEAK ADCCON2
        PUBWEAK ADCCON3
        PUBWEAK ADCH
        PUBWEAK ADCL
        PUBLIC HalAdcCheckVdd
        FUNCTION HalAdcCheckVdd,0203H
        ARGFRAME XSTACK, 0, STACK
        LOCFRAME ISTACK, 2, STACK
        PUBLIC HalAdcInit
        FUNCTION HalAdcInit,0203H
        ARGFRAME XSTACK, 0, STACK
        LOCFRAME ISTACK, 3, STACK
        LOCFRAME XSTACK, 1, STACK
        PUBLIC HalAdcRead
        FUNCTION HalAdcRead,080203H
        ARGFRAME XSTACK, 0, STACK
        LOCFRAME ISTACK, 1, STACK
        LOCFRAME XSTACK, 11, STACK
        PUBWEAK _A_TCON
        
        CFI Names cfiNames0
        CFI StackFrame CFA_SP SP DATA
        CFI StackFrame CFA_PSP16 PSP16 XDATA
        CFI StackFrame CFA_XSP16 XSP16 XDATA
        CFI StaticOverlayFrame CFA_IOVERLAY IOVERLAY
        CFI StaticOverlayFrame CFA_DOVERLAY DOVERLAY
        CFI Resource `PSW.CY`:1, `B.BR0`:1, `B.BR1`:1, `B.BR2`:1, `B.BR3`:1
        CFI Resource `B.BR4`:1, `B.BR5`:1, `B.BR6`:1, `B.BR7`:1, `VB.BR8`:1
        CFI Resource `VB.BR9`:1, `VB.BR10`:1, `VB.BR11`:1, `VB.BR12`:1
        CFI Resource `VB.BR13`:1, `VB.BR14`:1, `VB.BR15`:1, VB:8, B:8, A:8
        CFI Resource PSW:8, DPL0:8, DPH0:8, R0:8, R1:8, R2:8, R3:8, R4:8, R5:8
        CFI Resource R6:8, R7:8, V0:8, V1:8, V2:8, V3:8, V4:8, V5:8, V6:8, V7:8
        CFI Resource SP:8, PSPH:8, PSPL:8, PSP16:16, XSPH:8, XSPL:8, XSP16:16
        CFI VirtualResource ?RET:24
        CFI Resource ?BRET_EXT:8
        CFI VirtualResource ?RET_HIGH:8, ?RET_LOW:8
        CFI ResourceParts PSP16 PSPH, PSPL
        CFI ResourceParts XSP16 XSPH, XSPL
        CFI ResourceParts ?RET ?BRET_EXT, ?RET_HIGH, ?RET_LOW
        CFI EndNames cfiNames0
        
        CFI Common cfiCommon0 Using cfiNames0
        CFI CodeAlign 1
        CFI DataAlign -1
        CFI ReturnAddress ?RET CODE
        CFI CFA_DOVERLAY Used
        CFI CFA_IOVERLAY Used
        CFI CFA_SP SP+-3
        CFI CFA_PSP16 PSP16+0
        CFI CFA_XSP16 XSP16+0
        CFI `PSW.CY` SameValue
        CFI `B.BR0` SameValue
        CFI `B.BR1` SameValue
        CFI `B.BR2` SameValue
        CFI `B.BR3` SameValue
        CFI `B.BR4` SameValue
        CFI `B.BR5` SameValue
        CFI `B.BR6` SameValue
        CFI `B.BR7` SameValue
        CFI `VB.BR8` SameValue
        CFI `VB.BR9` SameValue
        CFI `VB.BR10` SameValue
        CFI `VB.BR11` SameValue
        CFI `VB.BR12` SameValue
        CFI `VB.BR13` SameValue
        CFI `VB.BR14` SameValue
        CFI `VB.BR15` SameValue
        CFI VB SameValue
        CFI B Undefined
        CFI A Undefined
        CFI PSW SameValue
        CFI DPL0 SameValue
        CFI DPH0 SameValue
        CFI R0 Undefined
        CFI R1 Undefined
        CFI R2 Undefined
        CFI R3 Undefined
        CFI R4 Undefined
        CFI R5 Undefined
        CFI R6 SameValue
        CFI R7 SameValue
        CFI V0 SameValue
        CFI V1 SameValue
        CFI V2 SameValue
        CFI V3 SameValue
        CFI V4 SameValue
        CFI V5 SameValue
        CFI V6 SameValue
        CFI V7 SameValue
        CFI PSPH Undefined
        CFI PSPL Undefined
        CFI XSPH Undefined
        CFI XSPL Undefined
        CFI ?RET Concat
        CFI ?BRET_EXT Frame(CFA_SP, 3)
        CFI ?RET_HIGH Frame(CFA_SP, 2)
        CFI ?RET_LOW Frame(CFA_SP, 1)
        CFI EndCommon cfiCommon0
        
HalAdcCheckVdd      SYMBOL "HalAdcCheckVdd"
??HalAdcCheckVdd?relay SYMBOL "?relay", HalAdcCheckVdd
HalAdcInit          SYMBOL "HalAdcInit"
??HalAdcInit?relay  SYMBOL "?relay", HalAdcInit
HalAdcRead          SYMBOL "HalAdcRead"
??HalAdcRead?relay  SYMBOL "?relay", HalAdcRead

// C:\Texas Instruments\ZStack-1.4.3-1.2.1\Components\hal\target\CC2430EB\hal_adc.c
//    1 /**************************************************************************************************
//    2   Filename:       hal_adc.c
//    3   Revised:        $Date: 2007-11-01 08:44:53 -0700 (Thu, 01 Nov 2007) $
//    4   Revision:       $Revision: 15821 $
//    5 
//    6   Description:    This file contains the interface to the HAL ADC.
//    7 
//    8 
//    9   Copyright 2006-2007 Texas Instruments Incorporated. All rights reserved.
//   10 
//   11   IMPORTANT: Your use of this Software is limited to those specific rights
//   12   granted under the terms of a software license agreement between the user
//   13   who downloaded the software, his/her employer (which must be your employer)
//   14   and Texas Instruments Incorporated (the "License").  You may not use this
//   15   Software unless you agree to abide by the terms of the License. The License
//   16   limits your use, and you acknowledge, that the Software may not be modified,
//   17   copied or distributed unless embedded on a Texas Instruments microcontroller
//   18   or used solely and exclusively in conjunction with a Texas Instruments radio
//   19   frequency transceiver, which is integrated into your product.  Other than for
//   20   the foregoing purpose, you may not use, reproduce, copy, prepare derivative
//   21   works of, modify, distribute, perform, display or sell this Software and/or
//   22   its documentation for any purpose.
//   23 
//   24   YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
//   25   PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//   26   INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 
//   27   NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
//   28   TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
//   29   NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
//   30   LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
//   31   INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
//   32   OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
//   33   OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
//   34   (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
//   35 
//   36   Should you have any questions regarding your right to use this Software,
//   37   contact Texas Instruments Incorporated at www.TI.com. 
//   38 **************************************************************************************************/
//   39 
//   40 /**************************************************************************************************
//   41  *                                           INCLUDES
//   42  **************************************************************************************************/
//   43 #include  "hal_mcu.h"

        ASEGN SFR_AN:DATA:NOROOT,088H
// union <unnamed> volatile __sfr _A_TCON
_A_TCON:
        DS 1

        ASEGN SFR_AN:DATA:NOROOT,0b4H
// unsigned char volatile __sfr ADCCON1
ADCCON1:
        DS 1

        ASEGN SFR_AN:DATA:NOROOT,0b5H
// unsigned char volatile __sfr ADCCON2
ADCCON2:
        DS 1

        ASEGN SFR_AN:DATA:NOROOT,0b6H
// unsigned char volatile __sfr ADCCON3
ADCCON3:
        DS 1

        ASEGN SFR_AN:DATA:NOROOT,0baH
// unsigned char volatile __sfr ADCL
ADCL:
        DS 1

        ASEGN SFR_AN:DATA:NOROOT,0bbH
// unsigned char volatile __sfr ADCH
ADCH:
        DS 1

        ASEGN SFR_AN:DATA:NOROOT,0f2H
// unsigned char volatile __sfr ADCCFG
ADCCFG:
        DS 1
//   44 #include  "hal_defs.h"
//   45 #include  "hal_types.h"
//   46 #include  "hal_adc.h"
//   47 
//   48 /**************************************************************************************************
//   49  *                                            CONSTANTS
//   50  **************************************************************************************************/
//   51 #define HAL_ADC_EOC         0x80    /* End of Conversion bit */
//   52 #define HAL_ADC_START       0x40    /* Starts Conversion */
//   53 
//   54 #define HAL_ADC_STSEL_EXT   0x00    /* External Trigger */
//   55 #define HAL_ADC_STSEL_FULL  0x10    /* Full Speed, No Trigger */
//   56 #define HAL_ADC_STSEL_T1C0  0x20    /* Timer1, Channel 0 Compare Event Trigger */
//   57 #define HAL_ADC_STSEL_ST    0x30    /* ADCCON1.ST =1 Trigger */
//   58 
//   59 #define HAL_ADC_RAND_NORM   0x00    /* Normal Operation */
//   60 #define HAL_ADC_RAND_LFSR   0x04    /* Clock LFSR */
//   61 #define HAL_ADC_RAND_SEED   0x08    /* Seed Modulator */
//   62 #define HAL_ADC_RAND_STOP   0x0c    /* Stop Random Generator */
//   63 #define HAL_ADC_RAND_BITS   0x0c    /* Bits [3:2] */
//   64 
//   65 #define HAL_ADC_REF_125V    0x00    /* Internal 1.25V Reference */
//   66 #define HAL_ADC_REF_AIN7    0x40    /* AIN7 Reference */
//   67 #define HAL_ADC_REF_AVDD    0x80    /* AVDD_SOC Pin Reference */
//   68 #define HAL_ADC_REF_DIFF    0xc0    /* AIN7,AIN6 Differential Reference */
//   69 #define HAL_ADC_REF_BITS    0xc0    /* Bits [7:6] */
//   70 
//   71 #define HAL_ADC_DEC_064     0x00    /* Decimate by 64 : 8-bit resolution */
//   72 #define HAL_ADC_DEC_128     0x10    /* Decimate by 128 : 10-bit resolution */
//   73 #define HAL_ADC_DEC_256     0x20    /* Decimate by 256 : 12-bit resolution */
//   74 #define HAL_ADC_DEC_512     0x30    /* Decimate by 512 : 14-bit resolution */
//   75 #define HAL_ADC_DEC_BITS    0x30    /* Bits [5:4] */
//   76 
//   77 #define HAL_ADC_CHN_AIN0    0x00    /* AIN0 */
//   78 #define HAL_ADC_CHN_AIN1    0x01    /* AIN1 */
//   79 #define HAL_ADC_CHN_AIN2    0x02    /* AIN2 */
//   80 #define HAL_ADC_CHN_AIN3    0x03    /* AIN3 */
//   81 #define HAL_ADC_CHN_AIN4    0x04    /* AIN4 */
//   82 #define HAL_ADC_CHN_AIN5    0x05    /* AIN5 */
//   83 #define HAL_ADC_CHN_AIN6    0x06    /* AIN6 */
//   84 #define HAL_ADC_CHN_AIN7    0x07    /* AIN7 */
//   85 #define HAL_ADC_CHN_A0A1    0x08    /* AIN0,AIN1 */
//   86 #define HAL_ADC_CHN_A2A3    0x09    /* AIN2,AIN3 */
//   87 #define HAL_ADC_CHN_A4A5    0x0a    /* AIN4,AIN5 */
//   88 #define HAL_ADC_CHN_A6A7    0x0b    /* AIN6,AIN7 */
//   89 #define HAL_ADC_CHN_GND     0x0c    /* GND */
//   90 #define HAL_ADC_CHN_VREF    0x0d    /* Positive voltage reference */
//   91 #define HAL_ADC_CHN_TEMP    0x0e    /* Temperature sensor */
//   92 #define HAL_ADC_CHN_VDD3    0x0f    /* VDD/3 */
//   93 #define HAL_ADC_CHN_BITS    0x0f    /* Bits [3:0] */
//   94 
//   95 #define HAL_ADC_STSEL       HAL_ADC_STSEL_ST
//   96 #define HAL_ADC_RAND_GEN    HAL_ADC_RAND_STOP
//   97 #define HAL_ADC_REF_VOLT    HAL_ADC_REF_AVDD
//   98 #define HAL_ADC_DEC_RATE    HAL_ADC_DEC_064
//   99 #define HAL_ADC_SCHN        HAL_ADC_CHN_VDD3
//  100 #define HAL_ADC_ECHN        HAL_ADC_CHN_GND
//  101 
//  102 
//  103 /* Vdd limit values */

        RSEG CODE_C:CODE:REORDER:NOROOT(0)
        DATA
//  104 static __code const uint16 HalAdcVddLimit[] =
HalAdcVddLimit:
        DW 13980, 14854, 15728, 16601, 17475, 18349, 19223, 20097, 20970
//  105 {
//  106   0x369C,       /*  VDD Limit - 1.6v  */
//  107   0x3A06,       /*  VDD Limit - 1.7v  */
//  108   0x3D70,       /*  VDD Limit - 1.8v  */
//  109   0x40D9,       /*  VDD Limit - 1.9v  */
//  110   0x4443,       /*  VDD Limit - 2.0v  */
//  111   0x47AD,       /*  VDD Limit - 2.1v  */
//  112   0x4B17,       /*  VDD Limit - 2.2v  */
//  113   0x4E81,       /*  VDD Limit - 2.3v  */
//  114   0x51EA,       /*  VDD Limit - 2.4v  */
//  115 };
//  116 
//  117 
//  118 /**************************************************************************************************
//  119  *                                              MACROS
//  120  **************************************************************************************************/
//  121 #define HAL_ADC_CLR_EOC()   asm("PUSH A"); asm("MOV A,ADCL"); asm("MOV A,ADCH"); asm("POP A");
//  122 
//  123 /**************************************************************************************************
//  124  *                                            TYPEDEFS
//  125  **************************************************************************************************/
//  126 
//  127 /**************************************************************************************************
//  128  *                                         GLOBAL VARIABLES
//  129  **************************************************************************************************/
//  130 
//  131 /**************************************************************************************************
//  132  *                                          FUNCTIONS - API
//  133  **************************************************************************************************/
//  134 extern bool HalAdcCheckVdd (uint8 limit);
//  135 
//  136 /**************************************************************************************************
//  137  * @fn      HalAdcInit
//  138  *
//  139  * @brief   Initialize ADC Service
//  140  *
//  141  * @param   None
//  142  *
//  143  * @return  None
//  144  **************************************************************************************************/

        RSEG BANKED_CODE:CODE:NOROOT(0)
//  145 void HalAdcInit (void)
HalAdcInit:
        CFI Block cfiBlock0 Using cfiCommon0
        CFI Function HalAdcInit
//  146 {
        PUSH    DPL
        CFI DPL0 Frame(CFA_SP, 4)
        CFI CFA_SP SP+-4
        PUSH    DPH
        CFI DPH0 Frame(CFA_SP, 5)
        CFI CFA_SP SP+-5
        ; Saved register size: 2
        ; Auto size: 1
        MOV     A,#-0x1
        LCALL   ?ALLOC_XSTACK8
        CFI CFA_XSP16 add(XSP16, 1)
//  147 #if (HAL_ADC == TRUE)
//  148   volatile uint8  tmp;
//  149 
//  150   ADCCON1 = HAL_ADC_STSEL | HAL_ADC_RAND_GEN | 0x03;
        MOV     0xb4,#0x3f
//  151   ADCCON2 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_SCHN;
        MOV     0xb5,#-0x71
//  152   /*
//  153   *  After reset, the first ADC reading of the extra conversion always reads GND level.
//  154   *  We will do a few dummy conversions to bypass this bug.
//  155   */
//  156   tmp = ADCL;     /* read ADCL,ADCH to clear EOC */
        MOV     A,0xba
        MOV     DPL,?XSP + 0
        MOV     DPH,?XSP + 1
        MOVX    @DPTR,A
//  157   tmp = ADCH;
        MOV     A,0xbb
        MOVX    @DPTR,A
//  158   ADCCON3 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_ECHN;
        MOV     0xb6,#-0x74
//  159   while ((ADCCON1 & HAL_ADC_EOC) != HAL_ADC_EOC);   /* Wait for conversion */
??HalAdcInit_0:
        MOV     A,0xb4
        MOV     C,0xE0 /* A   */.7
        JNC     ??HalAdcInit_0
//  160   tmp = ADCL;     /* read ADCL,ADCH to clear EOC */
        MOV     A,0xba
        MOVX    @DPTR,A
//  161   tmp = ADCH;
        MOV     A,0xbb
        MOVX    @DPTR,A
//  162   ADCCON3 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_ECHN;
        MOV     0xb6,#-0x74
//  163   while ((ADCCON1 & HAL_ADC_EOC) != HAL_ADC_EOC);   /* Wait for conversion */
??HalAdcInit_1:
        MOV     A,0xb4
        MOV     C,0xE0 /* A   */.7
        JNC     ??HalAdcInit_1
//  164   tmp = ADCL;     /* read ADCL,ADCH to clear EOC */
        MOV     A,0xba
        MOVX    @DPTR,A
//  165   tmp = ADCH;
        MOV     A,0xbb
        MOVX    @DPTR,A
//  166 #endif
//  167 }
        MOV     A,#0x1
        LCALL   ?DEALLOC_XSTACK8
        CFI EndBlock cfiBlock0
        REQUIRE ?Subroutine0
        REQUIRE ADCCON1
        REQUIRE ADCCON2
        REQUIRE ADCCON3
        REQUIRE ADCL
        REQUIRE ADCH
        ; // Fall through to label ?Subroutine0

        RSEG BANKED_CODE:CODE:NOROOT(0)
?Subroutine0:
        CFI Block cfiBlock1 Using cfiCommon0
        CFI NoFunction
        CFI CFA_SP SP+-5
        CFI DPL0 Frame(CFA_SP, 4)
        CFI DPH0 Frame(CFA_SP, 5)
        POP     DPH
        CFI CFA_SP SP+-4
        CFI DPH0 SameValue
        POP     DPL
        CFI CFA_SP SP+-3
        CFI DPL0 SameValue
        LJMP    ?BRET
        CFI EndBlock cfiBlock1
//  168 
//  169 /**************************************************************************************************
//  170  * @fn      HalAdcRead
//  171  *
//  172  * @brief   Read the ADC based on given channel and resolution
//  173  *
//  174  * @param   channel - channel where ADC will be read
//  175  * @param   resolution - the resolution of the value
//  176  *
//  177  * @return  16 bit value of the ADC in offset binary format.
//  178  *          Note that the ADC is "bipolar", which means the GND (0V) level is mid-scale.
//  179  **************************************************************************************************/

        RSEG BANKED_CODE:CODE:NOROOT(0)
//  180 uint16 HalAdcRead (uint8 channel, uint8 resolution)
HalAdcRead:
        CFI Block cfiBlock2 Using cfiCommon0
        CFI Function HalAdcRead
//  181 {
        MOV     A,#-0xa
        LCALL   ?BANKED_ENTER_XDATA
        CFI DPH0 load(1, XDATA, add(CFA_XSP16, literal(-1)))
        CFI DPL0 load(1, XDATA, add(CFA_XSP16, literal(-2)))
        CFI ?BRET_EXT load(1, XDATA, add(CFA_XSP16, literal(-3)))
        CFI ?RET_HIGH load(1, XDATA, add(CFA_XSP16, literal(-4)))
        CFI ?RET_LOW load(1, XDATA, add(CFA_XSP16, literal(-5)))
        CFI R7 load(1, XDATA, add(CFA_XSP16, literal(-6)))
        CFI V1 load(1, XDATA, add(CFA_XSP16, literal(-7)))
        CFI V0 load(1, XDATA, add(CFA_XSP16, literal(-8)))
        CFI VB load(1, XDATA, add(CFA_XSP16, literal(-9)))
        CFI R6 load(1, XDATA, add(CFA_XSP16, literal(-10)))
        CFI CFA_SP SP+0
        CFI CFA_XSP16 add(XSP16, 10)
        ; Saved register size: 10
        ; Auto size: 1
        MOV     A,#-0x1
        LCALL   ?ALLOC_XSTACK8
        CFI CFA_XSP16 add(XSP16, 11)
//  182   int16  reading = 0;
//  183 
//  184 #if (HAL_ADC == TRUE)
//  185 
//  186   uint8   i, resbits;
//  187   uint8   adctemp;
//  188   volatile  uint8 tmp;
//  189   uint8  adcChannel = 1;
        MOV     R4,#0x1
//  190 
//  191   /*
//  192   * If Analog input channel is AIN0..AIN7, make sure corresponing P0 I/O pin is enabled.  The code
//  193   * does NOT disable the pin at the end of this function.  I think it is better to leave the pin
//  194   * enabled because the results will be more accurate.  Because of the inherent capacitance on the
//  195   * pin, it takes time for the voltage on the pin to charge up to its steady-state level.  If
//  196   * HalAdcRead() has to turn on the pin for every conversion, the results may show a lower voltage
//  197   * than actuality because the pin did not have time to fully charge.
//  198   */
//  199   if (channel < 8)
        MOV     A,R1
        CLR     C
        SUBB    A,#0x8
        JNC     ??HalAdcRead_0
//  200   {
//  201     for (i=0; i < channel; i++)
        MOV     R3,#0x0
        SJMP    ??HalAdcRead_1
//  202     {
//  203       adcChannel <<= 1;
??HalAdcRead_2:
        MOV     A,R4
        CLR     C
        RLC     A
        MOV     R4,A
//  204     }
        INC     R3
??HalAdcRead_1:
        MOV     A,R3
        CLR     C
        SUBB    A,R1
        JC      ??HalAdcRead_2
//  205   }
//  206 
//  207   /* Enable channel */
//  208   ADCCFG |= adcChannel;
??HalAdcRead_0:
        MOV     A,R4
        ORL     A,0xf2
        MOV     0xf2,A
//  209 
//  210   /* Convert resolution to decimation rate */
//  211   switch (resolution)
        MOV     A,R2
        LCALL   ?UC_SWITCH_DENSE
`?<Jumptable for HalAdcRead>_0`:
        DATA
        DB        1
        DB        2
        DW        ??HalAdcRead_3
        DW        ??HalAdcRead_4
        DW        ??HalAdcRead_5
        DW        ??HalAdcRead_6
        CODE
//  212   {
//  213     case HAL_ADC_RESOLUTION_8:
//  214       resbits = HAL_ADC_DEC_064;
??HalAdcRead_4:
        MOV     R3,#0x0
        SJMP    ??HalAdcRead_7
//  215       break;
//  216     case HAL_ADC_RESOLUTION_10:
//  217       resbits = HAL_ADC_DEC_128;
??HalAdcRead_5:
        MOV     R3,#0x10
        SJMP    ??HalAdcRead_7
//  218       break;
//  219     case HAL_ADC_RESOLUTION_12:
//  220       resbits = HAL_ADC_DEC_256;
??HalAdcRead_6:
        MOV     R3,#0x20
        SJMP    ??HalAdcRead_7
//  221       break;
//  222     case HAL_ADC_RESOLUTION_14:
//  223     default:
//  224       resbits = HAL_ADC_DEC_512;
??HalAdcRead_3:
        MOV     R3,#0x30
//  225       break;
//  226   }
//  227 
//  228   /* read ADCL,ADCH to clear EOC */
//  229   tmp = ADCL;
??HalAdcRead_7:
        MOV     A,0xba
        MOV     DPL,?XSP + 0
        MOV     DPH,?XSP + 1
        MOVX    @DPTR,A
//  230   tmp = ADCH;
        MOV     A,0xbb
        MOVX    @DPTR,A
//  231 
//  232   /* Setup Sample */
//  233   adctemp = ADCCON3;
        MOV     A,0xb6
//  234   adctemp &= ~(HAL_ADC_CHN_BITS | HAL_ADC_DEC_BITS | HAL_ADC_REF_BITS);
//  235   adctemp |= channel | resbits | HAL_ADC_REF_VOLT;
//  236 
//  237   /* writing to this register starts the extra conversion */
//  238   ADCCON3 = adctemp;
        MOV     A,R3
        ORL     A,R1
        ORL     A,#0x80
        MOV     0xb6,A
//  239 
//  240   /* Wait for the conversion to be done */
//  241   while (!(ADCCON1 & HAL_ADC_EOC));
??HalAdcRead_8:
        MOV     A,0xb4
        MOV     C,0xE0 /* A   */.7
        JNC     ??HalAdcRead_8
//  242 
//  243   /* Disable channel after done conversion */
//  244   ADCCFG &= ~adcChannel;
        MOV     A,R4
        CPL     A
        ANL     A,0xf2
        MOV     0xf2,A
//  245 
//  246   /* Read the result */
//  247   reading = (int16) (ADCL);
        MOV     R3,0xba
        MOV     DPL,R3
        MOV     R0,DPL
//  248   reading |= (int16) (ADCH << 8);
        MOV     A,0xbb
        MOV     R1,A
//  249 
//  250   /* Treat small negative as 0 */
//  251   if (reading < 0)
        CLR     C
        SUBB    A,#0x0
        MOV     C,0xD0 /* PSW */.2
        XRL     A,PSW
        RLC     A
        JNC     ??HalAdcRead_9
//  252     reading = 0;
        MOV     R0,#0x0
        MOV     R1,#0x0
//  253 
//  254   switch (resolution)
??HalAdcRead_9:
        MOV     A,R2
        LCALL   ?UC_SWITCH_DENSE
`?<Jumptable for HalAdcRead>_1`:
        DATA
        DB        1
        DB        2
        DW        ??HalAdcRead_10
        DW        ??HalAdcRead_11
        DW        ??HalAdcRead_12
        DW        ??HalAdcRead_13
        CODE
//  255   {
//  256     case HAL_ADC_RESOLUTION_8:
//  257       reading >>= 8;
??HalAdcRead_11:
        MOV     ?V0 + 0,R0
        MOV     ?V0 + 1,R1
        MOV     A,#0x8
        SJMP    ??HalAdcRead_14
//  258       break;
//  259     case HAL_ADC_RESOLUTION_10:
//  260       reading >>= 6;
??HalAdcRead_12:
        MOV     ?V0 + 0,R0
        MOV     ?V0 + 1,R1
        MOV     A,#0x6
        SJMP    ??HalAdcRead_14
//  261       break;
//  262     case HAL_ADC_RESOLUTION_12:
//  263       reading >>= 4;
??HalAdcRead_13:
        MOV     ?V0 + 0,R0
        MOV     ?V0 + 1,R1
        MOV     A,#0x4
??HalAdcRead_14:
        MOV     R0,#?V0 + 0
        LCALL   ?SS_SHR
        MOV     R0,?V0 + 0
        MOV     R1,?V0 + 1
//  264       break;
//  265     case HAL_ADC_RESOLUTION_14:
//  266     default:
//  267     break;
//  268   }
//  269 #endif
//  270 
//  271   return ((uint16)reading);
??HalAdcRead_10:
        MOV     A,R0
        MOV     R2,A
        MOV     A,R1
        MOV     R3,A
        MOV     A,#0x1
        LCALL   ?DEALLOC_XSTACK8
        CFI CFA_XSP16 add(XSP16, 10)
        MOV     R7,#0x2
        LJMP    ?BANKED_LEAVE_XDATA
        CFI EndBlock cfiBlock2
        REQUIRE ADCCON1
        REQUIRE ADCCON3
        REQUIRE ADCL
        REQUIRE ADCH
        REQUIRE ADCCFG
//  272 }
//  273 
//  274 /**************************************************************************************************
//  275  * @fn      HalAdcCheckVdd
//  276  *
//  277  * @brief   Check the Vdd and return TRUE if it greater than or equal the limit
//  278  *
//  279  * @param   limit - limit that needs to be checked with the Vdd
//  280  *
//  281  * @return  TRUE if Vdd >= limit, FALSE otherwise
//  282  *
//  283  **************************************************************************************************/

        RSEG BANKED_CODE:CODE:NOROOT(0)
//  284 bool HalAdcCheckVdd (uint8 limit)
HalAdcCheckVdd:
        CFI Block cfiBlock3 Using cfiCommon0
        CFI Function HalAdcCheckVdd
//  285 {
        PUSH    DPL
        CFI DPL0 Frame(CFA_SP, 4)
        CFI CFA_SP SP+-4
        PUSH    DPH
        CFI DPH0 Frame(CFA_SP, 5)
        CFI CFA_SP SP+-5
        ; Saved register size: 2
        ; Auto size: 0
//  286   uint16 value;
//  287 
//  288   /* If rev C or earlier, dont check the voltage */
//  289   if (CHVER < REV_D)
        MOV     DPTR,#-0x20a0
        MOVX    A,@DPTR
        CLR     C
        SUBB    A,#0x3
        JNC     ??HalAdcCheckVdd_0
//  290   {
//  291     return TRUE;
??HalAdcCheckVdd_1:
        MOV     R1,#0x1
        SJMP    ??HalAdcCheckVdd_2
//  292   }
//  293 
//  294   /* Clear ADC interrupt flag */
//  295   ADCIF = 0;
??HalAdcCheckVdd_0:
        CLR     0x88.5
//  296 
//  297   /* Setup the new value for conversion */
//  298   ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_DEC_064 | HAL_ADC_CHN_VDD3);
        MOV     0xb6,#0xf
//  299 
//  300   /* Wait for the conversion to finish */
//  301   while ( !ADCIF );
??HalAdcCheckVdd_3:
        MOV     C,0x88.5
        JNC     ??HalAdcCheckVdd_3
//  302 
//  303   /* Get the result */
//  304   value = ADCL;
        MOV     R2,0xba
//  305   value |= ((uint16) ADCH) << 8;
        MOV     A,0xbb
        MOV     R3,A
//  306 
//  307   /* Check the limit and return */
//  308   return ( value >= HalAdcVddLimit[limit] );
        MOV     A,R1
        CLR     C
        RLC     A
        MOV     R0,A
        CLR     A
        RLC     A
        MOV     R1,A
        MOV     A,R0
        ADD     A,#(HalAdcVddLimit & 0xff)
        MOV     DPL,A
        MOV     A,R1
        ADDC    A,#((HalAdcVddLimit >> 8) & 0xff)
        MOV     DPH,A
        CLR     A
        MOVC    A,@A+DPTR
        MOV     R0,A
        MOV     A,#0x1
        MOVC    A,@A+DPTR
        MOV     R1,A
        CLR     C
        MOV     A,R2
        SUBB    A,R0
        MOV     A,R3
        SUBB    A,R1
        JNC     ??HalAdcCheckVdd_1
        MOV     R1,#0x0
??HalAdcCheckVdd_2:
        LJMP    ?Subroutine0 & 0xFFFF
        CFI EndBlock cfiBlock3
        REQUIRE _A_TCON
        REQUIRE ADCCON3
        REQUIRE ADCL
        REQUIRE ADCH
//  309 }

        RSEG BANK_RELAYS:CODE:REORDER:NOROOT(0)
??HalAdcInit?relay:
        LCALL   ?BDISPATCH
        DATA
        DC24    HalAdcInit

        RSEG BANK_RELAYS:CODE:REORDER:NOROOT(0)
??HalAdcRead?relay:
        LCALL   ?BDISPATCH
        DATA
        DC24    HalAdcRead

        RSEG BANK_RELAYS:CODE:REORDER:NOROOT(0)
??HalAdcCheckVdd?relay:
        LCALL   ?BDISPATCH
        DATA
        DC24    HalAdcCheckVdd

        END
//  310 
//  311 /**************************************************************************************************
//  312 **************************************************************************************************/
//  313 
//  314 
//  315 
//  316 
// 
// 315 bytes in segment BANKED_CODE
//  18 bytes in segment BANK_RELAYS
//  18 bytes in segment CODE_C
//   7 bytes in segment SFR_AN
// 
// 351 bytes of CODE memory
//   0 bytes of DATA memory (+ 7 bytes shared)
//
//Errors: none
//Warnings: none
