posilani dat
Dependencies: FatFileSystemCpp mbed PowerControl USBHostLite
ADC_full/adc.cpp@26:5674b8978551, 2017-05-23 (annotated)
- Committer:
- PavelKumpan
- Date:
- Tue May 23 18:42:14 2017 +0000
- Revision:
- 26:5674b8978551
- Parent:
- 0:16fd37cf4a7c
Recreated communication protocol.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jkaderka | 0:16fd37cf4a7c | 1 | /* mbed Library - ADC |
jkaderka | 0:16fd37cf4a7c | 2 | * Copyright (c) 2010, sblandford |
jkaderka | 0:16fd37cf4a7c | 3 | * released under MIT license http://mbed.org/licence/mit |
jkaderka | 0:16fd37cf4a7c | 4 | */ |
jkaderka | 0:16fd37cf4a7c | 5 | #include "mbed.h" |
jkaderka | 0:16fd37cf4a7c | 6 | #include "adc.h" |
jkaderka | 0:16fd37cf4a7c | 7 | |
jkaderka | 0:16fd37cf4a7c | 8 | |
jkaderka | 0:16fd37cf4a7c | 9 | ADC *ADC::instance; |
jkaderka | 0:16fd37cf4a7c | 10 | |
jkaderka | 0:16fd37cf4a7c | 11 | ADC::ADC(int sample_rate, int cclk_div) |
jkaderka | 0:16fd37cf4a7c | 12 | { |
jkaderka | 0:16fd37cf4a7c | 13 | |
jkaderka | 0:16fd37cf4a7c | 14 | int i, adc_clk_freq, pclk, clock_div, max_div=1; |
jkaderka | 0:16fd37cf4a7c | 15 | |
jkaderka | 0:16fd37cf4a7c | 16 | //Work out CCLK |
jkaderka | 0:16fd37cf4a7c | 17 | adc_clk_freq=CLKS_PER_SAMPLE*sample_rate; |
jkaderka | 0:16fd37cf4a7c | 18 | int m = (LPC_SC->PLL0CFG & 0xFFFF) + 1; |
jkaderka | 0:16fd37cf4a7c | 19 | int n = (LPC_SC->PLL0CFG >> 16) + 1; |
jkaderka | 0:16fd37cf4a7c | 20 | int cclkdiv = LPC_SC->CCLKCFG + 1; |
jkaderka | 0:16fd37cf4a7c | 21 | int Fcco = (2 * m * XTAL_FREQ) / n; |
jkaderka | 0:16fd37cf4a7c | 22 | int cclk = Fcco / cclkdiv; |
jkaderka | 0:16fd37cf4a7c | 23 | |
jkaderka | 0:16fd37cf4a7c | 24 | //Power up the ADC |
jkaderka | 0:16fd37cf4a7c | 25 | LPC_SC->PCONP |= (1 << 12); |
jkaderka | 0:16fd37cf4a7c | 26 | //Set clock at cclk / 1. |
jkaderka | 0:16fd37cf4a7c | 27 | LPC_SC->PCLKSEL0 &= ~(0x3 << 24); |
jkaderka | 0:16fd37cf4a7c | 28 | switch (cclk_div) { |
jkaderka | 0:16fd37cf4a7c | 29 | case 1: |
jkaderka | 0:16fd37cf4a7c | 30 | LPC_SC->PCLKSEL0 |= 0x1 << 24; |
jkaderka | 0:16fd37cf4a7c | 31 | break; |
jkaderka | 0:16fd37cf4a7c | 32 | case 2: |
jkaderka | 0:16fd37cf4a7c | 33 | LPC_SC->PCLKSEL0 |= 0x2 << 24; |
jkaderka | 0:16fd37cf4a7c | 34 | break; |
jkaderka | 0:16fd37cf4a7c | 35 | case 4: |
jkaderka | 0:16fd37cf4a7c | 36 | LPC_SC->PCLKSEL0 |= 0x0 << 24; |
jkaderka | 0:16fd37cf4a7c | 37 | break; |
jkaderka | 0:16fd37cf4a7c | 38 | case 8: |
jkaderka | 0:16fd37cf4a7c | 39 | LPC_SC->PCLKSEL0 |= 0x3 << 24; |
jkaderka | 0:16fd37cf4a7c | 40 | break; |
jkaderka | 0:16fd37cf4a7c | 41 | default: |
jkaderka | 0:16fd37cf4a7c | 42 | fprintf(stderr, "Warning: ADC CCLK clock divider must be 1, 2, 4 or 8. %u supplied.\n", |
jkaderka | 0:16fd37cf4a7c | 43 | cclk_div); |
jkaderka | 0:16fd37cf4a7c | 44 | fprintf(stderr, "Defaulting to 1.\n"); |
jkaderka | 0:16fd37cf4a7c | 45 | LPC_SC->PCLKSEL0 |= 0x1 << 24; |
jkaderka | 0:16fd37cf4a7c | 46 | break; |
jkaderka | 0:16fd37cf4a7c | 47 | } |
jkaderka | 0:16fd37cf4a7c | 48 | pclk = cclk / cclk_div; |
jkaderka | 0:16fd37cf4a7c | 49 | clock_div=pclk / adc_clk_freq; |
jkaderka | 0:16fd37cf4a7c | 50 | |
jkaderka | 0:16fd37cf4a7c | 51 | if (clock_div > 0xFF) { |
jkaderka | 0:16fd37cf4a7c | 52 | fprintf(stderr, "Warning: Clock division is %u which is above 255 limit. Re-Setting at limit.\n", |
jkaderka | 0:16fd37cf4a7c | 53 | clock_div); |
jkaderka | 0:16fd37cf4a7c | 54 | clock_div=0xFF; |
jkaderka | 0:16fd37cf4a7c | 55 | } |
jkaderka | 0:16fd37cf4a7c | 56 | if (clock_div == 0) { |
jkaderka | 0:16fd37cf4a7c | 57 | fprintf(stderr, "Warning: Clock division is 0. Re-Setting to 1.\n"); |
jkaderka | 0:16fd37cf4a7c | 58 | clock_div=1; |
jkaderka | 0:16fd37cf4a7c | 59 | } |
jkaderka | 0:16fd37cf4a7c | 60 | |
jkaderka | 0:16fd37cf4a7c | 61 | _adc_clk_freq=pclk / clock_div; |
jkaderka | 0:16fd37cf4a7c | 62 | if (_adc_clk_freq > MAX_ADC_CLOCK) { |
jkaderka | 0:16fd37cf4a7c | 63 | fprintf(stderr, "Warning: Actual ADC sample rate of %u which is above %u limit\n", |
jkaderka | 0:16fd37cf4a7c | 64 | _adc_clk_freq / CLKS_PER_SAMPLE, MAX_ADC_CLOCK / CLKS_PER_SAMPLE); |
jkaderka | 0:16fd37cf4a7c | 65 | while ((pclk / max_div) > MAX_ADC_CLOCK) max_div++; |
jkaderka | 0:16fd37cf4a7c | 66 | fprintf(stderr, "Maximum recommended sample rate is %u\n", (pclk / max_div) / CLKS_PER_SAMPLE); |
jkaderka | 0:16fd37cf4a7c | 67 | } |
jkaderka | 0:16fd37cf4a7c | 68 | |
jkaderka | 0:16fd37cf4a7c | 69 | LPC_ADC->ADCR = |
jkaderka | 0:16fd37cf4a7c | 70 | ((clock_div - 1 ) << 8 ) | //Clkdiv |
jkaderka | 0:16fd37cf4a7c | 71 | ( 1 << 21 ); //A/D operational |
jkaderka | 0:16fd37cf4a7c | 72 | |
jkaderka | 0:16fd37cf4a7c | 73 | //Default no channels enabled |
jkaderka | 0:16fd37cf4a7c | 74 | LPC_ADC->ADCR &= ~0xFF; |
jkaderka | 0:16fd37cf4a7c | 75 | //Default NULL global custom isr |
jkaderka | 0:16fd37cf4a7c | 76 | _adc_g_isr = NULL; |
jkaderka | 0:16fd37cf4a7c | 77 | //Initialize arrays |
jkaderka | 0:16fd37cf4a7c | 78 | for (i=7; i>=0; i--) { |
jkaderka | 0:16fd37cf4a7c | 79 | _adc_data[i] = 0; |
jkaderka | 0:16fd37cf4a7c | 80 | _adc_isr[i] = NULL; |
jkaderka | 0:16fd37cf4a7c | 81 | } |
jkaderka | 0:16fd37cf4a7c | 82 | |
jkaderka | 0:16fd37cf4a7c | 83 | |
jkaderka | 0:16fd37cf4a7c | 84 | //* Attach IRQ |
jkaderka | 0:16fd37cf4a7c | 85 | instance = this; |
jkaderka | 0:16fd37cf4a7c | 86 | NVIC_SetVector(ADC_IRQn, (uint32_t)&_adcisr); |
jkaderka | 0:16fd37cf4a7c | 87 | |
jkaderka | 0:16fd37cf4a7c | 88 | //Disable global interrupt |
jkaderka | 0:16fd37cf4a7c | 89 | LPC_ADC->ADINTEN &= ~0x100; |
jkaderka | 0:16fd37cf4a7c | 90 | |
jkaderka | 0:16fd37cf4a7c | 91 | }; |
jkaderka | 0:16fd37cf4a7c | 92 | |
jkaderka | 0:16fd37cf4a7c | 93 | void ADC::_adcisr(void) |
jkaderka | 0:16fd37cf4a7c | 94 | { |
jkaderka | 0:16fd37cf4a7c | 95 | instance->adcisr(); |
jkaderka | 0:16fd37cf4a7c | 96 | } |
jkaderka | 0:16fd37cf4a7c | 97 | |
jkaderka | 0:16fd37cf4a7c | 98 | |
jkaderka | 0:16fd37cf4a7c | 99 | void ADC::adcisr(void) |
jkaderka | 0:16fd37cf4a7c | 100 | { |
jkaderka | 0:16fd37cf4a7c | 101 | uint32_t stat; |
jkaderka | 0:16fd37cf4a7c | 102 | int chan; |
jkaderka | 0:16fd37cf4a7c | 103 | |
jkaderka | 0:16fd37cf4a7c | 104 | // Read status |
jkaderka | 0:16fd37cf4a7c | 105 | stat = LPC_ADC->ADSTAT; |
jkaderka | 0:16fd37cf4a7c | 106 | //Scan channels for over-run or done and update array |
jkaderka | 0:16fd37cf4a7c | 107 | if (stat & 0x0101) _adc_data[0] = LPC_ADC->ADDR0; |
jkaderka | 0:16fd37cf4a7c | 108 | if (stat & 0x0202) _adc_data[1] = LPC_ADC->ADDR1; |
jkaderka | 0:16fd37cf4a7c | 109 | if (stat & 0x0404) _adc_data[2] = LPC_ADC->ADDR2; |
jkaderka | 0:16fd37cf4a7c | 110 | if (stat & 0x0808) _adc_data[3] = LPC_ADC->ADDR3; |
jkaderka | 0:16fd37cf4a7c | 111 | if (stat & 0x1010) _adc_data[4] = LPC_ADC->ADDR4; |
jkaderka | 0:16fd37cf4a7c | 112 | if (stat & 0x2020) _adc_data[5] = LPC_ADC->ADDR5; |
jkaderka | 0:16fd37cf4a7c | 113 | if (stat & 0x4040) _adc_data[6] = LPC_ADC->ADDR6; |
jkaderka | 0:16fd37cf4a7c | 114 | if (stat & 0x8080) _adc_data[7] = LPC_ADC->ADDR7; |
jkaderka | 0:16fd37cf4a7c | 115 | |
jkaderka | 0:16fd37cf4a7c | 116 | // Channel that triggered interrupt |
jkaderka | 0:16fd37cf4a7c | 117 | chan = (LPC_ADC->ADGDR >> 24) & 0x07; |
jkaderka | 0:16fd37cf4a7c | 118 | //User defined interrupt handlers |
jkaderka | 0:16fd37cf4a7c | 119 | if (_adc_isr[chan] != NULL) |
jkaderka | 0:16fd37cf4a7c | 120 | _adc_isr[chan](_adc_data[chan]); |
jkaderka | 0:16fd37cf4a7c | 121 | if (_adc_g_isr != NULL) |
jkaderka | 0:16fd37cf4a7c | 122 | _adc_g_isr(chan, _adc_data[chan]); |
jkaderka | 0:16fd37cf4a7c | 123 | return; |
jkaderka | 0:16fd37cf4a7c | 124 | } |
jkaderka | 0:16fd37cf4a7c | 125 | |
jkaderka | 0:16fd37cf4a7c | 126 | int ADC::_pin_to_channel(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 127 | int chan; |
jkaderka | 0:16fd37cf4a7c | 128 | switch (pin) { |
jkaderka | 0:16fd37cf4a7c | 129 | case p15://=p0.23 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 130 | default: |
jkaderka | 0:16fd37cf4a7c | 131 | chan=0; |
jkaderka | 0:16fd37cf4a7c | 132 | break; |
jkaderka | 0:16fd37cf4a7c | 133 | case p16://=p0.24 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 134 | chan=1; |
jkaderka | 0:16fd37cf4a7c | 135 | break; |
jkaderka | 0:16fd37cf4a7c | 136 | case p17://=p0.25 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 137 | chan=2; |
jkaderka | 0:16fd37cf4a7c | 138 | break; |
jkaderka | 0:16fd37cf4a7c | 139 | case p18://=p0.26 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 140 | chan=3; |
jkaderka | 0:16fd37cf4a7c | 141 | break; |
jkaderka | 0:16fd37cf4a7c | 142 | case p19://=p1.30 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 143 | chan=4; |
jkaderka | 0:16fd37cf4a7c | 144 | break; |
jkaderka | 0:16fd37cf4a7c | 145 | case p20://=p1.31 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 146 | chan=5; |
jkaderka | 0:16fd37cf4a7c | 147 | break; |
jkaderka | 0:16fd37cf4a7c | 148 | } |
jkaderka | 0:16fd37cf4a7c | 149 | return(chan); |
jkaderka | 0:16fd37cf4a7c | 150 | } |
jkaderka | 0:16fd37cf4a7c | 151 | |
jkaderka | 0:16fd37cf4a7c | 152 | PinName ADC::channel_to_pin(int chan) { |
jkaderka | 0:16fd37cf4a7c | 153 | const PinName pin[8]={p15, p16, p17, p18, p19, p20, p15, p15}; |
jkaderka | 0:16fd37cf4a7c | 154 | |
jkaderka | 0:16fd37cf4a7c | 155 | if ((chan < 0) || (chan > 5)) |
jkaderka | 0:16fd37cf4a7c | 156 | fprintf(stderr, "ADC channel %u is outside range available to MBED pins.\n", chan); |
jkaderka | 0:16fd37cf4a7c | 157 | return(pin[chan & 0x07]); |
jkaderka | 0:16fd37cf4a7c | 158 | } |
jkaderka | 0:16fd37cf4a7c | 159 | |
jkaderka | 0:16fd37cf4a7c | 160 | |
jkaderka | 0:16fd37cf4a7c | 161 | int ADC::channel_to_pin_number(int chan) { |
jkaderka | 0:16fd37cf4a7c | 162 | const int pin[8]={15, 16, 17, 18, 19, 20, 0, 0}; |
jkaderka | 0:16fd37cf4a7c | 163 | |
jkaderka | 0:16fd37cf4a7c | 164 | if ((chan < 0) || (chan > 5)) |
jkaderka | 0:16fd37cf4a7c | 165 | fprintf(stderr, "ADC channel %u is outside range available to MBED pins.\n", chan); |
jkaderka | 0:16fd37cf4a7c | 166 | return(pin[chan & 0x07]); |
jkaderka | 0:16fd37cf4a7c | 167 | } |
jkaderka | 0:16fd37cf4a7c | 168 | |
jkaderka | 0:16fd37cf4a7c | 169 | |
jkaderka | 0:16fd37cf4a7c | 170 | uint32_t ADC::_data_of_pin(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 171 | //If in burst mode and at least one interrupt enabled then |
jkaderka | 0:16fd37cf4a7c | 172 | //take all values from _adc_data |
jkaderka | 0:16fd37cf4a7c | 173 | if (burst() && (LPC_ADC->ADINTEN & 0x3F)) { |
jkaderka | 0:16fd37cf4a7c | 174 | return(_adc_data[_pin_to_channel(pin)]); |
jkaderka | 0:16fd37cf4a7c | 175 | } else { |
jkaderka | 0:16fd37cf4a7c | 176 | //Return current register value or last value from interrupt |
jkaderka | 0:16fd37cf4a7c | 177 | switch (pin) { |
jkaderka | 0:16fd37cf4a7c | 178 | case p15://=p0.23 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 179 | default: |
jkaderka | 0:16fd37cf4a7c | 180 | return(LPC_ADC->ADINTEN & 0x01?_adc_data[0]:LPC_ADC->ADDR0); |
jkaderka | 0:16fd37cf4a7c | 181 | case p16://=p0.24 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 182 | return(LPC_ADC->ADINTEN & 0x02?_adc_data[1]:LPC_ADC->ADDR1); |
jkaderka | 0:16fd37cf4a7c | 183 | case p17://=p0.25 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 184 | return(LPC_ADC->ADINTEN & 0x04?_adc_data[2]:LPC_ADC->ADDR2); |
jkaderka | 0:16fd37cf4a7c | 185 | case p18://=p0.26 of LPC1768: |
jkaderka | 0:16fd37cf4a7c | 186 | return(LPC_ADC->ADINTEN & 0x08?_adc_data[3]:LPC_ADC->ADDR3); |
jkaderka | 0:16fd37cf4a7c | 187 | case p19://=p1.30 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 188 | return(LPC_ADC->ADINTEN & 0x10?_adc_data[4]:LPC_ADC->ADDR4); |
jkaderka | 0:16fd37cf4a7c | 189 | case p20://=p1.31 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 190 | return(LPC_ADC->ADINTEN & 0x20?_adc_data[5]:LPC_ADC->ADDR5); |
jkaderka | 0:16fd37cf4a7c | 191 | } |
jkaderka | 0:16fd37cf4a7c | 192 | } |
jkaderka | 0:16fd37cf4a7c | 193 | } |
jkaderka | 0:16fd37cf4a7c | 194 | |
jkaderka | 0:16fd37cf4a7c | 195 | //Enable or disable an ADC pin |
jkaderka | 0:16fd37cf4a7c | 196 | void ADC::setup(PinName pin, int state) { |
jkaderka | 0:16fd37cf4a7c | 197 | int chan; |
jkaderka | 0:16fd37cf4a7c | 198 | chan=_pin_to_channel(pin); |
jkaderka | 0:16fd37cf4a7c | 199 | if ((state & 1) == 1) { |
jkaderka | 0:16fd37cf4a7c | 200 | switch(pin) { |
jkaderka | 0:16fd37cf4a7c | 201 | case p15://=p0.23 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 202 | default: |
jkaderka | 0:16fd37cf4a7c | 203 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 14); |
jkaderka | 0:16fd37cf4a7c | 204 | LPC_PINCON->PINSEL1 |= (unsigned int)0x1 << 14; |
jkaderka | 0:16fd37cf4a7c | 205 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 14); |
jkaderka | 0:16fd37cf4a7c | 206 | LPC_PINCON->PINMODE1 |= (unsigned int)0x2 << 14; |
jkaderka | 0:16fd37cf4a7c | 207 | break; |
jkaderka | 0:16fd37cf4a7c | 208 | case p16://=p0.24 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 209 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 16); |
jkaderka | 0:16fd37cf4a7c | 210 | LPC_PINCON->PINSEL1 |= (unsigned int)0x1 << 16; |
jkaderka | 0:16fd37cf4a7c | 211 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 16); |
jkaderka | 0:16fd37cf4a7c | 212 | LPC_PINCON->PINMODE1 |= (unsigned int)0x2 << 16; |
jkaderka | 0:16fd37cf4a7c | 213 | break; |
jkaderka | 0:16fd37cf4a7c | 214 | case p17://=p0.25 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 215 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 18); |
jkaderka | 0:16fd37cf4a7c | 216 | LPC_PINCON->PINSEL1 |= (unsigned int)0x1 << 18; |
jkaderka | 0:16fd37cf4a7c | 217 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 18); |
jkaderka | 0:16fd37cf4a7c | 218 | LPC_PINCON->PINMODE1 |= (unsigned int)0x2 << 18; |
jkaderka | 0:16fd37cf4a7c | 219 | break; |
jkaderka | 0:16fd37cf4a7c | 220 | case p18://=p0.26 of LPC1768: |
jkaderka | 0:16fd37cf4a7c | 221 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 20); |
jkaderka | 0:16fd37cf4a7c | 222 | LPC_PINCON->PINSEL1 |= (unsigned int)0x1 << 20; |
jkaderka | 0:16fd37cf4a7c | 223 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 20); |
jkaderka | 0:16fd37cf4a7c | 224 | LPC_PINCON->PINMODE1 |= (unsigned int)0x2 << 20; |
jkaderka | 0:16fd37cf4a7c | 225 | break; |
jkaderka | 0:16fd37cf4a7c | 226 | case p19://=p1.30 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 227 | LPC_PINCON->PINSEL3 &= ~((unsigned int)0x3 << 28); |
jkaderka | 0:16fd37cf4a7c | 228 | LPC_PINCON->PINSEL3 |= (unsigned int)0x3 << 28; |
jkaderka | 0:16fd37cf4a7c | 229 | LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 28); |
jkaderka | 0:16fd37cf4a7c | 230 | LPC_PINCON->PINMODE3 |= (unsigned int)0x2 << 28; |
jkaderka | 0:16fd37cf4a7c | 231 | break; |
jkaderka | 0:16fd37cf4a7c | 232 | case p20://=p1.31 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 233 | LPC_PINCON->PINSEL3 &= ~((unsigned int)0x3 << 30); |
jkaderka | 0:16fd37cf4a7c | 234 | LPC_PINCON->PINSEL3 |= (unsigned int)0x3 << 30; |
jkaderka | 0:16fd37cf4a7c | 235 | LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 30); |
jkaderka | 0:16fd37cf4a7c | 236 | LPC_PINCON->PINMODE3 |= (unsigned int)0x2 << 30; |
jkaderka | 0:16fd37cf4a7c | 237 | break; |
jkaderka | 0:16fd37cf4a7c | 238 | } |
jkaderka | 0:16fd37cf4a7c | 239 | //Only one channel can be selected at a time if not in burst mode |
jkaderka | 0:16fd37cf4a7c | 240 | if (!burst()) LPC_ADC->ADCR &= ~0xFF; |
jkaderka | 0:16fd37cf4a7c | 241 | //Select channel |
jkaderka | 0:16fd37cf4a7c | 242 | LPC_ADC->ADCR |= (1 << chan); |
jkaderka | 0:16fd37cf4a7c | 243 | } |
jkaderka | 0:16fd37cf4a7c | 244 | else { |
jkaderka | 0:16fd37cf4a7c | 245 | switch(pin) { |
jkaderka | 0:16fd37cf4a7c | 246 | case p15://=p0.23 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 247 | default: |
jkaderka | 0:16fd37cf4a7c | 248 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 14); |
jkaderka | 0:16fd37cf4a7c | 249 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 14); |
jkaderka | 0:16fd37cf4a7c | 250 | break; |
jkaderka | 0:16fd37cf4a7c | 251 | case p16://=p0.24 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 252 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 16); |
jkaderka | 0:16fd37cf4a7c | 253 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 16); |
jkaderka | 0:16fd37cf4a7c | 254 | break; |
jkaderka | 0:16fd37cf4a7c | 255 | case p17://=p0.25 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 256 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 18); |
jkaderka | 0:16fd37cf4a7c | 257 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 18); |
jkaderka | 0:16fd37cf4a7c | 258 | break; |
jkaderka | 0:16fd37cf4a7c | 259 | case p18://=p0.26 of LPC1768: |
jkaderka | 0:16fd37cf4a7c | 260 | LPC_PINCON->PINSEL1 &= ~((unsigned int)0x3 << 20); |
jkaderka | 0:16fd37cf4a7c | 261 | LPC_PINCON->PINMODE1 &= ~((unsigned int)0x3 << 20); |
jkaderka | 0:16fd37cf4a7c | 262 | break; |
jkaderka | 0:16fd37cf4a7c | 263 | case p19://=p1.30 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 264 | LPC_PINCON->PINSEL3 &= ~((unsigned int)0x3 << 28); |
jkaderka | 0:16fd37cf4a7c | 265 | LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 28); |
jkaderka | 0:16fd37cf4a7c | 266 | break; |
jkaderka | 0:16fd37cf4a7c | 267 | case p20://=p1.31 of LPC1768 |
jkaderka | 0:16fd37cf4a7c | 268 | LPC_PINCON->PINSEL3 &= ~((unsigned int)0x3 << 30); |
jkaderka | 0:16fd37cf4a7c | 269 | LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 30); |
jkaderka | 0:16fd37cf4a7c | 270 | break; |
jkaderka | 0:16fd37cf4a7c | 271 | } |
jkaderka | 0:16fd37cf4a7c | 272 | LPC_ADC->ADCR &= ~(1 << chan); |
jkaderka | 0:16fd37cf4a7c | 273 | } |
jkaderka | 0:16fd37cf4a7c | 274 | } |
jkaderka | 0:16fd37cf4a7c | 275 | //Return channel enabled/disabled state |
jkaderka | 0:16fd37cf4a7c | 276 | int ADC::setup(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 277 | int chan; |
jkaderka | 0:16fd37cf4a7c | 278 | |
jkaderka | 0:16fd37cf4a7c | 279 | chan = _pin_to_channel(pin); |
jkaderka | 0:16fd37cf4a7c | 280 | return((LPC_ADC->ADCR & (1 << chan)) >> chan); |
jkaderka | 0:16fd37cf4a7c | 281 | } |
jkaderka | 0:16fd37cf4a7c | 282 | |
jkaderka | 0:16fd37cf4a7c | 283 | //Select channel already setup |
jkaderka | 0:16fd37cf4a7c | 284 | void ADC::select(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 285 | int chan; |
jkaderka | 0:16fd37cf4a7c | 286 | |
jkaderka | 0:16fd37cf4a7c | 287 | //Only one channel can be selected at a time if not in burst mode |
jkaderka | 0:16fd37cf4a7c | 288 | if (!burst()) LPC_ADC->ADCR &= ~0xFF; |
jkaderka | 0:16fd37cf4a7c | 289 | //Select channel |
jkaderka | 0:16fd37cf4a7c | 290 | chan = _pin_to_channel(pin); |
jkaderka | 0:16fd37cf4a7c | 291 | LPC_ADC->ADCR |= (1 << chan); |
jkaderka | 0:16fd37cf4a7c | 292 | } |
jkaderka | 0:16fd37cf4a7c | 293 | |
jkaderka | 0:16fd37cf4a7c | 294 | //Enable or disable burst mode |
jkaderka | 0:16fd37cf4a7c | 295 | void ADC::burst(int state) { |
jkaderka | 0:16fd37cf4a7c | 296 | if ((state & 1) == 1) { |
jkaderka | 0:16fd37cf4a7c | 297 | if (startmode(0) != 0) |
jkaderka | 0:16fd37cf4a7c | 298 | fprintf(stderr, "Warning. startmode is %u. Must be 0 for burst mode.\n", startmode(0)); |
jkaderka | 0:16fd37cf4a7c | 299 | LPC_ADC->ADCR |= (1 << 16); |
jkaderka | 0:16fd37cf4a7c | 300 | } |
jkaderka | 0:16fd37cf4a7c | 301 | else |
jkaderka | 0:16fd37cf4a7c | 302 | LPC_ADC->ADCR &= ~(1 << 16); |
jkaderka | 0:16fd37cf4a7c | 303 | } |
jkaderka | 0:16fd37cf4a7c | 304 | //Return burst mode state |
jkaderka | 0:16fd37cf4a7c | 305 | int ADC::burst(void) { |
jkaderka | 0:16fd37cf4a7c | 306 | return((LPC_ADC->ADCR & (1 << 16)) >> 16); |
jkaderka | 0:16fd37cf4a7c | 307 | } |
jkaderka | 0:16fd37cf4a7c | 308 | |
jkaderka | 0:16fd37cf4a7c | 309 | //Set startmode and edge |
jkaderka | 0:16fd37cf4a7c | 310 | void ADC::startmode(int mode, int edge) { |
jkaderka | 0:16fd37cf4a7c | 311 | int lpc_adc_temp; |
jkaderka | 0:16fd37cf4a7c | 312 | |
jkaderka | 0:16fd37cf4a7c | 313 | //Reset start mode and edge bit, |
jkaderka | 0:16fd37cf4a7c | 314 | lpc_adc_temp = LPC_ADC->ADCR & ~(0x0F << 24); |
jkaderka | 0:16fd37cf4a7c | 315 | //Write with new values |
jkaderka | 0:16fd37cf4a7c | 316 | lpc_adc_temp |= ((mode & 7) << 24) | ((edge & 1) << 27); |
jkaderka | 0:16fd37cf4a7c | 317 | LPC_ADC->ADCR = lpc_adc_temp; |
jkaderka | 0:16fd37cf4a7c | 318 | } |
jkaderka | 0:16fd37cf4a7c | 319 | |
jkaderka | 0:16fd37cf4a7c | 320 | //Return startmode state according to mode_edge=0: mode and mode_edge=1: edge |
jkaderka | 0:16fd37cf4a7c | 321 | int ADC::startmode(int mode_edge){ |
jkaderka | 0:16fd37cf4a7c | 322 | switch (mode_edge) { |
jkaderka | 0:16fd37cf4a7c | 323 | case 0: |
jkaderka | 0:16fd37cf4a7c | 324 | default: |
jkaderka | 0:16fd37cf4a7c | 325 | return((LPC_ADC->ADCR >> 24) & 0x07); |
jkaderka | 0:16fd37cf4a7c | 326 | case 1: |
jkaderka | 0:16fd37cf4a7c | 327 | return((LPC_ADC->ADCR >> 27) & 0x01); |
jkaderka | 0:16fd37cf4a7c | 328 | } |
jkaderka | 0:16fd37cf4a7c | 329 | } |
jkaderka | 0:16fd37cf4a7c | 330 | |
jkaderka | 0:16fd37cf4a7c | 331 | //Start ADC conversion |
jkaderka | 0:16fd37cf4a7c | 332 | void ADC::start(void) { |
jkaderka | 0:16fd37cf4a7c | 333 | startmode(1,0); |
jkaderka | 0:16fd37cf4a7c | 334 | } |
jkaderka | 0:16fd37cf4a7c | 335 | |
jkaderka | 0:16fd37cf4a7c | 336 | |
jkaderka | 0:16fd37cf4a7c | 337 | //Set interrupt enable/disable for pin to state |
jkaderka | 0:16fd37cf4a7c | 338 | void ADC::interrupt_state(PinName pin, int state) { |
jkaderka | 0:16fd37cf4a7c | 339 | int chan; |
jkaderka | 0:16fd37cf4a7c | 340 | |
jkaderka | 0:16fd37cf4a7c | 341 | chan = _pin_to_channel(pin); |
jkaderka | 0:16fd37cf4a7c | 342 | if (state == 1) { |
jkaderka | 0:16fd37cf4a7c | 343 | LPC_ADC->ADINTEN &= ~0x100; |
jkaderka | 0:16fd37cf4a7c | 344 | LPC_ADC->ADINTEN |= 1 << chan; |
jkaderka | 0:16fd37cf4a7c | 345 | /* Enable the ADC Interrupt */ |
jkaderka | 0:16fd37cf4a7c | 346 | NVIC_EnableIRQ(ADC_IRQn); |
jkaderka | 0:16fd37cf4a7c | 347 | } else { |
jkaderka | 0:16fd37cf4a7c | 348 | LPC_ADC->ADINTEN &= ~( 1 << chan ); |
jkaderka | 0:16fd37cf4a7c | 349 | //Disable interrrupt if no active pins left |
jkaderka | 0:16fd37cf4a7c | 350 | if ((LPC_ADC->ADINTEN & 0xFF) == 0) |
jkaderka | 0:16fd37cf4a7c | 351 | NVIC_DisableIRQ(ADC_IRQn); |
jkaderka | 0:16fd37cf4a7c | 352 | } |
jkaderka | 0:16fd37cf4a7c | 353 | } |
jkaderka | 0:16fd37cf4a7c | 354 | |
jkaderka | 0:16fd37cf4a7c | 355 | //Return enable/disable state of interrupt for pin |
jkaderka | 0:16fd37cf4a7c | 356 | int ADC::interrupt_state(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 357 | int chan; |
jkaderka | 0:16fd37cf4a7c | 358 | |
jkaderka | 0:16fd37cf4a7c | 359 | chan = _pin_to_channel(pin); |
jkaderka | 0:16fd37cf4a7c | 360 | return((LPC_ADC->ADINTEN >> chan) & 0x01); |
jkaderka | 0:16fd37cf4a7c | 361 | } |
jkaderka | 0:16fd37cf4a7c | 362 | |
jkaderka | 0:16fd37cf4a7c | 363 | |
jkaderka | 0:16fd37cf4a7c | 364 | //Attach custom interrupt handler replacing default |
jkaderka | 0:16fd37cf4a7c | 365 | void ADC::attach(void(*fptr)(void)) { |
jkaderka | 0:16fd37cf4a7c | 366 | //* Attach IRQ |
jkaderka | 0:16fd37cf4a7c | 367 | NVIC_SetVector(ADC_IRQn, (uint32_t)fptr); |
jkaderka | 0:16fd37cf4a7c | 368 | } |
jkaderka | 0:16fd37cf4a7c | 369 | |
jkaderka | 0:16fd37cf4a7c | 370 | //Restore default interrupt handler |
jkaderka | 0:16fd37cf4a7c | 371 | void ADC::detach(void) { |
jkaderka | 0:16fd37cf4a7c | 372 | //* Attach IRQ |
jkaderka | 0:16fd37cf4a7c | 373 | instance = this; |
jkaderka | 0:16fd37cf4a7c | 374 | NVIC_SetVector(ADC_IRQn, (uint32_t)&_adcisr); |
jkaderka | 0:16fd37cf4a7c | 375 | } |
jkaderka | 0:16fd37cf4a7c | 376 | |
jkaderka | 0:16fd37cf4a7c | 377 | |
jkaderka | 0:16fd37cf4a7c | 378 | //Append interrupt handler for pin to function isr |
jkaderka | 0:16fd37cf4a7c | 379 | void ADC::append(PinName pin, void(*fptr)(uint32_t value)) { |
jkaderka | 0:16fd37cf4a7c | 380 | int chan; |
jkaderka | 0:16fd37cf4a7c | 381 | |
jkaderka | 0:16fd37cf4a7c | 382 | chan = _pin_to_channel(pin); |
jkaderka | 0:16fd37cf4a7c | 383 | _adc_isr[chan] = fptr; |
jkaderka | 0:16fd37cf4a7c | 384 | } |
jkaderka | 0:16fd37cf4a7c | 385 | |
jkaderka | 0:16fd37cf4a7c | 386 | //Append interrupt handler for pin to function isr |
jkaderka | 0:16fd37cf4a7c | 387 | void ADC::unappend(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 388 | int chan; |
jkaderka | 0:16fd37cf4a7c | 389 | |
jkaderka | 0:16fd37cf4a7c | 390 | chan = _pin_to_channel(pin); |
jkaderka | 0:16fd37cf4a7c | 391 | _adc_isr[chan] = NULL; |
jkaderka | 0:16fd37cf4a7c | 392 | } |
jkaderka | 0:16fd37cf4a7c | 393 | |
jkaderka | 0:16fd37cf4a7c | 394 | //Unappend global interrupt handler to function isr |
jkaderka | 0:16fd37cf4a7c | 395 | void ADC::append(void(*fptr)(int chan, uint32_t value)) { |
jkaderka | 0:16fd37cf4a7c | 396 | _adc_g_isr = fptr; |
jkaderka | 0:16fd37cf4a7c | 397 | } |
jkaderka | 0:16fd37cf4a7c | 398 | |
jkaderka | 0:16fd37cf4a7c | 399 | //Detach global interrupt handler to function isr |
jkaderka | 0:16fd37cf4a7c | 400 | void ADC::unappend() { |
jkaderka | 0:16fd37cf4a7c | 401 | _adc_g_isr = NULL; |
jkaderka | 0:16fd37cf4a7c | 402 | } |
jkaderka | 0:16fd37cf4a7c | 403 | |
jkaderka | 0:16fd37cf4a7c | 404 | //Set ADC offset |
jkaderka | 0:16fd37cf4a7c | 405 | void offset(int offset) { |
jkaderka | 0:16fd37cf4a7c | 406 | LPC_ADC->ADTRM &= ~(0x07 << 4); |
jkaderka | 0:16fd37cf4a7c | 407 | LPC_ADC->ADTRM |= (offset & 0x07) << 4; |
jkaderka | 0:16fd37cf4a7c | 408 | } |
jkaderka | 0:16fd37cf4a7c | 409 | |
jkaderka | 0:16fd37cf4a7c | 410 | //Return current ADC offset |
jkaderka | 0:16fd37cf4a7c | 411 | int offset(void) { |
jkaderka | 0:16fd37cf4a7c | 412 | return((LPC_ADC->ADTRM >> 4) & 0x07); |
jkaderka | 0:16fd37cf4a7c | 413 | } |
jkaderka | 0:16fd37cf4a7c | 414 | |
jkaderka | 0:16fd37cf4a7c | 415 | //Return value of ADC on pin |
jkaderka | 0:16fd37cf4a7c | 416 | int ADC::read(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 417 | //Reset DONE and OVERRUN flags of interrupt handled ADC data |
jkaderka | 0:16fd37cf4a7c | 418 | _adc_data[_pin_to_channel(pin)] &= ~(((uint32_t)0x01 << 31) | ((uint32_t)0x01 << 30)); |
jkaderka | 0:16fd37cf4a7c | 419 | //Return value |
jkaderka | 0:16fd37cf4a7c | 420 | return((_data_of_pin(pin) >> 4) & 0xFFF); |
jkaderka | 0:16fd37cf4a7c | 421 | } |
jkaderka | 0:16fd37cf4a7c | 422 | |
jkaderka | 0:16fd37cf4a7c | 423 | //Return DONE flag of ADC on pin |
jkaderka | 0:16fd37cf4a7c | 424 | int ADC::done(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 425 | return((_data_of_pin(pin) >> 31) & 0x01); |
jkaderka | 0:16fd37cf4a7c | 426 | } |
jkaderka | 0:16fd37cf4a7c | 427 | |
jkaderka | 0:16fd37cf4a7c | 428 | //Return OVERRUN flag of ADC on pin |
jkaderka | 0:16fd37cf4a7c | 429 | int ADC::overrun(PinName pin) { |
jkaderka | 0:16fd37cf4a7c | 430 | return((_data_of_pin(pin) >> 30) & 0x01); |
jkaderka | 0:16fd37cf4a7c | 431 | } |
jkaderka | 0:16fd37cf4a7c | 432 | |
jkaderka | 0:16fd37cf4a7c | 433 | int ADC::actual_adc_clock(void) { |
jkaderka | 0:16fd37cf4a7c | 434 | return(_adc_clk_freq); |
jkaderka | 0:16fd37cf4a7c | 435 | } |
jkaderka | 0:16fd37cf4a7c | 436 | |
jkaderka | 0:16fd37cf4a7c | 437 | int ADC::actual_sample_rate(void) { |
jkaderka | 0:16fd37cf4a7c | 438 | return(_adc_clk_freq / CLKS_PER_SAMPLE); |
jkaderka | 0:16fd37cf4a7c | 439 | } |