您当前的位置: 首页 >  ar

仙剑情缘

暂无认证

  • 0浏览

    0关注

    333博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

STM32F030R8Tx使用HAL库实现uart通信

仙剑情缘 发布时间:2020-06-25 10:13:51 ,浏览量:0

  • 准备工程,此例程在以下链接的工程的基础上添加的uart功能

https://blog.csdn.net/mygod2008ok/article/details/106910607

  • 添加stm32f0xx_hal_uart.c和stm32f0xx_hal_uart_ex.c到工程

  • 在stm32f0xx_hal_conf.h中打开HAL_UART_MODULE_ENABLED宏

  • 新建BSP_uart.c和BSP_uart.h并加入到工程中
  • BSP_uart.c内容如下:
#include "BSP_uart.h"
#include "stm32f0xx_hal_uart.h"
#include "main.h"
#include "BSP_delay.h"
#include "BSP_pwm.h"
#include "stm_log.h"

/* UART handler declaration */
UART_HandleTypeDef UartHandle;
__IO ITStatus UartReady = RESET;
__IO uint32_t UserButtonStatus = 0;  /* set to 1 after User Button interrupt  */

/* Buffer used for transmission */



#define BUFFER_COUNT    2
#define TX_RX_LENGTH		20
static uint8_t aTxBuffer[TX_RX_LENGTH];

/* Buffer used for reception */
static uint8_t aRxBuffer[BUFFER_COUNT][TX_RX_LENGTH];   // 2组缓冲

static volatile uint8_t s_receive_index;
static volatile uint32_t uartReceiveCharTimeout;

uint8_t s_save_rx_buf[TX_RX_LENGTH];

static volatile uint8_t *p_rec_buf = &aRxBuffer[0][0];


uint8_t enter_stm32_dfu;

uint8_t from_app_stm32_version[4];




/**
* @brief 配置串口输入上拉
*/
static void uart_config_input(void)
{
	
	USARTx_TX_GPIO_CLK_ENABLE();
  USARTx_RX_GPIO_CLK_ENABLE();

	GPIO_InitTypeDef  GPIO_InitStruct;
	GPIO_InitStruct.Pin       = USARTx_TX_PIN;
  GPIO_InitStruct.Mode      = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);

  /* UART RX GPIO pin configuration  */
  GPIO_InitStruct.Pin = USARTx_RX_PIN;
  HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
}

/**
  * @brief UART MSP Initialization 
  *        This function configures the hardware resources used in this example: 
  *           - Peripheral's clock enable
  *           - Peripheral's GPIO Configuration  
  *           - NVIC configuration for UART interrupt request enable
  * @param huart: UART handle pointer
  * @retval None
  */
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{  
  GPIO_InitTypeDef  GPIO_InitStruct;
  
  /*##-1- Enable peripherals and GPIO Clocks #################################*/
  /* Enable GPIO TX/RX clock */
  USARTx_TX_GPIO_CLK_ENABLE();
  USARTx_RX_GPIO_CLK_ENABLE();


  /* Enable USARTx clock */
  USARTx_CLK_ENABLE(); 
  
  /*##-2- Configure peripheral GPIO ##########################################*/  
  /* UART TX GPIO pin configuration  */
  GPIO_InitStruct.Pin       = USARTx_TX_PIN;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.Alternate = USARTx_TX_AF;

  HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);

  /* UART RX GPIO pin configuration  */
  GPIO_InitStruct.Pin = USARTx_RX_PIN;
  GPIO_InitStruct.Alternate = USARTx_RX_AF;

  HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
    
  /*##-3- Configure the NVIC for UART ########################################*/
  /* NVIC for USART */
  HAL_NVIC_SetPriority(USARTx_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ(USARTx_IRQn);
	
	
}

/**
  * @brief UART MSP De-Initialization 
  *        This function frees the hardware resources used in this example:
  *          - Disable the Peripheral's clock
  *          - Revert GPIO and NVIC configuration to their default state
  * @param huart: UART handle pointer
  * @retval None
  */
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
  /*##-1- Reset peripherals ##################################################*/
  USARTx_FORCE_RESET();
  USARTx_RELEASE_RESET();

  /*##-2- Disable peripherals and GPIO Clocks #################################*/
  /* Configure UART Tx as alternate function  */
  HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
  /* Configure UART Rx as alternate function  */
  HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
  
  /*##-3- Disable the NVIC for UART ##########################################*/
  HAL_NVIC_DisableIRQ(USARTx_IRQn);
	
  uart_config_input();
}



static uint8_t s_receive_byte;
/**
* @brief 计算checksum
* @param [in] uint8_t const*
*  - 待计算的数据
* @param [in] uint8_t 
*  - len 计算数据的长度
* @return uint8_t
*  - checksum计算结果
*
*/
static uint8_t get_checksum(uint8_t const*dat, uint8_t len)
{
	uint8_t chksum = 0;
 
	for (uint8_t i = 0; i < len; i++)
		chksum += dat[i];

	return chksum;
}


/**
* @brief uart1 initialization
*/
void BSP_uart_init(void)
{
	
  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART configured as follows:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
	
  UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate   = 115200;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; 
	HAL_StatusTypeDef err_code = HAL_UART_DeInit(&UartHandle);
	APP_ERROR_CHECK(err_code);
  err_code = HAL_UART_Init(&UartHandle);
	APP_ERROR_CHECK(err_code);
	
	HAL_UART_Receive_IT(&UartHandle,&s_receive_byte, 1);
        UartReady = RESET;
}



/**
* @brief uart de-initialization
*/
void BSP_uart_deinit(void)
{

  HAL_StatusTypeDef err_code = HAL_UART_DeInit(&UartHandle);
  APP_ERROR_CHECK(err_code);
  UartReady = RESET;	 
	NRF_LOG_INFO("BSP_uart_deinit");
}



/**
* @brief 回复版本
*/
void reply_stm32_version(void)
{
	
		uint8_t buf[5];
		uint8_t version_buf[] = FIRMWARE_VERSION_DEF;
		for(uint8_t i=0; i=4))
		{
			s_receive_index -= 1;
			if(get_checksum(aRxBuffer[index], s_receive_index) == aRxBuffer[index][s_receive_index])
			{
				uartReceiveCharTimeout = 0;		   
        p_rec_buf = &aRxBuffer[index][0];
				if(++index >= BUFFER_COUNT)
				{
					index = 0;
				}	
         					
				s_wakeup_flag |= UART_RECEIVE_WAKEUP;
			}
		 
			s_receive_index = 0;
			
		}
}

/**
  * @brief  UART error callbacks
  * @param  UartHandle: UART handle
  * @note   This example shows a simple way to report transfer error, and you can
  *         add your own implementation.
  * @retval None
  */
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
     NRF_LOG_INFO("HAL_UART_ErrorCallback");	
     UartReady = RESET;
	
//	  APP_ERROR_CHECK(UartHandle->ErrorCode);
}



/**
* @brief 串口接收超时处理,此函数25H中调用
*/
void uart1_rec_timeout(void)
{
  
  if(uartReceiveCharTimeout == 0)
	   return;
	if(--uartReceiveCharTimeout == 0)
	{
		s_receive_index = 0;
		NRF_LOG_INFO("uartTimeoutHandler");
	}
}

/**
* @brief 串口接收到的数据处理
*/
void uart_data_handler(void)
{
	if((s_wakeup_flag & UART_RECEIVE_WAKEUP)==0)
		return;
	s_wakeup_flag &= CLEAR_UART_RECEIVE_WAKEUP;
	
	switch(p_rec_buf[2])
	{
	
		case STM32_ENTER_DFU:
		{
			enter_stm32_dfu = 1;
			for(uint8_t i=0; i            
关注
打赏
1658017818
查看更多评论
0.0420s