0

I am trying to generate system_stm32f1xx.c which will set up my clock as shown on the picture. System clock configuration

The problem is that, when I generate the code with CubeMX, I don't get this configuration. Instead, I get the (I guess) default configuration with internal RC clock enabled.

I had many problem with configuring timers and USART before I find out that some thing is wrong with my clock configuration.

Content of the system_init() function.

void SystemInit (void)
{
  /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  /* Set HSION bit */
  RCC->CR |= 0x00000001U;

  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#if !defined(STM32F105xC) && !defined(STM32F107xC)
  RCC->CFGR &= 0xF8FF0000U;
#else
  RCC->CFGR &= 0xF0FF0000U;
#endif /* STM32F105xC */   

  /* Reset HSEON, CSSON and PLLON bits */
  RCC->CR &= 0xFEF6FFFFU;

  /* Reset HSEBYP bit */
  RCC->CR &= 0xFFFBFFFFU;

  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  RCC->CFGR &= 0xFF80FFFFU;

#if defined(STM32F105xC) || defined(STM32F107xC)
  /* Reset PLL2ON and PLL3ON bits */
  RCC->CR &= 0xEBFFFFFFU;

  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x00FF0000U;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000U;
#elif defined(STM32F100xB) || defined(STM32F100xE)
  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x009F0000U;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000U;      
#else
  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x009F0000U;
#endif /* STM32F105xC */

#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  #ifdef DATA_IN_ExtSRAM
    SystemInit_ExtMemCtl(); 
  #endif /* DATA_IN_ExtSRAM */
#endif 

#ifdef VECT_TAB_SRAM
  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#else
  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
#endif 
}
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
AC Voltage
  • 339
  • 3
  • 9

2 Answers2

1

The clock configuration function is in the generated by the CubeMx main.c file and is called SystemClock_Config. The SystemInit does not set the clock. I do not know why my previos answer was removed as it answers the question.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • I saw that system_init() didn't do anything to my RCC registers but I thought that something is wrong with CubeMx. I am trying to use my custom Makefile without any IDE, and because of that I never opened main.c generated by CubeMx. I will try to find how to configure clock in register ( I don't like to use API provided by IDE-s), and I would appreciate if someone could point me where I could start my research. – AC Voltage Mar 26 '18 at 19:35
  • 1
    It is easy. I personally use CubeMx only because it is much easier to assign pins and see the conflicts using this tool. I also use the clock configuration - and just take the multipliers and dividers from the screen. F103 is a very simple uC. F7's clock tree is much more complicated and this clock tool helps a lot. – 0___________ Mar 26 '18 at 19:59
  • @ACVoltage I do not see ant reason in the 21st century to use DIY makefile during the development stage. I write one when I publish the project. IDE-s are perfect tool, you have exactly the same control, but it is just much easier. – 0___________ Mar 26 '18 at 20:02
  • PS I am the bare register programmer – 0___________ Mar 26 '18 at 20:03
  • I know that IDE-s are great tool, but I am Linux user and also very big fan of VIM and feel lot more comfortable when combining tmux and VIM, and I feel more productive working like this. Then, using makefile comes so natural for me, and they are in my case generic. Like, I have this Makefile for STM MCU ( I use them on STM32F103 and STM32F407, and I believe that I can use them on any other STM uC), and same story goes for Atmel uC. – AC Voltage Mar 26 '18 at 20:31
0

The clock configuration function is in tge main.c file. Do not touch the system files unless you know what you are doing.

SystemInit does something else and you should leave it as it is

0___________
  • 60,014
  • 4
  • 34
  • 74