smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

For documentation, license, ..., please check http://smoothieware.org/

This version has been tested with a 3 axis machine

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?

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