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