diff --git a/buttons.h b/buttons.h index 0c84998..254994b 100644 --- a/buttons.h +++ b/buttons.h @@ -27,8 +27,8 @@ #define BUTTON_AUTOREPEAT_INITIAL 100 // how many samples must read pressed before autorepeat starts #define BUTTON_AUTOREPEAT_NEXT 10 // how many samples must read pressed before the next repetition -#define JOYSTICK_UPPER_PRESS_THRESHOLD 2100 // above this ADC value, button is pressed -#define JOYSTICK_UPPER_RELEASE_THRESHOLD 2000 // below this ADC value, button is released +#define JOYSTICK_UPPER_PRESS_THRESHOLD 2200 // above this ADC value, button is pressed +#define JOYSTICK_UPPER_RELEASE_THRESHOLD 2100 // below this ADC value, button is released #define JOYSTICK_LOWER_PRESS_THRESHOLD 1700 // below this ADC value, button is pressed #define JOYSTICK_LOWER_RELEASE_THRESHOLD 1800 // above this ADC value, button is released diff --git a/main.c b/main.c index 865b60b..cbe6a4f 100644 --- a/main.c +++ b/main.c @@ -49,6 +49,20 @@ const float gVoltageScale[VOLTAGE_SCALES] = { 0.1, 0.2, 0.5, 1., 2. }; +#define TIME_SCALES 6 +const char * const gTimeScaleStr[TIME_SCALES] = { +"100 ms", "50 ms", "20 ms", " 10 ms", "50 us", "20 us" +}; + +const uint64_t gTImeScale[TIME_SCALES] = { + 100 * 1000 / PIXELS_PER_DIV, + 50 * 1000 / PIXELS_PER_DIV, + 20 * 1000 / PIXELS_PER_DIV, + 10 * 1000 / PIXELS_PER_DIV, + 50 / PIXELS_PER_DIV, + 20 / PIXELS_PER_DIV, +}; + uint32_t cputime_unloaded; int Trigger(bool rising); @@ -129,6 +143,7 @@ int main(void) { IntMasterEnable(); uint8_t voltage_scale = 0; + uint8_t time_scale = 0; uint8_t rising = 1; while (true) { @@ -154,6 +169,14 @@ int main(void) { case Down: // previous scale voltage_scale = (voltage_scale + VOLTAGE_SCALES - 1) % VOLTAGE_SCALES; break; + case Right: // next scale + time_scale = (time_scale + 1) % TIME_SCALES; + set_frequency(gTImeScale[time_scale]); + break; + case Left: // previous scale + time_scale = (time_scale + TIME_SCALES - 1) % TIME_SCALES; + set_frequency(gTImeScale[time_scale]); + break; } } @@ -176,6 +199,7 @@ int main(void) { // info GrContextForegroundSet(&sContext, ClrWheat); GrStringDraw(&sContext, gVoltageScaleStr[voltage_scale], /*length*/ -1, /*x*/ 0, /*y*/ 0, /*opaque*/ false); + GrStringDraw(&sContext, gTimeScaleStr[time_scale], /*length*/ -1, /*x*/ 60, /*y*/ 0, /*opaque*/ false); snprintf(str, sizeof(str), "CPU Load %.1f%%", usage_percent); GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 10, /*opaque*/ false); diff --git a/sampling.c b/sampling.c index c03fb79..2a0d432 100644 --- a/sampling.c +++ b/sampling.c @@ -6,6 +6,7 @@ #include "driverlib/gpio.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" +#include "driverlib/timer.h" #include "sysctl_pll.h" #include "inc/hw_types.h" #include "inc/hw_memmap.h" @@ -13,6 +14,8 @@ #include "buttons.h" #include "sampling.h" +extern uint32_t gSystemClock; // [Hz] system clock frequency + // latest sample index void ADC_ISR(void) { @@ -41,6 +44,9 @@ void start_sampler() { // initialize ADC peripherals SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC1); + + // timer for low-speed + SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); // ADC clock uint32_t pll_frequency = SysCtlFrequencyGet(CRYSTAL_FREQUENCY); uint32_t pll_divisor = @@ -64,3 +70,25 @@ void start_sampler() { // enable ADC1 sequence 0 interrupt in int. controller IntEnable(INT_ADC1SS0); } + +void set_frequency(uint64_t microseconds) { + if (microseconds == 1) { + ADCSequenceDisable(ADC1_BASE, 0); + // don't use a timer for high speed + ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_ALWAYS, 0); + ADCSequenceEnable(ADC1_BASE, 0); + return; + } + + // initialize timer 2 in periodic mode + TimerDisable(TIMER2_BASE, TIMER_BOTH); + TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC); + #define MICROSECONDS_PER_SECOND 1000000 + TimerLoadSet(TIMER2_BASE, TIMER_A, (uint32_t) ((uint64_t) gSystemClock * microseconds / MICROSECONDS_PER_SECOND) - 1); + TimerControlTrigger(TIMER2_BASE, TIMER_A, true); + TimerEnable(TIMER2_BASE, TIMER_A); + + ADCSequenceDisable(ADC1_BASE, 0); + ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_TIMER, 0); + ADCSequenceEnable(ADC1_BASE, 0); +} \ No newline at end of file diff --git a/sampling.h b/sampling.h index 06ea538..9cb9b8d 100644 --- a/sampling.h +++ b/sampling.h @@ -15,4 +15,6 @@ volatile uint32_t gADCErrors; // number of missed ADC deadlines // initialize ADC and ISR void start_sampler(void); +void set_frequency(uint64_t microseconds); + #endif /* SAMPLING_H_ */