This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TI-RTOS SYSBIOS TASK

HI   all

   

MCU: TM4C1294NCPDT

TI-RTOS: v2.01.00.36

NDK: v2.23.01.01

CCS: v6.0.1.0040

SYS/BIOS    6.4..02.27  GA  Release  Note

How can we implement this functionality when a task is not being used, it has to sleep, when to use, it was only wake up?

Thanks

Lewis

 

  • Lewis,

    I'm not sure I understand the question. Please provide more details about your application.

    A Semaphore object is often used to have a task block until it has work to do.

    The task calls Semaphore_pend(). Then an interrupt function calls Semaphore_post() that will cause the task to unblock.

    Am I on the right track?

    Alan
  • Alan,

    Void udpHandler(UArg arg0, UArg arg1)

    {

       SOCKET lSocket;

       struct sockaddr_in sLocalAddr;

       struct sockaddr_in client_addr;

       struct timeval to;

       fd_set readfds;

       int addrlen = sizeof(client_addr);

       int status;

       HANDLE hBuffer;

       IPN LocalAddress = 0;

       int nbytes;

       bool flag = true;

       char *buffer;

       fdOpenSession(TaskSelf());

       lSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

       if (lSocket < 0) {

           System_printf("udpHandler: socket failed\n");

           Task_exit();

           return;

       }

       memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));

       sLocalAddr.sin_family = AF_INET;

       sLocalAddr.sin_len = sizeof(sLocalAddr);

       sLocalAddr.sin_addr.s_addr = LocalAddress;

       sLocalAddr.sin_port = htons(arg0);

       status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));

       if (status < 0) {

           System_printf("udpHandler: bind failed: returned: %d, error: %d\n",

                   status, fdError());

           fdClose(lSocket);

           Task_exit();

           return;

       }

       /* Give user time to connect with udpSendReceive client app */

       to.tv_sec  = 30;

       to.tv_usec = 0;

       if (setsockopt( lSocket, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) ) < 0) {

           System_printf("udpHandler: setsockopt SO_SNDTIMEO failed\n");

           fdClose(lSocket);

           Task_exit();

           return;

       }

       if (setsockopt( lSocket, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) ) < 0) {

           System_printf("udpHandler: setsockopt SO_RCVTIMEO failed\n");

           fdClose(lSocket);

           Task_exit();

           return;

       }

       /* Loop while we receive data */

       while (flag) {

           /*

            * Wait for the reply. Timeout after TIMEOUT seconds (assume UDP

            * packet dropped)

            */

           FD_ZERO(&readfds);

           FD_SET(lSocket, &readfds);

           if (fdSelect(0, &readfds, NULL, NULL, NULL) != 1) {

            status = fdError();

               System_printf("timed out waiting for client\n", status);

               continue;

           }

           nbytes = recvncfrom(lSocket, (void **)&buffer, MSG_WAITALL,

                   (struct sockaddr *)&client_addr, &addrlen, &hBuffer);

           if (nbytes >= 0) {

                Semaphore_post(sem2);

                           /* Echo the data back */

               sendto(lSocket, (char *)buffer, nbytes, 0,

                       (struct sockaddr *)&client_addr, sizeof(client_addr));

               recvncfree( hBuffer );

           }

           else {

                Semaphore_pend(sem2, BIOS_WAIT_FOREVER);

                 status = fdError();

                 if (status == EWOULDBLOCK) {

                 System_printf("udpHandler: Waiting for client to send UDP data\n");

                continue;

                 }

               else {

                   System_printf(

                       "udpHandler: recvfrom failed: returned: %d, error: %d\n",

                       nbytes, status);

                       fdClose(lSocket);

         //              flag = false;

                      }

             }

       }

       System_printf("udpHandler stop, lSocket = 0x%x\n", lSocket);

       if (lSocket) {

           fdClose(lSocket);

       }

       fdCloseSession(TaskSelf());

       /*

        *  Since deleteTerminatedTasks is set in the cfg file,

        *  the Task will be deleted when the idle task runs.

        */

       Task_exit();

    }

    I want udphandle this task thread when I use it (that is, when data is received recvfrom), only to wake it to work, so that does not work when the time (ie recvfrom no data is received, that they do not connect to this thread network), has been sleeping on it, how can I change to use semaphore mechanism inside this thread? The code in red font is the code I add, what I want to achieve this function? If this causes the thread does not work, please help me change it!

     

  • Lewis,
    I lost track of this thread over the Christmas holidays. Have you found a way to achieve your objective yet?
    Alan
  • Hi Alan


    I am very happy to receive your reply! I hope you can give me some solution for my problem!


    Thanks

    Lewis

  • I think the call to recvncfrom() will block until data is ready to be processed.
    I do not understand why you need to explicitly add any blocking code to the udpHandler.

    Alan