TI中文支持网
TI专业的中文技术问题搜集分享网站

LAUNCHXL-CC1350: RF和UART交互使用遇到問題

Part Number:LAUNCHXL-CC1350Other Parts Discussed in Thread:SYSBIOS, CC1350

您好,請問我們正在整合RX和UART,主要是讓兩個裝置能UART用無線相互傳送顯示(uart輸入資料儲存於packet[2],並轉換成INT再TX,RX則是在callback_packet[2]中 ),目前配合smartRF stdio 測試TX應該沒有問題
但RX遇到以下問題:

1.測試傳送"123456789",如果在RX接收成功時執行UART_write第一個字大部分的時候都無法接收(我在RX callbaclk內有printf進行測試)。

2.目前使用RX endtime來關閉RX,因為先使用RX但如果需要TX時,無法將目前正在執行的RX關閉,需要等收到RX訊號後才會TX,不知道目前這個timeout關閉RX的方式是否正確或是標準

程式基本上都是參照RfpacketRX進行修改的,程式碼如下(這是在mainThread內使用task):

/***** Includes *****/
/* Standard C Libraries */
#include <stdlib.h>

/* TI Drivers */
#include <ti/drivers/rf/RF.h>
#include <ti/drivers/PIN.h>
#include <ti/drivers/UART.h>
#include <ti/sysbios/knl/Task.h>

/* Driverlib Header files */
#include DeviceFamily_constructPath(driverlib/rf_prop_mailbox.h)

/* Board Header files */
#include "Board.h"

/* Application Header files */
#include "RFQueue.h"
#include "smartrf_settings/smartrf_settings.h"

/***** Defines *****/

/* Packet RX Configuration */
#define DATA_ENTRY_HEADER_SIZE 8 /* Constant header size of a Generic Data Entry */
#define MAX_LENGTH 30 /* Max length byte the radio will accept */
#define NUM_DATA_ENTRIES 2 /* NOTE: Only two data entries supported at the moment */
#define NUM_APPENDED_BYTES 2 /* The Data Entries data field will contain:
* 1 Header byte (RF_cmdPropRx.rxConf.bIncludeHdr = 0x1)
* Max 30 payload bytes
* 1 status byte (RF_cmdPropRx.rxConf.bAppendStatus = 0x1) */

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

/***** Variable declarations *****/
static RF_Object rfObject;
static RF_Handle rfHandle;

/* Pin driver handle */
static PIN_Handle ledPinHandle;
static PIN_State ledPinState;

/* Buffer which contains all Data Entries for receiving data.
* Pragmas are needed to make sure this buffer is 4 byte aligned (requirement from the RF Core) */
#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

/* Receive dataQueue for RF Core to fill in data */
/*RX*/
static dataQueue_t dataQueue;
static rfc_dataEntryGeneral_t* currentDataEntry;
static uint8_t packetLength;
static uint8_t* packetDataPointer;
static uint8_t callback_packet[MAX_LENGTH + NUM_APPENDED_BYTES – 1]; /* The length byte is stored in a separate variable */
#define timeout (uint32_t)(4000000*0.1f)
/*RX*/

/*RX Task*/
static Task_Struct rxTask;
#define RX_TASK_STACK_SIZE 1024
static uint8_t rxTaskStack[RX_TASK_STACK_SIZE];
/*RX Task*/

/*TX*/
#define PAYLOAD_LENGTH 30
static uint8_t packet[PAYLOAD_LENGTH];
static uint16_t seqNumber;
int tx_sending = 0;
static uint8_t send_packet[MAX_LENGTH + NUM_APPENDED_BYTES – 1];
/*TX*/

/*TX Task*/
static Task_Struct txTask;
#define TX_TASK_STACK_SIZE 1024
static uint8_t txTaskStack[TX_TASK_STACK_SIZE];
/*TX Task*/

/*Uart*/
UART_Handle uart;
UART_Params uartParams;
char input,output;
/*Uart*/

/*
* Application LED pin configuration table:
* – All LEDs board LEDs are off.
*/
PIN_Config pinTable[] =
{
         Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
         Board_PIN_LED2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
         PIN_TERMINATE
};

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

void led_init()
{
         puts("led_init start");
         ledPinHandle = PIN_open(&ledPinState, pinTable);

         PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
         usleep(300000);
         PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));

         PIN_setOutputValue(ledPinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
         usleep(300000);
         PIN_setOutputValue(ledPinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));

         if (ledPinHandle == NULL)
         {
                  while(1);
         }
}

void rfhandle_init()
{
         puts("rfhandle_init");
         RF_Params rfParams;
         RF_Params_init(&rfParams);

         if( RFQueue_defineQueue(&dataQueue,rxDataEntryBuffer,sizeof(rxDataEntryBuffer),NUM_DATA_ENTRIES,MAX_LENGTH + NUM_APPENDED_BYTES))
         {
                  /* Failed to allocate space for all data entries */
                  while(1);
         }

         rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);

         int i = 0;

         while(i<10)
         {
                  PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
                  usleep(100000);
                  i++;
         }

         PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,0);
}

void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e)
{
         if (e & RF_EventRxEntryDone)
         {
                  /* Toggle pin to indicate RX */
                  PIN_setOutputValue(ledPinHandle, Board_PIN_LED2, 1);

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

                  /* Handle the packet data, located at &currentDataEntry->data:
                  * – Length is the first byte with the current configuration
                  * – Data starts from the second byte */
                  packetLength = *(uint8_t*)(&currentDataEntry->data);
                  packetDataPointer = (uint8_t*)(&currentDataEntry->data + 1);

                  /* Copy the payload + the status byte to the packet variable */
                  memcpy(callback_packet, packetDataPointer, (packetLength + 1));
                  printf("%d",callback_packet[2]);
                  RFQueue_nextEntry();
         }
}

void rx_init()
{
         puts("rx_init");
         /* Modify CMD_PROP_RX command for application needs */
         /* Set the Data Entity queue for received data */
         RF_cmdPropRx.pQueue = &dataQueue;
         /* Discard ignored packets from Rx queue */
         RF_cmdPropRx.rxConf.bAutoFlushIgnored = 1;
         /* Discard packets with CRC error from Rx queue */
         RF_cmdPropRx.rxConf.bAutoFlushCrcErr = 1;
         /* Implement packet length filtering to avoid PROP_ERROR_RXBUF */
         RF_cmdPropRx.maxPktLen = MAX_LENGTH;
         RF_cmdPropRx.pktConf.bRepeatOk = 0;
         RF_cmdPropRx.endTrigger.triggerType = TRIG_REL_START;
         RF_cmdPropRx.endTime = timeout;

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

         while(1)
         {
                  if(tx_sending == 0)
                  {
                           /* Enter RX mode and stay forever in RX */
                           RF_EventMask terminationReason = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropRx,RF_PriorityNormal, &callback,RF_EventRxEntryDone);

                           switch(terminationReason)
                           {
                                    case RF_EventLastCmdDone:
                                             // A stand-alone radio operation command or the last radio
                                             // operation command in a chain finished.
                                             //puts("RF_EventLastCmdDone");
                                             break;
                                    default:
                                             // Uncaught error event
                                             puts("error1");
                                             while(1);
                           }

                           uint32_t cmdStatus = ((volatile RF_Op*)&RF_cmdPropRx)->status;
                           switch(cmdStatus)
                           {
                                    case PROP_DONE_OK:
                                             // Packet received with CRC OK
                                             //puts("PROP_DONE_OK");
                                             PIN_setOutputValue(ledPinHandle, Board_PIN_LED2, 0);

                                             output = callback_packet[2];

                                             UART_write(uart,&output,1);
                                             break;
                                    default:
                                             // Uncaught error event – these could come from the
                                             // pool of states defined in rf_mailbox.h
                                             break;
                           }
                  }
         }
}

void uart_init()
{
         UART_init();

         UART_Params_init(&uartParams);
         uartParams.writeDataMode = UART_DATA_BINARY;
         uartParams.readDataMode = UART_DATA_BINARY;
         uartParams.readReturnMode = UART_RETURN_FULL;
         uartParams.readEcho = UART_ECHO_OFF;
         uartParams.baudRate = 9600;

         uart = UART_open(Board_UART0, &uartParams);

         if (uart == NULL)
         {
                  /* UART_open() failed */
                  while (1);
         }
}

void rx_task_init()
{
         puts("rx_task_init");

         Task_Params rxTaskParams;

         // TI-RTOS 用來初始化 Task_Params 的API ,這邊會給部分變數預設值
         Task_Params_init(&rxTaskParams);
         // 有些變數沒有預設值,必須要自行設定
         rxTaskParams.stack = rxTaskStack; // 規劃給任務使用的Stack位址
         rxTaskParams.stackSize = RX_TASK_STACK_SIZE;
         rxTaskParams.priority = 2; // 任務的優先級,數字越大代表該任務越重要

         Task_construct(&rxTask, rx_init, &rxTaskParams, NULL);
}

void task_init()
{
         puts("tx_init");

         RF_cmdPropTx.pktLen = PAYLOAD_LENGTH;
         RF_cmdPropTx.pPkt = packet;
         RF_cmdPropTx.startTrigger.triggerType = TRIG_NOW;

         while(1)
         {
                  UART_read(uart,&input,1);

                  tx_sending = 1;

                  PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,1);

                  RF_yield(rfHandle);

                  packet[0] = (uint8_t)(seqNumber >> 8);
                  packet[1] = (uint8_t)(seqNumber++);
                  uint8_t i;
                  for (i = 2; i < MAX_LENGTH; i++)
                  {
                           packet[i] = rand();
                  }

                  packet[2] = input;

                  RF_EventMask TXterminationReason = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropTx, RF_PriorityNormal, NULL, 0);

                  switch(TXterminationReason)
                  {
                           case RF_EventLastCmdDone:
                                    //puts("TX RF_EventLastCmdDone");
                                    break;
                           default:
                                    // Uncaught error event
                                    //puts("TX error1");
                                    while(1);
                  }

                  uint32_t TXcmdStatus = ((volatile RF_Op*)&RF_cmdPropTx)->status;
                  switch(TXcmdStatus)
                  {
                           case PROP_DONE_OK:
                                    //puts("TX PROP_DONE_OK");
                                    PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
                                    break;
                           default:
                                    //puts("TX error2");
                                    while(1);
                  }

                  tx_sending = 0;

                  PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,0);
         }
}

void tx_task_init()
{
         puts("tx_task_init");

         Task_Params txTaskParams;

         // TI-RTOS 用來初始化 Task_Params 的API ,這邊會給部分變數預設值
         Task_Params_init(&txTaskParams);
         // 有些變數沒有預設值,必須要自行設定
         txTaskParams.stack = txTaskStack; // 規劃給任務使用的Stack位址
         txTaskParams.stackSize = TX_TASK_STACK_SIZE;
         txTaskParams.priority = 3; // 任務的優先級,數字越大代表該任務越重要

         Task_construct(&txTask, task_init, &txTaskParams, NULL);
}

void *mainThread(void *arg0)
{
         while(ledPinHandle == NULL)
         {
                  led_init();
         }

         while(rfHandle == NULL)
         {
                  rfhandle_init();
         }

         while(uart == NULL)
         {
                  uart_init();
         }

         rx_task_init();

         tx_task_init();
}

Nick Sun:

您好,

收到您的问题了,我们升级到英文论坛给工程师看下,有答复尽快给到您。

,

Yi Shen:

好的 謝謝您

,

Nick Sun:

您好,

callback实际上是中断处理。 printf 需要 2-3 毫秒,并且会在print时阻塞,不应在callback中使用。

您可以参阅以下示例: https://www.ti2k.com/wp-content/uploads/ti2k/DeyiSupport_1GHz_node

这个可以移植到CC1350或者您可以了解如何做 RF + UART。

,

Yi Shen:

您好 Nick,rfUARTBridge這個實例非常有幫助,但是請問CC1350是否可以使用UART2,若可以請問如何import,若無法請問用UART是否可以達到一樣的功能我參考 這個連結 似乎UART2修改都是與RF相關的: https://software-dl.ti.com/simplelink/esd/simplelink_cc13xx_cc26xx_sdk/6.10.00.29/exports/docs/ble5stack/ble_user_guide/html/cc13xx_cc26xx/uart_to_uart2.html

,

Yi Shen:

您好 Nick,剛剛測試您提供的範例,有一個小問題,因為bytesToRead設定為64,所以一定要在uart read64個字之後才進入uart的callback function中,請問是否可以根據我輸入的字數直接讀取呢?EX : 我輸入ABC就只讀取3個字並傳送(TX)。可以的話是否能提供簡單的範例程式,非常謝謝

UART2_read(uart, &input, bytesToRead, NULL);

,

Nick Sun:

您好,

CC1350应该是沒有UART2,另外一个帖子中YIKAI也回复您了

,

Yi Shen:

您好,

我也發現這點了,所以我用uart來做上面這個範例,另外我將程式改為uart_read(uart,&input,1),若傳送的資訊不多的話,傳送沒有問題,但是否我可以直接根據輸入的字數進行讀取,例如直接讀取記憶體內資料?我希望能輸入123456789就讀9個字,如果可以的話,也請您提供我一個範例,就如上面的問題。

,

Nick Sun:

您好,

你有尝试去修改bytesToRead的值吗?

赞(0)
未经允许不得转载:TI中文支持网 » LAUNCHXL-CC1350: RF和UART交互使用遇到問題
分享到: 更多 (0)

© 2024 TI中文支持网   网站地图 鲁ICP备2022002796号-1