427 lines
11 KiB
C
427 lines
11 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : stm32wlxx_nucleo.c
|
|
* @brief : Source file for the BSP Common driver
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2024 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 "stm32wlxx_nucleo.h"
|
|
|
|
/** @defgroup BSP BSP
|
|
* @{
|
|
*/
|
|
|
|
/** @defgroup STM32WLXX_NUCLEO STM32WLXX_NUCLEO
|
|
* @{
|
|
*/
|
|
|
|
/** @defgroup STM32WLXX_NUCLEO_LOW_LEVEL STM32WLXX_NUCLEO LOW LEVEL
|
|
* @brief This file provides set of firmware functions to manage Leds and push-button
|
|
* available on STM32WLxx-Nucleo Kit from STMicroelectronics.
|
|
* @{
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/** @defgroup STM32WLXX_NUCLEO_LOW_LEVEL_Private_Defines STM32WLXX_NUCLEO LOW LEVEL Private Defines
|
|
* @{
|
|
*/
|
|
|
|
/** @defgroup STM32WLXX_NUCLEO_LOW_LEVEL_FunctionPrototypes STM32WLXX_NUCLEO LOW LEVEL Private Function Prototypes
|
|
* @{
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/** @defgroup STM32WLXX_NUCLEO_LOW_LEVEL_Private_Variables STM32WLXX_NUCLEO LOW LEVEL Private Variables
|
|
* @{
|
|
*/
|
|
|
|
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 IsUsart2MspCbValid = 0;
|
|
#endif
|
|
__weak HAL_StatusTypeDef MX_USART2_UART_Init(UART_HandleTypeDef* huart);
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/** @defgroup STM32WLXX_NUCLEO_LOW_LEVEL_Private_Functions STM32WLXX_NUCLEO LOW LEVEL Private Functions
|
|
* @{
|
|
*/
|
|
#if (USE_BSP_COM_FEATURE > 0)
|
|
static void USART2_MspInit(UART_HandleTypeDef *huart);
|
|
static void USART2_MspDeInit(UART_HandleTypeDef *huart);
|
|
#endif
|
|
/**
|
|
* @brief This method returns the STM32WLxx NUCLEO BSP Driver revision
|
|
* @retval version: 0xXYZR (8bits for each decimal, R for RC)
|
|
*/
|
|
int32_t BSP_GetVersion(void)
|
|
{
|
|
return (int32_t)__STM32WLXX_NUCLEO_BSP_VERSION;
|
|
}
|
|
|
|
#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 */
|
|
USART2_MspInit(&hcom_uart[COM]);
|
|
#else
|
|
if(IsUsart2MspCbValid == 0U)
|
|
{
|
|
if(BSP_COM_RegisterDefaultMspCallbacks(COM) != BSP_ERROR_NONE)
|
|
{
|
|
return BSP_ERROR_MSP_FAILURE;
|
|
}
|
|
}
|
|
#endif
|
|
if (MX_USART2_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)
|
|
USART2_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
|
|
*/
|
|
|
|
/* USART2 init function */
|
|
|
|
__weak HAL_StatusTypeDef MX_USART2_UART_Init(UART_HandleTypeDef* huart)
|
|
{
|
|
HAL_StatusTypeDef ret = HAL_OK;
|
|
|
|
huart->Instance = USART2;
|
|
huart->Init.BaudRate = 115200;
|
|
huart->Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart->Init.StopBits = UART_STOPBITS_1;
|
|
huart->Init.Parity = UART_PARITY_NONE;
|
|
huart->Init.Mode = UART_MODE_TX_RX;
|
|
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart->Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart->Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(huart) != HAL_OK)
|
|
{
|
|
ret = HAL_ERROR;
|
|
}
|
|
|
|
if (HAL_UARTEx_SetTxFifoThreshold(huart, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
ret = HAL_ERROR;
|
|
}
|
|
|
|
if (HAL_UARTEx_SetRxFifoThreshold(huart, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
ret = HAL_ERROR;
|
|
}
|
|
|
|
if (HAL_UARTEx_DisableFifoMode(huart) != HAL_OK)
|
|
{
|
|
ret = HAL_ERROR;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
#endif
|
|
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1U)
|
|
/**
|
|
* @brief Register Default USART2 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, USART2_MspInit) != HAL_OK)
|
|
{
|
|
ret = BSP_ERROR_PERIPH_FAILURE;
|
|
}
|
|
else if(HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPDEINIT_CB_ID, USART2_MspDeInit) != HAL_OK)
|
|
{
|
|
ret = BSP_ERROR_PERIPH_FAILURE;
|
|
}
|
|
else
|
|
{
|
|
IsUsart2MspCbValid = 1U;
|
|
}
|
|
}
|
|
|
|
/* BSP status */
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief Register USART2 Bus Msp Callback registering
|
|
* @param Callbacks pointer to USART2 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
|
|
{
|
|
IsUsart2MspCbValid = 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<Bufsize; i++)
|
|
{
|
|
(void)HAL_UART_Transmit(&hcom_uart[COM_ActiveLogPort], (uint8_t *)&Buf[i], 1, COM_POLL_TIMEOUT);
|
|
}
|
|
|
|
return Bufsize;
|
|
}
|
|
#elif defined(__CC_ARM) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 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 USART2 MSP.
|
|
* @param huart USART2 handle
|
|
* @retval None
|
|
*/
|
|
|
|
static void USART2_MspInit(UART_HandleTypeDef* uartHandle)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
|
/* USER CODE BEGIN USART2_MspInit 0 */
|
|
|
|
/* USER CODE END USART2_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clocks
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART2;
|
|
PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_SYSCLK;
|
|
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
|
|
|
|
/* Enable Peripheral clock */
|
|
__HAL_RCC_USART2_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
/**USART2 GPIO Configuration
|
|
PA3 ------> USART2_RX
|
|
PA2 ------> USART2_TX
|
|
*/
|
|
GPIO_InitStruct.Pin = BUS_USART2_RX_GPIO_PIN;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.Alternate = BUS_USART2_RX_GPIO_AF;
|
|
HAL_GPIO_Init(BUS_USART2_RX_GPIO_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = BUS_USART2_TX_GPIO_PIN;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.Alternate = BUS_USART2_TX_GPIO_AF;
|
|
HAL_GPIO_Init(BUS_USART2_TX_GPIO_PORT, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN USART2_MspInit 1 */
|
|
|
|
/* USER CODE END USART2_MspInit 1 */
|
|
}
|
|
|
|
static void USART2_MspDeInit(UART_HandleTypeDef* uartHandle)
|
|
{
|
|
/* USER CODE BEGIN USART2_MspDeInit 0 */
|
|
|
|
/* USER CODE END USART2_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_USART2_CLK_DISABLE();
|
|
|
|
/**USART2 GPIO Configuration
|
|
PA3 ------> USART2_RX
|
|
PA2 ------> USART2_TX
|
|
*/
|
|
HAL_GPIO_DeInit(BUS_USART2_RX_GPIO_PORT, BUS_USART2_RX_GPIO_PIN);
|
|
|
|
HAL_GPIO_DeInit(BUS_USART2_TX_GPIO_PORT, BUS_USART2_TX_GPIO_PIN);
|
|
|
|
/* USER CODE BEGIN USART2_MspDeInit 1 */
|
|
|
|
/* USER CODE END USART2_MspDeInit 1 */
|
|
}
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|