ADC Spike2

Dependencies:   mbed

Committer:
simonb
Date:
Tue Mar 09 16:41:55 2010 +0000
Revision:
1:0d866e3f26ab
Parent:
0:db49fab3bd78

        

Who changed what in which revision?

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