adc.c

Go to the documentation of this file.
00001 /**
00002  * \file
00003  *         ADC functions
00004  * \author
00005  *      Anthony "Asterisk" Ambuehl
00006  *
00007  *  ADC initialization routine, trigger and result conversion routines.
00008  *
00009  */
00010 
00011 #include <stdio.h>
00012 #include "banked.h"
00013 #include "contiki.h"
00014 #include "sys/clock.h"
00015 
00016 #include "cc2430_sfr.h"
00017 #include "dev/adc.h"
00018 #include "dev/dma.h"
00019 
00020 #ifdef HAVE_DMA
00021 xDMAHandle adc_dma=0xff;
00022 unsigned int *adc_dma_dest;
00023 #endif
00024 
00025 /*---------------------------------------------------------------------------*/
00026 void adc_init(void) __banked
00027 {
00028   unsigned char jj;
00029   while (!SLEEP&(HFRC_STB)) {}
00030   /* power both 32MHz crystal and 15MHz RC */
00031   SLEEP &= ~(OSC_PD); 
00032   /* printf("SLEEP 1 %x\n",SLEEP); */
00033   /* then wait for it to stabilize */
00034   while (!SLEEP&(XOSC_STB)) {}
00035   /* printf("SLEEP 2 %x\n",SLEEP); */
00036   /* then wait 64uS more */
00037   clock_delay(150);
00038   /* switch to 32MHz clock */
00039   /* printf("switch to 32MHz %x\n",CLKCON); */
00040   CLKCON &= ~(OSC);
00041   /* printf("switched to 32MHz %x\n",CLKCON); */
00042   /* power down 15MHz RC clock */
00043   SLEEP |= OSC_PD; 
00044   /* printf("pwr down hfrc\n",SLEEP);  */
00045 #ifdef HAVE_DMA
00046   /* preconfigure adc_dma before calling adc_init if a different dma type is desired. */
00047   if (adc_dma==0xff) {
00048     dma_init();
00049     /*  config DMA channel to copy results to single location */
00050     adc_dma=dma_config2(ADC_DMA_CONFIG_CHANNEL, &ADC_SHADOW, DMA_NOINC, adc_dma_dest, DMA_NOINC, 1, 1, DMA_VLEN_LEN, DMA_RPT, DMA_T_ADC_CHALL, 0);
00051   }
00052 #endif
00053 }
00054 /* single sample trigger */
00055 void adc_single_shot(void) __banked
00056 {
00057   ADCCON1 |= 0x73;
00058 }
00059 /* convert adc results */
00060 int16_t adc_convert_result(int16_t data) __banked {
00061   data = (0xfffc&data)>>2;
00062   return data; 
00063 }
00064 /* read/convert last conversion result */
00065 int16_t adc_get_last_conv() __banked {
00066   int16_t result;
00067   result = (ADCH<<8)|(ADCL);
00068   result  = (0xfffc&result)>>2;
00069   return result; 
00070 }

Generated on Mon Apr 11 14:23:37 2011 for Contiki 2.5 by  doxygen 1.6.1