mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 10:45:11 +03:00
ADC with DMA works!
This commit is contained in:
parent
96c7e0d77c
commit
0cd287d544
@ -4,18 +4,18 @@ BOOTSPEED ?= 115200
|
||||
# MCU FAMILY
|
||||
FAMILY ?= F1
|
||||
# MCU code
|
||||
MCU ?= F103xB
|
||||
MCU ?= F103x8
|
||||
# density (stm32f10x.h, lines 70-84)
|
||||
DENSITY ?= MD
|
||||
# change this linking script depending on particular MCU model,
|
||||
LDSCRIPT ?= stm32f103xB.ld
|
||||
LDSCRIPT ?= stm32f103x8.ld
|
||||
# debug
|
||||
DEFS = -DEBUG
|
||||
|
||||
INDEPENDENT_HEADERS=
|
||||
|
||||
FP_FLAGS ?= -msoft-float
|
||||
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3
|
||||
FP_FLAGS ?= -msoft-float -mfloat-abi=soft
|
||||
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd
|
||||
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS)
|
||||
|
||||
###############################################################################
|
||||
@ -59,17 +59,18 @@ LIB_DIR := $(INC_DIR)/ld
|
||||
# C flags
|
||||
CFLAGS += -O2 -g -D__thumb2__=1 -MD
|
||||
CFLAGS += -Wall -Werror -Wextra -Wshadow
|
||||
CFLAGS += -fno-common -ffunction-sections -fdata-sections
|
||||
CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector
|
||||
CFLAGS += $(ARCH_FLAGS)
|
||||
|
||||
###############################################################################
|
||||
# Linker flags
|
||||
LDFLAGS += -nostartfiles --static
|
||||
LDFLAGS += -nostartfiles --static -nostdlibs
|
||||
LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB)
|
||||
LDFLAGS += -T$(LDSCRIPT)
|
||||
|
||||
###############################################################################
|
||||
# Used libraries
|
||||
LDLIBS += -lc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
|
||||
LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
|
||||
|
||||
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY)
|
||||
|
||||
@ -93,11 +94,11 @@ $(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(STARTUP): $(INC_DIR)/startup/vector.c
|
||||
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $<
|
||||
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
@echo " CC $<"
|
||||
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $<
|
||||
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
|
||||
|
||||
$(BIN): $(ELF)
|
||||
@echo " OBJCOPY $(BIN)"
|
||||
|
||||
@ -56,17 +56,15 @@ int32_t getMCUtemp(){
|
||||
// Temp = (V25 - Vsense)/Avg_Slope + 25
|
||||
// V_25 = 1.45V, Slope = 4.3e-3
|
||||
int32_t Vsense = getVdd() * getADCval(1);
|
||||
Vsense /= 4096;
|
||||
int32_t temperature = 145 - Vsense;
|
||||
temperature *= 233;
|
||||
temperature /= 10; // convert from *100 to *10
|
||||
int32_t temperature = 593920 - Vsense; // 593920 == 145*4096
|
||||
temperature /= 172; // == /(4096*10*4.3e-3), 10 - to convert from *100 to *10
|
||||
temperature += 250;
|
||||
return(temperature);
|
||||
}
|
||||
|
||||
// return Vdd * 100 (V)
|
||||
uint32_t getVdd(){
|
||||
uint32_t vdd = ((uint32_t) *VREFINT_CAL_ADDR) * (uint32_t)300; // 3.0V
|
||||
uint32_t vdd = 120 * 4096; // 1.2V
|
||||
vdd /= getADCval(2);
|
||||
return vdd;
|
||||
}
|
||||
|
||||
@ -41,31 +41,32 @@ static inline void gpio_setup(){
|
||||
|
||||
static inline void adc_setup(){
|
||||
GPIOB->CRL |= CRL(0, CNF_ANALOG|MODE_INPUT);
|
||||
uint16_t ctr = 0; // 0xfff0 - more than 1.3ms
|
||||
uint32_t ctr = 0;
|
||||
// Enable clocking
|
||||
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
|
||||
RCC->CFGR &= ~(RCC_CFGR_ADCPRE);
|
||||
RCC->CFGR |= RCC_CFGR_ADCPRE_1;
|
||||
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV8; // ADC clock = RCC / 8
|
||||
// sampling time - 239.5 cycles for channels 8, 16 and 17
|
||||
ADC1->SMPR2 = ADC_SMPR2_SMP8;
|
||||
ADC1->SMPR1 = ADC_SMPR1_SMP16 | ADC_SMPR1_SMP17;
|
||||
// wake up ADC
|
||||
ADC1->CR2 |= ADC_CR2_ADON;
|
||||
// we have three conversions in group -> ADC1->SQR1[L] = 2, order: 8->16->17
|
||||
ADC1->SQR3 = 8 | (16<<5) | (17<<10);
|
||||
ADC1->SQR1 = ADC_SQR1_L_1;
|
||||
// calibration
|
||||
ADC1->CR2 |= ADC_CR2_CAL;
|
||||
while((ADC1->CR2 & ADC_CR2_CAL) && ++ctr < 0xfff0);
|
||||
ADC1->CR1 |= ADC_CR1_SCAN; // scan mode
|
||||
// DMA configuration
|
||||
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
|
||||
DMA1_Channel1->CPAR = (uint32_t) (&(ADC1->DR));
|
||||
DMA1_Channel1->CMAR = (uint32_t)(ADC_array);
|
||||
DMA1_Channel1->CNDTR = NUMBER_OF_ADC_CHANNELS * 9;
|
||||
DMA1_Channel1->CCR |= DMA_CCR_MINC | DMA_CCR_MSIZE_0 | DMA_CCR_PSIZE_0 | DMA_CCR_CIRC;
|
||||
DMA1_Channel1->CCR |= DMA_CCR_EN;
|
||||
// continuous mode & DMA; enable vref & Tsens; start
|
||||
ADC1->CR2 |= ADC_CR2_CONT | ADC_CR2_DMA | ADC_CR2_TSVREFE | ADC_CR2_SWSTART;
|
||||
DMA1_Channel1->CCR |= DMA_CCR_MINC | DMA_CCR_MSIZE_0 | DMA_CCR_PSIZE_0
|
||||
| DMA_CCR_CIRC | DMA_CCR_PL | DMA_CCR_EN;
|
||||
// continuous mode & DMA; enable vref & Tsens; wake up ADC
|
||||
ADC1->CR2 |= ADC_CR2_DMA | ADC_CR2_TSVREFE | ADC_CR2_CONT | ADC_CR2_ADON;
|
||||
// calibration
|
||||
ADC1->CR2 |= ADC_CR2_RSTCAL;
|
||||
while((ADC1->CR2 & ADC_CR2_RSTCAL) && ++ctr < 0xfffff);
|
||||
ADC1->CR2 |= ADC_CR2_CAL;
|
||||
ctr = 0; while((ADC1->CR2 & ADC_CR2_CAL) && ++ctr < 0xfffff);
|
||||
// turn ON ADC
|
||||
ADC1->CR2 |= ADC_CR2_ADON;
|
||||
}
|
||||
|
||||
@ -69,20 +69,11 @@ char *parse_cmd(char *buf){
|
||||
case '1':
|
||||
pin_clear(GPIOA, 1<<4);
|
||||
break;
|
||||
case 'a':
|
||||
printu(getADCval(0)); newline();
|
||||
printu(getADCval(1)); newline();
|
||||
printu(getADCval(2)); newline();
|
||||
break;
|
||||
case 'b':
|
||||
btns[5] = GET_BTN0() + '0';
|
||||
btns[13] = GET_BTN1() + '0';
|
||||
return btns;
|
||||
break;
|
||||
case 'c':
|
||||
printu((uint32_t) *VREFINT_CAL_ADDR);
|
||||
newline();
|
||||
break;
|
||||
case 'p':
|
||||
pin_toggle(USBPU_port, USBPU_pin);
|
||||
SEND("USB pullup is ");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user