Contiki 2.5
adc.c
Go to the documentation of this file.
1 /**
2  * \file
3  * ADC functions
4  * \author
5  * Anthony "Asterisk" Ambuehl
6  *
7  * ADC initialization routine, trigger and result conversion routines.
8  *
9  */
10 
11 #include <stdio.h>
12 #include "banked.h"
13 #include "contiki.h"
14 #include "sys/clock.h"
15 
16 #include "cc2430_sfr.h"
17 #include "dev/adc.h"
18 #include "dev/dma.h"
19 
20 #ifdef HAVE_DMA
21 xDMAHandle adc_dma=0xff;
22 unsigned int *adc_dma_dest;
23 #endif
24 
25 /*---------------------------------------------------------------------------*/
26 void adc_init(void) __banked
27 {
28  unsigned char jj;
29  while (!SLEEP&(HFRC_STB)) {}
30  /* power both 32MHz crystal and 15MHz RC */
31  SLEEP &= ~(OSC_PD);
32  /* printf("SLEEP 1 %x\n",SLEEP); */
33  /* then wait for it to stabilize */
34  while (!SLEEP&(XOSC_STB)) {}
35  /* printf("SLEEP 2 %x\n",SLEEP); */
36  /* then wait 64uS more */
37  clock_delay(150);
38  /* switch to 32MHz clock */
39  /* printf("switch to 32MHz %x\n",CLKCON); */
40  CLKCON &= ~(OSC);
41  /* printf("switched to 32MHz %x\n",CLKCON); */
42  /* power down 15MHz RC clock */
43  SLEEP |= OSC_PD;
44  /* printf("pwr down hfrc\n",SLEEP); */
45 #ifdef HAVE_DMA
46  /* preconfigure adc_dma before calling adc_init if a different dma type is desired. */
47  if (adc_dma==0xff) {
48  dma_init();
49  /* config DMA channel to copy results to single location */
50  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);
51  }
52 #endif
53 }
54 /* single sample trigger */
55 void adc_single_shot(void) __banked
56 {
57  ADCCON1 |= 0x73;
58 }
59 /* convert adc results */
60 int16_t adc_convert_result(int16_t data) __banked {
61  data = (0xfffc&data)>>2;
62  return data;
63 }
64 /* read/convert last conversion result */
65 int16_t adc_get_last_conv() __banked {
66  int16_t result;
67  result = (ADCH<<8)|(ADCL);
68  result = (0xfffc&result)>>2;
69  return result;
70 }