mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 03:44:30 +03:00
restructuring
This commit is contained in:
133
deprecated/F0/blink/Makefile
Normal file
133
deprecated/F0/blink/Makefile
Normal file
@@ -0,0 +1,133 @@
|
||||
BINARY = blink
|
||||
BOOTPORT ?= /dev/ttyUSB0
|
||||
BOOTSPEED ?= 115200
|
||||
# change this linking script depending on particular MCU model,
|
||||
# for example, if you have STM32F103VBT6, you should write:
|
||||
LDSCRIPT = ld/stm32f042k.ld
|
||||
LIBNAME = opencm3_stm32f0
|
||||
DEFS = -DSTM32F0 -DSTM32F042x6 -DEBUG
|
||||
|
||||
OBJDIR = mk
|
||||
INDEPENDENT_HEADERS=
|
||||
|
||||
FP_FLAGS ?= -msoft-float
|
||||
ARCH_FLAGS = -mthumb -mcpu=cortex-m0 $(FP_FLAGS)
|
||||
|
||||
###############################################################################
|
||||
# Executables
|
||||
PREFIX ?= arm-none-eabi
|
||||
|
||||
RM := rm -f
|
||||
RMDIR := rmdir
|
||||
CC := $(PREFIX)-gcc
|
||||
LD := $(PREFIX)-gcc
|
||||
AR := $(PREFIX)-ar
|
||||
AS := $(PREFIX)-as
|
||||
OBJCOPY := $(PREFIX)-objcopy
|
||||
OBJDUMP := $(PREFIX)-objdump
|
||||
GDB := $(PREFIX)-gdb
|
||||
STFLASH = $(shell which st-flash)
|
||||
STBOOT = $(shell which stm32flash)
|
||||
|
||||
###############################################################################
|
||||
# Source files
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
SRC = $(wildcard *.c)
|
||||
OBJS = $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o))
|
||||
|
||||
ifeq ($(strip $(OPENCM3_DIR)),)
|
||||
OPENCM3_DIR := /usr/local/arm-none-eabi
|
||||
$(info Using $(OPENCM3_DIR) path to library)
|
||||
endif
|
||||
|
||||
INCLUDE_DIR = $(OPENCM3_DIR)/include
|
||||
LIB_DIR = $(OPENCM3_DIR)/lib
|
||||
SCRIPT_DIR = $(OPENCM3_DIR)/scripts
|
||||
|
||||
###############################################################################
|
||||
# C flags
|
||||
CFLAGS += -O2 -g
|
||||
CFLAGS += -Wall -Wextra -Wshadow -Wimplicit-function-declaration
|
||||
CFLAGS += -Wredundant-decls
|
||||
# -Wmissing-prototypes -Wstrict-prototypes
|
||||
CFLAGS += -fno-common -ffunction-sections -fdata-sections
|
||||
|
||||
###############################################################################
|
||||
# C & C++ preprocessor common flags
|
||||
CPPFLAGS += -MD
|
||||
CPPFLAGS += -Wall -Werror
|
||||
CPPFLAGS += -I$(INCLUDE_DIR) $(DEFS)
|
||||
|
||||
###############################################################################
|
||||
# Linker flags
|
||||
LDFLAGS += --static -nostartfiles
|
||||
LDFLAGS += -L$(LIB_DIR)
|
||||
LDFLAGS += -T$(LDSCRIPT)
|
||||
LDFLAGS += -Wl,-Map=$(*).map
|
||||
LDFLAGS += -Wl,--gc-sections
|
||||
|
||||
###############################################################################
|
||||
# Used libraries
|
||||
LDLIBS += -l$(LIBNAME)
|
||||
LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group
|
||||
|
||||
.SUFFIXES: .elf .bin .hex .srec .list .map .images
|
||||
.SECONDEXPANSION:
|
||||
.SECONDARY:
|
||||
|
||||
ELF := $(OBJDIR)/$(BINARY).elf
|
||||
LIST := $(OBJDIR)/$(BINARY).list
|
||||
BIN := $(BINARY).bin
|
||||
HEX := $(BINARY).hex
|
||||
|
||||
all: list bin
|
||||
|
||||
elf: $(ELF)
|
||||
bin: $(BIN)
|
||||
hex: $(HEX)
|
||||
list: $(LIST)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
@printf " CC $<\n"
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $@ -c $<
|
||||
|
||||
$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS)
|
||||
@touch $@
|
||||
|
||||
%.h: ;
|
||||
|
||||
$(BIN): $(ELF)
|
||||
@printf " OBJCOPY $(BIN)\n"
|
||||
$(OBJCOPY) -Obinary $(ELF) $(BIN)
|
||||
|
||||
$(HEX): $(ELF)
|
||||
@printf " OBJCOPY $(HEX)\n"
|
||||
$(OBJCOPY) -Oihex $(ELF) $(HEX)
|
||||
|
||||
$(LIST): $(ELF)
|
||||
@printf " OBJDUMP $(LIST)\n"
|
||||
$(OBJDUMP) -S $(ELF) > $(LIST)
|
||||
|
||||
$(ELF): $(OBJDIR) $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a
|
||||
@printf " LD $(ELF)\n"
|
||||
$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
|
||||
|
||||
clean:
|
||||
@printf " CLEAN\n"
|
||||
$(RM) $(OBJS) $(OBJDIR)/*.d $(ELF) $(HEX) $(LIST) $(OBJDIR)/*.map
|
||||
$(RMDIR) $(OBJDIR)
|
||||
|
||||
flash: $(BIN)
|
||||
@printf " FLASH $(BIN)\n"
|
||||
$(STFLASH) write $(BIN) 0x8000000
|
||||
|
||||
boot: $(BIN)
|
||||
@printf " LOAD $(BIN) through bootloader\n"
|
||||
$(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN)
|
||||
|
||||
.PHONY: clean elf hex list flash boot
|
||||
|
||||
#-include $(OBJS:.o=.d)
|
||||
BIN
deprecated/F0/blink/blink.bin
Normal file
BIN
deprecated/F0/blink/blink.bin
Normal file
Binary file not shown.
31
deprecated/F0/blink/ld/stm32f042k.ld
Normal file
31
deprecated/F0/blink/ld/stm32f042k.ld
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2012 Karl Palsson <karlp@tweak.net.au>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* Linker script for STM32F100x4, 16K flash, 4K RAM. */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K
|
||||
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_stm32f0.ld
|
||||
|
||||
63
deprecated/F0/blink/stm32f0.h
Normal file
63
deprecated/F0/blink/stm32f0.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* stm32f0.h
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
#include <libopencm3/stm32/flash.h>
|
||||
|
||||
#define RCC_CFGR_PLLXTPRE_HSE_CLK 0x0
|
||||
#define RCC_CFGR_PLLXTPRE_HSE_CLK_DIV2 0x1
|
||||
#define RCC_CFGR_PLLSRC_HSI_CLK_DIV2 0x0
|
||||
#define RCC_CFGR_PLLSRC_HSE_CLK 0x1
|
||||
|
||||
|
||||
inline void rcc_clock_setup_in_hse_8mhz_out_48mhz(void){
|
||||
RCC_CR |= RCC_CR_HSEON;
|
||||
while ((RCC_CIR & RCC_CIR_HSERDYF) != 0);
|
||||
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_SW) | RCC_CFGR_SW_HSE;
|
||||
|
||||
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_HPRE) | RCC_CFGR_HPRE_NODIV;
|
||||
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_PPRE) | RCC_CFGR_PPRE_NODIV;
|
||||
|
||||
FLASH_ACR = (FLASH_ACR & ~FLASH_ACR_LATENCY) | FLASH_ACR_LATENCY_024_048MHZ;
|
||||
|
||||
/* PLL: 8MHz * 6 = 48MHz */
|
||||
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_PLLMUL) | RCC_CFGR_PLLMUL_MUL6;
|
||||
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_PLLSRC) | (RCC_CFGR_PLLSRC_HSE_CLK << 16);
|
||||
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_PLLXTPRE) | (RCC_CFGR_PLLXTPRE_HSE_CLK << 17);
|
||||
|
||||
RCC_CR |= RCC_CR_PLLON;
|
||||
while ((RCC_CIR & RCC_CIR_PLLRDYF) != 0);
|
||||
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_SW) | RCC_CFGR_SW_PLL;
|
||||
}
|
||||
|
||||
inline void pin_toggle(uint32_t gpioport, uint16_t gpios){
|
||||
uint32_t port = GPIO_ODR(gpioport);
|
||||
GPIO_BSRR(gpioport) = ((port & gpios) << 16) | (~port & gpios);
|
||||
}
|
||||
|
||||
inline void pin_set(uint32_t gpioport, uint16_t gpios){GPIO_BSRR(gpioport) = gpios;}
|
||||
inline void pin_clear(uint32_t gpioport, uint16_t gpios){GPIO_BSRR(gpioport) = (gpios << 16);}
|
||||
inline int pin_read(uint32_t gpioport, uint16_t gpios){return GPIO_IDR(gpioport) & gpios ? 1 : 0;}
|
||||
inline void pin_write(uint32_t gpioport, uint16_t gpios){GPIO_ODR(gpioport) = gpios;}
|
||||
|
||||
//#define do{}while(0)
|
||||
|
||||
|
||||
|
||||
82
deprecated/F0/blink/systick_blink.c
Normal file
82
deprecated/F0/blink/systick_blink.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2013 Chuck McManis <cmcmanis@mcmanis.com>
|
||||
* Copyright (C) 2013 Onno Kortmann <onno@gmx.net>
|
||||
* Copyright (C) 2013 Frantisek Burian <BuFran@seznam.cz> (merge)
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/cm3/systick.h>
|
||||
|
||||
#include "stm32f0.h"
|
||||
|
||||
/* Called when systick fires */
|
||||
void sys_tick_handler(void){
|
||||
pin_toggle(GPIOB, GPIO3);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set up timer to fire every x milliseconds
|
||||
* This is a unusual usage of systick, be very careful with the 24bit range
|
||||
* of the systick counter! You can range from 1 to 2796ms with this.
|
||||
*/
|
||||
static void systick_setup(int xms){
|
||||
static int curms = 0;
|
||||
if(curms == xms) return;
|
||||
// 6MHz
|
||||
systick_set_clocksource(STK_CSR_CLKSOURCE_EXT);
|
||||
/* clear counter so it starts right away */
|
||||
STK_CVR = 0;
|
||||
|
||||
systick_set_reload(6000 * xms);
|
||||
systick_counter_enable();
|
||||
systick_interrupt_enable();
|
||||
curms = xms;
|
||||
}
|
||||
|
||||
/* set STM32 to clock by 48MHz from HSI oscillator */
|
||||
static void clock_setup(void){
|
||||
rcc_clock_setup_in_hsi_out_48mhz();
|
||||
//rcc_clock_setup_in_hse_8mhz_out_48mhz();
|
||||
|
||||
/* Enable clocks to the GPIO subsystems (A&B) */
|
||||
RCC_AHBENR = RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
|
||||
}
|
||||
|
||||
static void gpio_setup(void){
|
||||
/* Set green led (PB3) as output */
|
||||
gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3);
|
||||
/* D2 - PA12 - switch blink rate (pullup input) */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO12);
|
||||
}
|
||||
|
||||
|
||||
int main(void){
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
|
||||
/* 500ms ticks => 1000ms period => 1Hz blinks */
|
||||
systick_setup(500);
|
||||
|
||||
/* Do nothing in main loop */
|
||||
while (1){
|
||||
if(pin_read(GPIOA, GPIO12)) systick_setup(125); // no jumper - 4Hz
|
||||
else systick_setup(500); // 1Hz
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user