Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I added support to run cantact-fw on STM32F072B-DISCO and build files for MDK-ARM/Keil #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Inc/led.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
#ifndef _LED_H
#define _LED_H


#define LED_DURATION 50

#ifdef LINKLAYER
// LINKLAYER CANTACT-HW
#define LED_GREEN_GPIO GPIOB
#define LED_RED_GPIO GPIOB
#define LED_GREEN_GPIO_CLK __GPIOB_CLK_ENABLE
#define LED_RED_GPIO_CLK __GPIOB_CLK_ENABLE
#define LED_GREEN (GPIO_PIN_0)
#define LED_RED (GPIO_PIN_1)

#else

// STM32F072B-DISCOVERY

#define LED_GREEN_GPIO GPIOC
#define LED_RED_GPIO GPIOC
#define LED_GREEN_GPIO_CLK __GPIOC_CLK_ENABLE
#define LED_RED_GPIO_CLK __GPIOC_CLK_ENABLE
#define LED_GREEN (GPIO_PIN_9)
#define LED_RED (GPIO_PIN_8)

#endif

void led_on(void);
void led_process(void);

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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ This repository contains sources for CANtact firmware.

Documentation can be found on the [Linklayer Wiki](https://wiki.linklayer.com/index.php/CANtact).

## STM32F072B-DISCO Notes

LD4: "RED" led (can frame rx/tx)
LD5: "Green" led (status)

CanBUS connection:
PB9: `CAN_TX`
PB8: `CAN_RX`

Firmware runs from internal HSI48 oscillator.
The connectors are nicely aligned together in top right of the board, on P2 connector.
GND, PB9, PB8, VCC is available in a single row.

Tested with: Waveshare CAN breakout board

USB connection via `USB USER` connector on the board

## Building

Firmware builds with GCC. Specifically, you will need gcc-arm-none-eabi, which
Expand Down
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(GPIOB, GPIO_PIN_1, 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(GPIOB, GPIO_PIN_1, 1);
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(GPIOB, GPIO_PIN_1, 0);
HAL_GPIO_WritePin(LED_RED_GPIO, LED_RED, GPIO_PIN_RESET);
led_laston = 0;
led_lastoff = HAL_GetTick();
}
Expand Down
Loading