]> begriffs open source - cmsis-driver-validation/blob - Tools/SPI_Server/Board/MCBSTM32F400/main.c
Update GitHub Actions runner to ubuntu-22.04 (#18)
[cmsis-driver-validation] / Tools / SPI_Server / Board / 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 <string.h>
37 #include <stdio.h>
38
39 #include "SPI_Server_Config.h"
40 #include "SPI_Server.h"
41
42 #include "stm32f4xx_hal.h"
43
44 // Local functions
45 static void SystemClock_Config(void);
46 static void Error_Handler(void);
47
48
49 // Main function
50 int32_t main (void) {
51
52   // Hardware Abstraction Layer (HAL) initialization
53   (void)HAL_Init();
54
55   // System initialization
56   SystemClock_Config();
57   SystemCoreClockUpdate();
58
59   // Initialize kernel, create threads and start kernel
60   (void)osKernelInitialize();
61   (void)SPI_Server_Start();
62   (void)osKernelStart();
63
64   for (;;) {}
65 }
66
67
68 #ifdef RTE_CMSIS_RTOS2_RTX5
69 /**
70   * Override default HAL_GetTick function
71   */
72 uint32_t HAL_GetTick (void) {
73   static uint32_t ticks = 0U;
74          uint32_t i, ret;
75
76   if (osKernelGetState () == osKernelRunning) {
77     ret = (uint32_t)osKernelGetTickCount ();
78   } else {
79     /* If Kernel is not running wait approximately 1 ms then increment 
80        and return auxiliary tick counter value */
81     for (i = (SystemCoreClock >> 14U); i > 0U; i--) {
82       __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
83       __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
84     }
85     ticks = ticks + 1U;
86     ret = ticks;
87   }
88
89   return ret;
90 }
91 #endif
92
93
94 /**
95   * @brief  System Clock Configuration
96   *         The system Clock is configured as follow :
97   *            System Clock source            = PLL (HSE)
98   *            SYSCLK(Hz)                     = 168000000
99   *            HCLK(Hz)                       = 168000000
100   *            AHB Prescaler                  = 1
101   *            APB1 Prescaler                 = 4
102   *            APB2 Prescaler                 = 2
103   *            HSE Frequency(Hz)              = 8000000
104   *            PLL_M                          = 25
105   *            PLL_N                          = 336
106   *            PLL_P                          = 2
107   *            PLL_Q                          = 7
108   *            VDD(V)                         = 3.3
109   *            Main regulator output voltage  = Scale1 mode
110   *            Flash Latency(WS)              = 5
111   * @param  None
112   * @retval None
113   */
114 static void SystemClock_Config(void)
115 {
116   RCC_ClkInitTypeDef RCC_ClkInitStruct;
117   RCC_OscInitTypeDef RCC_OscInitStruct;
118
119   /* Enable Power Control clock */
120   __HAL_RCC_PWR_CLK_ENABLE();
121
122   /* The voltage scaling allows optimizing the power consumption when the device is 
123      clocked below the maximum system frequency, to update the voltage scaling value 
124      regarding system frequency refer to product datasheet.  */
125   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
126
127   /* Enable HSE Oscillator and activate PLL with HSE as source */
128   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
129   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
130   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
131   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
132   RCC_OscInitStruct.PLL.PLLM = 25U;
133   RCC_OscInitStruct.PLL.PLLN = 336U;
134   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
135   RCC_OscInitStruct.PLL.PLLQ = 7U;
136   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
137   {
138     /* Initialization Error */
139     Error_Handler();
140   }
141
142   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
143      clocks dividers */
144   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
145   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
146   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
147   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
148   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
149   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
150   {
151     /* Initialization Error */
152     Error_Handler();
153   }
154
155   /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
156   if (HAL_GetREVID() == 0x1001)
157   {
158     /* Enable the Flash prefetch */
159     __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
160   }
161 }
162
163 /**
164   * @brief  This function is executed in case of error occurrence.
165   * @param  None
166   * @retval None
167   */
168 static __NO_RETURN void Error_Handler(void)
169 {
170   /* User may add here some code to deal with this error */
171   for (;;)
172   {
173   }
174 }
175
176 #ifdef  USE_FULL_ASSERT
177
178 /**
179   * @brief  Reports the name of the source file and the source line number
180   *         where the assert_param error has occurred.
181   * @param  file: pointer to the source file name
182   * @param  line: assert_param error line source number
183   * @retval None
184   */
185 void assert_failed(uint8_t* file, uint32_t line)
186
187   /* User can add his own implementation to report the file name and line number,
188      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
189
190   /* Infinite loop */
191   while (1)
192   {
193   }
194 }
195
196 #endif
197
198 /**
199   * @}
200   */ 
201
202 /**
203   * @}
204   */ 
205
206 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/