STM32学习笔记——DMA通信
上周学习使用串口通信收发数据,并使用USART1和USART3收发陀螺仪JY901s的数据。经过同学的指导,能够成功接收并处理数据。最后发送出来。这次记录一下学习DMA通信内容。
DMA通信流程
下面展示一些 内联代码片
。
#include "usart1.h"
#include "stm32f1xx.h"
#include <stdio.h>
#include <string.h>
uint8_t receive_buff[255]={0};
char tx_buffer[100]={0};
void usart1_dma_recv(void)
{
// uint8_t receive_buff[255]={0};
// uint8_t tx_buffer[100]={0};
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE) ;
// HAL_UART_Receive_DMA(&huart1, (uint8_t *)receive_buff, BUFFER_SIZE);
memcpy(tx_buffer,"this is a start\n",100);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)tx_buffer,strlen(tx_buffer));
}
void USER_UART_IRQHandler(UART_HandleTypeDef *huart)
{
if(USART1==huart1.Instance)
{
if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE))!=RESET)
{
__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_IDLE);
USAR_UART_IDLECallback(huart);
}
}
}
void USAR_UART_IDLECallback(UART_HandleTypeDef *huart)
{
// HAL_UART_Receive_DMA(&huart1, (uint8_t *)receive_buff, BUFFER_SIZE);
HAL_UART_DMAStop(&huart1);
uint8_t data_lenth=BUFFER_SIZE-__HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)receive_buff, data_lenth);
data_lenth=0;
HAL_UART_Receive_DMA(&huart1, (uint8_t *)receive_buff, BUFFER_SIZE);
}
实验结果