PageRenderTime 34ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/nucleo-f401RE/src/ch5/main-ex1.c

https://bitbucket.org/teknohan/mastering-stm32-book-example
C | 167 lines | 69 code | 30 blank | 68 comment | 0 complexity | 614e46fd84615543bead93ddf88614df 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 "stm32f4xx_hal.h"
  35. #include "diag/Trace.h"
  36. void SystemClock_Config(void);
  37. static void MX_GPIO_Init(void);
  38. int main(void)
  39. {
  40. char msg[] = "Hello STM32 lovers!\n";
  41. HAL_Init();
  42. SystemClock_Config();
  43. MX_GPIO_Init();
  44. trace_printf(msg);
  45. while(1) {
  46. HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  47. HAL_Delay(500);
  48. }
  49. }
  50. /** System Clock Configuration
  51. */
  52. void SystemClock_Config(void)
  53. {
  54. RCC_OscInitTypeDef RCC_OscInitStruct;
  55. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  56. __PWR_CLK_ENABLE();
  57. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  58. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  59. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  60. RCC_OscInitStruct.HSICalibrationValue = 16;
  61. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  62. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  63. RCC_OscInitStruct.PLL.PLLM = 16;
  64. RCC_OscInitStruct.PLL.PLLN = 336;
  65. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  66. RCC_OscInitStruct.PLL.PLLQ = 7;
  67. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  68. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  69. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  70. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  71. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  72. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  73. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  74. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  75. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  76. }
  77. /** Configure pins as
  78. * Analog
  79. * Input
  80. * Output
  81. * EVENT_OUT
  82. * EXTI
  83. PA2 ------> USART2_TX
  84. PA3 ------> USART2_RX
  85. */
  86. void MX_GPIO_Init(void)
  87. {
  88. GPIO_InitTypeDef GPIO_InitStruct;
  89. /* GPIO Ports Clock Enable */
  90. __GPIOC_CLK_ENABLE();
  91. __GPIOH_CLK_ENABLE();
  92. __GPIOA_CLK_ENABLE();
  93. __GPIOB_CLK_ENABLE();
  94. /*Configure GPIO pin : PC13 */
  95. GPIO_InitStruct.Pin = GPIO_PIN_13;
  96. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  97. GPIO_InitStruct.Pull = GPIO_NOPULL;
  98. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  99. /*Configure GPIO pins : PA2 PA3 */
  100. GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
  101. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  102. GPIO_InitStruct.Pull = GPIO_NOPULL;
  103. GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  104. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  105. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  106. /*Configure GPIO pin : PA5 */
  107. GPIO_InitStruct.Pin = GPIO_PIN_5;
  108. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  109. GPIO_InitStruct.Pull = GPIO_NOPULL;
  110. GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  111. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  112. }
  113. /* USER CODE BEGIN 4 */
  114. /* USER CODE END 4 */
  115. #ifdef USE_FULL_ASSERT
  116. /**
  117. * @brief Reports the name of the source file and the source line number
  118. * where the assert_param error has occurred.
  119. * @param file: pointer to the source file name
  120. * @param line: assert_param error line source number
  121. * @retval None
  122. */
  123. void assert_failed(uint8_t* file, uint32_t line)
  124. {
  125. /* USER CODE BEGIN 6 */
  126. /* User can add his own implementation to report the file name and line number,
  127. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  128. /* USER CODE END 6 */
  129. }
  130. #endif
  131. /**
  132. * @}
  133. */
  134. /**
  135. * @}
  136. */
  137. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/