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.

LP-EM-CC1354P10: Adding UART interruption to Wi-SUN CoAP project

Part Number: LP-EM-CC1354P10
Other Parts Discussed in Thread: SYSCONFIG

Tool/software:

Hi,

I am using ns_coap_node example and I'd like to implement an UART interruption so the node can process some data received and send it to border router using CoAP. I have tried implementing modifying the original code with the following steps:

Added UART peripheral in Sysconfig:

Implemented callback function in application.c:

Initialized UART in mainThread in application.c:

When I debug the code, while there is nothing received in the UART peripheral it keeps running as expected. When I write something to UART and then suspend the running project, I can see that it is stuck in this while(1) loop:

Is this what it was supposed to happen? I thought that it would go to the UART callback function and then keep running as usual.

Regards,
Eduardo.

  • Hi Eduardo,

    you cannot read or write in blocking mode in a callback.
    You can review the mode in the UART2.h file:

    If you change the mode to "uartParams.writeMode = UART2_Mode_NONBLOCKING;" your code works fne.

    Kind regards,
    Theo

  • Hi Theo,

    I changed the writeMode to NONBLOCKING and enabled Non Blocking Mode in Syscfg. I also changed the code a little bit and I'm not using Callback anymore to do some other tests. Now, I am trying to write to UART in the function that is called when the button is pressed. Here is what I'm doing:

    UART2_Handle uart_esp;
    UART2_Params uartParams;
    size_t bytesWritten = 0;

    I am using a logic analyzer and it seems like just part of the message is beeing sent:

    Some characters are missing. I tried switching to blocking mode, it prints to UART up to 16 bytes instead of 11 but still doesn't write the whole message.

    Is there any configuration that I'm missing and is causing this error?

    Regards,
    Eduardo.

  • Hi Eduardo,

    the issue that you experience is do to closing the UART right after writing. You need to ensure that it was enough time to send all the bytes by using a usleep(). UART2_write only sets the bytes in the registers and following the UART hardware transmits it. When you call UART2_close() it will shut down the UART hardware. So calling it immediately after setting the values in the registers will not leave enough time for the hardware to transmit all the data.

    As I could not replicate your code from the snippets I made a small example attached below:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /*
    * Copyright (c) 2015-2019, Texas Instruments Incorporated
    * All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    *
    * * Redistributions of source code must retain the above copyright
    * notice, this list of conditions and the following disclaimer.
    *
    * * Redistributions in binary form must reproduce the above copyright
    * notice, this list of conditions and the following disclaimer in the
    * documentation and/or other materials provided with the distribution.
    *
    * * Neither the name of Texas Instruments Incorporated nor the names of
    * its contributors may be used to endorse or promote products derived
    * from this software without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


    If you replace your application.c file with it and initialize a UART as shown below in sysconfig you will print a welcome message on the UART and every time the button that you press.

    Kind regards,
    Theo

  • Hi Theo,

    Thank you, setting a delay do solves this issue. Now I am able to successfully write a whole message to UART when I press the button.

    I was testing again the UART callback function and I'd like to know if there is any recommendation on caring about the size of the UART callback function. My intention is to use the callback function to parse the message received via UART and execute some functions based on what is received.

    Do you see any problem on doing this?

    Best regards,
    Eduardo.

  • Hi Eduardo,

    there is no problem in doing this. 

    A general recommendation is to keep the processing as short as possible and you could you create a separate UART thread that opens the UART and then handles following all processing related to it. This way you can keep the UART permanently open (so you don't need the delay).

    Kind regards,
    Theo

  • Hi Theo,

    Thank you for the support, everything seems to be working fine!

    Best regards,
    Eduardo.

  • Hi Theo,

    Actually I would be grateful if you could help me with one more thing.

    I am using a second Thread to receive UART messages, as you suggested:

    The callback function is as follows:

    I am sending some packets but it looks like I am not receiving it completely.

    As you can see I echo bach to UART the received packet on line 1244 from the callback function. As you can see it seems to be only receiving the first 4 bytes and is missing the rest of the packet.

    What can I do to correctly receive the whole packet?

    Best regards,
    Eduardo.

  • Update:

    I have just found out that switching the UART2_write parameter prints the whole packet:
    UART2_write(handle, data, count, &bytesWritten1) instead of UART2_write(handle, data, sizeof(data), &bytesWritten1).

    Why does this happen?

  • Hi Eduardo,

    I can't spot the definition of count in your snippets. Can you explain me which value is passed for count so that I can understand the difference between count and sizeof(data) ?

    Kind regards,
    Theo

  • Hi Theo,

    Count is the parameter of uartRxCallback function. I suppose it is the size of the message that has been read in UART.

    Just to show you the whole function:

    After I parse the received message I call UART2_read again otherwise I can't receive more than one message and I pass count as the parameter of the receive buffer.

    Best regards,
    Eduardo.

  • Hi Eduardo,

    looking again at your implementation I see that when you used sizeof(data) you where actually taking the size of the pointer = size of the memory address which is 32 bit. This explains why you only received 4 bytes.

    The count parameter instead is the number of received bytes and as you are sending all data out again the number of bytes was matching.

    I would like to give you some general recommendation for your implementation:
    - You should not implement a huge process in the callback especially no sleeps.
    - Best practice you just set a semaphore in the callback and then all processing is made outside of the callback. You can have a look at RTOS scheduling and semaphores for that.

    Kind regards,
    Theo