你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

简单点亮LED都hard fault了

[复制链接]
潇潇雨歇pku 提问时间:2018-5-18 16:30 /
单片机型号是STM32F070RBT6,自己做了一块板子,做一些AD检测、数据存储、USBUART通信等功能。
由于对单片机研究不是很多,使用cubemx生成初始程序,但是测试发现很严重的hard fault。
于是就把所有的东西都关掉了,只保留PB12翻转PIO,看LED的点亮、熄灭。发现还是会hardfault。hard fault的原因无非就是数组越界,堆栈溢出等,这些我都没有涉及到,除了系统的中断外也没有开启什么中断,实在想不通为什么会出错。
下面是代码和电路图,所有的元件都是mouser买的,应该不会有假货。麻烦大家帮我看看是哪里犯了什么低级错误。
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f0xx_hal.h"

  41. /* USER CODE BEGIN Includes */

  42. /* USER CODE END Includes */

  43. /* Private variables ---------------------------------------------------------*/

  44. /* USER CODE BEGIN PV */
  45. /* Private variables ---------------------------------------------------------*/

  46. /* USER CODE END PV */

  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. static void MX_GPIO_Init(void);

  50. /* USER CODE BEGIN PFP */
  51. /* Private function prototypes -----------------------------------------------*/

  52. /* USER CODE END PFP */

  53. /* USER CODE BEGIN 0 */

  54. /* USER CODE END 0 */

  55. int main(void)
  56. {

  57.   /* USER CODE BEGIN 1 */

  58.   /* USER CODE END 1 */

  59.   /* MCU Configuration----------------------------------------------------------*/

  60.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  61.   HAL_Init();

  62.   /* USER CODE BEGIN Init */

  63.   /* USER CODE END Init */

  64.   /* Configure the system clock */
  65.   SystemClock_Config();

  66.   /* USER CODE BEGIN SysInit */

  67.   /* USER CODE END SysInit */

  68.   /* Initialize all configured peripherals */
  69.   MX_GPIO_Init();

  70.   /* USER CODE BEGIN 2 */
  71.   
  72.   /* USER CODE END 2 */

  73.   /* Infinite loop */
  74.   /* USER CODE BEGIN WHILE */
  75.   while (1)
  76.   {
  77.   /* USER CODE END WHILE */

  78.   /* USER CODE BEGIN 3 */
  79.     HAL_Delay(200);
  80.     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  81.     HAL_Delay(200);
  82.     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  83.   /* USER CODE BEGIN 3 */

  84.   }
  85.   /* USER CODE END 3 */

  86. }

  87. /** System Clock Configuration
  88. */
  89. void SystemClock_Config(void)
  90. {

  91.   RCC_OscInitTypeDef RCC_OscInitStruct;
  92.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  93.     /**Initializes the CPU, AHB and APB busses clocks
  94.     */
  95.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  96.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  97.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  98.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  99.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  100.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  101.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  102.   {
  103.     _Error_Handler(__FILE__, __LINE__);
  104.   }

  105.     /**Initializes the CPU, AHB and APB busses clocks
  106.     */
  107.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  108.                               |RCC_CLOCKTYPE_PCLK1;
  109.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  110.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  111.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  112.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  113.   {
  114.     _Error_Handler(__FILE__, __LINE__);
  115.   }

  116.     /**Configure the Systick interrupt time
  117.     */
  118.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  119.     /**Configure the Systick
  120.     */
  121.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  122.   /* SysTick_IRQn interrupt configuration */
  123.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  124. }

  125. /** Configure pins as
  126.         * Analog
  127.         * Input
  128.         * Output
  129.         * EVENT_OUT
  130.         * EXTI
  131. */
  132. static void MX_GPIO_Init(void)
  133. {

  134.   GPIO_InitTypeDef GPIO_InitStruct;

  135.   /* GPIO Ports Clock Enable */
  136.   __HAL_RCC_GPIOF_CLK_ENABLE();
  137.   __HAL_RCC_GPIOA_CLK_ENABLE();

  138.   /*Configure GPIO pin Output Level */
  139.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

  140.   /*Configure GPIO pin : PA5 */
  141.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  142.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  143.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  144.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  145.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  146. }

  147. /* USER CODE BEGIN 4 */

  148. /* USER CODE END 4 */

  149. /**
  150.   * @brief  This function is executed in case of error occurrence.
  151.   * @param  None
  152.   * @retval None
  153.   */
  154. void _Error_Handler(char * file, int line)
  155. {
  156.   /* USER CODE BEGIN Error_Handler_Debug */
  157.   /* User can add his own implementation to report the HAL error return state */
  158.   while(1)
  159.   {
  160.   }
  161.   /* USER CODE END Error_Handler_Debug */
  162. }

  163. #ifdef USE_FULL_ASSERT

  164. /**
  165.    * @brief Reports the name of the source file and the source line number
  166.    * where the assert_param error has occurred.
  167.    * @param file: pointer to the source file name
  168.    * @param line: assert_param error line source number
  169.    * @retval None
  170.    */
  171. void assert_failed(uint8_t* file, uint32_t line)
  172. {
  173.   /* USER CODE BEGIN 6 */
  174.   /* User can add his own implementation to report the file name and line number,
  175.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  176.   /* USER CODE END 6 */

  177. }

  178. #endif

  179. /**
  180.   * @}
  181.   */

  182. /**
  183.   * @}
  184. */

  185. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
ELD1.png

收藏 评论6 发布时间:2018-5-18 16:30

举报

6个回答
wenyangzeng 回答时间:2018-5-18 22:10:23
按照外部晶振8MHZ:
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
不是
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;

评分

参与人数 1蝴蝶豆 +3 收起 理由
zero99 + 3

查看全部评分

feixiang20 回答时间:2018-5-19 02:00:20
可以去参考以下资料
【stm32f070RBT6开发,系统总是进入hardfault中断】
【STM32F0 M0 向结构体赋值进入HardFault异常】
【操作内部FLASH导致进入HardFault】
【stm32出现HardFault,应该如何解决】
希望对你有帮助

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

潇潇雨歇pku 回答时间:2018-5-19 08:30:23
feixiang20 发表于 2018-5-19 02:00
可以去参考以下资料
【stm32f070RBT6开发,系统总是进入hardfault中断】
【STM32F0 M0 向结构体赋值进入Hard ...

多谢啦。
现在在开发板上跑程序是好的,所以我怀疑是硬件问题。
但是板子又是照着nucleo布的……
wolfgang2015 回答时间:2018-5-21 17:59:13
确定
有“ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;”
HSE是好的。

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

潇潇雨歇pku 回答时间:2018-5-22 09:05:10
wolfgang2015 发表于 2018-5-21 17:59
确定
有“ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;”
HSE是好的。

多谢,我去检查一下。
应该是好的,晶振是在mouser买的。开发板上焊的是淘宝货都没问题的。
潇潇雨歇pku 回答时间:2018-7-4 17:00:48
时隔一个月,回来总结一下:
1 温度太高会导致死机
2 Flash等待周期不能设置为0,要根据电压和HCLK进行设置,cubemx有点坑,直接设置为0了

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
13245底部标题123相同标题
12底部标题123相同标题
33333底部标题123相同序号
3435底部标题-无链接
关注我们
st-img 微信公众号
st-img 手机版