What am I doing wrong in spi communication program?[kl46z]

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What am I doing wrong in spi communication program?[kl46z]

867 Views
fedeblanc
Contributor I

I'm trying to use spi communication to transfer/receive data from a Dragino LoraBee module based on sx1276/78, which has an rfm95_96w embedded [attached].

I'm using KSDK 2.3.0 library driver example to use interrupts. The project configures SPI0 to use as master and SPI0 as slave, so I connected a transceiver to spi0 and the other to spi1, but something is not working because the debug is not showing any transference.

This is the code:

/////////////////////////////////////////////////////////MAIN CODE//////////////////////////////////////////////////////////////

#define BUFFER_SIZE (64)
static uint8_t srcBuff[BUFFER_SIZE];
static uint8_t destBuff[BUFFER_SIZE];
static uint32_t masterIndex = BUFFER_SIZE;
static uint32_t slaveIndex = BUFFER_SIZE;
static volatile bool masterFinished = false;
static volatile bool slaveFinished = false;

/*******************************************************************************
* Code
******************************************************************************/

void SPI_MASTER_IRQHandler(void)
{
         if (SPI_GetStatusFlags(EXAMPLE_SPI_MASTER) & kSPI_TxBufferEmptyFlag)
         {
                     SPI_WriteData(EXAMPLE_SPI_MASTER, (uint16_t)(srcBuff[BUFFER_SIZE - masterIndex]));
                     masterIndex--;
         }
         if (masterIndex == 0U) 
         {
                  masterFinished = true;
                  SPI_DisableInterrupts(EXAMPLE_SPI_MASTER, kSPI_TxEmptyInterruptEnable);
         }
}

void SPI_SLAVE_IRQHandler(void)
{
         if (SPI_GetStatusFlags(EXAMPLE_SPI_SLAVE) & kSPI_RxBufferFullFlag)
         {
                  destBuff[BUFFER_SIZE - slaveIndex] = SPI_ReadData(EXAMPLE_SPI_SLAVE);
                  slaveIndex--;
          }
           if (slaveIndex == 0U)
           {
                      slaveFinished = true;
                      SPI_DisableInterrupts(EXAMPLE_SPI_SLAVE, kSPI_RxFullAndModfInterruptEnable);
           }
}

int main(void)
{
spi_master_config_t masterConfig = {0};
spi_slave_config_t slaveConfig = {0};
uint32_t sourceClock = 0U;
uint32_t i = 0U;
uint32_t err = 0U;

/* Init the boards */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();

PRINTF("\r\nSPI one board interrupt example started!\r\n");

/* Init SPI master */
/*
* masterConfig.enableStopInWaitMode = false;
* masterConfig.polarity = kSPI_ClockPolarityActiveHigh;
* masterConfig.phase = kSPI_ClockPhaseFirstEdge;
* masterConfig.direction = kSPI_MsbFirst;
* masterConfig.dataMode = kSPI_8BitMode;
* masterConfig.txWatermark = kSPI_TxFifoOneHalfEmpty;
* masterConfig.rxWatermark = kSPI_RxFifoOneHalfFull;
* masterConfig.pinMode = kSPI_PinModeNormal;
* masterConfig.outputMode = kSPI_SlaveSelectAutomaticOutput;
* masterConfig.baudRate_Bps = 500000U;
*/
SPI_MasterGetDefaultConfig(&masterConfig);
sourceClock = EXAMPLE_SPI_MASTER_CLK_FREQ;
SPI_MasterInit(EXAMPLE_SPI_MASTER, &masterConfig, sourceClock);

/* Init SPI slave */
/*
* slaveConfig.polarity = kSPI_ClockPolarityActiveHigh;
* slaveConfig.phase = kSPI_ClockPhaseFirstEdge;
* slaveConfig.direction = kSPI_MsbFirst;
* slaveConfig.enableStopInWaitMode = false;
* slaveConfig.dataMode = kSPI_8BitMode;
* slaveConfig.txWatermark = kSPI_TxFifoOneHalfEmpty;
* slaveConfig.rxWatermark = kSPI_RxFifoOneHalfFull;
*/
SPI_SlaveGetDefaultConfig(&slaveConfig);
SPI_SlaveInit(EXAMPLE_SPI_SLAVE, &slaveConfig);

/* Disable FIFO */
#if defined(FSL_FEATURE_SPI_HAS_FIFO) && (FSL_FEATURE_SPI_HAS_FIFO)
SPI_EnableFIFO(EXAMPLE_SPI_MASTER, false);
SPI_EnableFIFO(EXAMPLE_SPI_SLAVE, false);
#endif /* FSL_FEATURE_SPI_HAS_FIFO */

/* Set priority, slave have higher priority */
NVIC_SetPriority(EXAMPLE_SPI_MASTER_IRQ, 1U);
NVIC_SetPriority(EXAMPLE_SPI_SLAVE_IRQ, 0U);

/* Init source buffer */

for (i = 0U; i < BUFFER_SIZE; i++)
{
           srcBuff[i] = i;
}

/* Enable interrupt, first enable slave and then master. */
EnableIRQ(EXAMPLE_SPI_MASTER_IRQ);
EnableIRQ(EXAMPLE_SPI_SLAVE_IRQ);
SPI_EnableInterrupts(EXAMPLE_SPI_SLAVE, kSPI_RxFullAndModfInterruptEnable);
SPI_EnableInterrupts(EXAMPLE_SPI_MASTER, kSPI_TxEmptyInterruptEnable);

while ((masterFinished != true) || (slaveFinished != true))
{
}

/* Check the data received */
for (i = 0U; i < BUFFER_SIZE; i++)
{
           if (destBuff[i] != srcBuff[i])
{

PRINTF("\r\nThe %d data is wrong, the data received is %d \r\n", i, destBuff[i]);
err++;
}
}
if (err == 0U)
{
           PRINTF("\r\nSPI transfer finished!\r\n");
}

while (1)
{
}

}

////////////////////////////////////////////////////PIN CONFIG (BOARD_InitPins)///////////////////////////////////////////////////////

void BOARD_InitPins(void)
{
         /* Port A Clock Gate Control: Clock enabled */
         CLOCK_EnableClock(kCLOCK_PortA);

         /* Port D Clock Gate Control: Clock enabled */
         CLOCK_EnableClock(kCLOCK_PortD);

/* PORTA1 (pin 35) is configured as UART0_RX */
PORT_SetPinMux(PORTA, 1U, kPORT_MuxAlt2);

/* PORTA14 (pin 44) is configured as SPI0_PCS0 */
PORT_SetPinMux(PORTA, 14U, kPORT_MuxAlt2);

/* PORTA15 (pin 45) is configured as SPI0_SCK */
PORT_SetPinMux(PORTA, 15U, kPORT_MuxAlt2);

/* PORTA16 (pin 46) is configured as SPI0_MOSI */
PORT_SetPinMux(PORTA, 16U, kPORT_MuxAlt2);

/* PORTA17 (pin 47) is configured as SPI0_MISO */
PORT_SetPinMux(PORTA, 17U, kPORT_MuxAlt2);

/* PORTA2 (pin 36) is configured as UART0_TX */
PORT_SetPinMux(PORTA, 2U, kPORT_MuxAlt2);

/* PORTD4 (pin 97) is configured as SPI1_PCS0 */
PORT_SetPinMux(PORTD, 4U, kPORT_MuxAlt2);

/* PORTD5 (pin 98) is configured as SPI1_SCK */
PORT_SetPinMux(PORTD, 5U, kPORT_MuxAlt2);

/* PORTD6 (pin 99) is configured as SPI1_MOSI */
PORT_SetPinMux(PORTD, 6U, kPORT_MuxAlt2);

/* PORTD7 (pin 100) is configured as SPI1_MISO */
PORT_SetPinMux(PORTD, 7U, kPORT_MuxAlt2);

SIM->SOPT5 = ((SIM->SOPT5 &
/* Mask bits to zero which are setting */
(~(SIM_SOPT5_UART0TXSRC_MASK | SIM_SOPT5_UART0RXSRC_MASK)))

/* UART0 Transmit Data Source Select: UART0_TX pin. */
| SIM_SOPT5_UART0TXSRC(SOPT5_UART0TXSRC_UART_TX)

/* UART0 Receive Data Source Select: UART_RX pin. */
| SIM_SOPT5_UART0RXSRC(SOPT5_UART0RXSRC_UART_RX));

}

Here I attach the project (I've used MCUxpresso 10) and some useful images (board and module pinout).

I'm new at programming embeddeds so I'd appreciate if someone can give me a hand.

Labels (1)
0 Kudos
Reply
1 Reply

568 Views
jingpan
NXP TechSupport
NXP TechSupport

Hi Fede,

Please refer to the SDK_2.3_FRDM-KL46Z\boards\frdmkl46z\driver_examples\spi\polling_transfer. This example take SPI0 as master and SPI1 as slaver, just same to your case.

Regards

Jing

0 Kudos
Reply