Skip to content

Commit

Permalink
fix unresponsive buttons
Browse files Browse the repository at this point in the history
Fix unresponsive buttons by aboarting sampling on IRQ...
  • Loading branch information
michar71 committed Mar 27, 2018
1 parent da2c365 commit b19af8b
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 70 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Complete rewrite of DSO-150 Firmware

- No longer DSO-138 compatible (Sorry...)
- Rewritten for Atollic trueSTUDIO Yea... Real debugging)
- Rewritten for Atollic trueSTUDIO (Yea... Real debugging)
- Now using internal digital trigger through ADC IRQ (So trigger level can now be correlated to real voltage)
- Lots of new features

Expand Down Expand Up @@ -87,7 +87,7 @@ In Loop mode samples will be added on the right side of the waveform. The buffer
Open DSO-150 should build directly after opening the project in the free STM32 version of Atollic trueSTUDIO.
https://atollic.com/truestudio/

It should also compile under the System Workbench for STM32 after creating a new project and importing the soruce files but I haven't tried that...
It should also compile under the System Workbench for STM32 after creating a new project and importing the source files but I haven't tried that...

With an STLink V2 probe it is very easy to both program and debug the scope via the DebugWire Link.
(I haven't tried uploading hex-files through the serial port with the STM32 Bootloader but there's no reason it shouldn't work...)
Expand Down
1 change: 1 addition & 0 deletions Src/awrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void timerSetPeriod(TIM_HandleTypeDef *htim,uint32_t ms_period)
uint16_t overflow = (uint16_t)((period_cyc + (prescaler / 2)) / prescaler);
timerSetPrescaler(htim,prescaler);
timerSetOverflow(htim,overflow);
timerSetCount(htim,0);

}

Expand Down
92 changes: 59 additions & 33 deletions Src/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <string.h>

extern "C" void HAL_GPIO_TRIG_IRQHandler(uint16_t GPIO_Pin);
extern "C" void HAL_SAMPLING_IRQHandler(void);

extern TIM_HandleTypeDef htim4; //TIMER Sample Timing
extern TIM_HandleTypeDef htim2; //TIMER ScanTimeout
extern ADC_HandleTypeDef hadc1; //Analog ADC

Expand All @@ -20,6 +22,7 @@ extern const char* rngNames[];
extern const char* tbNames[];

extern volatile bool hold;
volatile uint16_t iIndex = 0;

extern t_config config;
extern t_Stats wStats;
Expand All @@ -30,30 +33,32 @@ uint16_t ch1Capture[NUM_SAMPLES] = {0};
uint16_t bitStore[NUM_SAMPLES] = {0};

volatile bool keepSampling = true;
volatile bool minSamplesAcquired;
volatile uint16_t lCtr = 0;
volatile bool triggered = false;
volatile bool hold = false;

bool minSamplesAcquired;
long prevTime = 0;
int16_t sDly, tDly;
uint16_t tIndex = 0;
uint16_t sIndex = 0;
volatile bool triggered = false;
volatile bool hold = false;

long samplingTime;

bool trigger_lo_hi = false;
bool trigger_hi_lo = false;
uint16_t trigger_level = 2048;


// hold pointer references for updating variables in memory
uint16_t *sIndexPtr = &sIndex;
volatile bool *triggeredPtr = &triggered;

//---------------------
void stopSampling(void)
//---------------------
{
#ifdef USE_TIMER_SAMPLE
minSamplesAcquired = true;
#else
keepSampling = false;
#endif
}


Expand All @@ -67,8 +72,6 @@ extern "C" void HAL_GPIO_TRIG_IRQHandler(uint16_t GPIO_Pin)
}
}



void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
scanTimeoutISR();
Expand All @@ -81,14 +84,12 @@ void setTriggerSourceAndDir(uint8_t source,uint8_t dir)
uint32_t pin = 0;
uint32_t mode = 0;

HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
HAL_NVIC_DisableIRQ(ADC1_2_IRQn);
// trigger changed, break out from previous sampling loop
stopSampling();

//Detach old source
setPinMode(TRIG_GPIO_Port,TRIG_Pin,GPIO_MODE_INPUT,GPIO_PULLDOWN,GPIO_SPEED_FREQ_LOW);
setPinMode(D1_GPIO_Port,D1_Pin,GPIO_MODE_INPUT,GPIO_PULLDOWN,GPIO_SPEED_FREQ_LOW);
setPinMode(D2_GPIO_Port,D2_Pin,GPIO_MODE_INPUT,GPIO_PULLDOWN,GPIO_SPEED_FREQ_LOW);
setPinMode(D3_GPIO_Port,D3_Pin,GPIO_MODE_INPUT,GPIO_PULLDOWN,GPIO_SPEED_FREQ_LOW);
Expand Down Expand Up @@ -159,14 +160,33 @@ void setTriggerSourceAndDir(uint8_t source,uint8_t dir)
void sampleWaves(bool wTimeout)
// ------------------------
{
#ifdef USE_TIMER_SAMPLE
if (!minSamplesAcquired)
return;
#endif

// setup timed interrupt to terminate scanning if trigger not found
if(wTimeout)
startScanTimeout(tDly);

// start sampling loop - until timeout or trigger
// clear old dataset

samplingTime = 0;
triggered = false;
sIndex = 0;
iIndex = 0;
prevTime = micros();
lCtr = 0;
keepSampling = true;
minSamplesAcquired = false;


#ifndef USE_TIMER_SAMPLE
startSampling(sDly);
// disable scan timeout timer
timerPause(&htim2);
#endif
}


Expand All @@ -191,13 +211,13 @@ void triggerISR(void)
// skip this trigger if min samples not acquired
if(!minSamplesAcquired && (sIndex < NUM_SAMPLES/2))
return;

// snap the position where trigger occurred
tIndex = sIndex;
// avoid multiple triggering
triggered = true;
}
}
}

// ------------------------
void scanTimeoutISR(void)
Expand All @@ -214,16 +234,8 @@ void scanTimeoutISR(void)
void startSampling(int16_t lDelay)
// ------------------------
{
uint16_t lCtr = 0;
int16_t cnt = lDelay;

// clear old dataset
keepSampling = true;
minSamplesAcquired = false;
samplingTime = 0;
triggered = false;
sIndex = 0;
prevTime = micros();
int16_t cnt = lDelay;

if(lDelay < 0) //Delay < 0 (What does that even mean??? As fast as possible?)
{
Expand Down Expand Up @@ -505,8 +517,6 @@ void autoCal(void)
//Start capturing
hold = false;

//Disable Waveform output
setTestPin(TESTMODE_GND);
//Capture full range for Zero Cal
loopThroughRange(RNG_20V,RNG_5mV+1,zeroOffset);
hold = true;
Expand Down Expand Up @@ -614,31 +624,47 @@ void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
}
}

/*
void adc_irq(void)

extern "C" void HAL_SAMPLING_IRQHandler(void)
{
__HAL_TIM_CLEAR_IT(&htim4, TIM_IT_CC1);
if (minSamplesAcquired)
{
__HAL_TIM_CLEAR_IT(&htim4, TIM_IT_CC1);
sIndex = iIndex;
return;
}

if (triggered) //After Triggered
{
lCtr++;
if(lCtr == (NUM_SAMPLES/2)) //Fill up half the buffer with samples so Trigger point aligns at middle of sample buffer..
{
//Stop sampling by disabling timer?
//TODO
minSamplesAcquired = true;
__HAL_TIM_CLEAR_IT(&htim4, TIM_IT_CC1);
sIndex = iIndex;
return;
}
}
//If sample index rolls over reset to zero and record start sampling time
if (sIndex == NUM_SAMPLES)
if (iIndex == NUM_SAMPLES)
{
sIndex = 0;
iIndex = 0;
snapMicros();
}

bitStore[sIndex] = GPIOB->IDR; //Store GPIO
ch1Capture[sIndex] = hadc1.Instance->DR; //Store ADC (Resets IrQ)
sIndex++; //Increase index
//Sample analog data
ch1Capture[iIndex] = hadc1.Instance->DR; //Store ADC

//Need to make sure pins are in input mode as we could hit IRQ during display routine...

bitStore[iIndex] = GPIOB->IDR; //Store GPIO


//Increase index
iIndex++;
}
/*
Expand Down
40 changes: 28 additions & 12 deletions Src/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
extern t_config config;
extern volatile bool triggered;
extern volatile bool hold;
extern volatile bool minSamplesAcquired;
static uint32_t cnt = SAVE_COUNTER;
// ------------------------
void controlLoop()
Expand All @@ -37,32 +38,38 @@ void controlLoop()
indicateCapturing();
// blocking call - until trigger
sampleWaves(false);
indicateCapturingDone();
hold = true;

// request repainting of screen labels in next draw cycle
repaintLabels();
// draw the waveform
#ifdef USE_TIMER_SAMPLE
if(minSamplesAcquired)
{
indicateCapturingDone();
drawWaves();
}
#else
indicateCapturingDone();
drawWaves();
#endif
// request repainting of screen labels in next draw cycle
repaintLabels();
blinkLED();

// freeze display
while(hold)
{
if (pollControlSwitches())
{
clearWaves();
repaintLabels();
drawWaves();
}
}
if (pollControlSwitches())
{
clearWaves();
repaintLabels();
drawWaves();
}
}

// update display indicating hold released
drawLabels();
}

// process any long pending operations which cannot be serviced in ISR

cnt--;
if (cnt==0)
{
Expand All @@ -81,8 +88,17 @@ void captureDisplayCycle(bool wTimeOut)
pollControlSwitches();

// draw the waveform
#ifdef USE_TIMER_SAMPLE
if(minSamplesAcquired)
{
indicateCapturingDone();
drawWaves();
}
#else
indicateCapturingDone();
drawWaves();
#endif

// inter wait before next sampling
if(triggered)
blinkLED();
Expand Down
16 changes: 15 additions & 1 deletion Src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@
extern t_config config;
extern uint8_t currentFocus;
extern volatile bool hold;
extern volatile bool keepSampling;

int encoderVal = 0;

extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
readEncoderISR();
//if encoder
if((GPIO_Pin == DB0_Pin) || (GPIO_Pin == DB1_Pin))
{
keepSampling = false;
readEncoderISR();
}
else if (GPIO_Pin == DB7_Pin) //OK button
{
hold = true;
}
else
{
keepSampling = false;
}
}


Expand Down
2 changes: 1 addition & 1 deletion Src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <stdint.h>

#define FIRMWARE_VERSION "2.0"
#define FIRMWARE_VERSION "3.0"
#define FIRMWARE_TARGET "C++ DSO-150"
#define FIRMWARE_INFO "DSO-150 Hardware by JYE-Tech"

Expand Down
15 changes: 11 additions & 4 deletions Src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "io.hpp"
#include "awrap.hpp"

extern TIM_HandleTypeDef htim4; //TIMER Sample Timing
extern TIM_HandleTypeDef htim2; //TIMER ScanTimeout

extern t_config config;
Expand All @@ -17,10 +18,11 @@ extern volatile bool hold;

uint8_t rangePos;

// sampling delay table in quarter-microseconds
// sampling delay table in quarter-microseconds (not sure where the quarter-microseconds come from it's just a delayloop...)
const int16_t samplingDelay[] = {-1, 0, 14, 38, 86, 229, 468, 948, 2385, 4776, 9570, 23940,0x7FFE};
const uint16_t timeoutDelayMs[] = {50, 50, 50, 100, 100, 100, 150, 250, 500, 1000, 2000, 4500,0x7FFE};

//Timeout delay in milliseconds
const uint16_t timeoutDelayMs[] = {75, 75, 75, 150, 150, 150, 150, 250, 500, 1000, 2000, 4000,0x7FFE};
const uint16_t samplingTimerDelay[] = {20, 30, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000,0xFFFF};
// ------------------------
void resetParam()
// ------------------------
Expand Down Expand Up @@ -562,7 +564,12 @@ void changeXCursor(int16_t xPos)
void setSamplingRate(uint8_t timeBase)
// ------------------------
{
sDly = samplingDelay[timeBase];
#ifdef USE_TIMER_SAMPLE
timerSetPeriod(&htim4,samplingTimerDelay[timeBase]);
#else
sDly = samplingDelay[timeBase];
#endif

tDly = timeoutDelayMs[timeBase];
// sampling rate changed, break out from previous sampling loop
stopSampling();
Expand Down
Loading

0 comments on commit b19af8b

Please sign in to comment.