Mercurial > pub > halpp
comparison l476rg/Src/main.c @ 0:32a3b1785697
a rough draft of Hardware Abstraction Layer for C++
STM32L476RG drivers
| author | cin |
|---|---|
| date | Thu, 12 Jan 2017 02:45:43 +0300 |
| parents | |
| children | 0c59e7a7782a |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:32a3b1785697 |
|---|---|
| 1 /** | |
| 2 ****************************************************************************** | |
| 3 * File Name : main.c | |
| 4 * Description : Main program body | |
| 5 ****************************************************************************** | |
| 6 * | |
| 7 * COPYRIGHT(c) 2017 STMicroelectronics | |
| 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 "main.h" | |
| 35 #include "stm32l4xx_hal.h" | |
| 36 #include "tim.h" | |
| 37 #include "usart.h" | |
| 38 #include "gpio.h" | |
| 39 | |
| 40 /* USER CODE BEGIN Includes */ | |
| 41 | |
| 42 /* USER CODE END Includes */ | |
| 43 | |
| 44 /* Private variables ---------------------------------------------------------*/ | |
| 45 | |
| 46 /* USER CODE BEGIN PV */ | |
| 47 /* Private variables ---------------------------------------------------------*/ | |
| 48 | |
| 49 /* USER CODE END PV */ | |
| 50 | |
| 51 /* Private function prototypes -----------------------------------------------*/ | |
| 52 void SystemClock_Config(void); | |
| 53 void Error_Handler(void); | |
| 54 | |
| 55 /* USER CODE BEGIN PFP */ | |
| 56 /* Private function prototypes -----------------------------------------------*/ | |
| 57 | |
| 58 /* USER CODE END PFP */ | |
| 59 | |
| 60 /* USER CODE BEGIN 0 */ | |
| 61 | |
| 62 /* USER CODE END 0 */ | |
| 63 | |
| 64 int main(void) | |
| 65 { | |
| 66 | |
| 67 /* USER CODE BEGIN 1 */ | |
| 68 | |
| 69 /* USER CODE END 1 */ | |
| 70 | |
| 71 /* MCU Configuration----------------------------------------------------------*/ | |
| 72 | |
| 73 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ | |
| 74 HAL_Init(); | |
| 75 | |
| 76 /* Configure the system clock */ | |
| 77 SystemClock_Config(); | |
| 78 | |
| 79 /* Initialize all configured peripherals */ | |
| 80 MX_GPIO_Init(); | |
| 81 MX_USART2_UART_Init(); | |
| 82 MX_TIM2_Init(); | |
| 83 | |
| 84 /* USER CODE BEGIN 2 */ | |
| 85 | |
| 86 /* USER CODE END 2 */ | |
| 87 | |
| 88 /* Infinite loop */ | |
| 89 /* USER CODE BEGIN WHILE */ | |
| 90 while (1) | |
| 91 { | |
| 92 /* USER CODE END WHILE */ | |
| 93 | |
| 94 /* USER CODE BEGIN 3 */ | |
| 95 | |
| 96 } | |
| 97 /* USER CODE END 3 */ | |
| 98 | |
| 99 } | |
| 100 | |
| 101 /** System Clock Configuration | |
| 102 */ | |
| 103 void SystemClock_Config(void) | |
| 104 { | |
| 105 | |
| 106 RCC_OscInitTypeDef RCC_OscInitStruct; | |
| 107 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
| 108 RCC_PeriphCLKInitTypeDef PeriphClkInit; | |
| 109 | |
| 110 /**Initializes the CPU, AHB and APB busses clocks | |
| 111 */ | |
| 112 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | |
| 113 RCC_OscInitStruct.HSIState = RCC_HSI_ON; | |
| 114 RCC_OscInitStruct.HSICalibrationValue = 16; | |
| 115 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
| 116 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; | |
| 117 RCC_OscInitStruct.PLL.PLLM = 1; | |
| 118 RCC_OscInitStruct.PLL.PLLN = 10; | |
| 119 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7; | |
| 120 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; | |
| 121 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; | |
| 122 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) | |
| 123 { | |
| 124 Error_Handler(); | |
| 125 } | |
| 126 | |
| 127 /**Initializes the CPU, AHB and APB busses clocks | |
| 128 */ | |
| 129 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK | |
| 130 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; | |
| 131 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
| 132 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | |
| 133 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; | |
| 134 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | |
| 135 | |
| 136 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) | |
| 137 { | |
| 138 Error_Handler(); | |
| 139 } | |
| 140 | |
| 141 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2; | |
| 142 PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1; | |
| 143 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) | |
| 144 { | |
| 145 Error_Handler(); | |
| 146 } | |
| 147 | |
| 148 /**Configure the main internal regulator output voltage | |
| 149 */ | |
| 150 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) | |
| 151 { | |
| 152 Error_Handler(); | |
| 153 } | |
| 154 | |
| 155 /**Configure the Systick interrupt time | |
| 156 */ | |
| 157 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); | |
| 158 | |
| 159 /**Configure the Systick | |
| 160 */ | |
| 161 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); | |
| 162 | |
| 163 /* SysTick_IRQn interrupt configuration */ | |
| 164 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); | |
| 165 } | |
| 166 | |
| 167 /* USER CODE BEGIN 4 */ | |
| 168 | |
| 169 /* USER CODE END 4 */ | |
| 170 | |
| 171 /** | |
| 172 * @brief Period elapsed callback in non blocking mode | |
| 173 * @note This function is called when TIM6 interrupt took place, inside | |
| 174 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment | |
| 175 * a global variable "uwTick" used as application time base. | |
| 176 * @param htim : TIM handle | |
| 177 * @retval None | |
| 178 */ | |
| 179 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |
| 180 { | |
| 181 /* USER CODE BEGIN Callback 0 */ | |
| 182 | |
| 183 /* USER CODE END Callback 0 */ | |
| 184 if (htim->Instance == TIM6) { | |
| 185 HAL_IncTick(); | |
| 186 } | |
| 187 /* USER CODE BEGIN Callback 1 */ | |
| 188 | |
| 189 /* USER CODE END Callback 1 */ | |
| 190 } | |
| 191 | |
| 192 /** | |
| 193 * @brief This function is executed in case of error occurrence. | |
| 194 * @param None | |
| 195 * @retval None | |
| 196 */ | |
| 197 void Error_Handler(void) | |
| 198 { | |
| 199 /* USER CODE BEGIN Error_Handler */ | |
| 200 /* User can add his own implementation to report the HAL error return state */ | |
| 201 while(1) | |
| 202 { | |
| 203 } | |
| 204 /* USER CODE END Error_Handler */ | |
| 205 } | |
| 206 | |
| 207 #ifdef USE_FULL_ASSERT | |
| 208 | |
| 209 /** | |
| 210 * @brief Reports the name of the source file and the source line number | |
| 211 * where the assert_param error has occurred. | |
| 212 * @param file: pointer to the source file name | |
| 213 * @param line: assert_param error line source number | |
| 214 * @retval None | |
| 215 */ | |
| 216 void assert_failed(uint8_t* file, uint32_t line) | |
| 217 { | |
| 218 /* USER CODE BEGIN 6 */ | |
| 219 /* User can add his own implementation to report the file name and line number, | |
| 220 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
| 221 /* USER CODE END 6 */ | |
| 222 | |
| 223 } | |
| 224 | |
| 225 #endif | |
| 226 | |
| 227 /** | |
| 228 * @} | |
| 229 */ | |
| 230 | |
| 231 /** | |
| 232 * @} | |
| 233 */ | |
| 234 | |
| 235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
