Merge Network code with webserver etc
[clinton/Smoothieware.git] / src / libs / ADC / adc.h
1 /* mbed Library - ADC
2 * Copyright (c) 2010, sblandford
3 * released under MIT license http://mbed.org/licence/mit
4 */
5
6 #ifndef MBED_ADC_H
7 #define MBED_ADC_H
8
9 #include "PinNames.h" // mbed.h lib
10 #define XTAL_FREQ 12000000
11 #define MAX_ADC_CLOCK 13000000
12 #define CLKS_PER_SAMPLE 64
13
14 class ADC {
15 public:
16
17 //Initialize ADC with ADC maximum sample rate of
18 //sample_rate and system clock divider of cclk_div
19 //Maximum recommened sample rate is 184000
20 ADC(int sample_rate, int cclk_div);
21
22 //Enable/disable ADC on pin according to state
23 //and also select/de-select for next conversion
24 void setup(PinName pin, int state);
25
26 //Return enabled/disabled state of ADC on pin
27 int setup(PinName pin);
28
29 //Enable/disable burst mode according to state
30 void burst(int state);
31
32 //Select channel already setup
33 void select(PinName pin);
34
35 //Return burst mode enabled/disabled
36 int burst(void);
37
38 /*Set start condition and edge according to mode:
39 0 - No start (this value should be used when clearing PDN to 0).
40 1 - Start conversion now.
41 2 - Start conversion when the edge selected by bit 27 occurs on the P2.10 / EINT0 / NMI pin.
42 3 - Start conversion when the edge selected by bit 27 occurs on the P1.27 / CLKOUT /
43 USB_OVRCRn / CAP0.1 pin.
44 4 - Start conversion when the edge selected by bit 27 occurs on MAT0.1. Note that this does
45 not require that the MAT0.1 function appear on a device pin.
46 5 - Start conversion when the edge selected by bit 27 occurs on MAT0.3. Note that it is not
47 possible to cause the MAT0.3 function to appear on a device pin.
48 6 - Start conversion when the edge selected by bit 27 occurs on MAT1.0. Note that this does
49 not require that the MAT1.0 function appear on a device pin.
50 7 - Start conversion when the edge selected by bit 27 occurs on MAT1.1. Note that this does
51 not require that the MAT1.1 function appear on a device pin.
52 When mode >= 2, conversion is triggered by edge:
53 0 - Rising edge
54 1 - Falling edge
55 */
56 void startmode(int mode, int edge);
57
58 //Return startmode state according to mode_edge=0: mode and mode_edge=1: edge
59 int startmode(int mode_edge);
60
61 //Start ADC conversion
62 void start(void);
63
64 //Set interrupt enable/disable for pin to state
65 void interrupt_state(PinName pin, int state);
66
67 //Return enable/disable state of interrupt for pin
68 int interrupt_state(PinName pin);
69
70 //Attach custom interrupt handler replacing default
71 void attach(void(*fptr)(void));
72
73 //Restore default interrupt handler
74 void detach(void);
75
76 //Append custom interrupt handler for pin
77 void append(PinName pin, void(*fptr)(uint32_t value));
78
79 //Unappend custom interrupt handler for pin
80 void unappend(PinName pin);
81
82 //Append custom global interrupt handler
83 void append(void(*fptr)(int chan, uint32_t value));
84
85 //Unappend custom global interrupt handler
86 void unappend(void);
87
88 //Set ADC offset to a value 0-7
89 void offset(int offset);
90
91 //Return current ADC offset
92 int offset(void);
93
94 //Return value of ADC on pin
95 int read(PinName pin);
96
97 //Return DONE flag of ADC on pin
98 int done(PinName pin);
99
100 //Return OVERRUN flag of ADC on pin
101 int overrun(PinName pin);
102
103 //Return actual ADC clock
104 int actual_adc_clock(void);
105
106 //Return actual maximum sample rate
107 int actual_sample_rate(void);
108
109 //Return pin ID of ADC channel
110 PinName channel_to_pin(int chan);
111
112 //Return pin number of ADC channel
113 int channel_to_pin_number(int chan);
114
115
116 private:
117 int _pin_to_channel(PinName pin);
118 uint32_t _data_of_pin(PinName pin);
119
120 int _adc_clk_freq;
121 void adcisr(void);
122 static void _adcisr(void);
123 static ADC *instance;
124
125 uint32_t _adc_data[8];
126 void(*_adc_isr[8])(uint32_t value);
127 void(*_adc_g_isr)(int chan, uint32_t value);
128 void(*_adc_m_isr)(void);
129 };
130
131 #endif