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