usart_handler: DMA based memory copy

This commit is contained in:
Attila Body 2019-10-01 14:49:10 +02:00
parent e8ab90c2b3
commit ecd6c1d5d6
13 changed files with 367 additions and 408 deletions

View file

@ -16,10 +16,6 @@ void HandleConsoleUsartTxDmaIrq(DMAINFO *info, USART_TypeDef *usart) // debug us
LL_USART_EnableIT_TC(usart);
LL_DMA_DisableStream(info->dma, info->stream);
}
else if(*info->isReg & info->htMask)
*info->ifcReg = info->htMask;
else if(*info->isReg & info->teMask)
*info->ifcReg = info->teMask;
}
void HandleConsoleUsartIrq(USART_TypeDef *usart)

39
lib/memcpy_dma.c Normal file
View file

@ -0,0 +1,39 @@
/*
* memcpy_dma.c
*
* Created on: Oct 1, 2019
* Author: abody
*/
#include "memcpy_dma.h"
#include "dma_helper.h"
volatile uint8_t g_memcpyDmaBusy = 0;
static DMAINFO g_memcpyDmaInfo;
void InitMemcpyDma(DMA_TypeDef *dma, uint32_t stream)
{
InitDmaInfo(&g_memcpyDmaInfo, dma, stream);
LL_DMA_EnableIT_TC(dma, stream);
}
void * MemcpyDma(void *dst, void const *src, size_t length)
{
LL_DMA_SetM2MSrcAddress(g_memcpyDmaInfo.dma, g_memcpyDmaInfo.stream, (uint32_t)src);
LL_DMA_SetM2MDstAddress(g_memcpyDmaInfo.dma, g_memcpyDmaInfo.stream, (uint32_t)dst);
LL_DMA_SetDataLength(g_memcpyDmaInfo.dma, g_memcpyDmaInfo.stream, (length+3)/4 );
g_memcpyDmaBusy = 1;
//DIAG_CRC_CALC_START();
LL_DMA_EnableStream(g_memcpyDmaInfo.dma, g_memcpyDmaInfo.stream);
while(g_memcpyDmaBusy);
return dst;
}
void HandleMemcpyDmaIrq()
{
if(*g_memcpyDmaInfo.isReg & g_memcpyDmaInfo.tcMask) { // DMA transfer complete
*g_memcpyDmaInfo.ifcReg = g_memcpyDmaInfo.tcMask;
LL_DMA_DisableStream(g_memcpyDmaInfo.dma, g_memcpyDmaInfo.stream);
g_memcpyDmaBusy = 0;
}
}

20
lib/memcpy_dma.h Normal file
View file

@ -0,0 +1,20 @@
/*
* memcpy_dma.h
*
* Created on: Oct 1, 2019
* Author: abody
*/
#ifndef MEMCPY_DMA_H_
#define MEMCPY_DMA_H_
#include <inttypes.h>
#include "dma.h"
//#include "dma_helper.h"
//extern DMAINFO g_memcpyDmaInfo;
void InitMemcpyDma(DMA_TypeDef *dma, uint32_t stream);
void * MemcpyDma(void *dst, void const *src, size_t length);
void HandleMemcpyDmaIrq();
#endif /* MEMCPY_DMA_H_ */

View file

@ -11,6 +11,7 @@
#include "usart_handler.h"
#include "dma_helper.h"
#include "crc_handler.h"
#include "memcpy_dma.h"
#ifndef DIAG_RX_BUFFER_SWITCH
# define DIAG_RX_BUFFER_SWITCH(x)
@ -73,7 +74,7 @@ uint8_t PostPacket(UARTSTATUS *status, uint8_t const *payload, uint16_t length,
BuildHeader(&status->txBuffer, status->txSerial++, length);
uint16_t payloadLength = (length+3) & 0xfffc; // round up to 4
if(payload)
memcpy(status->txBuffer.packet.payload, payload, length);
MemcpyDma(status->txBuffer.packet.payload, payload, length);
status->txBuffer.requestedLength = sizeof(UARTPACKETHEADER) + payloadLength + sizeof(uint32_t); // +4 for the hash
status->txBuffer.busy = 1;
status->txBuffer.error = 0;
@ -118,7 +119,7 @@ void ConsumePacket(UARTSTATUS *status, uint8_t packetIndex, struct crcstatus_t *
void SetupTransmit(USART_TypeDef *uart, DMA_TypeDef* dma, uint32_t stream, void *buffer, uint32_t length)
{
LL_DMA_ConfigAddresses(dma, stream, (uint32_t)buffer, LL_USART_DMA_GetRegAddr(uart),LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
LL_DMA_ConfigAddresses(dma, stream, (uint32_t)buffer, LL_USART_DMA_GetRegAddr(uart), LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
LL_DMA_SetDataLength(dma, stream, length);
LL_USART_EnableDMAReq_TX(uart);
LL_DMA_EnableStream(dma, stream);