mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-03-21 17:21:04 +03:00
add simple blink for ST32F103-nolib (Makefile still under construction!)
This commit is contained in:
@@ -11,11 +11,13 @@ DEFS = -DEBUG
|
||||
# change this linking script depending on particular MCU model,
|
||||
# for example, if you have STM32F103VBT6, you should write:
|
||||
LDSCRIPT ?= ld/stm32f103xB.ld
|
||||
#LDSCRIPT = STM32F103C8.ld
|
||||
|
||||
INDEPENDENT_HEADERS=
|
||||
|
||||
FP_FLAGS ?= -msoft-float
|
||||
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd
|
||||
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3
|
||||
#-mfix-cortex-m3-ldrd
|
||||
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS)
|
||||
|
||||
###############################################################################
|
||||
@@ -25,9 +27,10 @@ PREFIX ?= arm-none-eabi
|
||||
RM := rm -f
|
||||
RMDIR := rmdir
|
||||
CC := $(PREFIX)-gcc
|
||||
LD := $(PREFIX)-gcc
|
||||
LD := $(PREFIX)-ld
|
||||
AR := $(PREFIX)-ar
|
||||
AS := $(PREFIX)-as
|
||||
SIZE := $(PREFIX)-size
|
||||
OBJCOPY := $(PREFIX)-objcopy
|
||||
OBJDUMP := $(PREFIX)-objdump
|
||||
GDB := $(PREFIX)-gdb
|
||||
@@ -51,25 +54,28 @@ LIB_DIR := $(INC_DIR)/ld
|
||||
|
||||
###############################################################################
|
||||
# C flags
|
||||
CFLAGS += -O2 -g -MD -D__thumb2__=1
|
||||
CFLAGS += -Wall -Werror -Wextra -Wshadow -Wimplicit-function-declaration
|
||||
CFLAGS += -Wredundant-decls
|
||||
CFLAGS += -O0 -g
|
||||
# -MD -D__thumb2__=1
|
||||
#CFLAGS += -Wall -Werror -Wextra -Wshadow -Wimplicit-function-declaration
|
||||
#CFLAGS += -Wredundant-decls
|
||||
# -Wmissing-prototypes -Wstrict-prototypes
|
||||
CFLAGS += -fno-common -ffunction-sections -fdata-sections
|
||||
CFLAGS += -fno-common
|
||||
#-ffunction-sections -fdata-sections
|
||||
|
||||
###############################################################################
|
||||
# Linker flags
|
||||
LDFLAGS += --static -nostartfiles
|
||||
LDFLAGS += -nostartfiles
|
||||
# --static
|
||||
#--specs=nano.specs
|
||||
LDFLAGS += -L$(LIB_DIR)
|
||||
LDFLAGS += -T$(LDSCRIPT)
|
||||
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map
|
||||
LDFLAGS += -Wl,--gc-sections
|
||||
#LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map
|
||||
#LDFLAGS += -Wl,--gc-sections
|
||||
|
||||
###############################################################################
|
||||
# Used libraries
|
||||
LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group
|
||||
LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
|
||||
#LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group
|
||||
#LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
|
||||
|
||||
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY)
|
||||
|
||||
@@ -82,7 +88,7 @@ LIST := $(OBJDIR)/$(BINARY).list
|
||||
BIN := $(BINARY).bin
|
||||
HEX := $(BINARY).hex
|
||||
|
||||
all: bin list
|
||||
all: bin list size
|
||||
|
||||
elf: $(ELF)
|
||||
bin: $(BIN)
|
||||
@@ -120,7 +126,10 @@ $(LIST): $(ELF)
|
||||
|
||||
$(ELF): $(OBJDIR) $(OBJS)
|
||||
@echo " LD $(ELF)"
|
||||
$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
|
||||
$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
|
||||
|
||||
size: $(ELF)
|
||||
$(SIZE) $(ELF)
|
||||
|
||||
clean:
|
||||
@echo " CLEAN"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
Toggle LEDs (PB8/PB9) on STM32F103 development board depending on buttons PC0,PC1:
|
||||
- no jumper == 'SOS' in Morze
|
||||
- with jumper - blink with period of 4 seconds
|
||||
- no buttons pressed == 'SOS' in Morze @ LED D1
|
||||
- Button S2 pressed - D1 blinks with period of 5s
|
||||
- Button S1 pressed - D2 blinks with period of 1s
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* systick_blink.c
|
||||
* main.c
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||
* Copyright 2018 Edward V. Emelianov <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
|
||||
@@ -21,30 +21,24 @@
|
||||
|
||||
#include "stm32f1.h"
|
||||
|
||||
static volatile uint8_t blink_ctr = 0;
|
||||
static volatile uint32_t blink_ctr = 0;
|
||||
|
||||
/* Called when systick fires */
|
||||
void sys_tick_handler(void){
|
||||
++blink_ctr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set up timer to fire every x milliseconds
|
||||
*/
|
||||
// SysTick is 24 bit counter, so 16777215 - max value!!!
|
||||
// After HSE is ON it works @9MHz (????)
|
||||
static void systick_setup(uint32_t xms){
|
||||
static uint32_t curms = 0;
|
||||
if(curms == xms) return;
|
||||
// 6MHz - HCLK/8 (due to HPRE=1 HCLK=SYSCLK)
|
||||
// this function also clears counter so it starts right away
|
||||
SysTick_Config(6000 * xms);
|
||||
SysTick_Config(72000 * xms);
|
||||
curms = xms;
|
||||
}
|
||||
|
||||
|
||||
/* set STM32 to clock by 48MHz from HSI oscillator */
|
||||
//static void clock_setup(void){
|
||||
//StartHSE(RCC_CR_CSSON | RCC_CR_HSEBYP);
|
||||
//}
|
||||
static const uint16_t L[] = {125,100,125,100,125,200, 60,100,60,100,60,200, 125,100,125,100,125, 230};
|
||||
|
||||
static void gpio_setup(void){
|
||||
/* Enable clocks to the GPIO subsystems (A&B) */
|
||||
@@ -56,42 +50,13 @@ static void gpio_setup(void){
|
||||
GPIOC->CRL = CRL(0, CNF_PUDINPUT|MODE_INPUT) | CRL(1, CNF_PUDINPUT|MODE_INPUT);
|
||||
}
|
||||
|
||||
static const uint16_t L[] = {125,100,125,100,125,200, 350,100,350,100,350,200, 125,100,125,100,125, 1000};
|
||||
|
||||
int main(void){
|
||||
int main(){
|
||||
sysreset();
|
||||
// AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE; // turn off jtag and swim
|
||||
// StartHSE();
|
||||
|
||||
RCC->CFGR &= ~RCC_CFGR_SW; // Change System Clock to HSI
|
||||
while ((RCC->CFGR & RCC_CFGR_SWS) != 0x00) {
|
||||
__NOP();
|
||||
};
|
||||
RCC->CR &= ~RCC_CR_PLLON; // Disable Pll
|
||||
while ((RCC->CR & RCC_CR_PLLON)) {
|
||||
__NOP();
|
||||
};
|
||||
RCC->CFGR &= ~0x3C0000;
|
||||
RCC->CFGR |= RCC_CFGR_PLLMULL4; // Set Pll Mul to 4
|
||||
RCC->CFGR |= RCC_CFGR_USBPRE;
|
||||
RCC->CFGR |= RCC_CFGR_PLLSRC;
|
||||
RCC->CR |= RCC_CR_PLLON;
|
||||
while (!(RCC->CR & RCC_CR_PLLON)) {
|
||||
__NOP();
|
||||
};
|
||||
RCC->CFGR |= RCC_CFGR_SW_1; // Change System Clock to PLL
|
||||
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_1) {
|
||||
__NOP();
|
||||
};
|
||||
|
||||
StartHSE();
|
||||
gpio_setup();
|
||||
|
||||
/* 500ms ticks => 1000ms period => 1Hz blinks */
|
||||
systick_setup(100);
|
||||
uint8_t oldctr = blink_ctr+1;
|
||||
pin_clear(GPIOB, 1<<8);
|
||||
pin_clear(GPIOB, 1<<9);
|
||||
for(int i = 0; i < 1000000; ++i) nop();
|
||||
uint32_t oldctr = 0xff;
|
||||
pin_clear(GPIOB, 3<<8);
|
||||
/* Do nothing in main loop */
|
||||
while (1){
|
||||
if(pin_read(GPIOC, 1) && pin_read(GPIOC, 2)){ // no buttons present - morze @ LED1 (PB9)
|
||||
@@ -103,13 +68,22 @@ RCC->CFGR &= ~RCC_CFGR_SW; // Change System Clock to HSI
|
||||
oldctr = blink_ctr;
|
||||
}
|
||||
}else{ // button pressed: turn ON given LED
|
||||
if(pin_read(GPIOC, 1)){ // PC0 pressed (button S2)
|
||||
pin_clear(GPIOB, 1<<8);
|
||||
}else pin_set(GPIOB, 1<<8);
|
||||
if(pin_read(GPIOC, 2)){ // PC1 pressed (button S3)
|
||||
pin_clear(GPIOB, 1<<9);
|
||||
if(pin_read(GPIOC, 1) == 0){ // PC0 pressed (button S2)
|
||||
systick_setup(5);
|
||||
if(blink_ctr - oldctr > 499){
|
||||
pin_toggle(GPIOB, 1<<9);
|
||||
oldctr = blink_ctr;
|
||||
}
|
||||
}else pin_set(GPIOB, 1<<9);
|
||||
if(pin_read(GPIOC, 2) == 0){ // PC1 pressed (button S3)
|
||||
systick_setup(1);
|
||||
if(blink_ctr - oldctr > 499){
|
||||
pin_toggle(GPIOB, 1<<8);
|
||||
oldctr = blink_ctr;
|
||||
}
|
||||
}else pin_set(GPIOB, 1<<8);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* systick_blink.c
|
||||
*
|
||||
* 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 "stm32f1.h"
|
||||
|
||||
int main(void){
|
||||
sysreset();
|
||||
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
|
||||
GPIOB->CRH = 0x00000066; // PB8/9 - 2MHz opendrain
|
||||
GPIOB->ODR = 0;
|
||||
while(1){}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* systick_blink.c
|
||||
*
|
||||
* 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 "stm32f1.h"
|
||||
|
||||
int main(void){
|
||||
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
|
||||
GPIOB->CRH = 0x00000066; // PB8/9 - 2MHz opendrain
|
||||
GPIOB->ODR = 0;
|
||||
while(1){}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user