2.4G讲解(BasicMode)

BASIC模式讲解

下方代码实现基于basic模式下一端发送数据,另一端接收数据并返回数据的双向传输。

RF_PHY.c工程替换即可使用,注意未定义的tmos任务自行定义

#include "CONFIG.h"
#include "RF_PHY.h"

#define RF_AUTO_MODE_EXAM       0
#define rxmode 1

uint8_t taskID;
uint8_t TX_DATA[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

uint8_t rxmodeTX_DATA[10] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x10};

//RF 状态回调,注意:不可在此函数中直接调用RF接收或者发送API,需要使用事件的方式调用,通过tmos管理
void RF_2G4StatusCallBack(uint8_t sta, uint8_t crc, uint8_t *rxBuf)
{
    switch(sta)
    {
        case TX_MODE_TX_FINISH:
        {
            tmos_set_event(taskID, peripheral_RX_EVT);
            break;
        }
        case TX_MODE_TX_FAIL:
        {
            break;
        }
        case TX_MODE_RX_DATA:   //basic模式不会运行到此处
        {
            break;
        }
        case TX_MODE_RX_TIMEOUT: // Timeout is about 200us
        {
            break;
        }
        case RX_MODE_RX_DATA:
        {
            if (crc == 0) {
                uint8_t i;

                PRINT("rx recv, rssi: %d\n", (int8_t)rxBuf[0]);
                PRINT("len:%d - ", rxBuf[1]);
                
                for (i = 0; i < rxBuf[1]; i++) {
                    PRINT("%x ", rxBuf[i + 2]);
                }
                PRINT("\n");
            } else {
                if (crc & (1<<0)) {
                    PRINT("crc error\n");
                }

                if (crc & (1<<1)) {
                    PRINT("match type error\n");
                }
            }
#if rxmode
            tmos_set_event(taskID, peripheral_TX_EVT );
#endif

#if  (!RF_AUTO_MODE_EXAM)
            tmos_set_event(taskID, SBP_RF_RF_RX_EVT);   //当前工程,可以删除
#endif
            break;
        }
        case RX_MODE_TX_FINISH: //basic模式不会运行到此处
        {
            break;
        }
        case RX_MODE_TX_FAIL:
        {
            break;
        }
    }
    PRINT("STA: %x\n", sta);
}

uint16_t RF_ProcessEvent(uint8_t task_id, uint16_t events)
{
    if(events & SYS_EVENT_MSG)
    {
        uint8_t *pMsg;

        if((pMsg = tmos_msg_receive(task_id)) != NULL)
        {
            // Release the TMOS message
            tmos_msg_deallocate(pMsg);
        }
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
    }
    if(events & SBP_RF_START_DEVICE_EVT)
    {
        tmos_start_task(taskID, SBP_RF_PERIODIC_EVT, 1000);
        return events ^ SBP_RF_START_DEVICE_EVT;
    }
    if(events & SBP_RF_PERIODIC_EVT)
    {
        RF_Shut();
        RF_Tx(TX_DATA, 10, 0xFF, 0xFF);
        tmos_start_task(taskID, SBP_RF_PERIODIC_EVT, 10);
        return events ^ SBP_RF_PERIODIC_EVT;
    }
    if(events & SBP_RF_RF_RX_EVT)
    {
        uint8_t state;
        RF_Shut();
        TX_DATA[0]++;
        state = RF_Rx(TX_DATA, 10, 0xFF, 0xFF);
        PRINT("RX mode.state = %x\n", state);
        return events ^ SBP_RF_RF_RX_EVT;
    }

    if(events & peripheral_TX_EVT)
    {
        RF_Shut();
        uint8 ret = RF_Tx(rxmodeTX_DATA, 10, 0xFF, 0xFF);
        return events ^ peripheral_TX_EVT;
    }
    if(events & peripheral_RX_EVT)
    {
        RF_Shut();
        uint8 ret = RF_Rx(NULL, 10, 0xFF, 0xFF);
        return events ^ peripheral_RX_EVT;
    }
    return 0;
}

void RF_Init(void)
{
    uint8_t    state;
    rfConfig_t rfConfig;

    tmos_memset(&rfConfig, 0, sizeof(rfConfig_t));
    taskID = TMOS_ProcessEventRegister(RF_ProcessEvent);
    rfConfig.accessAddress = 0x71764129; // 禁止使用0x55555555以及0xAAAAAAAA ( 建议不超过24次位反转,且不超过连续的6个0或1 )
    rfConfig.CRCInit = 0x555555;
    rfConfig.Channel = 8;
    rfConfig.Frequency = 2480000;
#if  RF_AUTO_MODE_EXAM
    rfConfig.LLEMode = LLE_MODE_AUTO;
#else
    rfConfig.LLEMode = LLE_MODE_BASIC | LLE_MODE_EX_CHANNEL; // 使能 LLE_MODE_EX_CHANNEL 表示 选择 rfConfig.Frequency 作为通信频点
#endif
    rfConfig.rfStatusCB = RF_2G4StatusCallBack;
    rfConfig.RxMaxlen = 251;
    state = RF_Config(&rfConfig);
    PRINT("rf 2.4g init: %x\n", state);

#if rxmode
    { // RX mode
        state = RF_Rx(TX_DATA, 10, 0xFF, 0xFF);
        PRINT("RX mode.state = %x\n", state);
    }
#else
    { // TX mode
        PRINT("TX mode");
        tmos_set_event( taskID , SBP_RF_PERIODIC_EVT );
    }
#endif
}

 

posted @ 2024-05-31 14:45  SweetTea_lllpc  阅读(211)  评论(0编辑  收藏  举报