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 BX-car_2 by
bx-adc.cpp
00001 #include "bx-adc.h" 00002 #include "clk_freqs.h" 00003 00004 #define MAX_FADC 6000000 00005 #define CHANNELS_A_SHIFT 5 00006 00007 FastAnalogIn::FastAnalogIn(PinName pin, bool enabled) 00008 { 00009 ADCnumber = (ADCName)pinmap_peripheral(pin, PinMap_ADC); 00010 if (ADCnumber == (ADCName)NC) { 00011 error("ADC pin mapping failed"); 00012 } 00013 00014 SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; 00015 //----------------------------------------------------- 00016 uint32_t port = (uint32_t)pin >> PORT_SHIFT; 00017 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); 00018 00019 uint32_t cfg2_muxsel = ADC_CFG2_MUXSEL_MASK; 00020 if (ADCnumber & (1 << CHANNELS_A_SHIFT)) { 00021 cfg2_muxsel = 0; 00022 } 00023 //------------------------------------------------------ 00024 // bus clk 00025 uint32_t PCLK = bus_frequency(); 00026 uint32_t clkdiv; 00027 for (clkdiv = 0; clkdiv < 4; clkdiv++) { 00028 if ((PCLK >> clkdiv) <= MAX_FADC) 00029 break; 00030 } 00031 if (clkdiv == 4) //Set max div 00032 clkdiv = 0x7; 00033 00034 ADC0->SC1[1] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); 00035 00036 ADC0->CFG1 = ADC_CFG1_ADIV(clkdiv & 0x3) // Clock Divide Select: (Input Clock)/8 00037 | ADC_CFG1_MODE(3) // (16)bits Resolution 00038 | ADC_CFG1_ADICLK(clkdiv >> 2); // Input Clock: (Bus Clock)/2 00039 00040 ADC0->CFG2 = cfg2_muxsel // ADxxb or ADxxa channels 00041 | ADC_CFG2_ADACKEN_MASK // Asynchronous Clock Output Enable 00042 | ADC_CFG2_ADHSC_MASK; // High-Speed Configuration 00043 00044 ADC0->SC2 = ADC_SC2_REFSEL(0); // Default Voltage Reference 00045 00046 pinmap_pinout(pin, PinMap_ADC); 00047 00048 //Enable channel 00049 running = false; 00050 enable(enabled); 00051 } 00052 00053 void FastAnalogIn::enable(bool enabled) 00054 { 00055 //If currently not running 00056 if (!running) { 00057 if (enabled) { 00058 //Enable the ADC channel 00059 ADC0->SC3 |= ADC_SC3_ADCO_MASK; // Enable continuous conversion 00060 ADC0->SC1[0] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); //Start conversion 00061 running = true; 00062 } else 00063 disable(); 00064 } 00065 } 00066 00067 void FastAnalogIn::disable( void ) 00068 { 00069 //If currently running 00070 if (running) { 00071 ADC0->SC3 &= ~ADC_SC3_ADCO_MASK; // Disable continuous conversion 00072 } 00073 running = false; 00074 } 00075 00076 uint16_t FastAnalogIn::read_u16() 00077 { 00078 if (!running) 00079 { 00080 // start conversion 00081 ADC0->SC1[0] = ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT)); 00082 // Wait Conversion Complete 00083 while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); 00084 } 00085 if(running && ((ADC0->SC1[0]&ADC_SC1_ADCH_MASK) != (ADC_SC1_ADCH(ADCnumber & ~(1 << CHANNELS_A_SHIFT))))) 00086 { 00087 running = false; 00088 enable(); 00089 while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); 00090 } 00091 // Return value 00092 return (uint16_t)ADC0->R[0]; 00093 } 00094
Generated on Tue Jul 12 2022 21:33:29 by
1.7.2
