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

📄 hal.h

📁 基于无线传感器网络的CC2420收发芯片初始化源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************************************
 *                                                                                                     *
 *        **********                                                                                   *
 *       ************                                                                                  *
 *      ***        ***                                                                                 *
 *      ***   +++   ***                                                                                *
 *      ***   + +   ***                                                                                *
 *      ***   +                        CHIPCON HARDWARE ABSTRACTION LIBRARY FOR THE CC2420             *
 *      ***   + +   ***                               Library header file                              *
 *      ***   +++   ***                                                                                *
 *      ***        ***                                                                                 *
 *       ************                                                                                  *
 *        **********                                                                                   *
 *                                                                                                     *
 *******************************************************************************************************
 * The Chipcon Hardware Abstraction Library is a collection of functions, macros and constants, which  *
 * can be used to ease access to the hardware on the CC2420 and the target microcontroller.            *
 *                                                                                                     *
 * All HAL library macros and constants are defined here.                                              *
 *******************************************************************************************************
 * Compiler: AVR-GCC                                                                                   *
 * Target platform: CC2420DB, CC2420 + any ATMEGA MCU                                                  *
 *******************************************************************************************************
 * Revision history:                                                                                   *
 *  $Log: hal.h,v $
 *  Revision 1.8  2004/03/30 14:59:03  mbr
 *  Release for web
 *  
 *
 *
 *******************************************************************************************************/
#ifndef HAL_H
#define HAL_H




/*******************************************************************************************************
 *******************************************************************************************************
 **************************            AVR<->CC2420 SPI INTERFACE             **************************
 *******************************************************************************************************
 *******************************************************************************************************/


//-------------------------------------------------------------------------------------------------------
// Initialization

// Enables SPI, selects "master", clock rate FCK / 2, and SPI mode 0
#define SPI_INIT() \
    do { \
        SPCR = BM(SPE) | BM(MSTR); \
        SPSR = BM(SPI2X); \
    } while (0)
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
// FAST SPI: Low level functions
//      x = value (BYTE or WORD)
//      p = pointer to the byte array to operate on
//      c = the byte count
//
// SPI_ENABLE() and SPI_DISABLE() are located in the devboard header file (CS_N uses GPIO)

#define FASTSPI_WAIT() \
    do { \
        while (!(SPSR & BM(SPIF))); \
    } while (0) 

#define FASTSPI_TX(x) \
    do { \
        SPDR = x; \
        FASTSPI_WAIT(); \
    } while (0)

#define FASTSPI_RX(x) \
    do { \
        SPDR = 0; \
        FASTSPI_WAIT(); \
        x = SPDR; \
    } while (0)

#define FASTSPI_RX_GARBAGE() \
    do { \
        SPDR = 0; \
        FASTSPI_WAIT(); \
    } while (0)

#define FASTSPI_TX_WORD_LE(x) \
    do { \
        FASTSPI_TX(x); \
        FASTSPI_TX((x) >> 8); \
    } while (0)
    
#define FASTSPI_TX_WORD(x) \
    do { \
        FASTSPI_TX(((WORD)(x)) >> 8); \
        FASTSPI_TX((BYTE)(x)); \
    } while (0)
    
#define FASTSPI_TX_MANY(p,c) \
    do { \
        for (UINT8 spiCnt = 0; spiCnt < (c); spiCnt++) { \
            FASTSPI_TX(((BYTE*)(p))[spiCnt]); \
        } \
    } while (0)
        
#define FASTSPI_RX_WORD_LE(x) \
    do { \
        SPDR = 0; \
        FASTSPI_WAIT(); \
        x = SPDR; \
        SPDR = 0; \
        FASTSPI_WAIT(); \
        x |= SPDR << 8; \
    } while (0)

#define FASTSPI_RX_WORD(x) \
    do { \
        SPDR = 0; \
        FASTSPI_WAIT(); \
        x = SPDR << 8; \
        SPDR = 0; \
        FASTSPI_WAIT(); \
        x |= SPDR; \
    } while (0)
    
#define FASTSPI_RX_MANY(p,c) \
    do { \
        for (UINT8 spiCnt = 0; spiCnt < (c); spiCnt++) { \
            FASTSPI_RX((p)[spiCnt]); \
        } \
    } while (0)
        
// Register address:
#define FASTSPI_TX_ADDR(a) \
    do { \
        SPDR = a; \
        FASTSPI_WAIT(); \
    } while (0)

// Register address:
#define FASTSPI_RX_ADDR(a) \
    do { \
        SPDR = (a) | 0x40; \
        FASTSPI_WAIT(); \
    } while (0)
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
//  FAST SPI: Register access
//      s = command strobe
//      a = register address
//      v = register value

#define FASTSPI_STROBE(s) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX_ADDR(s); \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_SETREG(a,v) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX_ADDR(a); \
        FASTSPI_TX((BYTE) ((v) >> 8)); \
        FASTSPI_TX((BYTE) (v)); \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_GETREG(a,v) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_RX_ADDR(a); \
        FASTSPI_RX_WORD(v); \
        SPI_DISABLE(); \
    } while (0)

// Updates the SPI status byte
#define FASTSPI_UPD_STATUS(s) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX_ADDR(CC2420_SNOP); \
        s = SPDR; \
        SPI_DISABLE(); \
    } while (0)
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
//  FAST SPI: FIFO access
//      p = pointer to the byte array to be read/written
//      c = the number of bytes to read/write
//      b = single data byte

#define FASTSPI_WRITE_FIFO(p,c) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX_ADDR(CC2420_TXFIFO); \
        for (UINT8 spiCnt = 0; spiCnt < (c); spiCnt++) { \
            FASTSPI_TX(((BYTE*)(p))[spiCnt]); \
        } \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_READ_FIFO(p,c) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_RX_ADDR(CC2420_RXFIFO); \
        for (UINT8 spiCnt = 0; spiCnt < (c); spiCnt++) { \
            while (!FIFO_IS_1); \
            FASTSPI_RX(((BYTE*)(p))[spiCnt]); \
        } \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_READ_FIFO_BYTE(b) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_RX_ADDR(CC2420_RXFIFO); \
        FASTSPI_RX(b); \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_READ_FIFO_NO_WAIT(p,c) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_RX_ADDR(CC2420_RXFIFO); \
        for (UINT8 spiCnt = 0; spiCnt < (c); spiCnt++) { \
            FASTSPI_RX(((BYTE*)(p))[spiCnt]); \
        } \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_READ_FIFO_GARBAGE(c) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_RX_ADDR(CC2420_RXFIFO); \
        for (UINT8 spiCnt = 0; ((spiCnt < (c)) && (FIFO_IS_1)); spiCnt++) { \
            FASTSPI_RX_GARBAGE(); \
        } \
        SPI_DISABLE(); \
    } while (0)
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
//  FAST SPI: CC2420 RAM access (big or little-endian order)
//      p = pointer to the variable to be written
//      a = the CC2420 RAM address
//      c = the number of bytes to write
//      n = counter variable which is used in for/while loops (UINT8)
//
//  Example of usage:
//      UINT8 n;
//      UINT16 shortAddress = 0xBEEF;
//      FASTSPI_WRITE_RAM_LE(&shortAddress, CC2420RAM_SHORTADDR, 2);

#define FASTSPI_WRITE_RAM_LE(p,a,c,n) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX(0x80 | (a & 0x7F)); \
        FASTSPI_TX((a >> 1) & 0xC0); \
        for (n = 0; n < (c); n++) { \
            FASTSPI_TX(((BYTE*)(p))[n]); \
        } \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_READ_RAM_LE(p,a,c,n) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX(0x80 | (a & 0x7F)); \
        FASTSPI_TX(((a >> 1) & 0xC0) | 0x20); \
        for (n = 0; n < (c); n++) { \
            FASTSPI_RX(((BYTE*)(p))[n]); \
        } \
        SPI_DISABLE(); \
    } while (0)
    
#define FASTSPI_WRITE_RAM(p,a,c,n) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX(0x80 | (a & 0x7F)); \
        FASTSPI_TX((a >> 1) & 0xC0); \
        n = c; \
        do { \
            FASTSPI_TX(((BYTE*)(p))[--n]); \
        } while (n); \
        SPI_DISABLE(); \
    } while (0)

#define FASTSPI_READ_RAM(p,a,c,n) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX(0x80 | (a & 0x7F)); \
        FASTSPI_TX(((a >> 1) & 0xC0) | 0x20); \
        n = c; \
        do { \
            FASTSPI_RX(((BYTE*)(p))[--n]); \
        } while (n); \
        SPI_DISABLE(); \
    } while (0)
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
// Other useful SPI macros
#define FASTSPI_RESET_CC2420() \
    do { \
        FASTSPI_SETREG(CC2420_MAIN, 0x0000); \
        FASTSPI_SETREG(CC2420_MAIN, 0xF800); \
    } while (0)
//-------------------------------------------------------------------------------------------------------




/*******************************************************************************************************
 *******************************************************************************************************
 **************************                    INTERRUPTS                     **************************
 *******************************************************************************************************
 *******************************************************************************************************/


//-------------------------------------------------------------------------------------------------------
// General
#define ENABLE_GLOBAL_INT()         do { asm ("sei\n\t" ::); } while (0)
#define DISABLE_GLOBAL_INT()        do { asm ("cli\n\t" ::); } while (0)
//-------------------------------------------------------------------------------------------------------


⌨️ 快捷键说明

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