uart program

cancel
Showing results for 
Search instead for 
Did you mean: 

uart program

Jump to solution
580 Views
lijofrancis
Contributor III

dear sir,

i am using K60 controller .i wrote code to read write packets using uart that working.my aim is to get inerrupt when receive 4packets or may be 6 packets .the number may vary.if i want to get 5 packets when 5 packet come i want an interrupt.next i want to get interrupt when 10 packets come.what am i to do .there is fifo method in uart. i dont know how to use and other method is dma method.There is no more explanation in in datasheet or there is no sample code.

which is better method for my purpose dma or fifo uart method

and also i want to know that the two method is same or not please help me answer me as soon as

1 Solution
117 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

In your code below is set DMA channel1 for UART0-RX, while you setting DMA Channel 0 to do DMA transfer. Customer need to change below code using DMAMUX_CHCFG0 register.

DMAMUX0_CHCFG1 |= ( 0 | DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(2)); // enable the DMA channel request  // UART0-Rx is source 2

Wish it helps.

Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
11 Replies
117 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

UART0 and UART1 contains 8-entry transmit and 8-entry receive FIFOs, customer can set the water mark to generate the interrupt.

So, customer need to check the packet size, for the UART FIFO only 8-entry, 8 bytes in general.

If customer want to receive more packets and each packet with many bytes data, the FIFO mode shows the shortage.

The DMA mode could generate the interrupt after receive setting value bytes (as a major loop), and when UART receive data will trigger DMA transfer (as minor loop). After the packet data (customer setting numbers data) be received, the DMA will generate interrupt for handle.

Using DMA mode, customer need to know how many bytes will be receive of 5 packets.

Wish it helps.


Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

117 Views
lijofrancis
Contributor III

dear sir,

thank you for you response.i think you suggest for me dma method.can i vary received data bytes length.means if i get 5 bytes then dma interrupt comes next i want 10 bytes therefor i change byte length 10 to receive interrupt .is it possible is there any sample code  to do this please help me

0 Kudos
117 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

Yes, the DMA transfer data number could be modified at DMA TCD (Transfer Control Descriptor) register DMA_TCDn_NBYTES_MLNO [NBYTES] bits.

Please check attached UART DMA validation code for your reference.

Wish it helps.


Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

117 Views
lijofrancis
Contributor III

dear sir,

i write code to receive uart data normally.i get data bytes one by one its working

then i use dma method basis of your sample code but it not works .i write code to receive 4 bytes.but

code stuck at

while( (!(DMA_TCD0_CSR & DMA_CSR_DONE_MASK)) &(!(DMA_ES) ));

when i comment this while i gets value zero only

code

/* Enable the clock to the DMA channel mux */

   SIM_SCGC6 |= SIM_SCGC6_DMAMUX0_MASK;

     /* Enable the clock to the DMA module */

   SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;

   DMAMUX0_CHCFG1 |= ( 0 | DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(2));     // enable the DMA channel request  // UART0-Rx is source 2

  /* Configure UART to generate Rx  DMA requests */

  UART_C2_REG(UART0_BASE_PTR) |= UART_C2_RIE_MASK;

  UART_C5_REG(UART0_BASE_PTR) |= UART_C5_RDMAS_MASK;

  /* Configure DMA Channel TCD */

  DMA_TCD0_SADDR = (uint32) &UART_D_REG(UART0_BASE_PTR); // DMA source is the UART data register

  DMA_TCD0_ATTR = ( 0 | DMA_ATTR_SSIZE(0)| DMA_ATTR_DSIZE(0) );

  DMA_TCD0_SOFF = 0x0; // don't increment source address

  DMA_TCD0_NBYTES_MLNO = 1; // move 1 byte per request

  DMA_TCD0_SLAST = 0x0; // no adjust after last transfer

  DMA_TCD0_DADDR = (uint32)&packet[0]; // destination is the string variable

  DMA_TCD0_CITER_ELINKNO = ( 0 | DMA_CITER_ELINKNO_CITER(4) );       // execute the minor loop strLen times

  DMA_TCD0_DOFF = 0x01; // increment dest by 1 after each transfer

  DMA_TCD0_DLASTSGA = 0x0; // no adjust after last transfer

  DMA_TCD0_BITER_ELINKNO = ( 0  | DMA_BITER_ELINKNO_BITER(4) );       // execute the minor loop strLen times

  DMA_TCD0_CSR= ( 0| DMA_CSR_DREQ_MASK );// disable request when the transfer is complete

  while(1)

  {

  /* Wait for DMA to complete or to generate an error */

  while( (!(DMA_TCD0_CSR & DMA_CSR_DONE_MASK)) &(!(DMA_ES) ));

  for(char j=0;j<4;++j)

     printf("%x",packet[j]);

  printf("\n\r");

  }

0 Kudos
118 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi

In your code below is set DMA channel1 for UART0-RX, while you setting DMA Channel 0 to do DMA transfer. Customer need to change below code using DMAMUX_CHCFG0 register.

DMAMUX0_CHCFG1 |= ( 0 | DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(2)); // enable the DMA channel request  // UART0-Rx is source 2

Wish it helps.

Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
117 Views
lijofrancis
Contributor III

hello sir,

i stuck at the code please help me

same as i told above i got 4 bytes at ones .

actually i continuesly try to receive uart bytes but i get 4 bytes.To get remaining bytes i put the dma code as shown above in while loop.but i didnt get remaining bytes .i got 4 bytes when controller restart.then it stuck at while loop....

when i use DMA_TCD0_CSR= ( 0| DMA_CSR_DREQ_MASK )  status same as above.But when i replace it with

MA_TCD0_CSR |= DMA_CSR_START_MASK; i get no bytes ,starting also.

please sir i dont know what is the problem

0 Kudos
117 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

When the DMA finish the major loop (receive 4 bytes), it need to refresh the DMA TCD register, such as DMA_TCD0_CITER_ELINKNO register and DMA_TCD0_BITER_ELINKNO register.

In your code enable the channel’s ERQ bit is cleared when the major loop is complete setting, so it need to re-enable the channel's ERQ bit at DMA_ERQ register bit 0 (set this bit)  or write 0x0 to DMA_SERQ register .

Wish it helps.


Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
117 Views
lijofrancis
Contributor III

dear sir,

In my code i enable clock for dma,and also enabled it

and also enabled receive for uart and dma for receive

while(1)

  {

   // Configure RX DMA

   DMAMUX0_CHCFG0 = 0;

   DMA_CERQ       = DMA_CERQ_CERQ(0);

   DMA_TCD0_CSR   = DMA_CSR_DREQ_MASK;

   DMA_TCD0_BITER_ELINKNO = 0;

   DMA_TCD0_CITER_ELINKNO = 0;

   DMA_TCD0_SADDR = (uint32) &UART_D_REG(UART0_BASE_PTR);

   DMA_TCD0_SOFF = 0x0;

   DMA_TCD0_SLAST = 0x0;

   DMA_TCD0_DADDR = (uint32)&packet;

   DMA_TCD0_DOFF = 0x01;

   DMA_TCD0_DLASTSGA = 0x0;

   DMA_TCD0_ATTR = ( 0 | DMA_ATTR_SSIZE(0)| DMA_ATTR_DSIZE(0) );

   DMA_TCD0_NBYTES_MLNO = 1;

   DMA_TCD0_BITER_ELINKNO = 0;

   DMA_TCD0_CITER_ELINKNO = 0;

   DMAMUX0_CHCFG0 |= ( 0 | DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(2));

   DMA_TCD0_CITER_ELINKNO = ( 0 | DMA_CITER_ELINKNO_CITER(4) );

   DMA_TCD0_BITER_ELINKNO = ( 0  | DMA_BITER_ELINKNO_BITER(4) );

  // DMA_TCD0_CSR= ( 0| DMA_CSR_DREQ_MASK );

  // DMA_SERQ      = DMA_SERQ_SERQ(0);

   DMA_ERQ |= DMA_ERQ_ERQ0_MASK;

while(((DMA_TCD0_CSR & DMA_CSR_DONE_MASK) == 0) && ( DMA_ES == 0));

for(char j=0;j<4;++j)

   printf("%x",packet[j]);


}

result            10561e

i got only one packet

second time code stuck at while(((DMA_TCD0_CSR & DMA_CSR_DONE_MASK) == 0) && ( DMA_ES == 0));

actualy receive bytes comes frequently but dma receives 4 bytes only one time

0 Kudos
117 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

I would recommend to enable the an interrupt when major iteration count completes at DMA_TCD0_CSR register to generate an interrupt after receive 4 bytes. At the interrupt service routine using below code to refresh the TCD registers:

DMA_TCD0_DADDR = (uint32)&packet;

DMA_TCD0_NBYTES_MLNO = 1;

DMA_TCD0_CITER_ELINKNO = ( 0 | DMA_CITER_ELINKNO_CITER(4) );

DMA_TCD0_BITER_ELINKNO = ( 0  | DMA_BITER_ELINKNO_BITER(4) );

DMA_TCD0_CSR |= DMA_CSR_DREQ_MASK; //One transfer only.

DMA_ERQ |= DMA_ERQ_ERQ0_MASK;

There doesn't recommend to re-init DMA setting in a while loop.

Wish it helps.

 
Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

117 Views
lijofrancis
Contributor III

hello sir,

Thank you .Its working:smileyhappy::smileyhappy:

0 Kudos
117 Views
lijofrancis
Contributor III

hello sir,

thank you, i got 4 bytes

but problem is that i got that 4 bytes only one time

actually i put that code above in while loop and waiting for for each 4 bytes

second time it stuck at while( (!(DMA_TCD0_CSR & DMA_CSR_DONE_MASK)) &(!(DMA_ES) ))

please sir any solution for above problem

0 Kudos