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