From 8038df8c663ec7b6e6309c254e00d974662dfb4f Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 17 Dec 2016 13:16:56 +0900 Subject: [PATCH] change adc driver not to use chibios hal --- Makefile | 2 +- adc.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ halconf.h | 2 +- mcuconf.h | 4 +- nanovna.h | 12 ++++++ plot.c | 14 ++++--- ui.c | 84 +++++++++++++++++++++++++++--------------- 7 files changed, 187 insertions(+), 38 deletions(-) create mode 100644 adc.c diff --git a/Makefile b/Makefile index 2320125..208d1df 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ CSRC = $(STARTUPSRC) \ $(STREAMSSRC) \ $(SHELLSRC) \ usbcfg.c \ - main.c si5351.c si5351_low.c tlv320aic3204.c dsp.c plot.c ui.c ili9341.c numfont20x24.c Font5x7.c flash.c + main.c si5351.c si5351_low.c tlv320aic3204.c dsp.c plot.c ui.c ili9341.c numfont20x24.c Font5x7.c flash.c adc.c # $(TESTSRC) \ diff --git a/adc.c b/adc.c new file mode 100644 index 0000000..8c324e1 --- /dev/null +++ b/adc.c @@ -0,0 +1,107 @@ +#include "ch.h" +#include "hal.h" +#include "nanovna.h" + + +#define ADC_TR(low, high) (((uint32_t)(high) << 16U) | \ + (uint32_t)(low)) +#define ADC_SMPR_SMP_1P5 0U /**< @brief 14 cycles conversion time */ +#define ADC_CFGR1_RES_12BIT (0U << 3U) + +void adc_init(void) +{ + rccEnableADC1(FALSE); + + /* Calibration procedure.*/ + ADC->CCR = 0; + ADC1->CR |= ADC_CR_ADCAL; + while (ADC1->CR & ADC_CR_ADCAL) + ; + + ADC1->CR = ADC_CR_ADEN; + while (!(ADC1->ISR & ADC_ISR_ADRDY)) + ; +} + +uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel) +{ + /* ADC setup */ + adc->ISR = adc->ISR; + adc->IER = 0; + adc->TR = ADC_TR(0, 0); + adc->SMPR = ADC_SMPR_SMP_1P5; + adc->CFGR1 = ADC_CFGR1_RES_12BIT; + adc->CHSELR = chsel; + + /* ADC conversion start.*/ + adc->CR |= ADC_CR_ADSTART; + + while (adc->CR & ADC_CR_ADSTART) + ; + + return adc->DR; +} + +void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel) +{ + uint32_t cfgr1; + + cfgr1 = ADC_CFGR1_RES_12BIT | ADC_CFGR1_AWDEN + | ADC_CFGR1_EXTEN_0 // rising edge of external trigger + | ADC_CFGR1_EXTSEL_0 | ADC_CFGR1_EXTSEL_1; // TRG3 , /* CFGR1 */ + + /* ADC setup, if it is defined a callback for the analog watch dog then it + is enabled.*/ + adc->ISR = adc->ISR; + adc->IER = ADC_IER_AWDIE; + adc->TR = ADC_TR(0, 2000); + adc->SMPR = ADC_SMPR_SMP_1P5; + adc->CHSELR = chsel; + + /* ADC configuration and start.*/ + adc->CFGR1 = cfgr1; + + /* ADC conversion start.*/ + adc->CR |= ADC_CR_ADSTART; +} + +void adc_stop(ADC_TypeDef *adc) +{ + if (adc->CR & ADC_CR_ADEN) { + if (adc->CR & ADC_CR_ADSTART) { + adc->CR |= ADC_CR_ADSTP; + while (adc->CR & ADC_CR_ADSTP) + ; + } + + /* adc->CR |= ADC_CR_ADDIS; + while (adc->CR & ADC_CR_ADDIS) + ;*/ + } +} + +void adc_interrupt(ADC_TypeDef *adc) +{ + uint32_t isr = adc->ISR; + adc->ISR = isr; + + if (isr & ADC_ISR_OVR) { + /* ADC overflow condition, this could happen only if the DMA is unable + to read data fast enough.*/ + + } + if (isr & ADC_ISR_AWD) { + /* Analog watchdog error.*/ + extern int awd_count; + awd_count++; + } +} + +OSAL_IRQ_HANDLER(STM32_ADC1_HANDLER) +{ + OSAL_IRQ_PROLOGUE(); + + adc_interrupt(ADC1); + + OSAL_IRQ_EPILOGUE(); +} diff --git a/halconf.h b/halconf.h index 9a8634a..727be71 100644 --- a/halconf.h +++ b/halconf.h @@ -41,7 +41,7 @@ * @brief Enables the ADC subsystem. */ #if !defined(HAL_USE_ADC) || defined(__DOXYGEN__) -#define HAL_USE_ADC TRUE +#define HAL_USE_ADC FALSE #endif /** diff --git a/mcuconf.h b/mcuconf.h index 26f99bd..773537f 100644 --- a/mcuconf.h +++ b/mcuconf.h @@ -65,7 +65,7 @@ /* * ADC driver system settings. */ -#define STM32_ADC_USE_ADC1 TRUE +#define STM32_ADC_USE_ADC1 FALSE #define STM32_ADC_ADC1_CKMODE STM32_ADC_CKMODE_ADCCLK #define STM32_ADC_ADC1_DMA_PRIORITY 2 #define STM32_ADC_IRQ_PRIORITY 2 @@ -95,6 +95,8 @@ #define STM32_EXT_EXTI17_IRQ_PRIORITY 3 #define STM32_EXT_EXTI21_22_IRQ_PRIORITY 3 +#define STM32_DISABLE_EXTI2122_HANDLER TRUE + /* * GPT driver system settings. */ diff --git a/nanovna.h b/nanovna.h index cae92eb..a23abd0 100644 --- a/nanovna.h +++ b/nanovna.h @@ -1,4 +1,6 @@ +#include "ch.h" + /* * tlv320aic3204.c */ @@ -256,6 +258,16 @@ void ui_hide(void); extern uint8_t operation_requested; +/* + * adc.c + */ + +void adc_init(void); +uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel); +void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel); +void adc_stop(ADC_TypeDef *adc); +void adc_interrupt(ADC_TypeDef *adc); + /* * misclinous */ diff --git a/plot.c b/plot.c index 01b0c11..0c9e7dc 100644 --- a/plot.c +++ b/plot.c @@ -1237,15 +1237,17 @@ draw_frequencies(void) { char buf[24]; if (frequency1 > 0) { + int start = frequency0; + int stop = frequency1; chsnprintf(buf, 24, "START %d.%03d %03d MHz ", - (int)(frequency0 / 1000000), - (int)((frequency0 / 1000) % 1000), - (int)(frequency0 % 1000)); + (int)(start / 1000000), + (int)((start / 1000) % 1000), + (int)(start % 1000)); ili9341_drawstring_5x7(buf, OFFSETX, 233, 0xffff, 0x0000); chsnprintf(buf, 24, "STOP %d.%03d %03d MHz", - (int)(frequency1 / 1000000), - (int)((frequency1 / 1000) % 1000), - (int)(frequency1 % 1000)); + (int)(stop / 1000000), + (int)((stop / 1000) % 1000), + (int)(stop % 1000)); ili9341_drawstring_5x7(buf, 205, 233, 0xffff, 0x0000); } else if (frequency1 < 0) { int fcenter = frequency0; diff --git a/ui.c b/ui.c index 65ff357..4828b08 100644 --- a/ui.c +++ b/ui.c @@ -878,18 +878,21 @@ static const EXTConfig extcfg = { } }; +#if 0 static void adccallback(ADCDriver *adcp, adcsample_t *buffer, size_t n) { (void)adcp; (void)buffer; (void)n; } +#endif void test_touch(int *x, int *y); int awd_count; int touch_x, touch_y; +#if 0 static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) { (void)adcp; @@ -899,14 +902,18 @@ static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) //test_touch(&touch_x, &touch_y); } } +#endif static const GPTConfig gpt3cfg = { 1000, /* 1kHz timer clock.*/ NULL, /* Timer callback.*/ - 0x0020, + 0x0020, /* CR2:MMS=02 to output TRGO */ 0 }; + +#if 0 + #define ADC_GRP1_NUM_CHANNELS 1 #define ADC_GRP1_BUF_DEPTH 1 @@ -945,8 +952,43 @@ static const ADCConversionGroup adcgrpcfg_y = { ADC_CHSELR_CHSEL7 /* CHSELR */ }; + adcsample_t adc_samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH]; +#endif + +int +touch_measure_y(void) +{ +#if 1 + palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLDOWN ); + palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLDOWN ); + palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL ); + palClearPad(GPIOB, 0); + palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL ); + palSetPad(GPIOA, 6); + //adcConvert(&ADCD1, &adcgrpcfg_y, adc_samples, 1); + //return adc_samples[0]; +#endif + return adc_single_read(ADC1, ADC_CHSELR_CHSEL7); +} + +int +touch_measure_x(void) +{ +#if 1 + palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_PULLDOWN ); + palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN ); + palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL ); + palSetPad(GPIOB, 1); + palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL ); + palClearPad(GPIOA, 7); + //adcConvert(&ADCD1, &adcgrpcfg_x, adc_samples, 1); + //return adc_samples[0]; +#endif + return adc_single_read(ADC1, ADC_CHSELR_CHSEL6); +} + void touch_wait_sense(void) { @@ -956,47 +998,28 @@ touch_wait_sense(void) palSetPad(GPIOB, 0); palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL ); palSetPad(GPIOA, 6); -} -int -touch_measure_y(void) -{ - palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLDOWN ); - palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLDOWN ); - palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL ); - palClearPad(GPIOB, 0); - palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL ); - palSetPad(GPIOA, 6); - adcConvert(&ADCD1, &adcgrpcfg_y, adc_samples, 1); - return adc_samples[0]; -} - -int -touch_measure_x(void) -{ - palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_PULLDOWN ); - palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN ); - palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL ); - palSetPad(GPIOB, 1); - palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL ); - palClearPad(GPIOA, 7); - adcConvert(&ADCD1, &adcgrpcfg_x, adc_samples, 1); - return adc_samples[0]; + adc_start_analog_watchdogd(ADC1, ADC_CHSELR_CHSEL7); } void test_touch(int *x, int *y) { - adcStopConversion(&ADCD1); - *x = touch_measure_x(); + //adcStopConversion(&ADCD1); + adc_stop(ADC1); + *y = touch_measure_y(); + *x = touch_measure_x(); + touch_wait_sense(); - adcStartConversion(&ADCD1, &adcgrpcfg1, adc_samples, 1); + //adcStartConversion(&ADCD1, &adcgrpcfg1, adc_samples, 1); } void ui_init() { + adc_init(); + /* * Activates the EXT driver 1. */ @@ -1010,6 +1033,8 @@ ui_init() #endif touch_wait_sense(); + +#if 0 /* * Activates the ADC1 driver */ @@ -1017,4 +1042,5 @@ ui_init() adcSTM32SetCCR(ADC_CCR_VREFEN); adcStartConversion(&ADCD1, &adcgrpcfg1, adc_samples, 1); +#endif }