]> begriffs open source - cmsis-driver-validation/blob - Boards/Keil/MCBSTM32F400/main.c
Update GitHub Actions runner to ubuntu-22.04 (#18)
[cmsis-driver-validation] / Boards / Keil / MCBSTM32F400 / main.c
1 /*------------------------------------------------------------------------------
2  * Copyright (c) 2020 Arm Limited (or its affiliates). All
3  * rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *   1.Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *   2.Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   3.Neither the name of Arm nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *------------------------------------------------------------------------------
30  * Name:    main.c
31  * Purpose: Main module
32  *----------------------------------------------------------------------------*/
33
34 #include "main.h"
35
36 #include "cmsis_dv.h"
37
38 // Local functions
39 static void SystemClock_Config(void);
40 static void Error_Handler(void);
41
42
43 // Main function
44 int32_t main (void) {
45
46   // Hardware Abstraction Layer (HAL) initialization
47   (void)HAL_Init();
48
49   // System initialization
50   SystemClock_Config();
51   SystemCoreClockUpdate();
52
53   // Initialize kernel, create threads and start kernel
54   (void)osKernelInitialize();
55   osThreadNew(cmsis_dv, NULL, NULL);    // Create validation main thread
56   (void)osKernelStart();
57
58   for (;;) {}
59 }
60
61
62 #ifdef RTE_CMSIS_RTOS2_RTX5
63 /**
64   * Override default HAL_GetTick function
65   */
66 uint32_t HAL_GetTick (void) {
67   static uint32_t ticks = 0U;
68          uint32_t i, ret;
69
70   if (osKernelGetState () == osKernelRunning) {
71     ret = (uint32_t)osKernelGetTickCount ();
72   } else {
73     /* If Kernel is not running wait approximately 1 ms then increment 
74        and return auxiliary tick counter value */
75     for (i = (SystemCoreClock >> 14U); i > 0U; i--) {
76       __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
77       __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
78     }
79     ticks = ticks + 1U;
80     ret = ticks;
81   }
82
83   return ret;
84 }
85 #endif
86
87
88 /**
89   * @brief  System Clock Configuration
90   *         The system Clock is configured as follow :
91   *            System Clock source            = PLL (HSE)
92   *            SYSCLK(Hz)                     = 168000000
93   *            HCLK(Hz)                       = 168000000
94   *            AHB Prescaler                  = 1
95   *            APB1 Prescaler                 = 4
96   *            APB2 Prescaler                 = 2
97   *            HSE Frequency(Hz)              = 8000000
98   *            PLL_M                          = 25
99   *            PLL_N                          = 336
100   *            PLL_P                          = 2
101   *            PLL_Q                          = 7
102   *            VDD(V)                         = 3.3
103   *            Main regulator output voltage  = Scale1 mode
104   *            Flash Latency(WS)              = 5
105   * @param  None
106   * @retval None
107   */
108 static void SystemClock_Config(void)
109 {
110   RCC_ClkInitTypeDef RCC_ClkInitStruct;
111   RCC_OscInitTypeDef RCC_OscInitStruct;
112
113   /* Enable Power Control clock */
114   __HAL_RCC_PWR_CLK_ENABLE();
115
116   /* The voltage scaling allows optimizing the power consumption when the device is 
117      clocked below the maximum system frequency, to update the voltage scaling value 
118      regarding system frequency refer to product datasheet.  */
119   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
120
121   /* Enable HSE Oscillator and activate PLL with HSE as source */
122   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
123   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
124   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
125   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
126   RCC_OscInitStruct.PLL.PLLM = 25U;
127   RCC_OscInitStruct.PLL.PLLN = 336U;
128   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
129   RCC_OscInitStruct.PLL.PLLQ = 7U;
130   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
131   {
132     /* Initialization Error */
133     Error_Handler();
134   }
135
136   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
137      clocks dividers */
138   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
139   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
140   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
141   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
142   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
143   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
144   {
145     /* Initialization Error */
146     Error_Handler();
147   }
148
149   /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
150   if (HAL_GetREVID() == 0x1001)
151   {
152     /* Enable the Flash prefetch */
153     __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
154   }
155 }
156
157 /**
158   * @brief  This function is executed in case of error occurrence.
159   * @param  None
160   * @retval None
161   */
162 static void Error_Handler(void)
163 {
164   /* User may add here some code to deal with this error */
165   while(1)
166   {
167   }
168 }
169
170 #ifdef  USE_FULL_ASSERT
171
172 /**
173   * @brief  Reports the name of the source file and the source line number
174   *         where the assert_param error has occurred.
175   * @param  file: pointer to the source file name
176   * @param  line: assert_param error line source number
177   * @retval None
178   */
179 void assert_failed(uint8_t* file, uint32_t line)
180
181   /* User can add his own implementation to report the file name and line number,
182      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
183
184   /* Infinite loop */
185   while (1)
186   {
187   }
188 }
189
190 #endif
191
192 /**
193   * @}
194   */ 
195
196 /**
197   * @}
198   */ 
199
200 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/