From 323a9c3a55a835c11fc7ae24e6f6d3e2b3d9bd19 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Sat, 1 May 2021 20:06:51 +0300 Subject: [PATCH] init --- .gitignore | 4 ++ .gitmodules | 3 + .idea/.gitignore | 8 +++ .idea/misc.xml | 18 +++++ .idea/vcs.xml | 7 ++ Makefile | 22 ++++++ README.md | 7 ++ libopencm3 | 1 + main.c | 156 +++++++++++++++++++++++++++++++++++++++++ openocd.cfg | 24 +++++++ rules.mk | 177 +++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 427 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 Makefile create mode 100644 README.md create mode 160000 libopencm3 create mode 100644 main.c create mode 100644 openocd.cfg create mode 100644 rules.mk diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01acaa5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/ +*.elf +*.bin +generated.*.ld diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c4a0df1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libopencm3"] + path = libopencm3 + url = https://github.com/libopencm3/libopencm3.git diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..53624c9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..f9404a3 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f81b150 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +V=1 +DEBUG=1 +PROJECT = dht_test +BUILD_DIR = bin + +#SHARED_DIR = ../my-common-code +CFILES = main.c +#CFILES += api.c +#AFILES += api-asm.S + +# TODO - you will need to edit these two lines! +DEVICE=stm32f103rcbt6 +OOCD_FILE = board/stm32f4discovery.cfg + +# You shouldn't have to edit anything below here. +#VPATH += $(SHARED_DIR) +#INCLUDES += $(patsubst %,-I%, . $(SHARED_DIR)) +OPENCM3_DIR=libopencm3 + +include $(OPENCM3_DIR)/mk/genlink-config.mk +include rules.mk +include $(OPENCM3_DIR)/mk/genlink-rules.mk diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca0fd9f --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +## Build + +0. git submodule update --init (This is only needed once) +1. make -C libopencm3 # (Only needed once) +2. make + + diff --git a/libopencm3 b/libopencm3 new file mode 160000 index 0000000..4492841 --- /dev/null +++ b/libopencm3 @@ -0,0 +1 @@ +Subproject commit 44928416eacb4c8d7774b1385c7f38fb226d080b diff --git a/main.c b/main.c new file mode 100644 index 0000000..76c001a --- /dev/null +++ b/main.c @@ -0,0 +1,156 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +static void delay(int count) +{ + for (int i=0; i < count; i++) { + __asm__("nop"); + } +} + +static void gpio_setup(void) +{ + /* Enable GPIOA clock. */ + /* Manually: */ + // RCC_APB2ENR |= RCC_APB2ENR_IOPCEN; + /* Using API functions: */ + rcc_periph_clock_enable(RCC_GPIOB); + + /* Set GPIO5 (in GPIO port A) to 'output push-pull'. */ + /* Manually: */ + // GPIOA_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((5 - 8) * 4) + 2)); + // GPIOA_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((5 - 8) * 4)); + /* Using API functions: */ + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); +} + +static void tim_setup(void) +{ + nvic_enable_irq(NVIC_TIM3_IRQ); + nvic_set_priority(NVIC_TIM3_IRQ, 1); // interupts will not work without it??? + + rcc_periph_clock_enable(RCC_TIM3); + rcc_periph_reset_pulse(RST_TIM3); + + timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + timer_set_prescaler(TIM3, 1152); // set timer tickrate to 500khz + //timer_set_oc_value(TIM3, TIM_OC1, 0x1000); + timer_set_period(TIM3, 0xffff); + timer_one_shot_mode(TIM3); + + timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_IN_TI3); + //timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING); // TIM_IC_RISING + //timer_ic_enable(TIM3, TIM_IC3); + + timer_enable_counter(TIM3); + timer_enable_irq(TIM3, TIM_DIER_UIE); + timer_enable_irq(TIM3, TIM_DIER_CC1IE); // end start condition + timer_enable_irq(TIM3, TIM_DIER_CC3IE); + + // start condition + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0); + gpio_clear(GPIOB, GPIO0); +} + +enum DHT11_STATE { + DHT11_START, + DHT11_RESPONSE, + DHT11_BIT0, + DHT11_BIT1, + DHT11_END +}; + +int dht11_state = DHT11_START; + +void tim3_isr(void) +{ + + if (timer_get_flag(TIM3, TIM_SR_UIF)) // timer reached end + { + timer_clear_flag(TIM3, TIM_SR_UIF); + //gpio_toggle(GPIOB, GPIO1); + gpio_toggle(GPIOB, GPIO2); + + //timer_set_counter(TIM3, 0); + //timer_clear_flag(TIM3, TIM_SR_UIF); + //timer_set_oc_value(TIM3, TIM_OC1, 1000); + } else if (timer_get_flag(TIM3, TIM_SR_CC1IF)) + { + timer_clear_flag(TIM3, TIM_SR_CC3IF); + if (dht11_state == DHT11_START){ + gpio_set(GPIOB, GPIO0); + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0); + + // setup timer to wait for start response from dht11 + timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING); // TIM_IC_RISING + timer_ic_enable(TIM3, TIM_IC3); + + dht11_state = DHT11_START; + } + + } else if (timer_get_flag(TIM3, TIM_SR_CC3IF)) // PB0 changed value + { + timer_clear_flag(TIM3, TIM_SR_CC3IF); + + + + if (timer_get_flag(TIM3, TIM_SR_CC3OF)) + { + timer_clear_flag(TIM3, TIM_SR_CC3OF); + } + } + +} + +static void dht_start_signal(void) +{ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0); + gpio_clear(GPIOB, GPIO0); + delay(205000); + //delay(30000); + gpio_set(GPIOB, GPIO0); + delay(100); + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0); +} + +volatile int bla=0; + +int main(void) +{ + rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]); + gpio_setup(); + tim_setup(); + + /* Blink the LED (PA5) on the board. */ + while (1) { + /* Using API function gpio_toggle(): */ + gpio_toggle(GPIOB, GPIO1); /* LED on/off */ + delay(4000000); + dht_start_signal(); + } + + return 0; +} diff --git a/openocd.cfg b/openocd.cfg new file mode 100644 index 0000000..f45185d --- /dev/null +++ b/openocd.cfg @@ -0,0 +1,24 @@ +# This is an ST NUCLEO F103RB board with a single STM32F103RBT6 chip. +# http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1847/PF259875 + +interface ftdi +transport select swd +ftdi_vid_pid 0x0403 0x6010 + +# ftdi_device_desc "USB Serial Converter A" +# ftdi_device_desc "FT2232H ¿ª·¢°å" +# ftdi_serial "FTZ7O8O0" + +ftdi_channel 1 +#adapter_khz 500 +#adapter_khz 8 + +ftdi_layout_init 0x0018 0x05fb +ftdi_layout_signal SWD_EN -data 0 +#ftdi_layout_signal nSRST -data 0x0020 +ftdi_layout_signal nTRST -ndata 0x0010 -noe 0x0040 +ftdi_layout_signal nSRST -ndata 0x0020 -noe 0x0040 + +source [find target/stm32f1x.cfg] + +reset_config srst_only diff --git a/rules.mk b/rules.mk new file mode 100644 index 0000000..e417d2f --- /dev/null +++ b/rules.mk @@ -0,0 +1,177 @@ +# This version of rules.mk expects the following to be defined before +# inclusion.. +### REQUIRED ### +# OPENCM3_DIR - duh +# PROJECT - will be the basename of the output elf, eg usb-gadget0-stm32f4disco +# CFILES - basenames only, eg main.c blah.c +# CXXFILES - same for C++ files. Must have cxx suffix! +# DEVICE - the full device name, eg stm32f405ret6 +# _or_ +# LDSCRIPT - full path, eg ../../examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld +# OPENCM3_LIB - the basename, eg: opencm3_stm32f4 +# OPENCM3_DEFS - the target define eg: -DSTM32F4 +# ARCH_FLAGS - eg, -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 +# (ie, the full set of cpu arch flags, _none_ are defined in this file) +# +### OPTIONAL ### +# INCLUDES - fully formed -I paths, if you want extra, eg -I../shared +# BUILD_DIR - defaults to bin, should set this if you are building multiarch +# OPT - full -O flag, defaults to -Os +# CSTD - defaults -std=c99 +# CXXSTD - no default. +# OOCD_INTERFACE - eg stlink-v2 +# OOCD_TARGET - eg stm32f4x +# both only used if you use the "make flash" target. +# OOCD_FILE - eg my.openocd.cfg +# This overrides interface/target above, and is used as just -f FILE +### TODO/FIXME/notes ### +# No support for stylecheck. +# No support for BMP/texane/random flash methods, no plans either +# No support for magically finding the library. +# C++ hasn't been actually tested with this..... sorry bout that. ;) +# Second expansion/secondary not set, add this if you need them. + +BUILD_DIR ?= bin +OPT ?= -Os +CSTD ?= -std=c99 + +# Be silent per default, but 'make V=1' will show all compiler calls. +# If you're insane, V=99 will print out all sorts of things. +V?=0 +ifeq ($(V),0) +Q := @ +NULL := 2>/dev/null +endif + +# Tool paths. +PREFIX ?= arm-none-eabi- +CC = $(PREFIX)gcc +CXX = $(PREFIX)g++ +LD = $(PREFIX)gcc +OBJCOPY = $(PREFIX)objcopy +OBJDUMP = $(PREFIX)objdump +OOCD ?= openocd + +OPENCM3_INC = $(OPENCM3_DIR)/include + +# Inclusion of library header files +INCLUDES += $(patsubst %,-I%, . $(OPENCM3_INC) ) + +OBJS = $(CFILES:%.c=$(BUILD_DIR)/%.o) +OBJS += $(CXXFILES:%.cxx=$(BUILD_DIR)/%.o) +OBJS += $(AFILES:%.S=$(BUILD_DIR)/%.o) +GENERATED_BINS = $(PROJECT).elf $(PROJECT).bin $(PROJECT).map $(PROJECT).list $(PROJECT).lss + +TGT_CPPFLAGS += -MD +TGT_CPPFLAGS += -Wall -Wundef $(INCLUDES) +TGT_CPPFLAGS += $(INCLUDES) $(OPENCM3_DEFS) + +TGT_CFLAGS += $(OPT) $(CSTD) -ggdb3 +TGT_CFLAGS += $(ARCH_FLAGS) +TGT_CFLAGS += -fno-common +TGT_CFLAGS += -ffunction-sections -fdata-sections +TGT_CFLAGS += -Wextra -Wshadow -Wno-unused-variable -Wimplicit-function-declaration +TGT_CFLAGS += -Wredundant-decls -Wstrict-prototypes -Wmissing-prototypes + +TGT_CXXFLAGS += $(OPT) $(CXXSTD) -ggdb3 +TGT_CXXFLAGS += $(ARCH_FLAGS) +TGT_CXXFLAGS += -fno-common +TGT_CXXFLAGS += -ffunction-sections -fdata-sections +TGT_CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++ + +TGT_ASFLAGS += $(OPT) $(ARCH_FLAGS) -ggdb3 + +TGT_LDFLAGS += -T$(LDSCRIPT) -L$(OPENCM3_DIR)/lib -nostartfiles +TGT_LDFLAGS += $(ARCH_FLAGS) +TGT_LDFLAGS += -specs=nano.specs +TGT_LDFLAGS += -Wl,--gc-sections +# OPTIONAL +#TGT_LDFLAGS += -Wl,-Map=$(PROJECT).map +ifeq ($(V),99) +TGT_LDFLAGS += -Wl,--print-gc-sections +endif + +# Linker script generator fills this in for us. +ifeq (,$(DEVICE)) +LDLIBS += -l$(OPENCM3_LIB) +endif +# nosys is only in newer gcc-arm-embedded... +#LDLIBS += -specs=nosys.specs +LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group + +# Burn in legacy hell fortran modula pascal yacc idontevenwat +.SUFFIXES: +.SUFFIXES: .c .S .h .o .cxx .elf .bin .list .lss + +# Bad make, never *ever* try to get a file out of source control by yourself. +%: %,v +%: RCS/%,v +%: RCS/% +%: s.% +%: SCCS/s.% + +all: $(PROJECT).elf $(PROJECT).bin +flash: $(PROJECT).flash + +# error if not using linker script generator +ifeq (,$(DEVICE)) +$(LDSCRIPT): +ifeq (,$(wildcard $(LDSCRIPT))) + $(error Unable to find specified linker script: $(LDSCRIPT)) +endif +else +# if linker script generator was used, make sure it's cleaned. +GENERATED_BINS += $(LDSCRIPT) +endif + +# Need a special rule to have a bin dir +$(BUILD_DIR)/%.o: %.c + @printf " CC\t$<\n" + @mkdir -p $(dir $@) + $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $< + +$(BUILD_DIR)/%.o: %.cxx + @printf " CXX\t$<\n" + @mkdir -p $(dir $@) + $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $< + +$(BUILD_DIR)/%.o: %.S + @printf " AS\t$<\n" + @mkdir -p $(dir $@) + $(Q)$(CC) $(TGT_ASFLAGS) $(ASFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $< + +$(PROJECT).elf: $(OBJS) $(LDSCRIPT) $(LIBDEPS) + @printf " LD\t$@\n" + $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@ + +%.bin: %.elf + @printf " OBJCOPY\t$@\n" + $(Q)$(OBJCOPY) -O binary $< $@ + +%.lss: %.elf + $(OBJDUMP) -h -S $< > $@ + +%.list: %.elf + $(OBJDUMP) -S $< > $@ + +%.flash: %.elf + @printf " FLASH\t$<\n" +ifeq (,$(OOCD_FILE)) + $(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \ + $(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f target/$(OOCD_TARGET).cfg \ + -c "program $(realpath $(*).elf) verify reset exit" \ + $(NULL) +else + $(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \ + $(OOCD) -f $(OOCD_FILE) \ + -c "program $(realpath $(*).elf) verify reset exit" \ + $(NULL) +endif + +clean: + rm -rf $(BUILD_DIR) $(GENERATED_BINS) + +.PHONY: all clean flash +-include $(OBJS:.o=.d) +