Hi Team,
I am trying to modify several GPIO interfaces of the CC2642 to SPI interfaces to drive my SPI device (RC522 card reader). The SPI interface names and pin definitions I want to use are as follows:
// SPI Pin Definitions
#define NFC_SPI_MISO IOID_14 // MISO pin for NFC SPI
#define NFC_SPI_MOSI IOID_15 // MOSI pin for NFC SPI
#define NFC_SPI_CLK IOID_25 // Clock pin for NFC SPI
#define NFC_SPI_CS IOID_26 // Chip select pin for NFC SPI
My development board is CC26X2R1_LAUNCHXL, and the compilation environment is CCS10.1.1. I have rewritten an SPI driver, including two files, board_spi.c and board_spi.h (see attachment), and also added an SPI interface defined according to my pin definition in .syscfg. However, after downloading it to the development board, I am unable to perform SPI communication with other devices from the pins I defined. I have tried many methods, but it seems that this self-defined SPI interface can never be opened. I would like to ask if there is a problem with my driver. Or is there some other setting I did wrong? Thank you for your help.
/********************************************************************* * INCLUDES */ #include "board_spi.h" /********************* Macro Definitions ************************/ // Enumeration for SPI instances on CC2642R1 typedef enum CC2642R1_SPIName { CC2642R1_SPI0 = 0, CC2642R1_SPICOUNT } CC2642R1_SPIName; // SPI objects for CC2642R1 SPICC26X2DMA_Object NFC_spiCC26X2DMAObjects[CC2642R1_SPICOUNT]; /********************* Internal Variables ************************/ // Hardware attributes for SPI on CC2642R1 const SPICC26X2DMA_HWAttrs NFC_spiCC26X2DMAHWAttrs[CC2642R1_SPICOUNT] = { { .baseAddr = SSI0_BASE, // Base address for SPI .intNum = INT_SSI0_COMB, // Interrupt number .intPriority = ~0, // Interrupt priority .swiPriority = 0, // Software interrupt priority .powerMngrId = PowerCC26XX_PERIPH_SSI0, // Power manager ID .defaultTxBufValue = 0, // Default value for transmit buffer .rxChannelBitMask = 1<<UDMA_CHAN_SSI0_RX, // Bit mask for RX channel .txChannelBitMask = 1<<UDMA_CHAN_SSI0_TX, // Bit mask for TX channel .mosiPin = NFC_SPI_MOSI, // MOSI pin configuration .misoPin = NFC_SPI_MISO, // MISO pin configuration .clkPin = NFC_SPI_CLK, // Clock pin configuration .csnPin = NFC_SPI_CS // Chip select pin configuration } }; // SPI configuration for CC2642R1 const SPI_Config SPI0_config[CC2642R1_SPICOUNT] = { { .fxnTablePtr = &SPICC26X2DMA_fxnTable, // Pointer to function table .object = &NFC_spiCC26X2DMAObjects[CC2642R1_SPI0], // Pointer to SPI object .hwAttrs = &NFC_spiCC26X2DMAHWAttrs[CC2642R1_SPI0] // Pointer to hardware attributes }, }; // Count of SPI instances const uint_least8_t SPI0_count = CC2642R1_SPICOUNT; // SPI interface //static PIN_Handle NFC_hspiPin = NULL; // Deprecated: Handle for SPI pin (not used) static SPI_Handle NFC_spiHandle = NULL; // Handle for SPI static SPI_Params NFC_spiParams; // Parameters for SPI /********************* Internal Function Declarations ************************/ // Declaration for internal function to open SPI0 static bool NFC_SPI0_Open(NFC_U32 NFC_bitRate); //********************************************************************** // Name: NFC_SPI0_Open // Description: Opens the SPI interface // Parameters: NFC_bitRate - Desired SPI speed // Return: True if operation is successful, false otherwise // Author: 甜甜的大香瓜 // Email: [email] // QQ group: 香瓜BLE之CC2640R2F(557278427) // Shop: opengua.taobao.com // Change time: 2021.08.02 //********************************************************************** bool NFC_SPI0_Open(NFC_U32 NFC_bitRate) { /* Configure SPI as master */ SPI_Params_init(&NFC_spiParams); NFC_spiParams.frameFormat = SPI_POL0_PHA1; // Frame format configuration NFC_spiParams.bitRate = NFC_bitRate; // Set SPI speed NFC_spiParams.mode = SPI_MASTER; // Set SPI mode as master NFC_spiParams.transferMode = SPI_MODE_BLOCKING; // Set transfer mode /* Attempt to open SPI. */ NFC_spiHandle = SPI_open(CC2642R1_SPI0, &NFC_spiParams); return NFC_spiHandle != NULL; } /********************************************************************* * LOCAL VARIABLES */ // SPI handle and parameters //static SPI_Handle s_spiHandle; // Deprecated: SPI handle (not used) //static SPI_Params s_spiParams; // Deprecated: SPI parameters (not used) static SPI_Transaction s_spiTransaction; // SPI transaction structure // SPI chip select pin static PIN_State s_spiCsnState; // State for chip select pin static PIN_Handle s_spiCsnHandle; // Handle for chip select pin static PIN_Config s_spiCsnPinsCfg[] = // Configuration for chip select pins { NFC_SPI_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN, PIN_TERMINATE }; /********************************************************************* * PUBLIC FUNCTIONS */ /** @brief Initialization function for SPI @param None @return None */ bool SPI_Init(void) { bool nNFC_f; // Initialization of SPI chip select handle s_spiCsnHandle = PIN_open(&s_spiCsnState, s_spiCsnPinsCfg); // Initialize SPI module SPI_init(); // Open SPI0 with specified bit rate nNFC_f = NFC_SPI0_Open(NFC_SPI0_RATE); return nNFC_f; } /** @brief Sets the SPI chip select signal @param pinId -[in] Pin number @param pinState -[in] Pin state @return None */ void SPI_CsnSet(PIN_Id pinId, bool pinState) { if(pinState == NFC_SPI_CS_OFF) { PIN_setOutputValue(s_spiCsnHandle, pinId, 1); // Set pin high } else if(pinState == NFC_SPI_CS_ON) { PIN_setOutputValue(s_spiCsnHandle, pinId, 0); // Set pin low } } /** @brief Performs SPI read/write operation @param pWriteData -[in] Data to write @param pReadData -[out] Data read @param writeDataLen -[in] Length of data to write @return None */ void SPI_ReadWriteData(uint8_t *pWriteData, uint8_t *pReadData, uint8_t writeDataLen) { bool transferOk; s_spiTransaction.count = writeDataLen; // Length of data to write s_spiTransaction.arg = NULL; s_spiTransaction.txBuf = pWriteData; // Transmit buffer s_spiTransaction.rxBuf = pReadData; // Receive buffer // Start SPI transfer transferOk = SPI_transfer(NFC_spiHandle, &s_spiTransaction); if(!transferOk) { // Handle error in SPI transfer or if transfer already in progress. } } /*************************************END OF FILE*************************************/