31 #include "stm32l4xx_ll_system.h"
32 #include "stm32l4xx_ll_cortex.h"
43 QSPI_HandleTypeDef hqspi;
50 #ifdef HAL_ADC_MODULE_ENABLED
51 const PinMap PinMap_ADC[] = {
52 {PA_4, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 9, 0)},
53 {PC_0, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 1, 0)},
54 {PC_1, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 2, 0)},
55 {PC_2, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 3, 0)},
56 {PC_3, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 4, 0)},
61 #ifdef HAL_DAC_MODULE_ENABLED
62 const PinMap PinMap_DAC[] = {
67 #ifdef HAL_I2C_MODULE_ENABLED
68 const PinMap PinMap_I2C_SDA[] = {
69 {PB_7, I2C1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)},
70 {PB_14, I2C2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
73 const PinMap PinMap_I2C_SCL[] = {
74 {PB_6, I2C1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)},
75 {PB_10, I2C2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
80 #ifdef HAL_TIM_MODULE_ENABLED
81 const PinMap PinMap_TIM[] = {
86 #ifdef HAL_UART_MODULE_ENABLED
87 const PinMap PinMap_UART_TX[] = {
88 {PA_2_ALT1, USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
89 {PA_9, USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
92 const PinMap PinMap_UART_RX[] = {
93 {PA_10, USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
94 {PA_15, USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_USART2)},
97 const PinMap PinMap_UART_RTS[] = {
98 {PA_1, USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
101 const PinMap PinMap_UART_CTS[] = {
102 {PA_0, USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
107 #ifdef HAL_SPI_MODULE_ENABLED
108 const PinMap PinMap_SPI_MOSI[] = {
109 {PA_12, SPI1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
112 const PinMap PinMap_SPI_MISO[] = {
113 {PA_11, SPI1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
116 const PinMap PinMap_SPI_SCLK[] = {
117 {PA_5, SPI1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
120 const PinMap PinMap_SPI_SSEL[] = {
125 #ifdef HAL_CAN_MODULE_ENABLED
126 const PinMap PinMap_CAN_RD[] = {
127 {PB_12, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_CAN1)},
130 const PinMap PinMap_CAN_TD[] = {
131 {PB_13, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_CAN1)},
136 #ifdef HAL_QSPI_MODULE_ENABLED
137 const PinMap PinMap_QUADSPI_DATA0[] = {
138 {PB_1, QUADSPI, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_QUADSPI)},
141 const PinMap PinMap_QUADSPI_DATA1[] = {
142 {PB_0, QUADSPI, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_QUADSPI)},
145 const PinMap PinMap_QUADSPI_DATA2[] = {
146 {PA_7, QUADSPI, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_QUADSPI)},
149 const PinMap PinMap_QUADSPI_DATA3[] = {
150 {PA_6, QUADSPI, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_QUADSPI)},
153 const PinMap PinMap_QUADSPI_SCLK[] = {
154 {PA_3, QUADSPI, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_QUADSPI)},
157 const PinMap PinMap_QUADSPI_SSEL[] = {
158 {PB_11, QUADSPI, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_QUADSPI)},
163 #if defined(HAL_PCD_MODULE_ENABLED) || defined(HAL_HCD_MODULE_ENABLED)
164 const PinMap PinMap_USB[] = {
169 #ifdef HAL_SD_MODULE_ENABLED
170 const PinMap PinMap_SD[] = {
185 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
186 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
187 RCC_CRSInitTypeDef RCC_CRSInitStruct = {0};
191 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
198 HAL_PWR_EnableBkUpAccess();
199 __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
204 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;
205 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
206 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
207 RCC_OscInitStruct.MSICalibrationValue = 0;
208 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
209 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
210 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
211 RCC_OscInitStruct.PLL.PLLM = 1;
212 RCC_OscInitStruct.PLL.PLLN = 40;
213 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
214 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
215 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
216 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
223 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
224 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
225 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
226 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
227 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
228 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
230 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
237 HAL_RCCEx_EnableMSIPLLMode();
241 __HAL_RCC_CRS_CLK_ENABLE();
245 RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;
246 RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_LSE;
247 RCC_CRSInitStruct.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
248 RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000,32768);
249 RCC_CRSInitStruct.ErrorLimitValue = 34;
250 RCC_CRSInitStruct.HSI48CalibrationValue = 32;
252 HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);
258 FLASH_OBProgramInitTypeDef OBInit;
261 OBInit.WRPArea = OB_WRPAREA_BANK1_AREAA;
262 OBInit.PCROPConfig = FLASH_BANK_1;
263 HAL_FLASHEx_OBGetConfig(&OBInit);
266 if ((OBInit.USERConfig & OB_IWDG_STOP_RUN) == OB_IWDG_STOP_RUN) {
267 OBInit.OptionType = OPTIONBYTE_USER;
268 OBInit.USERType = OB_USER_IWDG_STOP | OB_USER_IWDG_STDBY;
269 OBInit.USERConfig = OB_IWDG_STOP_FREEZE | OB_IWDG_STDBY_FREEZE;
272 HAL_FLASH_OB_Unlock();
273 HAL_FLASHEx_OBProgram(&OBInit);
274 HAL_FLASH_OB_Launch();
280 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
282 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
285 LL_HANDLER_EnableFault(LL_HANDLER_FAULT_USG);
286 LL_HANDLER_EnableFault(LL_HANDLER_FAULT_BUS);
287 LL_HANDLER_EnableFault(LL_HANDLER_FAULT_MEM);
299 HAL_PWREx_EnableBatteryCharging(PWR_BATTERY_CHARGING_RESISTOR_5);
305 for(uint8_t uid=0; uid<12; uid++) {
306 ptrCpuId[uid++] = (uint8_t)(READ_REG(*((uint32_t *)(
UID_BASE_ADDRESS + uid))));
313 volatile uint64_t serNumb = 0;
314 uint8_t *ptrData = (uint8_t*)&serNumb;
335 CAN_FilterTypeDef CAN_FilterInitStruct;
338 hcan1.Instance = CAN1;
339 hcan1.Init.Prescaler = 8;
340 hcan1.Init.Mode = CAN_MODE_NORMAL;
341 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
342 hcan1.Init.TimeSeg1 = CAN_BS1_8TQ;
343 hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
344 hcan1.Init.TimeTriggeredMode = DISABLE;
345 hcan1.Init.AutoBusOff = DISABLE;
346 hcan1.Init.AutoWakeUp = DISABLE;
347 hcan1.Init.AutoRetransmission = DISABLE;
348 hcan1.Init.ReceiveFifoLocked = DISABLE;
349 hcan1.Init.TransmitFifoPriority = DISABLE;
350 if (HAL_CAN_Init(&
hcan1) != HAL_OK) {
355 CAN_FilterInitStruct.FilterIdHigh = 0x0000;
356 CAN_FilterInitStruct.FilterIdLow = 0x0000;
357 CAN_FilterInitStruct.FilterMaskIdHigh = 0x0000;
358 CAN_FilterInitStruct.FilterMaskIdLow = 0x0000;
359 CAN_FilterInitStruct.FilterFIFOAssignment = CAN_RX_FIFO0;
360 CAN_FilterInitStruct.FilterBank = 0;
361 CAN_FilterInitStruct.FilterMode = CAN_FILTERMODE_IDMASK;
362 CAN_FilterInitStruct.FilterScale = CAN_FILTERSCALE_32BIT;
363 CAN_FilterInitStruct.FilterActivation = ENABLE;
366 if (HAL_CAN_ConfigFilter(&
hcan1, &CAN_FilterInitStruct) != HAL_OK) {
374 extern "C" void MX_QUADSPI_Init(
void)
384 hqspi.Instance = QUADSPI;
385 hqspi.Init.ClockPrescaler = 1;
386 hqspi.Init.FifoThreshold = 4;
387 hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
388 hqspi.Init.FlashSize = 20;
389 hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
390 hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
391 hqspi.Init.FlashID = QSPI_FLASH_ID_1;
392 hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
393 if (HAL_QSPI_Init(&hqspi) != HAL_OK)
406 GPIO_InitTypeDef GPIO_InitStruct = {0};
409 __HAL_RCC_GPIOC_CLK_ENABLE();
410 __HAL_RCC_GPIOA_CLK_ENABLE();
411 __HAL_RCC_GPIOB_CLK_ENABLE();
412 __HAL_RCC_GPIOD_CLK_ENABLE();
413 #ifdef STIMAV4_SLAVE_HW_VER_01_01
414 __HAL_RCC_GPIOH_CLK_ENABLE();
423 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
424 GPIO_InitStruct.Pull = GPIO_NOPULL;
425 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
426 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
430 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
431 GPIO_InitStruct.Pull = GPIO_NOPULL;
432 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
436 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
437 GPIO_InitStruct.Pull = GPIO_NOPULL;
438 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
439 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
443 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
444 GPIO_InitStruct.Pull = GPIO_NOPULL;
449 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
450 GPIO_InitStruct.Pull = GPIO_NOPULL;
451 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
454 #ifdef STIMAV4_SLAVE_HW_VER_01_01
456 GPIO_InitStruct.Pin = IN_BTN_Pin;
457 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
458 GPIO_InitStruct.Pull = GPIO_NOPULL;
459 HAL_GPIO_Init(IN_BTN_GPIO_Port, &GPIO_InitStruct);
486 PWR_PVDTypeDef sConfigPVD = {0};
489 if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) {
490 LL_DBGMCU_EnableDBGStopMode();
492 LL_DBGMCU_DisableDBGStopMode();
495 __HAL_RCC_SYSCFG_CLK_ENABLE();
496 __HAL_RCC_PWR_CLK_ENABLE();
500 sConfigPVD.PVDLevel = PWR_PVDLEVEL_0;
501 sConfigPVD.Mode = PWR_PVD_MODE_NORMAL;
502 HAL_PWR_ConfigPVD(&sConfigPVD);
518 GPIO_InitTypeDef GPIO_InitStruct = {0};
519 if(hcan->Instance==CAN1)
526 __HAL_RCC_GPIOB_CLK_ENABLE();
529 __HAL_RCC_CAN1_CLK_ENABLE();
533 GPIO_InitStruct.Pin = GPIO_PIN_12;
534 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
535 GPIO_InitStruct.Pull = GPIO_NOPULL;
536 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
537 GPIO_InitStruct.Alternate = GPIO_AF10_CAN1;
538 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
540 GPIO_InitStruct.Pin = GPIO_PIN_13;
541 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
542 GPIO_InitStruct.Pull = GPIO_PULLUP;
543 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
544 GPIO_InitStruct.Alternate = GPIO_AF10_CAN1;
545 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
556 if(hcan->Instance==CAN1)
562 __HAL_RCC_CAN1_CLK_DISABLE();
568 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);
581 extern "C" void HAL_QSPI_MspInit(QSPI_HandleTypeDef* hqspi)
583 GPIO_InitTypeDef GPIO_InitStruct = {0};
584 if(hqspi->Instance==QUADSPI)
590 __HAL_RCC_QSPI_CLK_ENABLE();
592 __HAL_RCC_GPIOA_CLK_ENABLE();
593 __HAL_RCC_GPIOB_CLK_ENABLE();
602 GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7;
603 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
604 GPIO_InitStruct.Pull = GPIO_NOPULL;
605 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
606 GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
607 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
609 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_11;
610 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
611 GPIO_InitStruct.Pull = GPIO_NOPULL;
612 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
613 GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
614 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
617 HAL_NVIC_SetPriority(QUADSPI_IRQn, QSPI_NVIC_INT_PREMPT_PRIORITY, 0);
618 HAL_NVIC_EnableIRQ(QUADSPI_IRQn);
628 void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef* hqspi)
630 if(hqspi->Instance==QUADSPI)
636 __HAL_RCC_QSPI_CLK_DISABLE();
646 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7);
648 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_11);
651 HAL_NVIC_DisableIRQ(QUADSPI_IRQn);
#define MODULE_TYPE
Type of module. It is defined in registers.h.
void MX_CAN1_Init(void)
CAN1 Initialization Function.
void SetupSystemPeripheral(void)
Startup PeripheralConfig Local Board.
void STM32L4GetCPUID(uint8_t *ptrCpuId)
Get Unique ID HW of CPU (SerialNumber Unique ID)
uint64_t StimaV4GetSerialNumber(void)
Get StimaV4 Serial Number from UID Cpu and Module TYPE.
void SystemClock_Config(void)
System Clock Configuration.
void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
CAN MSP De-Initialization. This function freeze the hardware resources used in this example.
void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
CAN MSP Initialization. This function configures the hardware resources used in this example.
void MX_GPIO_Init(void)
GPIO Initialization Function.
void HAL_MspInit(void)
Initializes the Global MSP.
Interface STM32 hardware_hal STIMAV4 Header config.
#define EN_SPLY_GPIO_Port