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

📄 simple_mac.c

📁 zigbee 飞思卡尔 音频传输 基于ucos的所有源码
💻 C
字号:
/**
 * Copyright (c) 2004, Freescale Semiconductor
 * Freescale Confidential Proprietary
 *
 * File name :  simple_phy.c
 * Project name: SMAC (Simple Media Access Controller)
 *
 * Department : Freescale Radio Products Division
 *
 * Description : *    This is the SMAC C source media (i.e. MAC) layer file for
 * the HC(S)08 MCU and MC13192 transceiver.
 * The SMAC MAC is the highest layer of C code for the SMAC.*
 *
 * $Author: a20639 $
 * $Date: 2005/03/10 21:28:49 $
 * $Name:  $
 */

#include "pub_def.h"
#include "drivers.h"
#include "simple_phy.h"
#include "simple_mac.h"
#include "MC13192_regs.h"

/* Externals */ 
extern tRxPacket *psDrvRxPacket;
extern UINT8 gu8RTxMode;

/* Version string to put in NVM. Note! size limits */
/* Normally it shoud be enough to change the version numbers. */
#define Database_Label_Version  "1.00"
#define MAC_Version             "1.00"
#define MAC_Label               "SMAC "

/* #pragma MESSAGE DISABLE C3303   /* 
                                 * Warning C3303: Implicit concatenation of 
                                 * strings 
                                 */
/*#pragma MESSAGE DISABLE C4200   /* 
                                 * Warning C4200: Other segment than in 
                                 * previous declaration
                                 */

#pragma CONST_SEG BOOTLOADER_MAC_NV_DATA0

/* DO NOT CHANGE OR REMOVE */
/* These strings will be located in the NV RAM0 section.*/
/* Note!!Check that items are location in the same sequence as specified. */
const unsigned char Freescale_Copyright[54] = "(c) Copyright 2004 Freescale Inc. All rights reserved";
const unsigned char Firmware_Database_Label[40] = "DB Label: XXXXXXXXXXXXXXXXXXXX Ver " Database_Label_Version;
const unsigned char SMAC_Version[47] = "MAC " MAC_Label " Ver " MAC_Version " Build: "__DATE__" "__TIME__;

#pragma CONST_SEG DEFAULT


/*
 * MCPSDataRequest : Transmit data packet
 *
 * Parameters : *packet - packet pointer
 *
 * Return : Status
 */
UINT8 MCPSDataRequest(tTxPacket *psTxPacket)
{
    UINT8 u8Status;
    
    /* Send it to the phy for processing */
    u8Status = PDDataRequest(psTxPacket);
    return u8Status;
}

/*
 * MCPSDataIndication : Receive data packet indication
 *
 * Parameters : Data packet pointer
 *
 * Return : None
 *
 * Notes : This function return should be located in the application 
 */


/*
 * MLMEHibernateRequest : Hibernate the MC13192 (very low current, no CLKO)
 *
 * Parameters : None
 *
 * Return : Status
 */
UINT8 MLMEHibernateRequest(void)
{
    UINT8 u8Status = 0;
    
    u8Status = PLMEHibernateRequest();
    return u8Status;
}


/*
 * MLMEDozeRequest : Doze the MC13192 (Low current, CLKO <= 1MHz)
 *
 * Parameters : None
 *
 * Return : Status
 */
UINT8 MLMEDozeRequest(UINT32 u32Timeout)
{
    UINT8 u8Status = 0;
    
    u8Status = PLMEDozeRequest(u32Timeout);
    return u8Status;
}


/*
 * MLMEWakeRequest : Wake the MC13192 from Hibernate or Doze
 *
 * Parameters : None
 *
 * Return : Status
 */
UINT8 MLMEWakeRequest(void)
{
    UINT8 u8Status = 0;
    if (gu8RTxMode == DOZE_MODE || gu8RTxMode == HIBERNATE_MODE)
    {
        u8Status = PLMEWakeRequest();
        return u8Status;
    }
    return ERROR;
}


/*
 * MLMESetChannelRequest : Set the MC13192 operating channel
 *
 * Parameters : ch - Channel number (0-15)
 *
 * Channel frequencies :
 *  0 : 2.405GHz
 *  1 : 2.410GHz
 *  2 : 2.415GHz
 *  3 : 2.420GHz
 *  4 : 2.425GHz
 *  5 : 2.430GHz
 *  6 : 2.435GHz
 *  7 : 2.440GHz
 *  8 : 2.445GHz
 *  9 : 2.450GHz
 *  10: 2.455GHz
 *  11: 2.460GHz
 *  12: 2.465GHz
 *  13: 2.470GHz
 *  14: 2.475GHz
 *  15: 2.480GHz
 *
 * Return : Status
 */
UINT8 MLMESetChannelRequest(UINT8 u8Channel)
{
    UINT8 u8Status = 0;
    
    u8Status = PLMESetChannelRequest(u8Channel);
    return u8Status;
}


/*
 * MLMERXEnableRequest : Set the MC13192 receiver ON (with optional timeout)
 *
 * Parameters : *rx_packet, timeout - Packet pointer for received data and 
 * timeout
 *
 * Return : Status
 *
 * Notes : Timeout of 0 disables the timeout.
 * The actual timeout period is the timeout value times    the MC13192 timer rate
 * from MLMESetMC13192TmrPrescale.
 */
UINT8 MLMERXEnableRequest(tRxPacket *psRxPacket, UINT32 u32Timeout)
{
    UINT8 u8Status = 0;
    
    psDrvRxPacket = psRxPacket;      /* Assign the rx_packet to SMAC global. */
    if (u32Timeout == 0) {          /* Timeout disabled */
        /* Just enable the receiver */
        u8Status = PLMESetTrxStateRequest(RX_MODE);
    }
    else {
        /* Timeout requested. Get the current time and add the timeout value. */
        /* Erratum: In order to avoid the missing Irq, we reset the timer1 */
        PLMESetTimeRequest(0x00000000);
        u8Status = PLMEEnableMC13192Timer1(u32Timeout);        /* 
                                                                * Set the 
                                                                * timeout in TC1
                                                                */
        u8Status = PLMESetTrxStateRequest(RX_MODE_WTO);
    }
    return u8Status;
}


/*
 * MLMERXDisableRequest : Set the MC13192 receiver OFF
 *
 * Parameters : None
 *
 * Return : Status
 */
UINT8 MLMERXDisableRequest(void)
{
    PLMEDisableMC13192Timer1();     /* 
                                     * In case the timeout is being used, 
                                     * disable it also 
                                     */
    if (PLMESetTrxStateRequest(IDLE_MODE) == SUCCESS) {     /* 
                                                             * Attempt to 
                                                             * disable the 
                                                             * timer 
                                                             */
        return SUCCESS;
    }
    else {
        return ERROR;
    }
}


/*
 * MLMESetMC13192ClockRrate : Set MC13192 CLKo frequency
 *
 * Parameters : freq - Frequency value enumeration (0-7)
 *
 *  Freq    Output Frequency
 *  0       16MHz (Recommended default)
 *  1       8MHz
 *  2       4MHz
 *  3       2MHz
 *  4       1MHz
 *  5       62.5kHz
 *  6       32.786kHz
 *  7       16.393kHz  
 *
 * Return : Status
 */
UINT8 MLMESetMC13192ClockRate(UINT8 u8Freq)
{
    UINT8 u8Status;
    
    u8Status = PLMESetMC13192ClockRate(u8Freq);
    return u8Status;
}


/*
 * MLMESetMC13192TmrPrescale : Set MC13192 timer frequency
 *
 * Parameters : freq - Frequency value enumeration (0-7)
 *
 *  Freq    Output Frequency
 *  0       2MHz
 *  1       1MHz
 *  2       500kHz
 *  3       250kHz (Recommended default)
 *  4       125kHz
 *  5       62.5kHz
 *  6       31.25kHz
 *  7       15.625kHz
 *
 * Return : Status
 */
UINT8 MLMESetMC13192TmrPrescale (UINT8 u8Freq)
{
    UINT8 u8Status;
    
    u8Status = PLMESetMC13192TmrPrescale (u8Freq);
    return u8Status;
}


/*
 * MLMEEnergyDetect : Measure channel energy
 *
 * Parameters : None
 *
 * Return : Energy
 *
 * Notes : 
 * Actual power returned is: -(power/2)
 * Global calibration required for accuracy (from MLMEMC13192FEGainAdjust).
 */
UINT8 MLMEEnergyDetect (void)
{
    UINT8 u8Power;
    
    u8Power = PLMEEnergyDetect();
    return u8Power;
}


/*
 * MLMEMC13192SoftReset : Force the MC13192 into a soft reset condition
 *
 * Parameters : None
 *
 * Return : Status
 */
UINT8 MLMEMC13192SoftReset(void)
{
    UINT8 u8Status;

    /* Performs a soft reset of MC13192 via writing to register 0*/ 
    u8Status = PLMEMC13192SoftReset();
    return u8Status;
}


/*
 * MLMEMC13192XtalAdjust : Adjust the MC13192s crystal trim value
 *
 * Parameters : req_value - Trim value (0-255)
 *
 * Return : Status
 */
UINT8 MLMEMC13192XtalAdjust(UINT8 u8ReqValue)
{
    UINT8 u8Status;
    
    u8Status = PLMEMC13192XtalAdjust(u8ReqValue);
    return u8Status;
}


/*
 * MLMELinkQuality : Report energy from last successful RX packet
 *
 * Parameters : None
 *
 * Return : Energy
 *
 * Notes : 
 * Actual power returned is: -(power/2)
 * Global calibration required for accuracy (from MLMEMC13192FEGainAdjust).
 */
UINT8 MLMELinkQuality (void)
{
    UINT8 u8Power;
  
    u8Power = PLMELinkQuality ();
    return u8Power;
}


/*
 * MLMEMC13192FEGainAdjust : Adjust the MC13192s gain compensator
 *
 * Parameters : gain_value - Gain compensation value 
 * (0 to 255. 128 is center point)
 *
 * Return : Status
 */
UINT8 MLMEMC13192FEGainAdjust(UINT8 u8GainValue)
{
    UINT8 u8Status;
    
    u8Status = PLMEMC13192FEGainAdjust(u8GainValue);
    return u8Status;
}


/*
 * MLMEMC13192PAOutputAdjust : Adjust the Output power of the transmitter
 *
 * Parameters : pa_value - Course Value 
 *
 * Return : Status
 */
UINT8 MLMEMC13192PAOutputAdjust(UINT8 u8PaValue)
{
    UINT8 u8Status;
    
    u8Status = PLMEMC13192PAOutputAdjust(u8PaValue);
    return u8Status;
}


/*
 * MLMEGetRficVersion : Reads the version number of the IC
 *
 * Parameters : None 
 *
 * Return : The version number of the IC
 */
UINT8 MLMEGetRficVersion(void)
{
    UINT8 u8Version;
    
    u8Version = PLMEGetRficVersion();
    return u8Version;
}

/*
*    Function:     Execute a test mode
*    Parameters: packet pointer, desired test mode
*    Return:        none
*/
void MLMETestMode (tTxPacket *psPacket, UINT8 u8mode)
{
    UINT16 u16reg;
    UINT32 i;
    switch (u8mode)
    {
        /* Continuously transmit a PRBS9 pattern. */
        case PULSE_TX_PRBS9:            
            PLMELoadPRBS9 (psPacket);   /* Load the TX RAM */
            PLMESetTrxStateRequest(PULSE_TX_MODE); /* transmit it. Interrupt 
                                                    * routine will retransmit
                                                    * after completion 
                                                    */
        break;
        /* Sets the device back to original IDLE mode. */
        case FORCE_IDLE:
        
           MC13192DisableInterrupts();  /* Mask off interrupts from MC13192 */
           RTXENDeAssert();             /* Force MC13192 to IDLE */
           gu8RTxMode = IDLE_MODE;      /* set the SW mode to IDLE */
           MC13192RestoreInterrupts();
           
           for (i=0; i < 200; i++);
            PLMESetTrxStateRequest(IDLE_MODE);  /* Set to IDLE */
            u16reg = SPIDrvRead(BER_REG);       /* Turn off BER mode */
            u16reg = (u16reg & ~(BER_MASK));
            SPIDrvWrite(BER_REG,u16reg);
            u16reg = SPIDrvRead(PSM_REG);       /* Turn off PSM test mode */
            u16reg = (u16reg & ~(PSM_MASK));
            SPIDrvWrite(PSM_REG,u16reg);
            
            u16reg = SPIDrvRead(PAEN_REG);      /* dis-enable the PA */
            u16reg = (u16reg & ~PAEN_MASK);
            SPIDrvWrite(PAEN_REG,u16reg);
            
            PLMESetTrxStateRequest(TX_MODE);    /* Realign TX */
            
            u16reg = SPIDrvRead(PAEN_REG);      /* enable the PA */
            u16reg = (u16reg | PAEN_MASK);
            SPIDrvWrite(PAEN_REG,u16reg);
            
        break;
        /* Sets the device into continuous RX mode */
        case CONTINUOUS_RX:
            u16reg = SPIDrvRead(BER_REG);       /* Turn on BER mode */
            u16reg = (u16reg | BER_MASK);
            SPIDrvWrite(BER_REG,u16reg);
            PLMESetTrxStateRequest(RX_MODE);    /* Turn the receiver on */
        break;
        /* Sets the device to continuously transmit a 10101010 pattern */
        case CONTINUOUS_TX_MOD:
            SPIDrvWrite(TX_PKT,0xAAAA);         /* Load the test pattern */
            u16reg = SPIDrvRead(PAEN_REG);      /* Disable the PA */
            u16reg = (u16reg & ~(PAEN_MASK));
            SPIDrvWrite(PAEN_REG,u16reg);
            u16reg = SPIDrvRead(TX_PKT_LEN);    /* Set the length field */
            u16reg = ((u16reg & ~(TX_PKT_LEN_MASK)) | 0x0004);
            SPIDrvWrite(TX_PKT_LEN,u16reg);
            PLMESetTrxStateRequest(TX_MODE);    /* Do a transmit to initialize */
            while (gu8RTxMode != IDLE_MODE);    /* Wait till done */
            u16reg = SPIDrvRead(PAEN_REG);      /* Re-enable the PA */
            u16reg = (u16reg | PAEN_MASK);
            SPIDrvWrite(PAEN_REG,u16reg);
            u16reg = SPIDrvRead(BER_REG);       /* Turn on BER mode */
            u16reg = (u16reg | BER_MASK);
            SPIDrvWrite(BER_REG,u16reg);
            PLMESetTrxStateRequest(TX_MODE);    /* Turn the transmitter on */
        break;
        /* Sets the device to continuously transmit an unmodulated CW */
        case CONTINUOUS_TX_NOMOD:
            u16reg = SPIDrvRead(PAEN_REG);      /* Disable the PA */
            u16reg = (u16reg & ~(PAEN_MASK));
            SPIDrvWrite(PAEN_REG,u16reg);
            u16reg = SPIDrvRead(TX_PKT_LEN);    /* Set the length field */
            u16reg = ((u16reg & ~(TX_PKT_LEN_MASK)) | 0x0004);
            SPIDrvWrite(TX_PKT_LEN,u16reg);
            PLMESetTrxStateRequest(TX_MODE);    /* Do a transmit to initialize */
            while (gu8RTxMode != IDLE_MODE);    /* Wait till done */
            u16reg = SPIDrvRead(PAEN_REG);      /* Re-enable the PA */
            u16reg = (u16reg | PAEN_MASK);
            SPIDrvWrite(PAEN_REG,u16reg);
            u16reg = SPIDrvRead(BER_REG);       /* Turn on BER mode */
            u16reg = (u16reg | BER_MASK);
            SPIDrvWrite(BER_REG,u16reg);
            u16reg = SPIDrvRead(PSM_REG);       /* Turn off PSM */
            u16reg = (u16reg | PSM_MASK);
            SPIDrvWrite(PSM_REG,u16reg);
            PLMESetTrxStateRequest(TX_MODE);    /* Turn the transmitter on */
        break;
    }
}

⌨️ 快捷键说明

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