2 * Copyright (c) 2010, sblandford
3 * released under MIT license http://mbed.org/licence/mit
9 #include "PinNames.h" // mbed.h lib
10 #define XTAL_FREQ 12000000
11 #define MAX_ADC_CLOCK 13000000
12 #define CLKS_PER_SAMPLE 64
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
);
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
);
26 //Return enabled/disabled state of ADC on pin
27 int setup(PinName pin
);
29 //Enable/disable burst mode according to state
30 void burst(int state
);
32 //Select channel already setup
33 void select(PinName pin
);
35 //Return burst mode enabled/disabled
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:
56 void startmode(int mode
, int edge
);
58 //Return startmode state according to mode_edge=0: mode and mode_edge=1: edge
59 int startmode(int mode_edge
);
61 //Start ADC conversion
64 //Set interrupt enable/disable for pin to state
65 void interrupt_state(PinName pin
, int state
);
67 //Return enable/disable state of interrupt for pin
68 int interrupt_state(PinName pin
);
70 //Attach custom interrupt handler replacing default
71 void attach(void(*fptr
)(void));
73 //Restore default interrupt handler
76 //Append custom interrupt handler for pin
77 void append(PinName pin
, void(*fptr
)(uint32_t value
));
79 //Unappend custom interrupt handler for pin
80 void unappend(PinName pin
);
82 //Append custom global interrupt handler
83 void append(void(*fptr
)(int chan
, uint32_t value
));
85 //Unappend custom global interrupt handler
88 //Set ADC offset to a value 0-7
89 void offset(int offset
);
91 //Return current ADC offset
94 //Return value of ADC on pin
95 int read(PinName pin
);
97 //Return DONE flag of ADC on pin
98 int done(PinName pin
);
100 //Return OVERRUN flag of ADC on pin
101 int overrun(PinName pin
);
103 //Return actual ADC clock
104 int actual_adc_clock(void);
106 //Return actual maximum sample rate
107 int actual_sample_rate(void);
109 //Return pin ID of ADC channel
110 PinName
channel_to_pin(int chan
);
112 //Return pin number of ADC channel
113 int channel_to_pin_number(int chan
);
117 int _pin_to_channel(PinName pin
);
118 uint32_t _data_of_pin(PinName pin
);
122 static void _adcisr(void);
123 static ADC
*instance
;
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);