Skip to content

Commit

Permalink
merge INTERNAL_OSCILLATOR code from https://github.com/normaldotcom/c…
Browse files Browse the repository at this point in the history
  • Loading branch information
nexulm committed Nov 13, 2016
1 parent a6660dc commit 55248cc
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 32 deletions.
32 changes: 25 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
BUILD_NUMBER ?= 0

# 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
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 ring_buffer.c

# TARGET: name of the user application
TARGET = CANtact-b$(BUILD_NUMBER)
Expand All @@ -20,7 +20,12 @@ TARGET = CANtact-b$(BUILD_NUMBER)
BUILD_DIR = build

# LD_SCRIPT: location of the linker script
LD_SCRIPT = STM32F042C6_FLASH.ld
ifeq ($(STM32F072), 1)
LD_SCRIPT = STM32F072VB_FLASH.ld
else
LD_SCRIPT = STM32F042C6_FLASH.ld
endif


# USER_DEFS user defined macros
USER_DEFS = -D HSI48_VALUE=48000000 -D HSE_VALUE=16000000
Expand All @@ -34,11 +39,20 @@ USB_INCLUDES += -IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc

# USER_CFLAGS: user C flags (enable warnings, enable debug info)
USER_CFLAGS = -Wall -g -ffunction-sections -fdata-sections -Os

ifeq ($(INTERNAL_OSCILLATOR), 1)
USER_CFLAGS += -DINTERNAL_OSCILLATOR
endif

# 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
ifeq ($(STM32F072), 1)
TARGET_DEVICE = STM32F072xB
else
TARGET_DEVICE = STM32F042x6
endif

#######################################
# end of user configuration
Expand Down Expand Up @@ -73,7 +87,7 @@ DRIVER_PATH = Drivers/STM32F0xx_HAL_Driver
INCLUDES = -I$(CMSIS_PATH)/Include
INCLUDES += -I$(CMSIS_DEVICE_PATH)/Include
INCLUDES += -I$(DRIVER_PATH)/Inc
INCLUDES += -IInc
INCLUDES += -Iinc
INCLUDES += $(USB_INCLUDES)
INCLUDES += $(USER_INCLUDES)

Expand Down Expand Up @@ -143,7 +157,11 @@ $(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
ifeq ($(STM32F072), 1)
OBJECTS += $(BUILD_DIR)/startup_stm32f072xb.o
else
OBJECTS += $(BUILD_DIR)/startup_stm32f042x6.o
endif

# use the periphlib core library, plus generic ones (libc, libm, libnosys)
LIBS = -lstm32cube -lc -lm -lnosys
Expand All @@ -161,10 +179,10 @@ $(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) $(USB_OBJECTS) $(CUBELIB)
-Map=$(BUILD_DIR)/$(TARGET).map
$(SIZE) $@

$(BUILD_DIR)/%.o: Src/%.c | $(BUILD_DIR)
$(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -Os -c -o $@ $^

$(BUILD_DIR)/%.o: Src/%.s | $(BUILD_DIR)
$(BUILD_DIR)/%.o: src/%.s | $(BUILD_DIR)
$(CC) $(CFLAGS) -c -o $@ $^

$(BUILD_DIR):
Expand Down
113 changes: 90 additions & 23 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,97 @@ int main(void)
*/
void system_clock_config(void)
{
RCC_OscInitTypeDef osc_init;
RCC_ClkInitTypeDef clk_init;
RCC_PeriphCLKInitTypeDef periph_clk_init;

osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSE;
osc_init.HSEState = RCC_HSE_ON;
osc_init.PLL.PLLState = RCC_PLL_ON;
osc_init.PLL.PLLSource = RCC_PLLSOURCE_HSE;
osc_init.PLL.PLLMUL = RCC_PLL_MUL6;
osc_init.PLL.PREDIV = RCC_PREDIV_DIV1;
HAL_RCC_OscConfig(&osc_init);

clk_init.ClockType = RCC_CLOCKTYPE_SYSCLK;
clk_init.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clk_init.AHBCLKDivider = RCC_SYSCLK_DIV1;
clk_init.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&clk_init, FLASH_LATENCY_1);

periph_clk_init.PeriphClockSelection = RCC_PERIPHCLK_USB;
periph_clk_init.UsbClockSelection = RCC_USBCLKSOURCE_PLLCLK;
HAL_RCCEx_PeriphCLKConfig(&periph_clk_init);

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;

#ifdef INTERNAL_OSCILLATOR
// set up the oscillators
// use internal HSI48 (48 MHz), no PLL
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

// set sysclk, hclk, and pclk1 source to HSI48 (48 MHz)
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

// set USB clock source to HSI48 (48 MHz)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;


#else
// set up the oscillators
// use external oscillator (16 MHz), enable 3x PLL (48 MHz)
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;

// set sysclk, hclk, and pclk1 source to PLL (48 MHz)
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

// set USB clock source to PLL (48 MHz)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLCLK;

#endif

HAL_RCC_OscConfig(&RCC_OscInitStruct);
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__SYSCFG_CLK_ENABLE();


HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);


#ifdef INTERNAL_OSCILLATOR
// Enable clock recovery system for internal oscillator
RCC_CRSInitTypeDef RCC_CRSInitStruct;

// Enable CRS Clock
__CRS_CLK_ENABLE();

// Default Synchro Signal division factor (not divided)
RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;

// Set the SYNCSRC[1:0] bits according to CRS_Source value
RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB;

// HSI48 is synchronized with USB SOF at 1KHz rate
RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_CALCULATE_RELOADVALUE(48000000, 1000);
RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT;

// Set the TRIM[5:0] to the default value
RCC_CRSInitStruct.HSI48CalibrationValue = 0x20;

// Start automatic synchronization
HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);

// Force sync event
HAL_RCCEx_CRSSoftwareSynchronizationGenerate();

// Wait until synchronized
HAL_RCCEx_CRSWaitSynchronization(3000);
#endif

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
__GPIOF_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
}

/**
Expand Down
Empty file modified src/startup_stm32f042x6.s
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions src/startup_stm32f072xb.s
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
******************************************************************************
* @file startup_stm32f072xb.s
* @author MCD Application Team
* @version V2.2.1
* @date 09-January-2015
* @version V2.3.0
* @date 27-May-2016
* @brief STM32F072x8/STM32F072xB devices vector table for Atollic TrueSTUDIO toolchain.
* This module performs:
* - Set the initial SP
Expand Down

0 comments on commit 55248cc

Please sign in to comment.