xref: /btstack/port/stm32-l073rz-nucleo-em9304/Src/main.c (revision c8dfe071e5be306bdac290dfbe6cbf2b9a446e88)
1e8380792SMatthias Ringwald /* USER CODE BEGIN Header */
2e8380792SMatthias Ringwald /**
3e8380792SMatthias Ringwald   ******************************************************************************
4e8380792SMatthias Ringwald   * @file           : main.c
5e8380792SMatthias Ringwald   * @brief          : Main program body
6e8380792SMatthias Ringwald   ******************************************************************************
7e8380792SMatthias Ringwald   * @attention
8e8380792SMatthias Ringwald   *
9e8380792SMatthias Ringwald   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10e8380792SMatthias Ringwald   * All rights reserved.</center></h2>
11e8380792SMatthias Ringwald   *
12e8380792SMatthias Ringwald   * This software component is licensed by ST under BSD 3-Clause license,
13e8380792SMatthias Ringwald   * the "License"; You may not use this file except in compliance with the
14e8380792SMatthias Ringwald   * License. You may obtain a copy of the License at:
15e8380792SMatthias Ringwald   *                        opensource.org/licenses/BSD-3-Clause
16e8380792SMatthias Ringwald   *
17e8380792SMatthias Ringwald   ******************************************************************************
18e8380792SMatthias Ringwald   */
19e8380792SMatthias Ringwald /* USER CODE END Header */
20e8380792SMatthias Ringwald 
21e8380792SMatthias Ringwald /* Includes ------------------------------------------------------------------*/
22e8380792SMatthias Ringwald #include "main.h"
23e8380792SMatthias Ringwald 
24e8380792SMatthias Ringwald /* Private includes ----------------------------------------------------------*/
25e8380792SMatthias Ringwald /* USER CODE BEGIN Includes */
26*c8dfe071SMatthias Ringwald #include "port.h"
27e8380792SMatthias Ringwald /* USER CODE END Includes */
28e8380792SMatthias Ringwald 
29e8380792SMatthias Ringwald /* Private typedef -----------------------------------------------------------*/
30e8380792SMatthias Ringwald /* USER CODE BEGIN PTD */
31e8380792SMatthias Ringwald 
32e8380792SMatthias Ringwald /* USER CODE END PTD */
33e8380792SMatthias Ringwald 
34e8380792SMatthias Ringwald /* Private define ------------------------------------------------------------*/
35e8380792SMatthias Ringwald /* USER CODE BEGIN PD */
36e8380792SMatthias Ringwald 
37e8380792SMatthias Ringwald /* USER CODE END PD */
38e8380792SMatthias Ringwald 
39e8380792SMatthias Ringwald /* Private macro -------------------------------------------------------------*/
40e8380792SMatthias Ringwald /* USER CODE BEGIN PM */
41e8380792SMatthias Ringwald 
42e8380792SMatthias Ringwald /* USER CODE END PM */
43e8380792SMatthias Ringwald 
44e8380792SMatthias Ringwald /* Private variables ---------------------------------------------------------*/
45e8380792SMatthias Ringwald SPI_HandleTypeDef hspi1;
46e8380792SMatthias Ringwald DMA_HandleTypeDef hdma_spi1_rx;
47e8380792SMatthias Ringwald DMA_HandleTypeDef hdma_spi1_tx;
48e8380792SMatthias Ringwald 
49e8380792SMatthias Ringwald UART_HandleTypeDef huart2;
50e8380792SMatthias Ringwald 
51e8380792SMatthias Ringwald /* USER CODE BEGIN PV */
52e8380792SMatthias Ringwald 
53e8380792SMatthias Ringwald /* USER CODE END PV */
54e8380792SMatthias Ringwald 
55e8380792SMatthias Ringwald /* Private function prototypes -----------------------------------------------*/
56e8380792SMatthias Ringwald void SystemClock_Config(void);
57e8380792SMatthias Ringwald static void MX_GPIO_Init(void);
58e8380792SMatthias Ringwald static void MX_DMA_Init(void);
59e8380792SMatthias Ringwald static void MX_SPI1_Init(void);
60e8380792SMatthias Ringwald static void MX_USART2_UART_Init(void);
61e8380792SMatthias Ringwald /* USER CODE BEGIN PFP */
62e8380792SMatthias Ringwald 
63e8380792SMatthias Ringwald /* USER CODE END PFP */
64e8380792SMatthias Ringwald 
65e8380792SMatthias Ringwald /* Private user code ---------------------------------------------------------*/
66e8380792SMatthias Ringwald /* USER CODE BEGIN 0 */
67e8380792SMatthias Ringwald 
68e8380792SMatthias Ringwald /* USER CODE END 0 */
69e8380792SMatthias Ringwald 
70e8380792SMatthias Ringwald /**
71e8380792SMatthias Ringwald   * @brief  The application entry point.
72e8380792SMatthias Ringwald   * @retval int
73e8380792SMatthias Ringwald   */
main(void)74e8380792SMatthias Ringwald int main(void)
75e8380792SMatthias Ringwald {
76e8380792SMatthias Ringwald   /* USER CODE BEGIN 1 */
77e8380792SMatthias Ringwald 
78e8380792SMatthias Ringwald   /* USER CODE END 1 */
79e8380792SMatthias Ringwald 
80e8380792SMatthias Ringwald   /* MCU Configuration--------------------------------------------------------*/
81e8380792SMatthias Ringwald 
82e8380792SMatthias Ringwald   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
83e8380792SMatthias Ringwald   HAL_Init();
84e8380792SMatthias Ringwald 
85e8380792SMatthias Ringwald   /* USER CODE BEGIN Init */
86e8380792SMatthias Ringwald 
87e8380792SMatthias Ringwald   /* USER CODE END Init */
88e8380792SMatthias Ringwald 
89e8380792SMatthias Ringwald   /* Configure the system clock */
90e8380792SMatthias Ringwald   SystemClock_Config();
91e8380792SMatthias Ringwald 
92e8380792SMatthias Ringwald   /* USER CODE BEGIN SysInit */
93e8380792SMatthias Ringwald 
94e8380792SMatthias Ringwald   /* USER CODE END SysInit */
95e8380792SMatthias Ringwald 
96e8380792SMatthias Ringwald   /* Initialize all configured peripherals */
97e8380792SMatthias Ringwald   MX_GPIO_Init();
98e8380792SMatthias Ringwald   MX_DMA_Init();
99e8380792SMatthias Ringwald   MX_SPI1_Init();
100e8380792SMatthias Ringwald   MX_USART2_UART_Init();
101e8380792SMatthias Ringwald   /* USER CODE BEGIN 2 */
102e8380792SMatthias Ringwald   // jump to BTstack port
103e8380792SMatthias Ringwald   port_main();
104e8380792SMatthias Ringwald   /* USER CODE END 2 */
105e8380792SMatthias Ringwald 
106e8380792SMatthias Ringwald   /* Infinite loop */
107e8380792SMatthias Ringwald   /* USER CODE BEGIN WHILE */
108e8380792SMatthias Ringwald   while (1)
109e8380792SMatthias Ringwald   {
110e8380792SMatthias Ringwald     /* USER CODE END WHILE */
111e8380792SMatthias Ringwald 
112e8380792SMatthias Ringwald     /* USER CODE BEGIN 3 */
113e8380792SMatthias Ringwald   }
114e8380792SMatthias Ringwald   /* USER CODE END 3 */
115e8380792SMatthias Ringwald }
116e8380792SMatthias Ringwald 
117e8380792SMatthias Ringwald /**
118e8380792SMatthias Ringwald   * @brief System Clock Configuration
119e8380792SMatthias Ringwald   * @retval None
120e8380792SMatthias Ringwald   */
SystemClock_Config(void)121e8380792SMatthias Ringwald void SystemClock_Config(void)
122e8380792SMatthias Ringwald {
123e8380792SMatthias Ringwald   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
124e8380792SMatthias Ringwald   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
125e8380792SMatthias Ringwald   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
126e8380792SMatthias Ringwald 
127e8380792SMatthias Ringwald   /** Configure the main internal regulator output voltage
128e8380792SMatthias Ringwald   */
129e8380792SMatthias Ringwald   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
130e8380792SMatthias Ringwald   /** Initializes the CPU, AHB and APB busses clocks
131e8380792SMatthias Ringwald   */
132e8380792SMatthias Ringwald   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
133e8380792SMatthias Ringwald   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
134e8380792SMatthias Ringwald   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
135e8380792SMatthias Ringwald   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
136e8380792SMatthias Ringwald   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
137e8380792SMatthias Ringwald   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
138e8380792SMatthias Ringwald   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
139e8380792SMatthias Ringwald   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
140e8380792SMatthias Ringwald   {
141e8380792SMatthias Ringwald     Error_Handler();
142e8380792SMatthias Ringwald   }
143e8380792SMatthias Ringwald   /** Initializes the CPU, AHB and APB busses clocks
144e8380792SMatthias Ringwald   */
145e8380792SMatthias Ringwald   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
146e8380792SMatthias Ringwald                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
147e8380792SMatthias Ringwald   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
148e8380792SMatthias Ringwald   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
149e8380792SMatthias Ringwald   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
150e8380792SMatthias Ringwald   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
151e8380792SMatthias Ringwald 
152e8380792SMatthias Ringwald   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
153e8380792SMatthias Ringwald   {
154e8380792SMatthias Ringwald     Error_Handler();
155e8380792SMatthias Ringwald   }
156e8380792SMatthias Ringwald   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
157e8380792SMatthias Ringwald   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
158e8380792SMatthias Ringwald   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
159e8380792SMatthias Ringwald   {
160e8380792SMatthias Ringwald     Error_Handler();
161e8380792SMatthias Ringwald   }
162e8380792SMatthias Ringwald }
163e8380792SMatthias Ringwald 
164e8380792SMatthias Ringwald /**
165e8380792SMatthias Ringwald   * @brief SPI1 Initialization Function
166e8380792SMatthias Ringwald   * @param None
167e8380792SMatthias Ringwald   * @retval None
168e8380792SMatthias Ringwald   */
MX_SPI1_Init(void)169e8380792SMatthias Ringwald static void MX_SPI1_Init(void)
170e8380792SMatthias Ringwald {
171e8380792SMatthias Ringwald 
172e8380792SMatthias Ringwald   /* USER CODE BEGIN SPI1_Init 0 */
173e8380792SMatthias Ringwald 
174e8380792SMatthias Ringwald   /* USER CODE END SPI1_Init 0 */
175e8380792SMatthias Ringwald 
176e8380792SMatthias Ringwald   /* USER CODE BEGIN SPI1_Init 1 */
177e8380792SMatthias Ringwald 
178e8380792SMatthias Ringwald   /* USER CODE END SPI1_Init 1 */
179e8380792SMatthias Ringwald   /* SPI1 parameter configuration*/
180e8380792SMatthias Ringwald   hspi1.Instance = SPI1;
181e8380792SMatthias Ringwald   hspi1.Init.Mode = SPI_MODE_MASTER;
182e8380792SMatthias Ringwald   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
183e8380792SMatthias Ringwald   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
184e8380792SMatthias Ringwald   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
185e8380792SMatthias Ringwald   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
186e8380792SMatthias Ringwald   hspi1.Init.NSS = SPI_NSS_SOFT;
187e8380792SMatthias Ringwald   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
188e8380792SMatthias Ringwald   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
189e8380792SMatthias Ringwald   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
190e8380792SMatthias Ringwald   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
191e8380792SMatthias Ringwald   hspi1.Init.CRCPolynomial = 7;
192e8380792SMatthias Ringwald   if (HAL_SPI_Init(&hspi1) != HAL_OK)
193e8380792SMatthias Ringwald   {
194e8380792SMatthias Ringwald     Error_Handler();
195e8380792SMatthias Ringwald   }
196e8380792SMatthias Ringwald   /* USER CODE BEGIN SPI1_Init 2 */
197e8380792SMatthias Ringwald 
198e8380792SMatthias Ringwald   /* USER CODE END SPI1_Init 2 */
199e8380792SMatthias Ringwald 
200e8380792SMatthias Ringwald }
201e8380792SMatthias Ringwald 
202e8380792SMatthias Ringwald /**
203e8380792SMatthias Ringwald   * @brief USART2 Initialization Function
204e8380792SMatthias Ringwald   * @param None
205e8380792SMatthias Ringwald   * @retval None
206e8380792SMatthias Ringwald   */
MX_USART2_UART_Init(void)207e8380792SMatthias Ringwald static void MX_USART2_UART_Init(void)
208e8380792SMatthias Ringwald {
209e8380792SMatthias Ringwald 
210e8380792SMatthias Ringwald   /* USER CODE BEGIN USART2_Init 0 */
211e8380792SMatthias Ringwald 
212e8380792SMatthias Ringwald   /* USER CODE END USART2_Init 0 */
213e8380792SMatthias Ringwald 
214e8380792SMatthias Ringwald   /* USER CODE BEGIN USART2_Init 1 */
215e8380792SMatthias Ringwald 
216e8380792SMatthias Ringwald   /* USER CODE END USART2_Init 1 */
217e8380792SMatthias Ringwald   huart2.Instance = USART2;
218e8380792SMatthias Ringwald   huart2.Init.BaudRate = 115200;
219e8380792SMatthias Ringwald   huart2.Init.WordLength = UART_WORDLENGTH_8B;
220e8380792SMatthias Ringwald   huart2.Init.StopBits = UART_STOPBITS_1;
221e8380792SMatthias Ringwald   huart2.Init.Parity = UART_PARITY_NONE;
222e8380792SMatthias Ringwald   huart2.Init.Mode = UART_MODE_TX_RX;
223e8380792SMatthias Ringwald   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
224e8380792SMatthias Ringwald   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
225e8380792SMatthias Ringwald   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
226e8380792SMatthias Ringwald   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
227e8380792SMatthias Ringwald   if (HAL_UART_Init(&huart2) != HAL_OK)
228e8380792SMatthias Ringwald   {
229e8380792SMatthias Ringwald     Error_Handler();
230e8380792SMatthias Ringwald   }
231e8380792SMatthias Ringwald   /* USER CODE BEGIN USART2_Init 2 */
232e8380792SMatthias Ringwald 
233e8380792SMatthias Ringwald   /* USER CODE END USART2_Init 2 */
234e8380792SMatthias Ringwald 
235e8380792SMatthias Ringwald }
236e8380792SMatthias Ringwald 
237e8380792SMatthias Ringwald /**
238e8380792SMatthias Ringwald   * Enable DMA controller clock
239e8380792SMatthias Ringwald   */
MX_DMA_Init(void)240e8380792SMatthias Ringwald static void MX_DMA_Init(void)
241e8380792SMatthias Ringwald {
242e8380792SMatthias Ringwald   /* DMA controller clock enable */
243e8380792SMatthias Ringwald   __HAL_RCC_DMA1_CLK_ENABLE();
244e8380792SMatthias Ringwald 
245e8380792SMatthias Ringwald   /* DMA interrupt init */
246e8380792SMatthias Ringwald   /* DMA1_Channel2_3_IRQn interrupt configuration */
247e8380792SMatthias Ringwald   HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
248e8380792SMatthias Ringwald   HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
249e8380792SMatthias Ringwald 
250e8380792SMatthias Ringwald }
251e8380792SMatthias Ringwald 
252e8380792SMatthias Ringwald /**
253e8380792SMatthias Ringwald   * @brief GPIO Initialization Function
254e8380792SMatthias Ringwald   * @param None
255e8380792SMatthias Ringwald   * @retval None
256e8380792SMatthias Ringwald   */
MX_GPIO_Init(void)257e8380792SMatthias Ringwald static void MX_GPIO_Init(void)
258e8380792SMatthias Ringwald {
259e8380792SMatthias Ringwald   GPIO_InitTypeDef GPIO_InitStruct = {0};
260e8380792SMatthias Ringwald 
261e8380792SMatthias Ringwald   /* GPIO Ports Clock Enable */
262e8380792SMatthias Ringwald   __HAL_RCC_GPIOC_CLK_ENABLE();
263e8380792SMatthias Ringwald   __HAL_RCC_GPIOH_CLK_ENABLE();
264e8380792SMatthias Ringwald   __HAL_RCC_GPIOA_CLK_ENABLE();
265e8380792SMatthias Ringwald   __HAL_RCC_GPIOB_CLK_ENABLE();
266e8380792SMatthias Ringwald 
267e8380792SMatthias Ringwald   /*Configure GPIO pin Output Level */
268e8380792SMatthias Ringwald   HAL_GPIO_WritePin(GPIOB, EN_Pin|SPI1_CSN_Pin, GPIO_PIN_RESET);
269e8380792SMatthias Ringwald 
270e8380792SMatthias Ringwald   /*Configure GPIO pin : B1_Pin */
271e8380792SMatthias Ringwald   GPIO_InitStruct.Pin = B1_Pin;
272e8380792SMatthias Ringwald   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
273e8380792SMatthias Ringwald   GPIO_InitStruct.Pull = GPIO_NOPULL;
274e8380792SMatthias Ringwald   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
275e8380792SMatthias Ringwald 
276e8380792SMatthias Ringwald   /*Configure GPIO pins : EN_Pin SPI1_CSN_Pin */
277e8380792SMatthias Ringwald   GPIO_InitStruct.Pin = EN_Pin|SPI1_CSN_Pin;
278e8380792SMatthias Ringwald   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
279e8380792SMatthias Ringwald   GPIO_InitStruct.Pull = GPIO_NOPULL;
280e8380792SMatthias Ringwald   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
281e8380792SMatthias Ringwald   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
282e8380792SMatthias Ringwald 
283e8380792SMatthias Ringwald   /*Configure GPIO pin : SPI1_RDY_Pin */
284e8380792SMatthias Ringwald   GPIO_InitStruct.Pin = SPI1_RDY_Pin;
285e8380792SMatthias Ringwald   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
286e8380792SMatthias Ringwald   GPIO_InitStruct.Pull = GPIO_NOPULL;
287e8380792SMatthias Ringwald   HAL_GPIO_Init(SPI1_RDY_GPIO_Port, &GPIO_InitStruct);
288e8380792SMatthias Ringwald 
289e8380792SMatthias Ringwald }
290e8380792SMatthias Ringwald 
291e8380792SMatthias Ringwald /* USER CODE BEGIN 4 */
292e8380792SMatthias Ringwald 
293e8380792SMatthias Ringwald /* USER CODE END 4 */
294e8380792SMatthias Ringwald 
295e8380792SMatthias Ringwald /**
296e8380792SMatthias Ringwald   * @brief  This function is executed in case of error occurrence.
297e8380792SMatthias Ringwald   * @retval None
298e8380792SMatthias Ringwald   */
Error_Handler(void)299e8380792SMatthias Ringwald void Error_Handler(void)
300e8380792SMatthias Ringwald {
301e8380792SMatthias Ringwald   /* USER CODE BEGIN Error_Handler_Debug */
302e8380792SMatthias Ringwald   /* User can add his own implementation to report the HAL error return state */
303e8380792SMatthias Ringwald 
304e8380792SMatthias Ringwald   /* USER CODE END Error_Handler_Debug */
305e8380792SMatthias Ringwald }
306e8380792SMatthias Ringwald 
307e8380792SMatthias Ringwald #ifdef  USE_FULL_ASSERT
308e8380792SMatthias Ringwald /**
309e8380792SMatthias Ringwald   * @brief  Reports the name of the source file and the source line number
310e8380792SMatthias Ringwald   *         where the assert_param error has occurred.
311e8380792SMatthias Ringwald   * @param  file: pointer to the source file name
312e8380792SMatthias Ringwald   * @param  line: assert_param error line source number
313e8380792SMatthias Ringwald   * @retval None
314e8380792SMatthias Ringwald   */
assert_failed(uint8_t * file,uint32_t line)315e8380792SMatthias Ringwald void assert_failed(uint8_t *file, uint32_t line)
316e8380792SMatthias Ringwald {
317e8380792SMatthias Ringwald   /* USER CODE BEGIN 6 */
318e8380792SMatthias Ringwald   /* User can add his own implementation to report the file name and line number,
319e8380792SMatthias Ringwald      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
320e8380792SMatthias Ringwald   /* USER CODE END 6 */
321e8380792SMatthias Ringwald }
322e8380792SMatthias Ringwald #endif /* USE_FULL_ASSERT */
323e8380792SMatthias Ringwald 
324e8380792SMatthias Ringwald /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
325