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.

CC2652RSIP: CC2652RSIP UART

Part Number: CC2652RSIP

Tool/software:

/** CODE**/
/***** Includes *****/
/* Standard C Libraries */
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <ti/drivers/timer.h>
/* TI Drivers */
#include <ti/drivers/rf/RF.h>
#include <ti/drivers/GPIO.h>
/* Driverlib Header files */
#include DeviceFamily_constructPath(driverlib/rf_prop_mailbox.h)
/* Board Header files */
#include "ti_drivers_config.h"
#include "RFQueue.h"
#include <ti_radio_config.h>
#include <ti/drivers/UART2.h>
#include <ti/drivers/I2C.h>
#include "i2c_oled.h"
int tenths_of_second = 10;    // Initial time set to 1.0 seconds
int mode = 0;
volatile int total_time;
int selectedTime = 0;
const char *successMessage = "Time Set Successful";
const char *message = " ";
int state = 0;
int timeSetSuccess =0;
static uint8_t fuseTime[10];
int flag=1;
// Button states
int buttonK1Pressed = 0;
int buttonK2Pressed = 0;
int buttonK3Pressed = 0;
int buttonK4Pressed = 0;

/* Driver configur
/***** Defines *****/

/* Packet RX Configuration */
#define DATA_ENTRY_HEADER_SIZE 8
#define MAX_LENGTH             30
//#define MAX_LENGTH             10
#define NUM_DATA_ENTRIES       2
#define NUM_APPENDED_BYTES     2

/* Packet TX Configuration */
#define PAYLOAD_LENGTH         10
#define PACKET_INTERVAL        500000

/***** Prototypes *****/
static void rxCallback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e);

/***** Variable declarations *****/
static RF_Object rfObject;
static RF_Handle rfHandle;
static RF_CmdHandle rfPostHandle;
static uint8_t engy_har[MAX_LENGTH + NUM_APPENDED_BYTES - 1];

/* RX Data Entry Buffer */
#if defined(__TI_COMPILER_VERSION__)
#pragma DATA_ALIGN(rxDataEntryBuffer, 4);
static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES, MAX_LENGTH, NUM_APPENDED_BYTES)];
#elif defined(__IAR_SYSTEMS_ICC__)
#pragma data_alignment = 4
static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES, MAX_LENGTH, NUM_APPENDED_BYTES)];
#elif defined(__GNUC__)
static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES, MAX_LENGTH, NUM_APPENDED_BYTES)] __attribute__((aligned(4)));
#else
#error This compiler is not supported.
#endif

/* Data queue for RX */
static dataQueue_t dataQueue;
static rfc_dataEntryGeneral_t* currentDataEntry;
static uint8_t packetLength;
static uint8_t* packetDataPointer;

/* RX and TX Packets */
static uint8_t rxPacket[MAX_LENGTH + NUM_APPENDED_BYTES - 1];
static uint8_t txPacket[10]; // Make txPacket large enough to hold the received data
static uint8_t save[MAX_LENGTH + NUM_APPENDED_BYTES - 1];
//static uint8_t output[MAX_LENGTH + NUM_APPENDED_BYTES - 1];
static uint8_t output[4];
static uint8_t input[10];
static uint8_t num[10];

/***** Function definitions *****/

void uart();
void fcs_tx();
void fcs_rx();
void updateDisplay();
void button();
void fcs();


//________________________________________________________________
void updateDisplay()
{
    char buffer[21] = {0};

    // Display mode on line1
    if (mode == 0)
    {
        snprintf(buffer, 21, "Mode: Seconds       "); // Padded to 20 characters
    }
    else if (mode == 1)
    {
        snprintf(buffer, 21, "Mode: Tenths        ");
    }
    oled20x04_SetPosition(0, 0);
    displayText(buffer, 1);

    // Display set time on line2
    snprintf(buffer, 21, "Set time: %02d:%d ss", total_time / 10, total_time % 10);
    oled20x04_SetPosition(1, 0);
    displayText(buffer, 2);

    if((timeSetSuccess)== 0)
    {
    // Display success message on line3
    oled20x04_SetPosition(3, 0);
    displayText("buttons pressed", 3);
    }
    else if((timeSetSuccess) == 1)
    {
    oled20x04_SetPosition(2, 0);
    displayText(successMessage, 3);

    snprintf(buffer,21,"Selected: %02d:%d ss", selectedTime / 10, selectedTime % 10);
    oled20x04_SetPosition(3,0);
    displayText(buffer,4);
    }
}


//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void *mainThread(void *arg0)
{

I2C_init();
GPIO_init();
/* Initialize UART parameters */

  UART2_Params uartParams;

  UART2_Params_init(&uartParams);

  UART2_Handle  rs232Uart;
  uartParams.baudRate = 115200;
  rs232Uart = UART2_open(CONFIG_UART2_1, &uartParams);

UART2_write( rs232Uart, "\r\n UART opened successfully \r\n", strlen(" UART opened successfully \r\n"), NULL);

RF_Params rfParams;

RF_Params_init(&rfParams);

I2C_Handle i2c;

I2C_Params i2cParams;



/* Configure LEDs */
GPIO_setConfig(CONFIG_GPIO_RLED, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
GPIO_setConfig(CONFIG_GPIO_GLED, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
GPIO_write(CONFIG_GPIO_RLED, CONFIG_GPIO_LED_OFF);
GPIO_write(CONFIG_GPIO_GLED, CONFIG_GPIO_LED_OFF);

GPIO_setConfig(CONFIG_GPIO_K1, GPIO_CFG_IN_PD);
GPIO_setConfig(CONFIG_GPIO_K2, GPIO_CFG_IN_PD);
GPIO_setConfig(CONFIG_GPIO_K3, GPIO_CFG_IN_PD);
GPIO_setConfig(CONFIG_GPIO_K4, GPIO_CFG_IN_PD);


GPIO_setConfig(CONFIG_GPIO_Reset_Pin, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
  GPIO_write(CONFIG_GPIO_Reset_Pin, 0);
  usleep(10000);
  GPIO_write(CONFIG_GPIO_Reset_Pin, 1);
  usleep(10000);

  // Initialize I2C and OLED
  initI2C();
  initOLED();
  clearscreen();
  updateDisplay();


  // Check button states

  /* Initialize RX queue */
      if (RFQueue_defineQueue(&dataQueue, rxDataEntryBuffer, sizeof(rxDataEntryBuffer), NUM_DATA_ENTRIES, MAX_LENGTH + NUM_APPENDED_BYTES)) {
          while (1); // Allocation failed
      }

      /* Modify RX command */
      RF_cmdPropRx.pQueue = &dataQueue;
      RF_cmdPropRx.rxConf.bAutoFlushIgnored = 1;
      RF_cmdPropRx.rxConf.bAutoFlushCrcErr = 1;
      RF_cmdPropRx.maxPktLen = MAX_LENGTH;

      /* Set up TX command */
     // RF_cmdPropTx.pktLen = PAYLOAD_LENGTH;
      RF_cmdPropTx.pktLen = 6;
      RF_cmdPropTx.pPkt = txPacket;
      RF_cmdPropTx.startTrigger.triggerType = TRIG_NOW;

      /* Request access to the radio */
  #if defined(DeviceFamily_CC26X0R2)
      rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioSetup, &rfParams);
  #else
      rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
  #endif

      /* Set the frequency */
      RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);




    while (1) {


        UART2_read(rs232Uart, input, sizeof(input), NULL);


        switch(input[1])
        {

        case 0x0A:

            UART2_write( rs232Uart, "\r\n fcs open \r\n", strlen(" fcs open \r\n"), NULL);
            UART2_read(rs232Uart, input, sizeof(input), NULL);
                 for(int c=2;c<=2;c++)
                 {
                 txPacket[2]=0xf0;
                 txPacket[3]=0x02;
                 txPacket[4]= input[c];
                 txPacket[5]=0xfe;

                 }
                 RF_EventMask   txTermination = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropTx, RF_PriorityNormal, NULL, 1);

                 if (txTermination & RF_EventLastCmdDone)
                 {
                 GPIO_toggle(CONFIG_GPIO_GLED); // Indicate successful TX
                 // UART2_write( rs232Uart, " \r\nTX DATA successfully \r\n", strlen(" TX DATA successfully \r\n"), NULL);
                 }
                 /* Start RX */

                 RF_EventMask rxTermination = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropRx, RF_PriorityNormal, &rxCallback, RF_EventRxEntryDone);
                  /* Handle RX termination reasons */

                  if(( rxTermination & RF_EventCmdAborted ) && ( rxTermination & RF_EventCmdPreempted ))
                  {

                      GPIO_toggle(CONFIG_GPIO_RLED); // Indicate successful TX
                  } else {
                         }
                 
                    for(int p=0;p<MAX_LENGTH+ NUM_APPENDED_BYTES - 1;p++)
                    {
                    if(rxPacket[p]==0XF0)
                    {
                    for(int q=0;q<=50;q++)
                    {
                       
                        if(rxPacket[q]==0x03)
                        {
                            GPIO_toggle(CONFIG_GPIO_RLED); // Indicate RX

                           output[0]=0xf0;
                            output[1]=0x04;
                            output[2]=rxPacket[4];
                            output[3]=0xfe;
                            UART2_write( rs232Uart, &output,sizeof(output), NULL);

                        }
                    }
                    }
                    }


                   UART2_write( rs232Uart, "\r\n fcs close \r\n", strlen(" fcs close \r\n"), NULL);

            break;

        case 0x0B:


            if(buttonK4Pressed== 0)
                 {
           UART2_write( rs232Uart, "\r\n button open \r\n", strlen(" button open \r\n"), NULL);

            button(rs232Uart);

         
               }


                    break;
        case 0x0C:

    

            if(input[0]==0xF0)
            {
                

                              UART2_write( rs232Uart, "\r\n UART opened successfully for EH check \r\n", strlen(" UART opened successfully for EH check  \r\n"), NULL);
                              /* Start RX */

                                              RF_EventMask rxTermination = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropRx, RF_PriorityNormal, &rxCallback, RF_EventRxEntryDone);
                                               /* Handle RX termination reasons */

                                               if(( rxTermination & RF_EventCmdAborted ) && ( rxTermination & RF_EventCmdPreempted ))
                                               {

                                                   GPIO_toggle(CONFIG_GPIO_RLED); // Indicate successful TX
                                               } else {
                                                   // Handle other termination reasons as needed
                                               }
                                               /* Sleep between packets */
                                                           /*  usleep(PACKET_INTERVAL);*/

                                                         
                                                             for(int p=0;p<MAX_LENGTH+ NUM_APPENDED_BYTES - 1;p++)
                                                             {
                                                             if(rxPacket[p]==0XF0)
                                                             {
                                                            UART2_write( rs232Uart,&rxPacket , sizeof(rxPacket), NULL);
                                                             for(int q=0;q<=50;q++)
                                                             {
                                                                 
                                                                 if(rxPacket[q]==0x03)
                                                                 {
                                                                     GPIO_toggle(CONFIG_GPIO_RLED); // Indicate RX


                                                                    output[0]=0xf0;
                                                                     output[1]=0x04;
                                                                     output[2]=rxPacket[4];
                                                                     output[3]=0xfe;
                                                                     UART2_write( rs232Uart, &output,sizeof(output), NULL);

                                                                 }
                                                             }
                                                             }
                                                             }

                        }// for l end





                    break;
        default:
            UART2_read(rs232Uart, input, sizeof(input), NULL);
            break;

        }




    }
    I2C_close(i2c);

}

void rxCallback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e)
{
    if (e & RF_EventRxEntryDone) {
        GPIO_toggle(CONFIG_GPIO_RLED); // Indicate RX

        /* Get current unhandled data entry */
        currentDataEntry = RFQueue_getDataEntry();

        /* Extract packet data */
        packetLength = *(uint8_t*)(&currentDataEntry->data);
        packetDataPointer = (uint8_t*)(&currentDataEntry->data + 1);
        memcpy(rxPacket, packetDataPointer, packetLength);


        RFQueue_nextEntry();
       // usleep(5000);
    }
}



//----------------------------------------------------------------------------------------------------------------------------------------------------------
 void button(UART2_Handle  rs232Uart )
 {




       // while( buttonK4Pressed = GPIO_read(CONFIG_GPIO_K4)== 0)
               while(1)
        {

          buttonK1Pressed = GPIO_read(CONFIG_GPIO_K1)== 0;

          buttonK2Pressed = GPIO_read(CONFIG_GPIO_K2)== 0;

          buttonK3Pressed = GPIO_read(CONFIG_GPIO_K3)== 0;

          buttonK4Pressed = GPIO_read(CONFIG_GPIO_K4)== 0;

             if (buttonK4Pressed == 0 )
             {
             // usleep(1500);
              usleep(1500);
              if(GPIO_read(CONFIG_GPIO_K4) == 0)
              {
                 GPIO_write(CONFIG_GPIO_Signal4,1);
                 mode = (mode + 1) % 2;  // Cycle through modes (0 to 1)
                 updateDisplay();
                 GPIO_write(CONFIG_GPIO_Signal4,0);
              // Debounce delay



              }
              //UART2_write( rs232Uart, "\r\n button code 1 \r\n", strlen(" button code 1 \r\n"), NULL);

             }

             if (buttonK1Pressed == 0)
             {//increment
                 usleep(1500);
                 //usleep(2500);
                 if(GPIO_read(CONFIG_GPIO_K1) == 0)
                 {
                 GPIO_write(CONFIG_GPIO_Signal1,1);
                 int increment = (mode == 0) ? 10 : 1;  // Seconds or tenths of a second
                 total_time += increment;
                 if (total_time > 200) total_time = 200;
                 // Max 20 seconds
                 timeSetSuccess =0;
                 updateDisplay();
                // UART2_write( rs232Uart, "\r\n button code 2 \r\n", strlen(" button code 2 \r\n"), NULL);

                 }  //usleep(1000); // Debounce delay
             }

             if (buttonK2Pressed == 0 )
             { //decrement
                 usleep(1500);
                // usleep(2500);
                 if(GPIO_read(CONFIG_GPIO_K2) == 0)
                 {
                 GPIO_write(CONFIG_GPIO_Signal2,1);
                 int decrement = (mode == 0) ? 10 : 1;  // Seconds or tenths of a second
                 total_time -= decrement;
                 if (total_time < 1) total_time = 1;  // Min 0.1 seconds
                 timeSetSuccess =0;
                 updateDisplay();
                 GPIO_write(CONFIG_GPIO_Signal2,0);
                 }// Debounce delay
                // UART2_write( rs232Uart, "\r\n button code 3 \r\n", strlen(" button code 3 \r\n"), NULL);

             }

             if (buttonK3Pressed == 0)
             { //set fuse time
                 // Display success message
                // usleep(1500);
                 usleep(1500);
                 if(GPIO_read(CONFIG_GPIO_K3) == 0)
                 {
                         timeSetSuccess =1;
                         //oled20x04_SetPosition(4,0);
                         selectedTime=total_time;
                         GPIO_write(CONFIG_GPIO_Signal3,1);
                         updateDisplay();
                         GPIO_write(CONFIG_GPIO_Signal3,0);
                         //fuseTime[1] = selectedTime*10;
                         fuseTime[3] = selectedTime;
                         //UART2_write( rs232Uart, &fuseTime,sizeof(fuseTime) , NULL);

                         for(int c=2;c<=2;c++)
                             {
                             txPacket[2]=0xf0;
                             txPacket[3]=0x02;
                             txPacket[4]=fuseTime[3] ;
                             txPacket[5]=0xfe;
                             //UART2_write( rs232Uart, "\r\n WATING FOR RX DATA  \r\n", strlen("WATING FOR RX DATA   \r\n"), NULL);
                             }
                             /* Transmit packet */

                           RF_EventMask txTermination = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropTx, RF_PriorityNormal, NULL, 1);

                           if (txTermination & RF_EventLastCmdDone)
                           {
                               GPIO_toggle(CONFIG_GPIO_GLED); // Indicate successful TX

                           }
                           RF_EventMask rxTermination = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropRx, RF_PriorityNormal, &rxCallback, RF_EventRxEntryDone);
                                                                     /* Handle RX termination reasons */

                                                                     if(( rxTermination & RF_EventCmdAborted ) && ( rxTermination & RF_EventCmdPreempted ))
                                                                     {

                                                                         GPIO_toggle(CONFIG_GPIO_RLED); // Indicate successful TX
                                                                     } else {
                                                                         // Handle other termination reasons as needed
                                                                     }
                                                                     /* Sleep between packets */
                                                                                 /*  usleep(PACKET_INTERVAL);*/

                                                                                 // UART2_write( rs232Uart, "\r\n WATING FOR RX DATA  \r\n", strlen("WATING FOR RX DATA   \r\n"), NULL);
                                                                                   for(int p=0;p<MAX_LENGTH+ NUM_APPENDED_BYTES - 1;p++)
                                                                                   {
                                                                                   if(rxPacket[p]==0XF0)
                                                                                   {
                                                                                 // UART2_write( rs232Uart,&rxPacket , sizeof(rxPacket), NULL);
                                                                                   for(int q=0;q<=50;q++)
                                                                                   {
                                                                                       //save[q]=rxPacket[p];
                                                                                       //UART2_write( rs232Uart, "\r\nsave data\r\n", strlen("save data\r\n"), NULL);

                                                                                       //UART2_write( rs232Uart,&save , sizeof(save), NULL);
                                                                                       //p++;
                                                                                      // if(rxPacket[q]==0x03)
                                                                                       if(rxPacket[q]==0x03)
                                                                                       {
                                                                                           GPIO_toggle(CONFIG_GPIO_RLED); // Indicate RX


                                                                                          output[0]=0xf0;
                                                                                           output[1]=0x04;
                                                                                           output[2]=rxPacket[4];
                                                                                           output[3]=0xfe;
                                                                                          UART2_write( rs232Uart, &output,sizeof(output), NULL);

                                                                                       }
                                                                                   }
                                                                                   }
                                                                                //   UART2_write( rs232Uart, "\r\n button 4 \r\n", strlen(" button 4 \r\n"), NULL);
                                                                                   //

                                                                                   }

                                                                                   UART2_write( rs232Uart, "\r\n button close \r\n", strlen(" button close \r\n"), NULL);
                                                                                  /* while(1)
                                                                                   {
                                                                                   fcs(rs232Uart);


                                                                                   }*/
                                                                                       //   UART2_write( rs232Uart, "\r\n NOT WORKING \r\n", strlen(" NOT WORKING \r\n"), NULL);


                                                       // UART2_write( rs232Uart, &output,sizeof(output), NULL);


                                                                                  // UART2_read(rs232Uart, input, sizeof(input), NULL);
                             //  }  buttonK1Pressed = GPIO_read(CONFIG_GPIO_K1)== 0;

                                                            // goto all;



                                        //}


                        }


                 }

             }


       }// while end


Here i am using  uart function and button function 

first I am reading a data from Uart base on that i do switch case 

case 1 :0x0a is uart read data 

case 2: 0x0b is button data 

case 3: 0x0c is uart write data 

my question is case is 0x0b is working when i switch to case 0x0a  then uart is not reading any data it seems that gpio is only in read mode  . Plese hepl me in this condition 

  • Hi Kumaravel,

    You will need to provide greater detail as to your project's behavior and Debugging efforts that have been performed thus far.

    There is a UART2_read before and during the 0x0A case based on the value of input[1].  As UART2 is in blocking mode by default then UART2_read will wait for 10 bytes before 0x0A and then 10 bytes after entering the case, or 20 bytes in total.  There is no timeout or cancel functionality built into the current code provided.  Please further investigate the UART2 documentation and ensure that the expected number of bytes are being transferred to the device, or that you add in fail safes in case the incoming UART data is faulty or incomplete.

    Regards,
    Ryan

  • can you write a code for  uart timeout fuction in my above code . Is ther any problem with my void button funtion as gpio is in read mode with infint loop function  . How to chane  Uart in Non blocking mode and autocall function 

  • Here is the UART2 TI Driver documentation and uart examples.  You will find timeout, non-blocking, etc. references in the documentation.  I also recommend the FreeRTOS Overview from the stack User's Guides.

    If you ever want your application to exit the button function, I'm not sure how this is accomplished since it appears to wrap its operation within a forever while loop, thus your other infinite while loop is never again serviced.

    Regards,
    Ryan