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

📄 main.c

📁 CYRF6936 zigbee模块设计的全部资料
💻 C
字号:
//--------------------------------------------------------------------------
//
//  Filename:     main.c
//
//  Description:  Tutorial 4 file
//
//--------------------------------------------------------------------------
// $Archive: /WirelessUSB/WUSB Kits/CY3630 LP EVK/DocSrc/CD_Root/Firmware/Source/Unsupported/NetworkQuality/main.c $
// $Modtime: 4/25/06 8:50p $
// $Revision: 5 $
//--------------------------------------------------------------------------
//
// Copyright 2005-2006, 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.
//
//--------------------------------------------------------------------------

//--------------------------------------
// Included files
//--------------------------------------
#include <m8c.h>        // PSoC Part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "PSoCgpioint.h" // Contains nFPGA_SS* defines
#include "lpregs.h"
#include "lpradio.h"
#include "debug.h"


//--------------------------------------
// Local Definitions and Types
//--------------------------------------
#define PAYLOAD_LENGTH      5  // 0-10
#define CHANNEL             80 // 0-98
#define SOP_PN_CODE         2  // 0-11

// Select one DATMODE_1MBPS | DATMODE_8DR | DATMODE_DDR | DATMODE_SDR
#define DATA_RATE           DATMODE_8DR

#define RX_TIMEOUT          0x03FF // Number of receive loop iterations before a timeout occurs. ~40ms
#define TX_DELAY            0x03FF // Give the receiver time to start. ~4ms

#define LAST_RADIO_REGISTER 0x35


//--------------------------------------
// Global Definitions
//--------------------------------------
// The following bit array is used by DumpRadioRegisters() to know whether
// to dump a register value.  Each bit in the array represents a LP register.
// As DumpRadioRegisters() loops through the registers it checks the bits in
// this array.  If the bit is set, the register is printed.  If it is clear,
// "xx" is printed.
// 0x20-0x25 are the file registers.
// Within each byte, the least significant bit is 0 and the most significant
// bit is 7
//                                 Register  0-7   8-F 10-17 18-1F 20-27 28-2F 30-37
const unsigned char LpRegistersToPrint[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0x03, 0x24};


//--------------------------------------
// Local Function Declarations
//--------------------------------------
void HardwareInit(void);
void DumpRadioRegisters(void);
void Delay(unsigned short Count);
void DumpBuffer(char *ptr, unsigned Length);


//--------------------------------------
// Local Function Definitions
//--------------------------------------

//--------------------------------------------------------------------------
//
//  Function:    main
//
//  Description: The main program loop
//
//--------------------------------------------------------------------------
void main()
{
    unsigned char  TxPayloadBuffer[10] = { 0x51, 0xA2, 0xF3, 0x44, 0x95, 0xE6, 0x37, 0x88, 0xD9, 0x2A };
    unsigned char  RxPayloadBuffer[PAYLOAD_LENGTH];
    unsigned short RxTimeout;
    unsigned char  ConsecutiveDroppedPackets = 0;
    unsigned char  SopDetect; // Start Of Packet Detect

    HardwareInit();

    // DATMODE_8DR needs a little extra time to receive the ACK
    RadioInit(XACT_CFG_RST | END_STATE_IDLE | ACK_TO_8X, (TX_CFG_RST & ~TX_DATMODE_MSK) | DATA_RATE);

    // Use 64 chips for start of packet (will increase range)
    RadioSetFrameConfig(FRAMING_CFG_RST | SOP_LEN);

    RadioSetFrequency(CHANNEL); // Set the transmit frequency (2400MHz + CHANNEL)
    RadioSetSopPnCode(SOP_PN_CODE); // Change the PN Code for the Start of Packet

    OutStr("\r\n\n");
    DumpRadioRegisters();
    OutStr("\r\n\nWelcome to WirelessUSB LP - Network Quality Application\r\n");

    while(1)
    {
        RadioSetPtr(RxPayloadBuffer);
        RadioSetLength(PAYLOAD_LENGTH);

        RadioStartReceive();

        RxTimeout = RX_TIMEOUT;  // Receive times out when 0 is reached
        SopDetect = FALSE;

        // Receive a packet (or timeout trying)
        while (RxTimeout != 0)
        {
            RADIO_STATE RxState;
            RADIO_LENGTH ReceivedPayloadSize;

            RxState = RadioGetReceiveState();

            if ((RxState & RADIO_SOP) && !SopDetect)
            {
                // A Start of Packet has been detected
                RxTimeout = 1; // Give the packet a chance to arrive
                SopDetect = TRUE;
            }

            if (RxState & RADIO_COMPLETE)
            {
                ReceivedPayloadSize = RadioEndReceive();

                if (!(RxState & RADIO_ERROR))
                {
                    if (ReceivedPayloadSize == PAYLOAD_LENGTH)
                    {
                        // Successfully received a packet
                        ConsecutiveDroppedPackets = 0;

                        OutChar('_');
                        break;
                    }
                    // else not the correct length.  Drop Packet
                }
                else
                {
                    // Packet received with an error.
                    OutHex(ReceivedPayloadSize); // Print size
                }

                // Restart the receive operation
                SopDetect = FALSE;
                RxTimeout = 0;
                RadioStartReceive();
            }

            --RxTimeout;
        }

        if (RxTimeout == 0)
        {
            RxTimeout = RX_TIMEOUT;  // Receive times out when 0 is reached
            ++ConsecutiveDroppedPackets;

            if (ConsecutiveDroppedPackets == 0) // Wrap detection
                ConsecutiveDroppedPackets = 0xFF;

            if (SopDetect)
                OutChar('^'); // A SOP has been detected before calling Abort

            RadioAbort();
        }

        // Give the user feedback what is going on
        if (ConsecutiveDroppedPackets > 4)
        {
            nRED_LED_Data_ADDR    &= ~nRED_LED_MASK;    // Red LED On
            nYELLOW_LED_Data_ADDR |=  nYELLOW_LED_MASK; // Yellow LED Off
            nGREEN_LED_Data_ADDR  |=  nGREEN_LED_MASK;  // Green LED Off
        }
        else if (ConsecutiveDroppedPackets > 1)
        {
            nRED_LED_Data_ADDR    |=  nRED_LED_MASK;    // Red LED Off
            nYELLOW_LED_Data_ADDR &= ~nYELLOW_LED_MASK; // Yellow LED On
            nGREEN_LED_Data_ADDR  |=  nGREEN_LED_MASK;  // Green LED Off
        }
        else
        {
            nRED_LED_Data_ADDR    |=  nRED_LED_MASK;    // Red LED Off
            nYELLOW_LED_Data_ADDR |=  nYELLOW_LED_MASK; // Yellow LED Off
            nGREEN_LED_Data_ADDR  &= ~nGREEN_LED_MASK;  // Green LED On
        }

        Delay(TX_DELAY); // Give the receiver time to start.

        //--- Transmit a packet ----------------------------------
        RadioSetPtr(TxPayloadBuffer); // Set the transmit buffer

        RadioBlockingTransmit(1, PAYLOAD_LENGTH); // Send it
        OutChar('+');
    }
}


//--------------------------------------------------------------------------
//
//  Function:    HardwareInit
//
//  Description: Initialize the hardware components on the EVK Board
//               (Non-LP Radio related)
//
//--------------------------------------------------------------------------
void HardwareInit()
{
    // Turn LEDs off by default
    nRED_LED_Data_ADDR    |= nRED_LED_MASK;
    nYELLOW_LED_Data_ADDR |= nYELLOW_LED_MASK;
    nGREEN_LED_Data_ADDR  |= nGREEN_LED_MASK;

    // Serial Transmit line must start high
    UART_TX_Data_ADDR |= UART_TX_MASK;

    // Initialize the software-based serial output.
    OutChar(0x00);

    // Write a one to the PSoC register so the switches can be read
    SW1_Data_ADDR |= SW1_MASK;
    SW2_Data_ADDR |= SW2_MASK;

    // Start the SPI interface with the LP radio
    // This is a PSoC created function
    SPIM_Radio_Start(SPIM_Radio_SPIM_MODE_0 | SPIM_Radio_SPIM_MSB_FIRST);
}


//--------------------------------------------------------------------------
//
//  Function:    DumpRadioRegisters
//
//  Description: Print the radio registers to the screen
//
//--------------------------------------------------------------------------
void DumpRadioRegisters()
{
    unsigned char Index;
    unsigned char ByteInArray = 0;
    unsigned char BitInByte = 0x01;

    OutStr("\r\n Registers: ");

    for (Index = 0; Index <= LAST_RADIO_REGISTER; ++Index)
    {
        if (LpRegistersToPrint[ByteInArray] & BitInByte)
            OutHex(RadioRead(Index));
        else
            OutStr("xx"); // This register cannot be displayed

        BitInByte <<= 1;

        if (!BitInByte)
        {
            // Bit has rotated out
            ++ByteInArray; // Move to next byte in array
            BitInByte = 0x01; // Start with the first bit in the byte
        }

        // Start a new line after every 16 values
        if ((Index & 0x0F) == 0x0F)
            OutStr("\r\n            ");
        else
            OutChar(' ');
    }
}


//--------------------------------------------------------------------------
//
//  Function:    Delay
//
//  Description: Burn MCU cycles with an arbitrary delay
//
//  Inputs:      Count - the amount of arbitrary delay
//
//--------------------------------------------------------------------------
void Delay(unsigned short Count)
{
    for (; Count; --Count)
    {
        asm("nop");
    }
}


//--------------------------------------------------------------------------
//
//  Function:    DumpBuffer
//
//  Description: Ouput a buffer to the serial port
//
//  Inputs:      ptr and Length
//
//--------------------------------------------------------------------------
void DumpBuffer(char *Ptr, unsigned Length)
{
    for (; Length; Length--)
    {
        OutHex(*Ptr++);
        OutChar(' ');
    }
}

⌨️ 快捷键说明

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