Skip to content

Commit

Permalink
Updated Makefile to build for cantact/disco boards (build with make B…
Browse files Browse the repository at this point in the history
…ULID_HW=disco, default cantact, if no arguments specified)

Added (well, actually copied from CMSIS template folder) startup/linker files for stmf072 for gcc build
Made LED GPIO ports selectable for each LED (still need to edit LED_GPIO_CLK by the user but at that point they should know what they're doing)
  • Loading branch information
trollcop committed Jan 19, 2021
1 parent 69bd7c8 commit abca338
Show file tree
Hide file tree
Showing 7 changed files with 517 additions and 28 deletions.
6 changes: 4 additions & 2 deletions Inc/led.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

#ifdef LINKLAYER
// LINKLAYER CANTACT-HW
#define LED_GPIO GPIOB
#define LED_GREEN_GPIO GPIOB
#define LED_RED_GPIO GPIOB
#define LED_GPIO_CLK __GPIOB_CLK_ENABLE
#define LED_GREEN (GPIO_PIN_0)
#define LED_RED (GPIO_PIN_1)
Expand All @@ -14,7 +15,8 @@

// STM32F072B-DISCOVERY

#define LED_GPIO GPIOC
#define LED_GREEN_GPIO GPIOC
#define LED_RED_GPIO GPIOC
#define LED_GPIO_CLK __GPIOC_CLK_ENABLE
#define LED_GREEN (GPIO_PIN_9)
#define LED_RED (GPIO_PIN_8)
Expand Down
41 changes: 25 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,33 @@
#######################################

BUILD_NUMBER ?= 0
# build hw can be either 'cantact' or 'disco'
BUILD_HW ?= cantact

# BUILD_DIR: directory to place output files in
BUILD_DIR = build

ifeq ($(BUILD_HW),cantact)
EXTRA_DEFS = -D LINKLAYER
STARTUP_S = $(BUILD_DIR)/startup_stm32f042x6.o
TARGET_DEVICE = STM32F042x6
LD_SCRIPT = STM32F042C6_FLASH.ld
endif
ifeq ($(BUILD_HW),disco)
EXTRA_DEFS =
STARTUP_S = $(BUILD_DIR)/startup_stm32f072xb.o
TARGET_DEVICE = STM32F072xB
LD_SCRIPT = STM32F072RB_FLASH.ld
endif

# SOURCES: list of sources in the user application
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c can.c slcan.c led.c

# TARGET: name of the user application
TARGET = CANtact-b$(BUILD_NUMBER)

# BUILD_DIR: directory to place output files in
BUILD_DIR = build

# LD_SCRIPT: location of the linker script
LD_SCRIPT = STM32F042C6_FLASH.ld

# USER_DEFS user defined macros
USER_DEFS = -D HSI48_VALUE=48000000 -D HSE_VALUE=16000000
USER_DEFS = -D HSI48_VALUE=48000000 -D HSE_VALUE=16000000 $(EXTRA_DEFS)
USER_DEFS += -D CANTACT_BUILD_NUMBER=$(BUILD_NUMBER)
# USER_INCLUDES: user defined includes
USER_INCLUDES =
Expand All @@ -37,9 +49,6 @@ USER_CFLAGS = -Wall -g -ffunction-sections -fdata-sections -Os
# USER_LDFLAGS: user LD flags
USER_LDFLAGS = -fno-exceptions -ffunction-sections -fdata-sections -Wl,--gc-sections

# TARGET_DEVICE: device to compile for
TARGET_DEVICE = STM32F042x6

#######################################
# end of user configuration
#######################################
Expand Down Expand Up @@ -143,7 +152,7 @@ $(USB_BUILD_DIR):
# list of user program objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:.c=.o)))
# add an object for the startup code
OBJECTS += $(BUILD_DIR)/startup_stm32f042x6.o
OBJECTS += $(STARTUP_S)

# use the periphlib core library, plus generic ones (libc, libm, libnosys)
LIBS = -lstm32cube -lc -lm -lnosys
Expand Down Expand Up @@ -172,10 +181,10 @@ $(BUILD_DIR):

# delete all user application files, keep the libraries
clean:
-rm $(BUILD_DIR)/*.o
-rm $(BUILD_DIR)/*.elf
-rm $(BUILD_DIR)/*.hex
-rm $(BUILD_DIR)/*.map
-rm $(BUILD_DIR)/*.bin
-rm -f $(BUILD_DIR)/*.o
-rm -f $(BUILD_DIR)/*.elf
-rm -f $(BUILD_DIR)/*.hex
-rm -f $(BUILD_DIR)/*.map
-rm -f $(BUILD_DIR)/*.bin

.PHONY: clean all cubelib
168 changes: 168 additions & 0 deletions STM32F072RB_FLASH.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
*****************************************************************************
**

** File : stm32_flash.ld
**
** Abstract : Linker script for STM32F072RB Device with
** 128KByte FLASH, 16KByte RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used.
**
** Target : STMicroelectronics STM32
**
** Environment : Atollic TrueSTUDIO(R)
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
** (c)Copyright Atollic AB.
** You may use this file as-is or modify it according to the needs of your
** project. This file may only be built (assembled or compiled and linked)
** using the Atollic TrueSTUDIO(R) product. The use of this file together
** with other tools than Atollic TrueSTUDIO(R) is not permitted.
**
*****************************************************************************
*/

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20004000; /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 16K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 128K
}

/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH

/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)

KEEP (*(.init))
KEEP (*(.fini))

. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH

/* Constant data goes into FLASH */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH

.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH

.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH

/* used by the startup to initialize data */
_sidata = LOADADDR(.data);

/* Initialized data sections goes into RAM, load LMA copy after code */
.data :
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */

. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM AT> FLASH


/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)

. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAM

/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(4);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(4);
} >RAM



/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}

.ARM.attributes 0 : { *(.ARM.attributes) }
}


2 changes: 1 addition & 1 deletion Src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void can_disable(void) {
hcan.Instance->MCR |= CAN_MCR_RESET;
bus_state = OFF_BUS;
}
HAL_GPIO_WritePin(LED_GPIO, LED_RED, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_RESET);
}

void can_set_bitrate(enum can_bitrate bitrate) {
Expand Down
4 changes: 2 additions & 2 deletions Src/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void led_on(void)
// This prevents a solid status LED on a busy canbus
if(led_laston == 0 && HAL_GetTick() - led_lastoff > LED_DURATION)
{
HAL_GPIO_WritePin(LED_GPIO, LED_RED, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_SET);
led_laston = HAL_GetTick();
}
}
Expand All @@ -27,7 +27,7 @@ void led_process(void)
// If LED has been on for long enough, turn it off
if(led_laston > 0 && HAL_GetTick() - led_laston > LED_DURATION)
{
HAL_GPIO_WritePin(LED_GPIO, LED_RED, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_RESET);
led_laston = 0;
led_lastoff = HAL_GetTick();
}
Expand Down
16 changes: 9 additions & 7 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ int main(void)


// turn on green LED
HAL_GPIO_WritePin(LED_GPIO, LED_GREEN, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_GREEN_GPIO, LED_GREEN, GPIO_PIN_SET);

// blink red LED for test
HAL_GPIO_WritePin(LED_GPIO, LED_RED, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_GPIO, LED_RED, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_GPIO, LED_RED, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LED_GPIO, LED_RED, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_RESET);

// loop forever
CanRxMsgTypeDef rx_msg;
Expand Down Expand Up @@ -241,12 +241,14 @@ void MX_GPIO_Init(void)
static void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = { 0, };
GPIO_InitStruct.Pin = LED_RED | LED_GREEN;
GPIO_InitStruct.Pin = LED_GREEN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStruct.Alternate = 0;
HAL_GPIO_Init(LED_GPIO, &GPIO_InitStruct);
HAL_GPIO_Init(LED_GREEN_GPIO, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LED_RED;
HAL_GPIO_Init(LED_RED_GPIO, &GPIO_InitStruct);
}
/* USER CODE END 4 */

Expand Down
Loading

0 comments on commit abca338

Please sign in to comment.