Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oscilliscope: Implement two, three, and four channel mode #107

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pslab-core.X/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
},
{ // 2 ADC
// 0 1 CAPTURE_ONE 2 CAPTURE_TWO 3 CAPTURE_DMASPEED
Undefined, OSCILLOSCOPE_CaptureOne, Unimplemented, Unimplemented,
Undefined, OSCILLOSCOPE_CaptureOne, OSCILLOSCOPE_CaptureTwo, Unimplemented,
// 4 CAPTURE_FOUR 5 CONFIGURE_TRIGGER 6 GET_CAPTURE_STATUS 7 GET_CAPTURE_CHANNEL
Unimplemented, Unimplemented, OSCILLOSCOPE_GetCaptureStatus, Unimplemented,
OSCILLOSCOPE_CaptureFour, Unimplemented, OSCILLOSCOPE_GetCaptureStatus, Unimplemented,
// 8 SET_PGA_GAIN 9 GET_VOLTAGE 10 GET_VOLTAGE_SUMMED 11 START_ADC_STREAMING
Unimplemented, MULTIMETER_GetVoltage, MULTIMETER_GetVoltageSummed, Removed,
// 12 SELECT_PGA_CHANNEL 13 CAPTURE_12BIT 14 CAPTURE_MULTIPLE 15 SET_HI_CAPTURE
Unimplemented, Unimplemented, Removed, Unimplemented,
// 16 SET_LO_CAPTURE 17 SET_HI_CAPTURE12 18 SET_LO_CAPTURE12 19 CAPTURE_DMASPEED12
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 20 MULTIPOINT_CAPACITANCE 21 SET_CAP 22 PULSE_TRAIN 23
Unimplemented, Unimplemented, Unimplemented, Undefined,
// 20 MULTIPOINT_CAPACITANCE 21 SET_CAP 22 PULSE_TRAIN 23 CAPTURE_THREE
Unimplemented, Unimplemented, Unimplemented, OSCILLOSCOPE_CaptureThree,
// 24 25 26 27
Undefined, Undefined, Undefined, Undefined,
},
Expand Down
2 changes: 1 addition & 1 deletion pslab-core.X/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern "C" {
*/
#define NUM_PRIMARY_CMDS 11
#define NUM_FLASH_CMDS 4
#define NUM_ADC_CMDS 22
#define NUM_ADC_CMDS 23
#define NUM_SPI_CMDS 7
#define NUM_I2C_CMDS 16
#define NUM_UART2_CMDS 8
Expand Down
71 changes: 58 additions & 13 deletions pslab-core.X/instruments/oscilloscope.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ static uint16_t volatile SAMPLES_CAPTURED;
static uint16_t SAMPLES_REQUESTED;

/* Static function prototypes */
static void Capture(void);
static void ResetTrigger(void);
static void Setup10BitMode(void);
static void SetTimeGap(void);

/**
* @brief Handle trigger and data collection from ADC.
Expand Down Expand Up @@ -76,26 +77,70 @@ void __attribute__((interrupt, no_auto_psv)) _AD1Interrupt(void) {
}

response_t OSCILLOSCOPE_CaptureOne(void) {
uint8_t pin = UART1_Read();
CHANNELS = 0; // Capture one channel.
Capture();
return SUCCESS;
}

response_t OSCILLOSCOPE_CaptureTwo(void) {
CHANNELS = 1; // Capture two channels.
Capture();
return SUCCESS;
}

response_t OSCILLOSCOPE_CaptureThree(void) {
CHANNELS = 2; // Capture four channels, but ignore the fourth.
Capture();
return SUCCESS;
}

response_t OSCILLOSCOPE_CaptureFour(void) {
CHANNELS = 3; // Capture four channels.
Capture();
return SUCCESS;
}

static void Capture(void) {
uint8_t config = UART1_Read();
SAMPLES_REQUESTED = UART1_ReadInt();
DELAY = UART1_ReadInt();
/*
* First channel input map:
* CH1: 3,
* CH2: 0,
* CH3: 1,
* MIC: 2,
* RES: 7,
* CAP: 5,
* VOL: 8,
*/
uint8_t ch0sa = config & 0x0F;
/*
* Second, third, and fourth channels input map:
* CH2, CH3, MIC: 0
* CH1, CH2, CAP: 1
*/
uint8_t ch123sa = config & 0x10;
uint8_t trigger = config & 0x80;

CHANNELS = 0; //capture one channel
AD1CON2bits.CHPS = 0;
ADC1_SetOperationMode(ADC1_10BIT_SIMULTANEOUS_MODE, pin & 0x7F, 0);
AD1CON2bits.CHPS = 0;
AD1CON2bits.CHPS = CHANNELS;
ADC1_SetOperationMode(ADC1_10BIT_SIMULTANEOUS_MODE, ch0sa, ch123sa);
AD1CON2bits.CHPS = CHANNELS;

if (pin & 0x80) ResetTrigger();
if (trigger) ResetTrigger();
else TRIGGERED = 1;

int i;
for (i = 0; i <= CHANNELS; i++) {
BUFFER_IDX[i] = &BUFFER[i * SAMPLES_REQUESTED];
}

SAMPLES_CAPTURED = 0;
BUFFER_IDX[0] = &BUFFER[0];
Setup10BitMode();
SetTimeGap();
ADC1_InterruptFlagClear();
ADC1_InterruptEnable();
LED_SetLow();

return SUCCESS;
LED_SetLow();
}

static void ResetTrigger(void) {
Expand All @@ -104,7 +149,7 @@ static void ResetTrigger(void) {
TRIGGERED = 0;
}

static void Setup10BitMode(void) {
static void SetTimeGap(void) {
T5CONbits.TCKPS = 1;
TMR5_Period16BitSet(DELAY - 1);
TMR5 = 0;
Expand All @@ -113,7 +158,7 @@ static void Setup10BitMode(void) {

response_t OSCILLOSCOPE_GetCaptureStatus(void) {
uint8_t conversion_done;
conversion_done = SAMPLES_CAPTURED == SAMPLES_REQUESTED ? 1 : 0;
conversion_done = SAMPLES_CAPTURED == SAMPLES_REQUESTED;
UART1_Write(conversion_done);
UART1_WriteInt(SAMPLES_CAPTURED);
return SUCCESS;
Expand Down
31 changes: 28 additions & 3 deletions pslab-core.X/instruments/oscilloscope.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,39 @@
*
* @description
* This command function takes three arguments over serial:
* 1. (uint8) The pin to sample
* 2. (uint16) The number of samples to capture
* 3. (uint16) The time to wait between samples in instruction cycles
* 1. (uint8) Configuration byte; The first four LSB encode channel one mapping,
* the fifth LSB encodes channels two, three, and four mappings, and
* the MSB enables the trigger.
* 2. (uint16) The number of samples to capture.
* 3. (uint16) The time to wait between samples in instruction cycles.
* It returns nothing over serial.
* It sends an acknowledge byte (SUCCESS).
*
* @return SUCCESS
*/
response_t OSCILLOSCOPE_CaptureOne(void);

/**
* @brief Capture samples on two channels simultaneously.
*/
response_t OSCILLOSCOPE_CaptureTwo(void);

/**
* @brief Capture samples on three channels simultaneously.
*
* @description
* Since the MCU only supports 1, 2, or 4 simultaneous channels, this is
* accomplished by sampling four channels simultaneously and discarding the
* samples from the fourth. This function therefore has the same sample rate
* as OSCILLOSCOPE_CaptureFour, but greater sample depth.
*/
response_t OSCILLOSCOPE_CaptureThree(void);

/**
* @brief Capture samples on four channels simultaneously.
*/
response_t OSCILLOSCOPE_CaptureFour(void);

/**
* @brief
* Send capture progress.
Expand Down