Skip to content

Commit

Permalink
change adc driver not to use chibios hal
Browse files Browse the repository at this point in the history
  • Loading branch information
edy555 committed Dec 17, 2016
1 parent 8d39e43 commit 8038df8
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
107 changes: 107 additions & 0 deletions adc.c
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion halconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
4 changes: 3 additions & 1 deletion mcuconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
12 changes: 12 additions & 0 deletions nanovna.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#include "ch.h"

/*
* tlv320aic3204.c
*/
Expand Down Expand Up @@ -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
*/
Expand Down
14 changes: 8 additions & 6 deletions plot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 44 additions & 18 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand Down Expand Up @@ -945,58 +952,74 @@ static const ADCConversionGroup adcgrpcfg_y = {
ADC_CHSELR_CHSEL7 /* CHSELR */
};


adcsample_t adc_samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];

void
touch_wait_sense(void)
{
palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLDOWN );
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLDOWN );
palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOB, 0);
palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOA, 6);
}
#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];
//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];
//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)
{
palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLDOWN );
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLDOWN );
palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOB, 0);
palSetPadMode(GPIOA, 6, PAL_MODE_OUTPUT_PUSHPULL );
palSetPad(GPIOA, 6);

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.
*/
Expand All @@ -1010,11 +1033,14 @@ ui_init()
#endif

touch_wait_sense();

#if 0
/*
* Activates the ADC1 driver
*/
adcStart(&ADCD1, NULL);
adcSTM32SetCCR(ADC_CCR_VREFEN);

adcStartConversion(&ADCD1, &adcgrpcfg1, adc_samples, 1);
#endif
}

0 comments on commit 8038df8

Please sign in to comment.