/examples/i2c/stm32f1/Src/main.c

https://github.com/afiskon/stm32-ssd1306 · C · 224 lines · 90 code · 54 blank · 80 comment · 8 complexity · 3e65201bae22d7f304235e5a53710e18 MD5 · raw file

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f1xx_hal.h"
  4. /* USER CODE BEGIN Includes */
  5. #include "ssd1306_tests.h"
  6. /* USER CODE END Includes */
  7. /* Private variables ---------------------------------------------------------*/
  8. I2C_HandleTypeDef hi2c1;
  9. /* USER CODE BEGIN PV */
  10. /* Private variables ---------------------------------------------------------*/
  11. /* USER CODE END PV */
  12. /* Private function prototypes -----------------------------------------------*/
  13. void SystemClock_Config(void);
  14. static void MX_GPIO_Init(void);
  15. static void MX_I2C1_Init(void);
  16. /* USER CODE BEGIN PFP */
  17. /* Private function prototypes -----------------------------------------------*/
  18. /* USER CODE END PFP */
  19. /* USER CODE BEGIN 0 */
  20. void init() {
  21. ssd1306_TestAll();
  22. }
  23. void loop() {
  24. HAL_Delay(100);
  25. }
  26. /* USER CODE END 0 */
  27. int main(void)
  28. {
  29. /* USER CODE BEGIN 1 */
  30. /* USER CODE END 1 */
  31. /* MCU Configuration----------------------------------------------------------*/
  32. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  33. HAL_Init();
  34. /* USER CODE BEGIN Init */
  35. /* USER CODE END Init */
  36. /* Configure the system clock */
  37. SystemClock_Config();
  38. /* USER CODE BEGIN SysInit */
  39. /* USER CODE END SysInit */
  40. /* Initialize all configured peripherals */
  41. MX_GPIO_Init();
  42. MX_I2C1_Init();
  43. /* USER CODE BEGIN 2 */
  44. /* USER CODE END 2 */
  45. /* Infinite loop */
  46. /* USER CODE BEGIN WHILE */
  47. init();
  48. while (1)
  49. {
  50. loop();
  51. /* USER CODE END WHILE */
  52. /* USER CODE BEGIN 3 */
  53. }
  54. /* USER CODE END 3 */
  55. }
  56. /** System Clock Configuration
  57. */
  58. void SystemClock_Config(void)
  59. {
  60. RCC_OscInitTypeDef RCC_OscInitStruct;
  61. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  62. /**Initializes the CPU, AHB and APB busses clocks
  63. */
  64. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  65. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  66. RCC_OscInitStruct.HSICalibrationValue = 16;
  67. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  68. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  69. {
  70. _Error_Handler(__FILE__, __LINE__);
  71. }
  72. /**Initializes the CPU, AHB and APB busses clocks
  73. */
  74. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  75. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  76. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  77. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  78. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  79. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  80. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  81. {
  82. _Error_Handler(__FILE__, __LINE__);
  83. }
  84. /**Configure the Systick interrupt time
  85. */
  86. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  87. /**Configure the Systick
  88. */
  89. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  90. /* SysTick_IRQn interrupt configuration */
  91. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  92. }
  93. /* I2C1 init function */
  94. static void MX_I2C1_Init(void)
  95. {
  96. hi2c1.Instance = I2C1;
  97. hi2c1.Init.ClockSpeed = 100000*4;
  98. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  99. hi2c1.Init.OwnAddress1 = 0;
  100. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  101. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  102. hi2c1.Init.OwnAddress2 = 0;
  103. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  104. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  105. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  106. {
  107. _Error_Handler(__FILE__, __LINE__);
  108. }
  109. }
  110. /** Configure pins as
  111. * Analog
  112. * Input
  113. * Output
  114. * EVENT_OUT
  115. * EXTI
  116. */
  117. static void MX_GPIO_Init(void)
  118. {
  119. GPIO_InitTypeDef GPIO_InitStruct;
  120. /* GPIO Ports Clock Enable */
  121. __HAL_RCC_GPIOC_CLK_ENABLE();
  122. __HAL_RCC_GPIOA_CLK_ENABLE();
  123. __HAL_RCC_GPIOB_CLK_ENABLE();
  124. /*Configure GPIO pin Output Level */
  125. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  126. /*Configure GPIO pin : PC13 */
  127. GPIO_InitStruct.Pin = GPIO_PIN_13;
  128. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  129. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  130. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  131. }
  132. /* USER CODE BEGIN 4 */
  133. /* USER CODE END 4 */
  134. /**
  135. * @brief This function is executed in case of error occurrence.
  136. * @param None
  137. * @retval None
  138. */
  139. void _Error_Handler(char * file, int line)
  140. {
  141. /* USER CODE BEGIN Error_Handler_Debug */
  142. /* User can add his own implementation to report the HAL error return state */
  143. while(1)
  144. {
  145. }
  146. /* USER CODE END Error_Handler_Debug */
  147. }
  148. #ifdef USE_FULL_ASSERT
  149. /**
  150. * @brief Reports the name of the source file and the source line number
  151. * where the assert_param error has occurred.
  152. * @param file: pointer to the source file name
  153. * @param line: assert_param error line source number
  154. * @retval None
  155. */
  156. void assert_failed(uint8_t* file, uint32_t line)
  157. {
  158. /* USER CODE BEGIN 6 */
  159. /* User can add his own implementation to report the file name and line number,
  160. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  161. /* USER CODE END 6 */
  162. }
  163. #endif
  164. /**
  165. * @}
  166. */
  167. /**
  168. * @}
  169. */
  170. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/