PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/nucleo-f103RB/src/ch7/main-ex1.c

https://bitbucket.org/teknohan/mastering-stm32-book-example
C | 155 lines | 51 code | 33 blank | 71 comment | 0 complexity | f71bd43f56622e2c2e3c37c4278f4a2f MD5 | raw file
Possible License(s): GPL-3.0
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2015 Carmine Noviello
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f1xx_hal_conf.h"
  35. #include "stm32f1xx_hal.h"
  36. /* USER CODE BEGIN Includes */
  37. /* USER CODE END Includes */
  38. /* Private variables ---------------------------------------------------------*/
  39. UART_HandleTypeDef huart2;
  40. /* USER CODE BEGIN PV */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE END PV */
  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. /* USER CODE BEGIN PFP */
  46. /* Private function prototypes -----------------------------------------------*/
  47. /* USER CODE END PFP */
  48. /* USER CODE BEGIN 0 */
  49. /* USER CODE END 0 */
  50. int main(void) {
  51. GPIO_InitTypeDef GPIO_InitStruct;
  52. HAL_Init();
  53. /* GPIO Ports Clock Enable */
  54. __GPIOC_CLK_ENABLE();
  55. __GPIOA_CLK_ENABLE();
  56. /*Configure GPIO pin : PC13 */
  57. GPIO_InitStruct.Pin = B1_Pin;
  58. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  59. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  60. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  61. /*Configure GPIO pin : PA5 */
  62. GPIO_InitStruct.Pin = LD2_Pin;
  63. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  64. GPIO_InitStruct.Pull = GPIO_NOPULL;
  65. GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  66. HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  67. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  68. while(1);
  69. }
  70. void EXTI15_10_IRQHandler(void) {
  71. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_13);
  72. HAL_GPIO_TogglePin(LD2_GPIO_Port, GPIO_PIN_5);
  73. }
  74. /** System Clock Configuration
  75. */
  76. void SystemClock_Config(void)
  77. {
  78. RCC_OscInitTypeDef RCC_OscInitStruct;
  79. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  80. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  81. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  82. RCC_OscInitStruct.HSICalibrationValue = 16;
  83. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  84. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  85. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  86. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  87. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  88. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  89. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  90. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  91. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  92. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  93. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  94. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  95. /* SysTick_IRQn interrupt configuration */
  96. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  97. }
  98. /* USER CODE BEGIN 4 */
  99. /* USER CODE END 4 */
  100. #ifdef USE_FULL_ASSERT
  101. /**
  102. * @brief Reports the name of the source file and the source line number
  103. * where the assert_param error has occurred.
  104. * @param file: pointer to the source file name
  105. * @param line: assert_param error line source number
  106. * @retval None
  107. */
  108. void assert_failed(uint8_t* file, uint32_t line)
  109. {
  110. /* USER CODE BEGIN 6 */
  111. /* User can add his own implementation to report the file name and line number,
  112. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  113. /* USER CODE END 6 */
  114. }
  115. #endif
  116. /**
  117. * @}
  118. */
  119. /**
  120. * @}
  121. */
  122. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/