drv_clk.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-26 ChenYong first version
  9. * 2020-01-08 xiangxistu add HSI configuration
  10. */
  11. #include <board.h>
  12. #include <rtthread.h>
  13. #include <stm32f1xx.h>
  14. #include "drv_common.h"
  15. #define DBG_TAG "board"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. void system_clock_config(int target_freq_Mhz)
  19. {
  20. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  21. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  22. /** Initializes the CPU, AHB and APB busses clocks
  23. */
  24. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  25. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  26. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  27. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  28. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  29. #if defined(STM32F100xB) || defined(STM32F100xE)
  30. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  31. #endif
  32. #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG)
  33. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  34. #endif
  35. #if defined(STM32F102x6) || defined(STM32F102xB)
  36. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  37. #endif
  38. #if defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
  39. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  40. #endif
  41. #if defined(STM32F105xC) || defined(STM32F107xC)
  42. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  43. #endif
  44. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  45. {
  46. Error_Handler();
  47. }
  48. /** Initializes the CPU, AHB and APB busses clocks
  49. */
  50. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  51. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  52. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  53. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  54. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  55. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  56. #if defined(STM32F100xB) || defined(STM32F100xE)
  57. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  58. #endif
  59. #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG)
  60. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  61. #endif
  62. #if defined(STM32F102x6) || defined(STM32F102xB)
  63. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  64. #endif
  65. #if defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
  66. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  67. #endif
  68. #if defined(STM32F105xC) || defined(STM32F107xC)
  69. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  70. #endif
  71. {
  72. Error_Handler();
  73. }
  74. }
  75. void clk_init(char *clk_source, int source_freq, int target_freq)
  76. {
  77. system_clock_config(target_freq);
  78. }