Full ADC driver with demo

Dependencies:   mbed

Committer:
simonb
Date:
Wed Feb 10 13:11:26 2010 +0000
Revision:
0:a2562dfbf543

        

Who changed what in which revision?

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