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