完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
使用LL库配置,可与HAL库一起使用,0级优化编译文件大小如下:
1.工程目录 2.代码配置 usart1.h 主要定义接收缓存,发送缓存,处理缓存大小 #ifndef _USART1_H #define _USART1_H #include "stm32h7xx.h" #define USART1_TX_BUFFER_SIZE (1 * 1024) #define USART1_RX_BUFFER_SIZE (1 * 1024) #define USART1_RX_HANDLE_SIZE (1 * 1024) extern uint8_t USART1_TX_BUFFER[USART1_TX_BUFFER_SIZE]; extern uint8_t USART1_RX_BUFFER[USART1_RX_BUFFER_SIZE]; extern uint8_t USART1_RX_HANDLE[USART1_RX_HANDLE_SIZE]; extern uint32_t USART_RX_CNT; //接收的字节数 void IAP_USART1_Init(void); void DMA_START(DMA_TypeDef *DMAx, uint32_t Stream, uint32_t *SRC_ADDR, uint32_t *DES_ADDR, uint32_t Data_Len, uint32_t Direction); #endif usart1.c DMA1的USART1 TX RX都配置好了,但是一般不需要,所以虽然配置了但未使用 #include "usart1.h" #include "string.h" #include "stm32h7xx_ll_utils.h" #include "stm32h7xx_ll_dma.h" #include "stm32h7xx_ll_usart.h" #include "stm32h7xx_ll_gpio.h" uint8_t USART1_TX_BUFFER[USART1_TX_BUFFER_SIZE] = {0}; uint8_t USART1_RX_BUFFER[USART1_RX_BUFFER_SIZE] = {0}; uint8_t USART1_RX_HANDLE[USART1_RX_HANDLE_SIZE] = {0}; uint32_t USART_RX_CNT = 0; //接收的字节数 /** * @brief 配置USART1串口. */ void USART1_UART_Init(void) { LL_USART_InitTypeDef USART_InitStruct = {0}; LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; //开启外设 USART1 时钟 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1); //开启 GPIOA 时钟 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOA); //USART1 GPIO Configuration //PA9 ------> USART1_TX //PA10 ------> USART1_RX GPIO_InitStruct.Pin = LL_GPIO_PIN_9|LL_GPIO_PIN_10; GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; GPIO_InitStruct.Alternate = LL_GPIO_AF_7; //初始化 PA9 PA10 LL_GPIO_Init(GPIOA, &GPIO_InitStruct); USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1; //时钟不分频 USART_InitStruct.BaudRate = 512000; //波特率512000 USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B; //数据宽度8位 USART_InitStruct.StopBits = LL_USART_STOPBITS_1; //一个停止位 USART_InitStruct.Parity = LL_USART_PARITY_NONE; //无奇偶校验 USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX; //开启TX RX USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE; //无硬件流控制 USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16; //16倍过采样 //初始化 USART1 LL_USART_Init(USART1, &USART_InitStruct); LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);//1/8 FIFO LL_USART_SetRXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8); //LL_USART_EnableFIFO(USART1); LL_USART_DisableFIFO(USART1); //不使用 FIFO LL_USART_ConfigAsyncMode(USART1); //异步模式 //使能 USART1 LL_USART_Enable(USART1); LL_USART_ClearFlag_TC(USART1); /* USART1 interrupt Init */ NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));//优先级分组4,抢占优先级0,响应优先级0 //LL_USART_EnableIT_TC(USART1); //开启发送完成中断 //LL_USART_EnableIT_RXNE_RXFNE(USART1); //开启接收中断 LL_USART_EnableIT_IDLE(USART1); //开启空闲中断 NVIC_EnableIRQ(USART1_IRQn); /* Polling USART1 initialisation */ while((!(LL_USART_IsActiveFlag_TEACK(USART1))) || (!(LL_USART_IsActiveFlag_REACK(USART1)))) { } } /** * @brief 配置USART1 DMA. */ void USART1_DMA_Init(void) { LL_DMA_InitTypeDef DMA_Init_Struct; //开启DMA1时钟 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1); //DMA1 STREAM_0 ------- USART1_TX //DMA1 STREAM_1 ------- USART1_RX /* //USART1 TX DMA配置 DMA_Init_Struct.PeriphRequest = LL_DMAMUX1_REQ_USART1_TX; //USART1 TX请求 DMA_Init_Struct.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;//传输方向:内存到外设 DMA_Init_Struct.PeriphOrM2MSrcAddress = (uint32_t)&USART1->TDR; //外设地址 DMA_Init_Struct.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE; //外设数据对齐宽度:字节 DMA_Init_Struct.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; //外设地址不自增 DMA_Init_Struct.MemoryOrM2MDstAddress = (uint32_t) USART1_TX_BUFFER; //内存地址 DMA_Init_Struct.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE; //内存数据对齐宽度:字节 DMA_Init_Struct.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT; //内存地址自增 DMA_Init_Struct.NbData = USART1_TX_BUFFER_SIZE; //传输数据数量 DMA_Init_Struct.Mode = LL_DMA_MODE_NORMAL; //普通模式 DMA_Init_Struct.Priority = LL_DMA_PRIORITY_HIGH; //优先级高 DMA_Init_Struct.FIFOMode = LL_DMA_FIFOMODE_DISABLE; //关闭FIFO DMA_Init_Struct.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_FULL; DMA_Init_Struct.MemBurst = LL_DMA_MBURST_SINGLE; //内存单次突发传输 DMA_Init_Struct.PeriphBurst = LL_DMA_PBURST_SINGLE; //外设单次突发传输 LL_DMA_Init(DMA1, LL_DMA_STREAM_0, &DMA_Init_Struct); LL_DMA_ClearFlag_TC0(DMA1); //LL_DMA_EnableIT_TC(DMA1, LL_DMA_STREAM_0); LL_DMA_DisableStream(DMA1, LL_DMA_STREAM_0); */ //USART1 RX DMA配置 DMA_Init_Struct.PeriphRequest = LL_DMAMUX1_REQ_USART1_RX; //USART1 RX请求 DMA_Init_Struct.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;//传输方向:外设到内存 DMA_Init_Struct.PeriphOrM2MSrcAddress = (uint32_t)&USART1->RDR; //外设地址 DMA_Init_Struct.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE; //外设数据对齐宽度:字节 DMA_Init_Struct.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; //外设地址不自增 DMA_Init_Struct.MemoryOrM2MDstAddress = (uint32_t) USART1_RX_BUFFER; //内存地址 DMA_Init_Struct.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE; //内存数据对齐宽度:字节 DMA_Init_Struct.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT; //内存地址自增 DMA_Init_Struct.NbData = USART1_RX_BUFFER_SIZE; //传输数据数量 DMA_Init_Struct.Mode = LL_DMA_MODE_NORMAL; //普通模式 DMA_Init_Struct.Priority = LL_DMA_PRIORITY_HIGH; //优先级高 DMA_Init_Struct.FIFOMode = LL_DMA_FIFOMODE_DISABLE; //关闭FIFO DMA_Init_Struct.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_FULL; DMA_Init_Struct.MemBurst = LL_DMA_MBURST_SINGLE; //内存单次突发传输 DMA_Init_Struct.PeriphBurst = LL_DMA_PBURST_SINGLE; //外设单次突发传输 LL_DMA_Init(DMA1, LL_DMA_STREAM_1, &DMA_Init_Struct); LL_DMA_ClearFlag_TC1(DMA1); LL_DMA_EnableIT_TC(DMA1, LL_DMA_STREAM_1); LL_DMA_DisableStream(DMA1, LL_DMA_STREAM_1); /* DMA interrupt init */ /* DMA1_Stream0_IRQn interrupt configuration */ // NVIC_SetPriority(DMA1_Stream0_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); // NVIC_EnableIRQ(DMA1_Stream0_IRQn); /* DMA1_Stream1_IRQn interrupt configuration */ NVIC_SetPriority(DMA1_Stream1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); NVIC_EnableIRQ(DMA1_Stream1_IRQn); } /** * @brief 配置 USART1 功能. */ void IAP_USART1_Init(void) { USART1_UART_Init(); USART1_DMA_Init(); //使能DMA请求 //LL_USART_EnableDMAReq_TX(USART1); LL_USART_EnableDMAReq_RX(USART1); } /** * @brief This function handles USART1 global interrupt. */ void USART1_IRQHandler(void) { uint32_t RX_DATA_LEN = 0; // if(LL_USART_IsActiveFlag_TC(USART1) != RESET)//发送完成中断 // { // LL_USART_ClearFlag_TC(USART1); // } // if(LL_USART_IsActiveFlag_RXNE_RXFNE(USART1) != RESET)//接收中断 // { // USART1_RX_BUFFER[0] = (uint8_t)USART1->RDR; // // } if(LL_USART_IsActiveFlag_IDLE(USART1) != RESET)//空闲中断 { LL_DMA_DisableStream(DMA1, LL_DMA_STREAM_1);//关闭DMA LL_USART_ClearFlag_IDLE(USART1); LL_DMA_ClearFlag_TC1(DMA1); //计算DMA传输字节数, 最大字节数 USART1_RX_BUFFER_SIZE RX_DATA_LEN = USART1_RX_BUFFER_SIZE - LL_DMA_GetDataLength(DMA1, LL_DMA_STREAM_1); //如果缓存未满 if((USART_RX_CNT + RX_DATA_LEN) < USART1_RX_HANDLE_SIZE) { //复制数据到处理缓存 memcpy(USART1_RX_HANDLE, USART1_RX_BUFFER, RX_DATA_LEN); USART_RX_CNT = RX_DATA_LEN; } //再次开启DMA接收 DMA_START(DMA1, LL_DMA_STREAM_1, (uint32_t *)&USART1->RDR , (uint32_t *)USART1_RX_BUFFER, USART1_RX_BUFFER_SIZE, LL_DMA_DIRECTION_PERIPH_TO_MEMORY); } } void DMA_START(DMA_TypeDef *DMAx, uint32_t Stream, uint32_t *SRC_ADDR, uint32_t *DES_ADDR, uint32_t Data_Len, uint32_t Direction) { //LL_DMA_DisableStream(DMAx, Stream); //DMAx->LIFCR = 0x00000F40U; LL_DMA_ConfigAddresses(DMAx, Stream, (uint32_t)SRC_ADDR, (uint32_t)DES_ADDR, Direction); LL_DMA_SetDataLength(DMAx, Stream, Data_Len); LL_DMA_EnableStream(DMAx, Stream); //LL_DMA_EnableBufferableTransfer(DMAx, Stream); } /** * @brief This function handles DMA1 stream0 global interrupt. */ void DMA1_Stream0_IRQHandler(void) { if(LL_DMA_IsActiveFlag_TC0(DMA1) != RESET)//DMA1_Stream0 传输完成中断,USART1_TX { LL_DMA_DisableStream(DMA1, LL_DMA_STREAM_0);//关闭DMA LL_DMA_ClearFlag_TC0(DMA1); } } /** * @brief This function handles DMA1 stream1 global interrupt. */ void DMA1_Stream1_IRQHandler(void) { if(LL_DMA_IsActiveFlag_TC1(DMA1) != RESET)//DMA1_Stream1 传输完成中断, USART1_RX { LL_DMA_DisableStream(DMA1, LL_DMA_STREAM_1);//关闭DMA LL_DMA_ClearFlag_TC1(DMA1); } } main.h /* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.h * @brief : Header for main.c file. * This file contains the common defines of the application. ****************************************************************************** * @attention * *
* All rights reserved. |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1792 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1626 浏览 1 评论
1094 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
732 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1682 浏览 2 评论
1943浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
742浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
578浏览 3评论
601浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
563浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-26 16:35 , Processed in 1.011357 second(s), Total 78, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号