Guest User

Basic STM32 SPI Code

a guest
Aug 24th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.30 KB | Source Code | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2025 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21.  
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24.  
  25. /* USER CODE END Includes */
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29.  
  30. /* USER CODE END PTD */
  31.  
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34.  
  35. /* USER CODE END PD */
  36.  
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39.  
  40. /* USER CODE END PM */
  41.  
  42. /* Private variables ---------------------------------------------------------*/
  43.  
  44. SPI_HandleTypeDef hspi1;
  45.  
  46. /* USER CODE BEGIN PV */
  47.  
  48. uint8_t TX_Buffer [] = "A" ;
  49.  
  50. /* USER CODE END PV */
  51.  
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. static void MX_GPIO_Init(void);
  55. static void MX_SPI1_Init(void);
  56. /* USER CODE BEGIN PFP */
  57.  
  58. /* USER CODE END PFP */
  59.  
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62.  
  63. /* USER CODE END 0 */
  64.  
  65. /**
  66.   * @brief  The application entry point.
  67.   * @retval int
  68.   */
  69. int main(void)
  70. {
  71.  
  72.   /* USER CODE BEGIN 1 */
  73.  
  74.   /* USER CODE END 1 */
  75.  
  76.   /* MCU Configuration--------------------------------------------------------*/
  77.  
  78.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  79.   HAL_Init();
  80.  
  81.   /* USER CODE BEGIN Init */
  82.  
  83.   /* USER CODE END Init */
  84.  
  85.   /* Configure the system clock */
  86.   SystemClock_Config();
  87.  
  88.   /* USER CODE BEGIN SysInit */
  89.  
  90.   /* USER CODE END SysInit */
  91.  
  92.   /* Initialize all configured peripherals */
  93.   MX_GPIO_Init();
  94.   MX_SPI1_Init();
  95.   /* USER CODE BEGIN 2 */
  96.  
  97.   /* USER CODE END 2 */
  98.  
  99.   /* Infinite loop */
  100.   /* USER CODE BEGIN WHILE */
  101.   while (1)
  102.   {
  103.     /* USER CODE END WHILE */
  104.  
  105.     /* USER CODE BEGIN 3 */
  106.       HAL_SPI_Transmit(&hspi1, TX_Buffer, 1, 1000);
  107.       HAL_Delay(100);
  108.  
  109.   }
  110.   /* USER CODE END 3 */
  111. }
  112.  
  113. /**
  114.   * @brief System Clock Configuration
  115.   * @retval None
  116.   */
  117. void SystemClock_Config(void)
  118. {
  119.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  120.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  121.  
  122.   __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_0);
  123.  
  124.   /** Initializes the RCC Oscillators according to the specified parameters
  125.   * in the RCC_OscInitTypeDef structure.
  126.   */
  127.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  128.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  129.   RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV4;
  130.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  131.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  132.   {
  133.     Error_Handler();
  134.   }
  135.  
  136.   /** Initializes the CPU, AHB and APB buses clocks
  137.   */
  138.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  139.                               |RCC_CLOCKTYPE_PCLK1;
  140.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  141.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  142.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  143.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  144.  
  145.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  146.   {
  147.     Error_Handler();
  148.   }
  149. }
  150.  
  151. /**
  152.   * @brief SPI1 Initialization Function
  153.   * @param None
  154.   * @retval None
  155.   */
  156. static void MX_SPI1_Init(void)
  157. {
  158.  
  159.   /* USER CODE BEGIN SPI1_Init 0 */
  160.  
  161.   /* USER CODE END SPI1_Init 0 */
  162.  
  163.   /* USER CODE BEGIN SPI1_Init 1 */
  164.  
  165.   /* USER CODE END SPI1_Init 1 */
  166.   /* SPI1 parameter configuration*/
  167.   hspi1.Instance = SPI1;
  168.   hspi1.Init.Mode = SPI_MODE_MASTER;
  169.   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  170.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  171.   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  172.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  173.   hspi1.Init.NSS = SPI_NSS_SOFT;
  174.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  175.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  176.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  177.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  178.   hspi1.Init.CRCPolynomial = 7;
  179.   hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  180.   hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  181.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  182.   {
  183.     Error_Handler();
  184.   }
  185.   /* USER CODE BEGIN SPI1_Init 2 */
  186.  
  187.   /* USER CODE END SPI1_Init 2 */
  188.  
  189. }
  190.  
  191. /**
  192.   * @brief GPIO Initialization Function
  193.   * @param None
  194.   * @retval None
  195.   */
  196. static void MX_GPIO_Init(void)
  197. {
  198.   /* USER CODE BEGIN MX_GPIO_Init_1 */
  199.  
  200.   /* USER CODE END MX_GPIO_Init_1 */
  201.  
  202.   /* GPIO Ports Clock Enable */
  203.   __HAL_RCC_GPIOA_CLK_ENABLE();
  204.  
  205.   /* USER CODE BEGIN MX_GPIO_Init_2 */
  206.  
  207.   /* USER CODE END MX_GPIO_Init_2 */
  208. }
  209.  
  210. /* USER CODE BEGIN 4 */
  211.  
  212. /* USER CODE END 4 */
  213.  
  214. /**
  215.   * @brief  This function is executed in case of error occurrence.
  216.   * @retval None
  217.   */
  218. void Error_Handler(void)
  219. {
  220.   /* USER CODE BEGIN Error_Handler_Debug */
  221.   /* User can add his own implementation to report the HAL error return state */
  222.   __disable_irq();
  223.   while (1)
  224.   {
  225.   }
  226.   /* USER CODE END Error_Handler_Debug */
  227. }
  228. #ifdef USE_FULL_ASSERT
  229. /**
  230.   * @brief  Reports the name of the source file and the source line number
  231.   *         where the assert_param error has occurred.
  232.   * @param  file: pointer to the source file name
  233.   * @param  line: assert_param error line source number
  234.   * @retval None
  235.   */
  236. void assert_failed(uint8_t *file, uint32_t line)
  237. {
  238.   /* USER CODE BEGIN 6 */
  239.   /* User can add his own implementation to report the file name and line number,
  240.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  241.   /* USER CODE END 6 */
  242. }
  243. #endif /* USE_FULL_ASSERT */
  244.  
Tags: STM32 spi
Advertisement
Add Comment
Please, Sign In to add comment