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

STM32CubIDE点灯在Mac下

[复制链接]
STMCU小助手 发布时间:2023-1-10 16:53

mac 环境下没有keil,现在改为stm32 cubeide环境下开发。


由于我安装也花了半天时间,主要是下载安装包花了一点时间。还有就是权限也折腾了一小会。

# 创建LED灯程序

菜单File->New->STM32 Project

085017xao13939in6akj7m.png.thumb.png


   选Board Selector,输入U575选择好开发板

085405h4uehbbktdthcgiu.png.thumb.png


选中开发板点击next:

085601fcx4cnbd0m9tiobn.png.thumb.png


  输入工程名称stm32_LED_DEMO,然后Finsh完成

085734xwdhehrtyljeglht.png.thumb.png


进入图形配置界面:

090037un89m527mda4dgg9.png.thumb.png


观察了一下,系统默认配置好了三个LED ,如果自己需要调整,自行配置。

090440gu2fkfi9vun2h9co.png.thumb.png


打开main.c,GPIO、UART等基本上配置好了代码,如下:

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @File : main.c
  5.   * @brief : Main program body
  6.   ******************************************************************************
  7.   * @attention *
  8.   * Copyright (c) 2022 STMicroelectronics.
  9.   * All rights reserved.
  10.   *
  11.   * This software is licensed under terms that can be found in the LICENSE file
  12.   * in the root directory of this software component.
  13.   * If no LICENSE file comes with this software, it is provided AS-IS.
  14.   *
  15.   ******************************************************************************
  16.   */
  17. /* USER CODE END Header */
  18. /* Includes ------------------------------------------------------------------*/
  19. #include "main.h"

  20. /* Private includes ----------------------------------------------------------*/
  21. /* USER CODE BEGIN Includes */

  22. /* USER CODE END Includes */

  23. /* Private typedef -----------------------------------------------------------*/
  24. /* USER CODE BEGIN PTD */

  25. /* USER CODE END PTD */

  26. /* Private define ------------------------------------------------------------*/
  27. /* USER CODE BEGIN PD */
  28. /* USER CODE END PD */

  29. /* Private macro -------------------------------------------------------------*/
  30. /* USER CODE BEGIN PM */

  31. /* USER CODE END PM */

  32. /* Private variables ---------------------------------------------------------*/
  33. ADC_HandleTypeDef hadc1;

  34. UART_HandleTypeDef huart1;

  35. PCD_HandleTypeDef hpcd_USB_OTG_FS;

  36. /* USER CODE BEGIN PV */

  37. /* USER CODE END PV */

  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. static void SystemPower_Config(void);
  41. static void MX_GPIO_Init(void);
  42. static void MX_ADC1_Init(void);
  43. static void MX_UCPD1_Init(void);
  44. static void MX_USART1_UART_Init(void);
  45. static void MX_USB_OTG_FS_PCD_Init(void);
  46. /* USER CODE BEGIN PFP */

  47. /* USER CODE END PFP */

  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */

  50. /* USER CODE END 0 */

  51. /**
  52.   * @brief  The application entry point.
  53.   * @retval int
  54.   */
  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.   /* Configure the System Power */
  67.   SystemPower_Config();

  68.   /* USER CODE BEGIN SysInit */

  69.   /* USER CODE END SysInit */

  70.   /* Initialize all configured peripherals */
  71.   MX_GPIO_Init();
  72.   MX_ADC1_Init();
  73.   MX_UCPD1_Init();
  74.   MX_USART1_UART_Init();
  75.   MX_USB_OTG_FS_PCD_Init();
  76.   /* USER CODE BEGIN 2 */

  77.   /* USER CODE END 2 */

  78.   /* Infinite loop */
  79.   /* USER CODE BEGIN WHILE */
  80.   while (1)
  81.   {
  82.     /* USER CODE END WHILE */

  83.     /* USER CODE BEGIN 3 */
  84.   }
  85.   /* USER CODE END 3 */
  86. }

  87. /**
  88.   * @brief System Clock Configuration
  89.   * @retval None
  90.   */
  91. void SystemClock_Config(void)
  92. {
  93.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  94.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  95.   /** Configure the main internal regulator output voltage
  96.   */
  97.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  98.   {
  99.     Error_Handler();
  100.   }

  101.   /** Initializes the CPU, AHB and APB buses clocks
  102.   */
  103.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI
  104.                               |RCC_OSCILLATORTYPE_MSI;
  105.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  106.   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  107.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  108.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  109.   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  110.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  111.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  112.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  113.   RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  114.   RCC_OscInitStruct.PLL.PLLM = 1;
  115.   RCC_OscInitStruct.PLL.PLLN = 80;
  116.   RCC_OscInitStruct.PLL.PLLP = 2;
  117.   RCC_OscInitStruct.PLL.PLLQ = 2;
  118.   RCC_OscInitStruct.PLL.PLLR = 2;
  119.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
  120.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  121.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  122.   {
  123.     Error_Handler();
  124.   }

  125.   /** Initializes the CPU, AHB and APB buses clocks
  126.   */
  127.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  128.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  129.                               |RCC_CLOCKTYPE_PCLK3;
  130.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  131.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  132.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  133.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  134.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  135.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  136.   {
  137.     Error_Handler();
  138.   }
  139. }

  140. /**
  141.   * @brief Power Configuration
  142.   * @retval None
  143.   */
  144. static void SystemPower_Config(void)
  145. {
  146.   HAL_PWREx_EnableVddIO2();

  147.   /*
  148.    * Switch to SMPS regulator instead of LDO
  149.    */
  150.   if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  151.   {
  152.     Error_Handler();
  153.   }
  154. }

  155. /**
  156.   * @brief ADC1 Initialization Function
  157.   * @param None
  158.   * @retval None
  159.   */
  160. static void MX_ADC1_Init(void)
  161. {

  162.   /* USER CODE BEGIN ADC1_Init 0 */

  163.   /* USER CODE END ADC1_Init 0 */

  164.   /* USER CODE BEGIN ADC1_Init 1 */

  165.   /* USER CODE END ADC1_Init 1 */

  166.   /** Common config
  167.   */
  168.   hadc1.Instance = ADC1;
  169.   hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  170.   hadc1.Init.Resolution = ADC_RESOLUTION_14B;
  171.   hadc1.Init.GainCompensation = 0;
  172.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  173.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  174.   hadc1.Init.LowPowerAutoWait = DISABLE;
  175.   hadc1.Init.ContinuousConvMode = DISABLE;
  176.   hadc1.Init.NbrOfConversion = 1;
  177.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  178.   hadc1.Init.DMAContinuousRequests = DISABLE;
  179.   hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  180.   hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  181.   hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  182.   hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  183.   hadc1.Init.OversamplingMode = DISABLE;
  184.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  185.   {
  186.     Error_Handler();
  187.   }
  188.   /* USER CODE BEGIN ADC1_Init 2 */

  189.   /* USER CODE END ADC1_Init 2 */

  190. }

  191. /**
  192.   * @brief UCPD1 Initialization Function
  193.   * @param None
  194.   * @retval None
  195.   */
  196. static void MX_UCPD1_Init(void)
  197. {

  198.   /* USER CODE BEGIN UCPD1_Init 0 */

  199.   /* USER CODE END UCPD1_Init 0 */

  200.   LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  201.   /* Peripheral clock enable */
  202.   LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_UCPD1);

  203.   LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  204.   LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  205.   /**UCPD1 GPIO Configuration
  206.   PB15   ------> UCPD1_CC2
  207.   PA15 (JTDI)   ------> UCPD1_CC1
  208.   */
  209.   GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  210.   GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  211.   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  212.   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  213.   GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  214.   GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  215.   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  216.   LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  217.   /* USER CODE BEGIN UCPD1_Init 1 */

  218.   /* USER CODE END UCPD1_Init 1 */
  219.   /* USER CODE BEGIN UCPD1_Init 2 */

  220.   /* USER CODE END UCPD1_Init 2 */

  221. }

  222. /**
  223.   * @brief USART1 Initialization Function
  224.   * @param None
  225.   * @retval None
  226.   */
  227. static void MX_USART1_UART_Init(void)
  228. {

  229.   /* USER CODE BEGIN USART1_Init 0 */

  230.   /* USER CODE END USART1_Init 0 */

  231.   /* USER CODE BEGIN USART1_Init 1 */

  232.   /* USER CODE END USART1_Init 1 */
  233.   huart1.Instance = USART1;
  234.   huart1.Init.BaudRate = 115200;
  235.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  236.   huart1.Init.StopBits = UART_STOPBITS_1;
  237.   huart1.Init.Parity = UART_PARITY_NONE;
  238.   huart1.Init.Mode = UART_MODE_TX_RX;
  239.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  240.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  241.   huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  242.   huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  243.   huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  244.   if (HAL_UART_Init(&huart1) != HAL_OK)
  245.   {
  246.     Error_Handler();
  247.   }
  248.   if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  249.   {
  250.     Error_Handler();
  251.   }
  252.   if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  253.   {
  254.     Error_Handler();
  255.   }
  256.   if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  257.   {
  258.     Error_Handler();
  259.   }
  260.   /* USER CODE BEGIN USART1_Init 2 */

  261.   /* USER CODE END USART1_Init 2 */

  262. }

  263. /**
  264.   * @brief USB_OTG_FS Initialization Function
  265.   * @param None
  266.   * @retval None
  267.   */
  268. static void MX_USB_OTG_FS_PCD_Init(void)
  269. {

  270.   /* USER CODE BEGIN USB_OTG_FS_Init 0 */

  271.   /* USER CODE END USB_OTG_FS_Init 0 */

  272.   /* USER CODE BEGIN USB_OTG_FS_Init 1 */

  273.   /* USER CODE END USB_OTG_FS_Init 1 */
  274.   hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  275.   hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
  276.   hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  277.   hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  278.   hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  279.   hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  280.   hpcd_USB_OTG_FS.Init.battery_charging_enable = DISABLE;
  281.   hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  282.   hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
  283.   if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  284.   {
  285.     Error_Handler();
  286.   }
  287.   /* USER CODE BEGIN USB_OTG_FS_Init 2 */

  288.   /* USER CODE END USB_OTG_FS_Init 2 */

  289. }

  290. /**
  291.   * @brief GPIO Initialization Function
  292.   * @param None
  293.   * @retval None
  294.   */
  295. static void MX_GPIO_Init(void)
  296. {
  297.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  298.   /* GPIO Ports Clock Enable */
  299.   __HAL_RCC_GPIOC_CLK_ENABLE();
  300.   __HAL_RCC_GPIOB_CLK_ENABLE();
  301.   __HAL_RCC_GPIOG_CLK_ENABLE();
  302.   __HAL_RCC_GPIOA_CLK_ENABLE();

  303.   /*Configure GPIO pin Output Level */
  304.   HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);

  305.   /*Configure GPIO pin Output Level */
  306.   HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);

  307.   /*Configure GPIO pin Output Level */
  308.   HAL_GPIO_WritePin(GPIOB, UCPD_DBn_Pin|LED_BLUE_Pin, GPIO_PIN_RESET);

  309.   /*Configure GPIO pin : USER_BUTTON_Pin */
  310.   GPIO_InitStruct.Pin = USER_BUTTON_Pin;
  311.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  312.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  313.   HAL_GPIO_Init(USER_BUTTON_GPIO_Port, &GPIO_InitStruct);

  314.   /*Configure GPIO pin : UCPD_FLT_Pin */
  315.   GPIO_InitStruct.Pin = UCPD_FLT_Pin;
  316.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  317.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  318.   HAL_GPIO_Init(UCPD_FLT_GPIO_Port, &GPIO_InitStruct);

  319.   /*Configure GPIO pin : LED_RED_Pin */
  320.   GPIO_InitStruct.Pin = LED_RED_Pin;
  321.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  322.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  323.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  324.   HAL_GPIO_Init(LED_RED_GPIO_Port, &GPIO_InitStruct);

  325.   /*Configure GPIO pin : LED_GREEN_Pin */
  326.   GPIO_InitStruct.Pin = LED_GREEN_Pin;
  327.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  328.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  329.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  330.   HAL_GPIO_Init(LED_GREEN_GPIO_Port, &GPIO_InitStruct);

  331.   /*Configure GPIO pin : UCPD_DBn_Pin */
  332.   GPIO_InitStruct.Pin = UCPD_DBn_Pin;
  333.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  334.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  335.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  336.   HAL_GPIO_Init(UCPD_DBn_GPIO_Port, &GPIO_InitStruct);

  337.   /*Configure GPIO pin : LED_BLUE_Pin */
  338.   GPIO_InitStruct.Pin = LED_BLUE_Pin;
  339.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  340.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  341.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  342.   HAL_GPIO_Init(LED_BLUE_GPIO_Port, &GPIO_InitStruct);

  343. }

  344. /* USER CODE BEGIN 4 */

  345. /* USER CODE END 4 */

  346. /**
  347.   * @brief  This function is executed in case of error occurrence.
  348.   * @retval None
  349.   */
  350. void Error_Handler(void)
  351. {
  352.   /* USER CODE BEGIN Error_Handler_Debug */
  353.   /* User can add his own implementation to report the HAL error return state */
  354.   __disable_irq();
  355.   while (1)
  356.   {
  357.   }
  358.   /* USER CODE END Error_Handler_Debug */
  359. }

  360. #ifdef  USE_FULL_ASSERT
  361. /**
  362.   * @brief  Reports the name of the source file and the source line number
  363.   *         where the assert_param error has occurred.
  364.   * @param  file: pointer to the source file name
  365.   * @param  line: assert_param error line source number
  366.   * @retval None
  367.   */
  368. void assert_failed(uint8_t *file, uint32_t line)
  369. {
  370.   /* USER CODE BEGIN 6 */
  371.   /* User can add his own implementation to report the file name and line number,
  372.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  373.   /* USER CODE END 6 */
  374. }
  375. #endif /* USE_FULL_ASSERT */
复制代码

这个工具还是非常方便的,用户只需要写自己的代码就行了。时隔几年不见,这个工具改进了不少。

在main函数里写上LED闪灯程序:

  1. /* USER CODE END WHILE */
  2.           HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
  3.           HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);
  4.           HAL_Delay(1000);
  5.           HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
  6.           HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET);
  7.           HAL_Delay(1000);
  8.     /* USER CODE BEGIN 3 */
复制代码


点击编译,顺利通过。


091537j00dfrz7fwmmc0mk.png.thumb.png


用USB接上开发板,下载程序:

开发板上两个灯顺利点亮。


出处:社区用户:lugl发布



收藏 评论0 发布时间:2023-1-10 16:53

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版