GD32F130G6U6这MCU一直重启,请论坛大佬帮看下

[复制链接]
1784|10
cornrn 发表于 2025-9-16 15:51 | 显示全部楼层 |阅读模式
MCU, , , 论坛,
  1. /*!
  2.     \file  system_gd32f1x0.c
  3.     \brief CMSIS Cortex-M3 Device Peripheral Access Layer Source File for
  4.            GD32F1x0 Device Series
  5. */

  6. /* Copyright (c) 2012 ARM LIMITED

  7.    All rights reserved.
  8.    Redistribution and use in source and binary forms, with or without
  9.    modification, are permitted provided that the following conditions are met:
  10.    - Redistributions of source code must retain the above copyright
  11.      notice, this list of conditions and the following disclaimer.
  12.    - Redistributions in binary form must reproduce the above copyright
  13.      notice, this list of conditions and the following disclaimer in the
  14.      documentation and/or other materials provided with the distribution.
  15.    - Neither the name of ARM nor the names of its contributors may be used
  16.      to endorse or promote products derived from this software without
  17.      specific prior written permission.
  18.    *
  19.    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20.    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.    ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  23.    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24.    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25.    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26.    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27.    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28.    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.    POSSIBILITY OF SUCH DAMAGE.
  30.    ---------------------------------------------------------------------------*/

  31. /* This file refers the CMSIS standard, some adjustments are made according to GigaDevice chips */

  32. #include "gd32f1x0.h"

  33. /* system frequency define */
  34. #define __IRC8M           (IRC8M_VALUE)            /* internal 8 MHz RC oscillator frequency */
  35. #define __HXTAL           (HXTAL_VALUE)            /* high speed crystal oscillator frequency */
  36. #define __SYS_OSC_CLK     (__IRC8M)                /* main oscillator frequency */

  37. /* select a system clock by uncommenting the following line */
  38. //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL)
  39. //#define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M)
  40. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000)
  41. #define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000)

  42. #define SEL_IRC8M       0x00
  43. #define SEL_HXTAL       0x01
  44. #define SEL_PLL         0x02

  45. /* set the system clock frequency and declare the system clock configuration function */
  46. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  47. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL;
  48. static void system_clock_8m_hxtal(void);
  49. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  50. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
  51. static void system_clock_72m_hxtal(void);
  52. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  53. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2;
  54. static void system_clock_72m_irc8m(void);
  55. #else
  56. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M;
  57. static void system_clock_8m_irc8m(void);
  58. #endif /* __SYSTEM_CLOCK_8M_HXTAL */

  59. /* configure the system clock */
  60. static void system_clock_config(void);

  61. /*!
  62.     \brief      setup the microcontroller system, initialize the system
  63.     \param[in]  none
  64.     \param[out] none
  65.     \retval     none
  66. */
  67. void SystemInit (void)
  68. {
  69.     /* enable IRC8M */
  70.     RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  71.     while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  72.     }
  73.     /* reset RCU */
  74. #ifdef GD32F130_150
  75.     RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  76.                   RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  77. #elif defined (GD32F170_190)
  78.     RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  79.                   RCU_CFG0_ADCPSC | RCU_CFG0_CKOUT0SEL | RCU_CFG0_CKOUT0DIV | RCU_CFG0_PLLDV);
  80. #endif /* GD32F130_150 */
  81.     RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLPREDV);
  82. #ifdef GD32F130_150
  83.     RCU_CFG0 &= ~(RCU_CFG0_USBDPSC);
  84. #endif /* GD32F130_150 */
  85.     RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  86.     RCU_CFG1 &= ~RCU_CFG1_HXTALPREDV;
  87.     RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_CECSEL | RCU_CFG2_ADCSEL);
  88. #ifdef GD32F130_150
  89.     RCU_CTL1 &= ~RCU_CTL1_IRC14MEN;
  90. #elif defined (GD32F170_190)
  91.     RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  92.     RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  93.     RCU_CFG3 &= ~RCU_CFG3_CKOUT1SEL;
  94.     RCU_CFG3 &= ~RCU_CFG3_CKOUT1DIV;
  95. #endif /* GD32F130_150 */
  96.     RCU_INT = 0x00000000U;
  97.    
  98.     /* configure system clock */
  99.     system_clock_config();
  100. }

  101. /*!
  102.     \brief      configure the system clock
  103.     \param[in]  none
  104.     \param[out] none
  105.     \retval     none
  106. */
  107. static void system_clock_config(void)
  108. {
  109. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  110.     system_clock_8m_hxtal();
  111. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  112.     system_clock_72m_hxtal();
  113. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  114.     system_clock_72m_irc8m();
  115. #else
  116.     system_clock_8m_irc8m();
  117. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  118. }

  119. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  120. /*!
  121.     \brief      configure the system clock to 8M by HXTAL
  122.     \param[in]  none
  123.     \param[out] none
  124.     \retval     none
  125. */
  126. static void system_clock_8m_hxtal(void)
  127. {
  128.     uint32_t timeout = 0;
  129.    
  130.     /* enable HXTAL */
  131.     RCU_CTL0 |= RCU_CTL0_HXTALEN;
  132.    
  133.     /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  134.     while((0 == (RCU_CTL0 & RCU_CTL0_HXTALSTB)) && (HXTAL_STARTUP_TIMEOUT != timeout++));
  135.    
  136.     /* if fail */
  137.     if(0 == (RCU_CTL0 & RCU_CTL0_HXTALSTB))
  138.         return;
  139.    
  140.     /* HXTAL is stable */
  141.     /* AHB = SYSCLK */
  142.     RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  143.     /* APB2 = AHB */
  144.     RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  145.     /* APB1 = AHB */
  146.     RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  147.    
  148.     /* select HXTAL as system clock */
  149.     RCU_CFG0 &= ~RCU_CFG0_SCS;
  150.     RCU_CFG0 |= RCU_CKSYSSRC_HXTAL;
  151.    
  152.     /* wait until HXTAL is selected as system clock */
  153.     while(0 == (RCU_CFG0 & RCU_SCSS_HXTAL));
  154. }

  155. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  156. /*!
  157.     \brief      configure the system clock to 72M by PLL which selects HXTAL as its clock source
  158.     \param[in]  none
  159.     \param[out] none
  160.     \retval     none
  161. */
  162. static void system_clock_72m_hxtal(void)
  163. {
  164.     uint32_t timeout = 0U;
  165.     uint32_t stab_flag = 0U;
  166.    
  167.     /* enable HXTAL */
  168.     RCU_CTL0 |= RCU_CTL0_HXTALEN;

  169.     /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  170.     do{
  171.         timeout++;
  172.         stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  173.     }
  174.     while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));

  175.     /* if fail */
  176.     if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  177.         return;
  178.     }
  179.    
  180.     /* HXTAL is stable */
  181.     /* AHB = SYSCLK */
  182.     RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  183.     /* APB2 = AHB */
  184.     RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  185.     /* APB1 = AHB */
  186.     RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;

  187.     /* PLL = HXTAL * 9 = 72 MHz */
  188.     RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLDV);
  189.     RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL9);

  190.     /* enable PLL */
  191.     RCU_CTL0 |= RCU_CTL0_PLLEN;

  192.     /* wait until PLL is stable */
  193.     while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  194.     }

  195.     /* select PLL as system clock */
  196.     RCU_CFG0 &= ~RCU_CFG0_SCS;
  197.     RCU_CFG0 |= RCU_CKSYSSRC_PLL;

  198.     /* wait until PLL is selected as system clock */
  199.     while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  200.     }
  201. }

  202. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  203. /*!
  204.     \brief      configure the system clock to 72M by PLL which selects IRC8M/2 as its clock source
  205.     \param[in]  none
  206.     \param[out] none
  207.     \retval     none
  208. */
  209. static void system_clock_72m_irc8m(void)
  210. {
  211.     /* AHB = SYSCLK */
  212.     RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  213.     /* APB2 = AHB */
  214.     RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  215.     /* APB1 = AHB */
  216.     RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  217.     /* PLL = (IRC8M/2) * 18 = 72 MHz */
  218.     RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  219.     RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL12);
  220.    
  221.     /* enable PLL */
  222.     RCU_CTL0 |= RCU_CTL0_PLLEN;

  223.     /* wait until PLL is stable */
  224.     while(0 == (RCU_CTL0 & RCU_CTL0_PLLSTB));

  225.     /* select PLL as system clock */
  226.     RCU_CFG0 &= ~RCU_CFG0_SCS;
  227.     RCU_CFG0 |= RCU_CKSYSSRC_PLL;

  228.     /* wait until PLL is selected as system clock */
  229.     while(0 == (RCU_CFG0 & RCU_SCSS_PLL));
  230. }

  231. #else
  232. /*!
  233.     \brief      configure the system clock to 8M by IRC8M
  234.     \param[in]  none
  235.     \param[out] none
  236.     \retval     none
  237. */
  238. static void system_clock_8m_irc8m(void)
  239. {
  240.     /* AHB = SYSCLK */
  241.     RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  242.     /* APB2 = AHB */
  243.     RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  244.     /* APB1 = AHB */
  245.     RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  246.    
  247.     /* select IRC8M as system clock */
  248.     RCU_CFG0 &= ~RCU_CFG0_SCS;
  249.     RCU_CFG0 |= RCU_CKSYSSRC_IRC8M;
  250.    
  251.     /* wait until IRC8M is selected as system clock */
  252.     while(0 != (RCU_CFG0 & RCU_SCSS_IRC8M));
  253. }
  254. #endif /* __SYSTEM_CLOCK_8M_HXTAL */

  255. /*!
  256.     \brief      update the SystemCoreClock with current core clock retrieved from cpu registers
  257.     \param[in]  none
  258.     \param[out] none
  259.     \retval     none
  260. */
  261. void SystemCoreClockUpdate (void)
  262. {
  263.     uint32_t sws = 0U;
  264.     uint32_t pllmf = 0U, pllmf4 = 0U, pllsel = 0U, prediv = 0U, idx = 0U, clk_exp = 0U;
  265.     /* exponent of AHB clock divider */
  266.     const uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  267.    
  268.     sws = GET_BITS(RCU_CFG0, 2, 3);
  269.     switch(sws){
  270.     /* IRC8M is selected as CK_SYS */
  271.     case SEL_IRC8M:
  272.         SystemCoreClock = IRC8M_VALUE;
  273.         break;
  274.     /* HXTAL is selected as CK_SYS */
  275. //    case SEL_HXTAL:
  276. //        SystemCoreClock = HXTAL_VALUE;
  277. //        break;
  278.     /* PLL is selected as CK_SYS */
  279.     case SEL_PLL:
  280.         /* get the value of PLLMF[3:0] */
  281.         pllmf = GET_BITS(RCU_CFG0, 18, 21);
  282.         pllmf4 = GET_BITS(RCU_CFG0, 27, 27);
  283.         /* high 16 bits */
  284.         if(1U == pllmf4){
  285.             pllmf += 17U;
  286.         }else{
  287.             pllmf += 2U;
  288.         }
  289.         /* PLL clock source selection, HXTAL or IRC8M/2 */
  290.         pllsel = GET_BITS(RCU_CFG0, 16, 16);
  291.         if(0U != pllsel){
  292.             prediv = (GET_BITS(RCU_CFG1, 0, 3) + 1U);
  293.             SystemCoreClock = (HXTAL_VALUE / prediv) * pllmf;
  294.         }else{
  295.             SystemCoreClock = (IRC8M_VALUE >> 1) * pllmf;
  296.         }
  297.         break;
  298.     /* IRC8M is selected as CK_SYS */
  299.     default:
  300.         SystemCoreClock = IRC8M_VALUE;
  301.         break;
  302.     }
  303.     /* calculate AHB clock frequency */
  304.     idx = GET_BITS(RCU_CFG0, 4, 7);
  305.     clk_exp = ahb_exp[idx];
  306.     SystemCoreClock >>= clk_exp;
  307. }
时钟我配置的48MHZ,内部HSI时钟源。

  1. int main(void)
  2. {
  3.     //systick_config();  // 初始化Systick为1ms中断
  4.     gpio_config();
  5.     usart_config();
  6. //    timer_capture_config();  // 初始化定时器输入捕获
  7. //    delay_1ms(100);
  8.     LOG_DEBUG("GD32F130G6 Spirometer Ready\r\n");
  9.     LOG_DEBUG("Using TIMER2 capture on PA6 for speed measurement\r\n");
  10.     LOG_DEBUG("Wait for start command (0xA5 0x5A 0x01 0x00)\r\n");

  11.     while (1)
  12.     {
  13.         
  14.         LOG_DEBUG("L");
  15. //        if(rx_cmd_ready) {
  16. //            LOG_DEBUG("Receive new command\r\n");
  17. //            parse_command();
  18. //        }
  19.         
  20.        // handle_measurement();

  21.         delay_1ms(300);
  22.         LOG_DEBUG("A");
  23.         delay_1ms(300);
  24.         LOG_DEBUG("B");
  25.         delay_1ms(300);
  26.         LOG_DEBUG("C");
  27.         delay_1ms(300);
  28.         LOG_DEBUG("H");
  29.     }
  30. }
1812268c916ba8fb33.png 通过串口打印出来的消息可以发现一直在重启
jcky001 发表于 2025-9-23 17:02 | 显示全部楼层
时钟源不稳定?
onlycook 发表于 2025-9-23 19:02 | 显示全部楼层
PLL配置错误?
powerantone 发表于 2025-9-23 21:03 | 显示全部楼层
检查时钟安全机制,如果时钟失效,MCU可能会重启。
probedog 发表于 2025-9-23 22:03 | 显示全部楼层
电源是否稳定、?
stormwind123 发表于 2025-9-23 21:04 | 显示全部楼层
查复位电路是否正常工作.
七毛钱 发表于 2025-9-23 22:04 | 显示全部楼层
看门狗未喂狗?
海滨消消 发表于 2025-9-23 17:35 | 显示全部楼层
检查主循环中的代码有没有死循环或导致程序崩溃的代码。
豌豆爹 发表于 2025-9-23 17:06 | 显示全部楼层
堆栈溢出?
豌豆爹 发表于 2025-9-23 17:06 | 显示全部楼层
检查是否有引脚冲突,特别是调试引脚或其他复用功能引脚。
麻花油条 发表于 2025-9-23 18:36 | 显示全部楼层
检查是否有代码触发了软件复位。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

189

主题

897

帖子

12

粉丝
快速回复 在线客服 返回列表 返回顶部