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