GD32移植STM32HAL库接口


原工程使用的STM32移植调用的接口都为HAL库接口,故本次移植部分接口。

串口接口

在调试串口接口时发生接上串口,进入错误中断,GD32手册上写的消除错误的方式与ST32是一致的,读取状态
寄存器,然后对 USART数据寄存器执行读或写访问,之前中断处理函数中未加usart_flag_clear(huart->Instance ,xxx);接口导致错误中断一直进入造成Hardfault,目前已加。

/**
 *  @file: usart.c
 *
 *  @date: 2020/5/7
 *
 *  @author: aron566
 *
 *  @brief:usart init,移植HAL库串口驱动
 */
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "usart.h"
#include "systick.h"
#include "gd32f4xx.h"
/** Private typedef ----------------------------------------------------------*/
/** Private macros -----------------------------------------------------------*/
/** Private constants --------------------------------------------------------*/
/** Public variables ---------------------------------------------------------*/ 
UART_HandleTypeDef huart0;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart4;
/** Private variables --------------------------------------------------------*/   
/** Public variables ---------------------------------------------------------*/
uint8_t tx_buffer[] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 
                       0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF };
uint8_t rx_buffer[ARRAYNUM(tx_buffer)];
/** Private function prototypes ----------------------------------------------*/
static void USART0_MsInit(void);
static void USART2_MsInit(void);
static void UART4_MsInit(void);
static void USER_UART_Enable_HalfDuplex(void);

static void usart_dma_config(void);
static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
static void UART_EndTxTransfer(UART_HandleTypeDef
huart);
static void UART_EndRxTransfer(UART_HandleTypeDef huart);
static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef
huart);
static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef huart);
static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef
huart);
/** Private user code --------------------------------------------------------/
/** Private application code -------------------------------------------------
/
/*******************************************************************************
*

  •   Static code
    

/
/
!
\brief configure USART DMA
\param[in] none
\param[out] none
\retval none
/
static void usart_dma_config(void)
{
dma_single_data_parameter_struct dma_init_struct;
/
enable DMA1 /
rcu_periph_clock_enable(RCU_DMA1);
/
deinitialize DMA channel7(USART0 tx) */
dma_deinit(DMA1, DMA_CH7);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;/< 配置发送:内存到外设 */
dma_init_struct.memory0_addr = (uint32_t)tx_buffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
dma_init_struct.number = ARRAYNUM(tx_buffer);
dma_init_struct.periph_addr = USART0_DATA_ADDRESS;/
< 外设数据地址 /
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH7, dma_init_struct);
/
configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH7);
dma_channel_subperipheral_select(DMA1, DMA_CH7, DMA_SUBPERI4);

<span class="token function">dma_deinit</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH2<span class="token punctuation">)</span><span class="token punctuation">;</span>
dma_init_struct<span class="token punctuation">.</span>direction <span class="token operator">=</span> DMA_PERIPH_TO_MEMORY<span class="token punctuation">;</span><span class="token comment">/**&lt; 配置接收:外设到内存 */</span>
dma_init_struct<span class="token punctuation">.</span>memory0_addr <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">uint32_t</span><span class="token punctuation">)</span>rx_buffer<span class="token punctuation">;</span>
<span class="token function">dma_single_data_mode_init</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH2<span class="token punctuation">,</span> dma_init_struct<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">/* configure DMA mode */</span>
<span class="token function">dma_circulation_disable</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dma_channel_subperipheral_select</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH2<span class="token punctuation">,</span> DMA_SUBPERI4<span class="token punctuation">)</span><span class="token punctuation">;</span>

}/**


  • @brief 串口0配置
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
static void USART0_MsInit(void)
{
/
USART configure */

<span class="token comment">/*new config for hal lib*/</span>
<span class="token comment">/* Initialize the UART state */</span>
huart0<span class="token punctuation">.</span>ErrorCode <span class="token operator">=</span> HAL_UART_ERROR_NONE<span class="token punctuation">;</span>
huart0<span class="token punctuation">.</span>gState <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>
huart0<span class="token punctuation">.</span>RxState <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>
huart0<span class="token punctuation">.</span>Lock <span class="token operator">=</span> HAL_UNLOCKED<span class="token punctuation">;</span>

huart0<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>BaudRate <span class="token operator">=</span> <span class="token number">9600</span><span class="token punctuation">;</span>
huart0<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>StopBits <span class="token operator">=</span> USART_STB_1BIT<span class="token punctuation">;</span>
huart0<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>WordLength <span class="token operator">=</span> USART_WL_8BIT<span class="token punctuation">;</span>
huart0<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>Parity <span class="token operator">=</span> USART_PM_NONE<span class="token punctuation">;</span>
huart0<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>Mode <span class="token operator">=</span> <span class="token number">0x08</span><span class="token operator">|</span><span class="token number">0x04</span><span class="token punctuation">;</span><span class="token comment">/*发送接收*/</span>

huart0<span class="token punctuation">.</span>Instance <span class="token operator">=</span> USART0<span class="token punctuation">;</span>

<span class="token function">HAL_UART_Init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>huart0<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">nvic_irq_enable</span><span class="token punctuation">(</span>USART0_IRQn<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/**


  • @brief 串口2配置
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
static void USART2_MsInit(void)
{
/
USART configure */

<span class="token comment">/*new config for hal lib*/</span>
  <span class="token comment">/* Initialize the UART state */</span>
huart2<span class="token punctuation">.</span>ErrorCode <span class="token operator">=</span> HAL_UART_ERROR_NONE<span class="token punctuation">;</span>
huart2<span class="token punctuation">.</span>gState <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>
huart2<span class="token punctuation">.</span>RxState <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>
huart2<span class="token punctuation">.</span>Lock <span class="token operator">=</span> HAL_UNLOCKED<span class="token punctuation">;</span>

huart2<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>BaudRate <span class="token operator">=</span> <span class="token number">9600</span><span class="token punctuation">;</span>
huart2<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>StopBits <span class="token operator">=</span> USART_STB_1BIT<span class="token punctuation">;</span>
huart2<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>WordLength <span class="token operator">=</span> USART_WL_8BIT<span class="token punctuation">;</span>
huart2<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>Parity <span class="token operator">=</span> USART_PM_NONE<span class="token punctuation">;</span>
huart2<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>Mode <span class="token operator">=</span> <span class="token number">0x08</span><span class="token operator">|</span><span class="token number">0x04</span><span class="token punctuation">;</span><span class="token comment">/*发送接收*/</span>

huart2<span class="token punctuation">.</span>Instance <span class="token operator">=</span> USART2<span class="token punctuation">;</span>

<span class="token function">HAL_UART_Init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>huart2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">nvic_irq_enable</span><span class="token punctuation">(</span>USART2_IRQn<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/**


  • @brief 串口4配置
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
static void UART4_MsInit(void)
{
/
USART configure */

<span class="token comment">/*new config for hal lib*/</span>
  <span class="token comment">/* Initialize the UART state */</span>
huart4<span class="token punctuation">.</span>ErrorCode <span class="token operator">=</span> HAL_UART_ERROR_NONE<span class="token punctuation">;</span>
huart4<span class="token punctuation">.</span>gState <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>
huart4<span class="token punctuation">.</span>RxState <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>
huart4<span class="token punctuation">.</span>Lock <span class="token operator">=</span> HAL_UNLOCKED<span class="token punctuation">;</span>

huart4<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>BaudRate <span class="token operator">=</span> <span class="token number">9600</span><span class="token punctuation">;</span>
huart4<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>StopBits <span class="token operator">=</span> USART_STB_1BIT<span class="token punctuation">;</span>
huart4<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>WordLength <span class="token operator">=</span> USART_WL_8BIT<span class="token punctuation">;</span>
huart4<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>Parity <span class="token operator">=</span> USART_PM_NONE<span class="token punctuation">;</span>
huart4<span class="token punctuation">.</span>Init<span class="token punctuation">.</span>Mode <span class="token operator">=</span> <span class="token number">0x08</span><span class="token operator">|</span><span class="token number">0x04</span><span class="token punctuation">;</span><span class="token comment">/*发送接收*/</span>

huart4<span class="token punctuation">.</span>Instance <span class="token operator">=</span> UART4<span class="token punctuation">;</span>

<span class="token function">HAL_UART_Init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>huart4<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">nvic_irq_enable</span><span class="token punctuation">(</span>UART4_IRQn<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/**


  • @brief 串口半双工配置GPIO FOR USART0 PA9 PA10
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/7

/
static void USER_UART_Enable_HalfDuplex(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
/
enable USART clock /
rcu_periph_clock_enable(RCU_USART0);
/
connect port to USARTx_Tx /
gpio_af_set(GPIOA ,GPIO_AF_7, GPIO_PIN_9);
/
connect port to USARTx_Rx /
gpio_af_set(GPIOA ,GPIO_AF_7, GPIO_PIN_10);
/
configure USART Tx as alternate function push-pull /
gpio_mode_set(GPIOA ,GPIO_MODE_AF, GPIO_PUPD_PULLUP ,GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ ,GPIO_PIN_9);
/
configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA ,GPIO_MODE_AF ,GPIO_PUPD_PULLUP ,GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP ,GPIO_OSPEED_50MHZ ,GPIO_PIN_10);

<span class="token function">USART0_MsInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/** Public application code --------------------------------------------------/
/
******************************************************************************
*

  •   Public code
    

/ /******************************************************************

  • @brief 串口设置
  • @param[in] 串口句柄
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef
huart)
{
/* USART configure /
usart_deinit(huart->Instance);
usart_baudrate_set(huart->Instance ,huart->Init.BaudRate);
usart_receive_config(huart->Instance, USART_RECEIVE_ENABLE);
usart_transmit_config(huart->Instance, USART_TRANSMIT_ENABLE);
usart_enable(huart->Instance);
return HAL_OK;
}
/
******************************************************************

  • @brief 串口0引脚初始化 RX->PA10 TX->PA9
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
void USART0_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOA); /
enable USART clock /
rcu_periph_clock_enable(RCU_USART0); /
connect port to USARTx_Tx /
gpio_af_set(GPIOA ,GPIO_AF_7, GPIO_PIN_9); /
connect port to USARTx_Rx /
gpio_af_set(GPIOA ,GPIO_AF_7, GPIO_PIN_10); /
configure USART Tx as alternate function push-pull /
gpio_mode_set(GPIOA ,GPIO_MODE_AF, GPIO_PUPD_PULLUP ,GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ ,GPIO_PIN_9); /
configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA ,GPIO_MODE_AF ,GPIO_PUPD_PULLUP ,GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP ,GPIO_OSPEED_50MHZ ,GPIO_PIN_10);

<span class="token function">USART0_MsInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/*******************************************************************

  • @brief 串口0单线半双工引脚初始化 RX->PA10 TX->PA9
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
void USART0_HalfDuplexInit(void)
{
USER_UART_Enable_HalfDuplex();
}
/
******************************************************************

  • @brief 串口2引脚初始化 RX->PC11 TX->PC10
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
void USART2_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOC);
/
enable USART clock /
rcu_periph_clock_enable(RCU_USART2);
/
connect port to USARTx_Tx /
gpio_af_set(GPIOC ,GPIO_AF_7, GPIO_PIN_10);
/
connect port to USARTx_Rx /
gpio_af_set(GPIOC ,GPIO_AF_7, GPIO_PIN_11);
/
configure USART Tx as alternate function push-pull /
gpio_mode_set(GPIOC ,GPIO_MODE_AF, GPIO_PUPD_PULLUP ,GPIO_PIN_10);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ ,GPIO_PIN_10);
/
configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOC ,GPIO_MODE_AF ,GPIO_PUPD_PULLUP ,GPIO_PIN_11);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP ,GPIO_OSPEED_50MHZ ,GPIO_PIN_11);

<span class="token function">USART2_MsInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/**


  • @brief 串口4引脚初始化 RX->PD2 TX->PC12
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

*/
void UART4_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOD);
rcu_periph_clock_enable(RCU_GPIOC);

<span class="token comment">/* enable USART clock */</span>
<span class="token function">rcu_periph_clock_enable</span><span class="token punctuation">(</span>RCU_UART4<span class="token punctuation">)</span><span class="token punctuation">;</span>    
<span class="token comment">/* connect port to USARTx_Tx */</span>
<span class="token function">gpio_af_set</span><span class="token punctuation">(</span>GPIOC <span class="token punctuation">,</span>GPIO_AF_8<span class="token punctuation">,</span> GPIO_PIN_12<span class="token punctuation">)</span><span class="token punctuation">;</span>    
<span class="token comment">/* connect port to USARTx_Rx */</span>
<span class="token function">gpio_af_set</span><span class="token punctuation">(</span>GPIOD <span class="token punctuation">,</span>GPIO_AF_8<span class="token punctuation">,</span> GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>    
<span class="token comment">/* configure USART Tx as alternate function push-pull */</span>
<span class="token function">gpio_mode_set</span><span class="token punctuation">(</span>GPIOC <span class="token punctuation">,</span>GPIO_MODE_AF <span class="token punctuation">,</span>GPIO_PUPD_PULLUP <span class="token punctuation">,</span>GPIO_PIN_12<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">gpio_output_options_set</span><span class="token punctuation">(</span>GPIOC <span class="token punctuation">,</span>GPIO_OTYPE_PP <span class="token punctuation">,</span>GPIO_OSPEED_50MHZ <span class="token punctuation">,</span>GPIO_PIN_12<span class="token punctuation">)</span><span class="token punctuation">;</span>    
<span class="token comment">/* configure USART Rx as alternate function push-pull */</span>
<span class="token function">gpio_mode_set</span><span class="token punctuation">(</span>GPIOD <span class="token punctuation">,</span>GPIO_MODE_AF <span class="token punctuation">,</span>GPIO_PUPD_PULLUP <span class="token punctuation">,</span>GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">gpio_output_options_set</span><span class="token punctuation">(</span>GPIOD <span class="token punctuation">,</span>GPIO_OTYPE_PP <span class="token punctuation">,</span>GPIO_OSPEED_50MHZ <span class="token punctuation">,</span>GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token function">UART4_MsInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/**


  • @brief 串口DMA配置
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/7

/
void Usart_DMA_Init(void)
{
/
configure USART DMA */
usart_dma_config();

<span class="token comment">/* enable USART0 DMA channel transmission and reception */</span>
<span class="token function">dma_channel_enable</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH7<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dma_channel_enable</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* USART DMA enable for transmission and reception */</span>
<span class="token function">usart_dma_transmit_config</span><span class="token punctuation">(</span>USART0<span class="token punctuation">,</span> USART_DENT_ENABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">usart_dma_receive_config</span><span class="token punctuation">(</span>USART0<span class="token punctuation">,</span> USART_DENR_ENABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* wait until USART0 TX DMA1 channel transfer complete */</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>RESET <span class="token operator">==</span> <span class="token function">dma_flag_get</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH7<span class="token punctuation">,</span> DMA_INTF_FTFIF<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token punctuation">}</span>    
<span class="token comment">/* wait until USART0 RX DMA1 channel transfer complete */</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>RESET <span class="token operator">==</span> <span class="token function">dma_flag_get</span><span class="token punctuation">(</span>DMA1<span class="token punctuation">,</span> DMA_CH2<span class="token punctuation">,</span> DMA_INTF_FTFIF<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token punctuation">}</span>

}

/**

  • @brief Initializes the half-duplex mode according to the specified

  •     parameters in the UART_InitTypeDef and create the associated handle.
    
  • @param huart Pointer to a UART_HandleTypeDef structure that contains

  •            the configuration information for the specified UART module.
    
  • @retval HAL status
    /
    HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef
    huart)
    {
    /* Check the UART handle allocation /
    if (huart NULL)
    {
    return HAL_ERROR;
    } if (huart->gState HAL_UART_STATE_RESET)
    {
    /
    Allocate lock resource and initialize it */
    huart->Lock = HAL_UNLOCKED;

    //Is First Config GPIO
    USER_UART_Enable_HalfDuplex();
    }
    huart->gState = HAL_UART_STATE_BUSY;
    /* Disable the peripheral /
    usart_disable(huart->Instance);
    /
    In half-duplex mode, the following bits must be kept cleared:

    • LINEN and CLKEN bits in the USART_CR2 register,
    • SCEN and IREN bits in the USART_CR3 register.*/
      usart_lin_mode_disable(huart->Instance);
      usart_synchronous_clock_disable(huart->Instance);
      usart_smartcard_mode_disable(huart->Instance);
      usart_irda_mode_disable(huart->Instance);

/* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
usart_halfduplex_enable(huart->Instance);

/* Enable the peripheral */
usart_enable(huart->Instance);

/* clear the USARTx data register for GD32*/
usart_data_receive(huart->Instance);

/* Initialize the UART state*/
huart->ErrorCode = HAL_UART_ERROR_NONE;
huart->gState = HAL_UART_STATE_READY;
huart->RxState = HAL_UART_STATE_READY;
return HAL_OK;
}

/**

  • @brief Enables the UART transmitter and disables the UART receiver.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval HAL status
    /
    HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef
    huart)
    {
    /* Process Locked */
    __HAL_LOCK(huart);

huart->gState = HAL_UART_STATE_BUSY;

/-------------------------- USART CR1 Configuration -----------------------/

/* Clear TE and RE bits */
usart_receive_config(huart->Instance, USART_RECEIVE_DISABLE);
usart_transmit_config(huart->Instance, USART_TRANSMIT_DISABLE);

/* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
usart_transmit_config(huart->Instance, USART_TRANSMIT_ENABLE);

/* Write to USART CR1 */

huart->gState = HAL_UART_STATE_READY;

/* Process Unlocked */
__HAL_UNLOCK(huart);

return HAL_OK;
}

/**

  • @brief Enables the UART receiver and disables the UART transmitter.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval HAL status
    /
    HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef
    huart)
    {
    /* Process Locked */
    __HAL_LOCK(huart);

huart->gState = HAL_UART_STATE_BUSY;

/-------------------------- USART CR1 Configuration -----------------------/

/* Clear TE and RE bits */
usart_receive_config(huart->Instance, USART_RECEIVE_DISABLE);
usart_transmit_config(huart->Instance, USART_TRANSMIT_DISABLE);

/* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
usart_receive_config(huart->Instance, USART_RECEIVE_ENABLE);

/* Write to USART CR1 */

huart->gState = HAL_UART_STATE_READY;

/* Process Unlocked */
__HAL_UNLOCK(huart);

return HAL_OK;
}
/**

  • @brief Sends an amount of data in blocking mode.

  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),

  •     the sent data is handled as a set of u16. In this case, Size must indicate the number
    
  •     of u16 provided through pData.
    
  • @param huart Pointer to a UART_HandleTypeDef structure that contains

  •           the configuration information for the specified UART module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).

  • @param Size Amount of data elements (u8 or u16) to be sent

  • @param Timeout Timeout duration

  • @retval HAL status
    /
    HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef
    huart, uint8_t pData, uint16_t Size, uint32_t Timeout)
    {
    uint16_t
    tmp;
    uint32_t tickstart = 0U;
    /* Check that a Tx process is not already ongoing /
    if (huart->gState HAL_UART_STATE_READY)
    {
    if ((pData NULL) || (Size == 0U))
    {
    return HAL_ERROR;
    }
    /
    Process Locked */
    __HAL_LOCK(huart);
    huart->ErrorCode = HAL_UART_ERROR_NONE;
    huart->gState = HAL_UART_STATE_BUSY_TX;

    /* Init tickstart for timeout managment */
    tickstart = HAL_GetTick();

    huart->TxXferSize = Size;
    huart->TxXferCount = Size;

    /* Process Unlocked /
    __HAL_UNLOCK(huart);
    while (huart->TxXferCount > 0U)
    {
    huart->TxXferCount--;
    if (huart->Init.WordLength == USART_WL_9BIT)
    {
    if (UART_WaitOnFlagUntilTimeout(huart, USART_FLAG_TBE, RESET, tickstart, Timeout) != HAL_OK)
    {
    return HAL_TIMEOUT;
    }
    tmp = (uint16_t
    ) pData;
    usart_data_transmit(huart->Instance ,(tmp & (uint16_t)0x01FF));
    if (huart->Init.Parity == USART_PM_NONE)
    {
    pData += 2U;
    }
    else
    {
    pData += 1U;
    }
    }
    else
    {
    if (UART_WaitOnFlagUntilTimeout(huart, USART_FLAG_TBE, RESET, tickstart, Timeout) != HAL_OK)
    {
    return HAL_TIMEOUT;
    }
    usart_data_transmit(huart->Instance ,(
    pData++ & (uint8_t)0xFF));
    }
    } if (UART_WaitOnFlagUntilTimeout(huart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
    {
    return HAL_TIMEOUT;
    }
    /* At end of Tx process, restore huart->gState to Ready */
    huart->gState = HAL_UART_STATE_READY;
    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }

/**

  • @brief Receives an amount of data in blocking mode.

  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),

  •     the received data is handled as a set of u16. In this case, Size must indicate the number
    
  •     of u16 available through pData.
    
  • @param huart Pointer to a UART_HandleTypeDef structure that contains

  •           the configuration information for the specified UART module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).

  • @param Size Amount of data elements (u8 or u16) to be received.

  • @param Timeout Timeout duration

  • @retval HAL status
    /
    HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef
    huart, uint8_t pData, uint16_t Size, uint32_t Timeout)
    {
    uint16_t
    tmp;
    uint32_t tickstart = 0U;
    /* Check that a Rx process is not already ongoing /
    if (huart->RxState HAL_UART_STATE_READY)
    {
    if ((pData NULL) || (Size == 0U))
    {
    return HAL_ERROR;
    }
    /
    Process Locked /
    __HAL_LOCK(huart);
    huart->ErrorCode = HAL_UART_ERROR_NONE;
    huart->RxState = HAL_UART_STATE_BUSY_RX;
    /
    Init tickstart for timeout managment /
    tickstart = HAL_GetTick();
    huart->RxXferSize = Size;
    huart->RxXferCount = Size;
    /
    Process Unlocked /
    __HAL_UNLOCK(huart);
    /
    Check the remain data to be received /
    while (huart->RxXferCount > 0U)
    {
    huart->RxXferCount--;
    if (huart->Init.WordLength == USART_WL_9BIT)
    {
    if (UART_WaitOnFlagUntilTimeout(huart, USART_PM_NONE, RESET, tickstart, Timeout) != HAL_OK)
    {
    return HAL_TIMEOUT;
    }
    tmp = (uint16_t
    ) pData;
    if (huart->Init.Parity == USART_PM_NONE)
    {
    *tmp = (uint16_t)(usart_data_receive(huart->Instance) & (uint16_t)0x01FF);

      pData <span class="token operator">+=</span> <span class="token number">2U</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{<!-- --></span>
      <span class="token operator">*</span>tmp <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">uint16_t</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token function">usart_data_receive</span><span class="token punctuation">(</span>huart<span class="token operator">-&gt;</span>Instance<span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token punctuation">(</span><span class="token class-name">uint16_t</span><span class="token punctuation">)</span><span class="token number">0x00FF</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      pData <span class="token operator">+=</span> <span class="token number">1U</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>      <span class="token punctuation">}</span>
    

    else
    {
    if (UART_WaitOnFlagUntilTimeout(huart, USART_PM_NONE, RESET, tickstart, Timeout) != HAL_OK)
    {
    return HAL_TIMEOUT;
    }
    if (huart->Init.Parity == USART_PM_NONE)
    {
    pData++ = (uint8_t)(usart_data_receive(huart->Instance) & (uint8_t)0x00FF);
    }
    else
    {
    pData++ = (uint8_t)(usart_data_receive(huart->Instance) & (uint8_t)0x007F);
    } }
    }
    /* At end of Rx process, restore huart->RxState to Ready */
    huart->RxState = HAL_UART_STATE_READY;
    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }

/**

  • @brief This function handles UART Communication Timeout.

  • @param huart Pointer to a UART_HandleTypeDef structure that contains

  •            the configuration information for the specified UART module.
    
  • @param Flag specifies the UART flag to check.

  • @param Status The new Flag status (SET or RESET).

  • @param Tickstart Tick start value

  • @param Timeout Timeout duration

  • @retval HAL status
    /
    static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef
    huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
    {
    /* Wait until flag is set /
    while ((usart_flag_get(huart->Instance ,(usart_flag_enum)Flag) ? SET : RESET) == Status)
    {
    /
    Check for the Timeout /
    if (Timeout != HAL_MAX_DELAY)
    {
    if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
    {
    /
    Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_TBEIE);
    /
    Disable the UART Data Register not empty Interrupt /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_RBNEIE);
    /
    Disable the UART Parity Error Interrupt /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_PERRIE);
    /
    Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
    usart_interrupt_disable(huart->Instance ,USART_INTEN_ERRIE);

    huart<span class="token operator">-&gt;</span>gState  <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>
    huart<span class="token operator">-&gt;</span>RxState <span class="token operator">=</span> HAL_UART_STATE_READY<span class="token punctuation">;</span>        
    <span class="token comment">/* Process Unlocked */</span>
    <span class="token function">__HAL_UNLOCK</span><span class="token punctuation">(</span>huart<span class="token punctuation">)</span><span class="token punctuation">;</span>        
    <span class="token keyword">return</span> HAL_TIMEOUT<span class="token punctuation">;</span>
    

    }
    }
    }
    return HAL_OK;
    }
    /**

  • @brief Sends an amount of data in non blocking mode.

  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),

  •     the sent data is handled as a set of u16. In this case, Size must indicate the number
    
  •     of u16 provided through pData.
    
  • @param huart Pointer to a UART_HandleTypeDef structure that contains

  •           the configuration information for the specified UART module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).

  • @param Size Amount of data elements (u8 or u16) to be sent

  • @retval HAL status
    /
    HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef
    huart, uint8_t pData, uint16_t Size)
    {
    /
    Check that a Tx process is not already ongoing /
    if (huart->gState HAL_UART_STATE_READY)
    {
    if ((pData NULL) || (Size == 0U))
    {
    return HAL_ERROR;
    } /
    Process Locked /
    __HAL_LOCK(huart);
    huart->pTxBuffPtr = pData;
    huart->TxXferSize = Size;
    huart->TxXferCount = Size;
    huart->ErrorCode = HAL_UART_ERROR_NONE;
    huart->gState = HAL_UART_STATE_BUSY_TX;
    /
    Process Unlocked /
    __HAL_UNLOCK(huart);
    /
    Enable the UART Transmit data register empty Interrupt */
    usart_interrupt_enable(huart->Instance, USART_INTEN_TBEIE);

    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }
    /**

  • @brief Receives an amount of data in non blocking mode.

  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),

  •     the received data is handled as a set of u16. In this case, Size must indicate the number
    
  •     of u16 available through pData.
    
  • @param huart Pointer to a UART_HandleTypeDef structure that contains

  •           the configuration information for the specified UART module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).

  • @param Size Amount of data elements (u8 or u16) to be received.

  • @retval HAL status
    /
    HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef
    huart, uint8_t pData, uint16_t Size)
    {
    /
    Check that a Rx process is not already ongoing /
    if (huart->RxState HAL_UART_STATE_READY)
    {
    if ((pData NULL) || (Size == 0U))
    {
    return HAL_ERROR;
    } /
    Process Locked /
    __HAL_LOCK(huart); huart->pRxBuffPtr = pData;
    huart->RxXferSize = Size;
    huart->RxXferCount = Size;
    huart->ErrorCode = HAL_UART_ERROR_NONE;
    huart->RxState = HAL_UART_STATE_BUSY_RX;
    /
    Process Unlocked /
    __HAL_UNLOCK(huart);
    /
    Enable the UART Parity Error Interrupt /
    usart_interrupt_enable(huart->Instance, USART_INTEN_PERRIE);
    /
    Enable the UART Error Interrupt: (Frame error, noise error, overrun error) /
    usart_interrupt_enable(huart->Instance, USART_INTEN_ERRIE);
    /
    Enable the UART Data Register not empty Interrupt /
    usart_interrupt_enable(huart->Instance, USART_INTEN_RBNEIE);
    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }
    /
    *

  • @brief This function handles UART interrupt request.

  • @param huart Pointer to a UART_HandleTypeDef structure that contains

  •            the configuration information for the specified UART module.
    
  • @retval None
    /
    void HAL_UART_IRQHandler(UART_HandleTypeDef
    huart)
    {
    uint32_t errorflags = 0x00U;
    /get : parity err , frame err , overrun err ,noise err/
    errorflags |= usart_flag_get(huart->Instance ,USART_FLAG_PERR);
    errorflags |= usart_flag_get(huart->Instance ,USART_FLAG_FERR);
    errorflags |= usart_flag_get(huart->Instance ,USART_FLAG_ORERR);
    errorflags |= usart_flag_get(huart->Instance ,USART_FLAG_NERR);

/* If no error occurs /
if (errorflags == RESET)
{
/
UART in mode Receiver -------------------------------------------------/
if (((usart_flag_get(huart->Instance ,USART_FLAG_RBNE)) != RESET) && ((usart_interrupt_flag_get(huart->Instance ,USART_INT_RBNEIE)) != RESET))
/
datareg not empty and not empty intterrupt is open/
{
UART_Receive_IT(huart);
return;
}
} /
If some errors occur /
if ((errorflags != RESET) && (((usart_interrupt_flag_get(huart->Instance ,USART_INT_ERRIE)) != RESET) || ((usart_interrupt_flag_get(huart->Instance ,USART_INT_RBNEIE) | usart_interrupt_flag_get(huart->Instance ,USART_INT_PERRIE))) != RESET))
{
/
UART parity error interrupt occurred ----------------------------------/
if (((usart_flag_get(huart->Instance ,USART_FLAG_PERR)) != RESET) && ((usart_interrupt_flag_get(huart->Instance ,USART_INT_PERRIE)) != RESET))
{
usart_flag_clear(huart->Instance ,USART_FLAG_PERR);
huart->ErrorCode |= HAL_UART_ERROR_PE;
}
/
UART noise error interrupt occurred -----------------------------------/
if (((usart_flag_get(huart->Instance ,USART_FLAG_NERR)) != RESET) && ((usart_interrupt_flag_get(huart->Instance ,USART_INTEN_ERRIE)) != RESET))
{
usart_flag_clear(huart->Instance ,USART_FLAG_NERR);
huart->ErrorCode |= HAL_UART_ERROR_NE;
}
/
UART frame error interrupt occurred -----------------------------------/
if (((usart_flag_get(huart->Instance ,USART_FLAG_FERR)) != RESET) && ((usart_interrupt_flag_get(huart->Instance ,USART_INTEN_ERRIE)) != RESET))
{
usart_flag_clear(huart->Instance ,HAL_UART_ERROR_FE);
huart->ErrorCode |= HAL_UART_ERROR_FE;
}
/
UART Over-Run interrupt occurred --------------------------------------/
if (((usart_flag_get(huart->Instance ,USART_FLAG_ORERR)) != RESET) && (((usart_interrupt_flag_get(huart->Instance ,USART_INT_RBNEIE)) != RESET) || ((usart_interrupt_flag_get(huart->Instance ,USART_INTEN_ERRIE)) != RESET)))
{
usart_flag_clear(huart->Instance ,USART_FLAG_ORERR);
huart->ErrorCode |= HAL_UART_ERROR_ORE;
}
/
Call UART Error Call back function if need be --------------------------/
if (huart->ErrorCode != HAL_UART_ERROR_NONE)
{
/
UART in mode Receiver -----------------------------------------------/
if (((usart_flag_get(huart->Instance ,USART_FLAG_RBNE)) != RESET) && ((usart_interrupt_flag_get(huart->Instance ,USART_INT_RBNEIE)) != RESET))
{
UART_Receive_IT(huart);
}
}
return;
}
/
End if some error occurs / / UART in mode Transmitter ------------------------------------------------/
if (((usart_flag_get(huart->Instance ,USART_FLAG_TBE)) != RESET) && ((usart_interrupt_flag_get(huart->Instance ,USART_INT_TBEIE)) != RESET))
{
UART_Transmit_IT(huart);
return;
}
/
UART in mode Transmitter end --------------------------------------------*/
if (((usart_flag_get(huart->Instance ,USART_FLAG_TC)) != RESET) && ((usart_interrupt_flag_get(huart->Instance ,USART_INT_TCIE)) != RESET))
{
UART_EndTransmit_IT(huart);
return;
}
}
/**

  • @brief Tx Transfer completed callbacks.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval None
    /
    __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart);
    /
    NOTE: This function should not be modified, when the callback is needed,
    the HAL_UART_TxCpltCallback could be implemented in the user file
    /
    }/
    *
  • @brief Tx Half Transfer completed callbacks.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval None
    /
    __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart);
    /
    NOTE: This function should not be modified, when the callback is needed,
    the HAL_UART_TxHalfCpltCallback could be implemented in the user file
    /
    }/
    *
  • @brief Rx Transfer completed callbacks.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval None
    /
    __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart);
    /
    NOTE: This function should not be modified, when the callback is needed,
    the HAL_UART_RxCpltCallback could be implemented in the user file
    /
    }/
    *
  • @brief Rx Half Transfer completed callbacks.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval None
    /
    __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart);
    /
    NOTE: This function should not be modified, when the callback is needed,
    the HAL_UART_RxHalfCpltCallback could be implemented in the user file
    /
    }/
    *
  • @brief UART error callbacks.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval None
    /
    __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart);
    /
    NOTE: This function should not be modified, when the callback is needed,
    the HAL_UART_ErrorCallback could be implemented in the user file
    /
    }/
    *
  • @brief UART Abort Complete callback.
  • @param huart UART handle.
  • @retval None
    /
    __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart); /
    NOTE : This function should not be modified, when the callback is needed,
    the HAL_UART_AbortCpltCallback can be implemented in the user file.
    /
    }/
    *
  • @brief UART Abort Complete callback.
  • @param huart UART handle.
  • @retval None
    /
    __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart); /
    NOTE : This function should not be modified, when the callback is needed,
    the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
    /
    }/
    *
  • @brief UART Abort Receive Complete callback.
  • @param huart UART handle.
  • @retval None
    /
    __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef
    huart)
    {
    /* Prevent unused argument(s) compilation warning /
    UNUSED(huart); /
    NOTE : This function should not be modified, when the callback is needed,
    the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
    /
    }/
    *
  • @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
  • @param huart UART handle.
  • @retval None
    /
    static void UART_EndTxTransfer(UART_HandleTypeDef
    huart)
    {
    /* Disable TXEIE and TCIE interrupts /
    usart_interrupt_disable(huart->Instance, USART_INTEN_TCIE);/
    禁用发送完成中断/
    usart_interrupt_disable(huart->Instance, USART_INTEN_TBEIE);/
    禁用发送中断/ / At end of Tx process, restore huart->gState to Ready /
    huart->gState = HAL_UART_STATE_READY;
    }/
    *
  • @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  • @param huart UART handle.
  • @retval None
    /
    static void UART_EndRxTransfer(UART_HandleTypeDef
    huart)
    {
    /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts /
    usart_interrupt_disable(huart->Instance, USART_INTEN_PERRIE);
    usart_interrupt_disable(huart->Instance, USART_INTEN_ERRIE);
    usart_interrupt_disable(huart->Instance, USART_INTEN_RBNEIE); /
    At end of Rx process, restore huart->RxState to Ready /
    huart->RxState = HAL_UART_STATE_READY;
    }
    /
    *
  • @brief Sends an amount of data in non blocking mode.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval HAL status
    /
    static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef
    huart)
    {
    uint16_t tmp; / Check that a Tx process is ongoing /
    if (huart->gState HAL_UART_STATE_BUSY_TX)
    {
    if (huart->Init.WordLength USART_WL_9BIT)
    {
    tmp = (uint16_t
    ) huart->pTxBuffPtr;
    usart_data_transmit(huart->Instance ,(uint16_t)(tmp & (uint16_t)0x01FF));
    if (huart->Init.Parity == USART_PM_NONE)
    {
    huart->pTxBuffPtr += 2U;
    }
    else
    {
    huart->pTxBuffPtr += 1U;
    }
    }
    else
    {
    usart_data_transmit(huart->Instance ,(uint8_t)(
    huart->pTxBuffPtr++ & (uint8_t)0x00FF));
    } if (--huart->TxXferCount == 0U)
    {
    /* Disable the UART Transmit Complete Interrupt /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_TBEIE); /
    Enable the UART Transmit Complete Interrupt */
    usart_interrupt_enable(huart->Instance ,USART_INTEN_TCIE);
    }
    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }

/**

  • @brief Wraps up transmission in non blocking mode.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval HAL status
    /
    static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef
    huart)
    {
    /* Disable the UART Transmit Complete Interrupt /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_TCIE);
    /
    Tx process is ended, restore huart->gState to Ready /
    huart->gState = HAL_UART_STATE_READY;#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
    /
    Call registered Tx complete callback/
    huart->TxCpltCallback(huart);
    #else
    /
    Call legacy weak Tx complete callback/
    HAL_UART_TxCpltCallback(huart);
    #endif /
    USE_HAL_UART_REGISTER_CALLBACKS */
    return HAL_OK;
    }

/**

  • @brief Receives an amount of data in non blocking mode
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval HAL status
    /
    static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef
    huart)
    {
    uint16_t tmp;
    /
    Check that a Rx process is ongoing /
    if (huart->RxState HAL_UART_STATE_BUSY_RX)
    {
    if (huart->Init.WordLength USART_WL_9BIT)
    {
    tmp = (uint16_t
    ) huart->pRxBuffPtr;
    if (huart->Init.Parity USART_PM_NONE)
    {
    tmp = (uint16_t)(usart_data_receive(huart->Instance) & (uint16_t)0x01FF);
    huart->pRxBuffPtr += 2U;
    }
    else
    {
    tmp = (uint16_t)(usart_data_receive(huart->Instance) & (uint16_t)0x00FF);
    huart->pRxBuffPtr += 1U;
    }
    }
    else
    {
    if (huart->Init.Parity USART_PM_NONE)
    {
    huart->pRxBuffPtr++ = (uint8_t)(usart_data_receive(huart->Instance) & (uint8_t)0x00FF);
    }
    else
    {
    huart->pRxBuffPtr++ = (uint8_t)(usart_data_receive(huart->Instance) & (uint8_t)0x007F);
    }
    } if (--huart->RxXferCount 0U)
    {
    /* Disable the UART Data Register not empty Interrupt /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_RBNEIE);
    /
    Disable the UART Parity Error Interrupt /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_PERRIE);
    /
    Disable the UART Error Interrupt: (Frame error, noise error, overrun error) /
    usart_interrupt_disable(huart->Instance ,USART_INTEN_ERRIE);
    /
    Rx process is completed, restore huart->RxState to Ready */
    huart->RxState = HAL_UART_STATE_READY;#if (USE_HAL_UART_REGISTER_CALLBACKS 1)
    /Call registered Rx complete callback/
    huart->RxCpltCallback(huart);
    #else
    /Call legacy weak Rx complete callback/
    HAL_UART_RxCpltCallback(huart);
    #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
    return HAL_OK;
    }
    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }

/**

  • @brief Returns the UART state.
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •            the configuration information for the specified UART module.
    
  • @retval HAL state
    /
    HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef
    huart)
    {
    uint32_t temp1 = 0x00U, temp2 = 0x00U;
    temp1 = huart->gState;
    temp2 = huart->RxState;
    return (HAL_UART_StateTypeDef)(temp1 | temp2);
    }

/**

  • @brief Return the UART error code
  • @param huart Pointer to a UART_HandleTypeDef structure that contains
  •           the configuration information for the specified UART.
    
  • @retval UART Error Code
    /
    uint32_t HAL_UART_GetError(UART_HandleTypeDef
    huart)
    {
    return huart->ErrorCode;
    }

int fputc(int ch, FILE *f)
{
usart_data_transmit(DEBUG_USART_NUM, (uint8_t) ch);
while (RESET == usart_flag_get(DEBUG_USART_NUM,USART_FLAG_TBE));
return ch;
}

/**


  • @brief 停止UART
  • @param[in] huart
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/8

/
void USER_Stop_Usart(UART_HandleTypeDef
huart)
{
/停止发送/
UART_EndTxTransfer(huart);

<span class="token comment">/*停止接收*/</span>
<span class="token function">UART_EndRxTransfer</span><span class="token punctuation">(</span>huart<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*禁用USART*/</span>
<span class="token function">usart_disable</span><span class="token punctuation">(</span>huart<span class="token operator">-&gt;</span>Instance<span class="token punctuation">)</span><span class="token punctuation">;</span>

}#ifdef __cplusplus ///<end extern c
}
#endif
/******************************** End of file *********************************/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929
  • 930
  • 931
  • 932
  • 933
  • 934
  • 935
  • 936
  • 937
  • 938
  • 939
  • 940
  • 941
  • 942
  • 943
  • 944
  • 945
  • 946
  • 947
  • 948
  • 949
  • 950
  • 951
  • 952
  • 953
  • 954
  • 955
  • 956
  • 957
  • 958
  • 959
  • 960
  • 961
  • 962
  • 963
  • 964
  • 965
  • 966
  • 967
  • 968
  • 969
  • 970
  • 971
  • 972
  • 973
  • 974
  • 975
  • 976
  • 977
  • 978
  • 979
  • 980
  • 981
  • 982
  • 983
  • 984
  • 985
  • 986
  • 987
  • 988
  • 989
  • 990
  • 991
  • 992
  • 993
  • 994
  • 995
  • 996
  • 997
  • 998
  • 999
  • 1000
  • 1001
  • 1002
  • 1003
  • 1004
  • 1005
  • 1006
  • 1007
  • 1008
  • 1009
  • 1010
  • 1011
  • 1012
  • 1013
  • 1014
  • 1015
  • 1016
  • 1017
  • 1018
  • 1019
  • 1020
  • 1021
  • 1022
  • 1023
  • 1024
  • 1025
  • 1026
  • 1027
  • 1028
  • 1029
  • 1030
  • 1031
  • 1032
  • 1033
  • 1034
  • 1035
  • 1036
  • 1037
  • 1038
  • 1039
  • 1040
  • 1041
  • 1042
  • 1043
  • 1044
  • 1045
  • 1046
  • 1047
  • 1048
  • 1049
  • 1050
  • 1051
  • 1052
  • 1053
  • 1054
  • 1055
  • 1056
  • 1057
  • 1058
  • 1059
  • 1060
  • 1061
  • 1062
  • 1063
  • 1064
  • 1065
  • 1066
  • 1067
  • 1068
  • 1069
  • 1070
  • 1071
  • 1072
  • 1073
  • 1074
  • 1075
  • 1076
  • 1077
  • 1078
  • 1079
  • 1080
  • 1081
  • 1082
  • 1083
  • 1084
  • 1085
  • 1086
  • 1087
  • 1088
  • 1089
  • 1090
  • 1091
  • 1092
  • 1093
  • 1094
  • 1095
  • 1096
  • 1097
  • 1098
  • 1099
  • 1100
  • 1101
  • 1102
  • 1103
  • 1104
  • 1105
  • 1106
  • 1107
  • 1108
  • 1109
  • 1110
  • 1111
  • 1112
  • 1113
  • 1114
  • 1115
  • 1116
  • 1117
  • 1118
  • 1119
  • 1120
  • 1121
  • 1122
  • 1123
  • 1124
/**
 *  @file: usart.h
 *
 *  @date: 2020/5/7
 *
 *  @author: aron566
 *
 *  @brief:usart config
 *  
 *  @version:
 */
#ifndef _USART_H
#define _USART_H
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
#include <stdint.h> /**< nedd definition of uint8_t */
#include <stddef.h> /**< need definition of NULL    */
//#include <stdbool.h>/**< need definition of BOOL    */
#include <stdio.h>  /**< if need printf             */
#include <stdlib.h>
#include <string.h>
/** Private includes ---------------------------------------------------------*/
#include "HAL_Typedef.h"
/** Private defines ----------------------------------------------------------*/
/**
 * @name 调试串口端口号配置
 * @{
 */
#define DEBUG_USART_NUM                  USART0
/** @}*/    

/**

  • @name 串口数据端口
  • @{
    /
    #define USART_DATA_ADDR_OFFSET ((uint32_t)0x4)
    #define USART0_DATA_ADDRESS ((uint32_t)0x40011000+USART_DATA_ADDR_OFFSET)
    #define USART1_DATA_ADDRESS ((uint32_t)0x40004400+USART_DATA_ADDR_OFFSET)
    #define USART2_DATA_ADDRESS ((uint32_t)0x40004800+USART_DATA_ADDR_OFFSET)
    #define UART3_DATA_ADDRESS ((uint32_t)0x40004C00+USART_DATA_ADDR_OFFSET)
    #define UART4_DATA_ADDRESS ((uint32_t)0x40005000+USART_DATA_ADDR_OFFSET)
    #define USART5_DATA_ADDRESS ((uint32_t)0x40011400+USART_DATA_ADDR_OFFSET)
    #define UART6_DATA_ADDRESS ((uint32_t)0x40007800+USART_DATA_ADDR_OFFSET)
    #define UART7_DATA_ADDRESS ((uint32_t)0x40007C00+USART_DATA_ADDR_OFFSET)
    /
    * @}/
    /** Exported typedefines -----------------------------------------------------
    //**
  • @brief HAL UART State structures definition
  • @note HAL UART State value is a combination of 2 different substates: gState and RxState.
  •    - gState contains UART state information related to global Handle management
    
  •      and also information related to Tx operations.
    
  •      gState value coding follow below described bitmap :
    
  •      b7-b6  Error information
    
  •         00 : No Error
    
  •         01 : (Not Used)
    
  •         10 : Timeout
    
  •         11 : Error
    
  •      b5     Peripheral initialization status
    
  •         0  : Reset (Peripheral not initialized)
    
  •         1  : Init done (Peripheral not initialized. HAL UART Init function already called)
    
  •      b4-b3  (not used)
    
  •         xx : Should be set to 00
    
  •      b2     Intrinsic process state
    
  •         0  : Ready
    
  •         1  : Busy (Peripheral busy with some configuration or internal operations)
    
  •      b1     (not used)
    
  •         x  : Should be set to 0
    
  •      b0     Tx state
    
  •         0  : Ready (no Tx operation ongoing)
    
  •         1  : Busy (Tx operation ongoing)
    
  •    - RxState contains information related to Rx operations.
    
  •      RxState value coding follow below described bitmap :
    
  •      b7-b6  (not used)
    
  •         xx : Should be set to 00
    
  •      b5     Peripheral initialization status
    
  •         0  : Reset (Peripheral not initialized)
    
  •         1  : Init done (Peripheral not initialized)
    
  •      b4-b2  (not used)
    
  •        xxx : Should be set to 000
    
  •      b1     Rx state
    
  •         0  : Ready (no Rx operation ongoing)
    
  •         1  : Busy (Rx operation ongoing)
    
  •      b0     (not used)
    
  •         x  : Should be set to 0.
    

/
typedef enum
{
HAL_UART_STATE_RESET = 0x00U, /
!< Peripheral is not yet Initialized
Value is allowed for gState and RxState /
HAL_UART_STATE_READY = 0x20U, /
!< Peripheral Initialized and ready for use
Value is allowed for gState and RxState /
HAL_UART_STATE_BUSY = 0x24U, /
!< an internal process is ongoing
Value is allowed for gState only /
HAL_UART_STATE_BUSY_TX = 0x21U, /
!< Data Transmission process is ongoing
Value is allowed for gState only /
HAL_UART_STATE_BUSY_RX = 0x22U, /
!< Data Reception process is ongoing
Value is allowed for RxState only /
HAL_UART_STATE_BUSY_TX_RX = 0x23U, /
!< Data Transmission and Reception process is ongoing
Not to be used for neither gState nor RxState.
Value is result of combination (Or) between gState and RxState values /
HAL_UART_STATE_TIMEOUT = 0xA0U, /
!< Timeout state
Value is allowed for gState only /
HAL_UART_STATE_ERROR = 0xE0U /
!< Error
Value is allowed for gState only /
} HAL_UART_StateTypeDef;/
*

  • @brief UART Init Structure definition
    /
    typedef struct
    {
    uint32_t BaudRate; /
    !< This member configures the UART communication baud rate.
    The baud rate is computed using the following formula:
    - IntegerDivider = ((PCLKx) / (8 * (OVR8+1) * (huart->Init.BaudRate)))
    - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 8 * (OVR8+1)) + 0.5
    Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register. /
    uint32_t WordLength; /
    !< Specifies the number of data bits transmitted or received in a frame.
    This parameter can be a value of @ref UART_Word_Length */

    uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
    This parameter can be a value of @ref UART_Stop_Bits /

    uint32_t Parity; /
    !< Specifies the parity mode.
    This parameter can be a value of @ref UART_Parity
    @note When parity is enabled, the computed parity is inserted
    at the MSB position of the transmitted data (9th bit when
    the word length is set to 9 data bits; 8th bit when the
    word length is set to 8 data bits). /
    uint32_t Mode; /
    !< Specifies whether the Receive or Transmit mode is enabled or disabled.
    This parameter can be a value of @ref UART_Mode /
    uint32_t HwFlowCtl; /
    !< Specifies whether the hardware flow control mode is enabled or disabled.
    This parameter can be a value of @ref UART_Hardware_Flow_Control /
    uint32_t OverSampling; /
    !< Specifies whether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).
    This parameter can be a value of @ref UART_Over_Sampling */
    } UART_InitTypeDef;

/**

  • @brief UART handle Structure definition
    /
    typedef struct __UART_HandleTypeDef
    {
    uint32_t Instance; /
    !< UART registers base address /
    UART_InitTypeDef Init; /
    !< UART communication parameters /
    uint8_t
    pTxBuffPtr; /!< Pointer to UART Tx transfer Buffer /
    uint16_t TxXferSize; /
    !< UART Tx Transfer size /
    volatile uint16_t TxXferCount; /
    !< UART Tx Transfer Counter /
    uint8_t
    pRxBuffPtr; /
    !< Pointer to UART Rx transfer Buffer /
    uint16_t RxXferSize; /
    !< UART Rx Transfer size /
    volatile uint16_t RxXferCount; /
    !< UART Rx Transfer Counter */
    //DMA_HandleTypeDef hdmatx; /!< UART Tx DMA Handle parameters */
    //DMA_HandleTypeDef hdmarx; /!< UART Rx DMA Handle parameters /
    HAL_LockTypeDef Lock; /
    !< Locking object /
    volatile HAL_UART_StateTypeDef gState; /
    !< UART state information related to global Handle management
    and also related to Tx operations.
    This parameter can be a value of @ref HAL_UART_StateTypeDef /
    volatile HAL_UART_StateTypeDef RxState; /
    !< UART state information related to Rx operations.
    This parameter can be a value of @ref HAL_UART_StateTypeDef /
    volatile uint32_t ErrorCode; /
    !< UART Error code */
    #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)

void ( TxHalfCpltCallback)(struct __UART_HandleTypeDef huart); /!< UART Tx Half Complete Callback /
void (
TxCpltCallback)(struct __UART_HandleTypeDef
huart); /!< UART Tx Complete Callback /
void (
RxHalfCpltCallback)(struct __UART_HandleTypeDef
huart); /!< UART Rx Half Complete Callback /
void (
RxCpltCallback)(struct __UART_HandleTypeDef
huart); /!< UART Rx Complete Callback /
void (
ErrorCallback)(struct __UART_HandleTypeDef
huart); /!< UART Error Callback /
void (
AbortCpltCallback)(struct __UART_HandleTypeDef
huart); /!< UART Abort Complete Callback /
void (
AbortTransmitCpltCallback)(struct __UART_HandleTypeDef
huart); /!< UART Abort Transmit Complete Callback /
void (
AbortReceiveCpltCallback)(struct __UART_HandleTypeDef
huart); /!< UART Abort Receive Complete Callback /
void (
WakeupCallback)(struct __UART_HandleTypeDef
huart); /!< UART Wakeup Callback /
void (
MspInitCallback)(struct __UART_HandleTypeDef
huart); /!< UART Msp Init callback /
void (
MspDeInitCallback)(struct __UART_HandleTypeDef
huart); /!< UART Msp DeInit callback /
#endif /
USE_HAL_UART_REGISTER_CALLBACKS /
} UART_HandleTypeDef;
/
* Exported constants -------------------------------------------------------
/
/** @defgroup UART_Error_Code UART Error Code

  • @{
    /
    #define HAL_UART_ERROR_NONE 0x00000000U /
    !< No error /
    #define HAL_UART_ERROR_PE 0x00000001U /
    !< Parity error /
    #define HAL_UART_ERROR_NE 0x00000002U /
    !< Noise error /
    #define HAL_UART_ERROR_FE 0x00000004U /
    !< Frame error /
    #define HAL_UART_ERROR_ORE 0x00000008U /
    !< Overrun error /
    #define HAL_UART_ERROR_DMA 0x00000010U /
    !< DMA transfer error /
    /
    * @}/
    /** Exported macros-----------------------------------------------------------
    /
    #define ARRAYNUM(arr_name) (uint32_t)(sizeof(arr_name)/sizeof((arr_name)))
    /** Exported variables -------------------------------------------------------
    /
    extern UART_HandleTypeDef huart0;
    extern UART_HandleTypeDef huart2;
    extern UART_HandleTypeDef huart4;
    /** Exported functions prototypes --------------------------------------------*/
    void USART0_Init(void);
    void USART0_HalfDuplexInit(void);
    void USART2_Init(void);
    void UART4_Init(void);
    void Usart_DMA_Init(void);

HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef huart);
HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef
huart);
void USER_Stop_Usart(UART_HandleTypeDef huart);
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef
huart, uint8_t pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef
huart, uint8_t pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef
huart, uint8_t pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef
huart, uint8_t pData, uint16_t Size);
HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef
huart);
HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart);

void HAL_UART_IRQHandler(UART_HandleTypeDef huart);
void HAL_UART_TxCpltCallback(UART_HandleTypeDef
huart);
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef huart);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef
huart);
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef huart);
void HAL_UART_ErrorCallback(UART_HandleTypeDef
huart);
void HAL_UART_AbortCpltCallback(UART_HandleTypeDef huart);
void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef
huart);
void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef huart);HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef huart);
uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart);
#ifdef __cplusplus ///<end extern c
}
#endif
#endif
/******************************** End of file *********************************/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221

串口需要的延时

/*!
    \file  systick.c
    \brief the systick configuration file
*/
/*
    Copyright (C) 2016 GigaDevice    2016-10-19, V1.0.0, demo for GD32F4xx
*/
#include "gd32f4xx.h"
#include "systick.h"
static uint32_t delay;volatile uint32_t uwTick = 0;
HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
/*!
    \brief      configure systick
    \param[in]  none
    \param[out] none
    \retval     none
*/
void Systick_Init(void)
{
#if (USE_FREERTOS != 1)
    /* setup systick timer for 1000Hz interrupts */
    if (SysTick_Config(SystemCoreClock / 1000U)){
        /* capture error */
        while (1){
        }
    }
    /* configure the systick handler priority */
    NVIC_SetPriority(SysTick_IRQn, 0x00U);
#endif
}
/*!
    \brief      delay a time in milliseconds
    \param[in]  count: count in milliseconds
    \param[out] none
    \retval     none
*/
void delay_1ms(uint32_t count)
{
    delay = count;    while(0U != delay){
    }
}
/*!
    \brief      delay decrement
    \param[in]  none
    \param[out] none
    \retval     none
*/
void delay_decrement(void)
{
    if (0U != delay){
        delay--;
    }
}
/**
  * @brief Provides a tick value in millisecond.
  * @note This function is declared as __weak to be overwritten in case of other 
  *       implementations in user file.
  * @retval tick value
  */
__weak uint32_t HAL_GetTick(void)
{
  return uwTick;
}
/**
  * @brief This function is called to increment  a global variable "uwTick"
  *        used as application time base.
  * @note In the default implementation, this variable is incremented each 1ms
  *       in SysTick ISR.
 * @note This function is declared as __weak to be overwritten in case of other 
  *      implementations in user file.
  * @retval None
  */
__weak void HAL_IncTick(void)
{
  uwTick += uwTickFreq;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
/*!
    \file  systick.h
    \brief the header file of systick
*/
/*
    Copyright (C) 2016 GigaDevice    2016-10-19, V1.0.0, demo for GD32F4xx
*/
#ifndef SYS_TICK_H
#define SYS_TICK_H
#include <stdint.h>
/** @defgroup HAL_TICK_FREQ Tick Frequency
  * @{
  */
typedef enum
{
  HAL_TICK_FREQ_10HZ         = 100U,
  HAL_TICK_FREQ_100HZ        = 10U,
  HAL_TICK_FREQ_1KHZ         = 1U,
  HAL_TICK_FREQ_DEFAULT      = HAL_TICK_FREQ_1KHZ
} HAL_TickFreqTypeDef;
extern volatile uint32_t uwTick;
/* configure systick */
void Systick_Init(void);
/* delay a time in milliseconds */
void delay_1ms(uint32_t count);
/* delay decrement */
void delay_decrement(void);
uint32_t HAL_GetTick(void);
void HAL_IncTick(void);/*放入1ms中断*/
#endif /* SYS_TICK_H */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

GPIO接口及中断设置接口

/**
 *  @file: main.h
 *
 *  @date: 2020/5/2
 *
 *  @author: aron566
 *
 *  @brief:--main--
 *  
 *  @version:V1.0
 */
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
#include <stdint.h> /**< nedd definition of uint8_t */
#include <stddef.h> /**< need definition of NULL    */
//#include <stdbool.h>/**< need definition of BOOL    */
#include <stdio.h>  /**< if need printf             */
#include <stdlib.h>
#include <string.h>
//#include "printf.h"
/** Private includes ---------------------------------------------------------*/
#include "cmsis_os.h"
#include "gd32f4xx.h"
#include "systick.h"
#define  GPIO_SPEED_FREQ_LOW         GPIO_OSPEED_2MHZ  /*!< IO works at 2 MHz, please refer to the product datasheet */
#define  GPIO_SPEED_FREQ_MEDIUM      GPIO_OSPEED_25MHZ  /*!< range 12,5 MHz to 50 MHz, please refer to the product datasheet */
#define  GPIO_SPEED_FREQ_HIGH        GPIO_OSPEED_50MHZ  /*!< range 25 MHz to 100 MHz, please refer to the product datasheet  */
#define  GPIO_SPEED_FREQ_VERY_HIGH   GPIO_OSPEED_200MHZ  /*!< range 50 MHz to 200 MHz, please refer to the product datasheet  */
/**
  * @}
  */   /** @defgroup GPIO_pull_define GPIO pull define
   * @brief GPIO Pull-Up or Pull-Down Activation
   * @{
   */  
#define  GPIO_NOPULL        GPIO_PUPD_NONE       /*!< No Pull-up or Pull-down activation  */
#define  GPIO_PULLUP        GPIO_PUPD_PULLUP     /*!< Pull-up activation                  */
#define  GPIO_PULLDOWN      GPIO_PUPD_PULLDOWN   /*!< Pull-down activation                */
/*推挽 开漏输出模式*/   
#define  GPIO_MODE_OUTPUT_PP    GPIO_OTYPE_PP
#define  GPIO_MODE_OUTPUT_OD    GPIO_OTYPE_OD 
/**
  * @}
  */

/**

  • @name 退出状态常量
  • @{
    /
    #define EXIT_ERROR -1 /< 退出异常 */
    #define EXIT_OK 0 /
    < 退出正常 /
    /
    @}//** Exported typedefines -----------------------------------------------------/
    /**
  • @brief GPIO Bit SET and Bit RESET enumeration
    /
    typedef enum
    {
    GPIO_PIN_RESET = 0,
    GPIO_PIN_SET
    }GPIO_PinState;
    /
    *
  • @}
    /
    /
    *
  • @brief GPIO Init structure definition
    /
    typedef struct
    {
    uint32_t Pin; /
    !< Specifies the GPIO pins to be configured.
    This parameter can be any value of @ref GPIO_pins_define /
    uint32_t Mode; /
    !< Specifies the operating mode for the selected pins.
    This parameter can be a value of @ref GPIO_mode_define /
    uint32_t OutMode;
    uint32_t Pull; /
    !< Specifies the Pull-up or Pull-Down activation for the selected pins.
    This parameter can be a value of @ref GPIO_pull_define /
    uint32_t Speed; /
    !< Specifies the speed for the selected pins.
    This parameter can be a value of @ref GPIO_speed_define /
    uint32_t Alternate; /
    !< Peripheral to be connected to the selected pins.
    This parameter can be a value of @ref GPIO_Alternate_function_selection /
    }GPIO_InitTypeDef;
    /
    *
  • @name 芯片96位唯一ID
  • @{
    /
    typedef struct
    {
    uint32_t UUID[3];
    }UUID_t;
    /
    * @}/
    /** Exported constants -------------------------------------------------------
    /
    /** Exported macros-----------------------------------------------------------/
    #define CHIP_UUID ((UUID_t
    )0x1FFF7A10)/*< 芯片96位唯一ID /
    /
    Exported functions prototypes --------------------------------------------/
    /
    GPIO配置/
    void HAL_GPIO_Init(uint32_t GPIOx, GPIO_InitTypeDef
    GPIO_Init);
    /翻转输出脚/
    void HAL_GPIO_TogglePin(uint32_t GPIOx ,uint32_t GPIO_Pin);
    /读取GPIO脚/
    GPIO_PinState HAL_GPIO_ReadPin(uint32_t GPIOx ,uint32_t GPIO_Pin);
    /控制输出GPIO脚/
    void HAL_GPIO_WritePin(uint32_t GPIOx ,uint32_t GPIO_Pin, GPIO_PinState PinState);

/设置中断优先级分组/
void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup);
/设置中断优先级/
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority);
/使能中断/
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn);
/失能中断/
void HAL_NVIC_DisableIRQ(IRQn_Type IRQn);
/系统重启/
void HAL_NVIC_SystemReset(void);
/设置systick数/
uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb);
#ifdef __cplusplus ///<end extern c
}
#endif
#endif
/******************************** End of file *********************************/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
/**
  ******************************************************************
  * @brief   配置 GPIO
  * @param[in]  GPIO端口 GPIOx
  * @param[in]  GPIO_Init GPIO参数
  * @retval  None
  * @author  aron566
  * @version v1.0
  * @date    2020/5/6
  ******************************************************************
  */
void HAL_GPIO_Init(uint32_t GPIOx, GPIO_InitTypeDef *GPIO_Init)
{
    /* configure GPIO port */ 
    gpio_mode_set(GPIOx ,GPIO_Init->Mode ,GPIO_Init->Pull ,GPIO_Init->Pin);
    if(GPIO_Init->Mode == GPIO_MODE_OUTPUT || GPIO_Init->Mode == GPIO_MODE_AF)
    {
        gpio_output_options_set(GPIOx ,GPIO_Init->OutMode ,GPIO_Init->Speed ,GPIO_Init->Pin);
        if(GPIO_Init->Mode == GPIO_MODE_OUTPUT)
        {
        	GPIO_BC(GPIOx) = GPIO_Init->Pin;
        }
    }
}
/**
  ******************************************************************
  * @brief   翻转 GPIO
  * @param[in]  GPIO端口 GPIOx
  * @param[in]  GPIO_Pin GPIO参数
  * @retval  None
  * @author  aron566
  * @version v1.0
  * @date    2020/5/6
  ******************************************************************
  */
void HAL_GPIO_TogglePin(uint32_t GPIOx ,uint32_t GPIO_Pin)
{
    GPIO_TG(GPIOx) = GPIO_Pin;
}
/**
  ******************************************************************
  * @brief   HAL接口写入引脚状态
  * @param[in]   gpio_port
  * @param[in]   gpio_pin_num
  * @param[in]   GPIO_PIN_SET  HI   GPIO_PIN_RESET LOW
  * @retval       None
  * @author  aron566
  * @version v1.0
  * @date    2020/5/9
  ******************************************************************
  */
void HAL_GPIO_WritePin(uint32_t GPIOx ,uint32_t GPIO_Pin ,GPIO_PinState PinState)
{
    gpio_bit_write(GPIOx ,GPIO_Pin ,(bit_status)PinState);
}

/**


  • @brief HAL接口读取引脚状态
  • @param[in] gpio_port
  • @param[in] gpio_pin_num
  • @retval GPIO_PIN_SET HI
  • @retval GPIO_PIN_RESET LOW
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

*/
GPIO_PinState HAL_GPIO_ReadPin(uint32_t GPIOx ,uint32_t GPIO_Pin)
{
if(gpio_input_bit_get(GPIOx, GPIO_Pin))
{
return GPIO_PIN_SET;
}
else
{
return GPIO_PIN_RESET;
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

中断设置

/*中断映射 */
#define NVIC_PRIORITYGROUP_0	NVIC_PRIGROUP_PRE0_SUB4
#define NVIC_PRIORITYGROUP_1	NVIC_PRIGROUP_PRE1_SUB3
#define NVIC_PRIORITYGROUP_2	NVIC_PRIGROUP_PRE2_SUB2
#define NVIC_PRIORITYGROUP_3	NVIC_PRIGROUP_PRE3_SUB1
#define NVIC_PRIORITYGROUP_4	NVIC_PRIGROUP_PRE4_SUB0
/**  
* @brief  Sets the priority grouping field (preemption priority and subpriority)  
* 
*         using the required unlock sequence.  
* 
* @param  PriorityGroup The priority grouping bits length.  
* 
*         This parameter can be one of the following values:  
*         @arg NVIC_PRIORITYGROUP_0: 0 bits for preemption priority  
*                                    4 bits for subpriority  
*         @arg NVIC_PRIORITYGROUP_1: 1 bits for preemption priority  
*                                    3 bits for subpriority  
*         @arg NVIC_PRIORITYGROUP_2: 2 bits for preemption priority  
*                                    2 bits for subpriority  
*         @arg NVIC_PRIORITYGROUP_3: 3 bits for preemption priority  
*                                    1 bits for subpriority  
*         @arg NVIC_PRIORITYGROUP_4: 4 bits for preemption priority  
*                                    0 bits for subpriority  
* @note   When the NVIC_PriorityGroup_0 is selected, IRQ preemption is no more possible.  
*         The pending IRQ priority will be managed only by the subpriority.  
* @retval None  
*/

void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
{
/* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value /
NVIC_SetPriorityGrouping(PriorityGroup);
}
/
*

  • @brief Sets the priority of an interrupt.
  • @param IRQn External interrupt number.
  •     This parameter can be an enumerator of IRQn_Type enumeration
    
  •     (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32f4xxxx.h))
    
  • @param PreemptPriority The preemption priority for the IRQn channel.
  •     This parameter can be a value between 0 and 15
    
  •     A lower priority value indicates a higher priority 
    
  • @param SubPriority the subpriority level for the IRQ channel.
  •     This parameter can be a value between 0 and 15
    
  •     A lower priority value indicates a higher priority.          
    
  • @retval None
    */
    void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
    {
    uint32_t prioritygroup = 0x00U;

prioritygroup = NVIC_GetPriorityGrouping();

NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
}
/**

  • @brief Enables a device specific interrupt in the NVIC interrupt controller.
  • @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
  •     function should be called before. 
    
  • @param IRQn External interrupt number.
  •     This parameter can be an enumerator of IRQn_Type enumeration
    
  •     (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32f4xxxx.h))
    
  • @retval None
    /
    void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
    {
    /
    Enable interrupt /
    NVIC_EnableIRQ(IRQn);
    }
    /
    *
  • @brief Disables a device specific interrupt in the NVIC interrupt controller.
  • @param IRQn External interrupt number.
  •     This parameter can be an enumerator of IRQn_Type enumeration
    
  •     (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32f4xxxx.h))
    
  • @retval None
    /
    void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
    {
    /
    Disable interrupt /
    NVIC_DisableIRQ(IRQn);
    }
    /
    *
  • @brief Initiates a system reset request to reset the MCU.
  • @retval None
    /
    void HAL_NVIC_SystemReset(void)
    {
    /
    System Reset /
    NVIC_SystemReset();
    }
    /
    *
  • @brief Initializes the System Timer and its interrupt, and starts the System Tick Timer.
  •     Counter is in free running mode to generate periodic interrupts.
    
  • @param TicksNumb Specifies the ticks Number of ticks between two interrupts.
  • @retval status: - 0 Function succeeded.
  •              - 1  Function failed.
    

*/
uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb)
{
return SysTick_Config(TicksNumb);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

配置端口输出例子

/**
  ******************************************************************
  * @brief   LED配置 PG3  ***PD4 PD5*** 
  * @param   None
  * @retval  None
  * @author  aron566
  * @version v1.0
  * @date    2020/5/6
  ******************************************************************
  */
#define RUNNING_LOW_Pin GPIO_PIN_3          /*运行指示灯pin*/
#define RUNNING_LOW_GPIO_Port GPIOG         /*运行指示灯   */
void LED_Init(void)
{
    /* enable the led clock */
    rcu_periph_clock_enable(RCU_GPIOD);
    rcu_periph_clock_enable(RCU_GPIOG);
    /* configure led GPIO port */ 
    GPIO_InitTypeDef    GPIOConfig = {0};
    GPIOConfig.Mode = GPIO_MODE_OUTPUT;
    GPIOConfig.Pin = RUNNING_LOW_Pin;        /*运行指示灯*/
    GPIOConfig.Speed = GPIO_SPEED_FREQ_LOW;
    GPIOConfig.Pull = GPIO_NOPULL;
    GPIOConfig.OutMode = GPIO_MODE_OUTPUT_PP;/*推挽输出模式*/
    HAL_GPIO_Init(RUNNING_LOW_GPIO_Port ,&GPIOConfig);/*led3 配置*/
GPIOConfig<span class="token punctuation">.</span>Pin <span class="token operator">=</span> GPIO_PIN_4<span class="token punctuation">;</span>
<span class="token function">HAL_GPIO_Init</span><span class="token punctuation">(</span>GPIOD <span class="token punctuation">,</span><span class="token operator">&amp;</span>GPIOConfig<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/*led1 配置*/</span>

GPIOConfig<span class="token punctuation">.</span>Pin <span class="token operator">=</span> GPIO_PIN_5<span class="token punctuation">;</span>
<span class="token function">HAL_GPIO_Init</span><span class="token punctuation">(</span>GPIOD <span class="token punctuation">,</span><span class="token operator">&amp;</span>GPIOConfig<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/*led2 配置*/</span>     

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

FLASH接口

/**
 *  @file: flsh.c
 *
 *  @date: 2020/5/9
 *
 *  @author: aron566
 *
 *  @brief:Flash opt
 *
 *  @version:v1.0
 */
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "flash.h"
#include "systick.h"
/** Private typedef ----------------------------------------------------------*/
/** Private macros -----------------------------------------------------------*/
/** Private constants --------------------------------------------------------*/
/** Public variables ---------------------------------------------------------*/
/** Private variables --------------------------------------------------------*/
/** @addtogroup FLASH_Private_Variables
  * @{
  */
/* Variable used for Erase sectors under interruption */
FLASH_ProcessTypeDef pFlash;
/**
  * @}
  */
/** Private function prototypes ----------------------------------------------*/
static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
static void FLASH_SetErrorCode(void);
/** Private user code --------------------------------------------------------*/
/** Private application code -------------------------------------------------*/
/*******************************************************************************
*
*       Static code
*
********************************************************************************
*/
/**
  * @brief  Set the specific FLASH error flag.
  * @retval None
  */
static void FLASH_SetErrorCode(void)
{ 
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
  {
   pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;

/* Clear FLASH write protection error pending bit */
fmc_flag_clear(FLASH_FLAG_WRPERR);
}

if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;

/* Clear FLASH Programming alignment error pending bit */
fmc_flag_clear(FLASH_FLAG_PGAERR);
}

if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;

<span class="token comment">/* Clear FLASH Programming parallelism error pending bit */</span>
<span class="token function">fmc_flag_clear</span><span class="token punctuation">(</span>FLASH_FLAG_PGPERR<span class="token punctuation">)</span><span class="token punctuation">;</span>

}

if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;

<span class="token comment">/* Clear FLASH Programming sequence error pending bit */</span>
<span class="token function">fmc_flag_clear</span><span class="token punctuation">(</span>FLASH_FLAG_PGSERR<span class="token punctuation">)</span><span class="token punctuation">;</span>

}
#if defined(FLASH_SR_RDERR)
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;

<span class="token comment">/* Clear FLASH Proprietary readout protection error pending bit */</span>
<span class="token function">fmc_flag_clear</span><span class="token punctuation">(</span>FLASH_FLAG_RDERR<span class="token punctuation">)</span><span class="token punctuation">;</span>

}
#endif /* FLASH_SR_RDERR */
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
{
pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;

<span class="token comment">/* Clear FLASH Operation error pending bit */</span>
<span class="token function">fmc_flag_clear</span><span class="token punctuation">(</span>FLASH_FLAG_OPERR<span class="token punctuation">)</span><span class="token punctuation">;</span>

}
}
/**

  • @}
    //*
  • @brief Program a double word (64-bit) at a specified address.
  • @note This function must be used when the device voltage range is from
  •     2.7V to 3.6V and Vpp in the range 7V to 9V.
    
  • @note If an erase and a program operations are requested simultaneously,
  •     the erase operation is performed before the program one.
    
  • @param Address specifies the address to be programmed.
  • @param Data specifies the data to be programmed.
  • @retval None
    /
    static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
    {
    /
    Program first word /
    fmc_word_program(Address, (uint32_t) Data);
    /
    Program second word /
    fmc_word_program(Address ,(uint32_t)(Data >> 32));
    }
    /
    * Public application code --------------------------------------------------/
    /
    ******************************************************************************
  •   Public code
    

/
/
*

  • @brief Get the specified FLASH flag status.
  • @param FLAG specifies the FLASH flags to check.
  •      This parameter can be any combination of the following values:
    
  •        @arg FLASH_FLAG_EOP   : FLASH End of Operation flag 
    
  •        @arg FLASH_FLAG_OPERR : FLASH operation Error flag 
    
  •        @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag 
    
  •        @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
    
  •        @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
    
  •        @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
    
  •        @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP) (*)
    
  •        @arg FLASH_FLAG_BSY   : FLASH Busy flag
    
  •       (*) FLASH_FLAG_RDERR is not available for STM32F405xx/407xx/415xx/417xx devices                             
    
  • @retval The new state of FLAG (SET or RESET).
    /
    FlagStatus __HAL_FLASH_GET_FLAG(uint32_t fmc_flag)
    {
    return fmc_flag_get(fmc_flag);
    }
    /
    *

  • @brief Flash编程接口
  • @param[in] 写入类型
  • @param[in] 写入地址
  • @param[in] 写入数据
  • @retval HAL_OK 成功
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
/
*

  • @brief Program byte, halfword, word or double word at a specified address
  • @param TypeProgram Indicate the way to program at a specified address.
  •                       This parameter can be a value of @ref FLASH_Type_Program
    
  • @param Address specifies the address to be programmed.
  • @param Data specifies the data to be programmed
  • @retval HAL_StatusTypeDef HAL Status
    */
    HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
    {
    HAL_StatusTypeDef status = HAL_ERROR;

/* Process Locked */
__HAL_LOCK(&pFlash);

/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

if(status HAL_OK)
{
if(TypeProgram FLASH_TYPEPROGRAM_BYTE)
{
/Program byte (8-bit) at a specified address./
fmc_byte_program(Address, (uint8_t) Data);
}
else if(TypeProgram FLASH_TYPEPROGRAM_HALFWORD)
{
/Program halfword (16-bit) at a specified address./
fmc_halfword_program(Address, (uint16_t) Data);
}
else if(TypeProgram FLASH_TYPEPROGRAM_WORD)
{
/Program word (32-bit) at a specified address./
fmc_word_program(Address, (uint32_t) Data);
}
else
{
/Program double word (64-bit) at a specified address./
FLASH_Program_DoubleWord(Address, Data);
}

<span class="token comment">/* Wait for last operation to be completed */</span>
status <span class="token operator">=</span> <span class="token function">FLASH_WaitForLastOperation</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">uint32_t</span><span class="token punctuation">)</span>FLASH_TIMEOUT_VALUE<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* If the program operation is completed, disable the PG Bit */</span>

}

/* Process Unlocked */
__HAL_UNLOCK(&pFlash);

return status;
}

/**


  • @brief Flash 单字节编程接口
  • @param [in]Address 写入的地址
  • @param [in]Data 写入的数据
  • @retval HAL_OK 成功
  • @author aron566
  • @version V1.0
  • @date 2020-6-2

/
HAL_StatusTypeDef FLASH_ProgramByte(uint32_t Address, uint8_t Data)
{
HAL_StatusTypeDef status = HAL_ERROR;
/
Process Locked */
__HAL_LOCK(&pFlash);

/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

if(status == HAL_OK)
{
/Program byte (8-bit) at a specified address./
fmc_byte_program(Address, (uint8_t) Data);

  <span class="token comment">/* Wait for last operation to be completed */</span>
  status <span class="token operator">=</span> <span class="token function">FLASH_WaitForLastOperation</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">uint32_t</span><span class="token punctuation">)</span>FLASH_TIMEOUT_VALUE<span class="token punctuation">)</span><span class="token punctuation">;</span>

/* If the program operation is completed, disable the PG Bit */

}

/* Process Unlocked */
__HAL_UNLOCK(&pFlash);

return status;
}

/**

  • @brief Unlock the FLASH control register access
  • @retval HAL Status
    /
    HAL_StatusTypeDef HAL_FLASH_Unlock(void)
    {
    fmc_unlock();
    return HAL_OK;
    }
    /
    *
  • @brief Locks the FLASH control register access
  • @retval HAL Status
    /
    HAL_StatusTypeDef HAL_FLASH_Lock(void)
    {
    /
    Set the LOCK Bit to lock the FLASH Registers access */
    fmc_lock();

return HAL_OK;
}
/**

  • @brief Get the specific FLASH error flag.
  • @retval FLASH_ErrorCode: The returned value can be a combination of:
  •        @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
    
  •        @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag 
    
  •        @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag  
    
  •        @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
    
  •        @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
    
  •        @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag 
    

*/
uint32_t HAL_FLASH_GetError(void)
{
return pFlash.ErrorCode;
}

/**

  • @}
    / /*
  • @brief Wait for a FLASH operation to complete.
  • @param Timeout maximum flash operationtimeout
  • @retval HAL Status
    */
    HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
    {
    uint32_t tickstart = 0U;

/* Clear Error Code */
pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

/* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
Even if the FLASH operation fails, the BUSY flag will be reset and an error
flag will be set /

/
Get tick /
tickstart = HAL_GetTick();
while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
{
if(Timeout != HAL_MAX_DELAY)
{
if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
{
return HAL_TIMEOUT;
}
}
}
/
Check FLASH End of Operation flag /
if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
{
/
Clear FLASH End of Operation pending bit /
fmc_flag_clear(FLASH_FLAG_EOP);
}
#if defined(FLASH_SR_RDERR)
if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
#else
if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
#endif /
FLASH_SR_RDERR /
{
/
Save the error code/
FLASH_SetErrorCode();
return HAL_ERROR;
}
/
If there is no error flag set */
return HAL_OK;

}
/**

  • @brief Perform a mass erase or erase the specified FLASH memory sectors
  • @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  •     contains the configuration information for the erasing.
    
  • @param[out] SectorError pointer to variable that
  •     contains the configuration information on faulty sector in case of error 
    
  •     (0xFFFFFFFFU means that all the sectors have been correctly erased)
    
  • @retval HAL Status
    /
    HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef
    pEraseInit, uint32_t *SectorError)
    {
    HAL_StatusTypeDef status = HAL_ERROR;
    uint32_t index = 0U;

/* Process Locked /
__HAL_LOCK(&pFlash);
/
Wait for last operation to be completed /
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
if(status == HAL_OK)
{
/
Initialization of SectorError variable/
SectorError = 0xFFFFFFFFU;

<span class="token keyword">if</span><span class="token punctuation">(</span>pEraseInit<span class="token operator">-&gt;</span>TypeErase <span class="token operator">==</span> FLASH_TYPEERASE_MASSERASE<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
  <span class="token comment">/*Mass erase to be done*/</span>
  <span class="token keyword">if</span><span class="token punctuation">(</span>pEraseInit<span class="token operator">-&gt;</span>Banks <span class="token operator">==</span> FLASH_BANK_1<span class="token punctuation">)</span>
  <span class="token punctuation">{<!-- --></span>
      <span class="token function">fmc_bank0_erase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  
  <span class="token keyword">if</span><span class="token punctuation">(</span>pEraseInit<span class="token operator">-&gt;</span>Banks <span class="token operator">==</span> FLASH_BANK_2<span class="token punctuation">)</span>
  <span class="token punctuation">{<!-- --></span>
      <span class="token function">fmc_bank1_erase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  
  <span class="token keyword">if</span><span class="token punctuation">(</span>pEraseInit<span class="token operator">-&gt;</span>Banks <span class="token operator">==</span> FLASH_BANK_BOTH<span class="token punctuation">)</span>
  <span class="token punctuation">{<!-- --></span>
      <span class="token function">fmc_mass_erase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token comment">/* Wait for last operation to be completed */</span>
  status <span class="token operator">=</span> <span class="token function">FLASH_WaitForLastOperation</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">uint32_t</span><span class="token punctuation">)</span>FLASH_TIMEOUT_VALUE<span class="token punctuation">)</span><span class="token punctuation">;</span>
  
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{<!-- --></span>      
<span class="token comment">/* Erase by sector by sector to be done*/</span>
  <span class="token keyword">for</span><span class="token punctuation">(</span>index <span class="token operator">=</span> pEraseInit<span class="token operator">-&gt;</span>Sector<span class="token punctuation">;</span> index <span class="token operator">&lt;</span> <span class="token punctuation">(</span>pEraseInit<span class="token operator">-&gt;</span>NbSectors <span class="token operator">+</span> pEraseInit<span class="token operator">-&gt;</span>Sector<span class="token punctuation">)</span><span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span>
  <span class="token punctuation">{<!-- --></span>
    <span class="token function">fmc_sector_erase</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>        
    <span class="token comment">/* Wait for last operation to be completed */</span>
    status <span class="token operator">=</span> <span class="token function">FLASH_WaitForLastOperation</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">uint32_t</span><span class="token punctuation">)</span>FLASH_TIMEOUT_VALUE<span class="token punctuation">)</span><span class="token punctuation">;</span>        
    <span class="token keyword">if</span><span class="token punctuation">(</span>status <span class="token operator">!=</span> HAL_OK<span class="token punctuation">)</span> 
    <span class="token punctuation">{<!-- --></span>
      <span class="token comment">/* In case of error, stop erase procedure and return the faulty sector*/</span>
      <span class="token operator">*</span>SectorError <span class="token operator">=</span> index<span class="token punctuation">;</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">/* Flush the caches to be sure of the data consistency */</span>

// FLASH_FlushCaches();
} /* Process Unlocked */
__HAL_UNLOCK(&pFlash);
return status;
}

#ifdef __cplusplus ///<end extern c
}
#endif
/******************************** End of file *********************************/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
/**
 *  @file: flash.h
 *
 *  @date: 2020/5/9
 *
 *  @author: aron566
 *
 *  @brief:Flash opt
 *  
 *  @version:v1.0
 */
#ifndef FLASH_OPT_H
#define FLASH_OPT_H
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
#include <stdint.h> /**< nedd definition of uint8_t */
#include <stddef.h> /**< need definition of NULL    */
//#include <stdbool.h>/**< need definition of BOOL    */
#include <stdio.h>  /**< if need printf             */
#include <stdlib.h>
#include <string.h>
/** Private includes ---------------------------------------------------------*/
#include "gd32f4xx.h"
#include "HAL_Typedef.h"
/** Private defines ----------------------------------------------------------*/
/** @addtogroup FLASHEx_Private_Constants
  * @{
  */    
#define FLASH_TIMEOUT_VALUE       50000U /* 50 s */
/**
  * @}
  */

/** @addtogroup HAL库flash 扇区映射

  • @{
    /
    #define FLASH_SECTOR_0 CTL_SECTOR_NUMBER_0 /
    !< Sector Number 0 /
    #define FLASH_SECTOR_1 CTL_SECTOR_NUMBER_1 /
    !< Sector Number 1 /
    #define FLASH_SECTOR_2 CTL_SECTOR_NUMBER_2 /
    !< Sector Number 2 /
    #define FLASH_SECTOR_3 CTL_SECTOR_NUMBER_3 /
    !< Sector Number 3 /
    #define FLASH_SECTOR_4 CTL_SECTOR_NUMBER_4 /
    !< Sector Number 4 /
    #define FLASH_SECTOR_5 CTL_SECTOR_NUMBER_5 /
    !< Sector Number 5 /
    #define FLASH_SECTOR_6 CTL_SECTOR_NUMBER_6 /
    !< Sector Number 6 /
    #define FLASH_SECTOR_7 CTL_SECTOR_NUMBER_7 /
    !< Sector Number 7 /
    #define FLASH_SECTOR_8 CTL_SECTOR_NUMBER_8 /
    !< Sector Number 8 /
    #define FLASH_SECTOR_9 CTL_SECTOR_NUMBER_9 /
    !< Sector Number 9 /
    #define FLASH_SECTOR_10 CTL_SECTOR_NUMBER_10 /
    !< Sector Number 10 /
    #define FLASH_SECTOR_11 CTL_SECTOR_NUMBER_11 /
    !< Sector Number 11 /
    #define FLASH_SECTOR_12 CTL_SECTOR_NUMBER_12 /
    !< Sector Number 12 /
    #define FLASH_SECTOR_13 CTL_SECTOR_NUMBER_13 /
    !< Sector Number 13 /
    #define FLASH_SECTOR_14 CTL_SECTOR_NUMBER_14 /
    !< Sector Number 14 /
    #define FLASH_SECTOR_15 CTL_SECTOR_NUMBER_15 /
    !< Sector Number 15 /
    #define FLASH_SECTOR_16 CTL_SECTOR_NUMBER_16 /
    !< Sector Number 16 /
    #define FLASH_SECTOR_17 CTL_SECTOR_NUMBER_17 /
    !< Sector Number 17 /
    #define FLASH_SECTOR_18 CTL_SECTOR_NUMBER_18 /
    !< Sector Number 18 /
    #define FLASH_SECTOR_19 CTL_SECTOR_NUMBER_19 /
    !< Sector Number 19 /
    #define FLASH_SECTOR_20 CTL_SECTOR_NUMBER_20 /
    !< Sector Number 20 /
    #define FLASH_SECTOR_21 CTL_SECTOR_NUMBER_21 /
    !< Sector Number 21 /
    #define FLASH_SECTOR_22 CTL_SECTOR_NUMBER_22 /
    !< Sector Number 22 /
    #define FLASH_SECTOR_23 CTL_SECTOR_NUMBER_23 /
    !< Sector Number 23 /
    #define FLASH_SECTOR_24 CTL_SECTOR_NUMBER_24 /
    !< Sector Number 24 /
    #define FLASH_SECTOR_25 CTL_SECTOR_NUMBER_25 /
    !< Sector Number 25 /
    #define FLASH_SECTOR_26 CTL_SECTOR_NUMBER_26 /
    !< Sector Number 26 /
    #define FLASH_SECTOR_27 CTL_SECTOR_NUMBER_27 /
    !< Sector Number 27 /
    #define FLASH_SECTOR_28 CTL_SECTOR_NUMBER_28 /
    !< Sector Number 28 /
    #define FLASH_SECTOR_29 CTL_SECTOR_NUMBER_29 /
    !< Sector Number 29 /
    #define FLASH_SECTOR_30 CTL_SECTOR_NUMBER_30 /
    !< Sector Number 30 /
    /
    * @defgroup FLASHEx_Type_Erase FLASH Type Erase
  • @{
    /
    #define FLASH_TYPEERASE_SECTORS 0x00000000U /
    !< Sectors erase only /
    #define FLASH_TYPEERASE_MASSERASE 0x00000001U /
    !< Flash Mass erase activation /
    /
    *
  • @}
    /
    #define FLASH_BANK_1 1U /
    !< Bank 1 /
    #define FLASH_BANK_2 2U /
    !< Bank 2 /
    #define FLASH_BANK_BOTH ((uint32_t)FLASH_BANK_1 | FLASH_BANK_2) /
    !< Bank1 and Bank2 */

/** @defgroup FLASHEx_Voltage_Range FLASH Voltage Range

  • @{
    /
    #define FLASH_VOLTAGE_RANGE_1 0x00000000U /
    !< Device operating range: 1.8V to 2.1V /
    #define FLASH_VOLTAGE_RANGE_2 0x00000001U /
    !< Device operating range: 2.1V to 2.7V /
    #define FLASH_VOLTAGE_RANGE_3 0x00000002U /
    !< Device operating range: 2.7V to 3.6V /
    #define FLASH_VOLTAGE_RANGE_4 0x00000003U /
    !< Device operating range: 2.7V to 3.6V + External Vpp /
    /
    *
  • @}
    */

/** @name 映射HAL库与GD标准库Flash编程方式

  • @{
    /
    #define FLASH_TYPEPROGRAM_BYTE 0x00000000U /
    !< Program byte (8-bit) at a specified address /
    #define FLASH_TYPEPROGRAM_HALFWORD 0x00000001U /
    !< Program a half-word (16-bit) at a specified address /
    #define FLASH_TYPEPROGRAM_WORD 0x00000002U /
    !< Program a word (32-bit) at a specified address /
    #define FLASH_TYPEPROGRAM_DOUBLEWORD 0x00000003U /
    !< Program a double word (64-bit) at a specified address /
    /
    *
  • @}
    /
    /
    *
  • @name 映射HAL库与GD标准库Flash标志
  • @{
    /
    #define FLASH_FLAG_EOP FMC_FLAG_END /
    !< FLASH End of Operation flag /
    #define FLASH_FLAG_OPERR FMC_FLAG_OPERR /
    !< FLASH operation Error flag /
    #define FLASH_FLAG_WRPERR FMC_FLAG_WPERR /
    !< FLASH Write protected error flag /
    #define FLASH_FLAG_PGAERR FMC_FLAG_PGMERR /
    !< FLASH Programming Alignment error flag /
    #define FLASH_FLAG_PGPERR 0 /
    !< FLASH Programming Parallelism error flag /
    #define FLASH_FLAG_PGSERR FMC_FLAG_PGSERR /
    !< FLASH Programming Sequence error flag /
    #define FLASH_FLAG_RDERR FMC_FLAG_RDDERR /
    !< Read Protection error flag (PCROP) /
    #define FLASH_FLAG_BSY FMC_FLAG_BUSY /
    !< FLASH Busy flag /
    /
    * @}*/

/** @defgroup FLASH_Error_Code FLASH Error Code

  • @brief FLASH Error Code
  • @{
    /
    #define HAL_FLASH_ERROR_NONE 0x00000000U /
    !< No error /
    #define HAL_FLASH_ERROR_RD 0x00000001U /
    !< Read Protection error /
    #define HAL_FLASH_ERROR_PGS 0x00000002U /
    !< Programming Sequence error /
    #define HAL_FLASH_ERROR_PGP 0x00000004U /
    !< Programming Parallelism error /
    #define HAL_FLASH_ERROR_PGA 0x00000008U /
    !< Programming Alignment error /
    #define HAL_FLASH_ERROR_WRP 0x00000010U /
    !< Write protection error /
    #define HAL_FLASH_ERROR_OPERATION 0x00000020U /
    !< Operation Error /
    /
    *
  • @}
    */

/** Exported typedefines -----------------------------------------------------/
/** 数据结构体
/
/**

  • @brief FLASH Procedure structure definition
    /
    typedef enum
    {
    FLASH_PROC_NONE = 0U,
    FLASH_PROC_SECTERASE,
    FLASH_PROC_MASSERASE,
    FLASH_PROC_PROGRAM
    } FLASH_ProcedureTypeDef;/
    *
  • @brief FLASH handle Structure definition
    /
    typedef struct
    {
    volatile FLASH_ProcedureTypeDef ProcedureOnGoing; /
    Internal variable to indicate which procedure is ongoing or not in IT context*/

volatile uint32_t NbSectorsToErase; /Internal variable to save the remaining sectors to erase in IT context/

volatile uint8_t VoltageForErase; /Internal variable to provide voltage range selected by user in IT context/

volatile uint32_t Sector; /Internal variable to define the current sector which is erasing/

volatile uint32_t Bank; /Internal variable to save current bank selected during mass erase/

volatile uint32_t Address; /Internal variable to save address selected for program/

HAL_LockTypeDef Lock; /* FLASH locking object / volatile uint32_t ErrorCode; / FLASH error code /
}FLASH_ProcessTypeDef;
/
*

  • @}
    //*
  • @brief FLASH Erase structure definition
    /
    typedef struct
    {
    uint32_t TypeErase; /
    !< Mass erase or sector Erase.
    This parameter can be a value of @ref FLASHEx_Type_Erase /
    uint32_t Banks; /
    !< Select banks to erase when Mass erase is enabled.
    This parameter must be a value of @ref FLASHEx_Banks /
    uint32_t Sector; /
    !< Initial FLASH sector to erase when Mass erase is disabled
    This parameter must be a value of @ref FLASHEx_Sectors /
    uint32_t NbSectors; /
    !< Number of sectors to be erased.
    This parameter must be a value between 1 and (max number of sectors - value of Initial sector)/
    uint32_t VoltageRange;/
    !< The device voltage range which defines the erase parallelism
    This parameter must be a value of @ref FLASHEx_Voltage_Range /
    } FLASH_EraseInitTypeDef;
    /
    * Exported constants -------------------------------------------------------/
    /** Exported macros-----------------------------------------------------------
    /
    /** Exported variables -------------------------------------------------------/
    /** Exported functions prototypes --------------------------------------------
    /
    FlagStatus __HAL_FLASH_GET_FLAG(uint32_t fmc_flag);
    HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
    HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef pEraseInit, uint32_t SectorError);
    HAL_StatusTypeDef HAL_FLASH_Unlock(void);
    HAL_StatusTypeDef HAL_FLASH_Lock(void);
    uint32_t HAL_FLASH_GetError(void);
    HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
    HAL_StatusTypeDef FLASH_ProgramByte(uint32_t Address, uint8_t Data);
    #ifdef __cplusplus ///<end extern c
    }
    #endif
    #endif
    /******************************** End of file *********************************/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197

Flash测试代码

//此数组占用Sector2、Sector3用于存放数据
#if defined (__ICCARM__) 
__root __no_init const char data_in_flash[ 2 * 16 * 1024 ] @0x08008000; 
#elif defined (__CC_ARM)
const char data_in_flash[ 2 * 16 * 1024 ] __attribute__((at(0x08008000))) = {0};
#endif
/*测试向Flash写入数据,复位后数据应依然存在*/
void flashTEST(void)
{
        FLASH_EraseInitTypeDef config;
        uint32_t ERROR = 0;
        config.Banks = FLASH_BANK_1;
        config.NbSectors = 1;
        config.TypeErase = FLASH_TYPEERASE_SECTORS;
        config.VoltageRange = FLASH_VOLTAGE_RANGE_4;
        config.Sector = FLASH_SECTOR_2;
        static uint32_t data = 0;
        data = *(uint32_t *)0x08008000;
        HAL_FLASH_Unlock();                
//        /*判断数据是否写入*/
        if((data&0xFF) != 0x55)
        {
                HAL_FLASHEx_Erase(&config, &ERROR);
                /*写入数据*/
                HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x08008000, 0x55);
        }
        else
        {
            data <span class="token operator">=</span> <span class="token operator">*</span><span class="token punctuation">(</span><span class="token class-name">uint32_t</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token number">0x08008000</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">HAL_FLASH_Lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

看门狗非HAL接口

/**
 *  @file: fwdg.c
 *
 *  @date: 2020/5/7
 *
 *  @author: aron566
 *
 *  @brief:fwdg init
 */
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "fwdg.h"
#include "gd32f4xx.h"
/** Private typedef ----------------------------------------------------------*/
/** Private macros -----------------------------------------------------------*/
/** Private constants --------------------------------------------------------*/
/** Public variables ---------------------------------------------------------*/
/** Private variables --------------------------------------------------------*/
/** Private function prototypes ----------------------------------------------*/
/** Private user code --------------------------------------------------------*/
/** Public application code --------------------------------------------------*/
/*******************************************************************************
*
*       Public code
*
********************************************************************************
*/
/**
  ******************************************************************
  * @brief   初始化看门狗
  * @param   None
  * @retval  None
  * @author  aron566
  * @version v1.0
  * @date    2020/5/7
  ******************************************************************
  */
void Fwdg_Init(void)
{
    fwdgt_config((uint16_t)(FWDG_CYCLE_S*250)-1 ,FWDGT_PSC_DIV128);
    fwdgt_enable();
}/**
  ******************************************************************
  * @brief   喂狗
  * @param   None
  * @retval  None
  * @author  aron566
  * @version v1.0
  * @date    2020/5/7
  ******************************************************************
  */
void Feed_Fwdg(void)
{
    fwdgt_counter_reload();
}
#ifdef __cplusplus ///<end extern c
}
#endif
/******************************** End of file *********************************/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
/**
 *  @file: fwdg.h
 *
 *  @date: 2020/5/7
 *
 *  @author: aron566
 *
 *  @brief:fwdg init
 *  
 *  @version:
 */
#ifndef __FWDG_H
#define __FWDG_H
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
#include <stdint.h> /**< nedd definition of uint8_t */
#include <stddef.h> /**< need definition of NULL    */
//#include <stdbool.h>/**< need definition of BOOL    */
#include <stdio.h>  /**< if need printf             */
#include <stdlib.h>
#include <string.h>
/** Private includes ---------------------------------------------------------*/
/** Private defines ----------------------------------------------------------*/
#define FWDG_CYCLE_S        5U/**< 设定5秒内喂狗 */
/** Exported typedefines -----------------------------------------------------*/
/** Exported constants -------------------------------------------------------*/
/** Exported macros-----------------------------------------------------------*/
/** Exported variables -------------------------------------------------------*/
/** Exported functions prototypes --------------------------------------------*/
void Fwdg_Init(void);
void Feed_Fwdg(void);
#ifdef __cplusplus ///<end extern c
}
#endif
#endif
/******************************** End of file *********************************/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

定时器部分接口

/**
 *  @file: tim.c
 *
 *  @date: 2020/5/5
 *
 *  @author: aron566
 *
 *  @brief:timer init
 */
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "tim.h"
#include "systick.h"
#include "gd32f4xx.h"
#include "main.h"
/** Private typedef ----------------------------------------------------------*/
/** Private macros -----------------------------------------------------------*/
/** Private constants --------------------------------------------------------*/
/** Public variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;
/** Public variables ---------------------------------------------------------*/
__IO uint32_t CurrentTime = 0;
/** Private function prototypes ----------------------------------------------*/
static void timer4_gpio_config(void);
static void timer1_gpio_config(void);
static void timer1_config(void);
static void timer2_config(void);
static void timer3_config(void);
static void timer4_config(void);
/** Private user code --------------------------------------------------------*/

/** Private application code -------------------------------------------------/
/
******************************************************************************
*

  •   Static code
    

/
/
*
\brief configure the TIMER4 peripheral gpio
\param[in] none
\param[out] none
\retval none
*/
static void timer4_gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOA);

<span class="token comment">/*Configure PA1/2(TIMER1_CH1/CH2) as alternate function*/</span>
<span class="token function">gpio_mode_set</span><span class="token punctuation">(</span>GPIOA<span class="token punctuation">,</span> GPIO_MODE_AF<span class="token punctuation">,</span> GPIO_PUPD_NONE<span class="token punctuation">,</span> GPIO_PIN_1<span class="token operator">|</span>GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">gpio_output_options_set</span><span class="token punctuation">(</span>GPIOA<span class="token punctuation">,</span> GPIO_OTYPE_PP<span class="token punctuation">,</span>             
GPIO_OSPEED_50MHZ<span class="token punctuation">,</span>GPIO_PIN_1<span class="token operator">|</span>GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">gpio_af_set</span><span class="token punctuation">(</span>GPIOA<span class="token punctuation">,</span> GPIO_AF_2<span class="token punctuation">,</span> GPIO_PIN_1<span class="token operator">|</span>GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>

}

/**
\brief configure the TIMER1 peripheral gpio
\param[in] none
\param[out] none
\retval none
*/

static void timer1_gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOB);

<span class="token comment">/*Configure PB2(TIMER1_CH3) as alternate function*/</span>
<span class="token function">gpio_mode_set</span><span class="token punctuation">(</span>GPIOB<span class="token punctuation">,</span> GPIO_MODE_AF<span class="token punctuation">,</span> GPIO_PUPD_NONE <span class="token punctuation">,</span>GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">gpio_output_options_set</span><span class="token punctuation">(</span>GPIOB<span class="token punctuation">,</span> GPIO_OTYPE_PP<span class="token punctuation">,</span> GPIO_OSPEED_50MHZ <span class="token punctuation">,</span>GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">gpio_af_set</span><span class="token punctuation">(</span>GPIOB<span class="token punctuation">,</span> GPIO_AF_1<span class="token punctuation">,</span> GPIO_PIN_2<span class="token punctuation">)</span><span class="token punctuation">;</span>

}

/**
\brief configure the TIMER peripheral
\param[in] none
\param[out] none
\retval none
*/

static void timer2_config(void)
{
timer_parameter_struct timer_initpara;

<span class="token function">rcu_periph_clock_enable</span><span class="token punctuation">(</span>RCU_TIMER2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">rcu_timer_clock_prescaler_config</span><span class="token punctuation">(</span>RCU_TIMER_PSC_MUL4<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/**&lt; Timer_Clk = APB1_50Mhz * 4 for timer1.2.3.4.5.6.11.12.13*/</span>

<span class="token function">timer_deinit</span><span class="token punctuation">(</span>TIMER2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* TIMER2 configuration */</span>
timer_initpara<span class="token punctuation">.</span>prescaler         <span class="token operator">=</span> <span class="token number">200</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>alignedmode       <span class="token operator">=</span> TIMER_COUNTER_EDGE<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>counterdirection  <span class="token operator">=</span> TIMER_COUNTER_UP<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>period            <span class="token operator">=</span> <span class="token number">1000</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">/** CYCLE 1ms*/</span>
timer_initpara<span class="token punctuation">.</span>clockdivision     <span class="token operator">=</span> TIMER_CKDIV_DIV1<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>repetitioncounter <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token function">timer_init</span><span class="token punctuation">(</span>TIMER2<span class="token punctuation">,</span><span class="token operator">&amp;</span>timer_initpara<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* auto-reload preload enable */</span>
<span class="token function">timer_auto_reload_shadow_enable</span><span class="token punctuation">(</span>TIMER2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*初始化计数值*/</span>
<span class="token function">timer_counter_value_config</span><span class="token punctuation">(</span>TIMER2<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*清除中断标志*/</span>
<span class="token function">timer_interrupt_flag_clear</span><span class="token punctuation">(</span>TIMER2<span class="token punctuation">,</span>TIMER_FLAG_UP<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*使能更新中断*/</span>
<span class="token function">timer_interrupt_enable</span><span class="token punctuation">(</span>TIMER2 <span class="token punctuation">,</span>TIMER_INT_UP<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token function">HAL_NVIC_SetPriority</span><span class="token punctuation">(</span>TIMER2_IRQn<span class="token punctuation">,</span> <span class="token number">5U</span><span class="token punctuation">,</span> <span class="token number">0U</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/*抢占优先级 5 子优先级 0*/</span>

<span class="token comment">/*使能外部中断*/</span>
<span class="token function">HAL_NVIC_EnableIRQ</span><span class="token punctuation">(</span>TIMER2_IRQn<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* new code */</span>
htim2<span class="token punctuation">.</span>Instance <span class="token operator">=</span> TIMER2<span class="token punctuation">;</span>

<span class="token comment">/* TIMER2 enable */</span>
<span class="token function">timer_enable</span><span class="token punctuation">(</span>TIMER2<span class="token punctuation">)</span><span class="token punctuation">;</span>

}

/**
\brief configure the TIMER peripheral CH3-->PB2
\param[in] none
\param[out] none
\retval none
/

static void timer1_config(void)
{
/
TIMER1 configuration: generate PWM signals with different duty cycles:
TIMER1CLK = SystemCoreClock / 120 = 1MHz */
timer_parameter_struct timer_initpara;
timer_oc_parameter_struct timer_ocintpara;

<span class="token function">rcu_periph_clock_enable</span><span class="token punctuation">(</span>RCU_TIMER1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">rcu_timer_clock_prescaler_config</span><span class="token punctuation">(</span>RCU_TIMER_PSC_MUL4<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/**&lt; Timer_Clk = APB1_50Mhz * 4 for timer1.2.3.4.5.6.11.12.13*/</span>

<span class="token function">timer_deinit</span><span class="token punctuation">(</span>TIMER1<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* TIMER1 configuration */</span>
timer_initpara<span class="token punctuation">.</span>prescaler         <span class="token operator">=</span> <span class="token number">200</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>alignedmode       <span class="token operator">=</span> TIMER_COUNTER_EDGE<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>counterdirection  <span class="token operator">=</span> TIMER_COUNTER_UP<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>period            <span class="token operator">=</span> <span class="token number">1000</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>clockdivision     <span class="token operator">=</span> TIMER_CKDIV_DIV1<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>repetitioncounter <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token function">timer_init</span><span class="token punctuation">(</span>TIMER1<span class="token punctuation">,</span><span class="token operator">&amp;</span>timer_initpara<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* CH3 configuration in PWM mode 0 */</span>
timer_ocintpara<span class="token punctuation">.</span>outputstate <span class="token operator">=</span> TIMER_CCX_ENABLE<span class="token punctuation">;</span><span class="token comment">/**&lt; 启用 */</span>
timer_ocintpara<span class="token punctuation">.</span>ocpolarity  <span class="token operator">=</span> TIMER_OC_POLARITY_HIGH<span class="token punctuation">;</span><span class="token comment">/*有效电平为高*/</span>
timer_ocintpara<span class="token punctuation">.</span>ocidlestate  <span class="token operator">=</span> TIMER_OC_IDLE_STATE_LOW<span class="token punctuation">;</span><span class="token comment">/*空闲电平为低*/</span>

timer_ocintpara<span class="token punctuation">.</span>outputnstate <span class="token operator">=</span> TIMER_CCXN_DISABLE<span class="token punctuation">;</span><span class="token comment">/*互补禁用*/</span>
timer_ocintpara<span class="token punctuation">.</span>ocnpolarity  <span class="token operator">=</span> TIMER_OCN_POLARITY_HIGH<span class="token punctuation">;</span><span class="token comment">/*互补电平高*/</span>
timer_ocintpara<span class="token punctuation">.</span>ocnidlestate <span class="token operator">=</span> TIMER_OCN_IDLE_STATE_LOW<span class="token punctuation">;</span><span class="token comment">/*互补空闲电平低*/</span>

<span class="token function">timer_channel_output_config</span><span class="token punctuation">(</span>TIMER1<span class="token punctuation">,</span>TIMER_CH_3 <span class="token punctuation">,</span><span class="token operator">&amp;</span>timer_ocintpara<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* CH3 configuration in PWM mode 0,duty cycle 100% */</span>
<span class="token function">timer_channel_output_pulse_value_config</span><span class="token punctuation">(</span>TIMER1 <span class="token punctuation">,</span>TIMER_CH_3 <span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">timer_channel_output_mode_config</span><span class="token punctuation">(</span>TIMER1 <span class="token punctuation">,</span>TIMER_CH_3 <span class="token punctuation">,</span>TIMER_OC_MODE_PWM0<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/** &lt;PWM模式0 小于比较值则输出有效电平 */</span>
<span class="token function">timer_channel_output_shadow_config</span><span class="token punctuation">(</span>TIMER1 <span class="token punctuation">,</span>TIMER_CH_3 <span class="token punctuation">,</span>TIMER_OC_SHADOW_DISABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* auto-reload preload enable */</span>
<span class="token function">timer_auto_reload_shadow_enable</span><span class="token punctuation">(</span>TIMER1<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* Preset Disable channel_3*/</span>
<span class="token function">timer_channel_output_state_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_3 <span class="token punctuation">,</span>TIMER_CCX_DISABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>
 
<span class="token comment">/* new code */</span>
htim1<span class="token punctuation">.</span>Instance <span class="token operator">=</span> TIMER1<span class="token punctuation">;</span>
htim1<span class="token punctuation">.</span>Period <span class="token operator">=</span> timer_initpara<span class="token punctuation">.</span>period<span class="token punctuation">;</span>
	
<span class="token comment">/* TIMER1 enable */</span>
<span class="token function">timer_enable</span><span class="token punctuation">(</span>TIMER1<span class="token punctuation">)</span><span class="token punctuation">;</span>		

}

/**


  • @brief 初始化定时器3 分频为16K
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/7

*/
static void timer3_config(void)
{
timer_parameter_struct timer_initpara;

<span class="token function">rcu_periph_clock_enable</span><span class="token punctuation">(</span>RCU_TIMER3<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">rcu_timer_clock_prescaler_config</span><span class="token punctuation">(</span>RCU_TIMER_PSC_MUL4<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/**&lt; Timer_Clk = APB1_50Mhz * 4 for timer1.2.3.4.5.6.11.12.13*/</span>

<span class="token function">timer_deinit</span><span class="token punctuation">(</span>TIMER3<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* TIMER3 configuration */</span>
timer_initpara<span class="token punctuation">.</span>prescaler         <span class="token operator">=</span> <span class="token number">12500</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>alignedmode       <span class="token operator">=</span> TIMER_COUNTER_EDGE<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>counterdirection  <span class="token operator">=</span> TIMER_COUNTER_UP<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>period            <span class="token operator">=</span> <span class="token number">0xFFFF</span><span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>clockdivision     <span class="token operator">=</span> TIMER_CKDIV_DIV1<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>repetitioncounter <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token function">timer_init</span><span class="token punctuation">(</span>TIMER3<span class="token punctuation">,</span><span class="token operator">&amp;</span>timer_initpara<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* auto-reload preload enable */</span>
<span class="token function">timer_auto_reload_shadow_enable</span><span class="token punctuation">(</span>TIMER3<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*初始化计数值*/</span>
<span class="token function">timer_counter_value_config</span><span class="token punctuation">(</span>TIMER3<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*清除中断标志*/</span>
<span class="token function">timer_interrupt_flag_clear</span><span class="token punctuation">(</span>TIMER3<span class="token punctuation">,</span>TIMER_FLAG_UP<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*使能更新中断*/</span>
<span class="token function">timer_interrupt_enable</span><span class="token punctuation">(</span>TIMER3 <span class="token punctuation">,</span>TIMER_INT_UP<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*设置中断优先级5*/</span>
<span class="token function">HAL_NVIC_SetPriority</span><span class="token punctuation">(</span>TIMER3_IRQn<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/*抢占优先级 5 子优先级 0*/</span>
	
<span class="token comment">/*使能外部中断*/</span>
<span class="token function">HAL_NVIC_EnableIRQ</span><span class="token punctuation">(</span>TIMER3_IRQn<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* new code */</span>
htim3<span class="token punctuation">.</span>Instance <span class="token operator">=</span> TIMER3<span class="token punctuation">;</span>

<span class="token comment">/* TIMER3 enable */</span>
<span class="token function">timer_enable</span><span class="token punctuation">(</span>TIMER3<span class="token punctuation">)</span><span class="token punctuation">;</span>

}

/**


  • @brief 初始化定时器4 PWM USE CHANNEL_1(PA1) CHANNEL_2(PA2)
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/7

*/
static void timer4_config(void)
{
timer_parameter_struct timer_initpara;
timer_oc_parameter_struct timer_ocintpara;

<span class="token function">rcu_periph_clock_enable</span><span class="token punctuation">(</span>RCU_TIMER4<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">rcu_timer_clock_prescaler_config</span><span class="token punctuation">(</span>RCU_TIMER_PSC_MUL4<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/**&lt; Timer_Clk = APB1_50Mhz * 4 for timer1.2.3.4.5.6.11.12.13*/</span>

<span class="token function">timer_deinit</span><span class="token punctuation">(</span>TIMER4<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* TIMER4 configuration */</span>
timer_initpara<span class="token punctuation">.</span>prescaler         <span class="token operator">=</span> <span class="token number">200</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>alignedmode       <span class="token operator">=</span> TIMER_COUNTER_EDGE<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>counterdirection  <span class="token operator">=</span> TIMER_COUNTER_UP<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>period            <span class="token operator">=</span> <span class="token number">250</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>clockdivision     <span class="token operator">=</span> TIMER_CKDIV_DIV1<span class="token punctuation">;</span>
timer_initpara<span class="token punctuation">.</span>repetitioncounter <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token function">timer_init</span><span class="token punctuation">(</span>TIMER4<span class="token punctuation">,</span><span class="token operator">&amp;</span>timer_initpara<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* CH1 configuration in PWM mode 0 */</span>
timer_ocintpara<span class="token punctuation">.</span>outputstate <span class="token operator">=</span> TIMER_CCX_ENABLE<span class="token punctuation">;</span><span class="token comment">/**&lt; 启用 */</span>
timer_ocintpara<span class="token punctuation">.</span>ocpolarity  <span class="token operator">=</span> TIMER_OC_POLARITY_HIGH<span class="token punctuation">;</span><span class="token comment">/*有效电平为高*/</span>
timer_ocintpara<span class="token punctuation">.</span>ocidlestate  <span class="token operator">=</span> TIMER_OC_IDLE_STATE_LOW<span class="token punctuation">;</span><span class="token comment">/*空闲电平为低*/</span>

timer_ocintpara<span class="token punctuation">.</span>outputnstate <span class="token operator">=</span> TIMER_CCXN_DISABLE<span class="token punctuation">;</span><span class="token comment">/*互补禁用*/</span>
timer_ocintpara<span class="token punctuation">.</span>ocnpolarity  <span class="token operator">=</span> TIMER_OCN_POLARITY_HIGH<span class="token punctuation">;</span><span class="token comment">/*互补电平高*/</span>
timer_ocintpara<span class="token punctuation">.</span>ocnidlestate <span class="token operator">=</span> TIMER_OCN_IDLE_STATE_LOW<span class="token punctuation">;</span><span class="token comment">/*互补空闲电平低*/</span>

<span class="token function">timer_channel_output_config</span><span class="token punctuation">(</span>TIMER4<span class="token punctuation">,</span>TIMER_CH_1 <span class="token punctuation">,</span><span class="token operator">&amp;</span>timer_ocintpara<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* CH1 configuration in PWM mode 0,duty cycle 100% */</span>
<span class="token function">timer_channel_output_pulse_value_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_1 <span class="token punctuation">,</span><span class="token number">250</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">timer_channel_output_mode_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_1 <span class="token punctuation">,</span>TIMER_OC_MODE_PWM0<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/** &lt;PWM模式0 小于比较值则输出有效电平 */</span>
<span class="token function">timer_channel_output_shadow_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_1 <span class="token punctuation">,</span>TIMER_OC_SHADOW_DISABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">timer_channel_output_config</span><span class="token punctuation">(</span>TIMER1<span class="token punctuation">,</span>TIMER_CH_2 <span class="token punctuation">,</span><span class="token operator">&amp;</span>timer_ocintpara<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">/* CH2 configuration in PWM mode 0,duty cycle 100% */</span>
<span class="token function">timer_channel_output_pulse_value_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_2 <span class="token punctuation">,</span><span class="token number">250</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">timer_channel_output_mode_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_2 <span class="token punctuation">,</span>TIMER_OC_MODE_PWM0<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">/** &lt;PWM模式0 小于比较值则输出有效电平 */</span>
<span class="token function">timer_channel_output_shadow_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_2 <span class="token punctuation">,</span>TIMER_OC_SHADOW_DISABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* auto-reload preload enable */</span>
<span class="token function">timer_auto_reload_shadow_enable</span><span class="token punctuation">(</span>TIMER4<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* Preset Disable channel_1 channel_2 */</span>
<span class="token function">timer_channel_output_state_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_1 <span class="token punctuation">,</span>TIMER_CCX_DISABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">timer_channel_output_state_config</span><span class="token punctuation">(</span>TIMER4 <span class="token punctuation">,</span>TIMER_CH_2 <span class="token punctuation">,</span>TIMER_CCX_DISABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>  
 
<span class="token comment">/* new code */</span>
htim4<span class="token punctuation">.</span>Instance <span class="token operator">=</span> TIMER4<span class="token punctuation">;</span>
htim1<span class="token punctuation">.</span>Period <span class="token operator">=</span> timer_initpara<span class="token punctuation">.</span>period<span class="token punctuation">;</span>
	
<span class="token comment">/* TIMER4 enable */</span>
<span class="token function">timer_enable</span><span class="token punctuation">(</span>TIMER4<span class="token punctuation">)</span><span class="token punctuation">;</span>

}
/** Public application code --------------------------------------------------/
/
******************************************************************************
*

  •   Public code
    

*/

/**


  • @brief 定时器中断回调
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/5

*/
void USER_Timer_IR_Callback(uint32_t timer_periph)
{
switch(timer_periph)
{
case TIMER1:
if(RESET != timer_interrupt_flag_get(TIMER1 ,TIMER_FLAG_UP))
{
timer_interrupt_flag_clear(TIMER1 ,TIMER_FLAG_UP);
}
break;
case TIMER2:
if(RESET != timer_interrupt_flag_get(TIMER2 ,TIMER_FLAG_UP))
{
timer_interrupt_flag_clear(TIMER2 ,TIMER_FLAG_UP);
HAL_IncTick();
CurrentTime++;
delay_decrement();
}
break;
case TIMER3:
if(RESET != timer_interrupt_flag_get(TIMER3 ,TIMER_FLAG_UP))
{
timer_interrupt_flag_clear(TIMER3 ,TIMER_FLAG_UP);
}
break;
default:
break;
}
}

/**


  • @brief 停止定时器
  • @param[in] 外设句柄
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
void HAL_TIM_Base_Stop(TIM_HandleTypeDef
htim)
{
// timer_interrupt_disable(htim->Instance ,TIMER_INT_UP);
timer_disable(htim->Instance);
}

/**


  • @brief 启动定时器
  • @param[in] 外设句柄
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
void HAL_TIM_Base_Start(TIM_HandleTypeDef
htim)
{
// timer_interrupt_enable(htim->Instance ,TIMER_INT_UP);
timer_enable(htim->Instance);
}

/**


  • @brief 获取定时器计数
  • @param[in] 外设句柄
  • @retval 计数
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
uint32_t __HAL_TIM_GET_COUNTER(TIM_HandleTypeDef
htim)
{
return timer_counter_read(htim->Instance);
}

/**


  • @brief 设置定时器计数
  • @param[in] 外设句柄
  • @param[in] 计数值
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/9

/
void __HAL_TIM_SET_COUNTER(TIM_HandleTypeDef
htim ,uint32_t timer_counter)
{
timer_counter_value_config(htim->Instance ,timer_counter);
}

/**


  • @brief PWM启动
  • @param 外设句柄 通道号
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/6/20

/
void HAL_TIM_PWM_Stop(TIM_HandleTypeDef
htim ,uint16_t channel)
{
timer_channel_output_state_config(htim->Instance ,channel ,TIMER_CCX_DISABLE);
}

/**


  • @brief PWM停止
  • @param 外设句柄 通道号
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/6/20

/
void HAL_TIM_PWM_Start(TIM_HandleTypeDef
htim ,uint16_t channel)
{
timer_channel_output_state_config(htim->Instance ,channel ,TIMER_CCX_ENABLE);
}

/**


  • @brief PWM脉冲数设置
  • @param 外设句柄 通道号 脉冲数
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/6/20

/
void __HAL_TIM_SetCompare(TIM_HandleTypeDef
htim ,uint16_t channel ,uint32_t timer_pluse)
{
timer_channel_output_pulse_value_config(htim->Instance ,channel ,timer_pluse);
}

/**


  • @brief 定时器初始化
  • @param None
  • @retval None
  • @author aron566
  • @version v1.0
  • @date 2020/5/5

/
void Timer_Init(void)
{
/
pwm输出脚初始化/
/
backlight pwm/
timer1_gpio_config();
/
beep1 beep2*/
timer4_gpio_config();

<span class="token comment">/*back light pwm out CH3--&gt;PB2*/</span>
<span class="token function">timer1_config</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*GUI TIMER*/</span>
<span class="token function">timer2_config</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*IR TIMER*/</span>
<span class="token function">timer3_config</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*beep pwm*/</span>
<span class="token function">timer4_config</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}

#ifdef __cplusplus ///<end extern c
}
#endif
/******************************** End of file *********************************/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
/**
 *  @file: tim.h
 *
 *  @date: 2020/5/5
 *
 *  @author: aron566
 *
 *  @brief:timer init
 *  
 *  @version:
 */
#ifndef __TIM_H
#define __TIM_H
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
#include <stdint.h> /**< nedd definition of uint8_t */
#include <stddef.h> /**< need definition of NULL    */
//#include <stdbool.h>/**< need definition of BOOL    */
#include <stdio.h>  /**< if need printf             */
#include <stdlib.h>
#include <string.h>
/** Private includes ---------------------------------------------------------*/
/** Private defines ----------------------------------------------------------*/
/** Exported typedefines -----------------------------------------------------*/
typedef struct
{
  uint32_t                    Instance;     /*!< Register base address             */
  uint32_t 					  Period;
//  TIM_Base_InitTypeDef        Init;          /*!< TIM Time Base required parameters */
//  HAL_TIM_ActiveChannel       Channel;       /*!< Active channel                    */
//  DMA_HandleTypeDef           *hdma[7];      /*!< DMA Handlers array
//                                                  This array is accessed by a @ref DMA_Handle_index */
//  HAL_LockTypeDef             Lock;          /*!< Locking object                    */
//  __IO HAL_TIM_StateTypeDef   State;         /*!< TIM operation state               */    
} TIM_HandleTypeDef;
/** Exported constants -------------------------------------------------------*/
/** Exported macros-----------------------------------------------------------*/
/** Exported variables -------------------------------------------------------*/
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
/** Exported functions prototypes --------------------------------------------*/
void Timer_Init(void);
void HAL_TIM_Base_Start(TIM_HandleTypeDef *htim);
void HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim);
uint32_t __HAL_TIM_GET_COUNTER(TIM_HandleTypeDef *htim);
void __HAL_TIM_SET_COUNTER(TIM_HandleTypeDef *htim ,uint32_t timer_counter);
void __HAL_TIM_SetCompare(TIM_HandleTypeDef *htim ,uint16_t channel ,uint32_t timer_pluse);
void HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim ,uint16_t channel);
void HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim ,uint16_t channel);
/**< 定时器中断回调处理 */
void USER_Timer_IR_Callback(uint32_t timer_periph);#ifdef __cplusplus ///<end extern c
}
#endif
#endif
/******************************** End of file *********************************/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
posted @ 2023-07-21 08:56  SymPny  阅读(160)  评论(0编辑  收藏  举报