cancel
Showing results for 
Search instead for 
Did you mean: 

Troubles at receive more than one character (RTOS-RL-ARM)

esiqueira
Associate II
Posted on March 02, 2012 at 12:34

How to send and receive data using RTOS (RL-ARM) with tasks?

(Without generating an interruption?)

Using this functions below:

TO SEND

/*----------------------------------------------------------------------------

  Write character to Serial Port

 *----------------------------------------------------------------------------*/

int sendchar (int c) {

  if (c == '\n')  {

    while (!(USARTx->SR & USART_FLAG_TXE));

    USARTx->DR = 0x0D;

  }

  while (!(USARTx->SR & USART_FLAG_TXE));

  USARTx->DR = (c & 0x1FF);

  return (c);

}

TO RECEIVE

/*----------------------------------------------------------------------------

 *      Line Editor

 *---------------------------------------------------------------------------*/

void getline (char *line, int n) {

  int  cnt = 0;

  char c;

  do {

    if ((c = getkey ()) == CR)  c = LF;    /* read character                 */

    if (c == BACKSPACE  ||  c == DEL) {    /* process backspace              */

      if (cnt != 0)  {           

        cnt--;                             /* decrement count                */

        line--;                            /* and line pointer               */

        putchar (BACKSPACE);               /* echo backspace                 */

        putchar (' ');

        putchar (BACKSPACE);

      }

    }

    else if (c != CNTLQ && c != CNTLS) {   /* ignore Control S/Q             */

      putchar (*line = c);                 /* echo and store character       */

      line++;                              /* increment line pointer         */

      cnt++;                               /* and count                      */

    }

  } while (cnt < n - 1  &&  c != LF);      /* check limit and line feed      */

  *(line - 1) = 0;                         /* mark end of string             */

}

#rtos-rlarm-usart-send-receive
3 REPLIES 3
emalund
Associate III
Posted on March 02, 2012 at 16:25

(Without generating an interruption?)

WHY?

interrupts are just about the most efficent invention in computer design ever made.

Erik

Posted on March 02, 2012 at 16:48

I think the question is how to do it without stalling the CPU waiting for the USART.

Which would be to make some FIFO buffers, and service the USART interrupts to feed the data in/out. And modify the sendchar() and getkey() to push/pull data to the buffers.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
esiqueira
Associate II
Posted on March 08, 2012 at 13:07

Thanks for the answers

Actually, is generating interruption. Sorry for my mistake Mr. Erik.

As you can see in code below

/*----------------------------------------------------------------------------

  Read character from Serial Port   (blocking read)

 *----------------------------------------------------------------------------*/

int getkey (void) {

  while (!(USARTx->SR & USART_FLAG_RXNE));

  return ((int)(USARTx->DR & 0x1FF));

}

The function getkey used at getline handles interruption

And I solved the problem putting delay in other tasks:

os_dly_wait (50);            /

* wait for timeout: 50 ticks  */

I don't know if it's the better solution, but solved.

Below the code with buffer like recommended by the Clive1

/*----------------------------------------------------------------------------

 *        Task 2 Receive USART2

 *---------------------------------------------------------------------------*/

__task void receive (void) {

  int i=0;

  int resp=1;

  char letra=0x00;

  while (1) {

       letra=(getkey());    // Salva Caractere Recebido USART2

 

       if (letra==0x02)    // Se Receber Caracter Inicial (0x02)

      {

             count=0;         // Inicia Contador do Buffer

             fim_msg=6;     // Limite de Caracteres da Mensagem

      }

       else if (letra==0x03)   // Se Receber Caracter Final (0x03)

      {

             printf (''%c'', letra);  // Imprime Caractere

             buffer[count]=letra;  // Salva Caraceter na posição [atual] do Buffer

             fim_msg=0;     // Determina o Fim da Mensagem

      }

      if (fim_msg>0)     // Enquanto Maior que o Limite de Caracteres do Buffer

     {

           printf (''%c'', letra);    // Imprime Caractere

           buffer[count]=letra;   // Salva Caraceter na posição [atual] do Buffer

           count++;                  // Incrementa Contador do Buffer

           fim_msg--;               // Decrementa um Caractere da Mensagem

     }

 

      if (fim_msg==0)     // Se Chegou ao Fim da Mensagem

     {

            resp=strcmp(buffer, cmd);   // Compara com comando[S]

            if (resp==0)                        // Se comparacao OK

                     printf (''Igual'');          // Devolve Mensagem

           for (i=0;i<6;i++)

          {

                  buffer[i]=0x00;   // Limpa Buffer de Recepção

          }

     }

 

  }//end while

}

Sorry for the comments in portuguese.