diff options
author | Daniel Friesel <derf@finalrewind.org> | 2017-12-14 16:28:10 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2017-12-14 16:28:10 +0100 |
commit | 7fa97753d2f6ba2ce033b9da00d8020d8f247704 (patch) | |
tree | 426db44d2e0820b6c492b1f0e47a8fac3b45728c /src | |
parent | a92b8a813ecf4942988e052d7a4b8f6b2a220de9 (diff) |
Add MSP430FR5969 ADC driver
Diffstat (limited to 'src')
-rw-r--r-- | src/arch/msp430fr5969lp/Makefile.inc | 1 | ||||
-rw-r--r-- | src/arch/msp430fr5969lp/driver/adc.cc | 71 |
2 files changed, 72 insertions, 0 deletions
diff --git a/src/arch/msp430fr5969lp/Makefile.inc b/src/arch/msp430fr5969lp/Makefile.inc index 6d2ae6a..8d3647d 100644 --- a/src/arch/msp430fr5969lp/Makefile.inc +++ b/src/arch/msp430fr5969lp/Makefile.inc @@ -11,6 +11,7 @@ CXX = /opt/msp430/ti/gcc/bin/msp430-elf-g++ OBJCOPY = /opt/msp430/ti/gcc/bin/msp430-elf-objcopy TARGETS += src/arch/msp430fr5969lp/arch.cc +TARGETS += src/arch/msp430fr5969lp/driver/adc.cc TARGETS += src/arch/msp430fr5969lp/driver/gpio.cc TARGETS += src/arch/msp430fr5969lp/driver/stdout.cc TARGETS += src/arch/msp430fr5969lp/driver/uptime.cc diff --git a/src/arch/msp430fr5969lp/driver/adc.cc b/src/arch/msp430fr5969lp/driver/adc.cc new file mode 100644 index 0000000..5a044a6 --- /dev/null +++ b/src/arch/msp430fr5969lp/driver/adc.cc @@ -0,0 +1,71 @@ +#include "driver/adc.h" +#include <msp430.h> + +#define CALADC12_12V_30C *((unsigned int *)0x1A1A) +#define CALADC12_12V_85C *((unsigned int *)0x1A1C) + +float ADC::getTemp() +{ + float ret; + + while(REFCTL0 & REFGENBUSY); + + REFCTL0 = REFVSEL_0 | REFON; + ADC12CTL0 &= ~ADC12ENC; + ADC12CTL0 = ADC12SHT0_8 | ADC12ON; + ADC12CTL1 = ADC12SHP; + ADC12CTL3 = ADC12TCMAP; + ADC12MCTL0 = ADC12VRSEL_1 | ADC12INCH_30; + while(!(REFCTL0 & REFGENRDY)); + + ADC12CTL0 |= ADC12ENC; + ADC12CTL0 |= ADC12SC; + while (ADC12CTL1 & ADC12BUSY); + + ret = (float)((long)ADC12MEM0 - CALADC12_12V_30C) * (85 - 30) / + (CALADC12_12V_85C - CALADC12_12V_30C) + 30.0f; + + // Disable ADC + ADC12CTL0 &= ~ADC12ENC; // disable any conversion to allow ADC configuration + ADC12CTL0 &= ~ADC12ON; // Turn off ADC + + // Disable internal 2V reference + while(REFCTL0 & REFGENBUSY); + REFCTL0 &= ~REFON; + + return ret; +} + +float ADC::getVCC() +{ + float ret; + + while(REFCTL0 & REFGENBUSY); + + REFCTL0 = REFVSEL_1 | REFON; + ADC12CTL0 &= ~ADC12ENC; + ADC12CTL0 = ADC12SHT0_8 | ADC12ON; + ADC12CTL1 = ADC12SHP; + ADC12CTL3 = ADC12BATMAP; + ADC12MCTL0 = ADC12VRSEL_1 | ADC12INCH_31; + while(!(REFCTL0 & REFGENRDY)); + + ADC12CTL0 |= ADC12ENC; + ADC12CTL0 |= ADC12SC; + while (ADC12CTL1 & ADC12BUSY); + + ret = (float)ADC12MEM0 / 4096 * 2 * 2; + return ret; + + // Disable ADC + ADC12CTL0 &= ~ADC12ENC; // disable any conversion to allow ADC configuration + ADC12CTL0 &= ~ADC12ON; // Turn off ADC + + // Disable internal 2V reference + while(REFCTL0 & REFGENBUSY); + REFCTL0 &= ~REFON; + + return ret; +} + +ADC adc; |