Guest User

Untitled

a guest
Aug 31st, 2021
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.19 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *stm32f103c8t6
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include <stdio.h>
  26. #include <string.h>
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31.  
  32. /* USER CODE END PTD */
  33.  
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37.  
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40.  
  41. /* USER CODE END PM */
  42.  
  43. /* Private variables ---------------------------------------------------------*/
  44. SPI_HandleTypeDef hspi2;
  45.  
  46. /* USER CODE BEGIN PV */
  47.  
  48. /* USER CODE END PV */
  49.  
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. static void MX_GPIO_Init(void);
  53. static void MX_SPI2_Init(void);
  54. /* USER CODE BEGIN PFP */
  55.  
  56. /* USER CODE END PFP */
  57.  
  58. /* Private user code ---------------------------------------------------------*/
  59. /* USER CODE BEGIN 0 */
  60.  
  61. /* USER CODE END 0 */
  62.  
  63. /**
  64.   * @brief  The application entry point.
  65.   * @retval int
  66.   */
  67. int main(void)
  68. {
  69.   /* USER CODE BEGIN 1 */
  70.  
  71.   /* USER CODE END 1 */
  72.  
  73.   /* MCU Configuration--------------------------------------------------------*/
  74.  
  75.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  76.   HAL_Init();
  77.  
  78.   /* USER CODE BEGIN Init */
  79.  
  80.   /* USER CODE END Init */
  81.  
  82.   /* Configure the system clock */
  83.   SystemClock_Config();
  84.  
  85.   /* USER CODE BEGIN SysInit */
  86.  
  87.   /* USER CODE END SysInit */
  88.  
  89.   /* Initialize all configured peripherals */
  90.   MX_GPIO_Init();
  91.   MX_SPI2_Init();
  92.   /* USER CODE BEGIN 2 */
  93.   // CS pin should befault high
  94.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
  95.   uint8_t TX_Data[13] = "Hello World!";
  96.   uint8_t RX_Data[13];
  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_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // CS set LOW
  107.         HAL_SPI_TransmitReceive(&hspi2, TX_Data, RX_Data, 13, 100);    
  108.         }
  109.         while (hspi2.State == HAL_SPI_STATE_BUSY);
  110.         HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // CS set HIGH
  111.         HAL_Delay(1000);
  112.     }
  113.   /* USER CODE END 3 */
  114. }
  115.  
  116. /**
  117.   * @brief System Clock Configuration
  118.   * @retval None
  119.   */
  120. void SystemClock_Config(void)
  121. {
  122.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  123.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  124.  
  125.   /** Initializes the RCC Oscillators according to the specified parameters
  126.   * in the RCC_OscInitTypeDef structure.
  127.   */
  128.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  129.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  130.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  131.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  132.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  133.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  134.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  135.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  136.   {
  137.     Error_Handler();
  138.   }
  139.   /** Initializes the CPU, AHB and APB buses clocks
  140.   */
  141.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  142.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  143.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  144.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  145.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  146.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  147.  
  148.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  149.   {
  150.     Error_Handler();
  151.   }
  152. }
  153.  
  154. /**
  155.   * @brief SPI2 Initialization Function
  156.   * @param None
  157.   * @retval None
  158.   */
  159. static void MX_SPI2_Init(void)
  160. {
  161.  
  162.   /* USER CODE BEGIN SPI2_Init 0 */
  163.  
  164.   /* USER CODE END SPI2_Init 0 */
  165.  
  166.   /* USER CODE BEGIN SPI2_Init 1 */
  167.  
  168.   /* USER CODE END SPI2_Init 1 */
  169.   /* SPI2 parameter configuration*/
  170.   hspi2.Instance = SPI2;
  171.   hspi2.Init.Mode = SPI_MODE_MASTER;
  172.   hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  173.   hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  174.   hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  175.   hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  176.   hspi2.Init.NSS = SPI_NSS_SOFT;
  177.   hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  178.   hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  179.   hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  180.   hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  181.   hspi2.Init.CRCPolynomial = 10;
  182.   if (HAL_SPI_Init(&hspi2) != HAL_OK)
  183.   {
  184.     Error_Handler();
  185.   }
  186.   /* USER CODE BEGIN SPI2_Init 2 */
  187.  
  188.   /* USER CODE END SPI2_Init 2 */
  189.  
  190. }
  191.  
  192. /**
  193.   * @brief GPIO Initialization Function
  194.   * @param None
  195.   * @retval None
  196.   */
  197. static void MX_GPIO_Init(void)
  198. {
  199.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  200.  
  201.   /* GPIO Ports Clock Enable */
  202.   __HAL_RCC_GPIOD_CLK_ENABLE();
  203.   __HAL_RCC_GPIOB_CLK_ENABLE();
  204.   __HAL_RCC_GPIOA_CLK_ENABLE();
  205.  
  206.   /*Configure GPIO pin Output Level */
  207.   HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);
  208.  
  209.   /*Configure GPIO pin : SPI2_CS_Pin */
  210.   GPIO_InitStruct.Pin = SPI2_CS_Pin;
  211.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  212.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  213.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  214.   HAL_GPIO_Init(SPI2_CS_GPIO_Port, &GPIO_InitStruct);
  215.  
  216. }
  217.  
  218. /* USER CODE BEGIN 4 */
  219.  
  220. /* USER CODE END 4 */
  221.  
  222. /**
  223.   * @brief  This function is executed in case of error occurrence.
  224.   * @retval None
  225.   */
  226. void Error_Handler(void)
  227. {
  228.   /* USER CODE BEGIN Error_Handler_Debug */
  229.   /* User can add his own implementation to report the HAL error return state */
  230.   __disable_irq();
  231.   while (1)
  232.   {
  233.   }
  234.   /* USER CODE END Error_Handler_Debug */
  235. }
  236.  
  237. #ifdef  USE_FULL_ASSERT
  238. /**
  239.   * @brief  Reports the name of the source file and the source line number
  240.   *         where the assert_param error has occurred.
  241.   * @param  file: pointer to the source file name
  242.   * @param  line: assert_param error line source number
  243.   * @retval None
  244.   */
  245. void assert_failed(uint8_t *file, uint32_t line)
  246. {
  247.   /* USER CODE BEGIN 6 */
  248.   /* User can add his own implementation to report the file name and line number,
  249.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  250.   /* USER CODE END 6 */
  251. }
  252. #endif /* USE_FULL_ASSERT */
  253.  
  254. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  255.  
Advertisement
Add Comment
Please, Sign In to add comment