搜索
bottom↓
回复: 17

AT32F403A时钟直接寄存器设置成功频率却没变化

[复制链接]

出0入0汤圆

发表于 2022-5-24 21:53:39 | 显示全部楼层 |阅读模式
因为原代码是stm的不想再改,所以源文件的库都是用的stm的,看到at32f403a的频率可以到200m所以想其它的代码还是用stm一样,只是把频率用AT的库设置到192m,原代码是先用stm的库设置8m,倍频到72m,然后引用M_At32F40X_SystemClock(24),
24倍频到192m,因为直接加入引用at的库时,和stm的有些冲突,所以把at库里用到的枚举和函数复制过来,设置完成后,用stm的库函数RCC_GetClocksFreq(&RCC_ClocksStatus);  //读出时钟配置
如果小于17倍,读出的设置频率和设置的一样,大于17反而会变小,如16倍,读出设置的是128M,24倍本来应该倍频到192M的读出反而是64M,但是如果已设置的128M,实际运行的速度却没有,用指示灯闪烁查看
哪位兄度熟点测试下
  1. /**
  2.   **************************************************************************
  3.   * @file     at32f403a_407_clock.c
  4.   * @version  v2.0.9
  5.   * @date     2022-04-25
  6.   * @brief    system clock config program
  7.   **************************************************************************
  8.   *                       Copyright notice & Disclaimer
  9.   *
  10.   * The software Board Support Package (BSP) that is made available to
  11.   * download from Artery official website is the copyrighted work of Artery.
  12.   * Artery authorizes customers to use, copy, and distribute the BSP
  13.   * software and its related documentation for the purpose of design and
  14.   * development in conjunction with Artery microcontrollers. Use of the
  15.   * software is governed by this copyright notice and the following disclaimer.
  16.   *
  17.   * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  18.   * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  19.   * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  20.   * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  21.   * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  22.   * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  23.   *
  24.   **************************************************************************
  25.   */

  26. /* includes ------------------------------------------------------------------*/
  27. //#include "at32f403a_407_clock.h"

  28. /** @addtogroup AT32F403A_periph_template
  29.   * @{
  30.   */

  31. /** @addtogroup 403A_System_clock_configuration System_clock_configuration
  32.   * @{
  33.   */

  34. /**
  35.   * @brief  system clock config program
  36.   * @note   the system clock is configured as follow:
  37.   *         - system clock        = hext / 2 * pll_mult
  38.   *         - system clock source = pll (hext)
  39.   *         - hext                = 8000000
  40.   *         - sclk                = 240000000
  41.   *         - ahbdiv              = 1
  42.   *         - ahbclk              = 240000000
  43.   *         - apb2div             = 2
  44.   *         - apb2clk             = 120000000
  45.   *         - apb1div             = 2
  46.   *         - apb1clk             = 120000000
  47.   *         - pll_mult            = 60
  48.   *         - pll_range           = GT72MHZ (greater than 72 mhz)
  49.   * @param  none
  50.   * @retval none
  51.   */

  52. typedef struct
  53. {
  54.   /**
  55.     * @brief crm ctrl register, offset:0x00
  56.     */
  57.   union
  58.   {
  59.     __IO uint32_t ctrl;
  60.     struct
  61.     {
  62.       __IO uint32_t hicken               : 1; /* [0] */
  63.       __IO uint32_t hickstbl             : 1; /* [1] */
  64.       __IO uint32_t hicktrim             : 6; /* [7:2] */
  65.       __IO uint32_t hickcal              : 8; /* [15:8] */
  66.       __IO uint32_t hexten               : 1; /* [16] */
  67.       __IO uint32_t hextstbl             : 1; /* [17] */
  68.       __IO uint32_t hextbyps             : 1; /* [18] */
  69.       __IO uint32_t cfden                : 1; /* [19] */
  70.       __IO uint32_t reserved1            : 4; /* [23:20] */
  71.       __IO uint32_t pllen                : 1; /* [24] */
  72.       __IO uint32_t pllstbl              : 1; /* [25] */
  73.       __IO uint32_t reserved2            : 6; /* [31:26] */
  74.     } ctrl_bit;
  75.   };

  76.   /**
  77.     * @brief crm cfg register, offset:0x04
  78.     */
  79.   union
  80.   {
  81.     __IO uint32_t cfg;
  82.     struct
  83.     {
  84.       __IO uint32_t sclksel              : 2; /* [1:0] */
  85.       __IO uint32_t sclksts              : 2; /* [3:2] */
  86.       __IO uint32_t ahbdiv               : 4; /* [7:4] */
  87.       __IO uint32_t apb1div              : 3; /* [10:8] */
  88.       __IO uint32_t apb2div              : 3; /* [13:11] */
  89.       __IO uint32_t adcdiv_l             : 2; /* [15:14] */
  90.       __IO uint32_t pllrcs               : 1; /* [16] */
  91.       __IO uint32_t pllhextdiv           : 1; /* [17] */
  92.       __IO uint32_t pllmult_l            : 4; /* [21:18] */
  93.       __IO uint32_t usbdiv_l             : 2; /* [23:22] */
  94.       __IO uint32_t clkout_sel           : 3; /* [26:24] */
  95.       __IO uint32_t usbdiv_h             : 1; /* [27] */
  96.       __IO uint32_t adcdiv_h             : 1; /* [28] */
  97.       __IO uint32_t pllmult_h            : 2; /* [30:29] */
  98.       __IO uint32_t pllrange             : 1; /* [31] */
  99.     } cfg_bit;
  100.   };

  101.   /**
  102.     * @brief crm clkint register, offset:0x08
  103.     */
  104.   union
  105.   {
  106.     __IO uint32_t clkint;
  107.     struct
  108.     {
  109.       __IO uint32_t lickstblf            : 1; /* [0] */
  110.       __IO uint32_t lextstblf            : 1; /* [1] */
  111.       __IO uint32_t hickstblf            : 1; /* [2] */
  112.       __IO uint32_t hextstblf            : 1; /* [3] */
  113.       __IO uint32_t pllstblf             : 1; /* [4] */
  114.       __IO uint32_t reserved1            : 2; /* [6:5] */
  115.       __IO uint32_t cfdf                 : 1; /* [7] */
  116.       __IO uint32_t lickstblien          : 1; /* [8] */
  117.       __IO uint32_t lextstblien          : 1; /* [9] */
  118.       __IO uint32_t hickstblien          : 1; /* [10] */
  119.       __IO uint32_t hextstblien          : 1; /* [11] */
  120.       __IO uint32_t pllstblien           : 1; /* [12] */
  121.       __IO uint32_t reserved2            : 3; /* [15:13] */
  122.       __IO uint32_t lickstblfc           : 1; /* [16] */
  123.       __IO uint32_t lextstblfc           : 1; /* [17] */
  124.       __IO uint32_t hickstblfc           : 1; /* [18] */
  125.       __IO uint32_t hextstblfc           : 1; /* [19] */
  126.       __IO uint32_t pllstblfc            : 1; /* [20] */
  127.       __IO uint32_t reserved3            : 2; /* [22:21] */
  128.       __IO uint32_t cfdfc                : 1; /* [23] */
  129.       __IO uint32_t reserved4            : 8; /* [31:24] */
  130.     } clkint_bit;
  131.   };

  132.   /**
  133.     * @brief crm apb2rst register, offset:0x0C
  134.     */
  135.   union
  136.   {
  137.     __IO uint32_t apb2rst;
  138.     struct
  139.     {
  140.       __IO uint32_t iomuxrst             : 1; /* [0] */
  141.       __IO uint32_t exintrst             : 1; /* [1] */
  142.       __IO uint32_t gpioarst             : 1; /* [2] */
  143.       __IO uint32_t gpiobrst             : 1; /* [3] */
  144.       __IO uint32_t gpiocrst             : 1; /* [4] */
  145.       __IO uint32_t gpiodrst             : 1; /* [5] */
  146.       __IO uint32_t gpioerst             : 1; /* [6] */
  147.       __IO uint32_t reserved1            : 2; /* [8:7] */
  148.       __IO uint32_t adc1rst              : 1; /* [9] */
  149.       __IO uint32_t adc2rst              : 1; /* [10] */
  150.       __IO uint32_t tmr1rst              : 1; /* [11] */
  151.       __IO uint32_t spi1rst              : 1; /* [12] */
  152.       __IO uint32_t tmr8rst              : 1; /* [13] */
  153.       __IO uint32_t usart1rst            : 1; /* [14] */
  154.       __IO uint32_t adc3rst              : 1; /* [15] */
  155.       __IO uint32_t reserved2            : 3; /* [18:16] */
  156.       __IO uint32_t tmr9rst              : 1; /* [19] */
  157.       __IO uint32_t tmr10rst             : 1; /* [20] */
  158.       __IO uint32_t tmr11rst             : 1; /* [21] */
  159.       __IO uint32_t accrst               : 1; /* [22] */
  160.       __IO uint32_t i2c3rst              : 1; /* [23] */
  161.       __IO uint32_t usart6rst            : 1; /* [24] */
  162.       __IO uint32_t uart7rst             : 1; /* [25] */
  163.       __IO uint32_t uart8rst             : 1; /* [26] */
  164.       __IO uint32_t reserved3            : 5; /* [31:27] */
  165.     } apb2rst_bit;
  166.   };

  167.   /**
  168.     * @brief crm apb1rst register, offset:0x10
  169.     */
  170.   union
  171.   {
  172.     __IO uint32_t apb1rst;
  173.     struct
  174.     {
  175.       __IO uint32_t tmr2rst              : 1; /* [0] */
  176.       __IO uint32_t tmr3rst              : 1; /* [1] */
  177.       __IO uint32_t tmr4rst              : 1; /* [2] */
  178.       __IO uint32_t tmr5rst              : 1; /* [3] */
  179.       __IO uint32_t tmr6rst              : 1; /* [4] */
  180.       __IO uint32_t tmr7rst              : 1; /* [5] */
  181.       __IO uint32_t tmr12rst             : 1; /* [6] */
  182.       __IO uint32_t tmr13rst             : 1; /* [7] */
  183.       __IO uint32_t tmr14rst             : 1; /* [8] */
  184.       __IO uint32_t reserved1            : 2; /* [10:9] */
  185.       __IO uint32_t wwdtrst              : 1; /* [11] */
  186.       __IO uint32_t reserved2            : 2; /* [13:12] */
  187.       __IO uint32_t spi2rst              : 1; /* [14] */
  188.       __IO uint32_t spi3rst              : 1; /* [15] */
  189.       __IO uint32_t spi4rst              : 1; /* [16] */
  190.       __IO uint32_t usart2rst            : 1; /* [17] */
  191.       __IO uint32_t usart3rst            : 1; /* [18] */
  192.       __IO uint32_t uart4rst             : 1; /* [19] */
  193.       __IO uint32_t uart5rst             : 1; /* [20] */
  194.       __IO uint32_t i2c1rst              : 1; /* [21] */
  195.       __IO uint32_t i2c2rst              : 1; /* [22] */
  196.       __IO uint32_t usbrst               : 1; /* [23] */
  197.       __IO uint32_t reserved3            : 1; /* [24] */
  198.       __IO uint32_t can1rst              : 1; /* [25] */
  199.       __IO uint32_t can2rst              : 1; /* [26] */
  200.       __IO uint32_t bprrst               : 1; /* [27] */
  201.       __IO uint32_t pwcrst               : 1; /* [28] */
  202.       __IO uint32_t dacrst               : 1; /* [29] */
  203.       __IO uint32_t reserved4            : 2; /* [31:30] */
  204.     } apb1rst_bit;
  205.   };

  206.   /**
  207.     * @brief crm ahben register, offset:0x14
  208.     */
  209.   union
  210.   {
  211.     __IO uint32_t ahben;
  212. #if defined (AT32F403Axx)
  213.     struct
  214.     {
  215.       __IO uint32_t dma1en               : 1; /* [0] */
  216.       __IO uint32_t dma2en               : 1; /* [1] */
  217.       __IO uint32_t sramen               : 1; /* [2] */
  218.       __IO uint32_t reserved1            : 1; /* [3] */
  219.       __IO uint32_t flashen              : 1; /* [4] */
  220.       __IO uint32_t reserved2            : 1; /* [5] */
  221.       __IO uint32_t crcen                : 1; /* [6] */
  222.       __IO uint32_t reserved3            : 1; /* [7] */
  223.       __IO uint32_t xmcen                : 1; /* [8] */
  224.       __IO uint32_t reserved4            : 1; /* [9] */
  225.       __IO uint32_t sdio1en              : 1; /* [10] */
  226.       __IO uint32_t sdio2en              : 1; /* [11] */
  227.       __IO uint32_t reserved5            : 20;/* [31:12] */
  228.     } ahben_bit;
  229. #endif
  230. #if defined (AT32F407xx)
  231.     struct
  232.     {
  233.       __IO uint32_t dma1en               : 1; /* [0] */
  234.       __IO uint32_t dma2en               : 1; /* [1] */
  235.       __IO uint32_t sramen               : 1; /* [2] */
  236.       __IO uint32_t reserved1            : 1; /* [3] */
  237.       __IO uint32_t flashen              : 1; /* [4] */
  238.       __IO uint32_t reserved2            : 1; /* [5] */
  239.       __IO uint32_t crcen                : 1; /* [6] */
  240.       __IO uint32_t reserved3            : 1; /* [7] */
  241.       __IO uint32_t xmcen                : 1; /* [8] */
  242.       __IO uint32_t reserved4            : 1; /* [9] */
  243.       __IO uint32_t sdio1en              : 1; /* [10] */
  244.       __IO uint32_t sdio2en              : 1; /* [11] */
  245.       __IO uint32_t reserved5            : 2; /* [13:12] */
  246.       __IO uint32_t emacen               : 1; /* [14] */
  247.       __IO uint32_t emactxen             : 1; /* [15] */
  248.       __IO uint32_t emacrxen             : 1; /* [16] */
  249.       __IO uint32_t reserved6            : 11;/* [27:17] */
  250.       __IO uint32_t emacptpen            : 1; /* [28] */
  251.       __IO uint32_t reserved7            : 3; /* [31:29] */
  252.     } ahben_bit;
  253. #endif
  254.   };

  255.   /**
  256.     * @brief crm apb2en register, offset:0x18
  257.     */
  258.   union
  259.   {
  260.     __IO uint32_t apb2en;
  261.     struct
  262.     {
  263.       __IO uint32_t iomuxen              : 1; /* [0] */
  264.       __IO uint32_t reserved1            : 1; /* [1] */
  265.       __IO uint32_t gpioaen              : 1; /* [2] */
  266.       __IO uint32_t gpioben              : 1; /* [3] */
  267.       __IO uint32_t gpiocen              : 1; /* [4] */
  268.       __IO uint32_t gpioden              : 1; /* [5] */
  269.       __IO uint32_t gpioeen              : 1; /* [6] */
  270.       __IO uint32_t reserved2            : 2; /* [8:7] */
  271.       __IO uint32_t adc1en               : 1; /* [9] */
  272.       __IO uint32_t adc2en               : 1; /* [10] */
  273.       __IO uint32_t tmr1en               : 1; /* [11] */
  274.       __IO uint32_t spi1en               : 1; /* [12] */
  275.       __IO uint32_t tmr8en               : 1; /* [13] */
  276.       __IO uint32_t usart1en             : 1; /* [14] */
  277.       __IO uint32_t adc3en               : 1; /* [15] */
  278.       __IO uint32_t reserved3            : 3; /* [18:16] */
  279.       __IO uint32_t tmr9en               : 1; /* [19] */
  280.       __IO uint32_t tmr10en              : 1; /* [20] */
  281.       __IO uint32_t tmr11en              : 1; /* [21] */
  282.       __IO uint32_t accen                : 1; /* [22] */
  283.       __IO uint32_t i2c3en               : 1; /* [23] */
  284.       __IO uint32_t usart6en             : 1; /* [24] */
  285.       __IO uint32_t uart7en              : 1; /* [25] */
  286.       __IO uint32_t uart8en              : 1; /* [26] */
  287.       __IO uint32_t reserved4            : 5; /* [31:27] */
  288.     } apb2en_bit;
  289.   };

  290.   /**
  291.     * @brief crm apb1en register, offset:0x1C
  292.     */
  293.   union
  294.   {
  295.     __IO uint32_t apb1en;
  296.     struct
  297.     {
  298.       __IO uint32_t tmr2en               : 1; /* [0] */
  299.       __IO uint32_t tmr3en               : 1; /* [1] */
  300.       __IO uint32_t tmr4en               : 1; /* [2] */
  301.       __IO uint32_t tmr5en               : 1; /* [3] */
  302.       __IO uint32_t tmr6en               : 1; /* [4] */
  303.       __IO uint32_t tmr7en               : 1; /* [5] */
  304.       __IO uint32_t tmr12en              : 1; /* [6] */
  305.       __IO uint32_t tmr13en              : 1; /* [7] */
  306.       __IO uint32_t tmr14en              : 1; /* [8] */
  307.       __IO uint32_t reserved1            : 2; /* [10:9] */
  308.       __IO uint32_t wwdten               : 1; /* [11] */
  309.       __IO uint32_t reserved2            : 2; /* [13:12] */
  310.       __IO uint32_t spi2en               : 1; /* [14] */
  311.       __IO uint32_t spi3en               : 1; /* [15] */
  312.       __IO uint32_t spi4en               : 1; /* [16] */
  313.       __IO uint32_t usart2en             : 1; /* [17] */
  314.       __IO uint32_t usart3en             : 1; /* [18] */
  315.       __IO uint32_t uart4en              : 1; /* [19] */
  316.       __IO uint32_t uart5en              : 1; /* [20] */
  317.       __IO uint32_t i2c1en               : 1; /* [21] */
  318.       __IO uint32_t i2c2en               : 1; /* [22] */
  319.       __IO uint32_t usben                : 1; /* [23] */
  320.       __IO uint32_t reserved3            : 1; /* [24] */
  321.       __IO uint32_t can1en               : 1; /* [25] */
  322.       __IO uint32_t can2en               : 1; /* [26] */
  323.       __IO uint32_t bpren                : 1; /* [27] */
  324.       __IO uint32_t pwcen                : 1; /* [28] */
  325.       __IO uint32_t dacen                : 1; /* [29] */
  326.       __IO uint32_t reserved4            : 2; /* [31:30] */
  327.     } apb1en_bit;
  328.   };

  329.   /**
  330.     * @brief crm bpdc register, offset:0x20
  331.     */
  332.   union
  333.   {
  334.     __IO uint32_t bpdc;
  335.     struct
  336.     {
  337.       __IO uint32_t lexten               : 1; /* [0] */
  338.       __IO uint32_t lextstbl             : 1; /* [1] */
  339.       __IO uint32_t lextbyps             : 1; /* [2] */
  340.       __IO uint32_t reserved1            : 5; /* [7:3] */
  341.       __IO uint32_t rtcsel               : 2; /* [9:8] */
  342.       __IO uint32_t reserved2            : 5; /* [14:10] */
  343.       __IO uint32_t rtcen                : 1; /* [15] */
  344.       __IO uint32_t bpdrst               : 1; /* [16] */
  345.       __IO uint32_t reserved3            : 15;/* [31:17] */
  346.     } bpdc_bit;
  347.   };

  348.   /**
  349.     * @brief crm ctrlsts register, offset:0x24
  350.     */
  351.   union
  352.   {
  353.     __IO uint32_t ctrlsts;
  354.     struct
  355.     {
  356.       __IO uint32_t licken               : 1; /* [0] */
  357.       __IO uint32_t lickstbl             : 1; /* [1] */
  358.       __IO uint32_t reserved1            : 22;/* [23:2] */
  359.       __IO uint32_t rstfc                : 1; /* [24] */
  360.       __IO uint32_t reserved2            : 1; /* [25] */
  361.       __IO uint32_t nrstf                : 1; /* [26] */
  362.       __IO uint32_t porrstf              : 1; /* [27] */
  363.       __IO uint32_t swrstf               : 1; /* [28] */
  364.       __IO uint32_t wdtrstf              : 1; /* [29] */
  365.       __IO uint32_t wwdtrstf             : 1; /* [30] */
  366.       __IO uint32_t lprstf               : 1; /* [31] */
  367.     } ctrlsts_bit;
  368.   };

  369.   /**
  370.     * @brief crm ahbrst register, offset:0x28
  371.     */
  372.   union
  373.   {
  374.     __IO uint32_t ahbrst;
  375. #if defined (AT32F407xx)
  376.     struct
  377.     {
  378.       __IO uint32_t reserved1            : 14;/* [13:0] */
  379.       __IO uint32_t emacrst              : 1; /* [14] */
  380.       __IO uint32_t reserved2            : 17;/* [31:15] */
  381.     } ahbrst_bit;
  382. #endif
  383.   };

  384.   /**
  385.     * @brief crm reserved1 register, offset:0x2C
  386.     */
  387.   __IO uint32_t reserved1;

  388.   /**
  389.     * @brief crm misc1 register, offset:0x30
  390.     */
  391.   union
  392.   {
  393.     __IO uint32_t misc1;
  394.     struct
  395.     {
  396.       __IO uint32_t hickcal_key          : 8; /* [7:0] */
  397.       __IO uint32_t reserved1            : 8; /* [15:8] */
  398.       __IO uint32_t clkout_sel           : 1; /* [16] */
  399.       __IO uint32_t reserved2            : 7; /* [23:17] */
  400.       __IO uint32_t usbbufs              : 1; /* [24] */
  401.       __IO uint32_t hickdiv              : 1; /* [25] */
  402.       __IO uint32_t reserved3            : 2; /* [27:26] */
  403.       __IO uint32_t clkoutdiv            : 4; /* [31:28] */
  404.     } misc1_bit;
  405.   };

  406.   /**
  407.     * @brief crm reserved2 register, offset:0x4C~0x34
  408.     */
  409.   __IO uint32_t reserved2[7];

  410.   /**
  411.     * @brief crm misc2 register, offset:0x50
  412.     */
  413.   union
  414.   {
  415.     __IO uint32_t misc2;
  416.     struct
  417.     {
  418.       __IO uint32_t reserved1            : 16;/* [15:0] */
  419.       __IO uint32_t clk_to_tmr           : 1; /* [16] */
  420.       __IO uint32_t reserved2            : 15;/* [31:17] */
  421.     } misc2_bit;
  422.   };

  423.   /**
  424.     * @brief crm misc3 register, offset:0x54
  425.     */
  426.   union
  427.   {
  428.     __IO uint32_t misc3;
  429.     struct
  430.     {
  431.       __IO uint32_t reserved1            : 4; /* [3:0] */
  432.       __IO uint32_t auto_step_en         : 2; /* [5:4] */
  433.       __IO uint32_t reserved2            : 2; /* [7:6] */
  434.       __IO uint32_t hick_to_usb          : 1; /* [8] */
  435.       __IO uint32_t hick_to_sclk         : 1; /* [9] */
  436.       __IO uint32_t reserved3            : 2; /* [11:10] */
  437.       __IO uint32_t hextdiv              : 2; /* [13:12] */
  438.       __IO uint32_t reserved4            : 1; /* [14] */
  439.       __IO uint32_t emac_pps_sel         : 1; /* [15] */
  440.       __IO uint32_t reserved5            : 16;/* [31:16] */
  441.     } misc3_bit;
  442.   };

  443.   /**
  444.     * @brief crm reserved3 register, offset:0x58
  445.     */
  446.   __IO uint32_t reserved3;

  447.   /**
  448.     * @brief crm intmap register, offset:0x5C
  449.     */
  450.   union
  451.   {
  452.     __IO uint32_t intmap;
  453.     struct
  454.     {
  455.       __IO uint32_t usbintmap            : 1; /* [0] */
  456.       __IO uint32_t reserved1            : 31;/* [31:1] */
  457.     } intmap_bit;
  458.   };

  459. } crm_type;

  460. #define CRM                              ((crm_type *) CRM_BASE)

  461. typedef enum
  462. {
  463.   CRM_SCLK_HICK                          = 0x00, /*!< select high speed internal clock as sclk */
  464.   CRM_SCLK_HEXT                          = 0x01, /*!< select high speed external crystal as sclk */
  465.   CRM_SCLK_PLL                           = 0x02  /*!< select phase locking loop clock as sclk */
  466. } crm_sclk_type;

  467. void crm_reset(void)
  468. {
  469.   /* reset the crm clock configuration to the default reset state(for debug purpose) */
  470.   /* set hicken bit */
  471.   CRM->ctrl_bit.hicken = true;

  472.   /* wait hick stable */
  473.   while(CRM->ctrl_bit.hickstbl != SET);

  474.   /* hick used as system clock */
  475.   CRM->cfg_bit.sclksel = CRM_SCLK_HICK;

  476.   /* wait sclk switch status */
  477.   while(CRM->cfg_bit.sclksts != CRM_SCLK_HICK);

  478.   /* reset cfg register, include sclk switch, ahbdiv, apb1div, apb2div, adcdiv,
  479.      clkout pllrcs, pllhextdiv, pllmult, usbdiv and pllrange bits */
  480.   CRM->cfg = 0;

  481.   /* reset hexten, hextbyps, cfden and pllen bits */
  482.   CRM->ctrl &= ~(0x010D0000U);

  483.   /* reset clkout[3], usbbufs, hickdiv, clkoutdiv */
  484.   CRM->misc1 = 0;

  485.   /* disable all interrupts enable and clear pending bits  */
  486.   CRM->clkint = 0x009F0000;
  487. }

  488. /////////////////////////////////////////////////////////////////////////////////////
  489. typedef enum
  490. {
  491.   CRM_CLOCK_SOURCE_HICK                  = 0x00, /*!< high speed internal clock */
  492.   CRM_CLOCK_SOURCE_HEXT                  = 0x01, /*!< high speed external crystal */
  493.   CRM_CLOCK_SOURCE_PLL                   = 0x02, /*!< phase locking loop */
  494.   CRM_CLOCK_SOURCE_LEXT                  = 0x03, /*!< low speed external crystal */
  495.   CRM_CLOCK_SOURCE_LICK                  = 0x04  /*!< low speed internal clock */
  496. } crm_clock_source_type;
  497. typedef enum {FALSE = 0, TRUE = !FALSE} confirm_state;
  498. void crm_clock_source_enable(crm_clock_source_type source, confirm_state new_state)
  499. {
  500.   switch(source)
  501.   {
  502.     case CRM_CLOCK_SOURCE_HICK:
  503.       CRM->ctrl_bit.hicken = new_state;
  504.       break;
  505.     case CRM_CLOCK_SOURCE_HEXT:
  506.       CRM->ctrl_bit.hexten = new_state;
  507.       break;
  508.     case CRM_CLOCK_SOURCE_PLL:
  509.       CRM->ctrl_bit.pllen = new_state;
  510.       break;
  511.     case CRM_CLOCK_SOURCE_LEXT:
  512.       CRM->bpdc_bit.lexten = new_state;
  513.       break;
  514.     case CRM_CLOCK_SOURCE_LICK:
  515.       CRM->ctrlsts_bit.licken = new_state;
  516.       break;
  517.     default:
  518.       break;
  519.   }
  520. }

  521. /////////////////////////////////////////////////////////
  522. #define REG8(addr)                       *(volatile uint8_t *)(addr)
  523. #define REG16(addr)                      *(volatile uint16_t *)(addr)
  524. #define REG32(addr)                      *(volatile uint32_t *)(addr)
  525. #define CRM_PLL_STABLE_FLAG              MAKE_VALUE(0x00, 25) /*!< phase locking loop stable flag */
  526. #define HEXT_STARTUP_TIMEOUT     ((uint16_t)0x3000) /*!< time out for hext start up */
  527. #define MAKE_VALUE(reg_offset, bit_num)  (uint32_t)(((reg_offset) << 16) | (bit_num & 0x1F))
  528. #define CRM_HEXT_STABLE_FLAG             MAKE_VALUE(0x00, 17) /*!< high speed external crystal stable flag */
  529. #define PERIPH_REG(periph_base, value)   REG32((periph_base + (value >> 16)))
  530. #define PERIPH_REG_BIT(value)            (0x1U << (value & 0x1F))
  531. #define CRM_REG_BIT(value)               PERIPH_REG_BIT(value)
  532. #define CRM_REG(value)                   PERIPH_REG(CRM_BASE, value)
  533. bool crm_flag_get(uint32_t flag)
  534. {
  535.   bool status = RESET;
  536.   if((CRM_REG(flag) & CRM_REG_BIT(flag)) != CRM_REG_BIT(flag))
  537.   {
  538.     status = RESET;
  539.   }
  540.   else
  541.   {
  542.     status = SET;
  543.   }
  544.   return status;
  545. }

  546. bool crm_hext_stable_wait(void)
  547. {
  548.   uint32_t stable_cnt = 0;
  549.   bool status = ERROR;

  550.   while((crm_flag_get(CRM_HEXT_STABLE_FLAG) != SET) && (stable_cnt < HEXT_STARTUP_TIMEOUT))
  551.   {
  552.     stable_cnt ++;
  553.   }

  554.   if(crm_flag_get(CRM_HEXT_STABLE_FLAG) != SET)
  555.   {
  556.     status = ERROR;
  557.   }
  558.   else
  559.   {
  560.     status = SUCCESS;
  561.   }

  562.   return status;
  563. }
  564. /////////////////////////////////////////////////////

  565. typedef enum
  566. {
  567.   CRM_PLL_SOURCE_HICK                    = 0x00, /*!< high speed internal clock as pll reference clock source */
  568.   CRM_PLL_SOURCE_HEXT                    = 0x01, /*!< high speed external crystal as pll reference clock source */
  569.   CRM_PLL_SOURCE_HEXT_DIV                = 0x02  /*!< high speed external crystal div as pll reference clock source */
  570. } crm_pll_clock_source_type;
  571. typedef enum
  572. {
  573.   CRM_PLL_MULT_2                         = 0,  /*!< pll multiplication factor 2 */
  574.   CRM_PLL_MULT_3                         = 1,  /*!< pll multiplication factor 3 */
  575.   CRM_PLL_MULT_4                         = 2,  /*!< pll multiplication factor 4 */
  576.   CRM_PLL_MULT_5                         = 3,  /*!< pll multiplication factor 5 */
  577.   CRM_PLL_MULT_6                         = 4,  /*!< pll multiplication factor 6 */
  578.   CRM_PLL_MULT_7                         = 5,  /*!< pll multiplication factor 7 */
  579.   CRM_PLL_MULT_8                         = 6,  /*!< pll multiplication factor 8 */
  580.   CRM_PLL_MULT_9                         = 7,  /*!< pll multiplication factor 9 */
  581.   CRM_PLL_MULT_10                        = 8,  /*!< pll multiplication factor 10 */
  582.   CRM_PLL_MULT_11                        = 9,  /*!< pll multiplication factor 11 */
  583.   CRM_PLL_MULT_12                        = 10, /*!< pll multiplication factor 12 */
  584.   CRM_PLL_MULT_13                        = 11, /*!< pll multiplication factor 13 */
  585.   CRM_PLL_MULT_14                        = 12, /*!< pll multiplication factor 14 */
  586.   CRM_PLL_MULT_15                        = 13, /*!< pll multiplication factor 15 */
  587.   CRM_PLL_MULT_16                        = 14, /*!< pll multiplication factor 16 */
  588.   CRM_PLL_MULT_17                        = 15, /*!< pll multiplication factor 17 */
  589.   CRM_PLL_MULT_18                        = 16, /*!< pll multiplication factor 18 */
  590.   CRM_PLL_MULT_19                        = 17, /*!< pll multiplication factor 19 */
  591.   CRM_PLL_MULT_20                        = 18, /*!< pll multiplication factor 20 */
  592.   CRM_PLL_MULT_21                        = 19, /*!< pll multiplication factor 21 */
  593.   CRM_PLL_MULT_22                        = 20, /*!< pll multiplication factor 22 */
  594.   CRM_PLL_MULT_23                        = 21, /*!< pll multiplication factor 23 */
  595.   CRM_PLL_MULT_24                        = 22, /*!< pll multiplication factor 24 */
  596.   CRM_PLL_MULT_25                        = 23, /*!< pll multiplication factor 25 */
  597.   CRM_PLL_MULT_26                        = 24, /*!< pll multiplication factor 26 */
  598.   CRM_PLL_MULT_27                        = 25, /*!< pll multiplication factor 27 */
  599.   CRM_PLL_MULT_28                        = 26, /*!< pll multiplication factor 28 */
  600.   CRM_PLL_MULT_29                        = 27, /*!< pll multiplication factor 29 */
  601.   CRM_PLL_MULT_30                        = 28, /*!< pll multiplication factor 30 */
  602.   CRM_PLL_MULT_31                        = 29, /*!< pll multiplication factor 31 */
  603.   CRM_PLL_MULT_32                        = 30, /*!< pll multiplication factor 32 */
  604.   CRM_PLL_MULT_33                        = 31, /*!< pll multiplication factor 33 */
  605.   CRM_PLL_MULT_34                        = 32, /*!< pll multiplication factor 34 */
  606.   CRM_PLL_MULT_35                        = 33, /*!< pll multiplication factor 35 */
  607.   CRM_PLL_MULT_36                        = 34, /*!< pll multiplication factor 36 */
  608.   CRM_PLL_MULT_37                        = 35, /*!< pll multiplication factor 37 */
  609.   CRM_PLL_MULT_38                        = 36, /*!< pll multiplication factor 38 */
  610.   CRM_PLL_MULT_39                        = 37, /*!< pll multiplication factor 39 */
  611.   CRM_PLL_MULT_40                        = 38, /*!< pll multiplication factor 40 */
  612.   CRM_PLL_MULT_41                        = 39, /*!< pll multiplication factor 41 */
  613.   CRM_PLL_MULT_42                        = 40, /*!< pll multiplication factor 42 */
  614.   CRM_PLL_MULT_43                        = 41, /*!< pll multiplication factor 43 */
  615.   CRM_PLL_MULT_44                        = 42, /*!< pll multiplication factor 44 */
  616.   CRM_PLL_MULT_45                        = 43, /*!< pll multiplication factor 45 */
  617.   CRM_PLL_MULT_46                        = 44, /*!< pll multiplication factor 46 */
  618.   CRM_PLL_MULT_47                        = 45, /*!< pll multiplication factor 47 */
  619.   CRM_PLL_MULT_48                        = 46, /*!< pll multiplication factor 48 */
  620.   CRM_PLL_MULT_49                        = 47, /*!< pll multiplication factor 49 */
  621.   CRM_PLL_MULT_50                        = 48, /*!< pll multiplication factor 50 */
  622.   CRM_PLL_MULT_51                        = 49, /*!< pll multiplication factor 51 */
  623.   CRM_PLL_MULT_52                        = 50, /*!< pll multiplication factor 52 */
  624.   CRM_PLL_MULT_53                        = 51, /*!< pll multiplication factor 53 */
  625.   CRM_PLL_MULT_54                        = 52, /*!< pll multiplication factor 54 */
  626.   CRM_PLL_MULT_55                        = 53, /*!< pll multiplication factor 55 */
  627.   CRM_PLL_MULT_56                        = 54, /*!< pll multiplication factor 56 */
  628.   CRM_PLL_MULT_57                        = 55, /*!< pll multiplication factor 57 */
  629.   CRM_PLL_MULT_58                        = 56, /*!< pll multiplication factor 58 */
  630.   CRM_PLL_MULT_59                        = 57, /*!< pll multiplication factor 59 */
  631.   CRM_PLL_MULT_60                        = 58, /*!< pll multiplication factor 60 */
  632.   CRM_PLL_MULT_61                        = 59, /*!< pll multiplication factor 61 */
  633.   CRM_PLL_MULT_62                        = 60, /*!< pll multiplication factor 62 */
  634.   CRM_PLL_MULT_63                        = 61, /*!< pll multiplication factor 63 */
  635.   CRM_PLL_MULT_64                        = 62  /*!< pll multiplication factor 64 */
  636. } crm_pll_mult_type;

  637. typedef enum
  638. {
  639.   CRM_PLL_OUTPUT_RANGE_LE72MHZ           = 0x00, /*!< pll clock output range less than or equal to 72mhz */
  640.   CRM_PLL_OUTPUT_RANGE_GT72MHZ           = 0x01  /*!< pll clock output range greater than 72mhz */
  641. } crm_pll_output_range_type;


  642. void crm_pll_config(crm_pll_clock_source_type clock_source, crm_pll_mult_type mult_value, crm_pll_output_range_type pll_range)
  643. {
  644.   /* config pll clock source */
  645.   if(clock_source == CRM_PLL_SOURCE_HICK)
  646.   {
  647.     CRM->cfg_bit.pllrcs = FALSE;
  648.   }
  649.   else
  650.   {
  651.     CRM->cfg_bit.pllrcs = TRUE;
  652.     if(CRM_PLL_SOURCE_HEXT == clock_source)
  653.     {
  654.       CRM->cfg_bit.pllhextdiv = FALSE;
  655.     }
  656.     else
  657.     {
  658.       CRM->cfg_bit.pllhextdiv = TRUE;
  659.     }
  660.   }

  661.   /* config pll multiplication factor */
  662.   CRM->cfg_bit.pllmult_l = (mult_value & 0x0F);
  663.   CRM->cfg_bit.pllmult_h = ((mult_value & 0x30) >> 4);

  664.   /* config pll output range */
  665.   CRM->cfg_bit.pllrange = pll_range;
  666. }

  667. //////////////////////////////////////////////////////
  668. typedef enum
  669. {
  670.   CRM_HEXT_DIV_2                         = 0x00, /*!< high speed external crystal div2 */
  671.   CRM_HEXT_DIV_3                         = 0x01, /*!< high speed external crystal div3 */
  672.   CRM_HEXT_DIV_4                         = 0x02, /*!< high speed external crystal div4 */
  673.   CRM_HEXT_DIV_5                         = 0x03  /*!< high speed external crystal div5 */
  674. } crm_hext_div_type;

  675. void crm_hext_clock_div_set(crm_hext_div_type value)
  676. {
  677.   CRM->misc3_bit.hextdiv = value;
  678. }

  679. ////////////////////////////////////////
  680. //
  681. //bool crm_flag_get(uint32_t flag)
  682. //{
  683. //  bool status = RESET;
  684. //  if((CRM_REG(flag) & CRM_REG_BIT(flag)) != CRM_REG_BIT(flag))
  685. //  {
  686. //    status = RESET;
  687. //  }
  688. //  else
  689. //  {
  690. //    status = SET;
  691. //  }
  692. //  return status;
  693. //}
  694. ///////////////////////////////////////////
  695. typedef enum
  696. {
  697.   CRM_AHB_DIV_1                          = 0x00, /*!< sclk div1 to ahbclk */
  698.   CRM_AHB_DIV_2                          = 0x08, /*!< sclk div2 to ahbclk */
  699.   CRM_AHB_DIV_4                          = 0x09, /*!< sclk div4 to ahbclk */
  700.   CRM_AHB_DIV_8                          = 0x0A, /*!< sclk div8 to ahbclk */
  701.   CRM_AHB_DIV_16                         = 0x0B, /*!< sclk div16 to ahbclk */
  702.   CRM_AHB_DIV_64                         = 0x0C, /*!< sclk div64 to ahbclk */
  703.   CRM_AHB_DIV_128                        = 0x0D, /*!< sclk div128 to ahbclk */
  704.   CRM_AHB_DIV_256                        = 0x0E, /*!< sclk div256 to ahbclk */
  705.   CRM_AHB_DIV_512                        = 0x0F  /*!< sclk div512 to ahbclk */
  706. } crm_ahb_div_type;


  707. void crm_ahb_div_set(crm_ahb_div_type value)
  708. {
  709.   CRM->cfg_bit.ahbdiv = value;
  710. }
  711. //////////////////////////
  712. typedef enum
  713. {
  714.   CRM_APB2_DIV_1                         = 0x00, /*!< ahbclk div1 to apb2clk */
  715.   CRM_APB2_DIV_2                         = 0x04, /*!< ahbclk div2 to apb2clk */
  716.   CRM_APB2_DIV_4                         = 0x05, /*!< ahbclk div4 to apb2clk */
  717.   CRM_APB2_DIV_8                         = 0x06, /*!< ahbclk div8 to apb2clk */
  718.   CRM_APB2_DIV_16                        = 0x07  /*!< ahbclk div16 to apb2clk */
  719. } crm_apb2_div_type;

  720. void crm_apb2_div_set(crm_apb2_div_type value)
  721. {
  722.   CRM->cfg_bit.apb2div = value;
  723. }
  724. ///////////////////////////////////////
  725. typedef enum
  726. {
  727.   CRM_APB1_DIV_1                         = 0x00, /*!< ahbclk div1 to apb1clk */
  728.   CRM_APB1_DIV_2                         = 0x04, /*!< ahbclk div2 to apb1clk */
  729.   CRM_APB1_DIV_4                         = 0x05, /*!< ahbclk div4 to apb1clk */
  730.   CRM_APB1_DIV_8                         = 0x06, /*!< ahbclk div8 to apb1clk */
  731.   CRM_APB1_DIV_16                        = 0x07  /*!< ahbclk div16 to apb1clk */
  732. } crm_apb1_div_type;

  733. void crm_apb1_div_set(crm_apb1_div_type value)
  734. {
  735.   CRM->cfg_bit.apb1div = value;
  736. }
  737. ////////////////////////////////////////
  738. typedef enum
  739. {
  740.   CRM_AUTO_STEP_MODE_DISABLE             = 0x00, /*!< disable auto step mode */
  741.   CRM_AUTO_STEP_MODE_ENABLE              = 0x03  /*!< enable auto step mode */
  742. } crm_auto_step_mode_type;
  743. void crm_auto_step_mode_enable(bool new_state)
  744. {
  745.   if(new_state == TRUE)
  746.     CRM->misc3_bit.auto_step_en = CRM_AUTO_STEP_MODE_ENABLE;
  747.   else
  748.     CRM->misc3_bit.auto_step_en = CRM_AUTO_STEP_MODE_DISABLE;
  749. }
  750. ////////////////////////////////////////////////
  751. void crm_sysclk_switch(crm_sclk_type value)
  752. {
  753.   CRM->cfg_bit.sclksel = value;
  754. }
  755. ///////////////////////////////////////////
  756. crm_sclk_type crm_sysclk_switch_status_get(void)
  757. {
  758.   return (crm_sclk_type)CRM->cfg_bit.sclksts;
  759. }
  760. /////////////////////////////////////////
  761. #define HICK_VALUE               ((uint32_t)8000000) /*!< value of the high speed internal clock in hz */
  762. #define HEXT_VALUE               ((uint32_t)N_ConfigSys_CrystalOsc) /*!< value of the high speed exernal crystal in hz */
  763. unsigned int system_core_clock           = HICK_VALUE; /*!< system clock frequency (core clock) */
  764. void system_core_clock_update(void)
  765. {
  766.   uint32_t hext_prediv = 0, pll_mult = 0, pll_mult_h = 0, pll_clock_source = 0, temp = 0, div_value = 0;
  767.   crm_sclk_type sclk_source;

  768.   static const uint8_t sys_ahb_div_table[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};

  769.   /* get sclk source */
  770.   sclk_source = crm_sysclk_switch_status_get();

  771.   switch(sclk_source)
  772.   {
  773.     case CRM_SCLK_HICK:
  774.       if(((CRM->misc3_bit.hick_to_sclk) != RESET) && ((CRM->misc1_bit.hickdiv) != RESET))
  775.         system_core_clock = HICK_VALUE * 6;
  776.       else
  777.         system_core_clock = HICK_VALUE;
  778.       break;
  779.     case CRM_SCLK_HEXT:
  780.       system_core_clock = HEXT_VALUE;
  781.       break;
  782.     case CRM_SCLK_PLL:
  783.       pll_clock_source = CRM->cfg_bit.pllrcs;
  784.       {
  785.         /* get multiplication factor */
  786.         pll_mult = CRM->cfg_bit.pllmult_l;
  787.         pll_mult_h = CRM->cfg_bit.pllmult_h;
  788.         /* process high bits */
  789.         if((pll_mult_h != 0U) || (pll_mult == 15U)){
  790.             pll_mult += ((16U * pll_mult_h) + 1U);
  791.         }
  792.         else
  793.         {
  794.             pll_mult += 2U;
  795.         }

  796.         if (pll_clock_source == 0x00)
  797.         {
  798.           /* hick divided by 2 selected as pll clock entry */
  799.           system_core_clock = (HICK_VALUE >> 1) * pll_mult;
  800.         }
  801.         else
  802.         {
  803.           /* hext selected as pll clock entry */
  804.           if (CRM->cfg_bit.pllhextdiv != RESET)
  805.           {
  806.             hext_prediv = CRM->misc3_bit.hextdiv;

  807.             /* hext clock divided by 2 */
  808.             system_core_clock = (HEXT_VALUE / (hext_prediv + 2)) * pll_mult;
  809.           }
  810.           else
  811.           {
  812.             system_core_clock = HEXT_VALUE * pll_mult;
  813.           }
  814.         }
  815.       }
  816.       break;
  817.     default:
  818.       system_core_clock = HICK_VALUE;
  819.       break;
  820.   }

  821.   /* compute sclk, ahbclk frequency */
  822.   /* get ahb division */
  823.   temp = CRM->cfg_bit.ahbdiv;
  824.   div_value = sys_ahb_div_table[temp];
  825.   /* ahbclk frequency */
  826.   system_core_clock = system_core_clock >> div_value;
  827. }
  828. //////////////////////////////////////
  829. //设置时钟,有错误最大只能到17倍频
  830. void M_At32F40X_SystemClock(uint32_t  RCC_PLLMul_x )
  831. {
  832. /* reset crm */
  833.     crm_pll_mult_type T_bp;
  834.     //处理倍频
  835.     switch(RCC_PLLMul_x)
  836.     {
  837.         case 2:
  838.             T_bp = CRM_PLL_MULT_2;
  839.         break;
  840.         case 3:
  841.             T_bp = CRM_PLL_MULT_3;
  842.         break;
  843.         case 4:
  844.             T_bp = CRM_PLL_MULT_4;
  845.         break;
  846.         case 5:
  847.             T_bp = CRM_PLL_MULT_5;
  848.         break;
  849.         case 6:
  850.             T_bp = CRM_PLL_MULT_6;
  851.         break;
  852.         case 7:
  853.             T_bp = CRM_PLL_MULT_7;
  854.         break;
  855.         case 8:
  856.             T_bp = CRM_PLL_MULT_8;
  857.         break;
  858.         case 9:
  859.             T_bp = CRM_PLL_MULT_9;
  860.         break;
  861.         case 10:
  862.             T_bp = CRM_PLL_MULT_10;
  863.         break;
  864.         case 11:
  865.             T_bp = CRM_PLL_MULT_11;
  866.         break;
  867.         case 12:
  868.             T_bp = CRM_PLL_MULT_12;
  869.         break;
  870.         case 13:
  871.             T_bp = CRM_PLL_MULT_13;
  872.         break;
  873.         case 14:
  874.             T_bp = CRM_PLL_MULT_14;
  875.         break;
  876.         case 15:
  877.             T_bp = CRM_PLL_MULT_15;
  878.         break;
  879.         case 16:
  880.             T_bp = CRM_PLL_MULT_16;
  881.         break;
  882.         case 17:
  883.             T_bp = CRM_PLL_MULT_17;
  884.         break;
  885.         case 18:
  886.             T_bp = CRM_PLL_MULT_18;
  887.         break;
  888.         case 19:
  889.             T_bp = CRM_PLL_MULT_19;
  890.         break;
  891.         case 20:
  892.             T_bp = CRM_PLL_MULT_20;
  893.         break;
  894.         case 21:
  895.             T_bp = CRM_PLL_MULT_21;
  896.         break;
  897.         case 22:
  898.             T_bp = CRM_PLL_MULT_22;
  899.         break;
  900.         case 23:
  901.             T_bp = CRM_PLL_MULT_23;
  902.         break;
  903.         case 24:
  904.             T_bp = CRM_PLL_MULT_24;
  905.         break;
  906.         case 25:
  907.             T_bp = CRM_PLL_MULT_25;
  908.         break;
  909.         case 26:
  910.             T_bp = CRM_PLL_MULT_26;
  911.         break;
  912.         case 27:
  913.             T_bp = CRM_PLL_MULT_27;
  914.         break;
  915.         case 28:
  916.             T_bp = CRM_PLL_MULT_28;
  917.         break;
  918.         case 29:
  919.             T_bp = CRM_PLL_MULT_29;
  920.         break;
  921.         case 30:
  922.             T_bp = CRM_PLL_MULT_30;
  923.         break;
  924.         case 31:
  925.             T_bp = CRM_PLL_MULT_31;
  926.         break;
  927.         case 32:
  928.             T_bp = CRM_PLL_MULT_32;
  929.         break;
  930.         case 33:
  931.             T_bp = CRM_PLL_MULT_33;
  932.         break;
  933.         case 34:
  934.             T_bp = CRM_PLL_MULT_34;
  935.         break;
  936.         case 35:
  937.             T_bp = CRM_PLL_MULT_35;
  938.         break;
  939.         case 36:
  940.             T_bp = CRM_PLL_MULT_36;
  941.         break;
  942.         case 37:
  943.             T_bp = CRM_PLL_MULT_37;
  944.         break;
  945.         case 38:
  946.             T_bp = CRM_PLL_MULT_38;
  947.         break;
  948.         case 39:
  949.             T_bp = CRM_PLL_MULT_39;
  950.         break;   
  951.         case 40:
  952.             T_bp = CRM_PLL_MULT_40;
  953.         break;
  954.         case 41:
  955.             T_bp = CRM_PLL_MULT_41;
  956.         break;
  957.         case 42:
  958.             T_bp = CRM_PLL_MULT_42;
  959.         break;
  960.         case 43:
  961.             T_bp = CRM_PLL_MULT_43;
  962.         break;
  963.         case 44:
  964.             T_bp = CRM_PLL_MULT_44;
  965.         break;
  966.         case 45:
  967.             T_bp = CRM_PLL_MULT_45;
  968.         break;
  969.         case 46:
  970.             T_bp = CRM_PLL_MULT_46;
  971.         break;
  972.         case 47:
  973.             T_bp = CRM_PLL_MULT_47;
  974.         break;
  975.         case 48:
  976.             T_bp = CRM_PLL_MULT_48;
  977.         break;
  978.         case 49:
  979.             T_bp = CRM_PLL_MULT_49;
  980.         break;
  981.         case 50:
  982.             T_bp = CRM_PLL_MULT_50;
  983.         break;
  984.         case 51:
  985.             T_bp = CRM_PLL_MULT_51;
  986.         break;
  987.         case 52:
  988.             T_bp = CRM_PLL_MULT_52;
  989.         break;
  990.         case 53:
  991.             T_bp = CRM_PLL_MULT_53;
  992.         break;
  993.         case 54:
  994.             T_bp = CRM_PLL_MULT_54;
  995.         break;
  996.         case 55:
  997.             T_bp = CRM_PLL_MULT_55;
  998.         break;
  999.         case 56:
  1000.             T_bp = CRM_PLL_MULT_56;
  1001.         break;
  1002.         case 57:
  1003.             T_bp = CRM_PLL_MULT_57;
  1004.         break;
  1005.         case 58:
  1006.             T_bp = CRM_PLL_MULT_58;
  1007.         break;
  1008.         case 59:
  1009.             T_bp = CRM_PLL_MULT_59;
  1010.         break;
  1011.         case 60:
  1012.             T_bp = CRM_PLL_MULT_60;
  1013.         break;
  1014.         case 61:
  1015.             T_bp = CRM_PLL_MULT_61;
  1016.         break;
  1017.         case 62:
  1018.             T_bp = CRM_PLL_MULT_62;
  1019.         break;
  1020.         case 63:
  1021.             T_bp = CRM_PLL_MULT_63;
  1022.         break;
  1023.         case 64:
  1024.             T_bp = CRM_PLL_MULT_64;
  1025.         break;
  1026.         default:
  1027.             T_bp = CRM_PLL_MULT_4;
  1028.         break;         
  1029.     }  
  1030.   /* reset crm */
  1031.   crm_reset();

  1032.   /* enable hext */
  1033.   crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);

  1034.    /* wait till hext is ready */
  1035.   while(crm_hext_stable_wait() == ERROR)
  1036.   {
  1037.   }


  1038.   /* config pll clock resource */
  1039.   crm_pll_config(CRM_PLL_SOURCE_HEXT, T_bp, CRM_PLL_OUTPUT_RANGE_GT72MHZ);

  1040.   /* enable pll */
  1041.   crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);

  1042.   /* wait till pll is ready */
  1043.   while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
  1044.   {
  1045.   }


  1046.   /* config ahbclk */
  1047.   crm_ahb_div_set(CRM_AHB_DIV_1);

  1048.   /* config apb2clk */
  1049.   crm_apb2_div_set(CRM_APB2_DIV_2);

  1050.   /* config apb1clk */
  1051.   crm_apb1_div_set(CRM_APB1_DIV_2);

  1052.   /* enable auto step mode */
  1053.   crm_auto_step_mode_enable(TRUE);


  1054.   /* select pll as system clock source */
  1055.   crm_sysclk_switch(CRM_SCLK_PLL);

  1056.   /* wait till pll is used as system clock source */
  1057.   while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
  1058.   {
  1059.   }


  1060.   /* disable auto step mode */
  1061.   crm_auto_step_mode_enable(FALSE);


  1062.   /* update system_core_clock global variable */
  1063.   system_core_clock_update();
  1064. }
复制代码

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入42汤圆

发表于 2022-5-24 22:07:29 | 显示全部楼层
我用的403a,没有研究过怎么把stm32的移植过来,直接用的at给的例子,跑240M还可以。文档里的第10页有一个pll说明,你可以参考一下

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2022-5-24 22:11:16 | 显示全部楼层
我是一个大白菜 发表于 2022-5-24 22:07
我用的403a,没有研究过怎么把stm32的移植过来,直接用的at给的例子,跑240M还可以。文档里的第10页有一个p ...
(引用自2楼)

谢谢,我试试,现在设置代码是用他的哪个自动生成时钟代码的软件自动生成,我看了一下生成的代码和网上的代码一样

出0入0汤圆

 楼主| 发表于 2022-5-24 22:23:58 | 显示全部楼层
我是一个大白菜 发表于 2022-5-24 22:07
我用的403a,没有研究过怎么把stm32的移植过来,直接用的at给的例子,跑240M还可以。文档里的第10页有一个p ...
(引用自2楼)


system_at32f4xx.c这个文件哪里下载,我官网下载的库文件里面的system_at32f403a_407.c文件,内容和网上看到的截图里的部分代码,完全不同,里面也没有pdf说的static void SetSysClockTo168()函数

出0入42汤圆

发表于 2022-5-25 00:36:09 | 显示全部楼层
cnxh 发表于 2022-5-24 22:23
system_at32f4xx.c这个文件哪里下载,我官网下载的库文件里面的system_at32f403a_407.c文件,内容和网上看 ...
(引用自4楼)

官网升级了一次库,2.xx之后我觉得不好用,原来1.xx的类似stm的库,我比较顺手,我上传你看看,是不是你想要的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2022-5-25 07:49:28 | 显示全部楼层
我是一个大白菜 发表于 2022-5-25 00:36
官网升级了一次库,2.xx之后我觉得不好用,原来1.xx的类似stm的库,我比较顺手,我上传你看看,是不是你 ...
(引用自5楼)

太谢谢了

出0入0汤圆

发表于 2022-5-25 08:39:38 | 显示全部楼层
我是一个大白菜 发表于 2022-5-24 22:07
我用的403a,没有研究过怎么把stm32的移植过来,直接用的at给的例子,跑240M还可以。文档里的第10页有一个p ...
(引用自2楼)

原来很多人都在用,我也再用421同最近437测试替换407

出0入42汤圆

发表于 2022-5-25 09:08:42 | 显示全部楼层
mypear 发表于 2022-5-25 08:39
原来很多人都在用,我也再用421同最近437测试替换407
(引用自7楼)

软件库感觉还是支持的可以的,403a的dma灵活配置感觉比较好用,各个外设基本上都用上dma了,速度快很多

出0入0汤圆

 楼主| 发表于 2022-5-26 19:11:38 | 显示全部楼层
本帖最后由 cnxh 于 2022-5-26 19:21 编辑

下面的代码把72M的改成192M,参照pdf的说明,怎么频率还是不对和老的一样
73 85 98行处加入pdf的说的代码,试过RCC_CFG_PLLMULT17  RCC_CFG_PLLMULT20 ,都不正确,其它的pdf上没找到定义,没试
  1. static void SetSysClockTo192(void)
  2. {

  3.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  4.   
  5.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  6.   /* Enable HSE */   
  7.     RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  8.     RCC->CR |= ((uint32_t)RCC_CR_HSEBYP);       //外置晶振
  9.   /* Wait till HSE is ready and if Time out is reached exit */
  10.   do
  11.   {
  12.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  13.     StartUpCounter++;  
  14.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  15.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  16.   {
  17.     HSEStatus = (uint32_t)0x01;
  18.   }
  19.   else
  20.   {
  21.     HSEStatus = (uint32_t)0x00;
  22.   }  
  23. //如果进入HSEStatus = (uint32_t)0x01;则外置有源晶振成功起振,如果进入
  24. //HSEStatus = (uint32_t)0x00;则未成功起振,然后会自动切换成HSI。
  25.   if (HSEStatus == (uint32_t)0x01)
  26.   {
  27.     /* Enable Prefetch Buffer */
  28.     FLASH->ACR |= FLASH_ACR_PRFTBE;

  29.     /* Flash 2 wait state */
  30.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  31.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;   


  32.     /* HCLK = SYSCLK */
  33.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  34.       
  35.     /* PCLK2 = HCLK */
  36.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  37.    
  38.     /* PCLK1 = HCLK */
  39.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;

  40. #ifdef STM32F10X_CL
  41.         #error ("STM32F10X_CL")
  42. //    /* Configure PLLs ------------------------------------------------------*/
  43. //    /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  44. //    /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  45. //        
  46. //    RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  47. //                              RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  48. //    RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  49. //                             RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  50. //  
  51. //    /* Enable PLL2 */
  52. //    RCC->CR |= RCC_CR_PLL2ON;
  53. //    /* Wait till PLL2 is ready */
  54. //    while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  55. //    {
  56. //    }
  57. //   
  58. //   
  59. //    /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
  60. //    RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  61. //    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  62. //                            RCC_CFGR_PLLMULL9);
  63. #else   
  64.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |RCC_CFGR_PLLMULL));
  65.     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
  66.     //    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);//stm32的去掉
  67.     //---------------------------------AT32F403A加入
  68.     //192M
  69.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFG_PLLMULT20 |RCC_CFG_PLLRANGE_GT72MHZ);
  70. #endif /* STM32F10X_CL */

  71.     /* Enable PLL */
  72.     RCC->CR |= RCC_CR_PLLON;

  73.     /* Wait till PLL is ready */
  74.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  75.     {
  76.     }
  77.     //---------------------------------AT32F403A加入
  78.     *((unsigned int *)0x40021054) |= (0x30);// 开启自动滑顺频率切换功能
  79.     //#endif
  80.     /* Select PLL as system clock source */
  81.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  82.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  83.     /* Wait till PLL is used as system clock source */
  84.     //#if N_ConfigSys_Debug  != true           //是否整个程序调试状态
  85.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  86.     {
  87.     }
  88.     //#endif
  89.     //---------------------------------AT32F403A加入
  90.     //此时不需再等待200us
  91.     *((unsigned int *)0x40021054) &=~ (0x30); //关闭自动滑顺频率切换功能
  92.   }
  93.   else
  94.   { /* If HSE fails to start-up, the application will have wrong clock
  95.          configuration. User can add here some code to deal with this error */
  96.   }
  97. }
复制代码

出0入0汤圆

 楼主| 发表于 2022-5-26 21:59:52 | 显示全部楼层
搞不定,没办法,只能把设置时钟的做成库然后加入到文件中调用

出0入42汤圆

发表于 2022-5-26 22:13:17 | 显示全部楼层
cnxh 发表于 2022-5-26 19:11
下面的代码把72M的改成192M,参照pdf的说明,怎么频率还是不对和老的一样
73 85 98行处加入pdf的说的代码,试 ...
(引用自9楼)

你的晶振怎么配置的,在文件 at32f4xx.h 中,下面是我的

  1. /**
  2. * @brief In the following line adjust the value of External High Speed oscillator (HSE)
  3.    used in your application

  4.    Tip: To avoid modifying this file each time you need to use different HSE, you
  5.         can define the HSE value in your toolchain compiler preprocessor.
  6.   */
  7. #if !defined  HSE_VALUE
  8. //#define HSE_VALUE               ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
  9. #define HSE_VALUE               ((uint32_t)16000000) /*!< Value of the External oscillator in Hz */
  10. #endif /* HSE_VALUE */
复制代码

出0入0汤圆

 楼主| 发表于 2022-5-27 07:19:16 | 显示全部楼层
我是一个大白菜 发表于 2022-5-26 22:13
你的晶振怎么配置的,在文件 at32f4xx.h 中,下面是我的
(引用自11楼)

谢谢,默认8M

出0入0汤圆

 楼主| 发表于 2022-5-27 07:22:49 | 显示全部楼层
本帖最后由 cnxh 于 2022-5-27 07:26 编辑

编译成库,测试工程用c,可以了,现在有个问题,我的工程是c++的,加入调用就会出错,原来测试的c工程可以,把他改成c++,也一样会出错,应该是找不到这个库里的函数,调用时加入extern "C" {}也一样,库函数编译时改成c++也一样
提示 Error[Pe020]: identifier "system_clock_config" is undefined N:\Project\CP1C36\App\1000\App\main.c 327








可以了,加入了声明extern void  system_clock_config(void);就可以了

出0入0汤圆

 楼主| 发表于 2022-5-27 07:59:36 | 显示全部楼层
上传给也有同样需求的人,已弄好的库文件,加入到工程就可以了,然后主程序调用
extern "C" {
extern void  system_clock_config(void);
void gg(void)
{
    system_clock_config();
}
}


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2023-3-2 20:05:52 | 显示全部楼层
cnxh 发表于 2022-5-27 07:59
上传给也有同样需求的人,已弄好的库文件,加入到工程就可以了,然后主程序调用
extern "C" {
extern void  s ...
(引用自14楼)

请教一下,碰到同样的问题,下载您这个文件打开是.a文件,貌似打不开,请指教谢谢!

出0入0汤圆

 楼主| 发表于 2023-3-3 19:56:03 | 显示全部楼层
kingfa021 发表于 2023-3-2 20:05
请教一下,碰到同样的问题,下载您这个文件打开是.a文件,貌似打不开,请指教谢谢! ...
(引用自15楼)

不好意思,我自己都忘了怎么弄,现在还是用stm的72m,因为实际运行起来比72m还慢

出10入95汤圆

发表于 2023-3-5 06:52:01 来自手机 | 显示全部楼层
请教一下,这些仿制的,防破解怎样?

出0入0汤圆

发表于 2023-3-5 11:06:53 | 显示全部楼层
这个改动寄存器即可,雅特力把ST的PLL设置寄存器里,没用到的30:29位用起来了。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-27 03:39

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表