Adapted code from original GT_Tuner code (by Andrew Durand) for a school project by Tapton School.

Dependencies:   mbed

Committer:
mptapton
Date:
Tue Jan 03 13:46:19 2017 +0000
Revision:
0:6b0b61d411ad
Adaptation to original GT_Tuner code (by Andrew Durand), as part of a school project for Tapton school

Who changed what in which revision?

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