Skip to content

Commit 96271d7

Browse files
committed
Merge branch 'adc_support'
2 parents ad77f3b + 4ddba24 commit 96271d7

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

halconf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* @brief Enables the ADC subsystem.
4242
*/
4343
#if !defined(HAL_USE_ADC) || defined(__DOXYGEN__)
44-
#define HAL_USE_ADC FALSE
44+
#define HAL_USE_ADC TRUE
4545
#endif
4646

4747
/**

main.c

+34
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,27 @@ measure_power_dbm(void)
374374
measured_power_dbm = dbm;
375375
}
376376

377+
uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel)
378+
{
379+
/* ADC setup */
380+
adc->ISR = adc->ISR;
381+
adc->IER = 0;
382+
adc->SMPR1 = ADC_SMPR1_SMP0_2; // 19.5 cycle
383+
adc->CFGR = 0; // 12bit
384+
adc->SQR1 = chsel << 6;
385+
386+
/* ADC conversion start.*/
387+
adc->CR |= ADC_CR_ADSTART;
388+
while (adc->CR & ADC_CR_ADSTART)
389+
;
390+
391+
return adc->DR;
392+
}
393+
394+
#define ADC1_CHANNEL_TEMP 16
395+
#define ADC1_CHANNEL_BAT 17
396+
#define ADC1_CHANNEL_VREF 18
397+
377398
static void cmd_stat(BaseSequentialStream *chp, int argc, char *argv[])
378399
{
379400
(void)argc;
@@ -394,6 +415,10 @@ static void cmd_stat(BaseSequentialStream *chp, int argc, char *argv[])
394415
chprintf(chp, "fm stereo: %d %d\r\n", stereo_separate_state.sdi, stereo_separate_state.sdq);
395416
chprintf(chp, " corr: %d %d %d\r\n", stereo_separate_state.corr, stereo_separate_state.corr_ave, stereo_separate_state.corr_std);
396417
chprintf(chp, " int: %d\r\n", stereo_separate_state.integrator);
418+
419+
chprintf(chp, "temp: %d\r\n", adc_single_read(ADC1, ADC1_CHANNEL_TEMP));
420+
chprintf(chp, "bat: %d\r\n", adc_single_read(ADC1, ADC1_CHANNEL_BAT));
421+
chprintf(chp, "vref: %d\r\n", adc_single_read(ADC1, ADC1_CHANNEL_VREF));
397422

398423
#if 0
399424
p = &tx_buffer[0];
@@ -831,6 +856,15 @@ int __attribute__((noreturn)) main(void)
831856
dac1cfg1.init = config.dac_value;
832857
dacStart(&DACD1, &dac1cfg1);
833858

859+
/*
860+
* Activates the ADC1 driver and the temperature sensor.
861+
*/
862+
adcStart(&ADCD1, NULL);
863+
adcSTM32EnableTS(&ADCD1);
864+
adcSTM32EnableVBAT(&ADCD1);
865+
adcSTM32EnableVREF(&ADCD1);
866+
867+
834868
i2cStart(&I2CD1, &i2ccfg);
835869
/*
836870
* Initializes a serial-over-USB CDC driver.

mcuconf.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#define STM32_PPRE1 STM32_PPRE1_DIV2
5454
#define STM32_PPRE2 STM32_PPRE2_DIV1
5555
#define STM32_MCOSEL STM32_MCOSEL_PLLDIV2
56-
#define STM32_ADC12PRES STM32_ADC12PRES_DIV1
56+
#define STM32_ADC12PRES STM32_ADC12PRES_DIV64
5757
#define STM32_ADC34PRES STM32_ADC34PRES_DIV1
5858
#define STM32_USART1SW STM32_USART1SW_PCLK
5959
#define STM32_USART2SW STM32_USART2SW_PCLK
@@ -71,15 +71,19 @@
7171
/*
7272
* ADC driver system settings.
7373
*/
74-
#define STM32_ADC_USE_ADC1 FALSE
74+
#define STM32_ADC_USE_ADC1 TRUE
7575
#define STM32_ADC_USE_ADC3 FALSE
76+
#define STM32_ADC_ADC1_DMA_STREAM STM32_DMA_STREAM_ID(1, 1)
77+
#define STM32_ADC_ADC2_DMA_STREAM STM32_DMA_STREAM_ID(2, 1)
78+
#define STM32_ADC_ADC3_DMA_STREAM STM32_DMA_STREAM_ID(2, 5)
79+
#define STM32_ADC_ADC4_DMA_STREAM STM32_DMA_STREAM_ID(2, 2)
7680
#define STM32_ADC_ADC12_DMA_PRIORITY 2
7781
#define STM32_ADC_ADC34_DMA_PRIORITY 2
7882
#define STM32_ADC_ADC12_IRQ_PRIORITY 5
7983
#define STM32_ADC_ADC34_IRQ_PRIORITY 5
8084
#define STM32_ADC_ADC12_DMA_IRQ_PRIORITY 5
8185
#define STM32_ADC_ADC34_DMA_IRQ_PRIORITY 5
82-
#define STM32_ADC_ADC12_CLOCK_MODE ADC_CCR_CKMODE_AHB_DIV1
86+
#define STM32_ADC_ADC12_CLOCK_MODE ADC_CCR_CKMODE_AHB_DIV4
8387
#define STM32_ADC_ADC34_CLOCK_MODE ADC_CCR_CKMODE_AHB_DIV1
8488
#define STM32_ADC_DUAL_MODE FALSE
8589

0 commit comments

Comments
 (0)