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

📄 hal_cc2520.c

📁 zigbee 无线通信 CC2520 芯片 C语言 原代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    return s;
}


/***********************************************************************************
* @fn      CC2520_BCLR
*
* @brief   BCLR - Clear a single bit
*
* @param  uint8 bitAddr - address
*
* @return  uint8 - status byte
*/
uint8 CC2520_BCLR(uint8 bitAddr)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_BCLR);
    CC2520_SPI_TXRX(bitAddr);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_CTR
*
* @brief   CTR - Counter mode encryption
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - number of plaintext bytes
*         uint8 n - address of nonce
*         uint16 src - starting address of plaintext
*         uint16 dest - destination address, if dest=0 destination address is set
*                       equal to src address.
*
* @return  uint8 - status byte
*/
uint8 CC2520_CTR(uint8 pri, uint8 k, uint8 c, uint8 n, uint16 src, uint16 dest)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_CTR | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX(c);
    CC2520_SPI_TXRX(n);
    CC2520_SPI_TXRX((HIBYTE(src) << 4) | HIBYTE(dest));
    CC2520_SPI_TXRX(LOBYTE(src));
    CC2520_SPI_TXRX(LOBYTE(dest));
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_UCTR
*
* @brief   UCTR - Counter mode decryption.
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - number of plaintext bytes
*         uint8 n - address of nonce
*         uint16 src - starting address of ciphertext
*         uint16 dest - destination address
*
* @return  uint8 - status byte
*/
uint8 CC2520_UCTR(uint8 pri, uint8 k, uint8 c, uint8 n, uint16 src, uint16 dest)
{
    return CC2520_CTR(pri, k, c, n, src, dest);
}


/***********************************************************************************
* @fn     CC2520_CBCMAC
*
* @brief  CBCMAC - authentication using CBC-MAC
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - number of plaintext bytes
*         uint16 src - starting address of plaintext
*         uint16 dest - destination address
*         uint8 m - sets length of integrity code (m=1,2,3 gives lenght of integrity
*                   field 4,8,16)
*
* @return  uint8 - status byte
*/
uint8 CC2520_CBCMAC(uint8 pri, uint8 k, uint8 c, uint16 src, uint16 dest, uint8 m)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_CBCMAC | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX(c);
    CC2520_SPI_TXRX((HIBYTE(src) << 4) | HIBYTE(dest));
    CC2520_SPI_TXRX(LOBYTE(src));
    CC2520_SPI_TXRX(LOBYTE(dest));
    CC2520_SPI_TXRX(m);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn     CC2520_UCBCMAC
*
* @brief  UCBCMAC - reverse authentication using CBC-MAC
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - number of plaintext bytes
*         uint16 src - starting address of plaintext
*         uint8 m - sets length of integrity code (m=1,2,3 gives lenght of integrity
*                   field 4,8,16)
*
* @return  uint8 - status byte
*/
uint8 CC2520_UCBCMAC(uint8 pri, uint8 k, uint8 c, uint16 src, uint8 m)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_UCBCMAC | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX(c);
    CC2520_SPI_TXRX(HIBYTE(src));
    CC2520_SPI_TXRX(LOBYTE(src));
    CC2520_SPI_TXRX(m);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_CCM
*
* @brief   CCM - encryption and authentication using CCM
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - number of plaintext bytes to authenticate and encrypt
*         uint8 n - address of nonce
*         uint16 src - starting address of plaintext
*         uint16 dest - destination address
*         uint8 f - number of plaintext bytes to authenticate
*         uint8 m - sets length of integrity code (m=1,2,3 gives lenght of integrity
*                   field 4,8,16)
*
* @return  uint8 - status byte
*/
uint8 CC2520_CCM(uint8 pri, uint8 k, uint8 c, uint8 n, uint16 src, \
    uint16 dest, uint8 f, uint8 m)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_CCM | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX(c);
    CC2520_SPI_TXRX(n);
    CC2520_SPI_TXRX((HIBYTE(src) << 4) | HIBYTE(dest));
    CC2520_SPI_TXRX(LOBYTE(src));
    CC2520_SPI_TXRX(LOBYTE(dest));
    CC2520_SPI_TXRX(f);
    CC2520_SPI_TXRX(m);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_UCCM
*
* @brief   UCCM - decryption and reverse authentication using CCM
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - number of bytes to decrypt and authenticate
*         uint8 n - address of nonce
*         uint16 src - starting address
*         uint16 dest - destination address
*         uint8 f - number of bytes to authenticate
*         uint8 m - ets length of integrity code (m=1,2,3 gives lenght of integrity
*                   field 4,8,16)
*
* @return  uint8 - status byte
*/
uint8 CC2520_UCCM(uint8 pri, uint8 k, uint8 c, uint8 n, uint16 src, \
    uint16 dest, uint8 f, uint8 m)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_UCCM | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX(c);
    CC2520_SPI_TXRX(n);
    CC2520_SPI_TXRX((HIBYTE(src) << 4) | HIBYTE(dest));
    CC2520_SPI_TXRX(LOBYTE(src));
    CC2520_SPI_TXRX(LOBYTE(dest));
    CC2520_SPI_TXRX(f);
    CC2520_SPI_TXRX(m);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_ECB
*
* @brief   ECB encryption
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - encrypts 16-C bytes of data in a 16 byte block
*         uint16 src - source address
*         uint16 dest - destination address
*
* @return  uint8 - status byte
*/
uint8 CC2520_ECB(uint8 pri, uint8 k, uint8 c, uint16 src, uint16 dest)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_ECB | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX((c << 4) | HIBYTE(src));
    CC2520_SPI_TXRX(LOBYTE(src));
    CC2520_SPI_TXRX(HIBYTE(dest));
    CC2520_SPI_TXRX(LOBYTE(dest));
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_ECBO
*
* @brief   ECB encryption overwriting plaintext
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - encrypts 16-C bytes of data in a 16 byte block
*         uint16 addr - source and destination address
*
* @return  uint8 - status byte
*/
uint8 CC2520_ECBO(uint8 pri, uint8 k, uint8 c, uint16 addr)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_ECBO | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX((c << 4) | HIBYTE(addr));
    CC2520_SPI_TXRX(LOBYTE(addr));
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_ECBX
*
* @brief   ECB encryption and XOR
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - encrypts 16-C bytes of data in a 16 byte block
*         uint16 src - source address
*         uint16 dest - destination address
*
* @return  uint8 - status byte
*/
uint8 CC2520_ECBX(uint8 pri, uint8 k, uint8 c, uint16 src, uint16 dest)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_ECBX | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX((c << 4) | HIBYTE(src));
    CC2520_SPI_TXRX(LOBYTE(src));
    CC2520_SPI_TXRX(HIBYTE(dest));
    CC2520_SPI_TXRX(LOBYTE(dest));
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_ECBXO
*
* @brief   ECBO and XOR
*
* @param  uint8 pri - priority
*         uint8 k - address of key
*         uint8 c - encrypts 16-C bytes of data in a 16 byte block
*         uint16 addr - source and destination address
*
* @return  uint8 - status byte
*/
uint8 CC2520_ECBXO(uint8 pri, uint8 k, uint8 c, uint16 addr)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_ECBXO | pri);
    CC2520_SPI_TXRX(k);
    CC2520_SPI_TXRX((c << 4) | HIBYTE(addr));
    CC2520_SPI_TXRX(LOBYTE(addr));
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_INC
*
* @brief   INC - increment instruction. Increments 2^c byte word with least
*                significant byte at address.
*
* @param  uint8 pri - priority
*         uint8 c - increments 2^c byte word
*         uint16 addr - address
*
* @return  uint8 - status byte
*/
uint8 CC2520_INC(uint8 pri, uint8 c, uint16 addr)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_INC | pri);
    CC2520_SPI_TXRX((c << 4) | HIBYTE(addr));
    CC2520_SPI_TXRX(LOBYTE(addr));
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_ABORT
*
* @brief   Abort ongoing data management or security instruction
*
* @param  uint8 c - abort mode (see datasheet)
*
* @return  uint8 - status byte
*/
uint8 CC2520_ABORT(uint8 c)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_ABORT);
    CC2520_SPI_TXRX(c);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_REGRD
*
* @brief   Register read. Can only be started from addresses below 0x40
*
* @param  uint8 addr - address
*         uint8 count - number of bytes
*         uint8  *pValues - buffer to store result
*
* @return  uint8 - status byte
*/
uint8 CC2520_REGRD(uint8 addr, uint8 count, uint8  *pValues)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_REGRD | addr);
    CC2520_INS_RD_ARRAY(count, pValues);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_REGRD8
*
* @brief   Read one register byte
*
* @param  uint8 addr - address
*
* @return  uint8 - result
*/
uint8 CC2520_REGRD8(uint8 addr)
{
    uint8 value;
    CC2520_SPI_BEGIN();
    CC2520_SPI_TXRX(CC2520_INS_REGRD | addr);
    value = CC2520_SPI_TXRX(0x00);
    CC2520_SPI_END();
    return value;
}


/***********************************************************************************
* @fn      CC2520_REGRD16
*
* @brief   Read two register bytes
*
* @param  uint8 addr - address
*
* @return  uint16 - result
*/
uint16 CC2520_REGRD16(uint8 addr)
{
    EWORD value;
    CC2520_SPI_BEGIN();
    CC2520_SPI_TXRX(CC2520_INS_REGRD | addr);
    value.b.b0 = CC2520_SPI_TXRX(0x00);
    value.b.b1 = CC2520_SPI_TXRX(0x00);
    CC2520_SPI_END();
    return value.w;
}


/***********************************************************************************
* @fn      CC2520_REGRD24
*
* @brief   Read three register bytes
*
* @param  uint8 addr - address
*
* @return  uint32 - result
*/
uint32 CC2520_REGRD24(uint8 addr)
{
    EDWORD value;
    CC2520_SPI_BEGIN();
    CC2520_SPI_TXRX(CC2520_INS_REGRD | addr);
    value.b.b0 = CC2520_SPI_TXRX(0x00);
    value.b.b1 = CC2520_SPI_TXRX(0x00);
    value.b.b2 = CC2520_SPI_TXRX(0x00);
    value.b.b3 = 0x00;
    CC2520_SPI_END();
    return value.dw;
}


/***********************************************************************************
* @fn      CC2520_REGWR
*
* @brief   Register write. Can only be started from addresses below 0x40
*
* @param  uint8 addr - address
*         uint8 count - number of bytes
*         uint8  *pValues - data buffer
*
* @return  uint8 - status byte
*/
uint8 CC2520_REGWR(uint8 addr, uint8 count, uint8  *pValues)
{
    uint8 s;
    CC2520_SPI_BEGIN();
    s = CC2520_SPI_TXRX(CC2520_INS_REGWR | addr);
    CC2520_INS_WR_ARRAY(count, pValues);
    CC2520_SPI_END();
    return s;
}


/***********************************************************************************
* @fn      CC2520_REGWR8
*
* @brief   Write one register byte
*
* @param  uint8 addr - address
*         uint8 value
*
* @return  none
*/
void CC2520_REGWR8(uint8 addr, uint8 value)
{
    CC2520_SPI_BEGIN();
    CC2520_SPI_TXRX(CC2520_INS_REGWR | addr);
    CC2520_SPI_TXRX(value);
    CC2520_SPI_END();
    return;
}


/***********************************************************************************
* @fn      CC2520_REGWR16
*
* @brief   Write two register bytes
*
* @param  uint8 addr - address
*         uint16 value
*
* @return  none
*/
void CC2520_REGWR16(uint8 addr, uint16 value)
{
    CC2520_SPI_BEGIN();
    CC2520_SPI_TXRX(CC2520_INS_REGWR | addr);
    CC2520_SPI_TXRX(LOBYTE(value));
    CC2520_SPI_TXRX(HIBYTE(value));
    CC2520_SPI_END();
}


/***********************************************************************************
* @fn      CC2520_REGWR24
*
* @brief   Write three register bytes
*
* @param  uint8 addr
*         uint32 value
*
* @return  none
*/
void CC2520_REGWR24(uint8 addr, uint32 value)
{
    CC2520_SPI_BEGIN();
    CC2520_SPI_TXRX(CC2520_INS_REGWR | addr);
    CC2520_SPI_TXRX(LOBYTE(LOWORD(value)));
    CC2520_SPI_TXRX(HIBYTE(LOWORD(value)));
    CC2520_SPI_TXRX(LOBYTE(HIWORD(value)));
    CC2520_SPI_END();
}



/***********************************************************************************
  Copyright 2007 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED 揂S IS

⌨️ 快捷键说明

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