Commit 5fe09109 authored by Erik Strand's avatar Erik Strand

Remove call to pinPeripheral

parent 87cf858b
#include "wiring_private.h"
#define LED_GREEN 23 #define LED_GREEN 23
#define CHARGE_LINE_0 14 #define CHARGE_LINE_0 14
#define SENSE_LINE_0 2 #define SENSE_LINE_0 2
...@@ -25,22 +23,43 @@ static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to) ...@@ -25,22 +23,43 @@ static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to)
return value << (to-from); return value << (to-from);
} }
void init_pin_adc(uint32_t pin) {
uint8_t const pinPort = GetPort(pin);
uint8_t const pinNum = GetPin(pin);
// Preserve state of pullup/pulldown enable, clear the rest of the bits
uint8_t pinCfg = (PORT->Group[pinPort].PINCFG[pinNum].reg & PORT_PINCFG_PULLEN);
if ( pinNum & 1 ) // is pin odd?
{
// Get whole current setup for both odd and even pins and remove odd one, then set new muxing
uint32_t temp = (PORT->Group[pinPort].PMUX[pinNum >> 1].reg) & PORT_PMUX_PMUXE( 0xF ) ;
PORT->Group[pinPort].PMUX[pinNum >> 1].reg = temp|PORT_PMUX_PMUXO(PER_ANALOG) ;
}
else // even pin
{
// Get whole current setup for both odd and even pins and remove even one, then set new muxing
uint32_t temp = (PORT->Group[pinPort].PMUX[pinNum >> 1].reg) & PORT_PMUX_PMUXO( 0xF ) ;
PORT->Group[pinPort].PMUX[pinNum >> 1].reg = temp|PORT_PMUX_PMUXE(PER_ANALOG) ;
}
pinCfg |= PORT_PINCFG_PMUXEN; // Enable peripheral mux
// Set pin drive strength, enable/disable pull resistor, enable/disable INEN, and enable/disable the peripheral mux
PORT->Group[pinPort].PINCFG[pinNum].reg = (uint8_t)pinCfg ;
}
uint32_t bm_analogRead( uint32_t pin ) uint32_t bm_analogRead( uint32_t pin )
{ {
uint32_t valueRead = 0; uint32_t valueRead = 0;
// pinPeripheral now handles disabling the DAC (if active) // Start conversion
if ( pinPeripheral(pin, PIO_ANALOG_ADC) == RET_STATUS_OK ) ADC->SWTRIG.bit.START = 1;
{ syncADC();
// Start conversion
ADC->SWTRIG.bit.START = 1;
syncADC();
// Store the value // Store the value
while (ADC->INTFLAG.bit.RESRDY == 0); // Waiting for conversion to complete while (ADC->INTFLAG.bit.RESRDY == 0); // Waiting for conversion to complete
valueRead = ADC->RESULT.reg; valueRead = ADC->RESULT.reg;
}
return mapResolution(valueRead, _ADCResolution, _readResolution); return mapResolution(valueRead, _ADCResolution, _readResolution);
} }
...@@ -55,6 +74,10 @@ void setup() { ...@@ -55,6 +74,10 @@ void setup() {
// Initialize the ADC // Initialize the ADC
initADC(); initADC();
// Everything seems to function if I don't do this, but I'm leaving it in until I have a chance
// to investigate what's happening.
init_pin_adc(SENSE_LINE_0);
// Selection for the positive ADC input // Selection for the positive ADC input
ADC->INPUTCTRL.bit.MUXPOS = GetADC(SENSE_LINE_0); ADC->INPUTCTRL.bit.MUXPOS = GetADC(SENSE_LINE_0);
syncADC(); syncADC();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment