Skip to content

Commit

Permalink
fix: 修复串口DMA发送的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
PoisonNF committed Oct 15, 2023
1 parent 0e4a00c commit 9348888
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 236 deletions.
92 changes: 46 additions & 46 deletions Apply/Logic/Src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,67 +36,67 @@ tagGPIO_T demoGPIO[] =
tagUART_T demoUart =
{
//串口工作模式配置
.tUARTHandle.Instance = USART1, /* STM32 串口设备 */
.tUARTHandle.Init.BaudRate = 115200, /* 串口波特率 */
.tUARTHandle.Init.WordLength = UART_WORDLENGTH_8B,
.tUARTHandle.Init.StopBits = UART_STOPBITS_1,
.tUARTHandle.Init.Parity = UART_PARITY_NONE,
.tUARTHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE,
.tUARTHandle.Init.Mode = UART_MODE_TX_RX,
.tUARTHandle.Init.OverSampling = UART_OVERSAMPLING_16,
.tUARTHandle.Instance = USART1, /* STM32 串口设备 */
.tUARTHandle.Init.BaudRate = 115200, /* 串口波特率 */
.tUARTHandle.Init.WordLength = UART_WORDLENGTH_8B, /* 数据位长度 */
.tUARTHandle.Init.StopBits = UART_STOPBITS_1, /* 停止位长度 */
.tUARTHandle.Init.Parity = UART_PARITY_NONE, /* 校验位设置 */
.tUARTHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE, /* 硬件流控设置 */
.tUARTHandle.Init.Mode = UART_MODE_TX_RX, /* 串口模式 */
.tUARTHandle.Init.OverSampling = UART_OVERSAMPLING_16, /* 过采样 */

#if defined (STM32L4_SGA_ENABLE)
.tUARTHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE,
.tUARTHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT,
.tUARTHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE,
.tUARTHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT,
#endif

.ucPriority = 1, /* 中断优先级 */
.ucSubPriority = 3, /* 中断子优先级 */
.ucPriority = 1, /* 中断优先级 */
.ucSubPriority = 3, /* 中断子优先级 */

//串口DMA接收参数配置
.tUartDMA.bRxEnable = true, /* DMA接收使能 */
.tUartDMA.tDMARx.Instance = DMA1_Channel5,
.tUartDMA.tDMARx.Init.Direction = DMA_PERIPH_TO_MEMORY,
.tUartDMA.tDMARx.Init.PeriphInc = DMA_PINC_DISABLE,
.tUartDMA.tDMARx.Init.MemInc = DMA_MINC_ENABLE,
.tUartDMA.tDMARx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE,
.tUartDMA.tDMARx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE,
.tUartDMA.tDMARx.Init.Mode = DMA_CIRCULAR,
.tUartDMA.tDMARx.Init.Priority = DMA_PRIORITY_LOW,
.tUartDMA.bRxEnable = true, /* DMA接收使能 */
.tUartDMA.tDMARx.Instance = DMA1_Channel5,
.tUartDMA.tDMARx.Init.Direction = DMA_PERIPH_TO_MEMORY,
.tUartDMA.tDMARx.Init.PeriphInc = DMA_PINC_DISABLE,
.tUartDMA.tDMARx.Init.MemInc = DMA_MINC_ENABLE,
.tUartDMA.tDMARx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE,
.tUartDMA.tDMARx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE,
.tUartDMA.tDMARx.Init.Mode = DMA_CIRCULAR,
.tUartDMA.tDMARx.Init.Priority = DMA_PRIORITY_LOW,

.tRxInfo.usDMARxMAXSize = 100, /* 接收数据长度 长度保持在协议最长字节*2以上,确保缓存池一定能够稳定接收一个完整的数据帧*/
.tRxInfo.usDMARxMAXSize = 100, /* DMA接收缓冲区大小 大小保持在协议最长字节*2以上,确保缓存池一定能够稳定接收一个完整的数据帧*/

.tUartDMA.ucDMARxPriority = 1, /* DMA接收中断优先级 */
.tUartDMA.ucDMARxSubPriority = 1, /* DMA接收中断子优先级 */
.tUartDMA.ucDMARxPriority = 1, /* DMA接收中断优先级 */
.tUartDMA.ucDMARxSubPriority = 1, /* DMA接收中断子优先级 */

//串口DMA发送参数配置
.tUartDMA.bTxEnable = true, /* DMA发送使能 */
.tUartDMA.tDMATx.Instance = DMA1_Channel4,
.tUartDMA.tDMATx.Init.Direction = DMA_MEMORY_TO_PERIPH,
.tUartDMA.tDMATx.Init.PeriphInc = DMA_PINC_DISABLE,
.tUartDMA.tDMATx.Init.MemInc = DMA_MINC_ENABLE,
.tUartDMA.bTxEnable = true, /* DMA发送使能 */
.tUartDMA.tDMATx.Instance = DMA1_Channel4,
.tUartDMA.tDMATx.Init.Direction = DMA_MEMORY_TO_PERIPH,
.tUartDMA.tDMATx.Init.PeriphInc = DMA_PINC_DISABLE,
.tUartDMA.tDMATx.Init.MemInc = DMA_MINC_ENABLE,
.tUartDMA.tDMATx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE,
.tUartDMA.tDMATx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE,
.tUartDMA.tDMATx.Init.Mode = DMA_NORMAL,
.tUartDMA.tDMATx.Init.Priority = DMA_PRIORITY_LOW,
.tUartDMA.tDMATx.Init.Mode = DMA_NORMAL,
.tUartDMA.tDMATx.Init.Priority = DMA_PRIORITY_LOW,

.tTxInfo.usDMATxMAXSize = 50, /* 最大发送数据长度 */
.tTxInfo.usDMATxMAXSize = 50, /* DMA发送缓冲区大小 */

.tUartDMA.ucDMATxPriority = 1, /* DMA发送中断优先级 */
.tUartDMA.ucDMATxSubPriority = 1, /* DMA发送中断子优先级 */
.tUartDMA.ucDMATxPriority = 1, /* DMA发送中断优先级 */
.tUartDMA.ucDMATxSubPriority = 1, /* DMA发送中断子优先级 */

//串口GPIO配置
.tGPIO[0].tGPIOInit.Pin = GPIO_PIN_9, /* GPIO引脚 */
.tGPIO[0].tGPIOInit.Mode = GPIO_MODE_AF_PP, /* GPIO模式 */
.tGPIO[0].tGPIOInit.Pull = GPIO_NOPULL, /* GPIO上下拉设置,是否需要上下拉看硬件 */
.tGPIO[0].tGPIOInit.Speed = GPIO_SPEED_FREQ_HIGH, /* GPIO速度 */
.tGPIO[0].tGPIOPort = GPIOA, /* GPIO分组 */
.tGPIO[0].ucAFMode = NO_REMAP, /* GPIO重映射 */
.tGPIO[0].tGPIOInit.Pin = GPIO_PIN_9, /* GPIO引脚 */
.tGPIO[0].tGPIOInit.Mode = GPIO_MODE_AF_PP, /* GPIO模式 */
.tGPIO[0].tGPIOInit.Pull = GPIO_NOPULL, /* GPIO上下拉设置,是否需要上下拉看硬件 */
.tGPIO[0].tGPIOInit.Speed = GPIO_SPEED_FREQ_HIGH, /* GPIO速度 */
.tGPIO[0].tGPIOPort = GPIOA, /* GPIO分组 */
.tGPIO[0].ucAFMode = NO_REMAP, /* GPIO重映射 */

.tGPIO[1].tGPIOInit.Pin = GPIO_PIN_10, /* GPIO引脚 */
.tGPIO[1].tGPIOInit.Mode = GPIO_MODE_INPUT, /* GPIO模式 */
.tGPIO[1].tGPIOInit.Pull = GPIO_NOPULL, /* GPIO上下拉设置,是否需要上下拉看硬件 */
.tGPIO[1].tGPIOInit.Speed = GPIO_SPEED_FREQ_HIGH, /* GPIO速度 */
.tGPIO[1].tGPIOPort = GPIOA, /* GPIO分组 */
.tGPIO[1].ucAFMode = NO_REMAP, /* GPIO重映射 */
.tGPIO[1].tGPIOInit.Pin = GPIO_PIN_10, /* GPIO引脚 */
.tGPIO[1].tGPIOInit.Mode = GPIO_MODE_INPUT, /* GPIO模式 */
.tGPIO[1].tGPIOInit.Pull = GPIO_NOPULL, /* GPIO上下拉设置,是否需要上下拉看硬件 */
.tGPIO[1].tGPIOInit.Speed = GPIO_SPEED_FREQ_HIGH, /* GPIO速度 */
.tGPIO[1].tGPIOPort = GPIOA, /* GPIO分组 */
.tGPIO[1].ucAFMode = NO_REMAP, /* GPIO重映射 */
};
1 change: 1 addition & 0 deletions Apply/Logic/Src/threadpool.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "usercode.h" /* usercode头文件 */
#include "threadpool.h" /* threadpool头文件 */
#include "drv_hal_conf.h" /* SGA库头文件配置 */
#include "task_conf.h" /* task层头文件配置 */
Expand Down
4 changes: 2 additions & 2 deletions Apply/Task/Src/task_irq.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "task_conf.h"

#include "usercode.h"
#include "config.h"


Expand Down Expand Up @@ -38,7 +38,7 @@ void USART1_IRQHandler(void)
void USART2_IRQHandler(void)
{
/* 示例 */
// Drv_Uart_IRQHandler(&tJY901B.tUART); /* 必需部分 */
//Drv_Uart_IRQHandler(&tJY901B.tUART); /* 必需部分 */
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Doc/工程维护记录.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
*
/***************************************************************************************************************************************************************/
* �汾�� ���� ���� ˵��
* 2023-9-22 ����� ��������
* 2.8 2023-10-15 ����� ���������������޸
*
* �޸ļ�¼ :
* 1.��ʱ���趨��ʱ�򻯣���ʱ����ʼ���ṹ������Ĭ��ֵ��
* 2.����drv_hal_flash.c�ļ�������Ƭ��Flash��������
* 3.�޸�����DMA���͵����⡣
*
/***************************************************************************************************************************************************************/
*
Expand Down
2 changes: 1 addition & 1 deletion Driver/Inc/drv_hal_conf.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __DRV_CONF_H_
#define __DRV_CONF_H_

#define DRIVER_VERSION "2023/8/9 V2.7"
#define DRIVER_VERSION "2023/10/15 V2.8"

/* RT-Thread开关 使用RTT时需解除注释,且在工程中导入RTT相关内核 */
//#define RTT_ENABLE
Expand Down
8 changes: 4 additions & 4 deletions Driver/Inc/drv_hal_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include "drv_hal_conf.h"

int Drv_Flash_Erase_Page(uint32_t ulPageAddr,uint32_t ulNum);
int Drv_Flash_Erase_Sector(uint32_t ulStart_Addr, uint32_t ulEnd_Addr);
void Drv_Flash_Write(uint32_t ulAddr,uint32_t *ulpBuf,uint32_t ulWordSize);
void Drv_Flash_Read(uint32_t ulAddr, uint32_t *ulpBuf,uint32_t ulWordSize);
int Drv_Flash_Erase_Page(uint32_t _ulPageAddr,uint32_t _ulNum);
int Drv_Flash_Erase_Sector(uint32_t _ulStart_Addr, uint32_t _ulEnd_Addr);
void Drv_Flash_Write(uint32_t _ulAddr,uint32_t *_ulpBuf,uint32_t _ulWordSize);
void Drv_Flash_Read(uint32_t _ulAddr, uint32_t *_ulpBuf,uint32_t _ulWordSize);


#endif // !__DRV_FLASH_H_
8 changes: 4 additions & 4 deletions Driver/Inc/drv_hal_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ typedef struct

void Drv_Uart_ITInit(tagUART_T *_tUART);
void Drv_Uart_DMAInit(tagUART_T *_tUART);
void Drv_Uart_Transmit(tagUART_T *_tUART, uint8_t *_ucpTxData, uint16_t _uspSize);
void Drv_Uart_Transmit_IT(tagUART_T *_tUART, uint8_t *_ucpTxData, uint16_t _uspSize);
void Drv_Uart_Transmit_DMA(tagUART_T *_tUART, uint8_t *_ucpTxData, uint16_t _uspSize);
void Drv_Uart_Transmit(tagUART_T *_tUART, uint8_t *_ucpTxData, uint16_t _usSize);
void Drv_Uart_Transmit_IT(tagUART_T *_tUART, uint8_t *_ucpTxData, uint16_t _usSize);
void Drv_Uart_Transmit_DMA(tagUART_T *_tUART, uint8_t *_ucpTxData, uint16_t _usSize);
uint16_t Drv_Uart_Receive_IT(tagUART_T *_tUART, uint8_t *_ucpRxData);
uint16_t Drv_Uart_Receive_DMA(tagUART_T *_tUART, uint8_t *_ucpRxData);
void Drv_Uart_ReceIT_Enable(tagUART_T *_tUART, uint8_t *_ucpBuffer, uint16_t _uspSize);
void Drv_Uart_ReceIT_Enable(tagUART_T *_tUART, uint8_t *_ucpRxData, uint16_t _usSize);
void Drv_Uart_IRQHandler(tagUART_T *_tUART);
void Drv_Uart_IT_RxHandler(tagUART_T *_tUART, uint8_t _ucEndChar);
void Drv_Uart_DMA_RxHandler(tagUART_T *_tUART);
Expand Down
72 changes: 36 additions & 36 deletions Driver/Src/drv_hal_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* 文件历史:
* 版本号 日期 作者 说明
* 2023-09-22 鲍程璐 创建该文件
* 2.8 2023-09-22 鲍程璐 创建该文件
****************************************************************************/
#include "drv_hal_conf.h"
Expand All @@ -21,20 +21,20 @@
/**
* @brief Flash擦除页(F1系列)
*
* @param ulPageAddr 起始地址
* @param ulNum 擦除的页数
* @param _ulPageAddr 起始地址
* @param _ulNum 擦除的页数
* @return int 0 成功 -1 失败
*/
int Drv_Flash_Erase_Page(uint32_t ulPageAddr,uint32_t ulNum)
int Drv_Flash_Erase_Page(uint32_t _ulPageAddr,uint32_t _ulNum)
{
/* 解锁Flash */
HAL_FLASH_Unlock();

/* 擦除Flash*/
FLASH_EraseInitTypeDef FlashSet;
FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;
FlashSet.PageAddress = ulPageAddr;
FlashSet.NbPages = ulNum;
FlashSet.PageAddress = _ulPageAddr;
FlashSet.NbPages = _ulNum;

/*设置PageError,调用擦除函数*/
uint32_t PageError = 0;
Expand All @@ -56,36 +56,36 @@ int Drv_Flash_Erase_Page(uint32_t ulPageAddr,uint32_t ulNum)

/**
* @brief 获取地址所在的sector
* @param ulAddress 起始地址
* @param _ulAddress 起始地址
* @return uint32_t ulSector 扇区号
*/
static uint32_t S_Flash_Get_Sector(uint32_t ulAddress)
static uint32_t S_Flash_Get_Sector(uint32_t _ulAddress)
{
uint32_t ulSector = 0;

(ulAddress <= 0x080FFFFF && ulAddress >= 0x080E0000)? ulSector = 11:
(ulAddress >= 0x080C0000)? ulSector = 10:
(ulAddress >= 0x080A0000)? ulSector = 9:
(ulAddress >= 0x08080000)? ulSector = 8:
(ulAddress >= 0x08060000)? ulSector = 7:
(ulAddress >= 0x08040000)? ulSector = 6:
(ulAddress >= 0x08020000)? ulSector = 5:
(ulAddress >= 0x08010000)? ulSector = 4:
(ulAddress >= 0x0800C000)? ulSector = 3:
(ulAddress >= 0x08008000)? ulSector = 2:
(ulAddress >= 0x08006000)? ulSector = 1:
(ulAddress >= 0x08004000)? ulSector = 1:0;
(_ulAddress <= 0x080FFFFF && _ulAddress >= 0x080E0000)? ulSector = 11:
(_ulAddress >= 0x080C0000)? ulSector = 10:
(_ulAddress >= 0x080A0000)? ulSector = 9:
(_ulAddress >= 0x08080000)? ulSector = 8:
(_ulAddress >= 0x08060000)? ulSector = 7:
(_ulAddress >= 0x08040000)? ulSector = 6:
(_ulAddress >= 0x08020000)? ulSector = 5:
(_ulAddress >= 0x08010000)? ulSector = 4:
(_ulAddress >= 0x0800C000)? ulSector = 3:
(_ulAddress >= 0x08008000)? ulSector = 2:
(_ulAddress >= 0x08006000)? ulSector = 1:
(_ulAddress >= 0x08004000)? ulSector = 1:0;

return ulSector;
}

/**
* @brief Flash擦除Sector(F4系列)
* @param ulStart_Addr 起始地址
* @param ulEnd_Addr 结束地址
* @param _ulStart_Addr 起始地址
* @param _ulEnd_Addr 结束地址
* @return 0 成功 -1 失败
*/
int Drv_Flash_Erase_Sector(uint32_t ulStart_Addr, uint32_t ulEnd_Addr)
int Drv_Flash_Erase_Sector(uint32_t _ulStart_Addr, uint32_t _ulEnd_Addr)
{
uint32_t UserStartSector;
uint32_t SectorError = 0;
Expand All @@ -95,11 +95,11 @@ int Drv_Flash_Erase_Sector(uint32_t ulStart_Addr, uint32_t ulEnd_Addr)
HAL_FLASH_Unlock();

/* 获取起始地址的扇区,擦除FLASH*/
UserStartSector = S_Flash_Get_Sector(start_addr);
UserStartSector = S_Flash_Get_Sector(_ulStart_Addr);

FlashSet.TypeErase = TYPEERASE_SECTORS;
FlashSet.Sector = UserStartSector;
FlashSet.NbSectors = S_Flash_Get_Sector(end_addr) - UserStartSector;
FlashSet.NbSectors = S_Flash_Get_Sector(_ulEnd_Addr) - UserStartSector;
FlashSet.VoltageRange = VOLTAGE_RANGE_3;

/*调用擦除函数*/
Expand All @@ -119,19 +119,19 @@ int Drv_Flash_Erase_Sector(uint32_t ulStart_Addr, uint32_t ulEnd_Addr)

/**
* @brief Flash写若干个数据(word)
* @param ulAddr 写入的地址
* @param ulpBuf 写入数据的起始地址
* @param ulWordSize 长度
* @param _ulAddr 写入的地址
* @param _ulpBuf 写入数据的起始地址
* @param _ulWordSize 长度
* @return NULL
*/
void Drv_Flash_Write(uint32_t ulAddr,uint32_t *ulpBuf,uint32_t ulWordSize)
void Drv_Flash_Write(uint32_t _ulAddr,uint32_t *_ulpBuf,uint32_t _ulWordSize)
{
/* 解锁Flash */
HAL_FLASH_Unlock();

/* 烧写到指定地址 */
for(uint32_t i = 0;i < ulWordSize;i++)
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,ulAddr + 4*i,ulpBuf[i]);
for(uint32_t i = 0;i < _ulWordSize;i++)
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,_ulAddr + 4*i,_ulpBuf[i]);

/* 锁定Flash */
HAL_FLASH_Lock();
Expand All @@ -141,14 +141,14 @@ void Drv_Flash_Write(uint32_t ulAddr,uint32_t *ulpBuf,uint32_t ulWordSize)

/**
* @brief Flash读若干个数据(word)
* @param ulAddr 读数据的地址
* @param ulpBuf 读出数据的数组指针
* @param ulWordSize 长度
* @param _ulAddr 读数据的地址
* @param _ulpBuf 读出数据的数组指针
* @param _ulWordSize 长度
* @return NULL
*/
void Drv_Flash_Read(uint32_t ulAddr, uint32_t *ulpBuf,uint32_t ulWordSize)
void Drv_Flash_Read(uint32_t _ulAddr, uint32_t *_ulpBuf,uint32_t _ulWordSize)
{
memcpy(ulpBuf, (uint32_t*) ulAddr, ulWordSize * sizeof(uint32_t));
memcpy(_ulpBuf, (uint32_t*) _ulAddr, _ulWordSize * sizeof(uint32_t));
}

#endif
2 changes: 1 addition & 1 deletion Driver/Src/drv_hal_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* 文件历史:
* 版本号 日期 作者 说明
* 2023-09-04 鲍程璐 定时器设定计时简化
* 2.8 2023-09-04 鲍程璐 定时器设定计时简化
* 1.0.0a 2020-02-22 李环宇 创建该文件
Expand Down
Loading

0 comments on commit 9348888

Please sign in to comment.