/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : custom.c * @brief : Source file for the BSP Common driver ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "custom.h" /** @defgroup BSP BSP * @{ */ /** @defgroup CUSTOM CUSTOM * @{ */ /** @defgroup CUSTOM_LOW_LEVEL CUSTOM LOW LEVEL * @brief This file provides set of firmware functions to manage Leds and push-button * available on STM32WBxx-Nucleo Kit from STMicroelectronics. * @{ */ /** * @} */ /** @defgroup CUSTOM_LOW_LEVEL_Private_Defines CUSTOM LOW LEVEL Private Defines * @{ */ /** @defgroup CUSTOM_LOW_LEVEL_FunctionPrototypes CUSTOM LOW LEVEL Private Function Prototypes * @{ */ /** * @} */ /** @defgroup CUSTOM_LOW_LEVEL_Private_Variables CUSTOM LOW LEVEL Private Variables * @{ */ typedef void (* BSP_LED_GPIO_Init) (void); static GPIO_TypeDef* LED_PORT[LEDn] = {LED2_GPIO_PORT}; static const uint16_t LED_PIN[LEDn] = {LED2_PIN}; static void LED_USER_GPIO_Init(void); USART_TypeDef* COM_USART[COMn] = {COM1_UART}; UART_HandleTypeDef hcom_uart[COMn]; #if (USE_COM_LOG > 0) static COM_TypeDef COM_ActiveLogPort; #endif #if (USE_HAL_UART_REGISTER_CALLBACKS == 1U) static uint32_t IslpUart1MspCbValid = 0; #endif __weak HAL_StatusTypeDef MX_LPUART1_UART_Init(UART_HandleTypeDef* huart); /** * @} */ /** @defgroup CUSTOM_LOW_LEVEL_Private_Functions CUSTOM LOW LEVEL Private Functions * @{ */ #if (USE_BSP_COM_FEATURE > 0) static void LPUART1_MspInit(UART_HandleTypeDef *huart); static void LPUART1_MspDeInit(UART_HandleTypeDef *huart); #endif /** * @brief This method returns the STM32WBxx NUCLEO BSP Driver revision * @retval version: 0xXYZR (8bits for each decimal, R for RC) */ int32_t BSP_GetVersion(void) { return (int32_t)__CUSTOM_BSP_VERSION; } /** * @brief Configures LED on GPIO and/or on MFX. * @param Led: LED to be configured. * This parameter can be one of the following values: * @arg LED2, LED4, ... * @retval HAL status */ int32_t BSP_LED_Init(Led_TypeDef Led) { static const BSP_LED_GPIO_Init LedGpioInit[LEDn] = {LED_USER_GPIO_Init}; LedGpioInit[Led](); return BSP_ERROR_NONE; } /** * @brief DeInit LEDs. * @param Led: LED to be configured. * This parameter can be one of the following values: * @arg LED2, LED4, ... * @note Led DeInit does not disable the GPIO clock nor disable the Mfx * @retval HAL status */ int32_t BSP_LED_DeInit(Led_TypeDef Led) { GPIO_InitTypeDef gpio_init_structure; /* Turn off LED */ HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET); /* DeInit the GPIO_LED pin */ gpio_init_structure.Pin = LED_PIN[Led]; HAL_GPIO_DeInit(LED_PORT[Led], gpio_init_structure.Pin); return BSP_ERROR_NONE; } /** * @brief Turns selected LED On. * @param Led: LED to be set on * This parameter can be one of the following values: * @arg LED1 * @arg LED2 * @arg LED3 * @arg LED4 * @retval HAL status */ int32_t BSP_LED_On(Led_TypeDef Led) { HAL_GPIO_WritePin(LED_PORT [Led], LED_PIN [Led], GPIO_PIN_SET); return BSP_ERROR_NONE; } /** * @brief Turns selected LED Off. * @param Led: LED to be set off * This parameter can be one of the following values: * @arg LED1 * @arg LED2 * @arg LED3 * @arg LED4 * @retval HAL status */ int32_t BSP_LED_Off(Led_TypeDef Led) { HAL_GPIO_WritePin(LED_PORT [Led], LED_PIN [Led], GPIO_PIN_RESET); return BSP_ERROR_NONE; } /** * @brief Toggles the selected LED. * @param Led: LED to be toggled * This parameter can be one of the following values: * @arg LED1 * @arg LED2 * @arg LED3 * @arg LED4 * @retval HAL status */ int32_t BSP_LED_Toggle(Led_TypeDef Led) { HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]); return BSP_ERROR_NONE; } /** * @brief Get the status of the LED. * @param Led: LED for which get the status * This parameter can be one of the following values: * @arg LED1 * @arg LED2 * @arg LED3 * @arg LED4 * @retval HAL status (1=high, 0=low) */ int32_t BSP_LED_GetState(Led_TypeDef Led) { return (int32_t)(HAL_GPIO_ReadPin (LED_PORT [Led], LED_PIN [Led]) == GPIO_PIN_RESET); } /** * @brief * @retval None */ static void LED_USER_GPIO_Init(void) { /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOB_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(BUS_BSP_LED_GPIO_PORT, BUS_BSP_LED_GPIO_PIN, GPIO_PIN_RESET); /*Configure GPIO pin : PTPIN */ GPIO_InitStruct.Pin = BUS_BSP_LED_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(BUS_BSP_LED_GPIO_PORT, &GPIO_InitStruct); } #if (USE_BSP_COM_FEATURE > 0) /** * @brief Configures COM port. * @param COM: COM port to be configured. * This parameter can be COM1 * @param UART_Init: Pointer to a UART_HandleTypeDef structure that contains the * configuration information for the specified USART peripheral. * @retval BSP error code */ int32_t BSP_COM_Init(COM_TypeDef COM) { int32_t ret = BSP_ERROR_NONE; if(COM > COMn) { ret = BSP_ERROR_WRONG_PARAM; } else { hcom_uart[COM].Instance = COM_USART[COM]; #if (USE_HAL_UART_REGISTER_CALLBACKS == 0U) /* Init the UART Msp */ LPUART1_MspInit(&hcom_uart[COM]); #else if(IslpUart1MspCbValid == 0U) { if(BSP_COM_RegisterDefaultMspCallbacks(COM) != BSP_ERROR_NONE) { return BSP_ERROR_MSP_FAILURE; } } #endif if (MX_LPUART1_UART_Init(&hcom_uart[COM])) { ret = BSP_ERROR_PERIPH_FAILURE; } } return ret; } /** * @brief DeInit COM port. * @param COM COM port to be configured. * This parameter can be COM1 * @retval BSP status */ int32_t BSP_COM_DeInit(COM_TypeDef COM) { int32_t ret = BSP_ERROR_NONE; if(COM > COMn) { ret = BSP_ERROR_WRONG_PARAM; } else { /* USART configuration */ hcom_uart[COM].Instance = COM_USART[COM]; #if (USE_HAL_UART_REGISTER_CALLBACKS == 0U) LPUART1_MspDeInit(&hcom_uart[COM]); #endif /* (USE_HAL_UART_REGISTER_CALLBACKS == 0U) */ if(HAL_UART_DeInit(&hcom_uart[COM]) != HAL_OK) { ret = BSP_ERROR_PERIPH_FAILURE; } } return ret; } /** * @brief Configures COM port. * @param huart USART handle * This parameter can be COM1 * @param COM_Init Pointer to a UART_HandleTypeDef structure that contains the * configuration information for the specified USART peripheral. * @retval HAL error code */ /* LPUART1 init function */ __weak HAL_StatusTypeDef MX_LPUART1_UART_Init(UART_HandleTypeDef* hlpuart) { HAL_StatusTypeDef ret = HAL_OK; hlpuart->Instance = LPUART1; hlpuart->Init.BaudRate = 209700; hlpuart->Init.WordLength = UART_WORDLENGTH_8B; hlpuart->Init.StopBits = UART_STOPBITS_1; hlpuart->Init.Parity = UART_PARITY_NONE; hlpuart->Init.Mode = UART_MODE_TX_RX; hlpuart->Init.HwFlowCtl = UART_HWCONTROL_NONE; hlpuart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; hlpuart->Init.ClockPrescaler = UART_PRESCALER_DIV1; hlpuart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; hlpuart->FifoMode = UART_FIFOMODE_DISABLE; if (HAL_UART_Init(hlpuart) != HAL_OK) { ret = HAL_ERROR; } if (HAL_UARTEx_SetTxFifoThreshold(hlpuart, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) { ret = HAL_ERROR; } if (HAL_UARTEx_SetRxFifoThreshold(hlpuart, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) { ret = HAL_ERROR; } if (HAL_UARTEx_DisableFifoMode(hlpuart) != HAL_OK) { ret = HAL_ERROR; } return ret; } #endif #if (USE_HAL_UART_REGISTER_CALLBACKS == 1U) /** * @brief Register Default LPUART1 Bus Msp Callbacks * @retval BSP status */ int32_t BSP_COM_RegisterDefaultMspCallbacks(COM_TypeDef COM) { int32_t ret = BSP_ERROR_NONE; if(COM >= COMn) { ret = BSP_ERROR_WRONG_PARAM; } else { __HAL_UART_RESET_HANDLE_STATE(&hcom_uart[COM]); /* Register default MspInit/MspDeInit Callback */ if(HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPINIT_CB_ID, LPUART1_MspInit) != HAL_OK) { ret = BSP_ERROR_PERIPH_FAILURE; } else if(HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPDEINIT_CB_ID, LPUART1_MspDeInit) != HAL_OK) { ret = BSP_ERROR_PERIPH_FAILURE; } else { IslpUart1MspCbValid = 1U; } } /* BSP status */ return ret; } /** * @brief Register LPUART1 Bus Msp Callback registering * @param Callbacks pointer to LPUART1 MspInit/MspDeInit callback functions * @retval BSP status */ int32_t BSP_COM_RegisterMspCallbacks (COM_TypeDef COM , BSP_COM_Cb_t *Callback) { int32_t ret = BSP_ERROR_NONE; if(COM >= COMn) { ret = BSP_ERROR_WRONG_PARAM; } else { __HAL_UART_RESET_HANDLE_STATE(&hcom_uart[COM]); /* Register MspInit/MspDeInit Callbacks */ if(HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPINIT_CB_ID, Callback->pMspInitCb) != HAL_OK) { ret = BSP_ERROR_PERIPH_FAILURE; } else if(HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPDEINIT_CB_ID, Callback->pMspDeInitCb) != HAL_OK) { ret = BSP_ERROR_PERIPH_FAILURE; } else { IslpUart1MspCbValid = 1U; } } /* BSP status */ return ret; } #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ #if (USE_COM_LOG > 0) /** * @brief Select the active COM port. * @param COM COM port to be activated. * This parameter can be COM1 * @retval BSP status */ int32_t BSP_COM_SelectLogPort(COM_TypeDef COM) { if(COM_ActiveLogPort != COM) { COM_ActiveLogPort = COM; } return BSP_ERROR_NONE; } #if defined(__CC_ARM) /* For arm compiler 5 */ #if !defined(__MICROLIB) /* If not Microlib */ struct __FILE { int dummyVar; //Just for the sake of redefining __FILE, we won't we using it anyways ;) }; FILE __stdout; #endif /* If not Microlib */ #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* For arm compiler 6 */ #if !defined(__MICROLIB) /* If not Microlib */ FILE __stdout; #endif /* If not Microlib */ #endif /* For arm compiler 5 */ #if defined(__ICCARM__) /* For IAR */ size_t __write(int Handle, const unsigned char *Buf, size_t Bufsize) { int i; for(i=0; i= 6010050)) /* For ARM Compiler 5 and 6 */ int fputc (int ch, FILE *f) { (void)HAL_UART_Transmit(&hcom_uart[COM_ActiveLogPort], (uint8_t *)&ch, 1, COM_POLL_TIMEOUT); return ch; } #else /* For GCC Toolchains */ int __io_putchar (int ch) { (void)HAL_UART_Transmit(&hcom_uart[COM_ActiveLogPort], (uint8_t *)&ch, 1, COM_POLL_TIMEOUT); return ch; } #endif /* For IAR */ #endif /* USE_COM_LOG */ /** * @brief Initializes LPUART1 MSP. * @param huart LPUART1 handle * @retval None */ static void LPUART1_MspInit(UART_HandleTypeDef* uartHandle) { GPIO_InitTypeDef GPIO_InitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; /* USER CODE BEGIN LPUART1_MspInit 0 */ /* USER CODE END LPUART1_MspInit 0 */ /** Initializes the peripherals clock */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); /* Enable Peripheral clock */ __HAL_RCC_LPUART1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /**LPUART1 GPIO Configuration PA2 ------> LPUART1_TX PA3 ------> LPUART1_RX */ GPIO_InitStruct.Pin = BUS_LPUART1_TX_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = BUS_LPUART1_TX_GPIO_AF; HAL_GPIO_Init(BUS_LPUART1_TX_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = BUS_LPUART1_RX_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = BUS_LPUART1_RX_GPIO_AF; HAL_GPIO_Init(BUS_LPUART1_RX_GPIO_PORT, &GPIO_InitStruct); /* USER CODE BEGIN LPUART1_MspInit 1 */ /* USER CODE END LPUART1_MspInit 1 */ } static void LPUART1_MspDeInit(UART_HandleTypeDef* uartHandle) { /* USER CODE BEGIN LPUART1_MspDeInit 0 */ /* USER CODE END LPUART1_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_LPUART1_CLK_DISABLE(); /**LPUART1 GPIO Configuration PA2 ------> LPUART1_TX PA3 ------> LPUART1_RX */ HAL_GPIO_DeInit(BUS_LPUART1_TX_GPIO_PORT, BUS_LPUART1_TX_GPIO_PIN); HAL_GPIO_DeInit(BUS_LPUART1_RX_GPIO_PORT, BUS_LPUART1_RX_GPIO_PIN); /* USER CODE BEGIN LPUART1_MspDeInit 1 */ /* USER CODE END LPUART1_MspDeInit 1 */ } /** * @} */ /** * @} */ /** * @} */ /** * @} */