Example #1
0
File: adc.c Project: wzyy2/ECG-BLE
// auxIo, see MUX3 Register (Offset = 3h) [reset = X]
// auxIo, sensor controller engine IO, will map to M3's IO automatilcally
// for DIO23, auxIO7, the value should be 80h, 0x80
uint16_t OneShotADC(uint8_t auxIo)
{
  static __root uint16_t retval = 0xABBA;
  
  // Enable clock for ADC digital and analog interface (not currently enabled in driver)
  AUXWUCClockEnable(AUX_WUC_MODCLKEN0_ANAIF_M|AUX_WUC_MODCLKEN0_AUX_ADI4_M);
  // Connect AUX IO7 (DIO23) as analog input. Light sensor on SmartRF06EB
  AUXADCSelectInput(auxIo);
  
  // Set up ADC
  AUXADCEnableSync(AUXADC_REF_FIXED, AUXADC_SAMPLE_TIME_2P7_US, AUXADC_TRIGGER_MANUAL);
  
  // Disallow STANDBY mode while using the ADC.
  Power_setConstraint(Power_SB_DISALLOW);
  
  // Trigger ADC sampling
  AUXADCGenManualTrigger();
  
  retval = AUXADCReadFifo();
  
  // Disable ADC
  AUXADCDisable();
  // Allow STANDBY mode again
  Power_releaseConstraint(Power_SB_DISALLOW);
  
  return retval;
}
void cc26xx_adc_init(uint32_t ADCInput)
{

	//intialisation of ADC
	ti_lib_aon_wuc_aux_wakeup_event(AONWUC_AUX_WAKEUP);
	while(!(ti_lib_aon_wuc_power_status_get() & AONWUC_AUX_POWER_ON))
	{ }

	// Enable clock for ADC digital and analog interface (not currently enabled in driver)
	// Enable clocks
	ti_lib_aux_wuc_clock_enable(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK);
	while(ti_lib_aux_wuc_clock_status(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK) != AUX_WUC_CLOCK_READY)
	{ }

	printf("clock selected\r\n");

	// Connect AUX IO7 (DIO23, but also DP2 on XDS110) as analog input.
	AUXADCSelectInput(ADCInput);
	printf("input selected\r\n");

	// Set up ADC range
	// AUXADC_REF_FIXED = nominally 4.3 V
	AUXADCEnableSync(AUXADC_REF_FIXED,  AUXADC_SAMPLE_TIME_2P7_US, AUXADC_TRIGGER_MANUAL);
	printf("init adc --- OK\r\n");
}
//taskFxn
void taskFxn(UArg a0, UArg a1) {

  Hwi_Params hwiParams;
  Hwi_Params_init(&hwiParams);
  hwiParams.enableInt = true;

  Hwi_construct(&hwi, INT_AUX_ADC, adcIsr, &hwiParams, NULL);

  UART_Params uParams;
  // Initialize default values
  UART_Params_init(&uParams);
  // Configure custom data, don't care about read params as not used
  // 115.2kBaud, Text, blocking mode
  uHandle = UART_open(Board_UART,&uParams);

  // Set up pins
  pinHandle = PIN_open(&pinState, alsPins);

  // Enable clock for ADC digital and analog interface (not currently enabled in driver)
  AUXWUCClockEnable(AUX_WUC_MODCLKEN0_SOC_M|AUX_WUC_MODCLKEN0_AUX_ADI4_M);
  // Connect AUX IO7 (DIO23) as analog input. Light sensor on SmartRF06EB
  AUXADCSelectInput(ADC_COMPB_IN_AUXIO7);


  // Set up ADC
  AUXADCEnableSync(AUXADC_REF_FIXED, AUXADC_SAMPLE_TIME_2P7_US, AUXADC_TRIGGER_MANUAL);

  // Disallow STANDBY mode while using the ADC.
  Power_setConstraint(Power_SB_DISALLOW);


  while(1) {

    //Sleep 100ms in IDLE mode
    //Task_sleep(100 * 1000 / Clock_tickPeriod);

    // Trigger ADC sampling
    AUXADCGenManualTrigger();

    // Wait in IDLE until done
    Semaphore_pend(hSem, BIOS_WAIT_FOREVER );

    //System_printf("ADC: %d\r\n", singleSample);
    //printf ("ADC: %d\r\n");
    //printf ("ADC\r\n");
  }

  /*


  // Disable ADC
  AUXADCDisable();
  // Allow STANDBY mode again
  Power_releaseConstraint(Power_SB_DISALLOW);

  // Restore pins to values in BoardGpioTable
  PIN_close(pinHandle);

  // Log data through UART



    UART_write(uHandle, &adcSamples[1], SAMPLESIZE);



  // Goto STANDBY forever
  Task_sleep(BIOS_WAIT_FOREVER);
*/
}
static uint16_t Get_ADC_reading(void)
{
      uint16_t  result_data = 0;
      uint16_t ADC_array[ADC_Average] = {0};
      int32_t adc_temp=0;      
      double result=0;
      sensor_power_set_on();
      adc_ct_select_set_on();
      // Enable AUX 
      ti_lib_aon_wuc_aux_wakeup_event(AONWUC_AUX_WAKEUP);
      while(!(ti_lib_aon_wuc_power_status_get() & AONWUC_AUX_POWER_ON))
      { }

      

      // Enable clocks
      ti_lib_aux_wuc_clock_enable(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK);
      while(ti_lib_aux_wuc_clock_status(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK) != AUX_WUC_CLOCK_READY)
      { }

      
      // Select input
      AUXADCSelectInput(ADC_COMPB_IN_AUXIO5);
      

      // Configure and enable
    //  AUXADCEnableSync(AUXADC_REF_VDDS_REL,  AUXADC_SAMPLE_TIME_10P6_US, AUXADC_TRIGGER_MANUAL);
      AUXADCEnableSync(AUXADC_REF_VDDS_REL,  AUXADC_SAMPLE_TIME_170_US, AUXADC_TRIGGER_MANUAL);    
     // AUXADCEnableSync(AUXADC_REF_FIXED,  AUXADC_SAMPLE_TIME_10P6_US, AUXADC_TRIGGER_MANUAL);

  
     
          
      // Read
      for(int i=0;i<ADC_Average;i++)
      {
        AUXADCGenManualTrigger();
        
        adc_temp = AUXADCReadFifo();
        if(adc_temp<2048)
        {
           result = adc_temp - 2048;        
          
        }else{
          
           result = adc_temp;
          
        }
        
        ADC_array[i] = (uint16_t)(abs(result));
       //  AUXADCReadFifo();
        
      }
      
      result_data = Adc_Average(ADC_array);
      
     // result_data = upcount++;
    //  AUXADCGenManualTrigger();
    //  result_data = AUXADCReadFifo();
      
      
      


      // Disable ADC
      AUXADCDisable();
      adc_ct_select_set_off();
     // relay_all_clear();
     // sensor_power_set_off();
      
      return result_data;
      
}