⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftrfhub.c

📁 用cypress的3721芯片做的无线中心结点c代码
💻 C
📖 第 1 页 / 共 2 页
字号:
//----------------------------------------------------------------------------
//
// FTRFHub.c - Hub Implementation
//
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
//
// Copyright 2008, Cypress Semiconductor Corporation.
//
// This software is owned by Cypress Semiconductor Corporation (Cypress)
// and is protected by and subject to worldwide patent protection (United
// States and foreign), United States copyright laws and international
// treaty provisions. Cypress hereby grants to licensee a personal,
// non-exclusive, non-transferable license to copy, use, modify, create
// derivative works of, and compile the Cypress Source Code and derivative
// works for the sole purpose of creating custom software in support of
// licensee product to be used only in conjunction with a Cypress integrated
// circuit as specified in the applicable agreement. Any reproduction,
// modification, translation, compilation, or representation of this
// software except as specified above is prohibited without the express
// written permission of Cypress.
//
// Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,
// WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Cypress reserves the right to make changes without further notice to the
// materials described herein. Cypress does not assume any liability arising
// out of the application or use of any product or circuit described herein.
// Cypress does not authorize its products for use as critical components in
// life-support systems where a malfunction or failure may reasonably be
// expected to result in significant injury to the user. The inclusion of
// Cypress' product in a life-support systems application implies that the
// manufacturer assumes all risk of such use and in doing so indemnifies
// Cypress against all charges.
//
// Use may be limited by and subject to the applicable Cypress software
// license agreement.
//--------------------------------------------------------------------------

#include "FTRFHub.h"
#include "CYFISNP_protocol.h"

#define ENABLE_BIND_BUTTON      0   // 0 = enter bind only via PC command
                                    // 1 = enable local bind button

static void showDevRecords  (void);
static void showLeds        (void);

// ---------------------------------------------------------------------------
// Private variables
// ---------------------------------------------------------------------------
#define LED_I2C_PULSE_TIME      (25/CYFISNP_TIMER_UNITS)    // ON pulsewidth
static WORD ledI2cPulseTimer;

#define LED_I2C_PULSE_SCALER    20              // I2C polls per LED pulse
static signed char ledI2CPulseScaler;

#define LED_GREEN_PULSE         (25/CYFISNP_TIMER_UNITS)    // ON pulsewidth
static WORD greenLedTimer;


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
// main()
//
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void main()
{
    EzI2Cs_SetRamBuffer(sizeof(I2CBuffer), sizeof(I2CBuffer), pI2C);

    M8C_EnableGInt;                     //Enable Interrupts

    EzI2Cs_Start();                     //Turn on I2C

    LED_GREEN_Start();
    LED_RED_Start();

    LED_GREEN_On();

#if CYFISNP_DEBUG || DEBUG_I2C
    TX8_Start(0);
    showDevRecords();
#endif

    LED_GREEN_Off();
    CYFISNP_Start();                // Start Protocol (and radio)

    // -----------------------------------------------------------------------
    // POLLING LOOP
    // -----------------------------------------------------------------------
    while (1)
    {
        // -------------------------------------------------------------------
        // Periodic service SNP (processes a maximum of 1 Rx packet, so if
        //  you're expecting a packet every 10mS, you should be calling this
        //  at LEAST once every 10 mS)
        // -------------------------------------------------------------------
        CYFISNP_Run();              // Run CYFISNP once thru the loop.

        // -------------------------------------------------------------------
        // CYFISNP Bind Mode lasts ~12 seconds and SCD wants it longer,
        //  so each SCD bind command results in 2x CYFISNP Bind cycles
        // -------------------------------------------------------------------
        if (bindRetryCt != 0 && CYFISNP_eProtState != CYFISNP_BIND_MODE)
        {
            --bindRetryCt;
            CYFISNP_BindStart();
            CYFISNP_OutStr("<RestartBind>");
        }

        // -------------------------------------------------------------------
        // Control RED and GREEN LEDs
        // -------------------------------------------------------------------
        showLeds();

        // -------------------------------------------------------------------
        // Serve Rx data from node(s)
        // -------------------------------------------------------------------
        if (CYFISNP_RxDataPend())   // If SNP Rx data pending
        {
            CYFISNP_TimeSet(&greenLedTimer, LED_GREEN_PULSE);
            LED_GREEN_On();
            ServeSNPPackets();      // Service pending Rx data
        }

        // -------------------------------------------------------------------
        // Service possible I2C request from Host
        // -------------------------------------------------------------------
        CheckHostRequest();
    }
}


///---------------------------------------------------------------------------
// ServeSNPPackets() - A SNP Rx packet is pending, copy the packet to I2C buffer
//             and store in the buffer.
//
//Variables:
//             pApiPkt - pointer to SNP data
//             numberOfPacketsInBuffer - running packet count in the hub buffer
//             pNextWrite - pointer to the start of where the data is going to
//                          be placed that was received from SNP
//----------------------------------------------------------------------------
void ServeSNPPackets(void)
{
    BYTE index = 0;
    if (numberOfPacketsInBuffer != MAX_BUFF_PKTS) {
        pApiPkt = CYFISNP_RxDataGet();      // ptr to Rx API Packet

        devId = pApiPkt->devId;
        switch (pApiPkt->type) {
        //--------------------------------------------------------------------
        case CYFISNP_API_TYPE_BIND_RSP_ACK:
            bindRetryCt = 0;
            bindNodeId = pApiPkt->devId;
            bindNodeMID[0] = pApiPkt->payload[0];
            bindNodeMID[1] = pApiPkt->payload[1];
            bindNodeMID[2] = pApiPkt->payload[2];
            bindNodeMID[3] = pApiPkt->payload[3];
#ifdef CYFISNP_DEBUG
            CYFISNP_OutStr("\r\nSNP_BRespAck MID=");
            CYFISNP_OutHex(bindNodeMID[0]);    CYFISNP_OutHex(bindNodeMID[1]);
            CYFISNP_OutHex(bindNodeMID[2]);    CYFISNP_OutHex(bindNodeMID[3]);
#endif
            break;

        //--------------------------------------------------------------------
        case CYFISNP_API_TYPE_CONF_BCDR:
        case CYFISNP_API_TYPE_CONF:
        case CYFISNP_API_TYPE_SYNC_BCDR:
        case CYFISNP_API_TYPE_SYNC:
            pNextWrite->length = pApiPkt->length;
            pNextWrite->rssi = pApiPkt->rssi;
            pNextWrite->devId = pApiPkt->devId;
            length = pApiPkt->length;
            for (index = 0; index<=length; index++) {
                pNextWrite->payload[index] = pApiPkt->payload[index];
            }
            WriteBufferManager();       // manage pointer and buffer
#if CYFISNP_DEBUG
            CYFISNP_OutStr("<RxPkts=");
            CYFISNP_OutHex(numberOfPacketsInBuffer);
            CYFISNP_OutChar('>');
#endif
            break;
        default:
            break;
        }
        CYFISNP_RxDataRelease();        // Free the Rx buffer for reuse
    }
}


//----------------------------------------------------------------------------
// showI2C_Buffer() - Show Rx/Tx I2C buffer on TX8 serial port for debug
//----------------------------------------------------------------------------
#ifdef DEBUG_I2C
void showI2C_Buffer(const BYTE *msg, BYTE len) {
    BYTE *pch = &I2CBuffer[0];
    CYFISNP_OutStr(msg);
    while (len-- != 0) {
        CYFISNP_OutHex(*pch++);
        CYFISNP_OutChar(',');
    }
}
#else
#define showI2C_Buffer(msg, len)
#endif

//----------------------------------------------------------------------------
//
// CheckHostRequest() - Of the host has made a request for a packet, figure out
//              what type of packet is requested and return it thru the
//              I2C Buffer
//
//Variables:
//        I2CBuffer - buffer that EZI2C has access to. Used when hub wants to
//                    return data back to host.
//        sTestTxPkt - instantiation of CYFISNP_API_PKT. Used so data can be
//                     put in the defined structure by SNP
//        numberOfPacketsInBuffer - running packet count in the hub buffer
//        pNextRead - pointer to the start of the next set of data to return
//                    to host.
//----------------------------------------------------------------------------
void CheckHostRequest()
{
    BYTE ivar = 0;
    BYTE index = 0;
    BYTE payloadIndex = 0;

    switch (I2CBuffer[COMMAND_BYTE]) {
    case HOST_CONFIG_STATUS:
        showI2C_Buffer("\r\nI2C Rx:", 1);
        I2CBuffer[1] = FIRST_TOUCH_RF_DEVICE;
        I2CBuffer[2] = FW_VERSION;
        I2CBuffer[3] = (BYTE)CYFISNP_eProtState;   //current state of SNP Protocol
        CYFISNP_SetPtr(&I2CBuffer[4]);
        CYFISNP_SetLength(0x04);
        CYFISNP_GetFuses();
        I2CBuffer[8] = RESERVED;
        I2CBuffer[9] = NETWORK_SIZE;
        I2CBuffer[0] = HOST_CONFIG_STATUS_RSP;
        showI2C_Buffer(" Tx:", 10);
        break;

    // -----------------------------------------------------------------------
    // get next packet from hub buffer and return to HOST via I2CBuffer
    // -----------------------------------------------------------------------
    case FETCH_NXT_PKT:
        if (--ledI2CPulseScaler < 0) {
            ledI2CPulseScaler = LED_I2C_PULSE_SCALER;
            CYFISNP_TimeSet(&ledI2cPulseTimer, LED_I2C_PULSE_TIME);
            LED_RED_On();
        }

        if (numberOfPacketsInBuffer != 0) {
            showI2C_Buffer("\r\nI2C Rx:", 1);
            length = pNextRead->length;
            devId  = pNextRead->devId;

            //stuff I2C buffer with data
            I2CBuffer[1] = numberOfPacketsInBuffer;
            I2CBuffer[2] = length;
            I2CBuffer[3] = pNextRead->rssi;
            I2CBuffer[4] = RESERVED;
            I2CBuffer[5] = devId;
            for (index=6; index < (length+6); index++)
            {
                I2CBuffer[index] = pNextRead->payload[payloadIndex];
                payloadIndex++;
            }
            ReadBufferManager();
            showI2C_Buffer(" Tx:", 10);
        }
        else
        {
            I2CBuffer[1] = 0xFF;   // if Rx messages available, return 0xFF
            CYFISNP_OutChar('*');
        }
        I2CBuffer[RESPONSE_BYTE] = FETCH_NXT_PKT_RSP;
        break;

    // -----------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -