gd32 串口DMA发送&双缓冲接收不定长数据例程
创始人
2024-11-13 16:07:28
0

main.c

/*!     \file    main.c     \brief   running LED      \version 2023-03-31, V1.0.0, firmware for GD32H7xx */  /*     Copyright (c) 2023, GigaDevice Semiconductor Inc.      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:      1. Redistributions of source code must retain the above copyright notice, this        list of conditions and the following disclaimer.     2. Redistributions in binary form must reproduce the above copyright notice,        this list of conditions and the following disclaimer in the documentation        and/or other materials provided with the distribution.     3. Neither the name of the copyright holder nor the names of its contributors        may be used to endorse or promote products derived from this software without        specific prior written permission.      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */  #include "gd32h7xx.h" #include "systick.h"  #include "string.h" #include "stdio.h" #include "circular_buffer.h"   #define USART_DMA_TRANSFER_SIZE 4096  __attribute__ ((aligned(32))) uint8_t usart_rx_buff0[USART_DMA_TRANSFER_SIZE] = {0}; __attribute__ ((aligned(32))) uint8_t usart_rx_buff1[USART_DMA_TRANSFER_SIZE] = {0}; __attribute__ ((aligned(32))) uint8_t circular_buffer_data[USART_DMA_TRANSFER_SIZE] = {0};   struct circular_buffer_t circular_buffer;   volatile static int rx_buff_select = 0;  uint8_t* get_rx_buff() {     if (rx_buff_select) return usart_rx_buff0;     else return usart_rx_buff1; }  uint8_t* get_next_rx_buff() {     rx_buff_select = !rx_buff_select;     return get_rx_buff(); }   /*!     \brief      enable the CPU Chache     \param[in]  none     \param[out] none     \retval     none */ static void cache_enable(void) {     /* Enable I-Cache */     SCB_EnableICache();      /* Enable D-Cache */ //    SCB_EnableDCache(); }  void led_config() {     rcu_periph_clock_enable(RCU_GPIOJ);      gpio_mode_set(GPIOJ, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8);     gpio_output_options_set(GPIOJ, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_8);      gpio_mode_set(GPIOJ, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);     gpio_output_options_set(GPIOJ, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_9);      gpio_bit_set(GPIOJ, GPIO_PIN_8);     gpio_bit_set(GPIOJ, GPIO_PIN_9);  }  void clock_config() {     rcu_system_clock_source_config(RCU_CKSYSSRC_IRC64MDIV);     rcu_deinit();      rcu_osci_on(RCU_HXTAL); //开启外部时钟     rcu_osci_stab_wait(RCU_HXTAL); //等待外部时钟稳定 25MHZ      rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV2); //AHB 300MHz     rcu_apb1_clock_config(RCU_APB1_CKAHB_DIV2); //APB1 150MHz     rcu_apb2_clock_config(RCU_APB2_CKAHB_DIV1); //APB2 300MHz     rcu_apb3_clock_config(RCU_APB3_CKAHB_DIV2); //APB3 150MHz     rcu_apb4_clock_config(RCU_APB4_CKAHB_DIV2); //APB4 150MHz      rcu_pll_source_config(RCU_PLLSRC_HXTAL); //选择外部高速时钟源 25MHz      rcu_pll0_config(1, 24, 1, 1, 1); //PLL0P 600MHz     rcu_pll_clock_output_enable(RCU_PLL0P | RCU_PLL0Q | RCU_PLL0R); //PLL0输出使能      RCU_CTL |= RCU_CTL_PLL0EN; //PLL0 使能      rcu_osci_stab_wait(RCU_PLL0_CK); //等待PLL0稳定      rcu_system_clock_source_config(RCU_CKSYSSRC_PLL0P); //CK_SYS 600MHz     SystemCoreClock = 600 * 1000 * 1000; //600MHz }  void usart_config() {     rcu_periph_clock_enable(RCU_GPIOB);     rcu_periph_clock_enable(RCU_USART0);      nvic_irq_enable(USART0_IRQn, 2, 2);      gpio_af_set(GPIOB, GPIO_AF_7, GPIO_PIN_6);     gpio_af_set(GPIOB, GPIO_AF_7, GPIO_PIN_7);      gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_6);      gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_7);      usart_deinit(USART0);     usart_word_length_set(USART0, USART_WL_8BIT);     usart_stop_bit_set(USART0, USART_STB_1BIT);     usart_parity_config(USART0, USART_PM_NONE);     usart_baudrate_set(USART0, 921600U);     usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);     usart_receive_config(USART0, USART_RECEIVE_ENABLE);     usart_interrupt_enable(USART0, USART_INT_IDLE);     usart_enable(USART0); }  void usart_transmit(char* buff, int size) {     for (int i = 0; i < size; ++i) {         usart_data_transmit(USART0, buff[i]);         while (RESET == usart_flag_get(USART0, USART_FLAG_TBE)) {}     } }   void usart_transmit_dma(char* buff, int size) {     dma_memory_address_config(DMA0, DMA_CH0, DMA_MEMORY_0, buff);     dma_transfer_number_config(DMA0, DMA_CH0, size);     dma_channel_enable(DMA0, DMA_CH0); }   void dma_config() {     rcu_periph_clock_enable(RCU_DMA0);     rcu_periph_clock_enable(RCU_DMAMUX);     nvic_irq_enable(DMA0_Channel0_IRQn, 2, 1);     nvic_irq_enable(DMA0_Channel1_IRQn, 2, 0);      dma_single_data_parameter_struct dma_init_struct;      //TX     dma_deinit(DMA0, DMA_CH0);     dma_single_data_para_struct_init(&dma_init_struct);     dma_init_struct.request      = DMA_REQUEST_USART0_TX;     dma_init_struct.direction    = DMA_MEMORY_TO_PERIPH;     dma_init_struct.memory0_addr  = (uint32_t)0;     dma_init_struct.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;     dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;     dma_init_struct.number       = 0;     dma_init_struct.periph_addr  = (uint32_t)(&USART_TDATA(USART0));     dma_init_struct.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;     dma_init_struct.priority     = DMA_PRIORITY_ULTRA_HIGH;     dma_single_data_mode_init(DMA0, DMA_CH0, &dma_init_struct);      dma_circulation_disable(DMA0, DMA_CH0);     usart_dma_transmit_config(USART0, USART_TRANSMIT_DMA_ENABLE);     dma_interrupt_enable(DMA0, DMA_CH0, DMA_INT_FTF); //    dma_channel_enable(DMA0, DMA_CH0);      //RX     dma_deinit(DMA0, DMA_CH1);     dma_single_data_para_struct_init(&dma_init_struct);     dma_init_struct.request      = DMA_REQUEST_USART0_RX;     dma_init_struct.direction    = DMA_PERIPH_TO_MEMORY;     dma_init_struct.memory0_addr  = (uint32_t)get_next_rx_buff();     dma_init_struct.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;     dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;     dma_init_struct.number       = USART_DMA_TRANSFER_SIZE;     dma_init_struct.periph_addr  = (uint32_t)(&USART_RDATA(USART0));     dma_init_struct.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;     dma_init_struct.priority     = DMA_PRIORITY_ULTRA_HIGH;     dma_single_data_mode_init(DMA0, DMA_CH1, &dma_init_struct);      dma_circulation_disable(DMA0, DMA_CH1);     usart_dma_receive_config(USART0, USART_RECEIVE_DMA_ENABLE);     dma_interrupt_enable(DMA0, DMA_CH1, DMA_INT_FTF);     dma_channel_enable(DMA0, DMA_CH1); }  void DMA0_Channel0_IRQHandler() {     if (RESET != dma_interrupt_flag_get(DMA0, DMA_CH0, DMA_INT_FLAG_FTF)) {         dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_FTF);           gpio_bit_toggle(GPIOJ, GPIO_PIN_8);     } }  void DMA0_Channel1_IRQHandler() {     if (RESET != dma_interrupt_flag_get(DMA0, DMA_CH1, DMA_INT_FLAG_FTF)) { //传输完成         dma_interrupt_flag_clear(DMA0, DMA_CH1, DMA_INT_FLAG_FTF);          uint8_t* rx_buff = get_rx_buff();          dma_single_data_parameter_struct dma_init_struct;         dma_deinit(DMA0, DMA_CH1);         dma_single_data_para_struct_init(&dma_init_struct);         dma_init_struct.request      = DMA_REQUEST_USART0_RX;         dma_init_struct.direction    = DMA_PERIPH_TO_MEMORY;         dma_init_struct.memory0_addr  = (uint32_t)get_next_rx_buff();         dma_init_struct.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;         dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;         dma_init_struct.number       = USART_DMA_TRANSFER_SIZE;         dma_init_struct.periph_addr  = (uint32_t)(&USART_RDATA(USART0));         dma_init_struct.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;         dma_init_struct.priority     = DMA_PRIORITY_ULTRA_HIGH;         dma_single_data_mode_init(DMA0, DMA_CH1, &dma_init_struct);          dma_circulation_disable(DMA0, DMA_CH1);         usart_dma_receive_config(USART0, USART_RECEIVE_DMA_ENABLE);         dma_interrupt_enable(DMA0, DMA_CH1, DMA_INT_FTF);         dma_channel_enable(DMA0, DMA_CH1);          for (int i = 0; i < USART_DMA_TRANSFER_SIZE; ++i) circular_buffer_push_back(&circular_buffer, rx_buff[i]);          gpio_bit_toggle(GPIOJ, GPIO_PIN_9);     } }  void USART0_IRQHandler() {     if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {         usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);           uint32_t size = USART_DMA_TRANSFER_SIZE - dma_transfer_number_get(DMA0, DMA_CH1);         uint8_t* rx_buff = get_rx_buff();          dma_single_data_parameter_struct dma_init_struct;         dma_deinit(DMA0, DMA_CH1);         dma_single_data_para_struct_init(&dma_init_struct);         dma_init_struct.request      = DMA_REQUEST_USART0_RX;         dma_init_struct.direction    = DMA_PERIPH_TO_MEMORY;         dma_init_struct.memory0_addr  = (uint32_t)get_next_rx_buff();         dma_init_struct.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;         dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;         dma_init_struct.number       = USART_DMA_TRANSFER_SIZE;         dma_init_struct.periph_addr  = (uint32_t)(&USART_RDATA(USART0));         dma_init_struct.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;         dma_init_struct.priority     = DMA_PRIORITY_ULTRA_HIGH;         dma_single_data_mode_init(DMA0, DMA_CH1, &dma_init_struct);          dma_circulation_disable(DMA0, DMA_CH1);         usart_dma_receive_config(USART0, USART_RECEIVE_DMA_ENABLE);         dma_interrupt_enable(DMA0, DMA_CH1, DMA_INT_FTF);         dma_channel_enable(DMA0, DMA_CH1);          for (int i = 0; i < size; ++i) circular_buffer_push_back(&circular_buffer, rx_buff[i]);          gpio_bit_toggle(GPIOJ, GPIO_PIN_9);     } }  /*!     \brief      main function     \param[in]  none     \param[out] none     \retval     none */ int main(void) {     /* enable the CPU Cache */     cache_enable();      clock_config(); //使用外部时钟      /* configure systick */     systick_config();      create_circular_buffer(&circular_buffer, circular_buffer_data, USART_DMA_TRANSFER_SIZE);      led_config();     usart_config();     dma_config();       char msg[] = "Hello World\r\n";     usart_transmit_dma(msg, strlen(msg));       while(1) {         while (circular_buffer_available(&circular_buffer)) {             char ch;             circular_buffer_pop_front(&circular_buffer, &ch);             usart_transmit(&ch, 1);         }         delay_1ms(1);     } }   

相关内容

热门资讯

绝活儿辅助!广西老友玩老是输怎... 绝活儿辅助!广西老友玩老是输怎么办(辅助挂)都是真的有辅助app(讲解有挂)在进入广西老友玩老是输怎...
法门辅助!福建13水插件(辅助... 法门辅助!福建13水插件(辅助挂)一贯是有辅助技巧(有挂技术)1、许多玩家不知道福建13水插件辅助怎...
办法辅助!潮友会app下载官方... 办法辅助!潮友会app下载官方辅助器(辅助挂)真是真的是有辅助app(有挂教程)该软件可以轻松地帮助...
妙招辅助!邯郸胡乐挂辅助(辅助... 妙招辅助!邯郸胡乐挂辅助(辅助挂)好像存在有辅助插件(有挂方略)1、上手简单,内置详细流程视频教学,...
教程书辅助!乐酷辅助(辅助挂)... 教程书辅助!乐酷辅助(辅助挂)其实存在有辅助脚本(有挂细节)乐酷辅助能透视中分为三种模型:乐酷辅助模...
学习辅助!决战卡五星辅助(辅助... 学习辅助!决战卡五星辅助(辅助挂)本来真的是有辅助软件(有人有挂)学习辅助!决战卡五星辅助(辅助挂)...
绝活辅助!边锋嘉兴麻将辅助器(... 绝活辅助!边锋嘉兴麻将辅助器(辅助挂)真是真的有辅助神器(新版有挂)1、边锋嘉兴麻将辅助器公共底牌简...
举措辅助!枫叶辅助器(辅助挂)... 举措辅助!枫叶辅助器(辅助挂)本来存在有辅助技巧(竟然有挂)1、下载好枫叶辅助器正确养号方法之后点击...
讲义辅助!点我达辅助(辅助挂)... 讲义辅助!点我达辅助(辅助挂)一直存在有辅助技巧(有人有挂)1、点我达辅助辅助器安装包、点我达辅助辅...
模块辅助!威信茶馆有挂的吗(辅... 模块辅助!威信茶馆有挂的吗(辅助挂)一直真的是有辅助脚本(揭秘有挂)1、玩家可以在威信茶馆有挂的吗线...