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.

rfEasyLinkEchoRx接收例程不能使用UART吗?

Other Parts Discussed in Thread: SYSBIOS

Hi,大家好,今天在调试EasyLink的接收例程,想把TX发给RX端的数据通过串口发送到上位机。串口的初始化是参考Uartecho例程的,之前在其他例程中也是一直在使用没有问题。但是在EasyLink中使用时,打印出来的是一串错乱的16进制,并不是我想要的数据。我想请问一下,rfEasyLinkEchoRx这个例程与Uart驱动有冲突吗?以下是我的代码。display已经通过宏注释了,现在只有一个uart。请TI工程师帮忙验证一下。

/*
 *  ======== rfEasyLinkEchoRx.c ========
 */
/* Standard C Libraries */
#include <stdlib.h>

/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/Assert.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Clock.h>

/* TI-RTOS Header files */
#include <ti/drivers/PIN.h>

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

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

/* EasyLink API Header files */
#include "easylink/EasyLink.h"
#include "user/message.h"
#include <ti/drivers/NVS.h>
#if DISPLAY_OPEN
#include <ti/display/Display.h>
#endif
#include <ti/drivers/UART.h>

#define RFEASYLINKECHO_TASK_STACK_SIZE    1024
#define RFEASYLINKECHO_TASK_PRIORITY      2

#define RFEASYLINKECHO_PAYLOAD_LENGTH     30

Task_Struct echoTask;    /* not static so you can see in ROV */
static Task_Params echoTaskParams;
static uint8_t echoTaskStack[RFEASYLINKECHO_TASK_STACK_SIZE];

/* Pin driver handle */
static PIN_Handle pinHandle;
static PIN_State pinState;

/*NVS handle*/
NVS_Handle nvsHandle;

/*
 * 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
};

static Semaphore_Handle echoDoneSem;
static bool bBlockTransmit = false;

EasyLink_TxPacket txPacket = {{0}, 0, 0, {0}};
/***************************Handle***************************/
#if UART_OPEN
/*Uart handle*/
UART_Handle uart;
UART_Params uartParams;
#endif
#if DISPLAY_OPEN
Display_Handle uartDisplayHandle;
void Display_Init();
#endif
/**************************variable**************************/
Uart_Req_Data Sensor_Data = {
        .head.Mache = 0x01,
        .head.Func  = 0xA0,
        .Addr       = 0x0600,
        .tail.CRC16 = 0xA090,
        .tail.End   = 0x96,
};
Config_Req Rx_Config = {
        .Frequency = 868000000,
        .Address  = 0xDE,
};
Ack_Rx_Req Rx_Ack = {
        .head.Mache = 0x03,
        .head.Func  = 0x87,
        .report_time = 30000,  //MS
};
Data_Req Rx_Data;
uint8_t  Rx_Buff[5];
uint32_t Frq;
uint8_t Address;
uint16_t CRC;
/************************************************************/

void echoTxDoneCb(EasyLink_Status status)
{
    if (status == EasyLink_Status_Success)
    {
        /* Toggle LED2 to indicate Echo TX, clear LED1 */
        PIN_setOutputValue(pinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
        PIN_setOutputValue(pinHandle, Board_PIN_LED1, 0);
    }
    else
    {
        /* Set LED1 and clear LED2 to indicate error */
        PIN_setOutputValue(pinHandle, Board_PIN_LED1, 1);
        PIN_setOutputValue(pinHandle, Board_PIN_LED2, 0);
    }

    Semaphore_post(echoDoneSem);
}
void echoRxDoneCb(EasyLink_RxPacket * rxPacket, EasyLink_Status status)
{
    if (status == EasyLink_Status_Success)
    {
        /* Toggle LED2 to indicate RX, clear LED1 */
        PIN_setOutputValue(pinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
        PIN_setOutputValue(pinHandle, Board_PIN_LED1, 0);
        /* Permit echo transmission */
        bBlockTransmit = false;
        if(rxPacket->payload[0] == 0x01 && rxPacket->payload[1] == 0xA0)
        {
            memcpy(&Rx_Data,rxPacket->payload,sizeof(Data_Req));
            CRC = crc16((uint8_t *)&Rx_Data,sizeof(Data_Req) - 2);
            if(Rx_Data.Crc16 == CRC)
            {
                memcpy(&Sensor_Data.temp_data,rxPacket->payload+2,14);
                Sensor_Data.temp_data.Rssi_Val = rxPacket->rssi;
#if DISPLAY_OPEN
        Display_printf(uartDisplayHandle, 0, 0, "rssi:%d",rxPacket->rssi);
#endif
#if UART_OPEN
        UART_write(uart, &Sensor_Data, sizeof(Uart_Req_Data));
#endif
            }
        }
    }
    else
    {
        /* Set LED1 and clear LED2 to indicate error */
        PIN_setOutputValue(pinHandle, Board_PIN_LED1, 1);
        PIN_setOutputValue(pinHandle, Board_PIN_LED2, 0);
        /* Block echo transmission */
        bBlockTransmit = true;
    }

    Semaphore_post(echoDoneSem);
}

static void rfEasyLinkEchoRxFnx(UArg arg0, UArg arg1)
{
    /* Create a semaphore for Async */
    Semaphore_Params params;
    Error_Block      eb;

    /* Init params */
    Semaphore_Params_init(&params);
    Error_init(&eb);

    /* Create semaphore instance */
    echoDoneSem = Semaphore_create(0, &params, &eb);
    if(echoDoneSem == NULL)
    {
        System_abort("Semaphore creation failed");
    }

    // Initialize the EasyLink parameters to their default values
    EasyLink_Params easyLink_params;
    EasyLink_Params_init(&easyLink_params);

    /*
     * Initialize EasyLink with the settings found in easylink_config.h
     * Modify EASYLINK_PARAM_CONFIG in easylink_config.h to change the default
     * PHY
     */
    if(EasyLink_init(&easyLink_params) != EasyLink_Status_Success)
    {
        System_abort("EasyLink_init failed");
    }
#if DISPLAY_OPEN
    Display_Init();
#endif
#if UART_OPEN
    Uart_Init();
#endif
    /*
     * If you wish to use a frequency other than the default, use
     * the following API:
     * EasyLink_setFrequency(868000000);
     */
    EasyLink_setFrequency(Frq);
    while(1) {
        // Wait to receive a packet
        EasyLink_receiveAsync(echoRxDoneCb, 0);

        /* Wait indefinitely for Rx */
        Semaphore_pend(echoDoneSem, BIOS_WAIT_FOREVER);

        if(bBlockTransmit == false)
        {
            Rx_Ack.CRC = crc16((uint8_t *)&Rx_Ack,sizeof(Ack_Rx_Req) - 2);
            memcpy(&txPacket.payload, &Rx_Ack, sizeof(Ack_Rx_Req));
            txPacket.len = 8;
            txPacket.dstAddr[0] = Address;
            txPacket.absTime = 0;

            EasyLink_transmitAsync(&txPacket, echoTxDoneCb);

            /* Wait for Tx to complete. A Successful TX will cause the echoTxDoneCb
             * to be called and the echoDoneSem to be released, so we must
             * consume the echoDoneSem
             */
            Semaphore_pend(echoDoneSem, BIOS_WAIT_FOREVER);
        }
    }
}

void echoTask_init(PIN_Handle inPinHandle) {
    pinHandle = inPinHandle;

    Task_Params_init(&echoTaskParams);
    echoTaskParams.stackSize = RFEASYLINKECHO_TASK_STACK_SIZE;
    echoTaskParams.priority = RFEASYLINKECHO_TASK_PRIORITY;
    echoTaskParams.stack = &echoTaskStack;
    echoTaskParams.arg0 = (UInt)1000000;

    Task_construct(&echoTask, rfEasyLinkEchoRxFnx, &echoTaskParams, NULL);
}

/*
 *  ======== main ========
 */
int main(void)
{
    /* Call driver init functions. */
    Board_initGeneral();

    /* Open LED pins */
    pinHandle = PIN_open(&pinState, pinTable);
    Assert_isTrue(pinHandle != NULL, NULL);

    /* Clear LED pins */
    PIN_setOutputValue(pinHandle, Board_PIN_LED1, 0);
    PIN_setOutputValue(pinHandle, Board_PIN_LED2, 0);

    NVS_Init();
    //读寄存器存在两个全局变量中(频率和地址)
    NVS_read(nvsHandle, 0, (void *) Rx_Buff, sizeof(Rx_Buff));
    if(Rx_Buff[0] == 0xFF && Rx_Buff[1] == 0xFF && Rx_Buff[2] == 0xFF)
    {
       //将配置的变量分别替换到修改变量中
       Frq          = Rx_Config.Frequency;
       Address      = Rx_Config.Address;
    //        NVS_write(nvsHandle, 0, &TX_Config.Data,sizeof(Config_Data),\
    //                  NVS_WRITE_PRE_VERIFY | NVS_WRITE_POST_VERIFY);
    }
    else{
       //将读取到的值存到对应的变量中
       memcpy(&Rx_Config + 2, Rx_Buff, sizeof(uint32_t) + sizeof(uint8_t));
       Frq          = Rx_Config.Frequency;
       Address      = Rx_Config.Address;
    //        NVS_erase(nvsHandle, 0, regionAttrs.sectorSize);
    }

    echoTask_init(pinHandle);

    /* Start BIOS */
    BIOS_start();

    return (0);
}

#if DISPLAY_OPEN
void Display_Init()
{
    Display_init();

    Display_Params params;
    Display_Params_init(&params);
    uartDisplayHandle = Display_open(Display_Type_UART, &params);

    Display_printf(uartDisplayHandle, 0, 0, "Display Init Success");
}
#endif

#if UART_OPEN
void Uart_Init()
{
     UART_init();
     UART_Params_init(&uartParams);
     uartParams.writeDataMode = UART_DATA_BINARY;
     uartParams.baudRate = 115200;
     uartParams.readDataMode = UART_DATA_BINARY;
     uartParams.readEcho = UART_ECHO_OFF;

     uart = UART_open(Board_UART0, &uartParams);
     if(uart != NULL) {
        /* UART_open() success */
//         UART_write(uart, &Test, sizeof(uint8_t));
     }
}
#endif