kao yi
/
Boboobooov4
wu
Fork of Bov3 by
BX-adc.cpp@19:4869b10a962e, 2014-07-02 (annotated)
- Committer:
- backman
- Date:
- Wed Jul 02 03:23:07 2014 +0000
- Revision:
- 19:4869b10a962e
wang
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
backman | 19:4869b10a962e | 1 | #ifdef TARGET_KLXX |
backman | 19:4869b10a962e | 2 | |
backman | 19:4869b10a962e | 3 | #include "BX-adc.h" |
backman | 19:4869b10a962e | 4 | #include "clk_freqs.h" |
backman | 19:4869b10a962e | 5 | |
backman | 19:4869b10a962e | 6 | #define MAX_FADC 6000000 |
backman | 19:4869b10a962e | 7 | #define CHANNELS_A_SHIFT 5 |
backman | 19:4869b10a962e | 8 | |
backman | 19:4869b10a962e | 9 | FastAnalogIn::FastAnalogIn(PinName pin, bool enabled) |
backman | 19:4869b10a962e | 10 | { |
backman | 19:4869b10a962e | 11 | ADCnumber = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
backman | 19:4869b10a962e | 12 | if (ADCnumber == (ADCName)NC) { |
backman | 19:4869b10a962e | 13 | error("ADC pin mapping failed"); |
backman | 19:4869b10a962e | 14 | } |
backman | 19:4869b10a962e | 15 | |
backman | 19:4869b10a962e | 16 | SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; |
backman | 19:4869b10a962e | 17 | |
backman | 19:4869b10a962e | 18 | uint32_t port = (uint32_t)pin >> PORT_SHIFT; |
backman | 19:4869b10a962e | 19 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
backman | 19:4869b10a962e | 20 | |
backman | 19:4869b10a962e | 21 | uint32_t cfg2_muxsel = ADC_CFG2_MUXSEL_MASK; |
backman | 19:4869b10a962e | 22 | if (ADCnumber & (1 << CHANNELS_A_SHIFT)) { |
backman | 19:4869b10a962e | 23 | cfg2_muxsel = 0; |
backman | 19:4869b10a962e | 24 | } |
backman | 19:4869b10a962e | 25 | |
backman | 19:4869b10a962e | 26 | // bus clk |
backman | 19:4869b10a962e | 27 | uint32_t PCLK = bus_frequency(); |
backman | 19:4869b10a962e | 28 | uint32_t clkdiv; |
backman | 19:4869b10a962e | 29 | for (clkdiv = 0; clkdiv < 4; clkdiv++) { |
backman | 19:4869b10a962e | 30 | if ((PCLK >> clkdiv) <= MAX_FADC) |
backman | 19:4869b10a962e | 31 | break; |
backman | 19:4869b10a962e | 32 | } |
backman | 19:4869b10a962e | 33 | if (clkdiv == 4) //Set max div |
backman | 19:4869b10a962e | 34 | clkdiv = 0x7; |
backman | 19:4869b10a962e | 35 | |
backman | 19:4869b10a962e | 36 | ADC0->SC1[1] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); |
backman | 19:4869b10a962e | 37 | |
backman | 19:4869b10a962e | 38 | ADC0->CFG1 = ADC_CFG1_ADIV(clkdiv & 0x3) // Clock Divide Select: (Input Clock)/8 |
backman | 19:4869b10a962e | 39 | | ADC_CFG1_MODE(3) // (16)bits Resolution |
backman | 19:4869b10a962e | 40 | | ADC_CFG1_ADICLK(clkdiv >> 2); // Input Clock: (Bus Clock)/2 |
backman | 19:4869b10a962e | 41 | |
backman | 19:4869b10a962e | 42 | ADC0->CFG2 = cfg2_muxsel // ADxxb or ADxxa channels |
backman | 19:4869b10a962e | 43 | | ADC_CFG2_ADACKEN_MASK // Asynchronous Clock Output Enable |
backman | 19:4869b10a962e | 44 | | ADC_CFG2_ADHSC_MASK; // High-Speed Configuration |
backman | 19:4869b10a962e | 45 | |
backman | 19:4869b10a962e | 46 | ADC0->SC2 = ADC_SC2_REFSEL(0); // Default Voltage Reference |
backman | 19:4869b10a962e | 47 | |
backman | 19:4869b10a962e | 48 | pinmap_pinout(pin, PinMap_ADC); |
backman | 19:4869b10a962e | 49 | |
backman | 19:4869b10a962e | 50 | //Enable channel |
backman | 19:4869b10a962e | 51 | running = false; |
backman | 19:4869b10a962e | 52 | enable(enabled); |
backman | 19:4869b10a962e | 53 | } |
backman | 19:4869b10a962e | 54 | |
backman | 19:4869b10a962e | 55 | void FastAnalogIn::enable(bool enabled) |
backman | 19:4869b10a962e | 56 | { |
backman | 19:4869b10a962e | 57 | //If currently not running |
backman | 19:4869b10a962e | 58 | if (!running) { |
backman | 19:4869b10a962e | 59 | if (enabled) { |
backman | 19:4869b10a962e | 60 | //Enable the ADC channel |
backman | 19:4869b10a962e | 61 | ADC0->SC3 |= ADC_SC3_ADCO_MASK; // Enable continuous conversion |
backman | 19:4869b10a962e | 62 | ADC0->SC1[0] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); //Start conversion |
backman | 19:4869b10a962e | 63 | running = true; |
backman | 19:4869b10a962e | 64 | } else |
backman | 19:4869b10a962e | 65 | disable(); |
backman | 19:4869b10a962e | 66 | } |
backman | 19:4869b10a962e | 67 | } |
backman | 19:4869b10a962e | 68 | |
backman | 19:4869b10a962e | 69 | void FastAnalogIn::disable( void ) |
backman | 19:4869b10a962e | 70 | { |
backman | 19:4869b10a962e | 71 | //If currently running |
backman | 19:4869b10a962e | 72 | if (running) { |
backman | 19:4869b10a962e | 73 | ADC0->SC3 &= ~ADC_SC3_ADCO_MASK; // Disable continuous conversion |
backman | 19:4869b10a962e | 74 | } |
backman | 19:4869b10a962e | 75 | running = false; |
backman | 19:4869b10a962e | 76 | } |
backman | 19:4869b10a962e | 77 | |
backman | 19:4869b10a962e | 78 | uint16_t FastAnalogIn::read_u16() |
backman | 19:4869b10a962e | 79 | { |
backman | 19:4869b10a962e | 80 | if (!running) |
backman | 19:4869b10a962e | 81 | { |
backman | 19:4869b10a962e | 82 | // start conversion |
backman | 19:4869b10a962e | 83 | ADC0->SC1[0] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); |
backman | 19:4869b10a962e | 84 | // Wait Conversion Complete |
backman | 19:4869b10a962e | 85 | while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); |
backman | 19:4869b10a962e | 86 | } |
backman | 19:4869b10a962e | 87 | if(running && ((ADC0->SC1[0]&ADC_SC1_ADCH_MASK) != (ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT))))) |
backman | 19:4869b10a962e | 88 | { |
backman | 19:4869b10a962e | 89 | running = false; |
backman | 19:4869b10a962e | 90 | enable(); |
backman | 19:4869b10a962e | 91 | while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); |
backman | 19:4869b10a962e | 92 | } |
backman | 19:4869b10a962e | 93 | // Return value |
backman | 19:4869b10a962e | 94 | return (uint16_t)ADC0->R[0]; |
backman | 19:4869b10a962e | 95 | } |
backman | 19:4869b10a962e | 96 | |
backman | 19:4869b10a962e | 97 | #endif //defined TARGET_KLXX |