Advertisement
Guest User

Untitled

a guest
Mar 12th, 2022
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.92 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) 2022 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.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26.  
  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. I2C_HandleTypeDef hi2c1;
  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_I2C1_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.  
  74.   /* MCU Configuration--------------------------------------------------------*/
  75.  
  76.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  77.   HAL_Init();
  78.  
  79.   /* USER CODE BEGIN Init */
  80.  
  81.   /* USER CODE END Init */
  82.  
  83.   /* Configure the system clock */
  84.   SystemClock_Config();
  85.  
  86.   /* USER CODE BEGIN SysInit */
  87.  
  88.   /* USER CODE END SysInit */
  89.  
  90.   /* Initialize all configured peripherals */
  91.   MX_GPIO_Init();
  92.   MX_I2C1_Init();
  93.   /* USER CODE BEGIN 2 */
  94.  
  95.   /* USER CODE END 2 */
  96.  
  97.   /* Infinite loop */
  98.   /* USER CODE BEGIN WHILE */
  99.  
  100.  
  101.   char hi[1];
  102.   hi[0]   = 'h';
  103.  
  104.  
  105.   while (1)
  106.   {
  107.         HAL_Delay(10);
  108.         HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
  109.        HAL_I2C_Master_Transmit(&hi2c1, 144, (uint8_t*)hi, 1, 200);
  110.         /*
  111.          *
  112.          * ALREADY TRIED CODE
  113.          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
  114.          HAL_Delay(1);
  115.          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
  116.          HAL_Delay(1);
  117.  
  118.      __HAL_RCC_I2C1_CLK_DISABLE();
  119.       __HAL_RCC_I2C1_FORCE_RESET();
  120.       __HAL_RCC_I2C1_RELEASE_RESET();
  121.       __HAL_RCC_I2C1_CLK_ENABLE();
  122.         HAL_Delay(10);
  123.  
  124.        HAL_I2C_Master_Transmit(&hi2c1, 144, (uint8_t*)hi, 1, 200);
  125.  
  126.  
  127.           */
  128.     /* USER CODE END WHILE */
  129.  
  130.     /* USER CODE BEGIN 3 */
  131.   }
  132.   /* USER CODE END 3 */
  133. }
  134.  
  135. /**
  136.   * @brief System Clock Configuration
  137.   * @retval None
  138.   */
  139. void SystemClock_Config(void)
  140. {
  141.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  142.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  143.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  144.  
  145.   /** Initializes the CPU, AHB and APB busses clocks
  146.   */
  147.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  148.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  149.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  150.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  151.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  152.   {
  153.     Error_Handler();
  154.   }
  155.   /** Initializes the CPU, AHB and APB busses clocks
  156.   */
  157.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  158.                               |RCC_CLOCKTYPE_PCLK1;
  159.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  160.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  161.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  162.  
  163.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  164.   {
  165.     Error_Handler();
  166.   }
  167.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  168.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  169.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  170.   {
  171.     Error_Handler();
  172.   }
  173. }
  174.  
  175. /**
  176.   * @brief I2C1 Initialization Function
  177.   * @param None
  178.   * @retval None
  179.   */
  180. static void MX_I2C1_Init(void)
  181. {
  182.  
  183.   /* USER CODE BEGIN I2C1_Init 0 */
  184.  
  185.   /* USER CODE END I2C1_Init 0 */
  186.  
  187.   /* USER CODE BEGIN I2C1_Init 1 */
  188.  
  189.   /* USER CODE END I2C1_Init 1 */
  190.   hi2c1.Instance = I2C1;
  191.   hi2c1.Init.Timing = 0x200009FE;
  192.   hi2c1.Init.OwnAddress1 = 0;
  193.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  194.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  195.   hi2c1.Init.OwnAddress2 = 0;
  196.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  197.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  198.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  199.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  200.   {
  201.     Error_Handler();
  202.   }
  203.   /** Configure Analogue filter
  204.   */
  205.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  206.   {
  207.     Error_Handler();
  208.   }
  209.   /** Configure Digital filter
  210.   */
  211.   if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  212.   {
  213.     Error_Handler();
  214.   }
  215.   /* USER CODE BEGIN I2C1_Init 2 */
  216.  
  217.   /* USER CODE END I2C1_Init 2 */
  218.  
  219. }
  220.  
  221. /**
  222.   * @brief GPIO Initialization Function
  223.   * @param None
  224.   * @retval None
  225.   */
  226. static void MX_GPIO_Init(void)
  227. {
  228.  
  229.   /* GPIO Ports Clock Enable */
  230.   __HAL_RCC_GPIOB_CLK_ENABLE();
  231.  
  232. }
  233.  
  234. /* USER CODE BEGIN 4 */
  235.  
  236. /* USER CODE END 4 */
  237.  
  238. /**
  239.   * @brief  This function is executed in case of error occurrence.
  240.   * @retval None
  241.   */
  242. void Error_Handler(void)
  243. {
  244.   /* USER CODE BEGIN Error_Handler_Debug */
  245.   /* User can add his own implementation to report the HAL error return state */
  246.  
  247.   /* USER CODE END Error_Handler_Debug */
  248. }
  249.  
  250. #ifdef  USE_FULL_ASSERT
  251. /**
  252.   * @brief  Reports the name of the source file and the source line number
  253.   *         where the assert_param error has occurred.
  254.   * @param  file: pointer to the source file name
  255.   * @param  line: assert_param error line source number
  256.   * @retval None
  257.   */
  258. void assert_failed(char *file, uint32_t line)
  259. {
  260.   /* USER CODE BEGIN 6 */
  261.   /* User can add his own implementation to report the file name and line number,
  262.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  263.   /* USER CODE END 6 */
  264. }
  265. #endif /* USE_FULL_ASSERT */
  266.  
  267. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement