cancel
Showing results for 
Search instead for 
Did you mean: 

USART-DMA Problem

jki79ros81
Associate II
Posted on November 21, 2008 at 23:02

USART-DMA Problem

3 REPLIES 3
jki79ros81
Associate II
Posted on May 17, 2011 at 12:53

Hi, everyone!

I have a program that send a command and receive response via rs-232.

The commands is any length.

And I made a module that receive a command and send response.

I used DMA for it.

My problem is how do I predict command's length in the module?

I configured DMA for the reading as following.

...............................

#define MAX_COMMAND_LEN 400

u8 com_buf[MAX_COMMAND_LEN + 1];

u8 read_end = false;

..................................

void DMA_Channel5_Configuration(void)

{

DMA_InitTypeDef DMA_InitStructure;

/* DMA1 Channel5 (triggered by USART1 Rx event) Config */

DMA_DeInit(DMA1_Channel5);

DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;

DMA_InitStructure.DMA_MemoryBaseAddr = (u32)com_buf;

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

DMA_InitStructure.DMA_BufferSize = MAX_COMMAND_LEN;

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;

DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

DMA_Init(DMA1_Channel5, &DMA_InitStructure);

}

void DMA1_Channel5_IRQHandler(void)

{

u16 w_nCmdCode;

if(DMA_GetITStatus(DMA1_IT_TC5) == SET){

read_end = TRUE;

}

}

.......................................

void main()

{

..........

while(read_end == TRUE){

Process();

SendResponse();

read_end == FALSE;

}

..........

}

Now I could not predict the command's length,therefore wrote as DMA_InitStructure.DMA_BufferSize = MAX_COMMAND_LEN;

And the length of command which the program send must be equal to MAX_COMMAND_LEN. So I added dummy bytes(400-16 = 384 bytes) to end of 16byte command .

This works OK, but it is very non-efficient and slow when baudrate is low.

Please help.

Regards.

[ This message was edited by: jki79ros81 on 21-11-2008 09:16 ]

brianforde9
Associate II
Posted on May 17, 2011 at 12:53

A variable length packet should always be lead by a length field (byte, word or whatever). Receive the length field using interrupt processing, configure the DMA from the received length, and then continue to receive the remainder of the packet using the DMAC.

Brian F.

jki79ros81
Associate II
Posted on May 17, 2011 at 12:53

Thank you , Brian F!

I too thought this.But must send length packet ,then command packet again.

Is there only this method?

Thank you.

[ This message was edited by: jki79ros81 on 22-11-2008 03:35 ]

[ This message was edited by: jki79ros81 on 22-11-2008 14:07 ]