

$40007000 constant   ADC0_CFG       \  $00000000  ADC0 ADC Configuration
$40007004 constant   ADC0_PWRUP     \  $0000041C  ADC0 ADC Power-up Time
$40007008 constant   ADC0_CAL_WORD  \  $00000040  ADC0 Calibration Word
$4000700C constant   ADC0_CNV_CFG   \  $00000000  ADC0 ADC Conversion Configuration
$40007010 constant   ADC0_CNV_TIME  \  $00000000  ADC0 ADC Conversion Time
$40007014 constant   ADC0_AVG_CFG   \  $00004008  ADC0 Averaging Configuration
$40007020 constant   ADC0_IRQ_EN    \  $00000000  ADC0 Interrupt Enable
$40007024 constant   ADC0_STAT      \  $00000000  ADC0 ADC Status
$40007028 constant   ADC0_OVF       \  $00000000  ADC0 Overflow of Output Registers
$4000702C constant   ADC0_ALERT     \  $00000000  ADC0 Alert Indication
$40007030 constant   ADC0_CH0_OUT   \  $00000000  ADC0 Conversion Result Channel 0
$40007034 constant   ADC0_CH1_OUT   \  $00000000  ADC0 Conversion Result Channel 1
$40007038 constant   ADC0_CH2_OUT   \  $00000000  ADC0 Conversion Result Channel 2
$4000703C constant   ADC0_CH3_OUT   \  $00000000  ADC0 Conversion Result Channel 3
$40007040 constant   ADC0_CH4_OUT   \  $00000000  ADC0 Conversion Result Channel 4
$40007044 constant   ADC0_CH5_OUT   \  $00000000  ADC0 Conversion Result Channel 5
$40007048 constant   ADC0_CH6_OUT   \  $00000000  ADC0 Conversion Result Channel 6
$4000704C constant   ADC0_CH7_OUT   \  $00000000  ADC0 Conversion Result Channel 7
$40007050 constant   ADC0_BAT_OUT   \  $00000000  ADC0 Battery Monitoring Result
$40007054 constant   ADC0_TMP_OUT   \  $00000000  ADC0 Temperature Result
$40007058 constant   ADC0_TMP2_OUT  \  $00000000  ADC0 Temperature Result 2
$4000705C constant   ADC0_DMA_OUT   \  $00000000  ADC0 DMA Output Register
$40007060 constant   ADC0_LIM0_LO   \  $00000000  ADC0 Channel 0 Low Limit
$40007064 constant   ADC0_LIM0_HI   \  $00000FFF  ADC0 Channel 0 High Limit
$40007068 constant   ADC0_HYS0      \  $00000000  ADC0 Channel 0 Hysteresis
$40007070 constant   ADC0_LIM1_LO   \  $00000000  ADC0 Channel 1 Low Limit
$40007074 constant   ADC0_LIM1_HI   \  $00000FFF  ADC0 Channel 1 High Limit
$40007078 constant   ADC0_HYS1      \  $00000000  ADC0 Channel 1 Hysteresis
$40007080 constant   ADC0_LIM2_LO   \  $00000000  ADC0 Channel 2 Low Limit
$40007084 constant   ADC0_LIM2_HI   \  $00000FFF  ADC0 Channel 2 High Limit
$40007088 constant   ADC0_HYS2      \  $00000000  ADC0 Channel 2 Hysteresis
$40007090 constant   ADC0_LIM3_LO   \  $00000000  ADC0 Channel 3 Low Limit
$40007094 constant   ADC0_LIM3_HI   \  $00000FFF  ADC0 Channel 3 High Limit
$40007098 constant   ADC0_HYS3      \  $00000000  ADC0 Channel 3 Hysteresis
$400070C0 constant   ADC0_CFG1      \  $00000400  ADC0 Reference Buffer Low Power Mode

: analog-init ( -- ) \ Using Internal Reference Buffer

    \ To use the internal reference buffer, the following sequence must be employed at power-up:
    \ 1. Set the ADC_CFG.PWRUP bit to power up the ADC.

  1 ADC0_CFG !

    \ 2. Set the ADC_PWRUP.WAIT field to generate 20 μs wait time with respect to the PCLK frequency set in the CLKG_CLK_CTL1 register.

  $41C ADC0_PWRUP !

    \ 3. Select 1.25 V or 2.5 V as reference voltage using the ADC_CFG.VREFSEL bit.

  0 1 lshift ADC0_CFG bis! \ 0: 1.25 V, 1: 2.5 V reference
  
    \ 4. Assert the ADC_CFG.REFBUFEN bit.

  1 2 lshift ADC0_CFG bis!

    \ 5. Assert the ADC_CFG.EN bit.

  1 4 lshift ADC0_CFG bis!

    \ 6. Wait for 3.5 ms.

  \ 100000 0 do loop \ Not accurate...
  begin 1 15 lshift ADC0_STAT bit@ until \ Wait until RDY flag is set.

    \ 7. Write 1 to clear the ADC_STAT.RDY bit.

  1 15 lshift ADC0_STAT !

    \ 8. Set the ADC_CFG.STARTCAL bit to start the calibration cycle. The ADC is ready for conversion.

  1 5 lshift ADC0_CFG bis!

    \ Wait for calibration to be finished

  begin 1 14 lshift ADC0_STAT bit@ until
  1 14 lshift ADC0_STAT !

    \ Set sampling time in ACLK cycles (default: 1.625 MHz)

  32 ADC0_CNV_TIME ! \ ~ 50 kHz sample rate
;


$40020080 constant GPIO2_CFG   \ GPIO2 Port Configuration

: analog-ch7 ( -- value )
  1 7 lshift 1 14 lshift or ADC0_CNV_CFG !
  begin 1 7 lshift ADC0_STAT bit@ until
  ADC0_CH7_OUT @
  1 7 lshift ADC0_STAT !
;

: darkness ( -- )
  %01 10 2* lshift GPIO2_CFG ! \ Select P2.10 with LED2 as multiplexed function 1, ADC0_VIN7
  analog-init

  begin
    analog-ch7 . cr
  key? until
;
