Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Boboobooov4 by
BX-adc.cpp
00001 #ifdef TARGET_KLXX 00002 00003 #include "BX-adc.h" 00004 #include "clk_freqs.h" 00005 00006 #define MAX_FADC 6000000 00007 #define CHANNELS_A_SHIFT 5 00008 00009 FastAnalogIn::FastAnalogIn(PinName pin, bool enabled) 00010 { 00011 ADCnumber = (ADCName)pinmap_peripheral(pin, PinMap_ADC); 00012 if (ADCnumber == (ADCName)NC) { 00013 error("ADC pin mapping failed"); 00014 } 00015 00016 SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; 00017 00018 uint32_t port = (uint32_t)pin >> PORT_SHIFT; 00019 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); 00020 00021 uint32_t cfg2_muxsel = ADC_CFG2_MUXSEL_MASK; 00022 if (ADCnumber & (1 << CHANNELS_A_SHIFT)) { 00023 cfg2_muxsel = 0; 00024 } 00025 00026 // bus clk 00027 uint32_t PCLK = bus_frequency(); 00028 uint32_t clkdiv; 00029 for (clkdiv = 0; clkdiv < 4; clkdiv++) { 00030 if ((PCLK >> clkdiv) <= MAX_FADC) 00031 break; 00032 } 00033 if (clkdiv == 4) //Set max div 00034 clkdiv = 0x7; 00035 00036 ADC0->SC1[1] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); 00037 00038 ADC0->CFG1 = ADC_CFG1_ADIV(clkdiv & 0x3) // Clock Divide Select: (Input Clock)/8 00039 | ADC_CFG1_MODE(3) // (16)bits Resolution 00040 | ADC_CFG1_ADICLK(clkdiv >> 2); // Input Clock: (Bus Clock)/2 00041 00042 ADC0->CFG2 = cfg2_muxsel // ADxxb or ADxxa channels 00043 | ADC_CFG2_ADACKEN_MASK // Asynchronous Clock Output Enable 00044 | ADC_CFG2_ADHSC_MASK; // High-Speed Configuration 00045 00046 ADC0->SC2 = ADC_SC2_REFSEL(0); // Default Voltage Reference 00047 00048 pinmap_pinout(pin, PinMap_ADC); 00049 00050 //Enable channel 00051 running = false; 00052 enable(enabled); 00053 } 00054 00055 void FastAnalogIn::enable(bool enabled) 00056 { 00057 //If currently not running 00058 if (!running) { 00059 if (enabled) { 00060 //Enable the ADC channel 00061 ADC0->SC3 |= ADC_SC3_ADCO_MASK; // Enable continuous conversion 00062 ADC0->SC1[0] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); //Start conversion 00063 running = true; 00064 } else 00065 disable(); 00066 } 00067 } 00068 00069 void FastAnalogIn::disable( void ) 00070 { 00071 //If currently running 00072 if (running) { 00073 ADC0->SC3 &= ~ADC_SC3_ADCO_MASK; // Disable continuous conversion 00074 } 00075 running = false; 00076 } 00077 00078 uint16_t FastAnalogIn::read_u16() 00079 { 00080 if (!running) 00081 { 00082 // start conversion 00083 ADC0->SC1[0] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); 00084 // Wait Conversion Complete 00085 while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); 00086 } 00087 if(running && ((ADC0->SC1[0]&ADC_SC1_ADCH_MASK) != (ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT))))) 00088 { 00089 running = false; 00090 enable(); 00091 while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); 00092 } 00093 // Return value 00094 return (uint16_t)ADC0->R[0]; 00095 } 00096 00097 #endif //defined TARGET_KLXX
Generated on Thu Jul 14 2022 03:30:21 by
1.7.2
