add adc support
This commit is contained in:
parent
b2b18b37fc
commit
c27e96f1e9
138
lib/adc.c
Normal file
138
lib/adc.c
Normal file
|
@ -0,0 +1,138 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
|
||||||
|
* to the MC1322x project (http://mc1322x.devl.org)
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the Institute nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This file is part of libmc1322x: see http://mc1322x.devl.org
|
||||||
|
* for details.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "crm.h"
|
||||||
|
#include "adc.h"
|
||||||
|
#include "gpio-util.h"
|
||||||
|
|
||||||
|
//#define ADC_CHANS_ENABLED 0x3F // channels 0-5 enabled
|
||||||
|
//#define ADC_CHANS_ENABLED 0x7E // channels 1-6 enabled
|
||||||
|
//#define ADC_CHANS_ENABLED (1 << 6) // only channel 6 enabled
|
||||||
|
#define ADC_CHANS_ENABLED 0x1FF // all channels, including battery reference voltage
|
||||||
|
|
||||||
|
//#define ADC_PRESCALE_VALUE 24 // divide by 24 for 1MHz.
|
||||||
|
|
||||||
|
#define ADC_SAMPLE_FREQ 400 // Hz (minimum of ~366.21 Hz)
|
||||||
|
#define ADC_PRESCALE_VALUE (REF_OSC / ADC_SAMPLE_FREQ)
|
||||||
|
|
||||||
|
#define ADC_USE_TIMER 0
|
||||||
|
#define ADC_USE_INTERRUPTS 0 // incomplete support
|
||||||
|
|
||||||
|
uint16_t adc_reading[NUM_ADC_CHAN];
|
||||||
|
|
||||||
|
void ADC_flush(void) {
|
||||||
|
while(ADC->FIFO_STATUSbits.EMPTY == 0) ADC->FIFO_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t ADC_READ(void) {
|
||||||
|
while(ADC->FIFO_STATUSbits.EMPTY); // loop while empty
|
||||||
|
return ADC->FIFO_READ; // upper 4 bits are channel number
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void adc_service(void) {
|
||||||
|
uint16_t value;
|
||||||
|
uint8_t channel;
|
||||||
|
while (ADC->FIFO_STATUSbits.EMPTY == 0) {
|
||||||
|
value = ADC->FIFO_READ;
|
||||||
|
channel = value >> 12;
|
||||||
|
if (channel < NUM_ADC_CHAN) adc_reading[channel] = value & 0xFFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void adc_init(void) {
|
||||||
|
uint8_t n;
|
||||||
|
|
||||||
|
ADC->CLOCK_DIVIDER = 80; // 300 KHz
|
||||||
|
ADC->PRESCALE = ADC_PRESCALE_VALUE - 1; // divide by 24 for 1MHz.
|
||||||
|
|
||||||
|
ADC->CONTROL = 1;
|
||||||
|
|
||||||
|
// The ON-TIME must always be 10µs or greater - typical On-Time value = 0x000A (10dec)
|
||||||
|
ADC->ON_TIME = 10;
|
||||||
|
|
||||||
|
/*
|
||||||
|
NOTE
|
||||||
|
The ADC analog block requires 6 ADC_Clocks per conversion, and the
|
||||||
|
ADC_Clock must 300kHz or less. With 6 clocks/conversion and a 33.33µs
|
||||||
|
clock period:
|
||||||
|
* The ADC convert time must always be 20µs or greater
|
||||||
|
* If the ADC_Clock is a frequency lower than 300kHz, the convert time
|
||||||
|
must always be 6 ADC_Clock periods or greater
|
||||||
|
* For override mode, extend convert time to 40µs minimum or greater
|
||||||
|
For the convert time:
|
||||||
|
* This delay is a function of the Prescale Clock (typically 1 MHz)
|
||||||
|
* The register must be initialized for proper operation
|
||||||
|
* For a 20µs convert time with 1MHz, value = 0x0014 (20dec)
|
||||||
|
* If convert time is insufficient, inaccurate sample data will result
|
||||||
|
*/
|
||||||
|
ADC->CONVERT_TIME = 1000000 / (115200 / 8) - 1;
|
||||||
|
|
||||||
|
ADC->MODE = 0; // Automatic
|
||||||
|
|
||||||
|
#if ADC_USE_INTERRUPTS
|
||||||
|
ADC->FIFO_CONTROL = 7;
|
||||||
|
#else
|
||||||
|
ADC->FIFO_CONTROL = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if ADC_USE_TIMER
|
||||||
|
ADC->SR_1_HIGH = 0x0000;
|
||||||
|
ADC->SR_1_LOW = (REF_OSC / ADC_PRESCALE_VALUE) / (115200 / 8) + 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ADC->SEQ_1 = 0
|
||||||
|
#if ADC_USE_TIMER
|
||||||
|
| (1 << 15) // sequence based on Timer 1.
|
||||||
|
#else
|
||||||
|
| (0 << 15) // sequence based on convert time.
|
||||||
|
#endif
|
||||||
|
| ADC_CHANS_ENABLED;
|
||||||
|
|
||||||
|
ADC->CONTROL = 0xF001
|
||||||
|
//#if ADC_USE_TIMER
|
||||||
|
| (1 << 1) // Timer 1 enable
|
||||||
|
//#endif
|
||||||
|
;
|
||||||
|
ADC->OVERRIDE = (1 << 8);
|
||||||
|
|
||||||
|
for (n=0; n<=8; n++) {
|
||||||
|
if ((ADC_CHANS_ENABLED >> n) & 1) {
|
||||||
|
gpio_select_function(30 + n, 1); // Function 1 = ADC
|
||||||
|
gpio_set_pad_dir(30 + n, PAD_DIR_INPUT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
164
lib/include/adc.h
Normal file
164
lib/include/adc.h
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
|
||||||
|
* to the MC1322x project (http://mc1322x.devl.org)
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the Institute nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This file is part of libmc1322x: see http://mc1322x.devl.org
|
||||||
|
* for details.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ADC_H
|
||||||
|
#define ADC_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
/* ADC registers are all 16-bit wide with 16-bit access only */
|
||||||
|
#define ADC_BASE (0x8000D000)
|
||||||
|
|
||||||
|
/* Structure-based register definitions */
|
||||||
|
|
||||||
|
struct ADC_struct {
|
||||||
|
union {
|
||||||
|
uint16_t COMP[8];
|
||||||
|
struct {
|
||||||
|
uint16_t COMP_0;
|
||||||
|
uint16_t COMP_1;
|
||||||
|
uint16_t COMP_2;
|
||||||
|
uint16_t COMP_3;
|
||||||
|
uint16_t COMP_4;
|
||||||
|
uint16_t COMP_5;
|
||||||
|
uint16_t COMP_6;
|
||||||
|
uint16_t COMP_7;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
uint16_t BAT_COMP_OVER;
|
||||||
|
uint16_t BAT_COMP_UNDER;
|
||||||
|
union {
|
||||||
|
uint16_t SEQ_1;
|
||||||
|
struct ADC_SEQ_1 {
|
||||||
|
uint16_t CH0:1;
|
||||||
|
uint16_t CH1:1;
|
||||||
|
uint16_t CH2:1;
|
||||||
|
uint16_t CH3:1;
|
||||||
|
uint16_t CH4:1;
|
||||||
|
uint16_t CH5:1;
|
||||||
|
uint16_t CH6:1;
|
||||||
|
uint16_t CH7:1;
|
||||||
|
uint16_t BATT:1;
|
||||||
|
uint16_t :6;
|
||||||
|
uint16_t SEQ_MODE:1;
|
||||||
|
} SEQ_1bits;
|
||||||
|
};
|
||||||
|
union {
|
||||||
|
uint16_t SEQ_2;
|
||||||
|
struct ADC_SEQ_2 {
|
||||||
|
uint16_t CH0:1;
|
||||||
|
uint16_t CH1:1;
|
||||||
|
uint16_t CH2:1;
|
||||||
|
uint16_t CH3:1;
|
||||||
|
uint16_t CH4:1;
|
||||||
|
uint16_t CH5:1;
|
||||||
|
uint16_t CH6:1;
|
||||||
|
uint16_t CH7:1;
|
||||||
|
uint16_t :7;
|
||||||
|
uint16_t SEQ_MODE:1;
|
||||||
|
} SEQ_2bits;
|
||||||
|
};
|
||||||
|
union {
|
||||||
|
uint16_t CONTROL;
|
||||||
|
struct ADC_CONTROL {
|
||||||
|
uint16_t ON:1;
|
||||||
|
uint16_t TIMER1_ON:1;
|
||||||
|
uint16_t TIMER2_ON:1;
|
||||||
|
uint16_t SOFT_RESET:1;
|
||||||
|
uint16_t AD1_FREFHL_EN:1;
|
||||||
|
uint16_t AD2_VREFHL_EN:1;
|
||||||
|
uint16_t :6;
|
||||||
|
uint16_t COMPARE_IRQ_MASK:1;
|
||||||
|
uint16_t SEQ1_IRQ_MASK:1;
|
||||||
|
uint16_t SEQ2_IRQ_MASK:1;
|
||||||
|
uint16_t FIFO_IRQ_MASK:1;
|
||||||
|
} CONTROLbits;
|
||||||
|
};
|
||||||
|
uint16_t TRIGGERS;
|
||||||
|
uint16_t PRESCALE;
|
||||||
|
uint16_t reserved1;
|
||||||
|
uint16_t FIFO_READ;
|
||||||
|
uint16_t FIFO_CONTROL;
|
||||||
|
union {
|
||||||
|
uint16_t FIFO_STATUS;
|
||||||
|
struct ADC_FIFO_STATUS {
|
||||||
|
uint16_t LEVEL:4;
|
||||||
|
uint16_t FULL:1;
|
||||||
|
uint16_t EMPTY:1;
|
||||||
|
uint16_t :10;
|
||||||
|
} FIFO_STATUSbits;
|
||||||
|
};
|
||||||
|
uint16_t reserved2[5];
|
||||||
|
uint16_t SR_1_HIGH;
|
||||||
|
uint16_t SR_1_LOW;
|
||||||
|
uint16_t SR_2_HIGH;
|
||||||
|
uint16_t SR_2_LOW;
|
||||||
|
uint16_t ON_TIME;
|
||||||
|
uint16_t CONVERT_TIME;
|
||||||
|
uint16_t CLOCK_DIVIDER;
|
||||||
|
uint16_t reserved3;
|
||||||
|
union {
|
||||||
|
uint16_t OVERRIDE;
|
||||||
|
struct ADC_OVERRIDE {
|
||||||
|
uint16_t MUX1:4;
|
||||||
|
uint16_t MUX2:4;
|
||||||
|
uint16_t AD1_ON:1;
|
||||||
|
uint16_t AD2_ON:1;
|
||||||
|
uint16_t :6;
|
||||||
|
} OVERRIDEbits;
|
||||||
|
};
|
||||||
|
uint16_t IRQ;
|
||||||
|
uint16_t MODE;
|
||||||
|
uint16_t RESULT_1;
|
||||||
|
uint16_t RESULT_2;
|
||||||
|
};
|
||||||
|
|
||||||
|
static volatile struct ADC_struct * const ADC = (void *) (ADC_BASE);
|
||||||
|
|
||||||
|
#define NUM_ADC_CHAN 9
|
||||||
|
|
||||||
|
#define adc_enable() (ADC->CONTROLbits.ON = 1)
|
||||||
|
#define adc_disable() (ADC->CONTROLbits.ON = 0)
|
||||||
|
#define adc_select_channels(chans) (ADC->SEQ_1 = (ADC->SEQ_1 & 0xFE00) | chans)
|
||||||
|
|
||||||
|
extern uint16_t adc_reading[NUM_ADC_CHAN];
|
||||||
|
void ADC_flush(void);
|
||||||
|
uint16_t ADC_READ(void);
|
||||||
|
void read_scanners(void);
|
||||||
|
void adc_init(void);
|
||||||
|
void adc_service(void);
|
||||||
|
|
||||||
|
#endif
|
|
@ -121,8 +121,8 @@ enum {
|
||||||
#define LFSR 6 /* 1 use polynomial for Turbolink */
|
#define LFSR 6 /* 1 use polynomial for Turbolink */
|
||||||
#define TM 5
|
#define TM 5
|
||||||
|
|
||||||
#define MODE 3
|
#define MACA_MODE 3
|
||||||
#define MODE_MASK bit_mask(2,MODE)
|
#define MODE_MASK bit_mask(2,MACA_MODE)
|
||||||
#define NO_CCA 0
|
#define NO_CCA 0
|
||||||
#define NO_SLOT_CCA 1
|
#define NO_SLOT_CCA 1
|
||||||
#define SLOT_CCA 2
|
#define SLOT_CCA 2
|
||||||
|
|
|
@ -184,7 +184,7 @@ void maca_init(void) {
|
||||||
/* nop, promiscuous, no cca */
|
/* nop, promiscuous, no cca */
|
||||||
*MACA_CONTROL =
|
*MACA_CONTROL =
|
||||||
(prm_mode << PRM) |
|
(prm_mode << PRM) |
|
||||||
(NO_CCA << MODE);
|
(NO_CCA << MACA_MODE);
|
||||||
|
|
||||||
enable_irq(MACA);
|
enable_irq(MACA);
|
||||||
*INTFRC = (1 << INT_NUM_MACA);
|
*INTFRC = (1 << INT_NUM_MACA);
|
||||||
|
|
63
tests/adc.c
Normal file
63
tests/adc.c
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
|
||||||
|
* to the MC1322x project (http://mc1322x.devl.org)
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the Institute nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This file is part of libmc1322x: see http://mc1322x.devl.org
|
||||||
|
* for details.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <mc1322x.h>
|
||||||
|
#include <board.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "adc.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
uint8_t c;
|
||||||
|
|
||||||
|
trim_xtal();
|
||||||
|
uart1_init(INC,MOD,SAMP);
|
||||||
|
adc_init();
|
||||||
|
|
||||||
|
printf("adc test\r\n");
|
||||||
|
|
||||||
|
printf("\x1B[2J"); // clear screen
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
printf("\x1B[H"); // cursor home
|
||||||
|
printf("# Value\r\n");
|
||||||
|
for (c=0; c<NUM_ADC_CHAN; c++) {
|
||||||
|
adc_service();
|
||||||
|
printf("%u %04u\r\n", c, adc_reading[c]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue