Skip to content

Commit

Permalink
refactor: revert adc interrupt to adc from osc
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudyPadmal committed Oct 9, 2021
1 parent 003b9ef commit 6fc80a9
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 128 deletions.
28 changes: 14 additions & 14 deletions pslab-core.X/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,19 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
Undefined, Undefined, Undefined, Undefined,
},
{ // 11 COMMON
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
Undefined, Unimplemented, Unimplemented, Unimplemented,
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
Unimplemented, VERSION_GetVersion, Undefined, Undefined,
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
BUFFER_Retrieve, Unimplemented, Unimplemented, Unimplemented,
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
Unimplemented, Undefined, Unimplemented, Unimplemented,
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
Undefined, Unimplemented, MULTIMETER_GetCapacitance, Unimplemented,
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
Unimplemented, VERSION_GetVersion, Undefined, Undefined,
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
BUFFER_Retrieve, Unimplemented, Unimplemented, Unimplemented,
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
Unimplemented, Undefined, Unimplemented, Unimplemented,
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
},
};
152 changes: 132 additions & 20 deletions pslab-core.X/instruments/multimeter.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,75 @@
#include "../commands.h"
#include "../bus/uart/uart1.h"
#include "../helpers/delay.h"
#include "../registers/converters/adc1.h"
#include "../registers/converters/ctmu.h"
#include "../registers/memory/dma.h"
#include "../bus/uart/uart1.h"
#include "logicanalyzer.h"
#include "../registers/system/pin_manager.h"
#include "../registers/timers/tmr5.h"

#define CHARGE 0
#define DISCHARGE 1

/* Function prototypes */
void ChargeCapacitor(uint8_t charge, uint16_t period);
void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
uint16_t charge_time);
void GetCapacitance_ConfigADC_CTMU_TMR5();

void ChargeCapacitor(uint8_t charge, uint16_t period) {
CAP_OUT_SetDigitalOutput();
CAP_OUT_SetDigital();

if (charge) {
CAP_OUT_SetHigh();
} else {
CAP_OUT_SetLow();
}

// Wait out until the capacitor is charged or discharged
DELAY_us(period);

CAP_OUT_SetLow();
// Switch to high impedance state
CAP_OUT_SetDigitalInput();
CAP_OUT_SetAnalog();
}

void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
uint16_t charge_time) {
CTMU_Initialize();
// Enables edge delay generation
CTMUCON1bits.TGEN = 1;
// Set current level for current source
CTMUICONbits.ITRIM = trim;
// Current range for current source (0.53uA for base current)
CTMUICONbits.IRNG = current_range;

TMR5_Initialize();
TMR5_PrescalerSet(TMR_PRESCALER_64);
TMR5_Period16BitSet(charge_time);
}

void GetCapacitance_ConfigADC_CTMU_TMR5() {
// Begin ADC sampling
ADC1_Enable();
ADC1_InterruptEnable();
DELAY_us(20);
ADC1_SoftwareTriggerEnable();

CTMU_Enable();
DELAY_us(1000);
// Analog current source output is grounded
CTMUCON1bits.IDISSEN = 1;
DELAY_us(1500);
// Disconnect current source from ground
CTMUCON1bits.IDISSEN = 0;
// Edge 1 has occurred and capacitor is now being charged
CTMUCON2bits.EDG1STAT = 1;

TMR5_Start();
TMR5_WaitForInterruptEvent();
}

response_t MULTIMETER_GetVoltage(void) {

Expand Down Expand Up @@ -38,39 +104,85 @@ response_t MULTIMETER_GetVoltage(void) {
DELAY_us(20);
ADC1_SoftwareTriggerEnable();

while (!ADC1_IsConversionComplete(channel_CTMU));
while (!ADC1_IsConversionComplete());

UART1_WriteInt(ADC1_ConversionResultGet(channel_CTMU));

return SUCCESS;
}

response_t MULTIMETER_GetVoltageSummed(void) {

uint8_t channel = UART1_Read();

ADC1_SetOperationMode(ADC1_12BIT_AVERAGING_MODE, channel, 0);

ADC1_Enable();
DELAY_us(20);
ADC1_AutomaticSamplingEnable();

ADC1_InterruptFlagClear();
while (!_AD1IF);
ADC1_InterruptFlagClear();

while (!ADC1_IsConversionComplete(channel_CTMU));


ADC1_WaitForInterruptEvent();

while (!ADC1_IsConversionComplete());

ADC1_AutomaticSamplingDisable();
ADC1_Disable();

uint16_t voltage_sum =
(ADC1BUF0) + (ADC1BUF1) + (ADC1BUF2) + (ADC1BUF3) +
(ADC1BUF4) + (ADC1BUF5) + (ADC1BUF6) + (ADC1BUF7) +
(ADC1BUF8) + (ADC1BUF9) + (ADC1BUFA) + (ADC1BUFB) +
uint16_t voltage_sum =
(ADC1BUF0) + (ADC1BUF1) + (ADC1BUF2) + (ADC1BUF3) +
(ADC1BUF4) + (ADC1BUF5) + (ADC1BUF6) + (ADC1BUF7) +
(ADC1BUF8) + (ADC1BUF9) + (ADC1BUFA) + (ADC1BUFB) +
(ADC1BUFC) + (ADC1BUFD) + (ADC1BUFE) + (ADC1BUFF);

UART1_WriteInt(voltage_sum);

return SUCCESS;
}
}

response_t MULTIMETER_ChargeCapacitor(void) {

uint8_t charge = UART1_Read();
uint16_t period = UART1_ReadInt();

ChargeCapacitor(charge, period);

return SUCCESS;
}

response_t MULTIMETER_GetCapacitance(void) {

uint8_t current_range = UART1_Read();
uint8_t trim = UART1_Read();
uint16_t charge_time = UART1_ReadInt();

LED_SetLow();

uint16_t reading = 0;

ADC1_SetOperationMode(ADC1_CTMU_MODE, 5, 0);
// Initiate CTMU and TMR5 registers to measure capacitance discharge rate
GetCapacitance_InitCTMU_TMR5(current_range, trim, charge_time);
// Fully discharge the measuring capacitor
ChargeCapacitor(DISCHARGE, 50000);
// Configure ADC, CTMU and TMR5 register bits to start measuring
GetCapacitance_ConfigADC_CTMU_TMR5();
// Stop sampling
ADC1_SoftwareTriggerDisable();
// Pause charging the capacitor
CTMUCON2bits.EDG1STAT = 0;

ADC1_WaitForInterruptEvent();
while (!ADC1_IsConversionComplete());

reading = (ADC1BUF0) & 0xFF;

// Reset modules
CTMU_Initialize();
ADC1_Disable();

UART1_WriteInt(reading);

LED_SetHigh();

return SUCCESS;
}
12 changes: 12 additions & 0 deletions pslab-core.X/instruments/multimeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ extern "C" {
* @return SUCCESS
*/
response_t MULTIMETER_GetVoltageSummed(void);

/**
* @brief Charge or discharge capacitor through a resistor
* @return SUCCESS
*/
response_t MULTIMETER_ChargeCapacitor(void);

/**
* @brief Use CTMU to measure capacitance value and read it
* @return SUCCESS
*/
response_t MULTIMETER_GetCapacitance(void);

#ifdef __cplusplus
}
Expand Down
69 changes: 2 additions & 67 deletions pslab-core.X/instruments/oscilloscope.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,12 @@
#include "../helpers/buffer.h"
#include "../commands.h"

#define MAX_CHANNELS 4

/* Static globals */
static uint8_t volatile TRIGGERED = 0;
static uint8_t volatile TRIGGER_READY = 0;
static uint8_t TRIGGER_CHANNEL = 0;
static uint8_t CHANNELS = 0;

static uint16_t DELAY;
static const uint16_t TRIGGER_TIMEOUT = 50000;
static uint16_t volatile TRIGGER_WAITING = 0;
static uint16_t TRIGGER_LEVEL = 0;
static uint16_t TRIGGER_PRESCALER = 0;

static int16_t volatile* volatile BUFFER_IDX[MAX_CHANNELS];
static uint16_t volatile* ADCVALS[MAX_CHANNELS] = {
&ADC1BUF0, &ADC1BUF1, &ADC1BUF2, &ADC1BUF3
};
static uint16_t volatile SAMPLES_CAPTURED;
static uint16_t SAMPLES_REQUESTED;

/* Static function prototypes */
static void Capture(void);
static void ResetTrigger(void);
static void SetTimeGap(void);
static void SetCS(uint8_t channel, uint8_t value);

/**
* @brief Handle trigger and data collection from ADC.
*
* @description
* This interrupt handler is called every time the ADC finishes a conversion, if
* the ADC interrupt is enabled. It checks if the trigger condition is
* fulfilled, and if so copies ADC values into the buffer.
*/
void __attribute__((interrupt, no_auto_psv)) _AD1Interrupt(void) {
uint16_t adval;
int i;

ADC1_InterruptFlagClear();
LED_SetHigh();

if (TRIGGERED) {
for (i = 0; i <= CHANNELS; i++) *(BUFFER_IDX[i]++) = *ADCVALS[i];

SAMPLES_CAPTURED++;
LED_SetLow();
if (SAMPLES_CAPTURED == SAMPLES_REQUESTED) {
ADC1_InterruptDisable();
LED_SetHigh();
}
} else {
for (i = 0; i < MAX_CHANNELS; i++) {
if (TRIGGER_CHANNEL & 1 << i) adval = *ADCVALS[i];
}

// If the trigger hasn't timed out, check for trigger condition.
if (TRIGGER_WAITING < TRIGGER_TIMEOUT) {
TRIGGER_WAITING += (DELAY >> TRIGGER_PRESCALER);
if (!TRIGGER_READY && adval > TRIGGER_LEVEL + 10)TRIGGER_READY = 1;
else if (adval <= TRIGGER_LEVEL && TRIGGER_READY) {
TRIGGERED = 1;
}
}
// If the trigger has timed out, then proceed to data acquisition.
else {
TRIGGERED = 1;
}
}
}

response_t OSCILLOSCOPE_CaptureOne(void) {
CHANNELS = 0; // Capture one channel.
Capture();
Expand Down Expand Up @@ -126,7 +61,7 @@ static void Capture(void) {
SAMPLES_CAPTURED = 0;
BUFFER_IDX[0] = &BUFFER[0];
SetTimeGap();
LED_SetLow();
LED_SetLow();
}

response_t OSCILLOSCOPE_CaptureDMA(void) {
Expand Down Expand Up @@ -164,7 +99,7 @@ static void SetTimeGap(void) {
TMR5_Initialize();
TMR5_StopWhenIdle();
TMR5_Period16BitSet(DELAY - 1);
TMR5_SetPrescaler(TMR5_PRESCALER_8);
TMR5_SetPrescaler(TMR_PRESCALER_8);
TMR5_InterruptFlagClear();
TMR5_InterruptDisable();
TMR5_Start();
Expand Down
3 changes: 3 additions & 0 deletions pslab-core.X/nbproject/configurations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<logicalFolder name="converters" displayName="converters" projectFiles="true">
<itemPath>registers/converters/adc1.h</itemPath>
<itemPath>registers/converters/adc_module_features.h</itemPath>
<itemPath>registers/converters/ctmu.h</itemPath>
</logicalFolder>
<logicalFolder name="memory" displayName="memory" projectFiles="true">
<itemPath>registers/memory/dma.h</itemPath>
Expand All @@ -69,6 +70,7 @@
<itemPath>registers/timers/tmr1.h</itemPath>
<itemPath>registers/timers/tmr2.h</itemPath>
<itemPath>registers/timers/tmr5.h</itemPath>
<itemPath>registers/timers/timer_params.h</itemPath>
</logicalFolder>
</logicalFolder>
<logicalFolder name="sdcard" displayName="sdcard" projectFiles="true">
Expand Down Expand Up @@ -142,6 +144,7 @@
</logicalFolder>
<logicalFolder name="converters" displayName="converters" projectFiles="true">
<itemPath>registers/converters/adc1.c</itemPath>
<itemPath>registers/converters/ctmu.c</itemPath>
</logicalFolder>
<logicalFolder name="memory" displayName="memory" projectFiles="true">
<itemPath>registers/memory/dma.c</itemPath>
Expand Down
Loading

0 comments on commit 6fc80a9

Please sign in to comment.