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