10kHz ADC which reads from 17,18,19,20 pins and send the result to remote server (multicast now) via UDP

Dependencies:   EthernetNetIf mbed

Committer:
mrcandy
Date:
Mon Mar 26 15:54:08 2012 +0000
Revision:
1:beae6624e569
Parent:
0:7744cef9f8af
net::poll() added

Who changed what in which revision?

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