📄 mrfi_spi.c
字号:
/**************************************************************************************************
Revised: $Date: 2007-07-06 11:19:00 -0700 (Fri, 06 Jul 2007) $
Revision: $Revision: 13579 $
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 WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE
THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY
INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST
DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY
THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
Should you have any questions regarding your right to use this Software,
contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/
/* ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
* MRFI (Minimal RF Interface)
* Radios: CC2500, CC1100, CC1101
* SPI interface code.
* ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*/
/* ------------------------------------------------------------------------------------------------
* Includes
* ------------------------------------------------------------------------------------------------
*/
//#include "bsp_external/mrfi_board_defs.h"
#include "mrfi_spi.h"
#include "app.h"
/* ------------------------------------------------------------------------------------------------
* Defines
* ------------------------------------------------------------------------------------------------
*/
#define DUMMY_BYTE 0xDB
#define READ_BIT 0x80
#define BURST_BIT 0x40
/* ------------------------------------------------------------------------------------------------
* Macros
* ------------------------------------------------------------------------------------------------
*/
#define MRFI_SPI_TURN_CHIP_SELECT_ON() GPIO_ResetBits(CSPort, CSPICSN)//MRFI_SPI_DRIVE_CSN_LOW()
#define MRFI_SPI_TURN_CHIP_SELECT_OFF() GPIO_SetBits(CSPort, CSPICSN)//MRFI_SPI_DRIVE_CSN_HIGH()
#define MRFI_SPI_CHIP_SELECT_IS_OFF() (GPIO_ReadInput(CSPort) & CSPICSN)//MRFI_SPI_CSN_IS_HIGH()
#define MRFI_SPI_DRIVE_CSN_LOW() GPIO_ReSetBits(CSPort, CSPICSN)
#define MRFI_SPI_DRIVE_CSN_HIGH() GPIO_setBits(CSPort, CSPICSN)
#define MRFI_SPI_DEBUG
#ifdef MRFI_SPI_DEBUG
//#define MRFI_SPI_ASSERT(x) BSP_ASSERT(x)
#define MRFI_SPI_ASSERT(x) while(!(x))
#else
#define MRFI_SPI_ASSERT(x)
#endif
/* ------------------------------------------------------------------------------------------------
* Local Prototypes
* ------------------------------------------------------------------------------------------------
*/
static uint8_t spiRegAccess(uint8_t addrByte, uint8_t writeValue);
static void spiBurstFifoAccess(uint8_t addrByte, uint8_t * pData, uint8_t len);
/**************************************************************************************************
* @fn mrfiSpiInit
*
* @brief Initialize SPI.
*
* @param none
*
* @return none
**************************************************************************************************
*/
void mrfiSpiInit(void)
{
// /* configure all SPI related pins */
// MRFI_SPI_CONFIG_CSN_PIN_AS_OUTPUT();
// MRFI_SPI_CONFIG_SCLK_PIN_AS_OUTPUT();
// MRFI_SPI_CONFIG_SI_PIN_AS_OUTPUT();
// MRFI_SPI_CONFIG_SO_PIN_AS_INPUT();
//
// /* set CSn to default high level */
// MRFI_SPI_DRIVE_CSN_HIGH();
//
// /* initialize the SPI registers */
// MRFI_SPI_INIT();
}
/**************************************************************************************************
* @fn mrfiSpiCmdStrobe
*
* @brief Send command strobe to the radio. Returns status byte read during transfer
* of strobe command.
*
* @param addr - address of register to strobe
*
* @return status byte of radio
**************************************************************************************************
*/
uint8_t mrfiSpiCmdStrobe(uint8_t addr)
{
uint8_t statusByte;
mrfiSpiIState_t s=0;
//MRFI_SPI_ASSERT( MRFI_SPI_IS_INITIALIZED() ); /* SPI is not initialized */
MRFI_SPI_ASSERT((addr >= 0x30) && (addr <= 0x3D)); /* invalid address */
/* disable interrupts that use SPI */
MRFI_SPI_ENTER_CRITICAL_SECTION(s);
/* turn chip select "off" and then "on" to clear any current SPI access */
MRFI_SPI_TURN_CHIP_SELECT_OFF();
MRFI_SPI_TURN_CHIP_SELECT_ON();
/* send the command strobe, wait for SPI access to complete */
// MRFI_SPI_WRITE_BYTE(addr);
// MRFI_SPI_WAIT_DONE();
while ( (LPC_SSP0->SR & (SSP_SR_TNF|SSP_SR_BSY)) != SSP_SR_TNF );
LPC_SSP0->DR = addr;
//MRFI_DELAY(5);
/* read the readio status byte returned by the command strobe */
// statusByte = MRFI_SPI_READ_BYTE();
while ( (LPC_SSP0->SR & (SSP_SR_BSY|SSP_SR_RNE)) != SSP_SR_RNE );
statusByte = LPC_SSP0->DR;
mrfiRadioState = statusByte;
// MRFI_DELAY(20);
// MRFI_DELAY(20);
// while ( (LPC_SSP0->SR & (SSP_SR_TNF|SSP_SR_BSY)) != SSP_SR_TNF );
// LPC_SSP0->DR = addr;
// while ( (LPC_SSP0->SR & (SSP_SR_BSY|SSP_SR_RNE)) != SSP_SR_RNE );
// statusByte1 = LPC_SSP0->DR;
/* turn off chip select; enable interrupts that call SPI functions */
MRFI_SPI_TURN_CHIP_SELECT_OFF();
MRFI_SPI_EXIT_CRITICAL_SECTION(s);
/* return the status byte */
return(statusByte);
}
/**************************************************************************************************
* @fn mrfiSpiReadReg
*
* @brief Read value from radio register.
*
* @param addr - address of register
*
* @return register value
**************************************************************************************************
*/
uint8_t mrfiSpiReadReg(uint8_t addr)
{
MRFI_SPI_ASSERT(addr <= 0x3B); /* invalid address */
/*
* The burst bit is set to allow access to read-only status registers.
* This does not affect normal register reads.
*/
return( spiRegAccess(addr | BURST_BIT | READ_BIT, DUMMY_BYTE) );
}
/**************************************************************************************************
* @fn mrfiSpiWriteReg
*
* @brief Write value to radio register.
*
* @param addr - address of register
* @param value - register value to write
*
* @return none
**************************************************************************************************
*/
void mrfiSpiWriteReg(uint8_t addr, uint8_t value)
{
MRFI_SPI_ASSERT((addr <= 0x2E) || (addr == 0x3E)); /* invalid address */
spiRegAccess(addr, value);
}
/*=================================================================================================
* @fn spiRegAccess
*
* @brief This function performs a read or write. The
* calling code must configure the read/write bit of the register's address byte.
* This bit is set or cleared based on the type of access.
*
* @param regAddrByte - address byte of register; the read/write bit already configured
*
* @return register value
*=================================================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -